blob: d468403a0854289ae61e4f01998cf043896a579e [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));
578static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000580static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000594static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100599static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000601static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000613#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200614static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000615static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
616#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200617#ifdef FEAT_LUA
618static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
619#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000624static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200625static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000626static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000627static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000629static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000633#ifdef vim_mkdir
634static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100637#ifdef FEAT_MZSCHEME
638static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100642static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000643static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000644#ifdef FEAT_FLOAT
645static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000648static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000649static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200650#ifdef FEAT_PYTHON3
651static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
652#endif
653#ifdef FEAT_PYTHON
654static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
655#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000657static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000658static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#ifdef FEAT_FLOAT
671static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
672#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200673static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100675static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000678static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000680static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000687static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000688static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000689static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000690static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200692static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000693static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100695#ifdef FEAT_CRYPT
696static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
697#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000698static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200699static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000701#ifdef FEAT_FLOAT
702static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200703static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000706static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000707static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000710#ifdef FEAT_FLOAT
711static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
713#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000714static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200715static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716#ifdef HAVE_STRFTIME
717static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
718#endif
719static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200725static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200726static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000732static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200733static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200735static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000736static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000737static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000738static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000739static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000740static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000742static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200743#ifdef FEAT_FLOAT
744static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
746#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000750#ifdef FEAT_FLOAT
751static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
752#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200754static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200755static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100756static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100760static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000767static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
768static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000770static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100771static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772
Bram Moolenaar493c1782014-05-28 14:34:46 +0200773static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000774static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static int get_env_len __ARGS((char_u **arg));
776static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000777static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000778static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
779#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
780#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
781 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000782static 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 +0000783static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000784static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100785static 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 +0000786static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static typval_T *alloc_tv __ARGS((void));
788static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static void init_tv __ARGS((typval_T *varp));
790static long get_tv_number __ARGS((typval_T *varp));
791static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000792static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793static char_u *get_tv_string __ARGS((typval_T *varp));
794static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000795static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100796static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
797static 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 +0000798static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
799static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
800static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000801static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
802static 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 +0000803static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
804static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000805static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200806static int var_check_func_name __ARGS((char_u *name, int new_var));
807static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000808static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000809static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000810static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
811static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
812static int eval_fname_script __ARGS((char_u *p));
813static int eval_fname_sid __ARGS((char_u *p));
814static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000815static ufunc_T *find_func __ARGS((char_u *name));
816static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200817static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#ifdef FEAT_PROFILE
819static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000820static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
821static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
822static int
823# ifdef __BORLANDC__
824 _RTLENTRYF
825# endif
826 prof_total_cmp __ARGS((const void *s1, const void *s2));
827static int
828# ifdef __BORLANDC__
829 _RTLENTRYF
830# endif
831 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000832#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200833static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000834static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000835static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000836static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static 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 +0000838static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
839static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000840static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000841static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
842static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000843static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000844static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000845static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200846static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200847static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000848
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200849
850#ifdef EBCDIC
851static int compare_func_name __ARGS((const void *s1, const void *s2));
852static void sortFunctions __ARGS(());
853#endif
854
Bram Moolenaar33570922005-01-25 22:26:29 +0000855/*
856 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000857 */
858 void
859eval_init()
860{
Bram Moolenaar33570922005-01-25 22:26:29 +0000861 int i;
862 struct vimvar *p;
863
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200864 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
865 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200866 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000867 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000868 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000869
870 for (i = 0; i < VV_LEN; ++i)
871 {
872 p = &vimvars[i];
873 STRCPY(p->vv_di.di_key, p->vv_name);
874 if (p->vv_flags & VV_RO)
875 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
876 else if (p->vv_flags & VV_RO_SBX)
877 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
878 else
879 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000880
881 /* add to v: scope dict, unless the value is not always available */
882 if (p->vv_type != VAR_UNKNOWN)
883 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000885 /* add to compat scope dict */
886 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000887 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000888 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100889 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200890 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200891
892#ifdef EBCDIC
893 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100894 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200895 */
896 sortFunctions();
897#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000898}
899
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000900#if defined(EXITFREE) || defined(PROTO)
901 void
902eval_clear()
903{
904 int i;
905 struct vimvar *p;
906
907 for (i = 0; i < VV_LEN; ++i)
908 {
909 p = &vimvars[i];
910 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000911 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000912 vim_free(p->vv_str);
913 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000914 }
915 else if (p->vv_di.di_tv.v_type == VAR_LIST)
916 {
917 list_unref(p->vv_list);
918 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000919 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000920 }
921 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000922 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000923 hash_clear(&compat_hashtab);
924
Bram Moolenaard9fba312005-06-26 22:34:35 +0000925 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100926# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200927 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100928# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000929
930 /* global variables */
931 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000932
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000933 /* autoloaded script names */
934 ga_clear_strings(&ga_loaded);
935
Bram Moolenaarcca74132013-09-25 21:00:28 +0200936 /* Script-local variables. First clear all the variables and in a second
937 * loop free the scriptvar_T, because a variable in one script might hold
938 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200939 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200940 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200941 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200942 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200943 ga_clear(&ga_scripts);
944
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000945 /* unreferenced lists and dicts */
946 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000947
948 /* functions */
949 free_all_functions();
950 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000951}
952#endif
953
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 * Return the name of the executed function.
956 */
957 char_u *
958func_name(cookie)
959 void *cookie;
960{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000961 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962}
963
964/*
965 * Return the address holding the next breakpoint line for a funccall cookie.
966 */
967 linenr_T *
968func_breakpoint(cookie)
969 void *cookie;
970{
Bram Moolenaar33570922005-01-25 22:26:29 +0000971 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972}
973
974/*
975 * Return the address holding the debug tick for a funccall cookie.
976 */
977 int *
978func_dbg_tick(cookie)
979 void *cookie;
980{
Bram Moolenaar33570922005-01-25 22:26:29 +0000981 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982}
983
984/*
985 * Return the nesting level for a funccall cookie.
986 */
987 int
988func_level(cookie)
989 void *cookie;
990{
Bram Moolenaar33570922005-01-25 22:26:29 +0000991 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992}
993
994/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000995funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000997/* pointer to list of previously used funccal, still around because some
998 * item in it is still being used. */
999funccall_T *previous_funccal = NULL;
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001/*
1002 * Return TRUE when a function was ended by a ":return" command.
1003 */
1004 int
1005current_func_returned()
1006{
1007 return current_funccal->returned;
1008}
1009
1010
1011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * Set an internal variable to a string value. Creates the variable if it does
1013 * not already exist.
1014 */
1015 void
1016set_internal_string_var(name, value)
1017 char_u *name;
1018 char_u *value;
1019{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001020 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001021 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
1023 val = vim_strsave(value);
1024 if (val != NULL)
1025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001027 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001029 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001030 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032 }
1033}
1034
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001035static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001036static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001037static char_u *redir_endp = NULL;
1038static char_u *redir_varname = NULL;
1039
1040/*
1041 * Start recording command output to a variable
1042 * Returns OK if successfully completed the setup. FAIL otherwise.
1043 */
1044 int
1045var_redir_start(name, append)
1046 char_u *name;
1047 int append; /* append to an existing variable */
1048{
1049 int save_emsg;
1050 int err;
1051 typval_T tv;
1052
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001053 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001054 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 {
1056 EMSG(_(e_invarg));
1057 return FAIL;
1058 }
1059
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001060 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 redir_varname = vim_strsave(name);
1062 if (redir_varname == NULL)
1063 return FAIL;
1064
1065 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1066 if (redir_lval == NULL)
1067 {
1068 var_redir_stop();
1069 return FAIL;
1070 }
1071
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001072 /* The output is stored in growarray "redir_ga" until redirection ends. */
1073 ga_init2(&redir_ga, (int)sizeof(char), 500);
1074
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001076 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001077 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1079 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001080 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 if (redir_endp != NULL && *redir_endp != NUL)
1082 /* Trailing characters are present after the variable name */
1083 EMSG(_(e_trailing));
1084 else
1085 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001086 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 var_redir_stop();
1088 return FAIL;
1089 }
1090
1091 /* check if we can write to the variable: set it to or append an empty
1092 * string */
1093 save_emsg = did_emsg;
1094 did_emsg = FALSE;
1095 tv.v_type = VAR_STRING;
1096 tv.vval.v_string = (char_u *)"";
1097 if (append)
1098 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1099 else
1100 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001101 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001103 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 if (err)
1105 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001106 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 var_redir_stop();
1108 return FAIL;
1109 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
1111 return OK;
1112}
1113
1114/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 * Append "value[value_len]" to the variable set by var_redir_start().
1116 * The actual appending is postponed until redirection ends, because the value
1117 * appended may in fact be the string we write to, changing it may cause freed
1118 * memory to be used:
1119 * :redir => foo
1120 * :let foo
1121 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122 */
1123 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129
1130 if (redir_lval == NULL)
1131 return;
1132
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001134 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001136 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001138 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139 {
1140 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001141 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 }
1143 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145}
1146
1147/*
1148 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 */
1151 void
1152var_redir_stop()
1153{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001154 typval_T tv;
1155
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 if (redir_lval != NULL)
1157 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* If there was no error: assign the text to the variable. */
1159 if (redir_endp != NULL)
1160 {
1161 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1162 tv.v_type = VAR_STRING;
1163 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001164 /* Call get_lval() again, if it's inside a Dict or List it may
1165 * have changed. */
1166 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001167 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001168 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1169 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1170 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001171 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001172
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001173 /* free the collected output */
1174 vim_free(redir_ga.ga_data);
1175 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001176
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177 vim_free(redir_lval);
1178 redir_lval = NULL;
1179 }
1180 vim_free(redir_varname);
1181 redir_varname = NULL;
1182}
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184# if defined(FEAT_MBYTE) || defined(PROTO)
1185 int
1186eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1187 char_u *enc_from;
1188 char_u *enc_to;
1189 char_u *fname_from;
1190 char_u *fname_to;
1191{
1192 int err = FALSE;
1193
1194 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1195 set_vim_var_string(VV_CC_TO, enc_to, -1);
1196 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1197 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1198 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1199 err = TRUE;
1200 set_vim_var_string(VV_CC_FROM, NULL, -1);
1201 set_vim_var_string(VV_CC_TO, NULL, -1);
1202 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1203 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1204
1205 if (err)
1206 return FAIL;
1207 return OK;
1208}
1209# endif
1210
1211# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1212 int
1213eval_printexpr(fname, args)
1214 char_u *fname;
1215 char_u *args;
1216{
1217 int err = FALSE;
1218
1219 set_vim_var_string(VV_FNAME_IN, fname, -1);
1220 set_vim_var_string(VV_CMDARG, args, -1);
1221 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1222 err = TRUE;
1223 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1224 set_vim_var_string(VV_CMDARG, NULL, -1);
1225
1226 if (err)
1227 {
1228 mch_remove(fname);
1229 return FAIL;
1230 }
1231 return OK;
1232}
1233# endif
1234
1235# if defined(FEAT_DIFF) || defined(PROTO)
1236 void
1237eval_diff(origfile, newfile, outfile)
1238 char_u *origfile;
1239 char_u *newfile;
1240 char_u *outfile;
1241{
1242 int err = FALSE;
1243
1244 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1245 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1246 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1247 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1248 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1249 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1250 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1251}
1252
1253 void
1254eval_patch(origfile, difffile, outfile)
1255 char_u *origfile;
1256 char_u *difffile;
1257 char_u *outfile;
1258{
1259 int err;
1260
1261 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1262 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1263 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1264 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1265 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1266 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1267 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1268}
1269# endif
1270
1271/*
1272 * Top level evaluation function, returning a boolean.
1273 * Sets "error" to TRUE if there was an error.
1274 * Return TRUE or FALSE.
1275 */
1276 int
1277eval_to_bool(arg, error, nextcmd, skip)
1278 char_u *arg;
1279 int *error;
1280 char_u **nextcmd;
1281 int skip; /* only parse, don't execute */
1282{
Bram Moolenaar33570922005-01-25 22:26:29 +00001283 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 int retval = FALSE;
1285
1286 if (skip)
1287 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 else
1291 {
1292 *error = FALSE;
1293 if (!skip)
1294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001295 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001296 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298 }
1299 if (skip)
1300 --emsg_skip;
1301
1302 return retval;
1303}
1304
1305/*
1306 * Top level evaluation function, returning a string. If "skip" is TRUE,
1307 * only parsing to "nextcmd" is done, without reporting errors. Return
1308 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1309 */
1310 char_u *
1311eval_to_string_skip(arg, nextcmd, skip)
1312 char_u *arg;
1313 char_u **nextcmd;
1314 int skip; /* only parse, don't execute */
1315{
Bram Moolenaar33570922005-01-25 22:26:29 +00001316 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 char_u *retval;
1318
1319 if (skip)
1320 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001321 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 retval = NULL;
1323 else
1324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001325 retval = vim_strsave(get_tv_string(&tv));
1326 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328 if (skip)
1329 --emsg_skip;
1330
1331 return retval;
1332}
1333
1334/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001335 * Skip over an expression at "*pp".
1336 * Return FAIL for an error, OK otherwise.
1337 */
1338 int
1339skip_expr(pp)
1340 char_u **pp;
1341{
Bram Moolenaar33570922005-01-25 22:26:29 +00001342 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001343
1344 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001346}
1347
1348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350 * When "convert" is TRUE convert a List into a sequence of lines and convert
1351 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * Return pointer to allocated memory, or NULL for failure.
1353 */
1354 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 char_u *arg;
1357 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
Bram Moolenaar33570922005-01-25 22:26:29 +00001360 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001362 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001363#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 retval = NULL;
1369 else
1370 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001372 {
1373 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001374 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001375 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001376 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001377 if (tv.vval.v_list->lv_len > 0)
1378 ga_append(&ga, NL);
1379 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001380 ga_append(&ga, NUL);
1381 retval = (char_u *)ga.ga_data;
1382 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001383#ifdef FEAT_FLOAT
1384 else if (convert && tv.v_type == VAR_FLOAT)
1385 {
1386 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1387 retval = vim_strsave(numbuf);
1388 }
1389#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001390 else
1391 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394
1395 return retval;
1396}
1397
1398/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001399 * Call eval_to_string() without using current local variables and using
1400 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 */
1402 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001403eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 char_u *arg;
1405 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
1408 char_u *retval;
1409 void *save_funccalp;
1410
1411 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001412 if (use_sandbox)
1413 ++sandbox;
1414 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001415 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001416 if (use_sandbox)
1417 --sandbox;
1418 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 restore_funccal(save_funccalp);
1420 return retval;
1421}
1422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423/*
1424 * Top level evaluation function, returning a number.
1425 * Evaluates "expr" silently.
1426 * Returns -1 for an error.
1427 */
1428 int
1429eval_to_number(expr)
1430 char_u *expr;
1431{
Bram Moolenaar33570922005-01-25 22:26:29 +00001432 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001434 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
1436 ++emsg_off;
1437
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001438 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 retval = -1;
1440 else
1441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001442 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001443 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445 --emsg_off;
1446
1447 return retval;
1448}
1449
Bram Moolenaara40058a2005-07-11 22:42:07 +00001450/*
1451 * Prepare v: variable "idx" to be used.
1452 * Save the current typeval in "save_tv".
1453 * When not used yet add the variable to the v: hashtable.
1454 */
1455 static void
1456prepare_vimvar(idx, save_tv)
1457 int idx;
1458 typval_T *save_tv;
1459{
1460 *save_tv = vimvars[idx].vv_tv;
1461 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1462 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1463}
1464
1465/*
1466 * Restore v: variable "idx" to typeval "save_tv".
1467 * When no longer defined, remove the variable from the v: hashtable.
1468 */
1469 static void
1470restore_vimvar(idx, save_tv)
1471 int idx;
1472 typval_T *save_tv;
1473{
1474 hashitem_T *hi;
1475
Bram Moolenaara40058a2005-07-11 22:42:07 +00001476 vimvars[idx].vv_tv = *save_tv;
1477 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1478 {
1479 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1480 if (HASHITEM_EMPTY(hi))
1481 EMSG2(_(e_intern2), "restore_vimvar()");
1482 else
1483 hash_remove(&vimvarht, hi);
1484 }
1485}
1486
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001487#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001488/*
1489 * Evaluate an expression to a list with suggestions.
1490 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001491 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001492 */
1493 list_T *
1494eval_spell_expr(badword, expr)
1495 char_u *badword;
1496 char_u *expr;
1497{
1498 typval_T save_val;
1499 typval_T rettv;
1500 list_T *list = NULL;
1501 char_u *p = skipwhite(expr);
1502
1503 /* Set "v:val" to the bad word. */
1504 prepare_vimvar(VV_VAL, &save_val);
1505 vimvars[VV_VAL].vv_type = VAR_STRING;
1506 vimvars[VV_VAL].vv_str = badword;
1507 if (p_verbose == 0)
1508 ++emsg_off;
1509
1510 if (eval1(&p, &rettv, TRUE) == OK)
1511 {
1512 if (rettv.v_type != VAR_LIST)
1513 clear_tv(&rettv);
1514 else
1515 list = rettv.vval.v_list;
1516 }
1517
1518 if (p_verbose == 0)
1519 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001520 restore_vimvar(VV_VAL, &save_val);
1521
1522 return list;
1523}
1524
1525/*
1526 * "list" is supposed to contain two items: a word and a number. Return the
1527 * word in "pp" and the number as the return value.
1528 * Return -1 if anything isn't right.
1529 * Used to get the good word and score from the eval_spell_expr() result.
1530 */
1531 int
1532get_spellword(list, pp)
1533 list_T *list;
1534 char_u **pp;
1535{
1536 listitem_T *li;
1537
1538 li = list->lv_first;
1539 if (li == NULL)
1540 return -1;
1541 *pp = get_tv_string(&li->li_tv);
1542
1543 li = li->li_next;
1544 if (li == NULL)
1545 return -1;
1546 return get_tv_number(&li->li_tv);
1547}
1548#endif
1549
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001550/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 * Top level evaluation function.
1552 * Returns an allocated typval_T with the result.
1553 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001554 */
1555 typval_T *
1556eval_expr(arg, nextcmd)
1557 char_u *arg;
1558 char_u **nextcmd;
1559{
1560 typval_T *tv;
1561
1562 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001563 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001564 {
1565 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001566 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001567 }
1568
1569 return tv;
1570}
1571
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001575 * Uses argv[argc] for the function arguments. Only Number and String
1576 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001577 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001579 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001580call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 char_u *func;
1582 int argc;
1583 char_u **argv;
1584 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001585 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
Bram Moolenaar33570922005-01-25 22:26:29 +00001588 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 long n;
1590 int len;
1591 int i;
1592 int doesrange;
1593 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001594 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001596 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001598 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
1600 for (i = 0; i < argc; i++)
1601 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001602 /* Pass a NULL or empty argument as an empty string */
1603 if (argv[i] == NULL || *argv[i] == NUL)
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_STRING;
1606 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001607 continue;
1608 }
1609
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001610 if (str_arg_only)
1611 len = 0;
1612 else
1613 /* Recognize a number argument, the others must be strings. */
1614 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (len != 0 && len == (int)STRLEN(argv[i]))
1616 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001617 argvars[i].v_type = VAR_NUMBER;
1618 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
1620 else
1621 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001622 argvars[i].v_type = VAR_STRING;
1623 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 }
1625 }
1626
1627 if (safe)
1628 {
1629 save_funccalp = save_funccal();
1630 ++sandbox;
1631 }
1632
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1634 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (safe)
1638 {
1639 --sandbox;
1640 restore_funccal(save_funccalp);
1641 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 vim_free(argvars);
1643
1644 if (ret == FAIL)
1645 clear_tv(rettv);
1646
1647 return ret;
1648}
1649
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001650/*
1651 * Call vimL function "func" and return the result as a number.
1652 * Returns -1 when calling the function fails.
1653 * Uses argv[argc] for the function arguments.
1654 */
1655 long
1656call_func_retnr(func, argc, argv, safe)
1657 char_u *func;
1658 int argc;
1659 char_u **argv;
1660 int safe; /* use the sandbox */
1661{
1662 typval_T rettv;
1663 long retval;
1664
1665 /* All arguments are passed as strings, no conversion to number. */
1666 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1667 return -1;
1668
1669 retval = get_tv_number_chk(&rettv, NULL);
1670 clear_tv(&rettv);
1671 return retval;
1672}
1673
1674#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1675 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1676
Bram Moolenaar4f688582007-07-24 12:34:30 +00001677# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001679 * Call vimL function "func" and return the result as a string.
1680 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 * Uses argv[argc] for the function arguments.
1682 */
1683 void *
1684call_func_retstr(func, argc, argv, safe)
1685 char_u *func;
1686 int argc;
1687 char_u **argv;
1688 int safe; /* use the sandbox */
1689{
1690 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001691 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001693 /* All arguments are passed as strings, no conversion to number. */
1694 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 return NULL;
1696
1697 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 return retval;
1700}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001701# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001703/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001704 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001706 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 */
1708 void *
1709call_func_retlist(func, argc, argv, safe)
1710 char_u *func;
1711 int argc;
1712 char_u **argv;
1713 int safe; /* use the sandbox */
1714{
1715 typval_T rettv;
1716
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001717 /* All arguments are passed as strings, no conversion to number. */
1718 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719 return NULL;
1720
1721 if (rettv.v_type != VAR_LIST)
1722 {
1723 clear_tv(&rettv);
1724 return NULL;
1725 }
1726
1727 return rettv.vval.v_list;
1728}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#endif
1730
1731/*
1732 * Save the current function call pointer, and set it to NULL.
1733 * Used when executing autocommands and for ":source".
1734 */
1735 void *
1736save_funccal()
1737{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001738 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 current_funccal = NULL;
1741 return (void *)fc;
1742}
1743
1744 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001745restore_funccal(vfc)
1746 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001748 funccall_T *fc = (funccall_T *)vfc;
1749
1750 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751}
1752
Bram Moolenaar05159a02005-02-26 23:04:13 +00001753#if defined(FEAT_PROFILE) || defined(PROTO)
1754/*
1755 * Prepare profiling for entering a child or something else that is not
1756 * counted for the script/function itself.
1757 * Should always be called in pair with prof_child_exit().
1758 */
1759 void
1760prof_child_enter(tm)
1761 proftime_T *tm; /* place to store waittime */
1762{
1763 funccall_T *fc = current_funccal;
1764
1765 if (fc != NULL && fc->func->uf_profiling)
1766 profile_start(&fc->prof_child);
1767 script_prof_save(tm);
1768}
1769
1770/*
1771 * Take care of time spent in a child.
1772 * Should always be called after prof_child_enter().
1773 */
1774 void
1775prof_child_exit(tm)
1776 proftime_T *tm; /* where waittime was stored */
1777{
1778 funccall_T *fc = current_funccal;
1779
1780 if (fc != NULL && fc->func->uf_profiling)
1781 {
1782 profile_end(&fc->prof_child);
1783 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1784 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1785 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1786 }
1787 script_prof_restore(tm);
1788}
1789#endif
1790
1791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792#ifdef FEAT_FOLDING
1793/*
1794 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1795 * it in "*cp". Doesn't give error messages.
1796 */
1797 int
1798eval_foldexpr(arg, cp)
1799 char_u *arg;
1800 int *cp;
1801{
Bram Moolenaar33570922005-01-25 22:26:29 +00001802 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 int retval;
1804 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001805 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1806 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001809 if (use_sandbox)
1810 ++sandbox;
1811 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 retval = 0;
1815 else
1816 {
1817 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 if (tv.v_type == VAR_NUMBER)
1819 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001820 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 retval = 0;
1822 else
1823 {
1824 /* If the result is a string, check if there is a non-digit before
1825 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (!VIM_ISDIGIT(*s) && *s != '-')
1828 *cp = *s++;
1829 retval = atol((char *)s);
1830 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001834 if (use_sandbox)
1835 --sandbox;
1836 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
1838 return retval;
1839}
1840#endif
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 * ":let" list all variable values
1844 * ":let var1 var2" list variable values
1845 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001846 * ":let var += expr" assignment command.
1847 * ":let var -= expr" assignment command.
1848 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 */
1851 void
1852ex_let(eap)
1853 exarg_T *eap;
1854{
1855 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 int var_count = 0;
1860 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001861 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001862 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001863 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
Bram Moolenaardb552d602006-03-23 22:59:57 +00001865 argend = skip_var_list(arg, &var_count, &semicolon);
1866 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001868 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1869 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001870 expr = skipwhite(argend);
1871 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1872 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001874 /*
1875 * ":let" without "=": list variables
1876 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 if (*arg == '[')
1878 EMSG(_(e_invarg));
1879 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001881 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001885 list_glob_vars(&first);
1886 list_buf_vars(&first);
1887 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001888#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001889 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001890#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001891 list_script_vars(&first);
1892 list_func_vars(&first);
1893 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 eap->nextcmd = check_nextcmd(arg);
1896 }
1897 else
1898 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 op[0] = '=';
1900 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001901 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001902 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001903 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1904 op[0] = *expr; /* +=, -= or .= */
1905 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001907 else
1908 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (eap->skip)
1911 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (eap->skip)
1914 {
1915 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 --emsg_skip;
1918 }
1919 else if (i != FAIL)
1920 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925 }
1926}
1927
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928/*
1929 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1930 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 * When "nextchars" is not NULL it points to a string with characters that
1932 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1933 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 * Returns OK or FAIL;
1935 */
1936 static int
1937ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1938 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940 int copy; /* copy values from "tv", don't move */
1941 int semicolon; /* from skip_var_list() */
1942 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944{
1945 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001946 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 listitem_T *item;
1949 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950
1951 if (*arg != '[')
1952 {
1953 /*
1954 * ":let var = expr" or ":for var in list"
1955 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 return FAIL;
1958 return OK;
1959 }
1960
1961 /*
1962 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1963 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001964 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 {
1966 EMSG(_(e_listreq));
1967 return FAIL;
1968 }
1969
1970 i = list_len(l);
1971 if (semicolon == 0 && var_count < i)
1972 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001973 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 return FAIL;
1975 }
1976 if (var_count - semicolon > i)
1977 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001978 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 return FAIL;
1980 }
1981
1982 item = l->lv_first;
1983 while (*arg != ']')
1984 {
1985 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 item = item->li_next;
1988 if (arg == NULL)
1989 return FAIL;
1990
1991 arg = skipwhite(arg);
1992 if (*arg == ';')
1993 {
1994 /* Put the rest of the list (may be empty) in the var after ';'.
1995 * Create a new list for this. */
1996 l = list_alloc();
1997 if (l == NULL)
1998 return FAIL;
1999 while (item != NULL)
2000 {
2001 list_append_tv(l, &item->li_tv);
2002 item = item->li_next;
2003 }
2004
2005 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002006 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 ltv.vval.v_list = l;
2008 l->lv_refcount = 1;
2009
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002010 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2011 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 clear_tv(&ltv);
2013 if (arg == NULL)
2014 return FAIL;
2015 break;
2016 }
2017 else if (*arg != ',' && *arg != ']')
2018 {
2019 EMSG2(_(e_intern2), "ex_let_vars()");
2020 return FAIL;
2021 }
2022 }
2023
2024 return OK;
2025}
2026
2027/*
2028 * Skip over assignable variable "var" or list of variables "[var, var]".
2029 * Used for ":let varvar = expr" and ":for varvar in expr".
2030 * For "[var, var]" increment "*var_count" for each variable.
2031 * for "[var, var; var]" set "semicolon".
2032 * Return NULL for an error.
2033 */
2034 static char_u *
2035skip_var_list(arg, var_count, semicolon)
2036 char_u *arg;
2037 int *var_count;
2038 int *semicolon;
2039{
2040 char_u *p, *s;
2041
2042 if (*arg == '[')
2043 {
2044 /* "[var, var]": find the matching ']'. */
2045 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002046 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 {
2048 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2049 s = skip_var_one(p);
2050 if (s == p)
2051 {
2052 EMSG2(_(e_invarg2), p);
2053 return NULL;
2054 }
2055 ++*var_count;
2056
2057 p = skipwhite(s);
2058 if (*p == ']')
2059 break;
2060 else if (*p == ';')
2061 {
2062 if (*semicolon == 1)
2063 {
2064 EMSG(_("Double ; in list of variables"));
2065 return NULL;
2066 }
2067 *semicolon = 1;
2068 }
2069 else if (*p != ',')
2070 {
2071 EMSG2(_(e_invarg2), p);
2072 return NULL;
2073 }
2074 }
2075 return p + 1;
2076 }
2077 else
2078 return skip_var_one(arg);
2079}
2080
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002081/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002082 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002083 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002084 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002085 static char_u *
2086skip_var_one(arg)
2087 char_u *arg;
2088{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002089 if (*arg == '@' && arg[1] != NUL)
2090 return arg + 2;
2091 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2092 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093}
2094
Bram Moolenaara7043832005-01-21 11:56:39 +00002095/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002096 * List variables for hashtab "ht" with prefix "prefix".
2097 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002101 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002103 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002105{
Bram Moolenaar33570922005-01-25 22:26:29 +00002106 hashitem_T *hi;
2107 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002108 int todo;
2109
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002110 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2112 {
2113 if (!HASHITEM_EMPTY(hi))
2114 {
2115 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002116 di = HI2DI(hi);
2117 if (empty || di->di_tv.v_type != VAR_STRING
2118 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 }
2121 }
2122}
2123
2124/*
2125 * List global variables.
2126 */
2127 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128list_glob_vars(first)
2129 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002131 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002132}
2133
2134/*
2135 * List buffer variables.
2136 */
2137 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138list_buf_vars(first)
2139 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002140{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002141 char_u numbuf[NUMBUFLEN];
2142
Bram Moolenaar429fa852013-04-15 12:27:36 +02002143 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145
2146 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2148 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002149}
2150
2151/*
2152 * List window variables.
2153 */
2154 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155list_win_vars(first)
2156 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002157{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002158 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002160}
2161
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002162#ifdef FEAT_WINDOWS
2163/*
2164 * List tab page variables.
2165 */
2166 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167list_tab_vars(first)
2168 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002169{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002170 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002172}
2173#endif
2174
Bram Moolenaara7043832005-01-21 11:56:39 +00002175/*
2176 * List Vim variables.
2177 */
2178 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_vim_vars(first)
2180 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183}
2184
2185/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186 * List script-local variables, if there is a script.
2187 */
2188 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189list_script_vars(first)
2190 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002191{
2192 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2194 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002195}
2196
2197/*
2198 * List function variables, if there is a function.
2199 */
2200 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201list_func_vars(first)
2202 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002203{
2204 if (current_funccal != NULL)
2205 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002207}
2208
2209/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 * List variables in "arg".
2211 */
2212 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002213list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 exarg_T *eap;
2215 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217{
2218 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 char_u *name_start;
2222 char_u *arg_subsc;
2223 char_u *tofree;
2224 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225
2226 while (!ends_excmd(*arg) && !got_int)
2227 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002230 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2232 {
2233 emsg_severe = TRUE;
2234 EMSG(_(e_trailing));
2235 break;
2236 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 /* get_name_len() takes care of expanding curly braces */
2241 name_start = name = arg;
2242 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2243 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 /* This is mainly to keep test 49 working: when expanding
2246 * curly braces fails overrule the exception error message. */
2247 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 emsg_severe = TRUE;
2250 EMSG2(_(e_invarg2), arg);
2251 break;
2252 }
2253 error = TRUE;
2254 }
2255 else
2256 {
2257 if (tofree != NULL)
2258 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002259 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 else
2262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 /* handle d.key, l[idx], f(expr) */
2264 arg_subsc = arg;
2265 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002266 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002268 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002272 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 case 'g': list_glob_vars(first); break;
2274 case 'b': list_buf_vars(first); break;
2275 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002276#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002278#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002279 case 'v': list_vim_vars(first); break;
2280 case 's': list_script_vars(first); break;
2281 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 default:
2283 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
2286 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287 {
2288 char_u numbuf[NUMBUFLEN];
2289 char_u *tf;
2290 int c;
2291 char_u *s;
2292
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002293 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 c = *arg;
2295 *arg = NUL;
2296 list_one_var_a((char_u *)"",
2297 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002298 tv.v_type,
2299 s == NULL ? (char_u *)"" : s,
2300 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 *arg = c;
2302 vim_free(tf);
2303 }
2304 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002305 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 }
2307 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308
2309 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311
2312 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 }
2314
2315 return arg;
2316}
2317
2318/*
2319 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2320 * Returns a pointer to the char just after the var name.
2321 * Returns NULL if there is an error.
2322 */
2323 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002326 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330{
2331 int c1;
2332 char_u *name;
2333 char_u *p;
2334 char_u *arg_end = NULL;
2335 int len;
2336 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338
2339 /*
2340 * ":let $VAR = expr": Set environment variable.
2341 */
2342 if (*arg == '$')
2343 {
2344 /* Find the end of the name. */
2345 ++arg;
2346 name = arg;
2347 len = get_env_len(&arg);
2348 if (len == 0)
2349 EMSG2(_(e_invarg2), name - 1);
2350 else
2351 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 if (op != NULL && (*op == '+' || *op == '-'))
2353 EMSG2(_(e_letwrong), op);
2354 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002355 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002357 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 {
2359 c1 = name[len];
2360 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002361 p = get_tv_string_chk(tv);
2362 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 {
2364 int mustfree = FALSE;
2365 char_u *s = vim_getenv(name, &mustfree);
2366
2367 if (s != NULL)
2368 {
2369 p = tofree = concat_str(s, p);
2370 if (mustfree)
2371 vim_free(s);
2372 }
2373 }
2374 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002375 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002377 if (STRICMP(name, "HOME") == 0)
2378 init_homedir();
2379 else if (didset_vim && STRICMP(name, "VIM") == 0)
2380 didset_vim = FALSE;
2381 else if (didset_vimruntime
2382 && STRICMP(name, "VIMRUNTIME") == 0)
2383 didset_vimruntime = FALSE;
2384 arg_end = arg;
2385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 }
2389 }
2390 }
2391
2392 /*
2393 * ":let &option = expr": Set option value.
2394 * ":let &l:option = expr": Set local option value.
2395 * ":let &g:option = expr": Set global option value.
2396 */
2397 else if (*arg == '&')
2398 {
2399 /* Find the end of the name. */
2400 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002401 if (p == NULL || (endchars != NULL
2402 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 EMSG(_(e_letunexp));
2404 else
2405 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406 long n;
2407 int opt_type;
2408 long numval;
2409 char_u *stringval = NULL;
2410 char_u *s;
2411
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 c1 = *p;
2413 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414
2415 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 s = get_tv_string_chk(tv); /* != NULL if number or string */
2417 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 {
2419 opt_type = get_option_value(arg, &numval,
2420 &stringval, opt_flags);
2421 if ((opt_type == 1 && *op == '.')
2422 || (opt_type == 0 && *op != '.'))
2423 EMSG2(_(e_letwrong), op);
2424 else
2425 {
2426 if (opt_type == 1) /* number */
2427 {
2428 if (*op == '+')
2429 n = numval + n;
2430 else
2431 n = numval - n;
2432 }
2433 else if (opt_type == 0 && stringval != NULL) /* string */
2434 {
2435 s = concat_str(stringval, s);
2436 vim_free(stringval);
2437 stringval = s;
2438 }
2439 }
2440 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 if (s != NULL)
2442 {
2443 set_option_value(arg, n, s, opt_flags);
2444 arg_end = p;
2445 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 }
2449 }
2450
2451 /*
2452 * ":let @r = expr": Set register contents.
2453 */
2454 else if (*arg == '@')
2455 {
2456 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 if (op != NULL && (*op == '+' || *op == '-'))
2458 EMSG2(_(e_letwrong), op);
2459 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002460 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 EMSG(_(e_letunexp));
2462 else
2463 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002464 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 char_u *s;
2466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 p = get_tv_string_chk(tv);
2468 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002470 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 if (s != NULL)
2472 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 vim_free(s);
2475 }
2476 }
2477 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002480 arg_end = arg + 1;
2481 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002482 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
2484 }
2485
2486 /*
2487 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002490 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002492 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002494 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2498 EMSG(_(e_letunexp));
2499 else
2500 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 arg_end = p;
2503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 }
2507
2508 else
2509 EMSG2(_(e_invarg2), arg);
2510
2511 return arg_end;
2512}
2513
2514/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002515 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2516 */
2517 static int
2518check_changedtick(arg)
2519 char_u *arg;
2520{
2521 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2522 {
2523 EMSG2(_(e_readonlyvar), arg);
2524 return TRUE;
2525 }
2526 return FALSE;
2527}
2528
2529/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 * Get an lval: variable, Dict item or List item that can be assigned a value
2531 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2532 * "name.key", "name.key[expr]" etc.
2533 * Indexing only works if "name" is an existing List or Dictionary.
2534 * "name" points to the start of the name.
2535 * If "rettv" is not NULL it points to the value to be assigned.
2536 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2537 * wrong; must end in space or cmd separator.
2538 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539 * flags:
2540 * GLV_QUIET: do not give error messages
2541 * GLV_NO_AUTOLOAD: do not use script autoloading
2542 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002544 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 */
2547 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002549 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 typval_T *rettv;
2551 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 int unlet;
2553 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002554 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002555 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 char_u *expr_start, *expr_end;
2559 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 dictitem_T *v;
2561 typval_T var1;
2562 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002568 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572
2573 if (skip)
2574 {
2575 /* When skipping just find the end of the name. */
2576 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002577 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 }
2579
2580 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002581 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 if (expr_start != NULL)
2583 {
2584 /* Don't expand the name when we already know there is an error. */
2585 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2586 && *p != '[' && *p != '.')
2587 {
2588 EMSG(_(e_trailing));
2589 return NULL;
2590 }
2591
2592 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2593 if (lp->ll_exp_name == NULL)
2594 {
2595 /* Report an invalid expression in braces, unless the
2596 * expression evaluation has been cancelled due to an
2597 * aborting error, an interrupt, or an exception. */
2598 if (!aborting() && !quiet)
2599 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002600 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 EMSG2(_(e_invarg2), name);
2602 return NULL;
2603 }
2604 }
2605 lp->ll_name = lp->ll_exp_name;
2606 }
2607 else
2608 lp->ll_name = name;
2609
2610 /* Without [idx] or .key we are done. */
2611 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2612 return p;
2613
2614 cc = *p;
2615 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002616 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 if (v == NULL && !quiet)
2618 EMSG2(_(e_undefvar), lp->ll_name);
2619 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 if (v == NULL)
2621 return NULL;
2622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /*
2624 * Loop until no more [idx] or .key is following.
2625 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2630 && !(lp->ll_tv->v_type == VAR_DICT
2631 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_("E689: Can only index a List or Dictionary"));
2635 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (!quiet)
2640 EMSG(_("E708: [:] must come last"));
2641 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 len = -1;
2645 if (*p == '.')
2646 {
2647 key = p + 1;
2648 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2649 ;
2650 if (len == 0)
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_(e_emptykey));
2654 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656 p = key + len;
2657 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 else
2659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 if (*p == ':')
2663 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 else
2665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 empty1 = FALSE;
2667 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002669 if (get_tv_string_chk(&var1) == NULL)
2670 {
2671 /* not a number or string */
2672 clear_tv(&var1);
2673 return NULL;
2674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676
2677 /* Optionally get the second index [ :expr]. */
2678 if (*p == ':')
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002683 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 if (rettv != NULL && (rettv->v_type != VAR_LIST
2689 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
2692 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 }
2697 p = skipwhite(p + 1);
2698 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 else
2701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (!empty1)
2706 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709 if (get_tv_string_chk(&var2) == NULL)
2710 {
2711 /* not a number or string */
2712 if (!empty1)
2713 clear_tv(&var1);
2714 clear_tv(&var2);
2715 return NULL;
2716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (!quiet)
2726 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733
2734 /* Skip to past ']'. */
2735 ++p;
2736 }
2737
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 {
2740 if (len == -1)
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (*key == NUL)
2745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (!quiet)
2747 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 lp->ll_list = NULL;
2753 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002754 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002755
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 /* When assigning to a scope dictionary check that a function and
2757 * variable name is valid (only variable name unless it is l: or
2758 * g: dictionary). Disallow overwriting a builtin function. */
2759 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002760 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002761 int prevval;
2762 int wrong;
2763
2764 if (len != -1)
2765 {
2766 prevval = key[len];
2767 key[len] = NUL;
2768 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002769 else
2770 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002771 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2772 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 || !valid_varname(key);
2775 if (len != -1)
2776 key[len] = prevval;
2777 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002778 return NULL;
2779 }
2780
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 /* Can't add "v:" variable. */
2784 if (lp->ll_dict == &vimvardict)
2785 {
2786 EMSG2(_(e_illvar), name);
2787 return NULL;
2788 }
2789
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002790 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 if (len == -1)
2796 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 }
2799 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 if (len == -1)
2804 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 p = NULL;
2807 break;
2808 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002809 /* existing variable, need to check if it can be changed */
2810 else if (var_check_ro(lp->ll_di->di_flags, name))
2811 return NULL;
2812
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 if (len == -1)
2814 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 }
2817 else
2818 {
2819 /*
2820 * Get the number and item for the only or first index of the List.
2821 */
2822 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 else
2825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002826 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 clear_tv(&var1);
2828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_list = lp->ll_tv->vval.v_list;
2831 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2832 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002834 if (lp->ll_n1 < 0)
2835 {
2836 lp->ll_n1 = 0;
2837 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2838 }
2839 }
2840 if (lp->ll_li == NULL)
2841 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002844 if (!quiet)
2845 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 }
2848
2849 /*
2850 * May need to find the item or absolute index for the second
2851 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 * When no index given: "lp->ll_empty2" is TRUE.
2853 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002857 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002863 {
2864 if (!quiet)
2865 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2872 if (lp->ll_n1 < 0)
2873 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2874 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002875 {
2876 if (!quiet)
2877 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002879 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 }
2881
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 }
2885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return p;
2887}
2888
2889/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002890 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 */
2892 static void
2893clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895{
2896 vim_free(lp->ll_exp_name);
2897 vim_free(lp->ll_newkey);
2898}
2899
2900/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002901 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 */
2905 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002907 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002911 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912{
2913 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 listitem_T *ri;
2915 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916
2917 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002918 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 cc = *endp;
2922 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002923 if (op != NULL && *op != '=')
2924 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002925 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002927 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002928 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002929 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 {
2931 if (tv_op(&tv, rettv, op) == OK)
2932 set_var(lp->ll_name, &tv, FALSE);
2933 clear_tv(&tv);
2934 }
2935 }
2936 else
2937 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002941 else if (tv_check_lock(lp->ll_newkey == NULL
2942 ? lp->ll_tv->v_lock
2943 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2944 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 else if (lp->ll_range)
2946 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002947 listitem_T *ll_li = lp->ll_li;
2948 int ll_n1 = lp->ll_n1;
2949
2950 /*
2951 * Check whether any of the list items is locked
2952 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002953 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002954 {
2955 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name))
2956 return;
2957 ri = ri->li_next;
2958 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2959 break;
2960 ll_li = ll_li->li_next;
2961 ++ll_n1;
2962 }
2963
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 /*
2965 * Assign the List values to the list items.
2966 */
2967 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002968 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002969 if (op != NULL && *op != '=')
2970 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2971 else
2972 {
2973 clear_tv(&lp->ll_li->li_tv);
2974 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 ri = ri->li_next;
2977 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2978 break;
2979 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002980 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002982 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002983 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 ri = NULL;
2985 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002986 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 lp->ll_li = lp->ll_li->li_next;
2989 ++lp->ll_n1;
2990 }
2991 if (ri != NULL)
2992 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 else if (lp->ll_empty2
2994 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 : lp->ll_n1 != lp->ll_n2)
2996 EMSG(_("E711: List value has not enough items"));
2997 }
2998 else
2999 {
3000 /*
3001 * Assign to a List or Dictionary item.
3002 */
3003 if (lp->ll_newkey != NULL)
3004 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 if (op != NULL && *op != '=')
3006 {
3007 EMSG2(_(e_letwrong), op);
3008 return;
3009 }
3010
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003012 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 if (di == NULL)
3014 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003015 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3016 {
3017 vim_free(di);
3018 return;
3019 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003021 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 else if (op != NULL && *op != '=')
3023 {
3024 tv_op(lp->ll_tv, rettv, op);
3025 return;
3026 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003027 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003029
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003030 /*
3031 * Assign the value to the variable or list item.
3032 */
3033 if (copy)
3034 copy_tv(rettv, lp->ll_tv);
3035 else
3036 {
3037 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003038 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003040 }
3041 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003042}
3043
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003044/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003045 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3046 * Returns OK or FAIL.
3047 */
3048 static int
3049tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003050 typval_T *tv1;
3051 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 char_u *op;
3053{
3054 long n;
3055 char_u numbuf[NUMBUFLEN];
3056 char_u *s;
3057
3058 /* Can't do anything with a Funcref or a Dict on the right. */
3059 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3060 {
3061 switch (tv1->v_type)
3062 {
3063 case VAR_DICT:
3064 case VAR_FUNC:
3065 break;
3066
3067 case VAR_LIST:
3068 if (*op != '+' || tv2->v_type != VAR_LIST)
3069 break;
3070 /* List += List */
3071 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3072 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3073 return OK;
3074
3075 case VAR_NUMBER:
3076 case VAR_STRING:
3077 if (tv2->v_type == VAR_LIST)
3078 break;
3079 if (*op == '+' || *op == '-')
3080 {
3081 /* nr += nr or nr -= nr*/
3082 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003083#ifdef FEAT_FLOAT
3084 if (tv2->v_type == VAR_FLOAT)
3085 {
3086 float_T f = n;
3087
3088 if (*op == '+')
3089 f += tv2->vval.v_float;
3090 else
3091 f -= tv2->vval.v_float;
3092 clear_tv(tv1);
3093 tv1->v_type = VAR_FLOAT;
3094 tv1->vval.v_float = f;
3095 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003097#endif
3098 {
3099 if (*op == '+')
3100 n += get_tv_number(tv2);
3101 else
3102 n -= get_tv_number(tv2);
3103 clear_tv(tv1);
3104 tv1->v_type = VAR_NUMBER;
3105 tv1->vval.v_number = n;
3106 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003107 }
3108 else
3109 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003110 if (tv2->v_type == VAR_FLOAT)
3111 break;
3112
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 /* str .= str */
3114 s = get_tv_string(tv1);
3115 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3116 clear_tv(tv1);
3117 tv1->v_type = VAR_STRING;
3118 tv1->vval.v_string = s;
3119 }
3120 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003121
3122#ifdef FEAT_FLOAT
3123 case VAR_FLOAT:
3124 {
3125 float_T f;
3126
3127 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3128 && tv2->v_type != VAR_NUMBER
3129 && tv2->v_type != VAR_STRING))
3130 break;
3131 if (tv2->v_type == VAR_FLOAT)
3132 f = tv2->vval.v_float;
3133 else
3134 f = get_tv_number(tv2);
3135 if (*op == '+')
3136 tv1->vval.v_float += f;
3137 else
3138 tv1->vval.v_float -= f;
3139 }
3140 return OK;
3141#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003142 }
3143 }
3144
3145 EMSG2(_(e_letwrong), op);
3146 return FAIL;
3147}
3148
3149/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150 * Add a watcher to a list.
3151 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003152 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 list_T *l;
3155 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003156{
3157 lw->lw_next = l->lv_watch;
3158 l->lv_watch = lw;
3159}
3160
3161/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003162 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003163 * No warning when it isn't found...
3164 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003165 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003166list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 list_T *l;
3168 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169{
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171
3172 lwp = &l->lv_watch;
3173 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3174 {
3175 if (lw == lwrem)
3176 {
3177 *lwp = lw->lw_next;
3178 break;
3179 }
3180 lwp = &lw->lw_next;
3181 }
3182}
3183
3184/*
3185 * Just before removing an item from a list: advance watchers to the next
3186 * item.
3187 */
3188 static void
3189list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 list_T *l;
3191 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192{
Bram Moolenaar33570922005-01-25 22:26:29 +00003193 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194
3195 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3196 if (lw->lw_item == item)
3197 lw->lw_item = item->li_next;
3198}
3199
3200/*
3201 * Evaluate the expression used in a ":for var in expr" command.
3202 * "arg" points to "var".
3203 * Set "*errp" to TRUE for an error, FALSE otherwise;
3204 * Return a pointer that holds the info. Null when there is an error.
3205 */
3206 void *
3207eval_for_line(arg, errp, nextcmdp, skip)
3208 char_u *arg;
3209 int *errp;
3210 char_u **nextcmdp;
3211 int skip;
3212{
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 typval_T tv;
3216 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217
3218 *errp = TRUE; /* default: there is an error */
3219
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 if (fi == NULL)
3222 return NULL;
3223
3224 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3225 if (expr == NULL)
3226 return fi;
3227
3228 expr = skipwhite(expr);
3229 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3230 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003231 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 return fi;
3233 }
3234
3235 if (skip)
3236 ++emsg_skip;
3237 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3238 {
3239 *errp = FALSE;
3240 if (!skip)
3241 {
3242 l = tv.vval.v_list;
3243 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003244 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003246 clear_tv(&tv);
3247 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 else
3249 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003250 /* No need to increment the refcount, it's already set for the
3251 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 fi->fi_list = l;
3253 list_add_watch(l, &fi->fi_lw);
3254 fi->fi_lw.lw_item = l->lv_first;
3255 }
3256 }
3257 }
3258 if (skip)
3259 --emsg_skip;
3260
3261 return fi;
3262}
3263
3264/*
3265 * Use the first item in a ":for" list. Advance to the next.
3266 * Assign the values to the variable (list). "arg" points to the first one.
3267 * Return TRUE when a valid item was found, FALSE when at end of list or
3268 * something wrong.
3269 */
3270 int
3271next_for_item(fi_void, arg)
3272 void *fi_void;
3273 char_u *arg;
3274{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003275 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278
3279 item = fi->fi_lw.lw_item;
3280 if (item == NULL)
3281 result = FALSE;
3282 else
3283 {
3284 fi->fi_lw.lw_item = item->li_next;
3285 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3286 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3287 }
3288 return result;
3289}
3290
3291/*
3292 * Free the structure used to store info used by ":for".
3293 */
3294 void
3295free_for_info(fi_void)
3296 void *fi_void;
3297{
Bram Moolenaar33570922005-01-25 22:26:29 +00003298 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003300 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003301 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003303 list_unref(fi->fi_list);
3304 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305 vim_free(fi);
3306}
3307
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3309
3310 void
3311set_context_for_expression(xp, arg, cmdidx)
3312 expand_T *xp;
3313 char_u *arg;
3314 cmdidx_T cmdidx;
3315{
3316 int got_eq = FALSE;
3317 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320 if (cmdidx == CMD_let)
3321 {
3322 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003323 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 {
3325 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003326 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 {
3328 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003329 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 if (vim_iswhite(*p))
3331 break;
3332 }
3333 return;
3334 }
3335 }
3336 else
3337 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3338 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 while ((xp->xp_pattern = vim_strpbrk(arg,
3340 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3341 {
3342 c = *xp->xp_pattern;
3343 if (c == '&')
3344 {
3345 c = xp->xp_pattern[1];
3346 if (c == '&')
3347 {
3348 ++xp->xp_pattern;
3349 xp->xp_context = cmdidx != CMD_let || got_eq
3350 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3351 }
3352 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003355 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3356 xp->xp_pattern += 2;
3357
3358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
3360 else if (c == '$')
3361 {
3362 /* environment variable */
3363 xp->xp_context = EXPAND_ENV_VARS;
3364 }
3365 else if (c == '=')
3366 {
3367 got_eq = TRUE;
3368 xp->xp_context = EXPAND_EXPRESSION;
3369 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003370 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 && xp->xp_context == EXPAND_FUNCTIONS
3372 && vim_strchr(xp->xp_pattern, '(') == NULL)
3373 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003374 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 break;
3376 }
3377 else if (cmdidx != CMD_let || got_eq)
3378 {
3379 if (c == '"') /* string */
3380 {
3381 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3382 if (c == '\\' && xp->xp_pattern[1] != NUL)
3383 ++xp->xp_pattern;
3384 xp->xp_context = EXPAND_NOTHING;
3385 }
3386 else if (c == '\'') /* literal string */
3387 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003388 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3390 /* skip */ ;
3391 xp->xp_context = EXPAND_NOTHING;
3392 }
3393 else if (c == '|')
3394 {
3395 if (xp->xp_pattern[1] == '|')
3396 {
3397 ++xp->xp_pattern;
3398 xp->xp_context = EXPAND_EXPRESSION;
3399 }
3400 else
3401 xp->xp_context = EXPAND_COMMANDS;
3402 }
3403 else
3404 xp->xp_context = EXPAND_EXPRESSION;
3405 }
3406 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003407 /* Doesn't look like something valid, expand as an expression
3408 * anyway. */
3409 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 arg = xp->xp_pattern;
3411 if (*arg != NUL)
3412 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3413 /* skip */ ;
3414 }
3415 xp->xp_pattern = arg;
3416}
3417
3418#endif /* FEAT_CMDL_COMPL */
3419
3420/*
3421 * ":1,25call func(arg1, arg2)" function call.
3422 */
3423 void
3424ex_call(eap)
3425 exarg_T *eap;
3426{
3427 char_u *arg = eap->arg;
3428 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003430 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003432 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 linenr_T lnum;
3434 int doesrange;
3435 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003436 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003438 if (eap->skip)
3439 {
3440 /* trans_function_name() doesn't work well when skipping, use eval0()
3441 * instead to skip to any following command, e.g. for:
3442 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003443 ++emsg_skip;
3444 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3445 clear_tv(&rettv);
3446 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003447 return;
3448 }
3449
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003450 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003451 if (fudi.fd_newkey != NULL)
3452 {
3453 /* Still need to give an error message for missing key. */
3454 EMSG2(_(e_dictkey), fudi.fd_newkey);
3455 vim_free(fudi.fd_newkey);
3456 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003457 if (tofree == NULL)
3458 return;
3459
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003460 /* Increase refcount on dictionary, it could get deleted when evaluating
3461 * the arguments. */
3462 if (fudi.fd_dict != NULL)
3463 ++fudi.fd_dict->dv_refcount;
3464
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003465 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003466 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003467 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaar532c7802005-01-27 14:44:31 +00003469 /* Skip white space to allow ":call func ()". Not good, but required for
3470 * backward compatibility. */
3471 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003472 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
3474 if (*startarg != '(')
3475 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003476 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 goto end;
3478 }
3479
3480 /*
3481 * When skipping, evaluate the function once, to find the end of the
3482 * arguments.
3483 * When the function takes a range, this is discovered after the first
3484 * call, and the loop is broken.
3485 */
3486 if (eap->skip)
3487 {
3488 ++emsg_skip;
3489 lnum = eap->line2; /* do it once, also with an invalid range */
3490 }
3491 else
3492 lnum = eap->line1;
3493 for ( ; lnum <= eap->line2; ++lnum)
3494 {
3495 if (!eap->skip && eap->addr_count > 0)
3496 {
3497 curwin->w_cursor.lnum = lnum;
3498 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003499#ifdef FEAT_VIRTUALEDIT
3500 curwin->w_cursor.coladd = 0;
3501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
3503 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003504 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003505 eap->line1, eap->line2, &doesrange,
3506 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 failed = TRUE;
3509 break;
3510 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003511
3512 /* Handle a function returning a Funcref, Dictionary or List. */
3513 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3514 {
3515 failed = TRUE;
3516 break;
3517 }
3518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003519 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (doesrange || eap->skip)
3521 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003522
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003524 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003525 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003526 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 if (aborting())
3528 break;
3529 }
3530 if (eap->skip)
3531 --emsg_skip;
3532
3533 if (!failed)
3534 {
3535 /* Check for trailing illegal characters and a following command. */
3536 if (!ends_excmd(*arg))
3537 {
3538 emsg_severe = TRUE;
3539 EMSG(_(e_trailing));
3540 }
3541 else
3542 eap->nextcmd = check_nextcmd(arg);
3543 }
3544
3545end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003546 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003547 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548}
3549
3550/*
3551 * ":unlet[!] var1 ... " command.
3552 */
3553 void
3554ex_unlet(eap)
3555 exarg_T *eap;
3556{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003557 ex_unletlock(eap, eap->arg, 0);
3558}
3559
3560/*
3561 * ":lockvar" and ":unlockvar" commands
3562 */
3563 void
3564ex_lockvar(eap)
3565 exarg_T *eap;
3566{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003568 int deep = 2;
3569
3570 if (eap->forceit)
3571 deep = -1;
3572 else if (vim_isdigit(*arg))
3573 {
3574 deep = getdigits(&arg);
3575 arg = skipwhite(arg);
3576 }
3577
3578 ex_unletlock(eap, arg, deep);
3579}
3580
3581/*
3582 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3583 */
3584 static void
3585ex_unletlock(eap, argstart, deep)
3586 exarg_T *eap;
3587 char_u *argstart;
3588 int deep;
3589{
3590 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
3595 do
3596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003598 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003599 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003600 if (lv.ll_name == NULL)
3601 error = TRUE; /* error but continue parsing */
3602 if (name_end == NULL || (!vim_iswhite(*name_end)
3603 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003605 if (name_end != NULL)
3606 {
3607 emsg_severe = TRUE;
3608 EMSG(_(e_trailing));
3609 }
3610 if (!(eap->skip || error))
3611 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 break;
3613 }
3614
3615 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616 {
3617 if (eap->cmdidx == CMD_unlet)
3618 {
3619 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3620 error = TRUE;
3621 }
3622 else
3623 {
3624 if (do_lock_var(&lv, name_end, deep,
3625 eap->cmdidx == CMD_lockvar) == FAIL)
3626 error = TRUE;
3627 }
3628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 if (!eap->skip)
3631 clear_lval(&lv);
3632
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 arg = skipwhite(name_end);
3634 } while (!ends_excmd(*arg));
3635
3636 eap->nextcmd = check_nextcmd(arg);
3637}
3638
Bram Moolenaar8c711452005-01-14 21:53:12 +00003639 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003641 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003643 int forceit;
3644{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 int ret = OK;
3646 int cc;
3647
3648 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 cc = *name_end;
3651 *name_end = NUL;
3652
3653 /* Normal name or expanded name. */
3654 if (check_changedtick(lp->ll_name))
3655 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003656 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003659 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003660 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3661 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 else if (lp->ll_range)
3663 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003664 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003665 listitem_T *ll_li = lp->ll_li;
3666 int ll_n1 = lp->ll_n1;
3667
3668 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3669 {
3670 li = ll_li->li_next;
3671 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name))
3672 return FAIL;
3673 ll_li = li;
3674 ++ll_n1;
3675 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676
3677 /* Delete a range of List items. */
3678 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3679 {
3680 li = lp->ll_li->li_next;
3681 listitem_remove(lp->ll_list, lp->ll_li);
3682 lp->ll_li = li;
3683 ++lp->ll_n1;
3684 }
3685 }
3686 else
3687 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 /* unlet a List item. */
3690 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003691 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003693 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003694 }
3695
3696 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003697}
3698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699/*
3700 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003701 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 */
3703 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003704do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003706 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
Bram Moolenaar33570922005-01-25 22:26:29 +00003708 hashtab_T *ht;
3709 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003710 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003711 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaar33570922005-01-25 22:26:29 +00003713 ht = find_var_ht(name, &varname);
3714 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003716 hi = hash_find(ht, varname);
3717 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003718 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003719 di = HI2DI(hi);
3720 if (var_check_fixed(di->di_flags, name)
3721 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003722 return FAIL;
3723 delete_var(ht, hi);
3724 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727 if (forceit)
3728 return OK;
3729 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 return FAIL;
3731}
3732
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003733/*
3734 * Lock or unlock variable indicated by "lp".
3735 * "deep" is the levels to go (-1 for unlimited);
3736 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3737 */
3738 static int
3739do_lock_var(lp, name_end, deep, lock)
3740 lval_T *lp;
3741 char_u *name_end;
3742 int deep;
3743 int lock;
3744{
3745 int ret = OK;
3746 int cc;
3747 dictitem_T *di;
3748
3749 if (deep == 0) /* nothing to do */
3750 return OK;
3751
3752 if (lp->ll_tv == NULL)
3753 {
3754 cc = *name_end;
3755 *name_end = NUL;
3756
3757 /* Normal name or expanded name. */
3758 if (check_changedtick(lp->ll_name))
3759 ret = FAIL;
3760 else
3761 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003762 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003763 if (di == NULL)
3764 ret = FAIL;
3765 else
3766 {
3767 if (lock)
3768 di->di_flags |= DI_FLAGS_LOCK;
3769 else
3770 di->di_flags &= ~DI_FLAGS_LOCK;
3771 item_lock(&di->di_tv, deep, lock);
3772 }
3773 }
3774 *name_end = cc;
3775 }
3776 else if (lp->ll_range)
3777 {
3778 listitem_T *li = lp->ll_li;
3779
3780 /* (un)lock a range of List items. */
3781 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3782 {
3783 item_lock(&li->li_tv, deep, lock);
3784 li = li->li_next;
3785 ++lp->ll_n1;
3786 }
3787 }
3788 else if (lp->ll_list != NULL)
3789 /* (un)lock a List item. */
3790 item_lock(&lp->ll_li->li_tv, deep, lock);
3791 else
3792 /* un(lock) a Dictionary item. */
3793 item_lock(&lp->ll_di->di_tv, deep, lock);
3794
3795 return ret;
3796}
3797
3798/*
3799 * Lock or unlock an item. "deep" is nr of levels to go.
3800 */
3801 static void
3802item_lock(tv, deep, lock)
3803 typval_T *tv;
3804 int deep;
3805 int lock;
3806{
3807 static int recurse = 0;
3808 list_T *l;
3809 listitem_T *li;
3810 dict_T *d;
3811 hashitem_T *hi;
3812 int todo;
3813
3814 if (recurse >= DICT_MAXNEST)
3815 {
3816 EMSG(_("E743: variable nested too deep for (un)lock"));
3817 return;
3818 }
3819 if (deep == 0)
3820 return;
3821 ++recurse;
3822
3823 /* lock/unlock the item itself */
3824 if (lock)
3825 tv->v_lock |= VAR_LOCKED;
3826 else
3827 tv->v_lock &= ~VAR_LOCKED;
3828
3829 switch (tv->v_type)
3830 {
3831 case VAR_LIST:
3832 if ((l = tv->vval.v_list) != NULL)
3833 {
3834 if (lock)
3835 l->lv_lock |= VAR_LOCKED;
3836 else
3837 l->lv_lock &= ~VAR_LOCKED;
3838 if (deep < 0 || deep > 1)
3839 /* recursive: lock/unlock the items the List contains */
3840 for (li = l->lv_first; li != NULL; li = li->li_next)
3841 item_lock(&li->li_tv, deep - 1, lock);
3842 }
3843 break;
3844 case VAR_DICT:
3845 if ((d = tv->vval.v_dict) != NULL)
3846 {
3847 if (lock)
3848 d->dv_lock |= VAR_LOCKED;
3849 else
3850 d->dv_lock &= ~VAR_LOCKED;
3851 if (deep < 0 || deep > 1)
3852 {
3853 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003854 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003855 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3856 {
3857 if (!HASHITEM_EMPTY(hi))
3858 {
3859 --todo;
3860 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3861 }
3862 }
3863 }
3864 }
3865 }
3866 --recurse;
3867}
3868
Bram Moolenaara40058a2005-07-11 22:42:07 +00003869/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003870 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3871 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003872 */
3873 static int
3874tv_islocked(tv)
3875 typval_T *tv;
3876{
3877 return (tv->v_lock & VAR_LOCKED)
3878 || (tv->v_type == VAR_LIST
3879 && tv->vval.v_list != NULL
3880 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3881 || (tv->v_type == VAR_DICT
3882 && tv->vval.v_dict != NULL
3883 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3884}
3885
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3887/*
3888 * Delete all "menutrans_" variables.
3889 */
3890 void
3891del_menutrans_vars()
3892{
Bram Moolenaar33570922005-01-25 22:26:29 +00003893 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003894 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895
Bram Moolenaar33570922005-01-25 22:26:29 +00003896 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003897 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003898 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003899 {
3900 if (!HASHITEM_EMPTY(hi))
3901 {
3902 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003903 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3904 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003905 }
3906 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003907 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908}
3909#endif
3910
3911#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3912
3913/*
3914 * Local string buffer for the next two functions to store a variable name
3915 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3916 * get_user_var_name().
3917 */
3918
3919static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3920
3921static char_u *varnamebuf = NULL;
3922static int varnamebuflen = 0;
3923
3924/*
3925 * Function to concatenate a prefix and a variable name.
3926 */
3927 static char_u *
3928cat_prefix_varname(prefix, name)
3929 int prefix;
3930 char_u *name;
3931{
3932 int len;
3933
3934 len = (int)STRLEN(name) + 3;
3935 if (len > varnamebuflen)
3936 {
3937 vim_free(varnamebuf);
3938 len += 10; /* some additional space */
3939 varnamebuf = alloc(len);
3940 if (varnamebuf == NULL)
3941 {
3942 varnamebuflen = 0;
3943 return NULL;
3944 }
3945 varnamebuflen = len;
3946 }
3947 *varnamebuf = prefix;
3948 varnamebuf[1] = ':';
3949 STRCPY(varnamebuf + 2, name);
3950 return varnamebuf;
3951}
3952
3953/*
3954 * Function given to ExpandGeneric() to obtain the list of user defined
3955 * (global/buffer/window/built-in) variable names.
3956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 char_u *
3958get_user_var_name(xp, idx)
3959 expand_T *xp;
3960 int idx;
3961{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003962 static long_u gdone;
3963 static long_u bdone;
3964 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003965#ifdef FEAT_WINDOWS
3966 static long_u tdone;
3967#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003968 static int vidx;
3969 static hashitem_T *hi;
3970 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971
3972 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003973 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003974 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003975#ifdef FEAT_WINDOWS
3976 tdone = 0;
3977#endif
3978 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003979
3980 /* Global variables */
3981 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003983 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003984 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003985 else
3986 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003987 while (HASHITEM_EMPTY(hi))
3988 ++hi;
3989 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3990 return cat_prefix_varname('g', hi->hi_key);
3991 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003993
3994 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003995 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003998 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003999 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004000 else
4001 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004002 while (HASHITEM_EMPTY(hi))
4003 ++hi;
4004 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004008 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 return (char_u *)"b:changedtick";
4010 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004011
4012 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004013 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004014 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004016 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004017 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004018 else
4019 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004020 while (HASHITEM_EMPTY(hi))
4021 ++hi;
4022 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004025#ifdef FEAT_WINDOWS
4026 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004027 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004028 if (tdone < ht->ht_used)
4029 {
4030 if (tdone++ == 0)
4031 hi = ht->ht_array;
4032 else
4033 ++hi;
4034 while (HASHITEM_EMPTY(hi))
4035 ++hi;
4036 return cat_prefix_varname('t', hi->hi_key);
4037 }
4038#endif
4039
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 /* v: variables */
4041 if (vidx < VV_LEN)
4042 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
4044 vim_free(varnamebuf);
4045 varnamebuf = NULL;
4046 varnamebuflen = 0;
4047 return NULL;
4048}
4049
4050#endif /* FEAT_CMDL_COMPL */
4051
4052/*
4053 * types for expressions.
4054 */
4055typedef enum
4056{
4057 TYPE_UNKNOWN = 0
4058 , TYPE_EQUAL /* == */
4059 , TYPE_NEQUAL /* != */
4060 , TYPE_GREATER /* > */
4061 , TYPE_GEQUAL /* >= */
4062 , TYPE_SMALLER /* < */
4063 , TYPE_SEQUAL /* <= */
4064 , TYPE_MATCH /* =~ */
4065 , TYPE_NOMATCH /* !~ */
4066} exptype_T;
4067
4068/*
4069 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4072 */
4073
4074/*
4075 * Handle zero level expression.
4076 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004078 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 * Return OK or FAIL.
4080 */
4081 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 char_u **nextcmd;
4086 int evaluate;
4087{
4088 int ret;
4089 char_u *p;
4090
4091 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (ret == FAIL || !ends_excmd(*p))
4094 {
4095 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 /*
4098 * Report the invalid expression unless the expression evaluation has
4099 * been cancelled due to an aborting error, an interrupt, or an
4100 * exception.
4101 */
4102 if (!aborting())
4103 EMSG2(_(e_invexpr2), arg);
4104 ret = FAIL;
4105 }
4106 if (nextcmd != NULL)
4107 *nextcmd = check_nextcmd(p);
4108
4109 return ret;
4110}
4111
4112/*
4113 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004114 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 *
4116 * "arg" must point to the first non-white of the expression.
4117 * "arg" is advanced to the next non-white after the recognized expression.
4118 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004119 * Note: "rettv.v_lock" is not set.
4120 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 * Return OK or FAIL.
4122 */
4123 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 int evaluate;
4128{
4129 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004130 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132 /*
4133 * Get the first variable.
4134 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 return FAIL;
4137
4138 if ((*arg)[0] == '?')
4139 {
4140 result = FALSE;
4141 if (evaluate)
4142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143 int error = FALSE;
4144
4145 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004148 if (error)
4149 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 }
4151
4152 /*
4153 * Get the second variable.
4154 */
4155 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158
4159 /*
4160 * Check for the ":".
4161 */
4162 if ((*arg)[0] != ':')
4163 {
4164 EMSG(_("E109: Missing ':' after '?'"));
4165 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 return FAIL;
4168 }
4169
4170 /*
4171 * Get the third variable.
4172 */
4173 *arg = skipwhite(*arg + 1);
4174 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4175 {
4176 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 return FAIL;
4179 }
4180 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183
4184 return OK;
4185}
4186
4187/*
4188 * Handle first level expression:
4189 * expr2 || expr2 || expr2 logical OR
4190 *
4191 * "arg" must point to the first non-white of the expression.
4192 * "arg" is advanced to the next non-white after the recognized expression.
4193 *
4194 * Return OK or FAIL.
4195 */
4196 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 int evaluate;
4201{
Bram Moolenaar33570922005-01-25 22:26:29 +00004202 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 long result;
4204 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
4207 /*
4208 * Get the first variable.
4209 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 return FAIL;
4212
4213 /*
4214 * Repeat until there is no following "||".
4215 */
4216 first = TRUE;
4217 result = FALSE;
4218 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4219 {
4220 if (evaluate && first)
4221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004222 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004225 if (error)
4226 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 first = FALSE;
4228 }
4229
4230 /*
4231 * Get the second variable.
4232 */
4233 *arg = skipwhite(*arg + 2);
4234 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4235 return FAIL;
4236
4237 /*
4238 * Compute the result.
4239 */
4240 if (evaluate && !result)
4241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 if (error)
4246 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248 if (evaluate)
4249 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004250 rettv->v_type = VAR_NUMBER;
4251 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253 }
4254
4255 return OK;
4256}
4257
4258/*
4259 * Handle second level expression:
4260 * expr3 && expr3 && expr3 logical AND
4261 *
4262 * "arg" must point to the first non-white of the expression.
4263 * "arg" is advanced to the next non-white after the recognized expression.
4264 *
4265 * Return OK or FAIL.
4266 */
4267 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 int evaluate;
4272{
Bram Moolenaar33570922005-01-25 22:26:29 +00004273 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 long result;
4275 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
4278 /*
4279 * Get the first variable.
4280 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 return FAIL;
4283
4284 /*
4285 * Repeat until there is no following "&&".
4286 */
4287 first = TRUE;
4288 result = TRUE;
4289 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4290 {
4291 if (evaluate && first)
4292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004293 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004296 if (error)
4297 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 first = FALSE;
4299 }
4300
4301 /*
4302 * Get the second variable.
4303 */
4304 *arg = skipwhite(*arg + 2);
4305 if (eval4(arg, &var2, evaluate && result) == FAIL)
4306 return FAIL;
4307
4308 /*
4309 * Compute the result.
4310 */
4311 if (evaluate && result)
4312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004316 if (error)
4317 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319 if (evaluate)
4320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 rettv->v_type = VAR_NUMBER;
4322 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 }
4325
4326 return OK;
4327}
4328
4329/*
4330 * Handle third level expression:
4331 * var1 == var2
4332 * var1 =~ var2
4333 * var1 != var2
4334 * var1 !~ var2
4335 * var1 > var2
4336 * var1 >= var2
4337 * var1 < var2
4338 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004339 * var1 is var2
4340 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 *
4342 * "arg" must point to the first non-white of the expression.
4343 * "arg" is advanced to the next non-white after the recognized expression.
4344 *
4345 * Return OK or FAIL.
4346 */
4347 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 int evaluate;
4352{
Bram Moolenaar33570922005-01-25 22:26:29 +00004353 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 char_u *p;
4355 int i;
4356 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004357 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 int len = 2;
4359 long n1, n2;
4360 char_u *s1, *s2;
4361 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4362 regmatch_T regmatch;
4363 int ic;
4364 char_u *save_cpo;
4365
4366 /*
4367 * Get the first variable.
4368 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004369 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 return FAIL;
4371
4372 p = *arg;
4373 switch (p[0])
4374 {
4375 case '=': if (p[1] == '=')
4376 type = TYPE_EQUAL;
4377 else if (p[1] == '~')
4378 type = TYPE_MATCH;
4379 break;
4380 case '!': if (p[1] == '=')
4381 type = TYPE_NEQUAL;
4382 else if (p[1] == '~')
4383 type = TYPE_NOMATCH;
4384 break;
4385 case '>': if (p[1] != '=')
4386 {
4387 type = TYPE_GREATER;
4388 len = 1;
4389 }
4390 else
4391 type = TYPE_GEQUAL;
4392 break;
4393 case '<': if (p[1] != '=')
4394 {
4395 type = TYPE_SMALLER;
4396 len = 1;
4397 }
4398 else
4399 type = TYPE_SEQUAL;
4400 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004401 case 'i': if (p[1] == 's')
4402 {
4403 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4404 len = 5;
4405 if (!vim_isIDc(p[len]))
4406 {
4407 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4408 type_is = TRUE;
4409 }
4410 }
4411 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413
4414 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004415 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 */
4417 if (type != TYPE_UNKNOWN)
4418 {
4419 /* extra question mark appended: ignore case */
4420 if (p[len] == '?')
4421 {
4422 ic = TRUE;
4423 ++len;
4424 }
4425 /* extra '#' appended: match case */
4426 else if (p[len] == '#')
4427 {
4428 ic = FALSE;
4429 ++len;
4430 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004431 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 else
4433 ic = p_ic;
4434
4435 /*
4436 * Get the second variable.
4437 */
4438 *arg = skipwhite(p + len);
4439 if (eval5(arg, &var2, evaluate) == FAIL)
4440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004441 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 return FAIL;
4443 }
4444
4445 if (evaluate)
4446 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004447 if (type_is && rettv->v_type != var2.v_type)
4448 {
4449 /* For "is" a different type always means FALSE, for "notis"
4450 * it means TRUE. */
4451 n1 = (type == TYPE_NEQUAL);
4452 }
4453 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4454 {
4455 if (type_is)
4456 {
4457 n1 = (rettv->v_type == var2.v_type
4458 && rettv->vval.v_list == var2.vval.v_list);
4459 if (type == TYPE_NEQUAL)
4460 n1 = !n1;
4461 }
4462 else if (rettv->v_type != var2.v_type
4463 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4464 {
4465 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004466 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004467 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004468 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004469 clear_tv(rettv);
4470 clear_tv(&var2);
4471 return FAIL;
4472 }
4473 else
4474 {
4475 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004476 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4477 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004478 if (type == TYPE_NEQUAL)
4479 n1 = !n1;
4480 }
4481 }
4482
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004483 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4484 {
4485 if (type_is)
4486 {
4487 n1 = (rettv->v_type == var2.v_type
4488 && rettv->vval.v_dict == var2.vval.v_dict);
4489 if (type == TYPE_NEQUAL)
4490 n1 = !n1;
4491 }
4492 else if (rettv->v_type != var2.v_type
4493 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4494 {
4495 if (rettv->v_type != var2.v_type)
4496 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4497 else
4498 EMSG(_("E736: Invalid operation for Dictionary"));
4499 clear_tv(rettv);
4500 clear_tv(&var2);
4501 return FAIL;
4502 }
4503 else
4504 {
4505 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004506 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4507 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004508 if (type == TYPE_NEQUAL)
4509 n1 = !n1;
4510 }
4511 }
4512
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4514 {
4515 if (rettv->v_type != var2.v_type
4516 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4517 {
4518 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004519 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004520 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004521 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004522 clear_tv(rettv);
4523 clear_tv(&var2);
4524 return FAIL;
4525 }
4526 else
4527 {
4528 /* Compare two Funcrefs for being equal or unequal. */
4529 if (rettv->vval.v_string == NULL
4530 || var2.vval.v_string == NULL)
4531 n1 = FALSE;
4532 else
4533 n1 = STRCMP(rettv->vval.v_string,
4534 var2.vval.v_string) == 0;
4535 if (type == TYPE_NEQUAL)
4536 n1 = !n1;
4537 }
4538 }
4539
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004540#ifdef FEAT_FLOAT
4541 /*
4542 * If one of the two variables is a float, compare as a float.
4543 * When using "=~" or "!~", always compare as string.
4544 */
4545 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4546 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4547 {
4548 float_T f1, f2;
4549
4550 if (rettv->v_type == VAR_FLOAT)
4551 f1 = rettv->vval.v_float;
4552 else
4553 f1 = get_tv_number(rettv);
4554 if (var2.v_type == VAR_FLOAT)
4555 f2 = var2.vval.v_float;
4556 else
4557 f2 = get_tv_number(&var2);
4558 n1 = FALSE;
4559 switch (type)
4560 {
4561 case TYPE_EQUAL: n1 = (f1 == f2); break;
4562 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4563 case TYPE_GREATER: n1 = (f1 > f2); break;
4564 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4565 case TYPE_SMALLER: n1 = (f1 < f2); break;
4566 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4567 case TYPE_UNKNOWN:
4568 case TYPE_MATCH:
4569 case TYPE_NOMATCH: break; /* avoid gcc warning */
4570 }
4571 }
4572#endif
4573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 /*
4575 * If one of the two variables is a number, compare as a number.
4576 * When using "=~" or "!~", always compare as string.
4577 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004578 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004581 n1 = get_tv_number(rettv);
4582 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 switch (type)
4584 {
4585 case TYPE_EQUAL: n1 = (n1 == n2); break;
4586 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4587 case TYPE_GREATER: n1 = (n1 > n2); break;
4588 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4589 case TYPE_SMALLER: n1 = (n1 < n2); break;
4590 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4591 case TYPE_UNKNOWN:
4592 case TYPE_MATCH:
4593 case TYPE_NOMATCH: break; /* avoid gcc warning */
4594 }
4595 }
4596 else
4597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 s1 = get_tv_string_buf(rettv, buf1);
4599 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4601 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4602 else
4603 i = 0;
4604 n1 = FALSE;
4605 switch (type)
4606 {
4607 case TYPE_EQUAL: n1 = (i == 0); break;
4608 case TYPE_NEQUAL: n1 = (i != 0); break;
4609 case TYPE_GREATER: n1 = (i > 0); break;
4610 case TYPE_GEQUAL: n1 = (i >= 0); break;
4611 case TYPE_SMALLER: n1 = (i < 0); break;
4612 case TYPE_SEQUAL: n1 = (i <= 0); break;
4613
4614 case TYPE_MATCH:
4615 case TYPE_NOMATCH:
4616 /* avoid 'l' flag in 'cpoptions' */
4617 save_cpo = p_cpo;
4618 p_cpo = (char_u *)"";
4619 regmatch.regprog = vim_regcomp(s2,
4620 RE_MAGIC + RE_STRING);
4621 regmatch.rm_ic = ic;
4622 if (regmatch.regprog != NULL)
4623 {
4624 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004625 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (type == TYPE_NOMATCH)
4627 n1 = !n1;
4628 }
4629 p_cpo = save_cpo;
4630 break;
4631
4632 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4633 }
4634 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004635 clear_tv(rettv);
4636 clear_tv(&var2);
4637 rettv->v_type = VAR_NUMBER;
4638 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640 }
4641
4642 return OK;
4643}
4644
4645/*
4646 * Handle fourth level expression:
4647 * + number addition
4648 * - number subtraction
4649 * . string concatenation
4650 *
4651 * "arg" must point to the first non-white of the expression.
4652 * "arg" is advanced to the next non-white after the recognized expression.
4653 *
4654 * Return OK or FAIL.
4655 */
4656 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004657eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 int evaluate;
4661{
Bram Moolenaar33570922005-01-25 22:26:29 +00004662 typval_T var2;
4663 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 int op;
4665 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004666#ifdef FEAT_FLOAT
4667 float_T f1 = 0, f2 = 0;
4668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 char_u *s1, *s2;
4670 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4671 char_u *p;
4672
4673 /*
4674 * Get the first variable.
4675 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004676 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 return FAIL;
4678
4679 /*
4680 * Repeat computing, until no '+', '-' or '.' is following.
4681 */
4682 for (;;)
4683 {
4684 op = **arg;
4685 if (op != '+' && op != '-' && op != '.')
4686 break;
4687
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004688 if ((op != '+' || rettv->v_type != VAR_LIST)
4689#ifdef FEAT_FLOAT
4690 && (op == '.' || rettv->v_type != VAR_FLOAT)
4691#endif
4692 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004693 {
4694 /* For "list + ...", an illegal use of the first operand as
4695 * a number cannot be determined before evaluating the 2nd
4696 * operand: if this is also a list, all is ok.
4697 * For "something . ...", "something - ..." or "non-list + ...",
4698 * we know that the first operand needs to be a string or number
4699 * without evaluating the 2nd operand. So check before to avoid
4700 * side effects after an error. */
4701 if (evaluate && get_tv_string_chk(rettv) == NULL)
4702 {
4703 clear_tv(rettv);
4704 return FAIL;
4705 }
4706 }
4707
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 /*
4709 * Get the second variable.
4710 */
4711 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004712 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004714 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 return FAIL;
4716 }
4717
4718 if (evaluate)
4719 {
4720 /*
4721 * Compute the result.
4722 */
4723 if (op == '.')
4724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004725 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4726 s2 = get_tv_string_buf_chk(&var2, buf2);
4727 if (s2 == NULL) /* type error ? */
4728 {
4729 clear_tv(rettv);
4730 clear_tv(&var2);
4731 return FAIL;
4732 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004733 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004734 clear_tv(rettv);
4735 rettv->v_type = VAR_STRING;
4736 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004738 else if (op == '+' && rettv->v_type == VAR_LIST
4739 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004740 {
4741 /* concatenate Lists */
4742 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4743 &var3) == FAIL)
4744 {
4745 clear_tv(rettv);
4746 clear_tv(&var2);
4747 return FAIL;
4748 }
4749 clear_tv(rettv);
4750 *rettv = var3;
4751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 else
4753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 int error = FALSE;
4755
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756#ifdef FEAT_FLOAT
4757 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004758 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004759 f1 = rettv->vval.v_float;
4760 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004761 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004762 else
4763#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004764 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004765 n1 = get_tv_number_chk(rettv, &error);
4766 if (error)
4767 {
4768 /* This can only happen for "list + non-list". For
4769 * "non-list + ..." or "something - ...", we returned
4770 * before evaluating the 2nd operand. */
4771 clear_tv(rettv);
4772 return FAIL;
4773 }
4774#ifdef FEAT_FLOAT
4775 if (var2.v_type == VAR_FLOAT)
4776 f1 = n1;
4777#endif
4778 }
4779#ifdef FEAT_FLOAT
4780 if (var2.v_type == VAR_FLOAT)
4781 {
4782 f2 = var2.vval.v_float;
4783 n2 = 0;
4784 }
4785 else
4786#endif
4787 {
4788 n2 = get_tv_number_chk(&var2, &error);
4789 if (error)
4790 {
4791 clear_tv(rettv);
4792 clear_tv(&var2);
4793 return FAIL;
4794 }
4795#ifdef FEAT_FLOAT
4796 if (rettv->v_type == VAR_FLOAT)
4797 f2 = n2;
4798#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004799 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004800 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801
4802#ifdef FEAT_FLOAT
4803 /* If there is a float on either side the result is a float. */
4804 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4805 {
4806 if (op == '+')
4807 f1 = f1 + f2;
4808 else
4809 f1 = f1 - f2;
4810 rettv->v_type = VAR_FLOAT;
4811 rettv->vval.v_float = f1;
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814#endif
4815 {
4816 if (op == '+')
4817 n1 = n1 + n2;
4818 else
4819 n1 = n1 - n2;
4820 rettv->v_type = VAR_NUMBER;
4821 rettv->vval.v_number = n1;
4822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004824 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826 }
4827 return OK;
4828}
4829
4830/*
4831 * Handle fifth level expression:
4832 * * number multiplication
4833 * / number division
4834 * % number modulo
4835 *
4836 * "arg" must point to the first non-white of the expression.
4837 * "arg" is advanced to the next non-white after the recognized expression.
4838 *
4839 * Return OK or FAIL.
4840 */
4841 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004842eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004846 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847{
Bram Moolenaar33570922005-01-25 22:26:29 +00004848 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 int op;
4850 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004851#ifdef FEAT_FLOAT
4852 int use_float = FALSE;
4853 float_T f1 = 0, f2;
4854#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004855 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856
4857 /*
4858 * Get the first variable.
4859 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004860 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 return FAIL;
4862
4863 /*
4864 * Repeat computing, until no '*', '/' or '%' is following.
4865 */
4866 for (;;)
4867 {
4868 op = **arg;
4869 if (op != '*' && op != '/' && op != '%')
4870 break;
4871
4872 if (evaluate)
4873 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874#ifdef FEAT_FLOAT
4875 if (rettv->v_type == VAR_FLOAT)
4876 {
4877 f1 = rettv->vval.v_float;
4878 use_float = TRUE;
4879 n1 = 0;
4880 }
4881 else
4882#endif
4883 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885 if (error)
4886 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888 else
4889 n1 = 0;
4890
4891 /*
4892 * Get the second variable.
4893 */
4894 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004895 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 return FAIL;
4897
4898 if (evaluate)
4899 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004900#ifdef FEAT_FLOAT
4901 if (var2.v_type == VAR_FLOAT)
4902 {
4903 if (!use_float)
4904 {
4905 f1 = n1;
4906 use_float = TRUE;
4907 }
4908 f2 = var2.vval.v_float;
4909 n2 = 0;
4910 }
4911 else
4912#endif
4913 {
4914 n2 = get_tv_number_chk(&var2, &error);
4915 clear_tv(&var2);
4916 if (error)
4917 return FAIL;
4918#ifdef FEAT_FLOAT
4919 if (use_float)
4920 f2 = n2;
4921#endif
4922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923
4924 /*
4925 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928#ifdef FEAT_FLOAT
4929 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931 if (op == '*')
4932 f1 = f1 * f2;
4933 else if (op == '/')
4934 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004935# ifdef VMS
4936 /* VMS crashes on divide by zero, work around it */
4937 if (f2 == 0.0)
4938 {
4939 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004940 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004941 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004942 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004943 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004944 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004945 }
4946 else
4947 f1 = f1 / f2;
4948# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949 /* We rely on the floating point library to handle divide
4950 * by zero to result in "inf" and not a crash. */
4951 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004952# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004956 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004957 return FAIL;
4958 }
4959 rettv->v_type = VAR_FLOAT;
4960 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
4962 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004965 if (op == '*')
4966 n1 = n1 * n2;
4967 else if (op == '/')
4968 {
4969 if (n2 == 0) /* give an error message? */
4970 {
4971 if (n1 == 0)
4972 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4973 else if (n1 < 0)
4974 n1 = -0x7fffffffL;
4975 else
4976 n1 = 0x7fffffffL;
4977 }
4978 else
4979 n1 = n1 / n2;
4980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 {
4983 if (n2 == 0) /* give an error message? */
4984 n1 = 0;
4985 else
4986 n1 = n1 % n2;
4987 }
4988 rettv->v_type = VAR_NUMBER;
4989 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 }
4993
4994 return OK;
4995}
4996
4997/*
4998 * Handle sixth level expression:
4999 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005000 * "string" string constant
5001 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 * &option-name option value
5003 * @r register contents
5004 * identifier variable value
5005 * function() function call
5006 * $VAR environment variable
5007 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005008 * [expr, expr] List
5009 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 *
5011 * Also handle:
5012 * ! in front logical NOT
5013 * - in front unary minus
5014 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 * trailing [] subscript in String or List
5016 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 *
5018 * "arg" must point to the first non-white of the expression.
5019 * "arg" is advanced to the next non-white after the recognized expression.
5020 *
5021 * Return OK or FAIL.
5022 */
5023 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005024eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005028 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 long n;
5031 int len;
5032 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 char_u *start_leader, *end_leader;
5034 int ret = OK;
5035 char_u *alias;
5036
5037 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005038 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005039 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
5043 /*
5044 * Skip '!' and '-' characters. They are handled later.
5045 */
5046 start_leader = *arg;
5047 while (**arg == '!' || **arg == '-' || **arg == '+')
5048 *arg = skipwhite(*arg + 1);
5049 end_leader = *arg;
5050
5051 switch (**arg)
5052 {
5053 /*
5054 * Number constant.
5055 */
5056 case '0':
5057 case '1':
5058 case '2':
5059 case '3':
5060 case '4':
5061 case '5':
5062 case '6':
5063 case '7':
5064 case '8':
5065 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005066 {
5067#ifdef FEAT_FLOAT
5068 char_u *p = skipdigits(*arg + 1);
5069 int get_float = FALSE;
5070
5071 /* We accept a float when the format matches
5072 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005073 * strict to avoid backwards compatibility problems.
5074 * Don't look for a float after the "." operator, so that
5075 * ":let vers = 1.2.3" doesn't fail. */
5076 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005078 get_float = TRUE;
5079 p = skipdigits(p + 2);
5080 if (*p == 'e' || *p == 'E')
5081 {
5082 ++p;
5083 if (*p == '-' || *p == '+')
5084 ++p;
5085 if (!vim_isdigit(*p))
5086 get_float = FALSE;
5087 else
5088 p = skipdigits(p + 1);
5089 }
5090 if (ASCII_ISALPHA(*p) || *p == '.')
5091 get_float = FALSE;
5092 }
5093 if (get_float)
5094 {
5095 float_T f;
5096
5097 *arg += string2float(*arg, &f);
5098 if (evaluate)
5099 {
5100 rettv->v_type = VAR_FLOAT;
5101 rettv->vval.v_float = f;
5102 }
5103 }
5104 else
5105#endif
5106 {
5107 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5108 *arg += len;
5109 if (evaluate)
5110 {
5111 rettv->v_type = VAR_NUMBER;
5112 rettv->vval.v_number = n;
5113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117
5118 /*
5119 * String constant: "string".
5120 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 break;
5123
5124 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005128 break;
5129
5130 /*
5131 * List: [expr, expr]
5132 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 break;
5135
5136 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 * Dictionary: {key: val, key: val}
5138 */
5139 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5140 break;
5141
5142 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005143 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005145 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 break;
5147
5148 /*
5149 * Environment variable: $VAR.
5150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 break;
5153
5154 /*
5155 * Register contents: @r.
5156 */
5157 case '@': ++*arg;
5158 if (evaluate)
5159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005161 rettv->vval.v_string = get_reg_contents(**arg,
5162 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164 if (**arg != NUL)
5165 ++*arg;
5166 break;
5167
5168 /*
5169 * nested expression: (expression).
5170 */
5171 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (**arg == ')')
5174 ++*arg;
5175 else if (ret == OK)
5176 {
5177 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 ret = FAIL;
5180 }
5181 break;
5182
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 break;
5185 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186
5187 if (ret == NOTDONE)
5188 {
5189 /*
5190 * Must be a variable or function name.
5191 * Can also be a curly-braces kind of name: {expr}.
5192 */
5193 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005194 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195 if (alias != NULL)
5196 s = alias;
5197
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005198 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 ret = FAIL;
5200 else
5201 {
5202 if (**arg == '(') /* recursive! */
5203 {
5204 /* If "s" is the name of a variable of type VAR_FUNC
5205 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005206 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207
5208 /* Invoke the function. */
5209 ret = get_func_tv(s, len, rettv, arg,
5210 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005211 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005212
5213 /* If evaluate is FALSE rettv->v_type was not set in
5214 * get_func_tv, but it's needed in handle_subscript() to parse
5215 * what follows. So set it here. */
5216 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5217 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005218 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005219 rettv->v_type = VAR_FUNC;
5220 }
5221
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 /* Stop the expression evaluation when immediately
5223 * aborting on error, or when an interrupt occurred or
5224 * an exception was thrown but not caught. */
5225 if (aborting())
5226 {
5227 if (ret == OK)
5228 clear_tv(rettv);
5229 ret = FAIL;
5230 }
5231 }
5232 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005233 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005234 else
5235 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005237 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 }
5239
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 *arg = skipwhite(*arg);
5241
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005242 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5243 * expr(expr). */
5244 if (ret == OK)
5245 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246
5247 /*
5248 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5249 */
5250 if (ret == OK && evaluate && end_leader > start_leader)
5251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005252 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005253 int val = 0;
5254#ifdef FEAT_FLOAT
5255 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005256
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005257 if (rettv->v_type == VAR_FLOAT)
5258 f = rettv->vval.v_float;
5259 else
5260#endif
5261 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005262 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005264 clear_tv(rettv);
5265 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005267 else
5268 {
5269 while (end_leader > start_leader)
5270 {
5271 --end_leader;
5272 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005273 {
5274#ifdef FEAT_FLOAT
5275 if (rettv->v_type == VAR_FLOAT)
5276 f = !f;
5277 else
5278#endif
5279 val = !val;
5280 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005282 {
5283#ifdef FEAT_FLOAT
5284 if (rettv->v_type == VAR_FLOAT)
5285 f = -f;
5286 else
5287#endif
5288 val = -val;
5289 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005290 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005291#ifdef FEAT_FLOAT
5292 if (rettv->v_type == VAR_FLOAT)
5293 {
5294 clear_tv(rettv);
5295 rettv->vval.v_float = f;
5296 }
5297 else
5298#endif
5299 {
5300 clear_tv(rettv);
5301 rettv->v_type = VAR_NUMBER;
5302 rettv->vval.v_number = val;
5303 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306
5307 return ret;
5308}
5309
5310/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005311 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5312 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5314 */
5315 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005316eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005318 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005320 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321{
5322 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005323 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325 long len = -1;
5326 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005328 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005330 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005332 if (verbose)
5333 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 return FAIL;
5335 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005336#ifdef FEAT_FLOAT
5337 else if (rettv->v_type == VAR_FLOAT)
5338 {
5339 if (verbose)
5340 EMSG(_(e_float_as_string));
5341 return FAIL;
5342 }
5343#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344
Bram Moolenaar8c711452005-01-14 21:53:12 +00005345 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 /*
5348 * dict.name
5349 */
5350 key = *arg + 1;
5351 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5352 ;
5353 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 /*
5360 * something[idx]
5361 *
5362 * Get the (first) variable from inside the [].
5363 */
5364 *arg = skipwhite(*arg + 1);
5365 if (**arg == ':')
5366 empty1 = TRUE;
5367 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5368 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005369 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5370 {
5371 /* not a number or string */
5372 clear_tv(&var1);
5373 return FAIL;
5374 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005375
5376 /*
5377 * Get the second variable from inside the [:].
5378 */
5379 if (**arg == ':')
5380 {
5381 range = TRUE;
5382 *arg = skipwhite(*arg + 1);
5383 if (**arg == ']')
5384 empty2 = TRUE;
5385 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005387 if (!empty1)
5388 clear_tv(&var1);
5389 return FAIL;
5390 }
5391 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5392 {
5393 /* not a number or string */
5394 if (!empty1)
5395 clear_tv(&var1);
5396 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005397 return FAIL;
5398 }
5399 }
5400
5401 /* Check for the ']'. */
5402 if (**arg != ']')
5403 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005404 if (verbose)
5405 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 clear_tv(&var1);
5407 if (range)
5408 clear_tv(&var2);
5409 return FAIL;
5410 }
5411 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 }
5413
5414 if (evaluate)
5415 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416 n1 = 0;
5417 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005419 n1 = get_tv_number(&var1);
5420 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 }
5422 if (range)
5423 {
5424 if (empty2)
5425 n2 = -1;
5426 else
5427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005428 n2 = get_tv_number(&var2);
5429 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 }
5431 }
5432
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 {
5435 case VAR_NUMBER:
5436 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005437 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 len = (long)STRLEN(s);
5439 if (range)
5440 {
5441 /* The resulting variable is a substring. If the indexes
5442 * are out of range the result is empty. */
5443 if (n1 < 0)
5444 {
5445 n1 = len + n1;
5446 if (n1 < 0)
5447 n1 = 0;
5448 }
5449 if (n2 < 0)
5450 n2 = len + n2;
5451 else if (n2 >= len)
5452 n2 = len;
5453 if (n1 >= len || n2 < 0 || n1 > n2)
5454 s = NULL;
5455 else
5456 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5457 }
5458 else
5459 {
5460 /* The resulting variable is a string of a single
5461 * character. If the index is too big or negative the
5462 * result is empty. */
5463 if (n1 >= len || n1 < 0)
5464 s = NULL;
5465 else
5466 s = vim_strnsave(s + n1, 1);
5467 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 clear_tv(rettv);
5469 rettv->v_type = VAR_STRING;
5470 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 break;
5472
5473 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 if (n1 < 0)
5476 n1 = len + n1;
5477 if (!empty1 && (n1 < 0 || n1 >= len))
5478 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005479 /* For a range we allow invalid values and return an empty
5480 * list. A list index out of range is an error. */
5481 if (!range)
5482 {
5483 if (verbose)
5484 EMSGN(_(e_listidx), n1);
5485 return FAIL;
5486 }
5487 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488 }
5489 if (range)
5490 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005491 list_T *l;
5492 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493
5494 if (n2 < 0)
5495 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005496 else if (n2 >= len)
5497 n2 = len - 1;
5498 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005499 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 l = list_alloc();
5501 if (l == NULL)
5502 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 n1 <= n2; ++n1)
5505 {
5506 if (list_append_tv(l, &item->li_tv) == FAIL)
5507 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005508 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 return FAIL;
5510 }
5511 item = item->li_next;
5512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005513 clear_tv(rettv);
5514 rettv->v_type = VAR_LIST;
5515 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005516 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 }
5518 else
5519 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005520 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521 clear_tv(rettv);
5522 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 }
5524 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005525
5526 case VAR_DICT:
5527 if (range)
5528 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005529 if (verbose)
5530 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005531 if (len == -1)
5532 clear_tv(&var1);
5533 return FAIL;
5534 }
5535 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005536 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005537
5538 if (len == -1)
5539 {
5540 key = get_tv_string(&var1);
5541 if (*key == NUL)
5542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005543 if (verbose)
5544 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005545 clear_tv(&var1);
5546 return FAIL;
5547 }
5548 }
5549
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005550 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005551
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005552 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005553 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005554 if (len == -1)
5555 clear_tv(&var1);
5556 if (item == NULL)
5557 return FAIL;
5558
5559 copy_tv(&item->di_tv, &var1);
5560 clear_tv(rettv);
5561 *rettv = var1;
5562 }
5563 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 }
5565 }
5566
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 return OK;
5568}
5569
5570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 * Get an option value.
5572 * "arg" points to the '&' or '+' before the option name.
5573 * "arg" is advanced to character after the option name.
5574 * Return OK or FAIL.
5575 */
5576 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005579 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 int evaluate;
5581{
5582 char_u *option_end;
5583 long numval;
5584 char_u *stringval;
5585 int opt_type;
5586 int c;
5587 int working = (**arg == '+'); /* has("+option") */
5588 int ret = OK;
5589 int opt_flags;
5590
5591 /*
5592 * Isolate the option name and find its value.
5593 */
5594 option_end = find_option_end(arg, &opt_flags);
5595 if (option_end == NULL)
5596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005597 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 EMSG2(_("E112: Option name missing: %s"), *arg);
5599 return FAIL;
5600 }
5601
5602 if (!evaluate)
5603 {
5604 *arg = option_end;
5605 return OK;
5606 }
5607
5608 c = *option_end;
5609 *option_end = NUL;
5610 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612
5613 if (opt_type == -3) /* invalid name */
5614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005615 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 EMSG2(_("E113: Unknown option: %s"), *arg);
5617 ret = FAIL;
5618 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005619 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 if (opt_type == -2) /* hidden string option */
5622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623 rettv->v_type = VAR_STRING;
5624 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
5626 else if (opt_type == -1) /* hidden number option */
5627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005628 rettv->v_type = VAR_NUMBER;
5629 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
5631 else if (opt_type == 1) /* number option */
5632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005633 rettv->v_type = VAR_NUMBER;
5634 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 }
5636 else /* string option */
5637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005638 rettv->v_type = VAR_STRING;
5639 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 }
5641 }
5642 else if (working && (opt_type == -2 || opt_type == -1))
5643 ret = FAIL;
5644
5645 *option_end = c; /* put back for error messages */
5646 *arg = option_end;
5647
5648 return ret;
5649}
5650
5651/*
5652 * Allocate a variable for a string constant.
5653 * Return OK or FAIL.
5654 */
5655 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 int evaluate;
5660{
5661 char_u *p;
5662 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 int extra = 0;
5664
5665 /*
5666 * Find the end of the string, skipping backslashed characters.
5667 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005668 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 {
5670 if (*p == '\\' && p[1] != NUL)
5671 {
5672 ++p;
5673 /* A "\<x>" form occupies at least 4 characters, and produces up
5674 * to 6 characters: reserve space for 2 extra */
5675 if (*p == '<')
5676 extra += 2;
5677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679
5680 if (*p != '"')
5681 {
5682 EMSG2(_("E114: Missing quote: %s"), *arg);
5683 return FAIL;
5684 }
5685
5686 /* If only parsing, set *arg and return here */
5687 if (!evaluate)
5688 {
5689 *arg = p + 1;
5690 return OK;
5691 }
5692
5693 /*
5694 * Copy the string into allocated memory, handling backslashed
5695 * characters.
5696 */
5697 name = alloc((unsigned)(p - *arg + extra));
5698 if (name == NULL)
5699 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 rettv->v_type = VAR_STRING;
5701 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
5705 if (*p == '\\')
5706 {
5707 switch (*++p)
5708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 case 'b': *name++ = BS; ++p; break;
5710 case 'e': *name++ = ESC; ++p; break;
5711 case 'f': *name++ = FF; ++p; break;
5712 case 'n': *name++ = NL; ++p; break;
5713 case 'r': *name++ = CAR; ++p; break;
5714 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
5716 case 'X': /* hex: "\x1", "\x12" */
5717 case 'x':
5718 case 'u': /* Unicode: "\u0023" */
5719 case 'U':
5720 if (vim_isxdigit(p[1]))
5721 {
5722 int n, nr;
5723 int c = toupper(*p);
5724
5725 if (c == 'X')
5726 n = 2;
5727 else
5728 n = 4;
5729 nr = 0;
5730 while (--n >= 0 && vim_isxdigit(p[1]))
5731 {
5732 ++p;
5733 nr = (nr << 4) + hex2nr(*p);
5734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736#ifdef FEAT_MBYTE
5737 /* For "\u" store the number according to
5738 * 'encoding'. */
5739 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 else
5742#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 break;
5746
5747 /* octal: "\1", "\12", "\123" */
5748 case '0':
5749 case '1':
5750 case '2':
5751 case '3':
5752 case '4':
5753 case '5':
5754 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 case '7': *name = *p++ - '0';
5756 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 *name = (*name << 3) + *p++ - '0';
5759 if (*p >= '0' && *p <= '7')
5760 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 break;
5764
5765 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 if (extra != 0)
5768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 break;
5771 }
5772 /* FALLTHROUGH */
5773
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 break;
5776 }
5777 }
5778 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 *arg = p + 1;
5784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 return OK;
5786}
5787
5788/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 * Return OK or FAIL.
5791 */
5792 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005793get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 int evaluate;
5797{
5798 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 int reduce = 0;
5801
5802 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005804 */
5805 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5806 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005810 break;
5811 ++reduce;
5812 ++p;
5813 }
5814 }
5815
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005817 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005819 return FAIL;
5820 }
5821
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005823 if (!evaluate)
5824 {
5825 *arg = p + 1;
5826 return OK;
5827 }
5828
5829 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005831 */
5832 str = alloc((unsigned)((p - *arg) - reduce));
5833 if (str == NULL)
5834 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 rettv->v_type = VAR_STRING;
5836 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 break;
5844 ++p;
5845 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 *arg = p + 1;
5850
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 return OK;
5852}
5853
5854/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855 * Allocate a variable for a List and fill it from "*arg".
5856 * Return OK or FAIL.
5857 */
5858 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005859get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005861 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 int evaluate;
5863{
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 list_T *l = NULL;
5865 typval_T tv;
5866 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867
5868 if (evaluate)
5869 {
5870 l = list_alloc();
5871 if (l == NULL)
5872 return FAIL;
5873 }
5874
5875 *arg = skipwhite(*arg + 1);
5876 while (**arg != ']' && **arg != NUL)
5877 {
5878 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5879 goto failret;
5880 if (evaluate)
5881 {
5882 item = listitem_alloc();
5883 if (item != NULL)
5884 {
5885 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005886 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887 list_append(l, item);
5888 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005889 else
5890 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891 }
5892
5893 if (**arg == ']')
5894 break;
5895 if (**arg != ',')
5896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898 goto failret;
5899 }
5900 *arg = skipwhite(*arg + 1);
5901 }
5902
5903 if (**arg != ']')
5904 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906failret:
5907 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005908 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 return FAIL;
5910 }
5911
5912 *arg = skipwhite(*arg + 1);
5913 if (evaluate)
5914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005915 rettv->v_type = VAR_LIST;
5916 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 ++l->lv_refcount;
5918 }
5919
5920 return OK;
5921}
5922
5923/*
5924 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005925 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005927 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928list_alloc()
5929{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005930 list_T *l;
5931
5932 l = (list_T *)alloc_clear(sizeof(list_T));
5933 if (l != NULL)
5934 {
5935 /* Prepend the list to the list of lists for garbage collection. */
5936 if (first_list != NULL)
5937 first_list->lv_used_prev = l;
5938 l->lv_used_prev = NULL;
5939 l->lv_used_next = first_list;
5940 first_list = l;
5941 }
5942 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943}
5944
5945/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005946 * Allocate an empty list for a return value.
5947 * Returns OK or FAIL.
5948 */
5949 static int
5950rettv_list_alloc(rettv)
5951 typval_T *rettv;
5952{
5953 list_T *l = list_alloc();
5954
5955 if (l == NULL)
5956 return FAIL;
5957
5958 rettv->vval.v_list = l;
5959 rettv->v_type = VAR_LIST;
5960 ++l->lv_refcount;
5961 return OK;
5962}
5963
5964/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 * Unreference a list: decrement the reference count and free it when it
5966 * becomes zero.
5967 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005968 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005970 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005972 if (l != NULL && --l->lv_refcount <= 0)
5973 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974}
5975
5976/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005977 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978 * Ignores the reference count.
5979 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005980 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005981list_free(l, recurse)
5982 list_T *l;
5983 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984{
Bram Moolenaar33570922005-01-25 22:26:29 +00005985 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005987 /* Remove the list from the list of lists for garbage collection. */
5988 if (l->lv_used_prev == NULL)
5989 first_list = l->lv_used_next;
5990 else
5991 l->lv_used_prev->lv_used_next = l->lv_used_next;
5992 if (l->lv_used_next != NULL)
5993 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5994
Bram Moolenaard9fba312005-06-26 22:34:35 +00005995 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005997 /* Remove the item before deleting it. */
5998 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005999 if (recurse || (item->li_tv.v_type != VAR_LIST
6000 && item->li_tv.v_type != VAR_DICT))
6001 clear_tv(&item->li_tv);
6002 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 }
6004 vim_free(l);
6005}
6006
6007/*
6008 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006009 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006011 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012listitem_alloc()
6013{
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015}
6016
6017/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006018 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006020 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006024 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 vim_free(item);
6026}
6027
6028/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006029 * Remove a list item from a List and free it. Also clears the value.
6030 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006031 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006032listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 list_T *l;
6034 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006035{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006036 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006037 listitem_free(item);
6038}
6039
6040/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041 * Get the number of items in a list.
6042 */
6043 static long
6044list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 if (l == NULL)
6048 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006049 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050}
6051
6052/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006053 * Return TRUE when two lists have exactly the same values.
6054 */
6055 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006056list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 list_T *l1;
6058 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006059 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006060 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006061{
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006063
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006064 if (l1 == NULL || l2 == NULL)
6065 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006066 if (l1 == l2)
6067 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006068 if (list_len(l1) != list_len(l2))
6069 return FALSE;
6070
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006071 for (item1 = l1->lv_first, item2 = l2->lv_first;
6072 item1 != NULL && item2 != NULL;
6073 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006074 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075 return FALSE;
6076 return item1 == NULL && item2 == NULL;
6077}
6078
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006079#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6080 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006081/*
6082 * Return the dictitem that an entry in a hashtable points to.
6083 */
6084 dictitem_T *
6085dict_lookup(hi)
6086 hashitem_T *hi;
6087{
6088 return HI2DI(hi);
6089}
6090#endif
6091
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006093 * Return TRUE when two dictionaries have exactly the same key/values.
6094 */
6095 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006096dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 dict_T *d1;
6098 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006099 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006101{
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 hashitem_T *hi;
6103 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006104 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006105
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006106 if (d1 == NULL || d2 == NULL)
6107 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006108 if (d1 == d2)
6109 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006110 if (dict_len(d1) != dict_len(d2))
6111 return FALSE;
6112
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006113 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006114 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006115 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006116 if (!HASHITEM_EMPTY(hi))
6117 {
6118 item2 = dict_find(d2, hi->hi_key, -1);
6119 if (item2 == NULL)
6120 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006121 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006122 return FALSE;
6123 --todo;
6124 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006125 }
6126 return TRUE;
6127}
6128
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129static int tv_equal_recurse_limit;
6130
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006134 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 */
6136 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006137tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 typval_T *tv1;
6139 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006140 int ic; /* ignore case */
6141 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142{
6143 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006144 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006145 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006146 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006147
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006148 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006149 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006151 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006152 * recursiveness to a limit. We guess they are equal then.
6153 * A fixed limit has the problem of still taking an awful long time.
6154 * Reduce the limit every time running into it. That should work fine for
6155 * deeply linked structures that are not recursively linked and catch
6156 * recursiveness quickly. */
6157 if (!recursive)
6158 tv_equal_recurse_limit = 1000;
6159 if (recursive_cnt >= tv_equal_recurse_limit)
6160 {
6161 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006162 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006163 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006164
6165 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006167 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168 ++recursive_cnt;
6169 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6170 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006171 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006172
6173 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006174 ++recursive_cnt;
6175 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6176 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006177 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006178
6179 case VAR_FUNC:
6180 return (tv1->vval.v_string != NULL
6181 && tv2->vval.v_string != NULL
6182 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6183
6184 case VAR_NUMBER:
6185 return tv1->vval.v_number == tv2->vval.v_number;
6186
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006187#ifdef FEAT_FLOAT
6188 case VAR_FLOAT:
6189 return tv1->vval.v_float == tv2->vval.v_float;
6190#endif
6191
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006192 case VAR_STRING:
6193 s1 = get_tv_string_buf(tv1, buf1);
6194 s2 = get_tv_string_buf(tv2, buf2);
6195 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006196 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006197
6198 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199 return TRUE;
6200}
6201
6202/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006203 * Locate item with index "n" in list "l" and return it.
6204 * A negative index is counted from the end; -1 is the last item.
6205 * Returns NULL when "n" is out of range.
6206 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006207 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006209 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006210 long n;
6211{
Bram Moolenaar33570922005-01-25 22:26:29 +00006212 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006213 long idx;
6214
6215 if (l == NULL)
6216 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006217
6218 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006219 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006220 n = l->lv_len + n;
6221
6222 /* Check for index out of range. */
6223 if (n < 0 || n >= l->lv_len)
6224 return NULL;
6225
6226 /* When there is a cached index may start search from there. */
6227 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006229 if (n < l->lv_idx / 2)
6230 {
6231 /* closest to the start of the list */
6232 item = l->lv_first;
6233 idx = 0;
6234 }
6235 else if (n > (l->lv_idx + l->lv_len) / 2)
6236 {
6237 /* closest to the end of the list */
6238 item = l->lv_last;
6239 idx = l->lv_len - 1;
6240 }
6241 else
6242 {
6243 /* closest to the cached index */
6244 item = l->lv_idx_item;
6245 idx = l->lv_idx;
6246 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247 }
6248 else
6249 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006250 if (n < l->lv_len / 2)
6251 {
6252 /* closest to the start of the list */
6253 item = l->lv_first;
6254 idx = 0;
6255 }
6256 else
6257 {
6258 /* closest to the end of the list */
6259 item = l->lv_last;
6260 idx = l->lv_len - 1;
6261 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006263
6264 while (n > idx)
6265 {
6266 /* search forward */
6267 item = item->li_next;
6268 ++idx;
6269 }
6270 while (n < idx)
6271 {
6272 /* search backward */
6273 item = item->li_prev;
6274 --idx;
6275 }
6276
6277 /* cache the used index */
6278 l->lv_idx = idx;
6279 l->lv_idx_item = item;
6280
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281 return item;
6282}
6283
6284/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006285 * Get list item "l[idx]" as a number.
6286 */
6287 static long
6288list_find_nr(l, idx, errorp)
6289 list_T *l;
6290 long idx;
6291 int *errorp; /* set to TRUE when something wrong */
6292{
6293 listitem_T *li;
6294
6295 li = list_find(l, idx);
6296 if (li == NULL)
6297 {
6298 if (errorp != NULL)
6299 *errorp = TRUE;
6300 return -1L;
6301 }
6302 return get_tv_number_chk(&li->li_tv, errorp);
6303}
6304
6305/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006306 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6307 */
6308 char_u *
6309list_find_str(l, idx)
6310 list_T *l;
6311 long idx;
6312{
6313 listitem_T *li;
6314
6315 li = list_find(l, idx - 1);
6316 if (li == NULL)
6317 {
6318 EMSGN(_(e_listidx), idx);
6319 return NULL;
6320 }
6321 return get_tv_string(&li->li_tv);
6322}
6323
6324/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006325 * Locate "item" list "l" and return its index.
6326 * Returns -1 when "item" is not in the list.
6327 */
6328 static long
6329list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006330 list_T *l;
6331 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006332{
6333 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006335
6336 if (l == NULL)
6337 return -1;
6338 idx = 0;
6339 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6340 ++idx;
6341 if (li == NULL)
6342 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006343 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006344}
6345
6346/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006347 * Append item "item" to the end of list "l".
6348 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006349 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 list_T *l;
6352 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353{
6354 if (l->lv_last == NULL)
6355 {
6356 /* empty list */
6357 l->lv_first = item;
6358 l->lv_last = item;
6359 item->li_prev = NULL;
6360 }
6361 else
6362 {
6363 l->lv_last->li_next = item;
6364 item->li_prev = l->lv_last;
6365 l->lv_last = item;
6366 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006367 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 item->li_next = NULL;
6369}
6370
6371/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006373 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006374 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006375 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 list_T *l;
6378 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006380 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381
Bram Moolenaar05159a02005-02-26 23:04:13 +00006382 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006383 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006384 copy_tv(tv, &li->li_tv);
6385 list_append(l, li);
6386 return OK;
6387}
6388
6389/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006390 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006391 * Return FAIL when out of memory.
6392 */
6393 int
6394list_append_dict(list, dict)
6395 list_T *list;
6396 dict_T *dict;
6397{
6398 listitem_T *li = listitem_alloc();
6399
6400 if (li == NULL)
6401 return FAIL;
6402 li->li_tv.v_type = VAR_DICT;
6403 li->li_tv.v_lock = 0;
6404 li->li_tv.vval.v_dict = dict;
6405 list_append(list, li);
6406 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 return OK;
6408}
6409
6410/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006411 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006412 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006413 * Returns FAIL when out of memory.
6414 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006415 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006416list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006417 list_T *l;
6418 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006419 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006420{
6421 listitem_T *li = listitem_alloc();
6422
6423 if (li == NULL)
6424 return FAIL;
6425 list_append(l, li);
6426 li->li_tv.v_type = VAR_STRING;
6427 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006428 if (str == NULL)
6429 li->li_tv.vval.v_string = NULL;
6430 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006431 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006432 return FAIL;
6433 return OK;
6434}
6435
6436/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006437 * Append "n" to list "l".
6438 * Returns FAIL when out of memory.
6439 */
6440 static int
6441list_append_number(l, n)
6442 list_T *l;
6443 varnumber_T n;
6444{
6445 listitem_T *li;
6446
6447 li = listitem_alloc();
6448 if (li == NULL)
6449 return FAIL;
6450 li->li_tv.v_type = VAR_NUMBER;
6451 li->li_tv.v_lock = 0;
6452 li->li_tv.vval.v_number = n;
6453 list_append(l, li);
6454 return OK;
6455}
6456
6457/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459 * If "item" is NULL append at the end.
6460 * Return FAIL when out of memory.
6461 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006462 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006464 list_T *l;
6465 typval_T *tv;
6466 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006467{
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469
6470 if (ni == NULL)
6471 return FAIL;
6472 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006473 list_insert(l, ni, item);
6474 return OK;
6475}
6476
6477 void
6478list_insert(l, ni, item)
6479 list_T *l;
6480 listitem_T *ni;
6481 listitem_T *item;
6482{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006483 if (item == NULL)
6484 /* Append new item at end of list. */
6485 list_append(l, ni);
6486 else
6487 {
6488 /* Insert new item before existing item. */
6489 ni->li_prev = item->li_prev;
6490 ni->li_next = item;
6491 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006492 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006493 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006494 ++l->lv_idx;
6495 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006497 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006499 l->lv_idx_item = NULL;
6500 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006502 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504}
6505
6506/*
6507 * Extend "l1" with "l2".
6508 * If "bef" is NULL append at the end, otherwise insert before this item.
6509 * Returns FAIL when out of memory.
6510 */
6511 static int
6512list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006513 list_T *l1;
6514 list_T *l2;
6515 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516{
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006518 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006520 /* We also quit the loop when we have inserted the original item count of
6521 * the list, avoid a hang when we extend a list with itself. */
6522 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6524 return FAIL;
6525 return OK;
6526}
6527
6528/*
6529 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6530 * Return FAIL when out of memory.
6531 */
6532 static int
6533list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 list_T *l1;
6535 list_T *l2;
6536 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537{
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006540 if (l1 == NULL || l2 == NULL)
6541 return FAIL;
6542
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006544 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545 if (l == NULL)
6546 return FAIL;
6547 tv->v_type = VAR_LIST;
6548 tv->vval.v_list = l;
6549
6550 /* append all items from the second list */
6551 return list_extend(l, l2, NULL);
6552}
6553
6554/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006557 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 * Returns NULL when out of memory.
6559 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006561list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006564 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565{
Bram Moolenaar33570922005-01-25 22:26:29 +00006566 list_T *copy;
6567 listitem_T *item;
6568 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569
6570 if (orig == NULL)
6571 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572
6573 copy = list_alloc();
6574 if (copy != NULL)
6575 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006576 if (copyID != 0)
6577 {
6578 /* Do this before adding the items, because one of the items may
6579 * refer back to this list. */
6580 orig->lv_copyID = copyID;
6581 orig->lv_copylist = copy;
6582 }
6583 for (item = orig->lv_first; item != NULL && !got_int;
6584 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 {
6586 ni = listitem_alloc();
6587 if (ni == NULL)
6588 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006589 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006590 {
6591 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6592 {
6593 vim_free(ni);
6594 break;
6595 }
6596 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006598 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 list_append(copy, ni);
6600 }
6601 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006602 if (item != NULL)
6603 {
6604 list_unref(copy);
6605 copy = NULL;
6606 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607 }
6608
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609 return copy;
6610}
6611
6612/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006613 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006614 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006615 * This used to be called list_remove, but that conflicts with a Sun header
6616 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006618 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006619vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006620 list_T *l;
6621 listitem_T *item;
6622 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006623{
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626 /* notify watchers */
6627 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006629 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006630 list_fix_watch(l, ip);
6631 if (ip == item2)
6632 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634
6635 if (item2->li_next == NULL)
6636 l->lv_last = item->li_prev;
6637 else
6638 item2->li_next->li_prev = item->li_prev;
6639 if (item->li_prev == NULL)
6640 l->lv_first = item2->li_next;
6641 else
6642 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006643 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644}
6645
6646/*
6647 * Return an allocated string with the string representation of a list.
6648 * May return NULL.
6649 */
6650 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006651list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006652 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006653 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654{
6655 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656
6657 if (tv->vval.v_list == NULL)
6658 return NULL;
6659 ga_init2(&ga, (int)sizeof(char), 80);
6660 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006661 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006662 {
6663 vim_free(ga.ga_data);
6664 return NULL;
6665 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666 ga_append(&ga, ']');
6667 ga_append(&ga, NUL);
6668 return (char_u *)ga.ga_data;
6669}
6670
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006671typedef struct join_S {
6672 char_u *s;
6673 char_u *tofree;
6674} join_T;
6675
6676 static int
6677list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6678 garray_T *gap; /* to store the result in */
6679 list_T *l;
6680 char_u *sep;
6681 int echo_style;
6682 int copyID;
6683 garray_T *join_gap; /* to keep each list item string */
6684{
6685 int i;
6686 join_T *p;
6687 int len;
6688 int sumlen = 0;
6689 int first = TRUE;
6690 char_u *tofree;
6691 char_u numbuf[NUMBUFLEN];
6692 listitem_T *item;
6693 char_u *s;
6694
6695 /* Stringify each item in the list. */
6696 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6697 {
6698 if (echo_style)
6699 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6700 else
6701 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6702 if (s == NULL)
6703 return FAIL;
6704
6705 len = (int)STRLEN(s);
6706 sumlen += len;
6707
6708 ga_grow(join_gap, 1);
6709 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6710 if (tofree != NULL || s != numbuf)
6711 {
6712 p->s = s;
6713 p->tofree = tofree;
6714 }
6715 else
6716 {
6717 p->s = vim_strnsave(s, len);
6718 p->tofree = p->s;
6719 }
6720
6721 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006722 if (did_echo_string_emsg) /* recursion error, bail out */
6723 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 }
6725
6726 /* Allocate result buffer with its total size, avoid re-allocation and
6727 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6728 if (join_gap->ga_len >= 2)
6729 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6730 if (ga_grow(gap, sumlen + 2) == FAIL)
6731 return FAIL;
6732
6733 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6734 {
6735 if (first)
6736 first = FALSE;
6737 else
6738 ga_concat(gap, sep);
6739 p = ((join_T *)join_gap->ga_data) + i;
6740
6741 if (p->s != NULL)
6742 ga_concat(gap, p->s);
6743 line_breakcheck();
6744 }
6745
6746 return OK;
6747}
6748
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006749/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006750 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006751 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006752 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006753 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006754 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006755list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006756 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006757 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006759 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006760 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006761{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006762 garray_T join_ga;
6763 int retval;
6764 join_T *p;
6765 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006766
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006767 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6768 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6769
6770 /* Dispose each item in join_ga. */
6771 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006772 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006773 p = (join_T *)join_ga.ga_data;
6774 for (i = 0; i < join_ga.ga_len; ++i)
6775 {
6776 vim_free(p->tofree);
6777 ++p;
6778 }
6779 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006780 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006781
6782 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006783}
6784
6785/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006786 * Garbage collection for lists and dictionaries.
6787 *
6788 * We use reference counts to be able to free most items right away when they
6789 * are no longer used. But for composite items it's possible that it becomes
6790 * unused while the reference count is > 0: When there is a recursive
6791 * reference. Example:
6792 * :let l = [1, 2, 3]
6793 * :let d = {9: l}
6794 * :let l[1] = d
6795 *
6796 * Since this is quite unusual we handle this with garbage collection: every
6797 * once in a while find out which lists and dicts are not referenced from any
6798 * variable.
6799 *
6800 * Here is a good reference text about garbage collection (refers to Python
6801 * but it applies to all reference-counting mechanisms):
6802 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006803 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006804
6805/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006806 * Do garbage collection for lists and dicts.
6807 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006808 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006809 int
6810garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006811{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006812 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006813 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006814 buf_T *buf;
6815 win_T *wp;
6816 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006817 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006818 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006819 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006820#ifdef FEAT_WINDOWS
6821 tabpage_T *tp;
6822#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006823
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006824 /* Only do this once. */
6825 want_garbage_collect = FALSE;
6826 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006827 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006828
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006829 /* We advance by two because we add one for items referenced through
6830 * previous_funccal. */
6831 current_copyID += COPYID_INC;
6832 copyID = current_copyID;
6833
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 /*
6835 * 1. Go through all accessible variables and mark all lists and dicts
6836 * with copyID.
6837 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006838
6839 /* Don't free variables in the previous_funccal list unless they are only
6840 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006841 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006842 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6843 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006844 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6845 NULL);
6846 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6847 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 }
6849
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 /* script-local variables */
6851 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006852 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853
6854 /* buffer-local variables */
6855 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006856 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6857 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858
6859 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006860 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006861 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6862 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006863#ifdef FEAT_AUTOCMD
6864 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006865 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6866 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006867#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006868
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006869#ifdef FEAT_WINDOWS
6870 /* tabpage-local variables */
6871 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6873 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006874#endif
6875
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878
6879 /* function-local variables */
6880 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6881 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006882 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6883 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884 }
6885
Bram Moolenaard812df62008-11-09 12:46:09 +00006886 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006887 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006888
Bram Moolenaar1dced572012-04-05 16:54:08 +02006889#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006891#endif
6892
Bram Moolenaardb913952012-06-29 12:54:53 +02006893#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006894 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006895#endif
6896
6897#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006898 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006899#endif
6900
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006902 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 /*
6904 * 2. Free lists and dictionaries that are not referenced.
6905 */
6906 did_free = free_unref_items(copyID);
6907
6908 /*
6909 * 3. Check if any funccal can be freed now.
6910 */
6911 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006912 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 if (can_free_funccal(*pfc, copyID))
6914 {
6915 fc = *pfc;
6916 *pfc = fc->caller;
6917 free_funccal(fc, TRUE);
6918 did_free = TRUE;
6919 did_free_funccal = TRUE;
6920 }
6921 else
6922 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006923 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006924 if (did_free_funccal)
6925 /* When a funccal was freed some more items might be garbage
6926 * collected, so run again. */
6927 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006928 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 else if (p_verbose > 0)
6930 {
6931 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6932 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933
6934 return did_free;
6935}
6936
6937/*
6938 * Free lists and dictionaries that are no longer referenced.
6939 */
6940 static int
6941free_unref_items(copyID)
6942 int copyID;
6943{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006944 dict_T *dd, *dd_next;
6945 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006946 int did_free = FALSE;
6947
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006948 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950 */
6951 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006952 {
6953 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006955 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006956 /* Free the Dictionary and ordinary items it contains, but don't
6957 * recurse into Lists and Dictionaries, they will be in the list
6958 * of dicts or list of lists. */
6959 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006962 dd = dd_next;
6963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964
6965 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966 * Go through the list of lists and free items without the copyID.
6967 * But don't free a list that has a watcher (used in a for loop), these
6968 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 */
6970 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006971 {
6972 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6974 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006976 /* Free the List and ordinary items it contains, but don't recurse
6977 * into Lists and Dictionaries, they will be in the list of dicts
6978 * or list of lists. */
6979 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006982 ll = ll_next;
6983 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 return did_free;
6985}
6986
6987/*
6988 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006989 * "list_stack" is used to add lists to be marked. Can be NULL.
6990 *
6991 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006993 int
6994set_ref_in_ht(ht, copyID, list_stack)
6995 hashtab_T *ht;
6996 int copyID;
6997 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998{
6999 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007000 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007002 hashtab_T *cur_ht;
7003 ht_stack_T *ht_stack = NULL;
7004 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007006 cur_ht = ht;
7007 for (;;)
7008 {
7009 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007011 /* Mark each item in the hashtab. If the item contains a hashtab
7012 * it is added to ht_stack, if it contains a list it is added to
7013 * list_stack. */
7014 todo = (int)cur_ht->ht_used;
7015 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7016 if (!HASHITEM_EMPTY(hi))
7017 {
7018 --todo;
7019 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7020 &ht_stack, list_stack);
7021 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023
7024 if (ht_stack == NULL)
7025 break;
7026
7027 /* take an item from the stack */
7028 cur_ht = ht_stack->ht;
7029 tempitem = ht_stack;
7030 ht_stack = ht_stack->prev;
7031 free(tempitem);
7032 }
7033
7034 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035}
7036
7037/*
7038 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7040 *
7041 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007042 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007043 int
7044set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 list_T *l;
7046 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049 listitem_T *li;
7050 int abort = FALSE;
7051 list_T *cur_l;
7052 list_stack_T *list_stack = NULL;
7053 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007054
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007055 cur_l = l;
7056 for (;;)
7057 {
7058 if (!abort)
7059 /* Mark each item in the list. If the item contains a hashtab
7060 * it is added to ht_stack, if it contains a list it is added to
7061 * list_stack. */
7062 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7063 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7064 ht_stack, &list_stack);
7065 if (list_stack == NULL)
7066 break;
7067
7068 /* take an item from the stack */
7069 cur_l = list_stack->list;
7070 tempitem = list_stack;
7071 list_stack = list_stack->prev;
7072 free(tempitem);
7073 }
7074
7075 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076}
7077
7078/*
7079 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 * "list_stack" is used to add lists to be marked. Can be NULL.
7081 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7082 *
7083 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 int
7086set_ref_in_item(tv, copyID, ht_stack, list_stack)
7087 typval_T *tv;
7088 int copyID;
7089 ht_stack_T **ht_stack;
7090 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007091{
7092 dict_T *dd;
7093 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007094 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007095
7096 switch (tv->v_type)
7097 {
7098 case VAR_DICT:
7099 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007100 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007101 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102 /* Didn't see this dict yet. */
7103 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 if (ht_stack == NULL)
7105 {
7106 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7107 }
7108 else
7109 {
7110 ht_stack_T *newitem = (ht_stack_T*)malloc(
7111 sizeof(ht_stack_T));
7112 if (newitem == NULL)
7113 abort = TRUE;
7114 else
7115 {
7116 newitem->ht = &dd->dv_hashtab;
7117 newitem->prev = *ht_stack;
7118 *ht_stack = newitem;
7119 }
7120 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007121 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007122 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007123
7124 case VAR_LIST:
7125 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007126 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007127 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007128 /* Didn't see this list yet. */
7129 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 if (list_stack == NULL)
7131 {
7132 abort = set_ref_in_list(ll, copyID, ht_stack);
7133 }
7134 else
7135 {
7136 list_stack_T *newitem = (list_stack_T*)malloc(
7137 sizeof(list_stack_T));
7138 if (newitem == NULL)
7139 abort = TRUE;
7140 else
7141 {
7142 newitem->list = ll;
7143 newitem->prev = *list_stack;
7144 *list_stack = newitem;
7145 }
7146 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007148 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007149 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007151}
7152
7153/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154 * Allocate an empty header for a dictionary.
7155 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007156 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007157dict_alloc()
7158{
Bram Moolenaar33570922005-01-25 22:26:29 +00007159 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007160
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007163 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007164 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007165 if (first_dict != NULL)
7166 first_dict->dv_used_prev = d;
7167 d->dv_used_next = first_dict;
7168 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007169 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007170
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007172 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007173 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007174 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007175 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007176 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178}
7179
7180/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007181 * Allocate an empty dict for a return value.
7182 * Returns OK or FAIL.
7183 */
7184 static int
7185rettv_dict_alloc(rettv)
7186 typval_T *rettv;
7187{
7188 dict_T *d = dict_alloc();
7189
7190 if (d == NULL)
7191 return FAIL;
7192
7193 rettv->vval.v_dict = d;
7194 rettv->v_type = VAR_DICT;
7195 ++d->dv_refcount;
7196 return OK;
7197}
7198
7199
7200/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201 * Unreference a Dictionary: decrement the reference count and free it when it
7202 * becomes zero.
7203 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007204 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007206 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007208 if (d != NULL && --d->dv_refcount <= 0)
7209 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210}
7211
7212/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007213 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214 * Ignores the reference count.
7215 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007216 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007217dict_free(d, recurse)
7218 dict_T *d;
7219 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007221 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007222 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007223 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007225 /* Remove the dict from the list of dicts for garbage collection. */
7226 if (d->dv_used_prev == NULL)
7227 first_dict = d->dv_used_next;
7228 else
7229 d->dv_used_prev->dv_used_next = d->dv_used_next;
7230 if (d->dv_used_next != NULL)
7231 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7232
7233 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007234 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007235 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007236 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007238 if (!HASHITEM_EMPTY(hi))
7239 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007240 /* Remove the item before deleting it, just in case there is
7241 * something recursive causing trouble. */
7242 di = HI2DI(hi);
7243 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007244 if (recurse || (di->di_tv.v_type != VAR_LIST
7245 && di->di_tv.v_type != VAR_DICT))
7246 clear_tv(&di->di_tv);
7247 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007248 --todo;
7249 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 vim_free(d);
7253}
7254
7255/*
7256 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007257 * The "key" is copied to the new item.
7258 * Note that the value of the item "di_tv" still needs to be initialized!
7259 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007261 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007262dictitem_alloc(key)
7263 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264{
Bram Moolenaar33570922005-01-25 22:26:29 +00007265 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007266
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007267 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007268 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007269 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007271 di->di_flags = 0;
7272 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274}
7275
7276/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007277 * Make a copy of a Dictionary item.
7278 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282{
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007284
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007285 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7286 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 if (di != NULL)
7288 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007290 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007291 copy_tv(&org->di_tv, &di->di_tv);
7292 }
7293 return di;
7294}
7295
7296/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297 * Remove item "item" from Dictionary "dict" and free it.
7298 */
7299 static void
7300dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dict_T *dict;
7302 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303{
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305
Bram Moolenaar33570922005-01-25 22:26:29 +00007306 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 if (HASHITEM_EMPTY(hi))
7308 EMSG2(_(e_intern2), "dictitem_remove()");
7309 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 dictitem_free(item);
7312}
7313
7314/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315 * Free a dict item. Also clears the value.
7316 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007317 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007318dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007320{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321 clear_tv(&item->di_tv);
7322 vim_free(item);
7323}
7324
7325/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7327 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007328 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 * Returns NULL when out of memory.
7330 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007332dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007333 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007335 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336{
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 dict_T *copy;
7338 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007341
7342 if (orig == NULL)
7343 return NULL;
7344
7345 copy = dict_alloc();
7346 if (copy != NULL)
7347 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007348 if (copyID != 0)
7349 {
7350 orig->dv_copyID = copyID;
7351 orig->dv_copydict = copy;
7352 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007353 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007354 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007356 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007357 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 --todo;
7359
7360 di = dictitem_alloc(hi->hi_key);
7361 if (di == NULL)
7362 break;
7363 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007364 {
7365 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7366 copyID) == FAIL)
7367 {
7368 vim_free(di);
7369 break;
7370 }
7371 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007372 else
7373 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7374 if (dict_add(copy, di) == FAIL)
7375 {
7376 dictitem_free(di);
7377 break;
7378 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007381
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007383 if (todo > 0)
7384 {
7385 dict_unref(copy);
7386 copy = NULL;
7387 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 }
7389
7390 return copy;
7391}
7392
7393/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007395 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007397 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007399 dict_T *d;
7400 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401{
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007403}
7404
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007406 * Add a number or string entry to dictionary "d".
7407 * When "str" is NULL use number "nr", otherwise use "str".
7408 * Returns FAIL when out of memory and when key already exists.
7409 */
7410 int
7411dict_add_nr_str(d, key, nr, str)
7412 dict_T *d;
7413 char *key;
7414 long nr;
7415 char_u *str;
7416{
7417 dictitem_T *item;
7418
7419 item = dictitem_alloc((char_u *)key);
7420 if (item == NULL)
7421 return FAIL;
7422 item->di_tv.v_lock = 0;
7423 if (str == NULL)
7424 {
7425 item->di_tv.v_type = VAR_NUMBER;
7426 item->di_tv.vval.v_number = nr;
7427 }
7428 else
7429 {
7430 item->di_tv.v_type = VAR_STRING;
7431 item->di_tv.vval.v_string = vim_strsave(str);
7432 }
7433 if (dict_add(d, item) == FAIL)
7434 {
7435 dictitem_free(item);
7436 return FAIL;
7437 }
7438 return OK;
7439}
7440
7441/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007442 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007443 * Returns FAIL when out of memory and when key already exists.
7444 */
7445 int
7446dict_add_list(d, key, list)
7447 dict_T *d;
7448 char *key;
7449 list_T *list;
7450{
7451 dictitem_T *item;
7452
7453 item = dictitem_alloc((char_u *)key);
7454 if (item == NULL)
7455 return FAIL;
7456 item->di_tv.v_lock = 0;
7457 item->di_tv.v_type = VAR_LIST;
7458 item->di_tv.vval.v_list = list;
7459 if (dict_add(d, item) == FAIL)
7460 {
7461 dictitem_free(item);
7462 return FAIL;
7463 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007464 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007465 return OK;
7466}
7467
7468/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469 * Get the number of items in a Dictionary.
7470 */
7471 static long
7472dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007473 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 if (d == NULL)
7476 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007477 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478}
7479
7480/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481 * Find item "key[len]" in Dictionary "d".
7482 * If "len" is negative use strlen(key).
7483 * Returns NULL when not found.
7484 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007485 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007486dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007487 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 char_u *key;
7489 int len;
7490{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007491#define AKEYLEN 200
7492 char_u buf[AKEYLEN];
7493 char_u *akey;
7494 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007497 if (len < 0)
7498 akey = key;
7499 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007500 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007501 tofree = akey = vim_strnsave(key, len);
7502 if (akey == NULL)
7503 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007504 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 else
7506 {
7507 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007508 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 akey = buf;
7510 }
7511
Bram Moolenaar33570922005-01-25 22:26:29 +00007512 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 vim_free(tofree);
7514 if (HASHITEM_EMPTY(hi))
7515 return NULL;
7516 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517}
7518
7519/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007520 * Get a string item from a dictionary.
7521 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007522 * Returns NULL if the entry doesn't exist or out of memory.
7523 */
7524 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007525get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007526 dict_T *d;
7527 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007528 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007529{
7530 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007531 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007532
7533 di = dict_find(d, key, -1);
7534 if (di == NULL)
7535 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007536 s = get_tv_string(&di->di_tv);
7537 if (save && s != NULL)
7538 s = vim_strsave(s);
7539 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007540}
7541
7542/*
7543 * Get a number item from a dictionary.
7544 * Returns 0 if the entry doesn't exist or out of memory.
7545 */
7546 long
7547get_dict_number(d, key)
7548 dict_T *d;
7549 char_u *key;
7550{
7551 dictitem_T *di;
7552
7553 di = dict_find(d, key, -1);
7554 if (di == NULL)
7555 return 0;
7556 return get_tv_number(&di->di_tv);
7557}
7558
7559/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 * Return an allocated string with the string representation of a Dictionary.
7561 * May return NULL.
7562 */
7563 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007564dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007566 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567{
7568 garray_T ga;
7569 int first = TRUE;
7570 char_u *tofree;
7571 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007574 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 return NULL;
7579 ga_init2(&ga, (int)sizeof(char), 80);
7580 ga_append(&ga, '{');
7581
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007582 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007583 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007585 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007587 --todo;
7588
7589 if (first)
7590 first = FALSE;
7591 else
7592 ga_concat(&ga, (char_u *)", ");
7593
7594 tofree = string_quote(hi->hi_key, FALSE);
7595 if (tofree != NULL)
7596 {
7597 ga_concat(&ga, tofree);
7598 vim_free(tofree);
7599 }
7600 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007602 if (s != NULL)
7603 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007605 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007606 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007607 line_breakcheck();
7608
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007611 if (todo > 0)
7612 {
7613 vim_free(ga.ga_data);
7614 return NULL;
7615 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616
7617 ga_append(&ga, '}');
7618 ga_append(&ga, NUL);
7619 return (char_u *)ga.ga_data;
7620}
7621
7622/*
7623 * Allocate a variable for a Dictionary and fill it from "*arg".
7624 * Return OK or FAIL. Returns NOTDONE for {expr}.
7625 */
7626 static int
7627get_dict_tv(arg, rettv, evaluate)
7628 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007629 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 int evaluate;
7631{
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 dict_T *d = NULL;
7633 typval_T tvkey;
7634 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007635 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639
7640 /*
7641 * First check if it's not a curly-braces thing: {expr}.
7642 * Must do this without evaluating, otherwise a function may be called
7643 * twice. Unfortunately this means we need to call eval1() twice for the
7644 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 if (*start != '}')
7648 {
7649 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7650 return FAIL;
7651 if (*start == '}')
7652 return NOTDONE;
7653 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654
7655 if (evaluate)
7656 {
7657 d = dict_alloc();
7658 if (d == NULL)
7659 return FAIL;
7660 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 tvkey.v_type = VAR_UNKNOWN;
7662 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663
7664 *arg = skipwhite(*arg + 1);
7665 while (**arg != '}' && **arg != NUL)
7666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 goto failret;
7669 if (**arg != ':')
7670 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007671 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007672 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 goto failret;
7674 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007675 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007677 key = get_tv_string_buf_chk(&tvkey, buf);
7678 if (key == NULL || *key == NUL)
7679 {
7680 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7681 if (key != NULL)
7682 EMSG(_(e_emptykey));
7683 clear_tv(&tvkey);
7684 goto failret;
7685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687
7688 *arg = skipwhite(*arg + 1);
7689 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7690 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007691 if (evaluate)
7692 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 goto failret;
7694 }
7695 if (evaluate)
7696 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007697 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 if (item != NULL)
7699 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007700 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007701 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 clear_tv(&tv);
7703 goto failret;
7704 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007705 item = dictitem_alloc(key);
7706 clear_tv(&tvkey);
7707 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007709 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007710 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007711 if (dict_add(d, item) == FAIL)
7712 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713 }
7714 }
7715
7716 if (**arg == '}')
7717 break;
7718 if (**arg != ',')
7719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007720 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 goto failret;
7722 }
7723 *arg = skipwhite(*arg + 1);
7724 }
7725
7726 if (**arg != '}')
7727 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007728 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729failret:
7730 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007731 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 return FAIL;
7733 }
7734
7735 *arg = skipwhite(*arg + 1);
7736 if (evaluate)
7737 {
7738 rettv->v_type = VAR_DICT;
7739 rettv->vval.v_dict = d;
7740 ++d->dv_refcount;
7741 }
7742
7743 return OK;
7744}
7745
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007747 * Return a string with the string representation of a variable.
7748 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007749 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007750 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007751 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007752 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007753 */
7754 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007755echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007756 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007757 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007758 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007759 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007760{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007761 static int recurse = 0;
7762 char_u *r = NULL;
7763
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007765 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007766 if (!did_echo_string_emsg)
7767 {
7768 /* Only give this message once for a recursive call to avoid
7769 * flooding the user with errors. And stop iterating over lists
7770 * and dicts. */
7771 did_echo_string_emsg = TRUE;
7772 EMSG(_("E724: variable nested too deep for displaying"));
7773 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007774 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007775 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007776 }
7777 ++recurse;
7778
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 switch (tv->v_type)
7780 {
7781 case VAR_FUNC:
7782 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007783 r = tv->vval.v_string;
7784 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007785
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007786 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007787 if (tv->vval.v_list == NULL)
7788 {
7789 *tofree = NULL;
7790 r = NULL;
7791 }
7792 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7793 {
7794 *tofree = NULL;
7795 r = (char_u *)"[...]";
7796 }
7797 else
7798 {
7799 tv->vval.v_list->lv_copyID = copyID;
7800 *tofree = list2string(tv, copyID);
7801 r = *tofree;
7802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007803 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007804
Bram Moolenaar8c711452005-01-14 21:53:12 +00007805 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007806 if (tv->vval.v_dict == NULL)
7807 {
7808 *tofree = NULL;
7809 r = NULL;
7810 }
7811 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7812 {
7813 *tofree = NULL;
7814 r = (char_u *)"{...}";
7815 }
7816 else
7817 {
7818 tv->vval.v_dict->dv_copyID = copyID;
7819 *tofree = dict2string(tv, copyID);
7820 r = *tofree;
7821 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007823
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007824 case VAR_STRING:
7825 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826 *tofree = NULL;
7827 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007828 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007829
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007830#ifdef FEAT_FLOAT
7831 case VAR_FLOAT:
7832 *tofree = NULL;
7833 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7834 r = numbuf;
7835 break;
7836#endif
7837
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007838 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007839 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007840 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842
Bram Moolenaar8502c702014-06-17 12:51:16 +02007843 if (--recurse == 0)
7844 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007846}
7847
7848/*
7849 * Return a string with the string representation of a variable.
7850 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7851 * "numbuf" is used for a number.
7852 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007853 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007854 */
7855 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007856tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007857 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007858 char_u **tofree;
7859 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007860 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007861{
7862 switch (tv->v_type)
7863 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007864 case VAR_FUNC:
7865 *tofree = string_quote(tv->vval.v_string, TRUE);
7866 return *tofree;
7867 case VAR_STRING:
7868 *tofree = string_quote(tv->vval.v_string, FALSE);
7869 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870#ifdef FEAT_FLOAT
7871 case VAR_FLOAT:
7872 *tofree = NULL;
7873 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7874 return numbuf;
7875#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007877 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007878 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007882 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007883 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007884}
7885
7886/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007887 * Return string "str" in ' quotes, doubling ' characters.
7888 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007889 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890 */
7891 static char_u *
7892string_quote(str, function)
7893 char_u *str;
7894 int function;
7895{
Bram Moolenaar33570922005-01-25 22:26:29 +00007896 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897 char_u *p, *r, *s;
7898
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 len = (function ? 13 : 3);
7900 if (str != NULL)
7901 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007902 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 for (p = str; *p != NUL; mb_ptr_adv(p))
7904 if (*p == '\'')
7905 ++len;
7906 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907 s = r = alloc(len);
7908 if (r != NULL)
7909 {
7910 if (function)
7911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007912 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007913 r += 10;
7914 }
7915 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007916 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007917 if (str != NULL)
7918 for (p = str; *p != NUL; )
7919 {
7920 if (*p == '\'')
7921 *r++ = '\'';
7922 MB_COPY_CHAR(p, r);
7923 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007924 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007925 if (function)
7926 *r++ = ')';
7927 *r++ = NUL;
7928 }
7929 return s;
7930}
7931
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007932#ifdef FEAT_FLOAT
7933/*
7934 * Convert the string "text" to a floating point number.
7935 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7936 * this always uses a decimal point.
7937 * Returns the length of the text that was consumed.
7938 */
7939 static int
7940string2float(text, value)
7941 char_u *text;
7942 float_T *value; /* result stored here */
7943{
7944 char *s = (char *)text;
7945 float_T f;
7946
7947 f = strtod(s, &s);
7948 *value = f;
7949 return (int)((char_u *)s - text);
7950}
7951#endif
7952
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 * Get the value of an environment variable.
7955 * "arg" is pointing to the '$'. It is advanced to after the name.
7956 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007957 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 */
7959 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007960get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 int evaluate;
7964{
7965 char_u *string = NULL;
7966 int len;
7967 int cc;
7968 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007969 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970
7971 ++*arg;
7972 name = *arg;
7973 len = get_env_len(arg);
7974 if (evaluate)
7975 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007976 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007977 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007978
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007979 cc = name[len];
7980 name[len] = NUL;
7981 /* first try vim_getenv(), fast for normal environment vars */
7982 string = vim_getenv(name, &mustfree);
7983 if (string != NULL && *string != NUL)
7984 {
7985 if (!mustfree)
7986 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007988 else
7989 {
7990 if (mustfree)
7991 vim_free(string);
7992
7993 /* next try expanding things like $VIM and ${HOME} */
7994 string = expand_env_save(name - 1);
7995 if (string != NULL && *string == '$')
7996 {
7997 vim_free(string);
7998 string = NULL;
7999 }
8000 }
8001 name[len] = cc;
8002
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008003 rettv->v_type = VAR_STRING;
8004 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 }
8006
8007 return OK;
8008}
8009
8010/*
8011 * Array with names and number of arguments of all internal functions
8012 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8013 */
8014static struct fst
8015{
8016 char *f_name; /* function name */
8017 char f_min_argc; /* minimal number of arguments */
8018 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008019 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008020 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021} functions[] =
8022{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008023#ifdef FEAT_FLOAT
8024 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008025 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008026#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008027 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008028 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"append", 2, 2, f_append},
8030 {"argc", 0, 0, f_argc},
8031 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008032 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008033 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008034#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008035 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008036 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008037 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008040 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {"bufexists", 1, 1, f_bufexists},
8042 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8043 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8044 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8045 {"buflisted", 1, 1, f_buflisted},
8046 {"bufloaded", 1, 1, f_bufloaded},
8047 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008048 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"bufwinnr", 1, 1, f_bufwinnr},
8050 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008051 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008052 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008053 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008054#ifdef FEAT_FLOAT
8055 {"ceil", 1, 1, f_ceil},
8056#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008057 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008058 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008060 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008062#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008063 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008064 {"complete_add", 1, 1, f_complete_add},
8065 {"complete_check", 0, 0, f_complete_check},
8066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008068 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008069#ifdef FEAT_FLOAT
8070 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008071 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008073 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008075 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008076 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"delete", 1, 1, f_delete},
8078 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008079 {"diff_filler", 1, 1, f_diff_filler},
8080 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008081 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008083 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {"eventhandler", 0, 0, f_eventhandler},
8085 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008086 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008088#ifdef FEAT_FLOAT
8089 {"exp", 1, 1, f_exp},
8090#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008091 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008092 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008093 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8095 {"filereadable", 1, 1, f_filereadable},
8096 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008097 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008098 {"finddir", 1, 3, f_finddir},
8099 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008100#ifdef FEAT_FLOAT
8101 {"float2nr", 1, 1, f_float2nr},
8102 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008103 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008104#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008105 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"fnamemodify", 2, 2, f_fnamemodify},
8107 {"foldclosed", 1, 1, f_foldclosed},
8108 {"foldclosedend", 1, 1, f_foldclosedend},
8109 {"foldlevel", 1, 1, f_foldlevel},
8110 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008111 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008113 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008114 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008115 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008116 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008117 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"getchar", 0, 1, f_getchar},
8119 {"getcharmod", 0, 0, f_getcharmod},
8120 {"getcmdline", 0, 0, f_getcmdline},
8121 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008122 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008123 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008124 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008126 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008127 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"getfsize", 1, 1, f_getfsize},
8129 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008130 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008131 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008132 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008133 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008134 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008135 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008136 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008137 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008139 {"gettabvar", 2, 3, f_gettabvar},
8140 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"getwinposx", 0, 0, f_getwinposx},
8142 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008143 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008144 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02008145 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008147 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008148 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008149 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8151 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8152 {"histadd", 2, 2, f_histadd},
8153 {"histdel", 1, 2, f_histdel},
8154 {"histget", 1, 2, f_histget},
8155 {"histnr", 1, 1, f_histnr},
8156 {"hlID", 1, 1, f_hlID},
8157 {"hlexists", 1, 1, f_hlexists},
8158 {"hostname", 0, 0, f_hostname},
8159 {"iconv", 3, 3, f_iconv},
8160 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008161 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008162 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008164 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"inputrestore", 0, 0, f_inputrestore},
8166 {"inputsave", 0, 0, f_inputsave},
8167 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008168 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008169 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008171 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008172 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008173 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008174 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008176 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"libcall", 3, 3, f_libcall},
8178 {"libcallnr", 3, 3, f_libcallnr},
8179 {"line", 1, 1, f_line},
8180 {"line2byte", 1, 1, f_line2byte},
8181 {"lispindent", 1, 1, f_lispindent},
8182 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008183#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008184 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008185 {"log10", 1, 1, f_log10},
8186#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008187#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008188 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008189#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008190 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008191 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008192 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008193 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008194 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008195 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008196 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008197 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008198 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008199 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008200 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008201 {"max", 1, 1, f_max},
8202 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008203#ifdef vim_mkdir
8204 {"mkdir", 1, 3, f_mkdir},
8205#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008206 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008207#ifdef FEAT_MZSCHEME
8208 {"mzeval", 1, 1, f_mzeval},
8209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008211 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008212 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008213 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008214#ifdef FEAT_FLOAT
8215 {"pow", 2, 2, f_pow},
8216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008218 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008219 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008220#ifdef FEAT_PYTHON3
8221 {"py3eval", 1, 1, f_py3eval},
8222#endif
8223#ifdef FEAT_PYTHON
8224 {"pyeval", 1, 1, f_pyeval},
8225#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008226 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008227 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008228 {"reltime", 0, 2, f_reltime},
8229 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"remote_expr", 2, 3, f_remote_expr},
8231 {"remote_foreground", 1, 1, f_remote_foreground},
8232 {"remote_peek", 1, 2, f_remote_peek},
8233 {"remote_read", 1, 1, f_remote_read},
8234 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008235 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008237 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008239 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008240#ifdef FEAT_FLOAT
8241 {"round", 1, 1, f_round},
8242#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008243 {"screenattr", 2, 2, f_screenattr},
8244 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008245 {"screencol", 0, 0, f_screencol},
8246 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008247 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008248 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008249 {"searchpair", 3, 7, f_searchpair},
8250 {"searchpairpos", 3, 7, f_searchpairpos},
8251 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {"server2client", 2, 2, f_server2client},
8253 {"serverlist", 0, 0, f_serverlist},
8254 {"setbufvar", 3, 3, f_setbufvar},
8255 {"setcmdpos", 1, 1, f_setcmdpos},
8256 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008257 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008258 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008259 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008260 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008262 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008263 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008265#ifdef FEAT_CRYPT
8266 {"sha256", 1, 1, f_sha256},
8267#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008268 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008269 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008271#ifdef FEAT_FLOAT
8272 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008273 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008274#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008275 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008276 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008277 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008278 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008279 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008280#ifdef FEAT_FLOAT
8281 {"sqrt", 1, 1, f_sqrt},
8282 {"str2float", 1, 1, f_str2float},
8283#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008284 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008285 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008286 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287#ifdef HAVE_STRFTIME
8288 {"strftime", 1, 2, f_strftime},
8289#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008290 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008291 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"strlen", 1, 1, f_strlen},
8293 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008294 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008296 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008297 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"substitute", 4, 4, f_substitute},
8299 {"synID", 3, 3, f_synID},
8300 {"synIDattr", 2, 3, f_synIDattr},
8301 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008302 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008303 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008304 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008305 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008306 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008307 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008308 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008309 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008310 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008311#ifdef FEAT_FLOAT
8312 {"tan", 1, 1, f_tan},
8313 {"tanh", 1, 1, f_tanh},
8314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008316 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"tolower", 1, 1, f_tolower},
8318 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008319 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008320#ifdef FEAT_FLOAT
8321 {"trunc", 1, 1, f_trunc},
8322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008324 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008325 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008326 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008327 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"virtcol", 1, 1, f_virtcol},
8329 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008330 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {"winbufnr", 1, 1, f_winbufnr},
8332 {"wincol", 0, 0, f_wincol},
8333 {"winheight", 1, 1, f_winheight},
8334 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008335 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008337 {"winrestview", 1, 1, f_winrestview},
8338 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008340 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008341 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342};
8343
8344#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8345
8346/*
8347 * Function given to ExpandGeneric() to obtain the list of internal
8348 * or user defined function names.
8349 */
8350 char_u *
8351get_function_name(xp, idx)
8352 expand_T *xp;
8353 int idx;
8354{
8355 static int intidx = -1;
8356 char_u *name;
8357
8358 if (idx == 0)
8359 intidx = -1;
8360 if (intidx < 0)
8361 {
8362 name = get_user_func_name(xp, idx);
8363 if (name != NULL)
8364 return name;
8365 }
8366 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8367 {
8368 STRCPY(IObuff, functions[intidx].f_name);
8369 STRCAT(IObuff, "(");
8370 if (functions[intidx].f_max_argc == 0)
8371 STRCAT(IObuff, ")");
8372 return IObuff;
8373 }
8374
8375 return NULL;
8376}
8377
8378/*
8379 * Function given to ExpandGeneric() to obtain the list of internal or
8380 * user defined variable or function names.
8381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 char_u *
8383get_expr_name(xp, idx)
8384 expand_T *xp;
8385 int idx;
8386{
8387 static int intidx = -1;
8388 char_u *name;
8389
8390 if (idx == 0)
8391 intidx = -1;
8392 if (intidx < 0)
8393 {
8394 name = get_function_name(xp, idx);
8395 if (name != NULL)
8396 return name;
8397 }
8398 return get_user_var_name(xp, ++intidx);
8399}
8400
8401#endif /* FEAT_CMDL_COMPL */
8402
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008403#if defined(EBCDIC) || defined(PROTO)
8404/*
8405 * Compare struct fst by function name.
8406 */
8407 static int
8408compare_func_name(s1, s2)
8409 const void *s1;
8410 const void *s2;
8411{
8412 struct fst *p1 = (struct fst *)s1;
8413 struct fst *p2 = (struct fst *)s2;
8414
8415 return STRCMP(p1->f_name, p2->f_name);
8416}
8417
8418/*
8419 * Sort the function table by function name.
8420 * The sorting of the table above is ASCII dependant.
8421 * On machines using EBCDIC we have to sort it.
8422 */
8423 static void
8424sortFunctions()
8425{
8426 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8427
8428 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8429}
8430#endif
8431
8432
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433/*
8434 * Find internal function in table above.
8435 * Return index, or -1 if not found
8436 */
8437 static int
8438find_internal_func(name)
8439 char_u *name; /* name of the function */
8440{
8441 int first = 0;
8442 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8443 int cmp;
8444 int x;
8445
8446 /*
8447 * Find the function name in the table. Binary search.
8448 */
8449 while (first <= last)
8450 {
8451 x = first + ((unsigned)(last - first) >> 1);
8452 cmp = STRCMP(name, functions[x].f_name);
8453 if (cmp < 0)
8454 last = x - 1;
8455 else if (cmp > 0)
8456 first = x + 1;
8457 else
8458 return x;
8459 }
8460 return -1;
8461}
8462
8463/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008464 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8465 * name it contains, otherwise return "name".
8466 */
8467 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008468deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008469 char_u *name;
8470 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008471 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008472{
Bram Moolenaar33570922005-01-25 22:26:29 +00008473 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008474 int cc;
8475
8476 cc = name[*lenp];
8477 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008478 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008479 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008480 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008481 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008482 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008483 {
8484 *lenp = 0;
8485 return (char_u *)""; /* just in case */
8486 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008487 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008488 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008489 }
8490
8491 return name;
8492}
8493
8494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 * Allocate a variable for the result of a function.
8496 * Return OK or FAIL.
8497 */
8498 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008499get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8500 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 char_u *name; /* name of the function */
8502 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 char_u **arg; /* argument, pointing to the '(' */
8505 linenr_T firstline; /* first line of range */
8506 linenr_T lastline; /* last line of range */
8507 int *doesrange; /* return: function handled range */
8508 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008509 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510{
8511 char_u *argp;
8512 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008513 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 int argcount = 0; /* number of arguments found */
8515
8516 /*
8517 * Get the arguments.
8518 */
8519 argp = *arg;
8520 while (argcount < MAX_FUNC_ARGS)
8521 {
8522 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8523 if (*argp == ')' || *argp == ',' || *argp == NUL)
8524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8526 {
8527 ret = FAIL;
8528 break;
8529 }
8530 ++argcount;
8531 if (*argp != ',')
8532 break;
8533 }
8534 if (*argp == ')')
8535 ++argp;
8536 else
8537 ret = FAIL;
8538
8539 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008541 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 {
8544 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008545 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008547 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549
8550 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008551 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
8553 *arg = skipwhite(argp);
8554 return ret;
8555}
8556
8557
8558/*
8559 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008560 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008561 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 */
8563 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008564call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008565 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008566 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008568 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008570 typval_T *argvars; /* vars for arguments, must have "argcount"
8571 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 linenr_T firstline; /* first line of range */
8573 linenr_T lastline; /* last line of range */
8574 int *doesrange; /* return: function handled range */
8575 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577{
8578 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579#define ERROR_UNKNOWN 0
8580#define ERROR_TOOMANY 1
8581#define ERROR_TOOFEW 2
8582#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008583#define ERROR_DICT 4
8584#define ERROR_NONE 5
8585#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 int error = ERROR_NONE;
8587 int i;
8588 int llen;
8589 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590#define FLEN_FIXED 40
8591 char_u fname_buf[FLEN_FIXED + 1];
8592 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008593 char_u *name;
8594
8595 /* Make a copy of the name, if it comes from a funcref variable it could
8596 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008597 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008598 if (name == NULL)
8599 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600
8601 /*
8602 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8603 * Change <SNR>123_name() to K_SNR 123_name().
8604 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 llen = eval_fname_script(name);
8607 if (llen > 0)
8608 {
8609 fname_buf[0] = K_SPECIAL;
8610 fname_buf[1] = KS_EXTRA;
8611 fname_buf[2] = (int)KE_SNR;
8612 i = 3;
8613 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8614 {
8615 if (current_SID <= 0)
8616 error = ERROR_SCRIPT;
8617 else
8618 {
8619 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8620 i = (int)STRLEN(fname_buf);
8621 }
8622 }
8623 if (i + STRLEN(name + llen) < FLEN_FIXED)
8624 {
8625 STRCPY(fname_buf + i, name + llen);
8626 fname = fname_buf;
8627 }
8628 else
8629 {
8630 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8631 if (fname == NULL)
8632 error = ERROR_OTHER;
8633 else
8634 {
8635 mch_memmove(fname, fname_buf, (size_t)i);
8636 STRCPY(fname + i, name + llen);
8637 }
8638 }
8639 }
8640 else
8641 fname = name;
8642
8643 *doesrange = FALSE;
8644
8645
8646 /* execute the function if no errors detected and executing */
8647 if (evaluate && error == ERROR_NONE)
8648 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008649 char_u *rfname = fname;
8650
8651 /* Ignore "g:" before a function name. */
8652 if (fname[0] == 'g' && fname[1] == ':')
8653 rfname = fname + 2;
8654
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008655 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8656 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 error = ERROR_UNKNOWN;
8658
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008659 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 {
8661 /*
8662 * User defined function.
8663 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008664 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008665
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008667 /* Trigger FuncUndefined event, may load the function. */
8668 if (fp == NULL
8669 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008670 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008671 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008673 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008674 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 }
8676#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008677 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008678 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008679 {
8680 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008681 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008682 }
8683
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 if (fp != NULL)
8685 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008686 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008688 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008690 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008692 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008693 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 else
8695 {
8696 /*
8697 * Call the user function.
8698 * Save and restore search patterns, script variables and
8699 * redo buffer.
8700 */
8701 save_search_patterns();
8702 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008703 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008704 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008705 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008706 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8707 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8708 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008709 /* Function was unreferenced while being used, free it
8710 * now. */
8711 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 restoreRedobuff();
8713 restore_search_patterns();
8714 error = ERROR_NONE;
8715 }
8716 }
8717 }
8718 else
8719 {
8720 /*
8721 * Find the function name in the table, call its implementation.
8722 */
8723 i = find_internal_func(fname);
8724 if (i >= 0)
8725 {
8726 if (argcount < functions[i].f_min_argc)
8727 error = ERROR_TOOFEW;
8728 else if (argcount > functions[i].f_max_argc)
8729 error = ERROR_TOOMANY;
8730 else
8731 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008732 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008733 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 error = ERROR_NONE;
8735 }
8736 }
8737 }
8738 /*
8739 * The function call (or "FuncUndefined" autocommand sequence) might
8740 * have been aborted by an error, an interrupt, or an explicitly thrown
8741 * exception that has not been caught so far. This situation can be
8742 * tested for by calling aborting(). For an error in an internal
8743 * function or for the "E132" error in call_user_func(), however, the
8744 * throw point at which the "force_abort" flag (temporarily reset by
8745 * emsg()) is normally updated has not been reached yet. We need to
8746 * update that flag first to make aborting() reliable.
8747 */
8748 update_force_abort();
8749 }
8750 if (error == ERROR_NONE)
8751 ret = OK;
8752
8753 /*
8754 * Report an error unless the argument evaluation or function call has been
8755 * cancelled due to an aborting error, an interrupt, or an exception.
8756 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008757 if (!aborting())
8758 {
8759 switch (error)
8760 {
8761 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008762 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008763 break;
8764 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008765 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008766 break;
8767 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008768 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008769 name);
8770 break;
8771 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008772 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008773 name);
8774 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008775 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008776 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008777 name);
8778 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008779 }
8780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 if (fname != name && fname != fname_buf)
8783 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008784 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786 return ret;
8787}
8788
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008789/*
8790 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008791 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008792 */
8793 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008794emsg_funcname(ermsg, name)
8795 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008796 char_u *name;
8797{
8798 char_u *p;
8799
8800 if (*name == K_SPECIAL)
8801 p = concat_str((char_u *)"<SNR>", name + 3);
8802 else
8803 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008804 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008805 if (p != name)
8806 vim_free(p);
8807}
8808
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008809/*
8810 * Return TRUE for a non-zero Number and a non-empty String.
8811 */
8812 static int
8813non_zero_arg(argvars)
8814 typval_T *argvars;
8815{
8816 return ((argvars[0].v_type == VAR_NUMBER
8817 && argvars[0].vval.v_number != 0)
8818 || (argvars[0].v_type == VAR_STRING
8819 && argvars[0].vval.v_string != NULL
8820 && *argvars[0].vval.v_string != NUL));
8821}
8822
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823/*********************************************
8824 * Implementation of the built-in functions
8825 */
8826
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008827#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008828static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8829
8830/*
8831 * Get the float value of "argvars[0]" into "f".
8832 * Returns FAIL when the argument is not a Number or Float.
8833 */
8834 static int
8835get_float_arg(argvars, f)
8836 typval_T *argvars;
8837 float_T *f;
8838{
8839 if (argvars[0].v_type == VAR_FLOAT)
8840 {
8841 *f = argvars[0].vval.v_float;
8842 return OK;
8843 }
8844 if (argvars[0].v_type == VAR_NUMBER)
8845 {
8846 *f = (float_T)argvars[0].vval.v_number;
8847 return OK;
8848 }
8849 EMSG(_("E808: Number or Float required"));
8850 return FAIL;
8851}
8852
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008853/*
8854 * "abs(expr)" function
8855 */
8856 static void
8857f_abs(argvars, rettv)
8858 typval_T *argvars;
8859 typval_T *rettv;
8860{
8861 if (argvars[0].v_type == VAR_FLOAT)
8862 {
8863 rettv->v_type = VAR_FLOAT;
8864 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8865 }
8866 else
8867 {
8868 varnumber_T n;
8869 int error = FALSE;
8870
8871 n = get_tv_number_chk(&argvars[0], &error);
8872 if (error)
8873 rettv->vval.v_number = -1;
8874 else if (n > 0)
8875 rettv->vval.v_number = n;
8876 else
8877 rettv->vval.v_number = -n;
8878 }
8879}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008880
8881/*
8882 * "acos()" function
8883 */
8884 static void
8885f_acos(argvars, rettv)
8886 typval_T *argvars;
8887 typval_T *rettv;
8888{
8889 float_T f;
8890
8891 rettv->v_type = VAR_FLOAT;
8892 if (get_float_arg(argvars, &f) == OK)
8893 rettv->vval.v_float = acos(f);
8894 else
8895 rettv->vval.v_float = 0.0;
8896}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008897#endif
8898
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008900 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 */
8902 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008903f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008904 typval_T *argvars;
8905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
Bram Moolenaar33570922005-01-25 22:26:29 +00008907 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008909 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008910 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008912 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008913 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008914 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008916 }
8917 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008918 EMSG(_(e_listreq));
8919}
8920
8921/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008922 * "and(expr, expr)" function
8923 */
8924 static void
8925f_and(argvars, rettv)
8926 typval_T *argvars;
8927 typval_T *rettv;
8928{
8929 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8930 & get_tv_number_chk(&argvars[1], NULL);
8931}
8932
8933/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008934 * "append(lnum, string/list)" function
8935 */
8936 static void
8937f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008938 typval_T *argvars;
8939 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008940{
8941 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008942 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008943 list_T *l = NULL;
8944 listitem_T *li = NULL;
8945 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008946 long added = 0;
8947
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008948 /* When coming here from Insert mode, sync undo, so that this can be
8949 * undone separately from what was previously inserted. */
8950 if (u_sync_once == 2)
8951 {
8952 u_sync_once = 1; /* notify that u_sync() was called */
8953 u_sync(TRUE);
8954 }
8955
Bram Moolenaar0d660222005-01-07 21:51:51 +00008956 lnum = get_tv_lnum(argvars);
8957 if (lnum >= 0
8958 && lnum <= curbuf->b_ml.ml_line_count
8959 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008960 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008961 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008962 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008963 l = argvars[1].vval.v_list;
8964 if (l == NULL)
8965 return;
8966 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008967 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008968 for (;;)
8969 {
8970 if (l == NULL)
8971 tv = &argvars[1]; /* append a string */
8972 else if (li == NULL)
8973 break; /* end of list */
8974 else
8975 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008976 line = get_tv_string_chk(tv);
8977 if (line == NULL) /* type error */
8978 {
8979 rettv->vval.v_number = 1; /* Failed */
8980 break;
8981 }
8982 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008983 ++added;
8984 if (l == NULL)
8985 break;
8986 li = li->li_next;
8987 }
8988
8989 appended_lines_mark(lnum, added);
8990 if (curwin->w_cursor.lnum > lnum)
8991 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008993 else
8994 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995}
8996
8997/*
8998 * "argc()" function
8999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009002 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006}
9007
9008/*
9009 * "argidx()" function
9010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009013 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017}
9018
9019/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009020 * "arglistid()" function
9021 */
9022 static void
9023f_arglistid(argvars, rettv)
9024 typval_T *argvars UNUSED;
9025 typval_T *rettv;
9026{
9027 win_T *wp;
9028 tabpage_T *tp = NULL;
9029 long n;
9030
9031 rettv->vval.v_number = -1;
9032 if (argvars[0].v_type != VAR_UNKNOWN)
9033 {
9034 if (argvars[1].v_type != VAR_UNKNOWN)
9035 {
9036 n = get_tv_number(&argvars[1]);
9037 if (n >= 0)
9038 tp = find_tabpage(n);
9039 }
9040 else
9041 tp = curtab;
9042
9043 if (tp != NULL)
9044 {
9045 wp = find_win_by_nr(&argvars[0], tp);
9046 if (wp != NULL)
9047 rettv->vval.v_number = wp->w_alist->id;
9048 }
9049 }
9050 else
9051 rettv->vval.v_number = curwin->w_alist->id;
9052}
9053
9054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 * "argv(nr)" function
9056 */
9057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *argvars;
9060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
9062 int idx;
9063
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009064 if (argvars[0].v_type != VAR_UNKNOWN)
9065 {
9066 idx = get_tv_number_chk(&argvars[0], NULL);
9067 if (idx >= 0 && idx < ARGCOUNT)
9068 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9069 else
9070 rettv->vval.v_string = NULL;
9071 rettv->v_type = VAR_STRING;
9072 }
9073 else if (rettv_list_alloc(rettv) == OK)
9074 for (idx = 0; idx < ARGCOUNT; ++idx)
9075 list_append_string(rettv->vval.v_list,
9076 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077}
9078
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009079#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009080/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009081 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009082 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009083 static void
9084f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009085 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009086 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009087{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009088 float_T f;
9089
9090 rettv->v_type = VAR_FLOAT;
9091 if (get_float_arg(argvars, &f) == OK)
9092 rettv->vval.v_float = asin(f);
9093 else
9094 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009095}
9096
9097/*
9098 * "atan()" function
9099 */
9100 static void
9101f_atan(argvars, rettv)
9102 typval_T *argvars;
9103 typval_T *rettv;
9104{
9105 float_T f;
9106
9107 rettv->v_type = VAR_FLOAT;
9108 if (get_float_arg(argvars, &f) == OK)
9109 rettv->vval.v_float = atan(f);
9110 else
9111 rettv->vval.v_float = 0.0;
9112}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009113
9114/*
9115 * "atan2()" function
9116 */
9117 static void
9118f_atan2(argvars, rettv)
9119 typval_T *argvars;
9120 typval_T *rettv;
9121{
9122 float_T fx, fy;
9123
9124 rettv->v_type = VAR_FLOAT;
9125 if (get_float_arg(argvars, &fx) == OK
9126 && get_float_arg(&argvars[1], &fy) == OK)
9127 rettv->vval.v_float = atan2(fx, fy);
9128 else
9129 rettv->vval.v_float = 0.0;
9130}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009131#endif
9132
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133/*
9134 * "browse(save, title, initdir, default)" function
9135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009138 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140{
9141#ifdef FEAT_BROWSE
9142 int save;
9143 char_u *title;
9144 char_u *initdir;
9145 char_u *defname;
9146 char_u buf[NUMBUFLEN];
9147 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009148 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 save = get_tv_number_chk(&argvars[0], &error);
9151 title = get_tv_string_chk(&argvars[1]);
9152 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9153 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009155 if (error || title == NULL || initdir == NULL || defname == NULL)
9156 rettv->vval.v_string = NULL;
9157 else
9158 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009159 do_browse(save ? BROWSE_SAVE : 0,
9160 title, defname, NULL, initdir, NULL, curbuf);
9161#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009163#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009165}
9166
9167/*
9168 * "browsedir(title, initdir)" function
9169 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009172 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009173 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009174{
9175#ifdef FEAT_BROWSE
9176 char_u *title;
9177 char_u *initdir;
9178 char_u buf[NUMBUFLEN];
9179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009180 title = get_tv_string_chk(&argvars[0]);
9181 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 if (title == NULL || initdir == NULL)
9184 rettv->vval.v_string = NULL;
9185 else
9186 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009187 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192}
9193
Bram Moolenaar33570922005-01-25 22:26:29 +00009194static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009195
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196/*
9197 * Find a buffer by number or exact name.
9198 */
9199 static buf_T *
9200find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009201 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
9203 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009205 if (avar->v_type == VAR_NUMBER)
9206 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009207 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009209 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009210 if (buf == NULL)
9211 {
9212 /* No full path name match, try a match with a URL or a "nofile"
9213 * buffer, these don't use the full path. */
9214 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9215 if (buf->b_fname != NULL
9216 && (path_with_url(buf->b_fname)
9217#ifdef FEAT_QUICKFIX
9218 || bt_nofile(buf)
9219#endif
9220 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009221 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009222 break;
9223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 }
9225 return buf;
9226}
9227
9228/*
9229 * "bufexists(expr)" function
9230 */
9231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009232f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009233 typval_T *argvars;
9234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009236 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237}
9238
9239/*
9240 * "buflisted(expr)" function
9241 */
9242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009243f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009244 typval_T *argvars;
9245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246{
9247 buf_T *buf;
9248
9249 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009250 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251}
9252
9253/*
9254 * "bufloaded(expr)" function
9255 */
9256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009257f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009258 typval_T *argvars;
9259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
9261 buf_T *buf;
9262
9263 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009264 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265}
9266
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009267static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009268
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269/*
9270 * Get buffer by number or pattern.
9271 */
9272 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009273get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009274 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009275 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 int save_magic;
9279 char_u *save_cpo;
9280 buf_T *buf;
9281
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009282 if (tv->v_type == VAR_NUMBER)
9283 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009284 if (tv->v_type != VAR_STRING)
9285 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 if (name == NULL || *name == NUL)
9287 return curbuf;
9288 if (name[0] == '$' && name[1] == NUL)
9289 return lastbuf;
9290
9291 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9292 save_magic = p_magic;
9293 p_magic = TRUE;
9294 save_cpo = p_cpo;
9295 p_cpo = (char_u *)"";
9296
9297 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009298 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299
9300 p_magic = save_magic;
9301 p_cpo = save_cpo;
9302
9303 /* If not found, try expanding the name, like done for bufexists(). */
9304 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306
9307 return buf;
9308}
9309
9310/*
9311 * "bufname(expr)" function
9312 */
9313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009314f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009315 typval_T *argvars;
9316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317{
9318 buf_T *buf;
9319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009320 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009322 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 --emsg_off;
9329}
9330
9331/*
9332 * "bufnr(expr)" function
9333 */
9334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009336 typval_T *argvars;
9337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338{
9339 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009340 int error = FALSE;
9341 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009343 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009345 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009346 --emsg_off;
9347
9348 /* If the buffer isn't found and the second argument is not zero create a
9349 * new buffer. */
9350 if (buf == NULL
9351 && argvars[1].v_type != VAR_UNKNOWN
9352 && get_tv_number_chk(&argvars[1], &error) != 0
9353 && !error
9354 && (name = get_tv_string_chk(&argvars[0])) != NULL
9355 && !error)
9356 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9357
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009359 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362}
9363
9364/*
9365 * "bufwinnr(nr)" function
9366 */
9367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 typval_T *argvars;
9370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371{
9372#ifdef FEAT_WINDOWS
9373 win_T *wp;
9374 int winnr = 0;
9375#endif
9376 buf_T *buf;
9377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009378 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009380 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381#ifdef FEAT_WINDOWS
9382 for (wp = firstwin; wp; wp = wp->w_next)
9383 {
9384 ++winnr;
9385 if (wp->w_buffer == buf)
9386 break;
9387 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009388 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009390 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391#endif
9392 --emsg_off;
9393}
9394
9395/*
9396 * "byte2line(byte)" function
9397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009400 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402{
9403#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405#else
9406 long boff = 0;
9407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 (linenr_T)0, &boff);
9414#endif
9415}
9416
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009417 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009418byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 typval_T *argvars;
9420 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009421 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009422{
9423#ifdef FEAT_MBYTE
9424 char_u *t;
9425#endif
9426 char_u *str;
9427 long idx;
9428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009429 str = get_tv_string_chk(&argvars[0]);
9430 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009432 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009433 return;
9434
9435#ifdef FEAT_MBYTE
9436 t = str;
9437 for ( ; idx > 0; idx--)
9438 {
9439 if (*t == NUL) /* EOL reached */
9440 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009441 if (enc_utf8 && comp)
9442 t += utf_ptr2len(t);
9443 else
9444 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009445 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009446 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009447#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009448 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009450#endif
9451}
9452
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009453/*
9454 * "byteidx()" function
9455 */
9456 static void
9457f_byteidx(argvars, rettv)
9458 typval_T *argvars;
9459 typval_T *rettv;
9460{
9461 byteidx(argvars, rettv, FALSE);
9462}
9463
9464/*
9465 * "byteidxcomp()" function
9466 */
9467 static void
9468f_byteidxcomp(argvars, rettv)
9469 typval_T *argvars;
9470 typval_T *rettv;
9471{
9472 byteidx(argvars, rettv, TRUE);
9473}
9474
Bram Moolenaardb913952012-06-29 12:54:53 +02009475 int
9476func_call(name, args, selfdict, rettv)
9477 char_u *name;
9478 typval_T *args;
9479 dict_T *selfdict;
9480 typval_T *rettv;
9481{
9482 listitem_T *item;
9483 typval_T argv[MAX_FUNC_ARGS + 1];
9484 int argc = 0;
9485 int dummy;
9486 int r = 0;
9487
9488 for (item = args->vval.v_list->lv_first; item != NULL;
9489 item = item->li_next)
9490 {
9491 if (argc == MAX_FUNC_ARGS)
9492 {
9493 EMSG(_("E699: Too many arguments"));
9494 break;
9495 }
9496 /* Make a copy of each argument. This is needed to be able to set
9497 * v_lock to VAR_FIXED in the copy without changing the original list.
9498 */
9499 copy_tv(&item->li_tv, &argv[argc++]);
9500 }
9501
9502 if (item == NULL)
9503 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9504 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9505 &dummy, TRUE, selfdict);
9506
9507 /* Free the arguments. */
9508 while (argc > 0)
9509 clear_tv(&argv[--argc]);
9510
9511 return r;
9512}
9513
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009514/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009515 * "call(func, arglist)" function
9516 */
9517 static void
9518f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009519 typval_T *argvars;
9520 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009521{
9522 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009523 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009524
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009525 if (argvars[1].v_type != VAR_LIST)
9526 {
9527 EMSG(_(e_listreq));
9528 return;
9529 }
9530 if (argvars[1].vval.v_list == NULL)
9531 return;
9532
9533 if (argvars[0].v_type == VAR_FUNC)
9534 func = argvars[0].vval.v_string;
9535 else
9536 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009537 if (*func == NUL)
9538 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009539
Bram Moolenaare9a41262005-01-15 22:18:47 +00009540 if (argvars[2].v_type != VAR_UNKNOWN)
9541 {
9542 if (argvars[2].v_type != VAR_DICT)
9543 {
9544 EMSG(_(e_dictreq));
9545 return;
9546 }
9547 selfdict = argvars[2].vval.v_dict;
9548 }
9549
Bram Moolenaardb913952012-06-29 12:54:53 +02009550 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009551}
9552
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009553#ifdef FEAT_FLOAT
9554/*
9555 * "ceil({float})" function
9556 */
9557 static void
9558f_ceil(argvars, rettv)
9559 typval_T *argvars;
9560 typval_T *rettv;
9561{
9562 float_T f;
9563
9564 rettv->v_type = VAR_FLOAT;
9565 if (get_float_arg(argvars, &f) == OK)
9566 rettv->vval.v_float = ceil(f);
9567 else
9568 rettv->vval.v_float = 0.0;
9569}
9570#endif
9571
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009572/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009573 * "changenr()" function
9574 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009575 static void
9576f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009577 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009578 typval_T *rettv;
9579{
9580 rettv->vval.v_number = curbuf->b_u_seq_cur;
9581}
9582
9583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 * "char2nr(string)" function
9585 */
9586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009588 typval_T *argvars;
9589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590{
9591#ifdef FEAT_MBYTE
9592 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009593 {
9594 int utf8 = 0;
9595
9596 if (argvars[1].v_type != VAR_UNKNOWN)
9597 utf8 = get_tv_number_chk(&argvars[1], NULL);
9598
9599 if (utf8)
9600 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9601 else
9602 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 else
9605#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607}
9608
9609/*
9610 * "cindent(lnum)" function
9611 */
9612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009613f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009614 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616{
9617#ifdef FEAT_CINDENT
9618 pos_T pos;
9619 linenr_T lnum;
9620
9621 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9624 {
9625 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 curwin->w_cursor = pos;
9628 }
9629 else
9630#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632}
9633
9634/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009635 * "clearmatches()" function
9636 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009637 static void
9638f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009639 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009640 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009641{
9642#ifdef FEAT_SEARCH_EXTRA
9643 clear_matches(curwin);
9644#endif
9645}
9646
9647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 * "col(string)" function
9649 */
9650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009652 typval_T *argvars;
9653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
9655 colnr_T col = 0;
9656 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009657 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009659 fp = var2fpos(&argvars[0], FALSE, &fnum);
9660 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 {
9662 if (fp->col == MAXCOL)
9663 {
9664 /* '> can be MAXCOL, get the length of the line then */
9665 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009666 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 else
9668 col = MAXCOL;
9669 }
9670 else
9671 {
9672 col = fp->col + 1;
9673#ifdef FEAT_VIRTUALEDIT
9674 /* col(".") when the cursor is on the NUL at the end of the line
9675 * because of "coladd" can be seen as an extra column. */
9676 if (virtual_active() && fp == &curwin->w_cursor)
9677 {
9678 char_u *p = ml_get_cursor();
9679
9680 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9681 curwin->w_virtcol - curwin->w_cursor.coladd))
9682 {
9683# ifdef FEAT_MBYTE
9684 int l;
9685
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009686 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 col += l;
9688# else
9689 if (*p != NUL && p[1] == NUL)
9690 ++col;
9691# endif
9692 }
9693 }
9694#endif
9695 }
9696 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698}
9699
Bram Moolenaar572cb562005-08-05 21:35:02 +00009700#if defined(FEAT_INS_EXPAND)
9701/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009702 * "complete()" function
9703 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009704 static void
9705f_complete(argvars, rettv)
9706 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009707 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009708{
9709 int startcol;
9710
9711 if ((State & INSERT) == 0)
9712 {
9713 EMSG(_("E785: complete() can only be used in Insert mode"));
9714 return;
9715 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009716
9717 /* Check for undo allowed here, because if something was already inserted
9718 * the line was already saved for undo and this check isn't done. */
9719 if (!undo_allowed())
9720 return;
9721
Bram Moolenaarade00832006-03-10 21:46:58 +00009722 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9723 {
9724 EMSG(_(e_invarg));
9725 return;
9726 }
9727
9728 startcol = get_tv_number_chk(&argvars[0], NULL);
9729 if (startcol <= 0)
9730 return;
9731
9732 set_completion(startcol - 1, argvars[1].vval.v_list);
9733}
9734
9735/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009736 * "complete_add()" function
9737 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009738 static void
9739f_complete_add(argvars, rettv)
9740 typval_T *argvars;
9741 typval_T *rettv;
9742{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009743 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009744}
9745
9746/*
9747 * "complete_check()" function
9748 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009749 static void
9750f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009751 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009752 typval_T *rettv;
9753{
9754 int saved = RedrawingDisabled;
9755
9756 RedrawingDisabled = 0;
9757 ins_compl_check_keys(0);
9758 rettv->vval.v_number = compl_interrupted;
9759 RedrawingDisabled = saved;
9760}
9761#endif
9762
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763/*
9764 * "confirm(message, buttons[, default [, type]])" function
9765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009768 typval_T *argvars UNUSED;
9769 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9772 char_u *message;
9773 char_u *buttons = NULL;
9774 char_u buf[NUMBUFLEN];
9775 char_u buf2[NUMBUFLEN];
9776 int def = 1;
9777 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009778 char_u *typestr;
9779 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 message = get_tv_string_chk(&argvars[0]);
9782 if (message == NULL)
9783 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009784 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009786 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9787 if (buttons == NULL)
9788 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009789 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009791 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009792 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009794 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9795 if (typestr == NULL)
9796 error = TRUE;
9797 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009799 switch (TOUPPER_ASC(*typestr))
9800 {
9801 case 'E': type = VIM_ERROR; break;
9802 case 'Q': type = VIM_QUESTION; break;
9803 case 'I': type = VIM_INFO; break;
9804 case 'W': type = VIM_WARNING; break;
9805 case 'G': type = VIM_GENERIC; break;
9806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 }
9808 }
9809 }
9810 }
9811
9812 if (buttons == NULL || *buttons == NUL)
9813 buttons = (char_u *)_("&Ok");
9814
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009815 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009816 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009817 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818#endif
9819}
9820
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009821/*
9822 * "copy()" function
9823 */
9824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009826 typval_T *argvars;
9827 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009828{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009829 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009830}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009832#ifdef FEAT_FLOAT
9833/*
9834 * "cos()" function
9835 */
9836 static void
9837f_cos(argvars, rettv)
9838 typval_T *argvars;
9839 typval_T *rettv;
9840{
9841 float_T f;
9842
9843 rettv->v_type = VAR_FLOAT;
9844 if (get_float_arg(argvars, &f) == OK)
9845 rettv->vval.v_float = cos(f);
9846 else
9847 rettv->vval.v_float = 0.0;
9848}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009849
9850/*
9851 * "cosh()" function
9852 */
9853 static void
9854f_cosh(argvars, rettv)
9855 typval_T *argvars;
9856 typval_T *rettv;
9857{
9858 float_T f;
9859
9860 rettv->v_type = VAR_FLOAT;
9861 if (get_float_arg(argvars, &f) == OK)
9862 rettv->vval.v_float = cosh(f);
9863 else
9864 rettv->vval.v_float = 0.0;
9865}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009866#endif
9867
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009869 * "count()" function
9870 */
9871 static void
9872f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009873 typval_T *argvars;
9874 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009875{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009876 long n = 0;
9877 int ic = FALSE;
9878
Bram Moolenaare9a41262005-01-15 22:18:47 +00009879 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009880 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009881 listitem_T *li;
9882 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009883 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009884
Bram Moolenaare9a41262005-01-15 22:18:47 +00009885 if ((l = argvars[0].vval.v_list) != NULL)
9886 {
9887 li = l->lv_first;
9888 if (argvars[2].v_type != VAR_UNKNOWN)
9889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009890 int error = FALSE;
9891
9892 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009893 if (argvars[3].v_type != VAR_UNKNOWN)
9894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009895 idx = get_tv_number_chk(&argvars[3], &error);
9896 if (!error)
9897 {
9898 li = list_find(l, idx);
9899 if (li == NULL)
9900 EMSGN(_(e_listidx), idx);
9901 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009902 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009903 if (error)
9904 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 }
9906
9907 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009908 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009909 ++n;
9910 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009911 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 else if (argvars[0].v_type == VAR_DICT)
9913 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009914 int todo;
9915 dict_T *d;
9916 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009917
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009918 if ((d = argvars[0].vval.v_dict) != NULL)
9919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009920 int error = FALSE;
9921
Bram Moolenaare9a41262005-01-15 22:18:47 +00009922 if (argvars[2].v_type != VAR_UNKNOWN)
9923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009925 if (argvars[3].v_type != VAR_UNKNOWN)
9926 EMSG(_(e_invarg));
9927 }
9928
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009929 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009930 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009931 {
9932 if (!HASHITEM_EMPTY(hi))
9933 {
9934 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009935 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009936 ++n;
9937 }
9938 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009939 }
9940 }
9941 else
9942 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009943 rettv->vval.v_number = n;
9944}
9945
9946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9948 *
9949 * Checks the existence of a cscope connection.
9950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009952f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009953 typval_T *argvars UNUSED;
9954 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955{
9956#ifdef FEAT_CSCOPE
9957 int num = 0;
9958 char_u *dbpath = NULL;
9959 char_u *prepend = NULL;
9960 char_u buf[NUMBUFLEN];
9961
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009962 if (argvars[0].v_type != VAR_UNKNOWN
9963 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965 num = (int)get_tv_number(&argvars[0]);
9966 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009967 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 }
9970
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972#endif
9973}
9974
9975/*
9976 * "cursor(lnum, col)" function
9977 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009978 * Moves the cursor to the specified line and column.
9979 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009983 typval_T *argvars;
9984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985{
9986 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009987#ifdef FEAT_VIRTUALEDIT
9988 long coladd = 0;
9989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009991 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009992 if (argvars[1].v_type == VAR_UNKNOWN)
9993 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009994 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +02009995 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009996
Bram Moolenaar493c1782014-05-28 14:34:46 +02009997 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009998 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009999 line = pos.lnum;
10000 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010001#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010002 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010003#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010004 if (curswant >= 0)
10005 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010006 }
10007 else
10008 {
10009 line = get_tv_lnum(argvars);
10010 col = get_tv_number_chk(&argvars[1], NULL);
10011#ifdef FEAT_VIRTUALEDIT
10012 if (argvars[2].v_type != VAR_UNKNOWN)
10013 coladd = get_tv_number_chk(&argvars[2], NULL);
10014#endif
10015 }
10016 if (line < 0 || col < 0
10017#ifdef FEAT_VIRTUALEDIT
10018 || coladd < 0
10019#endif
10020 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010021 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 if (line > 0)
10023 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 if (col > 0)
10025 curwin->w_cursor.col = col - 1;
10026#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010027 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028#endif
10029
10030 /* Make sure the cursor is in a valid position. */
10031 check_cursor();
10032#ifdef FEAT_MBYTE
10033 /* Correct cursor for multi-byte character. */
10034 if (has_mbyte)
10035 mb_adjust_cursor();
10036#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010037
10038 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010039 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040}
10041
10042/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010043 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 */
10045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010047 typval_T *argvars;
10048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010050 int noref = 0;
10051
10052 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010053 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010054 if (noref < 0 || noref > 1)
10055 EMSG(_(e_invarg));
10056 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010057 {
10058 current_copyID += COPYID_INC;
10059 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061}
10062
10063/*
10064 * "delete()" function
10065 */
10066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010067f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010068 typval_T *argvars;
10069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070{
10071 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010074 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075}
10076
10077/*
10078 * "did_filetype()" function
10079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010082 typval_T *argvars UNUSED;
10083 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084{
10085#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010086 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087#endif
10088}
10089
10090/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010091 * "diff_filler()" function
10092 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010095 typval_T *argvars UNUSED;
10096 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010097{
10098#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010100#endif
10101}
10102
10103/*
10104 * "diff_hlID()" function
10105 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010108 typval_T *argvars UNUSED;
10109 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010110{
10111#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010113 static linenr_T prev_lnum = 0;
10114 static int changedtick = 0;
10115 static int fnum = 0;
10116 static int change_start = 0;
10117 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010118 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010119 int filler_lines;
10120 int col;
10121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010122 if (lnum < 0) /* ignore type error in {lnum} arg */
10123 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010124 if (lnum != prev_lnum
10125 || changedtick != curbuf->b_changedtick
10126 || fnum != curbuf->b_fnum)
10127 {
10128 /* New line, buffer, change: need to get the values. */
10129 filler_lines = diff_check(curwin, lnum);
10130 if (filler_lines < 0)
10131 {
10132 if (filler_lines == -1)
10133 {
10134 change_start = MAXCOL;
10135 change_end = -1;
10136 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10137 hlID = HLF_ADD; /* added line */
10138 else
10139 hlID = HLF_CHD; /* changed line */
10140 }
10141 else
10142 hlID = HLF_ADD; /* added line */
10143 }
10144 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010145 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010146 prev_lnum = lnum;
10147 changedtick = curbuf->b_changedtick;
10148 fnum = curbuf->b_fnum;
10149 }
10150
10151 if (hlID == HLF_CHD || hlID == HLF_TXD)
10152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010154 if (col >= change_start && col <= change_end)
10155 hlID = HLF_TXD; /* changed text */
10156 else
10157 hlID = HLF_CHD; /* changed line */
10158 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010159 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010160#endif
10161}
10162
10163/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010164 * "empty({expr})" function
10165 */
10166 static void
10167f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010168 typval_T *argvars;
10169 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010170{
10171 int n;
10172
10173 switch (argvars[0].v_type)
10174 {
10175 case VAR_STRING:
10176 case VAR_FUNC:
10177 n = argvars[0].vval.v_string == NULL
10178 || *argvars[0].vval.v_string == NUL;
10179 break;
10180 case VAR_NUMBER:
10181 n = argvars[0].vval.v_number == 0;
10182 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010183#ifdef FEAT_FLOAT
10184 case VAR_FLOAT:
10185 n = argvars[0].vval.v_float == 0.0;
10186 break;
10187#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010188 case VAR_LIST:
10189 n = argvars[0].vval.v_list == NULL
10190 || argvars[0].vval.v_list->lv_first == NULL;
10191 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010192 case VAR_DICT:
10193 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010194 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010195 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010196 default:
10197 EMSG2(_(e_intern2), "f_empty()");
10198 n = 0;
10199 }
10200
10201 rettv->vval.v_number = n;
10202}
10203
10204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 * "escape({string}, {chars})" function
10206 */
10207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010208f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010209 typval_T *argvars;
10210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211{
10212 char_u buf[NUMBUFLEN];
10213
Bram Moolenaar758711c2005-02-02 23:11:38 +000010214 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10215 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217}
10218
10219/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010220 * "eval()" function
10221 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010222 static void
10223f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 typval_T *argvars;
10225 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010226{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010227 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 s = get_tv_string_chk(&argvars[0]);
10230 if (s != NULL)
10231 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010232
Bram Moolenaar615b9972015-01-14 17:15:05 +010010233 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010234 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10235 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010236 if (p != NULL && !aborting())
10237 EMSG2(_(e_invexpr2), p);
10238 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010239 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010240 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010242 else if (*s != NUL)
10243 EMSG(_(e_trailing));
10244}
10245
10246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 * "eventhandler()" function
10248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010250f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010251 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010254 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255}
10256
10257/*
10258 * "executable()" function
10259 */
10260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010261f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010262 typval_T *argvars;
10263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010265 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10266}
10267
10268/*
10269 * "exepath()" function
10270 */
10271 static void
10272f_exepath(argvars, rettv)
10273 typval_T *argvars;
10274 typval_T *rettv;
10275{
10276 char_u *p = NULL;
10277
10278 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10279 rettv->v_type = VAR_STRING;
10280 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281}
10282
10283/*
10284 * "exists()" function
10285 */
10286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010287f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010288 typval_T *argvars;
10289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290{
10291 char_u *p;
10292 char_u *name;
10293 int n = FALSE;
10294 int len = 0;
10295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010296 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 if (*p == '$') /* environment variable */
10298 {
10299 /* first try "normal" environment variables (fast) */
10300 if (mch_getenv(p + 1) != NULL)
10301 n = TRUE;
10302 else
10303 {
10304 /* try expanding things like $VIM and ${HOME} */
10305 p = expand_env_save(p);
10306 if (p != NULL && *p != '$')
10307 n = TRUE;
10308 vim_free(p);
10309 }
10310 }
10311 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010314 if (*skipwhite(p) != NUL)
10315 n = FALSE; /* trailing garbage */
10316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 else if (*p == '*') /* internal or user defined function */
10318 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010319 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 }
10321 else if (*p == ':')
10322 {
10323 n = cmd_exists(p + 1);
10324 }
10325 else if (*p == '#')
10326 {
10327#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010328 if (p[1] == '#')
10329 n = autocmd_supported(p + 2);
10330 else
10331 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332#endif
10333 }
10334 else /* internal variable */
10335 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010336 char_u *tofree;
10337 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010339 /* get_name_len() takes care of expanding curly braces */
10340 name = p;
10341 len = get_name_len(&p, &tofree, TRUE, FALSE);
10342 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010344 if (tofree != NULL)
10345 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010346 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010347 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010349 /* handle d.key, l[idx], f(expr) */
10350 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10351 if (n)
10352 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 }
10354 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010355 if (*p != NUL)
10356 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010358 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 }
10360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010361 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362}
10363
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010364#ifdef FEAT_FLOAT
10365/*
10366 * "exp()" function
10367 */
10368 static void
10369f_exp(argvars, rettv)
10370 typval_T *argvars;
10371 typval_T *rettv;
10372{
10373 float_T f;
10374
10375 rettv->v_type = VAR_FLOAT;
10376 if (get_float_arg(argvars, &f) == OK)
10377 rettv->vval.v_float = exp(f);
10378 else
10379 rettv->vval.v_float = 0.0;
10380}
10381#endif
10382
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383/*
10384 * "expand()" function
10385 */
10386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010387f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010388 typval_T *argvars;
10389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390{
10391 char_u *s;
10392 int len;
10393 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010394 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010396 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010397 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010399 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010400 if (argvars[1].v_type != VAR_UNKNOWN
10401 && argvars[2].v_type != VAR_UNKNOWN
10402 && get_tv_number_chk(&argvars[2], &error)
10403 && !error)
10404 {
10405 rettv->v_type = VAR_LIST;
10406 rettv->vval.v_list = NULL;
10407 }
10408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010409 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 if (*s == '%' || *s == '#' || *s == '<')
10411 {
10412 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010413 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010415 if (rettv->v_type == VAR_LIST)
10416 {
10417 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10418 list_append_string(rettv->vval.v_list, result, -1);
10419 else
10420 vim_free(result);
10421 }
10422 else
10423 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 }
10425 else
10426 {
10427 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010428 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010429 if (argvars[1].v_type != VAR_UNKNOWN
10430 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010431 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 if (!error)
10433 {
10434 ExpandInit(&xpc);
10435 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010436 if (p_wic)
10437 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010438 if (rettv->v_type == VAR_STRING)
10439 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10440 options, WILD_ALL);
10441 else if (rettv_list_alloc(rettv) != FAIL)
10442 {
10443 int i;
10444
10445 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10446 for (i = 0; i < xpc.xp_numfiles; i++)
10447 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10448 ExpandCleanup(&xpc);
10449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 }
10451 else
10452 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 }
10454}
10455
10456/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010457 * Go over all entries in "d2" and add them to "d1".
10458 * When "action" is "error" then a duplicate key is an error.
10459 * When "action" is "force" then a duplicate key is overwritten.
10460 * Otherwise duplicate keys are ignored ("action" is "keep").
10461 */
10462 void
10463dict_extend(d1, d2, action)
10464 dict_T *d1;
10465 dict_T *d2;
10466 char_u *action;
10467{
10468 dictitem_T *di1;
10469 hashitem_T *hi2;
10470 int todo;
10471
10472 todo = (int)d2->dv_hashtab.ht_used;
10473 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10474 {
10475 if (!HASHITEM_EMPTY(hi2))
10476 {
10477 --todo;
10478 di1 = dict_find(d1, hi2->hi_key, -1);
10479 if (d1->dv_scope != 0)
10480 {
10481 /* Disallow replacing a builtin function in l: and g:.
10482 * Check the key to be valid when adding to any
10483 * scope. */
10484 if (d1->dv_scope == VAR_DEF_SCOPE
10485 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10486 && var_check_func_name(hi2->hi_key,
10487 di1 == NULL))
10488 break;
10489 if (!valid_varname(hi2->hi_key))
10490 break;
10491 }
10492 if (di1 == NULL)
10493 {
10494 di1 = dictitem_copy(HI2DI(hi2));
10495 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10496 dictitem_free(di1);
10497 }
10498 else if (*action == 'e')
10499 {
10500 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10501 break;
10502 }
10503 else if (*action == 'f' && HI2DI(hi2) != di1)
10504 {
10505 clear_tv(&di1->di_tv);
10506 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10507 }
10508 }
10509 }
10510}
10511
10512/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010513 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010514 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010515 */
10516 static void
10517f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010518 typval_T *argvars;
10519 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010520{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010521 char *arg_errmsg = N_("extend() argument");
10522
Bram Moolenaare9a41262005-01-15 22:18:47 +000010523 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010524 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010525 list_T *l1, *l2;
10526 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010527 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010528 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010529
Bram Moolenaare9a41262005-01-15 22:18:47 +000010530 l1 = argvars[0].vval.v_list;
10531 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010532 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010533 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010534 {
10535 if (argvars[2].v_type != VAR_UNKNOWN)
10536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010537 before = get_tv_number_chk(&argvars[2], &error);
10538 if (error)
10539 return; /* type error; errmsg already given */
10540
Bram Moolenaar758711c2005-02-02 23:11:38 +000010541 if (before == l1->lv_len)
10542 item = NULL;
10543 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010544 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010545 item = list_find(l1, before);
10546 if (item == NULL)
10547 {
10548 EMSGN(_(e_listidx), before);
10549 return;
10550 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010551 }
10552 }
10553 else
10554 item = NULL;
10555 list_extend(l1, l2, item);
10556
Bram Moolenaare9a41262005-01-15 22:18:47 +000010557 copy_tv(&argvars[0], rettv);
10558 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010559 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010560 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10561 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010562 dict_T *d1, *d2;
10563 char_u *action;
10564 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010565
10566 d1 = argvars[0].vval.v_dict;
10567 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010568 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010569 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 {
10571 /* Check the third argument. */
10572 if (argvars[2].v_type != VAR_UNKNOWN)
10573 {
10574 static char *(av[]) = {"keep", "force", "error"};
10575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 action = get_tv_string_chk(&argvars[2]);
10577 if (action == NULL)
10578 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010579 for (i = 0; i < 3; ++i)
10580 if (STRCMP(action, av[i]) == 0)
10581 break;
10582 if (i == 3)
10583 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010584 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010585 return;
10586 }
10587 }
10588 else
10589 action = (char_u *)"force";
10590
Bram Moolenaara9922d62013-05-30 13:01:18 +020010591 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 copy_tv(&argvars[0], rettv);
10594 }
10595 }
10596 else
10597 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010598}
10599
10600/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010601 * "feedkeys()" function
10602 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010603 static void
10604f_feedkeys(argvars, rettv)
10605 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010606 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010607{
10608 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010609 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010610 char_u *keys, *flags;
10611 char_u nbuf[NUMBUFLEN];
10612 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010613 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010614
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010615 /* This is not allowed in the sandbox. If the commands would still be
10616 * executed in the sandbox it would be OK, but it probably happens later,
10617 * when "sandbox" is no longer set. */
10618 if (check_secure())
10619 return;
10620
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010621 keys = get_tv_string(&argvars[0]);
10622 if (*keys != NUL)
10623 {
10624 if (argvars[1].v_type != VAR_UNKNOWN)
10625 {
10626 flags = get_tv_string_buf(&argvars[1], nbuf);
10627 for ( ; *flags != NUL; ++flags)
10628 {
10629 switch (*flags)
10630 {
10631 case 'n': remap = FALSE; break;
10632 case 'm': remap = TRUE; break;
10633 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010634 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010635 }
10636 }
10637 }
10638
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010639 /* Need to escape K_SPECIAL and CSI before putting the string in the
10640 * typeahead buffer. */
10641 keys_esc = vim_strsave_escape_csi(keys);
10642 if (keys_esc != NULL)
10643 {
10644 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010645 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010646 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010647 if (vgetc_busy)
10648 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010649 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010650 }
10651}
10652
10653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 * "filereadable()" function
10655 */
10656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010657f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010658 typval_T *argvars;
10659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010661 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 char_u *p;
10663 int n;
10664
Bram Moolenaarc236c162008-07-13 17:41:49 +000010665#ifndef O_NONBLOCK
10666# define O_NONBLOCK 0
10667#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010669 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10670 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 {
10672 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010673 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 }
10675 else
10676 n = FALSE;
10677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010678 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679}
10680
10681/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010682 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 * rights to write into.
10684 */
10685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010687 typval_T *argvars;
10688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010690 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691}
10692
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010693static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010694
10695 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010696findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010697 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010698 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010699 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010700{
10701#ifdef FEAT_SEARCHPATH
10702 char_u *fname;
10703 char_u *fresult = NULL;
10704 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10705 char_u *p;
10706 char_u pathbuf[NUMBUFLEN];
10707 int count = 1;
10708 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010709 int error = FALSE;
10710#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010711
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010712 rettv->vval.v_string = NULL;
10713 rettv->v_type = VAR_STRING;
10714
10715#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010716 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010717
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010718 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010720 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10721 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010722 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010723 else
10724 {
10725 if (*p != NUL)
10726 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010728 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010729 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010730 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010731 }
10732
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010733 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10734 error = TRUE;
10735
10736 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010738 do
10739 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010740 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010741 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010742 fresult = find_file_in_path_option(first ? fname : NULL,
10743 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010744 0, first, path,
10745 find_what,
10746 curbuf->b_ffname,
10747 find_what == FINDFILE_DIR
10748 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010749 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010750
10751 if (fresult != NULL && rettv->v_type == VAR_LIST)
10752 list_append_string(rettv->vval.v_list, fresult, -1);
10753
10754 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010755 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010756
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010757 if (rettv->v_type == VAR_STRING)
10758 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010759#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010760}
10761
Bram Moolenaar33570922005-01-25 22:26:29 +000010762static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10763static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010764
10765/*
10766 * Implementation of map() and filter().
10767 */
10768 static void
10769filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010770 typval_T *argvars;
10771 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010772 int map;
10773{
10774 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010775 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010776 listitem_T *li, *nli;
10777 list_T *l = NULL;
10778 dictitem_T *di;
10779 hashtab_T *ht;
10780 hashitem_T *hi;
10781 dict_T *d = NULL;
10782 typval_T save_val;
10783 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010784 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010785 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010786 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10787 char *arg_errmsg = (map ? N_("map() argument")
10788 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010789 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010790 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010791
Bram Moolenaare9a41262005-01-15 22:18:47 +000010792 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010793 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010794 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010795 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010796 return;
10797 }
10798 else if (argvars[0].v_type == VAR_DICT)
10799 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010800 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010801 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010802 return;
10803 }
10804 else
10805 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010806 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010807 return;
10808 }
10809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010810 expr = get_tv_string_buf_chk(&argvars[1], buf);
10811 /* On type errors, the preceding call has already displayed an error
10812 * message. Avoid a misleading error message for an empty string that
10813 * was not passed as argument. */
10814 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010816 prepare_vimvar(VV_VAL, &save_val);
10817 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010818
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010819 /* We reset "did_emsg" to be able to detect whether an error
10820 * occurred during evaluation of the expression. */
10821 save_did_emsg = did_emsg;
10822 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010823
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010824 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010825 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010827 vimvars[VV_KEY].vv_type = VAR_STRING;
10828
10829 ht = &d->dv_hashtab;
10830 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010831 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010832 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010834 if (!HASHITEM_EMPTY(hi))
10835 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010836 int r;
10837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010838 --todo;
10839 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010840 if (tv_check_lock(di->di_tv.v_lock,
10841 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 break;
10843 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010844 r = filter_map_one(&di->di_tv, expr, map, &rem);
10845 clear_tv(&vimvars[VV_KEY].vv_tv);
10846 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010847 break;
10848 if (!map && rem)
10849 dictitem_remove(d, di);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010850 }
10851 }
10852 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 }
10854 else
10855 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010856 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010858 for (li = l->lv_first; li != NULL; li = nli)
10859 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010860 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010861 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010862 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010863 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010864 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010865 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010866 break;
10867 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010869 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010870 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010871 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010872
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010873 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010875
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010876 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010877 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010878
10879 copy_tv(&argvars[0], rettv);
10880}
10881
10882 static int
10883filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010884 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010885 char_u *expr;
10886 int map;
10887 int *remp;
10888{
Bram Moolenaar33570922005-01-25 22:26:29 +000010889 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010890 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010891 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010892
Bram Moolenaar33570922005-01-25 22:26:29 +000010893 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010894 s = expr;
10895 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010896 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010897 if (*s != NUL) /* check for trailing chars after expr */
10898 {
10899 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010900 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010901 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010902 }
10903 if (map)
10904 {
10905 /* map(): replace the list item value */
10906 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010907 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010908 *tv = rettv;
10909 }
10910 else
10911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010912 int error = FALSE;
10913
Bram Moolenaare9a41262005-01-15 22:18:47 +000010914 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010916 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 /* On type error, nothing has been removed; return FAIL to stop the
10918 * loop. The error message was given by get_tv_number_chk(). */
10919 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010920 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010921 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010922 retval = OK;
10923theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010924 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010925 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010926}
10927
10928/*
10929 * "filter()" function
10930 */
10931 static void
10932f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010933 typval_T *argvars;
10934 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010935{
10936 filter_map(argvars, rettv, FALSE);
10937}
10938
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010939/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010940 * "finddir({fname}[, {path}[, {count}]])" function
10941 */
10942 static void
10943f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010944 typval_T *argvars;
10945 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010946{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010947 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010948}
10949
10950/*
10951 * "findfile({fname}[, {path}[, {count}]])" function
10952 */
10953 static void
10954f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010955 typval_T *argvars;
10956 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010957{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010958 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010959}
10960
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010961#ifdef FEAT_FLOAT
10962/*
10963 * "float2nr({float})" function
10964 */
10965 static void
10966f_float2nr(argvars, rettv)
10967 typval_T *argvars;
10968 typval_T *rettv;
10969{
10970 float_T f;
10971
10972 if (get_float_arg(argvars, &f) == OK)
10973 {
10974 if (f < -0x7fffffff)
10975 rettv->vval.v_number = -0x7fffffff;
10976 else if (f > 0x7fffffff)
10977 rettv->vval.v_number = 0x7fffffff;
10978 else
10979 rettv->vval.v_number = (varnumber_T)f;
10980 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010981}
10982
10983/*
10984 * "floor({float})" function
10985 */
10986 static void
10987f_floor(argvars, rettv)
10988 typval_T *argvars;
10989 typval_T *rettv;
10990{
10991 float_T f;
10992
10993 rettv->v_type = VAR_FLOAT;
10994 if (get_float_arg(argvars, &f) == OK)
10995 rettv->vval.v_float = floor(f);
10996 else
10997 rettv->vval.v_float = 0.0;
10998}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010999
11000/*
11001 * "fmod()" function
11002 */
11003 static void
11004f_fmod(argvars, rettv)
11005 typval_T *argvars;
11006 typval_T *rettv;
11007{
11008 float_T fx, fy;
11009
11010 rettv->v_type = VAR_FLOAT;
11011 if (get_float_arg(argvars, &fx) == OK
11012 && get_float_arg(&argvars[1], &fy) == OK)
11013 rettv->vval.v_float = fmod(fx, fy);
11014 else
11015 rettv->vval.v_float = 0.0;
11016}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011017#endif
11018
Bram Moolenaar0d660222005-01-07 21:51:51 +000011019/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011020 * "fnameescape({string})" function
11021 */
11022 static void
11023f_fnameescape(argvars, rettv)
11024 typval_T *argvars;
11025 typval_T *rettv;
11026{
11027 rettv->vval.v_string = vim_strsave_fnameescape(
11028 get_tv_string(&argvars[0]), FALSE);
11029 rettv->v_type = VAR_STRING;
11030}
11031
11032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 * "fnamemodify({fname}, {mods})" function
11034 */
11035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011037 typval_T *argvars;
11038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039{
11040 char_u *fname;
11041 char_u *mods;
11042 int usedlen = 0;
11043 int len;
11044 char_u *fbuf = NULL;
11045 char_u buf[NUMBUFLEN];
11046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 fname = get_tv_string_chk(&argvars[0]);
11048 mods = get_tv_string_buf_chk(&argvars[1], buf);
11049 if (fname == NULL || mods == NULL)
11050 fname = NULL;
11051 else
11052 {
11053 len = (int)STRLEN(fname);
11054 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 vim_free(fbuf);
11063}
11064
Bram Moolenaar33570922005-01-25 22:26:29 +000011065static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066
11067/*
11068 * "foldclosed()" function
11069 */
11070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011072 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011073 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011074 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075{
11076#ifdef FEAT_FOLDING
11077 linenr_T lnum;
11078 linenr_T first, last;
11079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11082 {
11083 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11084 {
11085 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 return;
11090 }
11091 }
11092#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011093 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094}
11095
11096/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011097 * "foldclosed()" function
11098 */
11099 static void
11100f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011101 typval_T *argvars;
11102 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011103{
11104 foldclosed_both(argvars, rettv, FALSE);
11105}
11106
11107/*
11108 * "foldclosedend()" function
11109 */
11110 static void
11111f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011112 typval_T *argvars;
11113 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011114{
11115 foldclosed_both(argvars, rettv, TRUE);
11116}
11117
11118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 * "foldlevel()" function
11120 */
11121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011123 typval_T *argvars UNUSED;
11124 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125{
11126#ifdef FEAT_FOLDING
11127 linenr_T lnum;
11128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133}
11134
11135/*
11136 * "foldtext()" function
11137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011140 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142{
11143#ifdef FEAT_FOLDING
11144 linenr_T lnum;
11145 char_u *s;
11146 char_u *r;
11147 int len;
11148 char *txt;
11149#endif
11150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151 rettv->v_type = VAR_STRING;
11152 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011154 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11155 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11156 <= curbuf->b_ml.ml_line_count
11157 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 {
11159 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011160 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11161 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 {
11163 if (!linewhite(lnum))
11164 break;
11165 ++lnum;
11166 }
11167
11168 /* Find interesting text in this line. */
11169 s = skipwhite(ml_get(lnum));
11170 /* skip C comment-start */
11171 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011174 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011175 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011176 {
11177 s = skipwhite(ml_get(lnum + 1));
11178 if (*s == '*')
11179 s = skipwhite(s + 1);
11180 }
11181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 txt = _("+-%s%3ld lines: ");
11183 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011184 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 + 20 /* for %3ld */
11186 + STRLEN(s))); /* concatenated */
11187 if (r != NULL)
11188 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011189 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11190 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11191 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 len = (int)STRLEN(r);
11193 STRCAT(r, s);
11194 /* remove 'foldmarker' and 'commentstring' */
11195 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 }
11198 }
11199#endif
11200}
11201
11202/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011203 * "foldtextresult(lnum)" function
11204 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011207 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011208 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011209{
11210#ifdef FEAT_FOLDING
11211 linenr_T lnum;
11212 char_u *text;
11213 char_u buf[51];
11214 foldinfo_T foldinfo;
11215 int fold_count;
11216#endif
11217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 rettv->v_type = VAR_STRING;
11219 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011220#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 /* treat illegal types and illegal string values for {lnum} the same */
11223 if (lnum < 0)
11224 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011225 fold_count = foldedCount(curwin, lnum, &foldinfo);
11226 if (fold_count > 0)
11227 {
11228 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11229 &foldinfo, buf);
11230 if (text == buf)
11231 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011233 }
11234#endif
11235}
11236
11237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 * "foreground()" function
11239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011242 typval_T *argvars UNUSED;
11243 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245#ifdef FEAT_GUI
11246 if (gui.in_use)
11247 gui_mch_set_foreground();
11248#else
11249# ifdef WIN32
11250 win32_set_foreground();
11251# endif
11252#endif
11253}
11254
11255/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011256 * "function()" function
11257 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011262{
11263 char_u *s;
11264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011266 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011267 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011268 /* Don't check an autoload name for existence here. */
11269 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011270 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011271 else
11272 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011273 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011274 {
11275 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011276 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011277
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011278 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11279 * also be called from another script. Using trans_function_name()
11280 * would also work, but some plugins depend on the name being
11281 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011282 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011283 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011284 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011285 if (rettv->vval.v_string != NULL)
11286 {
11287 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011288 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011289 }
11290 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011291 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011292 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011293 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011294 }
11295}
11296
11297/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011298 * "garbagecollect()" function
11299 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011300 static void
11301f_garbagecollect(argvars, rettv)
11302 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011303 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011304{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011305 /* This is postponed until we are back at the toplevel, because we may be
11306 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11307 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011308
11309 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11310 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011311}
11312
11313/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011314 * "get()" function
11315 */
11316 static void
11317f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011318 typval_T *argvars;
11319 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011320{
Bram Moolenaar33570922005-01-25 22:26:29 +000011321 listitem_T *li;
11322 list_T *l;
11323 dictitem_T *di;
11324 dict_T *d;
11325 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011326
Bram Moolenaare9a41262005-01-15 22:18:47 +000011327 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011328 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011329 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 int error = FALSE;
11332
11333 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11334 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011335 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011336 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011337 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011338 else if (argvars[0].v_type == VAR_DICT)
11339 {
11340 if ((d = argvars[0].vval.v_dict) != NULL)
11341 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011342 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011343 if (di != NULL)
11344 tv = &di->di_tv;
11345 }
11346 }
11347 else
11348 EMSG2(_(e_listdictarg), "get()");
11349
11350 if (tv == NULL)
11351 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011352 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011353 copy_tv(&argvars[2], rettv);
11354 }
11355 else
11356 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011357}
11358
Bram Moolenaar342337a2005-07-21 21:11:17 +000011359static 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 +000011360
11361/*
11362 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011363 * Return a range (from start to end) of lines in rettv from the specified
11364 * buffer.
11365 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011366 */
11367 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011368get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011369 buf_T *buf;
11370 linenr_T start;
11371 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011372 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011373 typval_T *rettv;
11374{
11375 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011376
Bram Moolenaar959a1432013-12-14 12:17:38 +010011377 rettv->v_type = VAR_STRING;
11378 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011379 if (retlist && rettv_list_alloc(rettv) == FAIL)
11380 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011381
11382 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11383 return;
11384
11385 if (!retlist)
11386 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011387 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11388 p = ml_get_buf(buf, start, FALSE);
11389 else
11390 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011391 rettv->vval.v_string = vim_strsave(p);
11392 }
11393 else
11394 {
11395 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011396 return;
11397
11398 if (start < 1)
11399 start = 1;
11400 if (end > buf->b_ml.ml_line_count)
11401 end = buf->b_ml.ml_line_count;
11402 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011403 if (list_append_string(rettv->vval.v_list,
11404 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011405 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011406 }
11407}
11408
11409/*
11410 * "getbufline()" function
11411 */
11412 static void
11413f_getbufline(argvars, rettv)
11414 typval_T *argvars;
11415 typval_T *rettv;
11416{
11417 linenr_T lnum;
11418 linenr_T end;
11419 buf_T *buf;
11420
11421 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11422 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011423 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011424 --emsg_off;
11425
Bram Moolenaar661b1822005-07-28 22:36:45 +000011426 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011427 if (argvars[2].v_type == VAR_UNKNOWN)
11428 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011429 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011430 end = get_tv_lnum_buf(&argvars[2], buf);
11431
Bram Moolenaar342337a2005-07-21 21:11:17 +000011432 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011433}
11434
Bram Moolenaar0d660222005-01-07 21:51:51 +000011435/*
11436 * "getbufvar()" function
11437 */
11438 static void
11439f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *argvars;
11441 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011442{
11443 buf_T *buf;
11444 buf_T *save_curbuf;
11445 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011446 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011447 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11450 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011451 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011452 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011453
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011454 rettv->v_type = VAR_STRING;
11455 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011456
11457 if (buf != NULL && varname != NULL)
11458 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011459 /* set curbuf to be our buf, temporarily */
11460 save_curbuf = curbuf;
11461 curbuf = buf;
11462
Bram Moolenaar0d660222005-01-07 21:51:51 +000011463 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011464 {
11465 if (get_option_tv(&varname, rettv, TRUE) == OK)
11466 done = TRUE;
11467 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011468 else if (STRCMP(varname, "changedtick") == 0)
11469 {
11470 rettv->v_type = VAR_NUMBER;
11471 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011472 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011473 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011474 else
11475 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011476 /* Look up the variable. */
11477 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11478 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11479 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011480 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011481 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011483 done = TRUE;
11484 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011485 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011486
11487 /* restore previous notion of curbuf */
11488 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011489 }
11490
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011491 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11492 /* use the default value */
11493 copy_tv(&argvars[2], rettv);
11494
Bram Moolenaar0d660222005-01-07 21:51:51 +000011495 --emsg_off;
11496}
11497
11498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 * "getchar()" function
11500 */
11501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011503 typval_T *argvars;
11504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505{
11506 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011507 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011509 /* Position the cursor. Needed after a message that ends in a space. */
11510 windgoto(msg_row, msg_col);
11511
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 ++no_mapping;
11513 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011514 for (;;)
11515 {
11516 if (argvars[0].v_type == VAR_UNKNOWN)
11517 /* getchar(): blocking wait. */
11518 n = safe_vgetc();
11519 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11520 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011521 n = vpeekc_any();
11522 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011523 /* illegal argument or getchar(0) and no char avail: return zero */
11524 n = 0;
11525 else
11526 /* getchar(0) and char avail: return char */
11527 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011528
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011529 if (n == K_IGNORE)
11530 continue;
11531 break;
11532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533 --no_mapping;
11534 --allow_keys;
11535
Bram Moolenaar219b8702006-11-01 14:32:36 +000011536 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11537 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11538 vimvars[VV_MOUSE_COL].vv_nr = 0;
11539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 if (IS_SPECIAL(n) || mod_mask != 0)
11542 {
11543 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11544 int i = 0;
11545
11546 /* Turn a special key into three bytes, plus modifier. */
11547 if (mod_mask != 0)
11548 {
11549 temp[i++] = K_SPECIAL;
11550 temp[i++] = KS_MODIFIER;
11551 temp[i++] = mod_mask;
11552 }
11553 if (IS_SPECIAL(n))
11554 {
11555 temp[i++] = K_SPECIAL;
11556 temp[i++] = K_SECOND(n);
11557 temp[i++] = K_THIRD(n);
11558 }
11559#ifdef FEAT_MBYTE
11560 else if (has_mbyte)
11561 i += (*mb_char2bytes)(n, temp + i);
11562#endif
11563 else
11564 temp[i++] = n;
11565 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566 rettv->v_type = VAR_STRING;
11567 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011568
11569#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011570 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011571 {
11572 int row = mouse_row;
11573 int col = mouse_col;
11574 win_T *win;
11575 linenr_T lnum;
11576# ifdef FEAT_WINDOWS
11577 win_T *wp;
11578# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011579 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011580
11581 if (row >= 0 && col >= 0)
11582 {
11583 /* Find the window at the mouse coordinates and compute the
11584 * text position. */
11585 win = mouse_find_win(&row, &col);
11586 (void)mouse_comp_pos(win, &row, &col, &lnum);
11587# ifdef FEAT_WINDOWS
11588 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011589 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011590# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011591 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011592 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11593 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11594 }
11595 }
11596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 }
11598}
11599
11600/*
11601 * "getcharmod()" function
11602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011604f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011605 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011608 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609}
11610
11611/*
11612 * "getcmdline()" function
11613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011616 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 rettv->v_type = VAR_STRING;
11620 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621}
11622
11623/*
11624 * "getcmdpos()" function
11625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011628 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632}
11633
11634/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011635 * "getcmdtype()" function
11636 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011637 static void
11638f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011639 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011640 typval_T *rettv;
11641{
11642 rettv->v_type = VAR_STRING;
11643 rettv->vval.v_string = alloc(2);
11644 if (rettv->vval.v_string != NULL)
11645 {
11646 rettv->vval.v_string[0] = get_cmdline_type();
11647 rettv->vval.v_string[1] = NUL;
11648 }
11649}
11650
11651/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011652 * "getcmdwintype()" function
11653 */
11654 static void
11655f_getcmdwintype(argvars, rettv)
11656 typval_T *argvars UNUSED;
11657 typval_T *rettv;
11658{
11659 rettv->v_type = VAR_STRING;
11660 rettv->vval.v_string = NULL;
11661#ifdef FEAT_CMDWIN
11662 rettv->vval.v_string = alloc(2);
11663 if (rettv->vval.v_string != NULL)
11664 {
11665 rettv->vval.v_string[0] = cmdwin_type;
11666 rettv->vval.v_string[1] = NUL;
11667 }
11668#endif
11669}
11670
11671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 * "getcwd()" function
11673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011675f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011676 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011679 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011682 rettv->vval.v_string = NULL;
11683 cwd = alloc(MAXPATHL);
11684 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011686 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11687 {
11688 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011690 if (rettv->vval.v_string != NULL)
11691 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011693 }
11694 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695 }
11696}
11697
11698/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011699 * "getfontname()" function
11700 */
11701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011702f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011703 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011704 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011705{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011706 rettv->v_type = VAR_STRING;
11707 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011708#ifdef FEAT_GUI
11709 if (gui.in_use)
11710 {
11711 GuiFont font;
11712 char_u *name = NULL;
11713
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011714 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011715 {
11716 /* Get the "Normal" font. Either the name saved by
11717 * hl_set_font_name() or from the font ID. */
11718 font = gui.norm_font;
11719 name = hl_get_font_name();
11720 }
11721 else
11722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011723 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011724 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11725 return;
11726 font = gui_mch_get_font(name, FALSE);
11727 if (font == NOFONT)
11728 return; /* Invalid font name, return empty string. */
11729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011731 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011732 gui_mch_free_font(font);
11733 }
11734#endif
11735}
11736
11737/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011738 * "getfperm({fname})" function
11739 */
11740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011741f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011742 typval_T *argvars;
11743 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011744{
11745 char_u *fname;
11746 struct stat st;
11747 char_u *perm = NULL;
11748 char_u flags[] = "rwx";
11749 int i;
11750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011751 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011754 if (mch_stat((char *)fname, &st) >= 0)
11755 {
11756 perm = vim_strsave((char_u *)"---------");
11757 if (perm != NULL)
11758 {
11759 for (i = 0; i < 9; i++)
11760 {
11761 if (st.st_mode & (1 << (8 - i)))
11762 perm[i] = flags[i % 3];
11763 }
11764 }
11765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011766 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011767}
11768
11769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 * "getfsize({fname})" function
11771 */
11772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011773f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011774 typval_T *argvars;
11775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776{
11777 char_u *fname;
11778 struct stat st;
11779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011780 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011782 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783
11784 if (mch_stat((char *)fname, &st) >= 0)
11785 {
11786 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011791
11792 /* non-perfect check for overflow */
11793 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11794 rettv->vval.v_number = -2;
11795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 }
11797 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011798 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799}
11800
11801/*
11802 * "getftime({fname})" function
11803 */
11804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011805f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011806 typval_T *argvars;
11807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808{
11809 char_u *fname;
11810 struct stat st;
11811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813
11814 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818}
11819
11820/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011821 * "getftype({fname})" function
11822 */
11823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011825 typval_T *argvars;
11826 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011827{
11828 char_u *fname;
11829 struct stat st;
11830 char_u *type = NULL;
11831 char *t;
11832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011835 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011836 if (mch_lstat((char *)fname, &st) >= 0)
11837 {
11838#ifdef S_ISREG
11839 if (S_ISREG(st.st_mode))
11840 t = "file";
11841 else if (S_ISDIR(st.st_mode))
11842 t = "dir";
11843# ifdef S_ISLNK
11844 else if (S_ISLNK(st.st_mode))
11845 t = "link";
11846# endif
11847# ifdef S_ISBLK
11848 else if (S_ISBLK(st.st_mode))
11849 t = "bdev";
11850# endif
11851# ifdef S_ISCHR
11852 else if (S_ISCHR(st.st_mode))
11853 t = "cdev";
11854# endif
11855# ifdef S_ISFIFO
11856 else if (S_ISFIFO(st.st_mode))
11857 t = "fifo";
11858# endif
11859# ifdef S_ISSOCK
11860 else if (S_ISSOCK(st.st_mode))
11861 t = "fifo";
11862# endif
11863 else
11864 t = "other";
11865#else
11866# ifdef S_IFMT
11867 switch (st.st_mode & S_IFMT)
11868 {
11869 case S_IFREG: t = "file"; break;
11870 case S_IFDIR: t = "dir"; break;
11871# ifdef S_IFLNK
11872 case S_IFLNK: t = "link"; break;
11873# endif
11874# ifdef S_IFBLK
11875 case S_IFBLK: t = "bdev"; break;
11876# endif
11877# ifdef S_IFCHR
11878 case S_IFCHR: t = "cdev"; break;
11879# endif
11880# ifdef S_IFIFO
11881 case S_IFIFO: t = "fifo"; break;
11882# endif
11883# ifdef S_IFSOCK
11884 case S_IFSOCK: t = "socket"; break;
11885# endif
11886 default: t = "other";
11887 }
11888# else
11889 if (mch_isdir(fname))
11890 t = "dir";
11891 else
11892 t = "file";
11893# endif
11894#endif
11895 type = vim_strsave((char_u *)t);
11896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011897 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011898}
11899
11900/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011901 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011902 */
11903 static void
11904f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011905 typval_T *argvars;
11906 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011907{
11908 linenr_T lnum;
11909 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011910 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011911
11912 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011913 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011914 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011915 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011916 retlist = FALSE;
11917 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011918 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011919 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011920 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011921 retlist = TRUE;
11922 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011923
Bram Moolenaar342337a2005-07-21 21:11:17 +000011924 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011925}
11926
11927/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011928 * "getmatches()" function
11929 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011930 static void
11931f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011932 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011933 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011934{
11935#ifdef FEAT_SEARCH_EXTRA
11936 dict_T *dict;
11937 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011938 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011939
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011940 if (rettv_list_alloc(rettv) == OK)
11941 {
11942 while (cur != NULL)
11943 {
11944 dict = dict_alloc();
11945 if (dict == NULL)
11946 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011947 if (cur->match.regprog == NULL)
11948 {
11949 /* match added with matchaddpos() */
11950 for (i = 0; i < MAXPOSMATCH; ++i)
11951 {
11952 llpos_T *llpos;
11953 char buf[6];
11954 list_T *l;
11955
11956 llpos = &cur->pos.pos[i];
11957 if (llpos->lnum == 0)
11958 break;
11959 l = list_alloc();
11960 if (l == NULL)
11961 break;
11962 list_append_number(l, (varnumber_T)llpos->lnum);
11963 if (llpos->col > 0)
11964 {
11965 list_append_number(l, (varnumber_T)llpos->col);
11966 list_append_number(l, (varnumber_T)llpos->len);
11967 }
11968 sprintf(buf, "pos%d", i + 1);
11969 dict_add_list(dict, buf, l);
11970 }
11971 }
11972 else
11973 {
11974 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11975 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011976 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011977 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11978 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11979 list_append_dict(rettv->vval.v_list, dict);
11980 cur = cur->next;
11981 }
11982 }
11983#endif
11984}
11985
11986/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011987 * "getpid()" function
11988 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011989 static void
11990f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011991 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011992 typval_T *rettv;
11993{
11994 rettv->vval.v_number = mch_get_pid();
11995}
11996
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011997static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
11998
11999/*
12000 * "getcurpos()" function
12001 */
12002 static void
12003f_getcurpos(argvars, rettv)
12004 typval_T *argvars;
12005 typval_T *rettv;
12006{
12007 getpos_both(argvars, rettv, TRUE);
12008}
12009
Bram Moolenaar18081e32008-02-20 19:11:07 +000012010/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012011 * "getpos(string)" function
12012 */
12013 static void
12014f_getpos(argvars, rettv)
12015 typval_T *argvars;
12016 typval_T *rettv;
12017{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012018 getpos_both(argvars, rettv, FALSE);
12019}
12020
12021 static void
12022getpos_both(argvars, rettv, getcurpos)
12023 typval_T *argvars;
12024 typval_T *rettv;
12025 int getcurpos;
12026{
Bram Moolenaara5525202006-03-02 22:52:09 +000012027 pos_T *fp;
12028 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012029 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012030
12031 if (rettv_list_alloc(rettv) == OK)
12032 {
12033 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012034 if (getcurpos)
12035 fp = &curwin->w_cursor;
12036 else
12037 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012038 if (fnum != -1)
12039 list_append_number(l, (varnumber_T)fnum);
12040 else
12041 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012042 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12043 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012044 list_append_number(l, (fp != NULL)
12045 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012046 : (varnumber_T)0);
12047 list_append_number(l,
12048#ifdef FEAT_VIRTUALEDIT
12049 (fp != NULL) ? (varnumber_T)fp->coladd :
12050#endif
12051 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012052 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012053 list_append_number(l, curwin->w_curswant == MAXCOL ?
12054 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012055 }
12056 else
12057 rettv->vval.v_number = FALSE;
12058}
12059
12060/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012061 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012062 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012063 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012064f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012065 typval_T *argvars UNUSED;
12066 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012067{
12068#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012069 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012070#endif
12071
Bram Moolenaar2641f772005-03-25 21:58:17 +000012072#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012073 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012074 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012075 wp = NULL;
12076 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12077 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012078 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012079 if (wp == NULL)
12080 return;
12081 }
12082
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012083 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012084 }
12085#endif
12086}
12087
12088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 * "getreg()" function
12090 */
12091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012093 typval_T *argvars;
12094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095{
12096 char_u *strregname;
12097 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012098 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012099 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012100 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012102 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012104 strregname = get_tv_string_chk(&argvars[0]);
12105 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012106 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012107 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012108 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012109 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12110 return_list = get_tv_number_chk(&argvars[2], &error);
12111 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012114 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012115
12116 if (error)
12117 return;
12118
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 regname = (strregname == NULL ? '"' : *strregname);
12120 if (regname == 0)
12121 regname = '"';
12122
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012123 if (return_list)
12124 {
12125 rettv->v_type = VAR_LIST;
12126 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12127 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012128 if (rettv->vval.v_list != NULL)
12129 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012130 }
12131 else
12132 {
12133 rettv->v_type = VAR_STRING;
12134 rettv->vval.v_string = get_reg_contents(regname,
12135 arg2 ? GREG_EXPR_SRC : 0);
12136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137}
12138
12139/*
12140 * "getregtype()" function
12141 */
12142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012144 typval_T *argvars;
12145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146{
12147 char_u *strregname;
12148 int regname;
12149 char_u buf[NUMBUFLEN + 2];
12150 long reglen = 0;
12151
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012152 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153 {
12154 strregname = get_tv_string_chk(&argvars[0]);
12155 if (strregname == NULL) /* type error; errmsg already given */
12156 {
12157 rettv->v_type = VAR_STRING;
12158 rettv->vval.v_string = NULL;
12159 return;
12160 }
12161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 else
12163 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012164 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165
12166 regname = (strregname == NULL ? '"' : *strregname);
12167 if (regname == 0)
12168 regname = '"';
12169
12170 buf[0] = NUL;
12171 buf[1] = NUL;
12172 switch (get_reg_type(regname, &reglen))
12173 {
12174 case MLINE: buf[0] = 'V'; break;
12175 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 case MBLOCK:
12177 buf[0] = Ctrl_V;
12178 sprintf((char *)buf + 1, "%ld", reglen + 1);
12179 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181 rettv->v_type = VAR_STRING;
12182 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183}
12184
12185/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012186 * "gettabvar()" function
12187 */
12188 static void
12189f_gettabvar(argvars, rettv)
12190 typval_T *argvars;
12191 typval_T *rettv;
12192{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012193 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012194 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012195 dictitem_T *v;
12196 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012197 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012198
12199 rettv->v_type = VAR_STRING;
12200 rettv->vval.v_string = NULL;
12201
12202 varname = get_tv_string_chk(&argvars[1]);
12203 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12204 if (tp != NULL && varname != NULL)
12205 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012206 /* Set tp to be our tabpage, temporarily. Also set the window to the
12207 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012208 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12209 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012210 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012211 /* look up the variable */
12212 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12213 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12214 if (v != NULL)
12215 {
12216 copy_tv(&v->di_tv, rettv);
12217 done = TRUE;
12218 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012219 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012220
12221 /* restore previous notion of curwin */
12222 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012223 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012224
12225 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012226 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012227}
12228
12229/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012230 * "gettabwinvar()" function
12231 */
12232 static void
12233f_gettabwinvar(argvars, rettv)
12234 typval_T *argvars;
12235 typval_T *rettv;
12236{
12237 getwinvar(argvars, rettv, 1);
12238}
12239
12240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 * "getwinposx()" function
12242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012245 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012248 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249#ifdef FEAT_GUI
12250 if (gui.in_use)
12251 {
12252 int x, y;
12253
12254 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012255 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 }
12257#endif
12258}
12259
12260/*
12261 * "getwinposy()" function
12262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012265 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269#ifdef FEAT_GUI
12270 if (gui.in_use)
12271 {
12272 int x, y;
12273
12274 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 }
12277#endif
12278}
12279
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012280/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012281 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012282 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012283 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012284find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012285 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012286 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012287{
12288#ifdef FEAT_WINDOWS
12289 win_T *wp;
12290#endif
12291 int nr;
12292
12293 nr = get_tv_number_chk(vp, NULL);
12294
12295#ifdef FEAT_WINDOWS
12296 if (nr < 0)
12297 return NULL;
12298 if (nr == 0)
12299 return curwin;
12300
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012301 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12302 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012303 if (--nr <= 0)
12304 break;
12305 return wp;
12306#else
12307 if (nr == 0 || nr == 1)
12308 return curwin;
12309 return NULL;
12310#endif
12311}
12312
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313/*
12314 * "getwinvar()" function
12315 */
12316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012318 typval_T *argvars;
12319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012321 getwinvar(argvars, rettv, 0);
12322}
12323
12324/*
12325 * getwinvar() and gettabwinvar()
12326 */
12327 static void
12328getwinvar(argvars, rettv, off)
12329 typval_T *argvars;
12330 typval_T *rettv;
12331 int off; /* 1 for gettabwinvar() */
12332{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 win_T *win, *oldcurwin;
12334 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012335 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012336 tabpage_T *tp = NULL;
12337 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012338 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012340#ifdef FEAT_WINDOWS
12341 if (off == 1)
12342 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12343 else
12344 tp = curtab;
12345#endif
12346 win = find_win_by_nr(&argvars[off], tp);
12347 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012348 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012350 rettv->v_type = VAR_STRING;
12351 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352
12353 if (win != NULL && varname != NULL)
12354 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012355 /* Set curwin to be our win, temporarily. Also set the tabpage,
12356 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012357 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012358 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012359 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012360 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012361 if (get_option_tv(&varname, rettv, 1) == OK)
12362 done = TRUE;
12363 }
12364 else
12365 {
12366 /* Look up the variable. */
12367 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12368 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12369 varname, FALSE);
12370 if (v != NULL)
12371 {
12372 copy_tv(&v->di_tv, rettv);
12373 done = TRUE;
12374 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012377
12378 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012379 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 }
12381
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012382 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12383 /* use the default return value */
12384 copy_tv(&argvars[off + 2], rettv);
12385
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386 --emsg_off;
12387}
12388
12389/*
12390 * "glob()" function
12391 */
12392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012393f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012394 typval_T *argvars;
12395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012397 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012399 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012401 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012402 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012403 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012404 if (argvars[1].v_type != VAR_UNKNOWN)
12405 {
12406 if (get_tv_number_chk(&argvars[1], &error))
12407 options |= WILD_KEEP_ALL;
12408 if (argvars[2].v_type != VAR_UNKNOWN
12409 && get_tv_number_chk(&argvars[2], &error))
12410 {
12411 rettv->v_type = VAR_LIST;
12412 rettv->vval.v_list = NULL;
12413 }
12414 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012415 if (!error)
12416 {
12417 ExpandInit(&xpc);
12418 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012419 if (p_wic)
12420 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012421 if (rettv->v_type == VAR_STRING)
12422 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012423 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012424 else if (rettv_list_alloc(rettv) != FAIL)
12425 {
12426 int i;
12427
12428 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12429 NULL, options, WILD_ALL_KEEP);
12430 for (i = 0; i < xpc.xp_numfiles; i++)
12431 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12432
12433 ExpandCleanup(&xpc);
12434 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012435 }
12436 else
12437 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438}
12439
12440/*
12441 * "globpath()" function
12442 */
12443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012445 typval_T *argvars;
12446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012448 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012450 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012451 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012452 garray_T ga;
12453 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012455 /* When the optional second argument is non-zero, don't remove matches
12456 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012458 if (argvars[2].v_type != VAR_UNKNOWN)
12459 {
12460 if (get_tv_number_chk(&argvars[2], &error))
12461 flags |= WILD_KEEP_ALL;
12462 if (argvars[3].v_type != VAR_UNKNOWN
12463 && get_tv_number_chk(&argvars[3], &error))
12464 {
12465 rettv->v_type = VAR_LIST;
12466 rettv->vval.v_list = NULL;
12467 }
12468 }
12469 if (file != NULL && !error)
12470 {
12471 ga_init2(&ga, (int)sizeof(char_u *), 10);
12472 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12473 if (rettv->v_type == VAR_STRING)
12474 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12475 else if (rettv_list_alloc(rettv) != FAIL)
12476 for (i = 0; i < ga.ga_len; ++i)
12477 list_append_string(rettv->vval.v_list,
12478 ((char_u **)(ga.ga_data))[i], -1);
12479 ga_clear_strings(&ga);
12480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012481 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012482 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483}
12484
12485/*
12486 * "has()" function
12487 */
12488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012489f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012490 typval_T *argvars;
12491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492{
12493 int i;
12494 char_u *name;
12495 int n = FALSE;
12496 static char *(has_list[]) =
12497 {
12498#ifdef AMIGA
12499 "amiga",
12500# ifdef FEAT_ARP
12501 "arp",
12502# endif
12503#endif
12504#ifdef __BEOS__
12505 "beos",
12506#endif
12507#ifdef MSDOS
12508# ifdef DJGPP
12509 "dos32",
12510# else
12511 "dos16",
12512# endif
12513#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012514#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515 "mac",
12516#endif
12517#if defined(MACOS_X_UNIX)
12518 "macunix",
12519#endif
12520#ifdef OS2
12521 "os2",
12522#endif
12523#ifdef __QNX__
12524 "qnx",
12525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526#ifdef UNIX
12527 "unix",
12528#endif
12529#ifdef VMS
12530 "vms",
12531#endif
12532#ifdef WIN16
12533 "win16",
12534#endif
12535#ifdef WIN32
12536 "win32",
12537#endif
12538#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12539 "win32unix",
12540#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012541#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542 "win64",
12543#endif
12544#ifdef EBCDIC
12545 "ebcdic",
12546#endif
12547#ifndef CASE_INSENSITIVE_FILENAME
12548 "fname_case",
12549#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012550#ifdef HAVE_ACL
12551 "acl",
12552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553#ifdef FEAT_ARABIC
12554 "arabic",
12555#endif
12556#ifdef FEAT_AUTOCMD
12557 "autocmd",
12558#endif
12559#ifdef FEAT_BEVAL
12560 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012561# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12562 "balloon_multiline",
12563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564#endif
12565#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12566 "builtin_terms",
12567# ifdef ALL_BUILTIN_TCAPS
12568 "all_builtin_terms",
12569# endif
12570#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012571#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12572 || defined(FEAT_GUI_W32) \
12573 || defined(FEAT_GUI_MOTIF))
12574 "browsefilter",
12575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576#ifdef FEAT_BYTEOFF
12577 "byte_offset",
12578#endif
12579#ifdef FEAT_CINDENT
12580 "cindent",
12581#endif
12582#ifdef FEAT_CLIENTSERVER
12583 "clientserver",
12584#endif
12585#ifdef FEAT_CLIPBOARD
12586 "clipboard",
12587#endif
12588#ifdef FEAT_CMDL_COMPL
12589 "cmdline_compl",
12590#endif
12591#ifdef FEAT_CMDHIST
12592 "cmdline_hist",
12593#endif
12594#ifdef FEAT_COMMENTS
12595 "comments",
12596#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012597#ifdef FEAT_CONCEAL
12598 "conceal",
12599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600#ifdef FEAT_CRYPT
12601 "cryptv",
12602#endif
12603#ifdef FEAT_CSCOPE
12604 "cscope",
12605#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012606#ifdef FEAT_CURSORBIND
12607 "cursorbind",
12608#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012609#ifdef CURSOR_SHAPE
12610 "cursorshape",
12611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612#ifdef DEBUG
12613 "debug",
12614#endif
12615#ifdef FEAT_CON_DIALOG
12616 "dialog_con",
12617#endif
12618#ifdef FEAT_GUI_DIALOG
12619 "dialog_gui",
12620#endif
12621#ifdef FEAT_DIFF
12622 "diff",
12623#endif
12624#ifdef FEAT_DIGRAPHS
12625 "digraphs",
12626#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012627#ifdef FEAT_DIRECTX
12628 "directx",
12629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630#ifdef FEAT_DND
12631 "dnd",
12632#endif
12633#ifdef FEAT_EMACS_TAGS
12634 "emacs_tags",
12635#endif
12636 "eval", /* always present, of course! */
12637#ifdef FEAT_EX_EXTRA
12638 "ex_extra",
12639#endif
12640#ifdef FEAT_SEARCH_EXTRA
12641 "extra_search",
12642#endif
12643#ifdef FEAT_FKMAP
12644 "farsi",
12645#endif
12646#ifdef FEAT_SEARCHPATH
12647 "file_in_path",
12648#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012649#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012650 "filterpipe",
12651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652#ifdef FEAT_FIND_ID
12653 "find_in_path",
12654#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012655#ifdef FEAT_FLOAT
12656 "float",
12657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658#ifdef FEAT_FOLDING
12659 "folding",
12660#endif
12661#ifdef FEAT_FOOTER
12662 "footer",
12663#endif
12664#if !defined(USE_SYSTEM) && defined(UNIX)
12665 "fork",
12666#endif
12667#ifdef FEAT_GETTEXT
12668 "gettext",
12669#endif
12670#ifdef FEAT_GUI
12671 "gui",
12672#endif
12673#ifdef FEAT_GUI_ATHENA
12674# ifdef FEAT_GUI_NEXTAW
12675 "gui_neXtaw",
12676# else
12677 "gui_athena",
12678# endif
12679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680#ifdef FEAT_GUI_GTK
12681 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012684#ifdef FEAT_GUI_GNOME
12685 "gui_gnome",
12686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687#ifdef FEAT_GUI_MAC
12688 "gui_mac",
12689#endif
12690#ifdef FEAT_GUI_MOTIF
12691 "gui_motif",
12692#endif
12693#ifdef FEAT_GUI_PHOTON
12694 "gui_photon",
12695#endif
12696#ifdef FEAT_GUI_W16
12697 "gui_win16",
12698#endif
12699#ifdef FEAT_GUI_W32
12700 "gui_win32",
12701#endif
12702#ifdef FEAT_HANGULIN
12703 "hangul_input",
12704#endif
12705#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12706 "iconv",
12707#endif
12708#ifdef FEAT_INS_EXPAND
12709 "insert_expand",
12710#endif
12711#ifdef FEAT_JUMPLIST
12712 "jumplist",
12713#endif
12714#ifdef FEAT_KEYMAP
12715 "keymap",
12716#endif
12717#ifdef FEAT_LANGMAP
12718 "langmap",
12719#endif
12720#ifdef FEAT_LIBCALL
12721 "libcall",
12722#endif
12723#ifdef FEAT_LINEBREAK
12724 "linebreak",
12725#endif
12726#ifdef FEAT_LISP
12727 "lispindent",
12728#endif
12729#ifdef FEAT_LISTCMDS
12730 "listcmds",
12731#endif
12732#ifdef FEAT_LOCALMAP
12733 "localmap",
12734#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012735#ifdef FEAT_LUA
12736# ifndef DYNAMIC_LUA
12737 "lua",
12738# endif
12739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740#ifdef FEAT_MENU
12741 "menu",
12742#endif
12743#ifdef FEAT_SESSION
12744 "mksession",
12745#endif
12746#ifdef FEAT_MODIFY_FNAME
12747 "modify_fname",
12748#endif
12749#ifdef FEAT_MOUSE
12750 "mouse",
12751#endif
12752#ifdef FEAT_MOUSESHAPE
12753 "mouseshape",
12754#endif
12755#if defined(UNIX) || defined(VMS)
12756# ifdef FEAT_MOUSE_DEC
12757 "mouse_dec",
12758# endif
12759# ifdef FEAT_MOUSE_GPM
12760 "mouse_gpm",
12761# endif
12762# ifdef FEAT_MOUSE_JSB
12763 "mouse_jsbterm",
12764# endif
12765# ifdef FEAT_MOUSE_NET
12766 "mouse_netterm",
12767# endif
12768# ifdef FEAT_MOUSE_PTERM
12769 "mouse_pterm",
12770# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012771# ifdef FEAT_MOUSE_SGR
12772 "mouse_sgr",
12773# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012774# ifdef FEAT_SYSMOUSE
12775 "mouse_sysmouse",
12776# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012777# ifdef FEAT_MOUSE_URXVT
12778 "mouse_urxvt",
12779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780# ifdef FEAT_MOUSE_XTERM
12781 "mouse_xterm",
12782# endif
12783#endif
12784#ifdef FEAT_MBYTE
12785 "multi_byte",
12786#endif
12787#ifdef FEAT_MBYTE_IME
12788 "multi_byte_ime",
12789#endif
12790#ifdef FEAT_MULTI_LANG
12791 "multi_lang",
12792#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012793#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012794#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012795 "mzscheme",
12796#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798#ifdef FEAT_OLE
12799 "ole",
12800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801#ifdef FEAT_PATH_EXTRA
12802 "path_extra",
12803#endif
12804#ifdef FEAT_PERL
12805#ifndef DYNAMIC_PERL
12806 "perl",
12807#endif
12808#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012809#ifdef FEAT_PERSISTENT_UNDO
12810 "persistent_undo",
12811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812#ifdef FEAT_PYTHON
12813#ifndef DYNAMIC_PYTHON
12814 "python",
12815#endif
12816#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012817#ifdef FEAT_PYTHON3
12818#ifndef DYNAMIC_PYTHON3
12819 "python3",
12820#endif
12821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822#ifdef FEAT_POSTSCRIPT
12823 "postscript",
12824#endif
12825#ifdef FEAT_PRINTER
12826 "printer",
12827#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012828#ifdef FEAT_PROFILE
12829 "profile",
12830#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012831#ifdef FEAT_RELTIME
12832 "reltime",
12833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834#ifdef FEAT_QUICKFIX
12835 "quickfix",
12836#endif
12837#ifdef FEAT_RIGHTLEFT
12838 "rightleft",
12839#endif
12840#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12841 "ruby",
12842#endif
12843#ifdef FEAT_SCROLLBIND
12844 "scrollbind",
12845#endif
12846#ifdef FEAT_CMDL_INFO
12847 "showcmd",
12848 "cmdline_info",
12849#endif
12850#ifdef FEAT_SIGNS
12851 "signs",
12852#endif
12853#ifdef FEAT_SMARTINDENT
12854 "smartindent",
12855#endif
12856#ifdef FEAT_SNIFF
12857 "sniff",
12858#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012859#ifdef STARTUPTIME
12860 "startuptime",
12861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862#ifdef FEAT_STL_OPT
12863 "statusline",
12864#endif
12865#ifdef FEAT_SUN_WORKSHOP
12866 "sun_workshop",
12867#endif
12868#ifdef FEAT_NETBEANS_INTG
12869 "netbeans_intg",
12870#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012871#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012872 "spell",
12873#endif
12874#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875 "syntax",
12876#endif
12877#if defined(USE_SYSTEM) || !defined(UNIX)
12878 "system",
12879#endif
12880#ifdef FEAT_TAG_BINS
12881 "tag_binary",
12882#endif
12883#ifdef FEAT_TAG_OLDSTATIC
12884 "tag_old_static",
12885#endif
12886#ifdef FEAT_TAG_ANYWHITE
12887 "tag_any_white",
12888#endif
12889#ifdef FEAT_TCL
12890# ifndef DYNAMIC_TCL
12891 "tcl",
12892# endif
12893#endif
12894#ifdef TERMINFO
12895 "terminfo",
12896#endif
12897#ifdef FEAT_TERMRESPONSE
12898 "termresponse",
12899#endif
12900#ifdef FEAT_TEXTOBJ
12901 "textobjects",
12902#endif
12903#ifdef HAVE_TGETENT
12904 "tgetent",
12905#endif
12906#ifdef FEAT_TITLE
12907 "title",
12908#endif
12909#ifdef FEAT_TOOLBAR
12910 "toolbar",
12911#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012912#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12913 "unnamedplus",
12914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915#ifdef FEAT_USR_CMDS
12916 "user-commands", /* was accidentally included in 5.4 */
12917 "user_commands",
12918#endif
12919#ifdef FEAT_VIMINFO
12920 "viminfo",
12921#endif
12922#ifdef FEAT_VERTSPLIT
12923 "vertsplit",
12924#endif
12925#ifdef FEAT_VIRTUALEDIT
12926 "virtualedit",
12927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929#ifdef FEAT_VISUALEXTRA
12930 "visualextra",
12931#endif
12932#ifdef FEAT_VREPLACE
12933 "vreplace",
12934#endif
12935#ifdef FEAT_WILDIGN
12936 "wildignore",
12937#endif
12938#ifdef FEAT_WILDMENU
12939 "wildmenu",
12940#endif
12941#ifdef FEAT_WINDOWS
12942 "windows",
12943#endif
12944#ifdef FEAT_WAK
12945 "winaltkeys",
12946#endif
12947#ifdef FEAT_WRITEBACKUP
12948 "writebackup",
12949#endif
12950#ifdef FEAT_XIM
12951 "xim",
12952#endif
12953#ifdef FEAT_XFONTSET
12954 "xfontset",
12955#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012956#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012957 "xpm",
12958 "xpm_w32", /* for backward compatibility */
12959#else
12960# if defined(HAVE_XPM)
12961 "xpm",
12962# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964#ifdef USE_XSMP
12965 "xsmp",
12966#endif
12967#ifdef USE_XSMP_INTERACT
12968 "xsmp_interact",
12969#endif
12970#ifdef FEAT_XCLIPBOARD
12971 "xterm_clipboard",
12972#endif
12973#ifdef FEAT_XTERM_SAVE
12974 "xterm_save",
12975#endif
12976#if defined(UNIX) && defined(FEAT_X11)
12977 "X11",
12978#endif
12979 NULL
12980 };
12981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 for (i = 0; has_list[i] != NULL; ++i)
12984 if (STRICMP(name, has_list[i]) == 0)
12985 {
12986 n = TRUE;
12987 break;
12988 }
12989
12990 if (n == FALSE)
12991 {
12992 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012993 {
12994 if (name[5] == '-'
12995 && STRLEN(name) > 11
12996 && vim_isdigit(name[6])
12997 && vim_isdigit(name[8])
12998 && vim_isdigit(name[10]))
12999 {
13000 int major = atoi((char *)name + 6);
13001 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013002
13003 /* Expect "patch-9.9.01234". */
13004 n = (major < VIM_VERSION_MAJOR
13005 || (major == VIM_VERSION_MAJOR
13006 && (minor < VIM_VERSION_MINOR
13007 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013008 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013009 }
13010 else
13011 n = has_patch(atoi((char *)name + 5));
13012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013 else if (STRICMP(name, "vim_starting") == 0)
13014 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013015#ifdef FEAT_MBYTE
13016 else if (STRICMP(name, "multi_byte_encoding") == 0)
13017 n = has_mbyte;
13018#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013019#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13020 else if (STRICMP(name, "balloon_multiline") == 0)
13021 n = multiline_balloon_available();
13022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023#ifdef DYNAMIC_TCL
13024 else if (STRICMP(name, "tcl") == 0)
13025 n = tcl_enabled(FALSE);
13026#endif
13027#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13028 else if (STRICMP(name, "iconv") == 0)
13029 n = iconv_enabled(FALSE);
13030#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013031#ifdef DYNAMIC_LUA
13032 else if (STRICMP(name, "lua") == 0)
13033 n = lua_enabled(FALSE);
13034#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013035#ifdef DYNAMIC_MZSCHEME
13036 else if (STRICMP(name, "mzscheme") == 0)
13037 n = mzscheme_enabled(FALSE);
13038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039#ifdef DYNAMIC_RUBY
13040 else if (STRICMP(name, "ruby") == 0)
13041 n = ruby_enabled(FALSE);
13042#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013043#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044#ifdef DYNAMIC_PYTHON
13045 else if (STRICMP(name, "python") == 0)
13046 n = python_enabled(FALSE);
13047#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013048#endif
13049#ifdef FEAT_PYTHON3
13050#ifdef DYNAMIC_PYTHON3
13051 else if (STRICMP(name, "python3") == 0)
13052 n = python3_enabled(FALSE);
13053#endif
13054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055#ifdef DYNAMIC_PERL
13056 else if (STRICMP(name, "perl") == 0)
13057 n = perl_enabled(FALSE);
13058#endif
13059#ifdef FEAT_GUI
13060 else if (STRICMP(name, "gui_running") == 0)
13061 n = (gui.in_use || gui.starting);
13062# ifdef FEAT_GUI_W32
13063 else if (STRICMP(name, "gui_win32s") == 0)
13064 n = gui_is_win32s();
13065# endif
13066# ifdef FEAT_BROWSE
13067 else if (STRICMP(name, "browse") == 0)
13068 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13069# endif
13070#endif
13071#ifdef FEAT_SYN_HL
13072 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013073 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074#endif
13075#if defined(WIN3264)
13076 else if (STRICMP(name, "win95") == 0)
13077 n = mch_windows95();
13078#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013079#ifdef FEAT_NETBEANS_INTG
13080 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013081 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 }
13084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013085 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086}
13087
13088/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013089 * "has_key()" function
13090 */
13091 static void
13092f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013093 typval_T *argvars;
13094 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013095{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013096 if (argvars[0].v_type != VAR_DICT)
13097 {
13098 EMSG(_(e_dictreq));
13099 return;
13100 }
13101 if (argvars[0].vval.v_dict == NULL)
13102 return;
13103
13104 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013105 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013106}
13107
13108/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013109 * "haslocaldir()" function
13110 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013111 static void
13112f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013113 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013114 typval_T *rettv;
13115{
13116 rettv->vval.v_number = (curwin->w_localdir != NULL);
13117}
13118
13119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 * "hasmapto()" function
13121 */
13122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013123f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013124 typval_T *argvars;
13125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126{
13127 char_u *name;
13128 char_u *mode;
13129 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013130 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013132 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013133 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 mode = (char_u *)"nvo";
13135 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013138 if (argvars[2].v_type != VAR_UNKNOWN)
13139 abbr = get_tv_number(&argvars[2]);
13140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141
Bram Moolenaar2c932302006-03-18 21:42:09 +000013142 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013143 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013145 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146}
13147
13148/*
13149 * "histadd()" function
13150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013152f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013153 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155{
13156#ifdef FEAT_CMDHIST
13157 int histype;
13158 char_u *str;
13159 char_u buf[NUMBUFLEN];
13160#endif
13161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 if (check_restricted() || check_secure())
13164 return;
13165#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013166 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13167 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168 if (histype >= 0)
13169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 if (*str != NUL)
13172 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013173 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013175 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 return;
13177 }
13178 }
13179#endif
13180}
13181
13182/*
13183 * "histdel()" function
13184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013187 typval_T *argvars UNUSED;
13188 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189{
13190#ifdef FEAT_CMDHIST
13191 int n;
13192 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013193 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013195 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13196 if (str == NULL)
13197 n = 0;
13198 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013200 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013201 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013203 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013204 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 else
13206 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013207 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013208 get_tv_string_buf(&argvars[1], buf));
13209 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210#endif
13211}
13212
13213/*
13214 * "histget()" function
13215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013217f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013218 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220{
13221#ifdef FEAT_CMDHIST
13222 int type;
13223 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013224 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013226 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13227 if (str == NULL)
13228 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013230 {
13231 type = get_histtype(str);
13232 if (argvars[1].v_type == VAR_UNKNOWN)
13233 idx = get_history_idx(type);
13234 else
13235 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13236 /* -1 on type error */
13237 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013242 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243}
13244
13245/*
13246 * "histnr()" function
13247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013250 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252{
13253 int i;
13254
13255#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013256 char_u *history = get_tv_string_chk(&argvars[0]);
13257
13258 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 if (i >= HIST_CMD && i < HIST_COUNT)
13260 i = get_history_idx(i);
13261 else
13262#endif
13263 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013264 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265}
13266
13267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268 * "highlightID(name)" function
13269 */
13270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013271f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013272 typval_T *argvars;
13273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013275 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276}
13277
13278/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013279 * "highlight_exists()" function
13280 */
13281 static void
13282f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013283 typval_T *argvars;
13284 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013285{
13286 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13287}
13288
13289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 * "hostname()" function
13291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013293f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013294 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296{
13297 char_u hostname[256];
13298
13299 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013300 rettv->v_type = VAR_STRING;
13301 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302}
13303
13304/*
13305 * iconv() function
13306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013308f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013309 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311{
13312#ifdef FEAT_MBYTE
13313 char_u buf1[NUMBUFLEN];
13314 char_u buf2[NUMBUFLEN];
13315 char_u *from, *to, *str;
13316 vimconv_T vimconv;
13317#endif
13318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013319 rettv->v_type = VAR_STRING;
13320 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321
13322#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013323 str = get_tv_string(&argvars[0]);
13324 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13325 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 vimconv.vc_type = CONV_NONE;
13327 convert_setup(&vimconv, from, to);
13328
13329 /* If the encodings are equal, no conversion needed. */
13330 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013331 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013333 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334
13335 convert_setup(&vimconv, NULL, NULL);
13336 vim_free(from);
13337 vim_free(to);
13338#endif
13339}
13340
13341/*
13342 * "indent()" function
13343 */
13344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013346 typval_T *argvars;
13347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348{
13349 linenr_T lnum;
13350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013351 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013353 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013355 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356}
13357
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013358/*
13359 * "index()" function
13360 */
13361 static void
13362f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013363 typval_T *argvars;
13364 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013365{
Bram Moolenaar33570922005-01-25 22:26:29 +000013366 list_T *l;
13367 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013368 long idx = 0;
13369 int ic = FALSE;
13370
13371 rettv->vval.v_number = -1;
13372 if (argvars[0].v_type != VAR_LIST)
13373 {
13374 EMSG(_(e_listreq));
13375 return;
13376 }
13377 l = argvars[0].vval.v_list;
13378 if (l != NULL)
13379 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013380 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013381 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013383 int error = FALSE;
13384
Bram Moolenaar758711c2005-02-02 23:11:38 +000013385 /* Start at specified item. Use the cached index that list_find()
13386 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013387 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013388 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013389 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013390 ic = get_tv_number_chk(&argvars[3], &error);
13391 if (error)
13392 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013393 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013394
Bram Moolenaar758711c2005-02-02 23:11:38 +000013395 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013396 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013397 {
13398 rettv->vval.v_number = idx;
13399 break;
13400 }
13401 }
13402}
13403
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404static int inputsecret_flag = 0;
13405
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013406static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13407
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013409 * This function is used by f_input() and f_inputdialog() functions. The third
13410 * argument to f_input() specifies the type of completion to use at the
13411 * prompt. The third argument to f_inputdialog() specifies the value to return
13412 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 */
13414 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013415get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013416 typval_T *argvars;
13417 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013418 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013420 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421 char_u *p = NULL;
13422 int c;
13423 char_u buf[NUMBUFLEN];
13424 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013425 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013426 int xp_type = EXPAND_NOTHING;
13427 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013430 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431
13432#ifdef NO_CONSOLE_INPUT
13433 /* While starting up, there is no place to enter text. */
13434 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436#endif
13437
13438 cmd_silent = FALSE; /* Want to see the prompt. */
13439 if (prompt != NULL)
13440 {
13441 /* Only the part of the message after the last NL is considered as
13442 * prompt for the command line */
13443 p = vim_strrchr(prompt, '\n');
13444 if (p == NULL)
13445 p = prompt;
13446 else
13447 {
13448 ++p;
13449 c = *p;
13450 *p = NUL;
13451 msg_start();
13452 msg_clr_eos();
13453 msg_puts_attr(prompt, echo_attr);
13454 msg_didout = FALSE;
13455 msg_starthere();
13456 *p = c;
13457 }
13458 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013460 if (argvars[1].v_type != VAR_UNKNOWN)
13461 {
13462 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13463 if (defstr != NULL)
13464 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013466 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013467 {
13468 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013469 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013470 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013471
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013472 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013473 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013474
Bram Moolenaar4463f292005-09-25 22:20:24 +000013475 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13476 if (xp_name == NULL)
13477 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013478
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013479 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013480
Bram Moolenaar4463f292005-09-25 22:20:24 +000013481 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13482 &xp_arg) == FAIL)
13483 return;
13484 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013485 }
13486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013487 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013488 {
13489# ifdef FEAT_EX_EXTRA
13490 int save_ex_normal_busy = ex_normal_busy;
13491 ex_normal_busy = 0;
13492# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013494 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13495 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013496# ifdef FEAT_EX_EXTRA
13497 ex_normal_busy = save_ex_normal_busy;
13498# endif
13499 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013500 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013501 && argvars[1].v_type != VAR_UNKNOWN
13502 && argvars[2].v_type != VAR_UNKNOWN)
13503 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13504 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013505
13506 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013508 /* since the user typed this, no need to wait for return */
13509 need_wait_return = FALSE;
13510 msg_didout = FALSE;
13511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 cmd_silent = cmd_silent_save;
13513}
13514
13515/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013516 * "input()" function
13517 * Also handles inputsecret() when inputsecret is set.
13518 */
13519 static void
13520f_input(argvars, rettv)
13521 typval_T *argvars;
13522 typval_T *rettv;
13523{
13524 get_user_input(argvars, rettv, FALSE);
13525}
13526
13527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 * "inputdialog()" function
13529 */
13530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013531f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013532 typval_T *argvars;
13533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534{
13535#if defined(FEAT_GUI_TEXTDIALOG)
13536 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13537 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13538 {
13539 char_u *message;
13540 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013541 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013543 message = get_tv_string_chk(&argvars[0]);
13544 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013545 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013546 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 else
13548 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013549 if (message != NULL && defstr != NULL
13550 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013551 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013552 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 else
13554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013555 if (message != NULL && defstr != NULL
13556 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013557 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013558 rettv->vval.v_string = vim_strsave(
13559 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013563 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 }
13565 else
13566#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013567 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568}
13569
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013570/*
13571 * "inputlist()" function
13572 */
13573 static void
13574f_inputlist(argvars, rettv)
13575 typval_T *argvars;
13576 typval_T *rettv;
13577{
13578 listitem_T *li;
13579 int selected;
13580 int mouse_used;
13581
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013582#ifdef NO_CONSOLE_INPUT
13583 /* While starting up, there is no place to enter text. */
13584 if (no_console_input())
13585 return;
13586#endif
13587 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13588 {
13589 EMSG2(_(e_listarg), "inputlist()");
13590 return;
13591 }
13592
13593 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013594 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013595 lines_left = Rows; /* avoid more prompt */
13596 msg_scroll = TRUE;
13597 msg_clr_eos();
13598
13599 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13600 {
13601 msg_puts(get_tv_string(&li->li_tv));
13602 msg_putchar('\n');
13603 }
13604
13605 /* Ask for choice. */
13606 selected = prompt_for_number(&mouse_used);
13607 if (mouse_used)
13608 selected -= lines_left;
13609
13610 rettv->vval.v_number = selected;
13611}
13612
13613
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13615
13616/*
13617 * "inputrestore()" function
13618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013621 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623{
13624 if (ga_userinput.ga_len > 0)
13625 {
13626 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13628 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013629 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 }
13631 else if (p_verbose > 1)
13632 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013633 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 }
13636}
13637
13638/*
13639 * "inputsave()" function
13640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013643 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013646 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 if (ga_grow(&ga_userinput, 1) == OK)
13648 {
13649 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13650 + ga_userinput.ga_len);
13651 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013652 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653 }
13654 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656}
13657
13658/*
13659 * "inputsecret()" function
13660 */
13661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013663 typval_T *argvars;
13664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665{
13666 ++cmdline_star;
13667 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 --cmdline_star;
13670 --inputsecret_flag;
13671}
13672
13673/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013674 * "insert()" function
13675 */
13676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013678 typval_T *argvars;
13679 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013680{
13681 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013682 listitem_T *item;
13683 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013684 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013685
13686 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013687 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013688 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013689 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013690 {
13691 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013692 before = get_tv_number_chk(&argvars[2], &error);
13693 if (error)
13694 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013695
Bram Moolenaar758711c2005-02-02 23:11:38 +000013696 if (before == l->lv_len)
13697 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013698 else
13699 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013700 item = list_find(l, before);
13701 if (item == NULL)
13702 {
13703 EMSGN(_(e_listidx), before);
13704 l = NULL;
13705 }
13706 }
13707 if (l != NULL)
13708 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013709 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013710 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013711 }
13712 }
13713}
13714
13715/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013716 * "invert(expr)" function
13717 */
13718 static void
13719f_invert(argvars, rettv)
13720 typval_T *argvars;
13721 typval_T *rettv;
13722{
13723 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13724}
13725
13726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 * "isdirectory()" function
13728 */
13729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013730f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013731 typval_T *argvars;
13732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013734 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735}
13736
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013737/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013738 * "islocked()" function
13739 */
13740 static void
13741f_islocked(argvars, rettv)
13742 typval_T *argvars;
13743 typval_T *rettv;
13744{
13745 lval_T lv;
13746 char_u *end;
13747 dictitem_T *di;
13748
13749 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013750 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13751 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013752 if (end != NULL && lv.ll_name != NULL)
13753 {
13754 if (*end != NUL)
13755 EMSG(_(e_trailing));
13756 else
13757 {
13758 if (lv.ll_tv == NULL)
13759 {
13760 if (check_changedtick(lv.ll_name))
13761 rettv->vval.v_number = 1; /* always locked */
13762 else
13763 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013764 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013765 if (di != NULL)
13766 {
13767 /* Consider a variable locked when:
13768 * 1. the variable itself is locked
13769 * 2. the value of the variable is locked.
13770 * 3. the List or Dict value is locked.
13771 */
13772 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13773 || tv_islocked(&di->di_tv));
13774 }
13775 }
13776 }
13777 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013778 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013779 else if (lv.ll_newkey != NULL)
13780 EMSG2(_(e_dictkey), lv.ll_newkey);
13781 else if (lv.ll_list != NULL)
13782 /* List item. */
13783 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13784 else
13785 /* Dictionary item. */
13786 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13787 }
13788 }
13789
13790 clear_lval(&lv);
13791}
13792
Bram Moolenaar33570922005-01-25 22:26:29 +000013793static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013794
13795/*
13796 * Turn a dict into a list:
13797 * "what" == 0: list of keys
13798 * "what" == 1: list of values
13799 * "what" == 2: list of items
13800 */
13801 static void
13802dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013803 typval_T *argvars;
13804 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013805 int what;
13806{
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 list_T *l2;
13808 dictitem_T *di;
13809 hashitem_T *hi;
13810 listitem_T *li;
13811 listitem_T *li2;
13812 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013813 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013814
Bram Moolenaar8c711452005-01-14 21:53:12 +000013815 if (argvars[0].v_type != VAR_DICT)
13816 {
13817 EMSG(_(e_dictreq));
13818 return;
13819 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013820 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013821 return;
13822
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013823 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013824 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013825
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013826 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013827 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013828 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013829 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013830 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013831 --todo;
13832 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013833
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013834 li = listitem_alloc();
13835 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013836 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013837 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013838
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013839 if (what == 0)
13840 {
13841 /* keys() */
13842 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013843 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013844 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13845 }
13846 else if (what == 1)
13847 {
13848 /* values() */
13849 copy_tv(&di->di_tv, &li->li_tv);
13850 }
13851 else
13852 {
13853 /* items() */
13854 l2 = list_alloc();
13855 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013856 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013857 li->li_tv.vval.v_list = l2;
13858 if (l2 == NULL)
13859 break;
13860 ++l2->lv_refcount;
13861
13862 li2 = listitem_alloc();
13863 if (li2 == NULL)
13864 break;
13865 list_append(l2, li2);
13866 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013867 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013868 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13869
13870 li2 = listitem_alloc();
13871 if (li2 == NULL)
13872 break;
13873 list_append(l2, li2);
13874 copy_tv(&di->di_tv, &li2->li_tv);
13875 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013876 }
13877 }
13878}
13879
13880/*
13881 * "items(dict)" function
13882 */
13883 static void
13884f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013885 typval_T *argvars;
13886 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013887{
13888 dict_list(argvars, rettv, 2);
13889}
13890
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013892 * "join()" function
13893 */
13894 static void
13895f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013896 typval_T *argvars;
13897 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013898{
13899 garray_T ga;
13900 char_u *sep;
13901
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013902 if (argvars[0].v_type != VAR_LIST)
13903 {
13904 EMSG(_(e_listreq));
13905 return;
13906 }
13907 if (argvars[0].vval.v_list == NULL)
13908 return;
13909 if (argvars[1].v_type == VAR_UNKNOWN)
13910 sep = (char_u *)" ";
13911 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013912 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013913
13914 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013915
13916 if (sep != NULL)
13917 {
13918 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013919 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013920 ga_append(&ga, NUL);
13921 rettv->vval.v_string = (char_u *)ga.ga_data;
13922 }
13923 else
13924 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013925}
13926
13927/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013928 * "keys()" function
13929 */
13930 static void
13931f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013932 typval_T *argvars;
13933 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013934{
13935 dict_list(argvars, rettv, 0);
13936}
13937
13938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 * "last_buffer_nr()" function.
13940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013942f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013943 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945{
13946 int n = 0;
13947 buf_T *buf;
13948
13949 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13950 if (n < buf->b_fnum)
13951 n = buf->b_fnum;
13952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013953 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013954}
13955
13956/*
13957 * "len()" function
13958 */
13959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013960f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013961 typval_T *argvars;
13962 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013963{
13964 switch (argvars[0].v_type)
13965 {
13966 case VAR_STRING:
13967 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013968 rettv->vval.v_number = (varnumber_T)STRLEN(
13969 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013970 break;
13971 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013972 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013973 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013974 case VAR_DICT:
13975 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13976 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013977 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013978 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013979 break;
13980 }
13981}
13982
Bram Moolenaar33570922005-01-25 22:26:29 +000013983static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013984
13985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013986libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013987 typval_T *argvars;
13988 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013989 int type;
13990{
13991#ifdef FEAT_LIBCALL
13992 char_u *string_in;
13993 char_u **string_result;
13994 int nr_result;
13995#endif
13996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013997 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013998 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013999 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014000
14001 if (check_restricted() || check_secure())
14002 return;
14003
14004#ifdef FEAT_LIBCALL
14005 /* The first two args must be strings, otherwise its meaningless */
14006 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14007 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014008 string_in = NULL;
14009 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014010 string_in = argvars[2].vval.v_string;
14011 if (type == VAR_NUMBER)
14012 string_result = NULL;
14013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014014 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014015 if (mch_libcall(argvars[0].vval.v_string,
14016 argvars[1].vval.v_string,
14017 string_in,
14018 argvars[2].vval.v_number,
14019 string_result,
14020 &nr_result) == OK
14021 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014022 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014023 }
14024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025}
14026
14027/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014028 * "libcall()" function
14029 */
14030 static void
14031f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014032 typval_T *argvars;
14033 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014034{
14035 libcall_common(argvars, rettv, VAR_STRING);
14036}
14037
14038/*
14039 * "libcallnr()" function
14040 */
14041 static void
14042f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014043 typval_T *argvars;
14044 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014045{
14046 libcall_common(argvars, rettv, VAR_NUMBER);
14047}
14048
14049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 * "line(string)" function
14051 */
14052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014053f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014054 typval_T *argvars;
14055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056{
14057 linenr_T lnum = 0;
14058 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014059 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014061 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 if (fp != NULL)
14063 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014064 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065}
14066
14067/*
14068 * "line2byte(lnum)" function
14069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014071f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014072 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074{
14075#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014076 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077#else
14078 linenr_T lnum;
14079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014080 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014084 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14085 if (rettv->vval.v_number >= 0)
14086 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087#endif
14088}
14089
14090/*
14091 * "lispindent(lnum)" function
14092 */
14093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014094f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014095 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097{
14098#ifdef FEAT_LISP
14099 pos_T pos;
14100 linenr_T lnum;
14101
14102 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014103 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14105 {
14106 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108 curwin->w_cursor = pos;
14109 }
14110 else
14111#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014112 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113}
14114
14115/*
14116 * "localtime()" function
14117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014119f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014120 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124}
14125
Bram Moolenaar33570922005-01-25 22:26:29 +000014126static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127
14128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014130 typval_T *argvars;
14131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 int exact;
14133{
14134 char_u *keys;
14135 char_u *which;
14136 char_u buf[NUMBUFLEN];
14137 char_u *keys_buf = NULL;
14138 char_u *rhs;
14139 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014140 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014141 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014142 mapblock_T *mp;
14143 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144
14145 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014146 rettv->v_type = VAR_STRING;
14147 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 if (*keys == NUL)
14151 return;
14152
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014153 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014155 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014156 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014157 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014158 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014159 if (argvars[3].v_type != VAR_UNKNOWN)
14160 get_dict = get_tv_number(&argvars[3]);
14161 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 else
14164 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014165 if (which == NULL)
14166 return;
14167
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 mode = get_map_mode(&which, 0);
14169
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014170 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014171 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014173
14174 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014176 /* Return a string. */
14177 if (rhs != NULL)
14178 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179
Bram Moolenaarbd743252010-10-20 21:23:33 +020014180 }
14181 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14182 {
14183 /* Return a dictionary. */
14184 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14185 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14186 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187
Bram Moolenaarbd743252010-10-20 21:23:33 +020014188 dict_add_nr_str(dict, "lhs", 0L, lhs);
14189 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14190 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14191 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14192 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14193 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14194 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014195 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014196 dict_add_nr_str(dict, "mode", 0L, mapmode);
14197
14198 vim_free(lhs);
14199 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 }
14201}
14202
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014203#ifdef FEAT_FLOAT
14204/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014205 * "log()" function
14206 */
14207 static void
14208f_log(argvars, rettv)
14209 typval_T *argvars;
14210 typval_T *rettv;
14211{
14212 float_T f;
14213
14214 rettv->v_type = VAR_FLOAT;
14215 if (get_float_arg(argvars, &f) == OK)
14216 rettv->vval.v_float = log(f);
14217 else
14218 rettv->vval.v_float = 0.0;
14219}
14220
14221/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014222 * "log10()" function
14223 */
14224 static void
14225f_log10(argvars, rettv)
14226 typval_T *argvars;
14227 typval_T *rettv;
14228{
14229 float_T f;
14230
14231 rettv->v_type = VAR_FLOAT;
14232 if (get_float_arg(argvars, &f) == OK)
14233 rettv->vval.v_float = log10(f);
14234 else
14235 rettv->vval.v_float = 0.0;
14236}
14237#endif
14238
Bram Moolenaar1dced572012-04-05 16:54:08 +020014239#ifdef FEAT_LUA
14240/*
14241 * "luaeval()" function
14242 */
14243 static void
14244f_luaeval(argvars, rettv)
14245 typval_T *argvars;
14246 typval_T *rettv;
14247{
14248 char_u *str;
14249 char_u buf[NUMBUFLEN];
14250
14251 str = get_tv_string_buf(&argvars[0], buf);
14252 do_luaeval(str, argvars + 1, rettv);
14253}
14254#endif
14255
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014257 * "map()" function
14258 */
14259 static void
14260f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014261 typval_T *argvars;
14262 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014263{
14264 filter_map(argvars, rettv, TRUE);
14265}
14266
14267/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014268 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 */
14270 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014271f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014272 typval_T *argvars;
14273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014275 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276}
14277
14278/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014279 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 */
14281 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014282f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014283 typval_T *argvars;
14284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014286 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287}
14288
Bram Moolenaar33570922005-01-25 22:26:29 +000014289static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290
14291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014292find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014293 typval_T *argvars;
14294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 int type;
14296{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014297 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014298 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014299 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 char_u *pat;
14301 regmatch_T regmatch;
14302 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014303 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 char_u *save_cpo;
14305 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014306 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014307 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014308 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014309 list_T *l = NULL;
14310 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014311 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014312 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313
14314 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14315 save_cpo = p_cpo;
14316 p_cpo = (char_u *)"";
14317
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014318 rettv->vval.v_number = -1;
14319 if (type == 3)
14320 {
14321 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014322 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014323 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014324 }
14325 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327 rettv->v_type = VAR_STRING;
14328 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014331 if (argvars[0].v_type == VAR_LIST)
14332 {
14333 if ((l = argvars[0].vval.v_list) == NULL)
14334 goto theend;
14335 li = l->lv_first;
14336 }
14337 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014338 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014339 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014340 len = (long)STRLEN(str);
14341 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14344 if (pat == NULL)
14345 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014346
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014347 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 int error = FALSE;
14350
14351 start = get_tv_number_chk(&argvars[2], &error);
14352 if (error)
14353 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014354 if (l != NULL)
14355 {
14356 li = list_find(l, start);
14357 if (li == NULL)
14358 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014359 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014360 }
14361 else
14362 {
14363 if (start < 0)
14364 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014365 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014366 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014367 /* When "count" argument is there ignore matches before "start",
14368 * otherwise skip part of the string. Differs when pattern is "^"
14369 * or "\<". */
14370 if (argvars[3].v_type != VAR_UNKNOWN)
14371 startcol = start;
14372 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014373 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014374 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014375 len -= start;
14376 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014377 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014378
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014379 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380 nth = get_tv_number_chk(&argvars[3], &error);
14381 if (error)
14382 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 }
14384
14385 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14386 if (regmatch.regprog != NULL)
14387 {
14388 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014389
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014390 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014391 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014392 if (l != NULL)
14393 {
14394 if (li == NULL)
14395 {
14396 match = FALSE;
14397 break;
14398 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014399 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014400 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014401 if (str == NULL)
14402 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014403 }
14404
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014405 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014406
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014407 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014408 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014409 if (l == NULL && !match)
14410 break;
14411
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014412 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014413 if (l != NULL)
14414 {
14415 li = li->li_next;
14416 ++idx;
14417 }
14418 else
14419 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014420#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014421 startcol = (colnr_T)(regmatch.startp[0]
14422 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014423#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014424 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014425#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014426 if (startcol > (colnr_T)len
14427 || str + startcol <= regmatch.startp[0])
14428 {
14429 match = FALSE;
14430 break;
14431 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014432 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014433 }
14434
14435 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014437 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014438 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014439 int i;
14440
14441 /* return list with matched string and submatches */
14442 for (i = 0; i < NSUBEXP; ++i)
14443 {
14444 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014445 {
14446 if (list_append_string(rettv->vval.v_list,
14447 (char_u *)"", 0) == FAIL)
14448 break;
14449 }
14450 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014451 regmatch.startp[i],
14452 (int)(regmatch.endp[i] - regmatch.startp[i]))
14453 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014454 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014455 }
14456 }
14457 else if (type == 2)
14458 {
14459 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014460 if (l != NULL)
14461 copy_tv(&li->li_tv, rettv);
14462 else
14463 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014465 }
14466 else if (l != NULL)
14467 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 else
14469 {
14470 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014471 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 (varnumber_T)(regmatch.startp[0] - str);
14473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014476 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 }
14478 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014479 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 }
14481
14482theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014483 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 p_cpo = save_cpo;
14485}
14486
14487/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014488 * "match()" function
14489 */
14490 static void
14491f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014492 typval_T *argvars;
14493 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014494{
14495 find_some_match(argvars, rettv, 1);
14496}
14497
14498/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014499 * "matchadd()" function
14500 */
14501 static void
14502f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014503 typval_T *argvars UNUSED;
14504 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014505{
14506#ifdef FEAT_SEARCH_EXTRA
14507 char_u buf[NUMBUFLEN];
14508 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14509 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14510 int prio = 10; /* default priority */
14511 int id = -1;
14512 int error = FALSE;
14513
14514 rettv->vval.v_number = -1;
14515
14516 if (grp == NULL || pat == NULL)
14517 return;
14518 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014519 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014520 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014521 if (argvars[3].v_type != VAR_UNKNOWN)
14522 id = get_tv_number_chk(&argvars[3], &error);
14523 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014524 if (error == TRUE)
14525 return;
14526 if (id >= 1 && id <= 3)
14527 {
14528 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14529 return;
14530 }
14531
Bram Moolenaarb3414592014-06-17 17:48:32 +020014532 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14533#endif
14534}
14535
14536/*
14537 * "matchaddpos()" function
14538 */
14539 static void
14540f_matchaddpos(argvars, rettv)
14541 typval_T *argvars UNUSED;
14542 typval_T *rettv UNUSED;
14543{
14544#ifdef FEAT_SEARCH_EXTRA
14545 char_u buf[NUMBUFLEN];
14546 char_u *group;
14547 int prio = 10;
14548 int id = -1;
14549 int error = FALSE;
14550 list_T *l;
14551
14552 rettv->vval.v_number = -1;
14553
14554 group = get_tv_string_buf_chk(&argvars[0], buf);
14555 if (group == NULL)
14556 return;
14557
14558 if (argvars[1].v_type != VAR_LIST)
14559 {
14560 EMSG2(_(e_listarg), "matchaddpos()");
14561 return;
14562 }
14563 l = argvars[1].vval.v_list;
14564 if (l == NULL)
14565 return;
14566
14567 if (argvars[2].v_type != VAR_UNKNOWN)
14568 {
14569 prio = get_tv_number_chk(&argvars[2], &error);
14570 if (argvars[3].v_type != VAR_UNKNOWN)
14571 id = get_tv_number_chk(&argvars[3], &error);
14572 }
14573 if (error == TRUE)
14574 return;
14575
14576 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14577 if (id == 1 || id == 2)
14578 {
14579 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14580 return;
14581 }
14582
14583 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014584#endif
14585}
14586
14587/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014588 * "matcharg()" function
14589 */
14590 static void
14591f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014592 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014593 typval_T *rettv;
14594{
14595 if (rettv_list_alloc(rettv) == OK)
14596 {
14597#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014598 int id = get_tv_number(&argvars[0]);
14599 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014600
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014601 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014602 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014603 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14604 {
14605 list_append_string(rettv->vval.v_list,
14606 syn_id2name(m->hlg_id), -1);
14607 list_append_string(rettv->vval.v_list, m->pattern, -1);
14608 }
14609 else
14610 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014611 list_append_string(rettv->vval.v_list, NULL, -1);
14612 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014613 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014614 }
14615#endif
14616 }
14617}
14618
14619/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014620 * "matchdelete()" function
14621 */
14622 static void
14623f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014624 typval_T *argvars UNUSED;
14625 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014626{
14627#ifdef FEAT_SEARCH_EXTRA
14628 rettv->vval.v_number = match_delete(curwin,
14629 (int)get_tv_number(&argvars[0]), TRUE);
14630#endif
14631}
14632
14633/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634 * "matchend()" function
14635 */
14636 static void
14637f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014638 typval_T *argvars;
14639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640{
14641 find_some_match(argvars, rettv, 0);
14642}
14643
14644/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014645 * "matchlist()" function
14646 */
14647 static void
14648f_matchlist(argvars, rettv)
14649 typval_T *argvars;
14650 typval_T *rettv;
14651{
14652 find_some_match(argvars, rettv, 3);
14653}
14654
14655/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014656 * "matchstr()" function
14657 */
14658 static void
14659f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014660 typval_T *argvars;
14661 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014662{
14663 find_some_match(argvars, rettv, 2);
14664}
14665
Bram Moolenaar33570922005-01-25 22:26:29 +000014666static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014667
14668 static void
14669max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014670 typval_T *argvars;
14671 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014672 int domax;
14673{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014674 long n = 0;
14675 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014676 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014677
14678 if (argvars[0].v_type == VAR_LIST)
14679 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014680 list_T *l;
14681 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014682
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014683 l = argvars[0].vval.v_list;
14684 if (l != NULL)
14685 {
14686 li = l->lv_first;
14687 if (li != NULL)
14688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014689 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014690 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014691 {
14692 li = li->li_next;
14693 if (li == NULL)
14694 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014695 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014696 if (domax ? i > n : i < n)
14697 n = i;
14698 }
14699 }
14700 }
14701 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014702 else if (argvars[0].v_type == VAR_DICT)
14703 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014704 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014705 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014706 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014707 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014708
14709 d = argvars[0].vval.v_dict;
14710 if (d != NULL)
14711 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014712 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014713 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014714 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014715 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014716 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014717 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014718 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014719 if (first)
14720 {
14721 n = i;
14722 first = FALSE;
14723 }
14724 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014725 n = i;
14726 }
14727 }
14728 }
14729 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014730 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014731 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014732 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014733}
14734
14735/*
14736 * "max()" function
14737 */
14738 static void
14739f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014740 typval_T *argvars;
14741 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014742{
14743 max_min(argvars, rettv, TRUE);
14744}
14745
14746/*
14747 * "min()" function
14748 */
14749 static void
14750f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014751 typval_T *argvars;
14752 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014753{
14754 max_min(argvars, rettv, FALSE);
14755}
14756
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014757static int mkdir_recurse __ARGS((char_u *dir, int prot));
14758
14759/*
14760 * Create the directory in which "dir" is located, and higher levels when
14761 * needed.
14762 */
14763 static int
14764mkdir_recurse(dir, prot)
14765 char_u *dir;
14766 int prot;
14767{
14768 char_u *p;
14769 char_u *updir;
14770 int r = FAIL;
14771
14772 /* Get end of directory name in "dir".
14773 * We're done when it's "/" or "c:/". */
14774 p = gettail_sep(dir);
14775 if (p <= get_past_head(dir))
14776 return OK;
14777
14778 /* If the directory exists we're done. Otherwise: create it.*/
14779 updir = vim_strnsave(dir, (int)(p - dir));
14780 if (updir == NULL)
14781 return FAIL;
14782 if (mch_isdir(updir))
14783 r = OK;
14784 else if (mkdir_recurse(updir, prot) == OK)
14785 r = vim_mkdir_emsg(updir, prot);
14786 vim_free(updir);
14787 return r;
14788}
14789
14790#ifdef vim_mkdir
14791/*
14792 * "mkdir()" function
14793 */
14794 static void
14795f_mkdir(argvars, rettv)
14796 typval_T *argvars;
14797 typval_T *rettv;
14798{
14799 char_u *dir;
14800 char_u buf[NUMBUFLEN];
14801 int prot = 0755;
14802
14803 rettv->vval.v_number = FAIL;
14804 if (check_restricted() || check_secure())
14805 return;
14806
14807 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014808 if (*dir == NUL)
14809 rettv->vval.v_number = FAIL;
14810 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014811 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014812 if (*gettail(dir) == NUL)
14813 /* remove trailing slashes */
14814 *gettail_sep(dir) = NUL;
14815
14816 if (argvars[1].v_type != VAR_UNKNOWN)
14817 {
14818 if (argvars[2].v_type != VAR_UNKNOWN)
14819 prot = get_tv_number_chk(&argvars[2], NULL);
14820 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14821 mkdir_recurse(dir, prot);
14822 }
14823 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014824 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014825}
14826#endif
14827
Bram Moolenaar0d660222005-01-07 21:51:51 +000014828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829 * "mode()" function
14830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014832f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014833 typval_T *argvars;
14834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014836 char_u buf[3];
14837
14838 buf[1] = NUL;
14839 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 if (VIsual_active)
14842 {
14843 if (VIsual_select)
14844 buf[0] = VIsual_mode + 's' - 'v';
14845 else
14846 buf[0] = VIsual_mode;
14847 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014848 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014849 || State == CONFIRM)
14850 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014852 if (State == ASKMORE)
14853 buf[1] = 'm';
14854 else if (State == CONFIRM)
14855 buf[1] = '?';
14856 }
14857 else if (State == EXTERNCMD)
14858 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859 else if (State & INSERT)
14860 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014861#ifdef FEAT_VREPLACE
14862 if (State & VREPLACE_FLAG)
14863 {
14864 buf[0] = 'R';
14865 buf[1] = 'v';
14866 }
14867 else
14868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869 if (State & REPLACE_FLAG)
14870 buf[0] = 'R';
14871 else
14872 buf[0] = 'i';
14873 }
14874 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014877 if (exmode_active)
14878 buf[1] = 'v';
14879 }
14880 else if (exmode_active)
14881 {
14882 buf[0] = 'c';
14883 buf[1] = 'e';
14884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014888 if (finish_op)
14889 buf[1] = 'o';
14890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014892 /* Clear out the minor mode when the argument is not a non-zero number or
14893 * non-empty string. */
14894 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014895 buf[1] = NUL;
14896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014897 rettv->vval.v_string = vim_strsave(buf);
14898 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899}
14900
Bram Moolenaar429fa852013-04-15 12:27:36 +020014901#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014902/*
14903 * "mzeval()" function
14904 */
14905 static void
14906f_mzeval(argvars, rettv)
14907 typval_T *argvars;
14908 typval_T *rettv;
14909{
14910 char_u *str;
14911 char_u buf[NUMBUFLEN];
14912
14913 str = get_tv_string_buf(&argvars[0], buf);
14914 do_mzeval(str, rettv);
14915}
Bram Moolenaar75676462013-01-30 14:55:42 +010014916
14917 void
14918mzscheme_call_vim(name, args, rettv)
14919 char_u *name;
14920 typval_T *args;
14921 typval_T *rettv;
14922{
14923 typval_T argvars[3];
14924
14925 argvars[0].v_type = VAR_STRING;
14926 argvars[0].vval.v_string = name;
14927 copy_tv(args, &argvars[1]);
14928 argvars[2].v_type = VAR_UNKNOWN;
14929 f_call(argvars, rettv);
14930 clear_tv(&argvars[1]);
14931}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014932#endif
14933
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014935 * "nextnonblank()" function
14936 */
14937 static void
14938f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014939 typval_T *argvars;
14940 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014941{
14942 linenr_T lnum;
14943
14944 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014946 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014947 {
14948 lnum = 0;
14949 break;
14950 }
14951 if (*skipwhite(ml_get(lnum)) != NUL)
14952 break;
14953 }
14954 rettv->vval.v_number = lnum;
14955}
14956
14957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958 * "nr2char()" function
14959 */
14960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014962 typval_T *argvars;
14963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964{
14965 char_u buf[NUMBUFLEN];
14966
14967#ifdef FEAT_MBYTE
14968 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014969 {
14970 int utf8 = 0;
14971
14972 if (argvars[1].v_type != VAR_UNKNOWN)
14973 utf8 = get_tv_number_chk(&argvars[1], NULL);
14974 if (utf8)
14975 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14976 else
14977 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 else
14980#endif
14981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014982 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 buf[1] = NUL;
14984 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014985 rettv->v_type = VAR_STRING;
14986 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014987}
14988
14989/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014990 * "or(expr, expr)" function
14991 */
14992 static void
14993f_or(argvars, rettv)
14994 typval_T *argvars;
14995 typval_T *rettv;
14996{
14997 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14998 | get_tv_number_chk(&argvars[1], NULL);
14999}
15000
15001/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015002 * "pathshorten()" function
15003 */
15004 static void
15005f_pathshorten(argvars, rettv)
15006 typval_T *argvars;
15007 typval_T *rettv;
15008{
15009 char_u *p;
15010
15011 rettv->v_type = VAR_STRING;
15012 p = get_tv_string_chk(&argvars[0]);
15013 if (p == NULL)
15014 rettv->vval.v_string = NULL;
15015 else
15016 {
15017 p = vim_strsave(p);
15018 rettv->vval.v_string = p;
15019 if (p != NULL)
15020 shorten_dir(p);
15021 }
15022}
15023
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015024#ifdef FEAT_FLOAT
15025/*
15026 * "pow()" function
15027 */
15028 static void
15029f_pow(argvars, rettv)
15030 typval_T *argvars;
15031 typval_T *rettv;
15032{
15033 float_T fx, fy;
15034
15035 rettv->v_type = VAR_FLOAT;
15036 if (get_float_arg(argvars, &fx) == OK
15037 && get_float_arg(&argvars[1], &fy) == OK)
15038 rettv->vval.v_float = pow(fx, fy);
15039 else
15040 rettv->vval.v_float = 0.0;
15041}
15042#endif
15043
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015044/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015045 * "prevnonblank()" function
15046 */
15047 static void
15048f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015049 typval_T *argvars;
15050 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051{
15052 linenr_T lnum;
15053
15054 lnum = get_tv_lnum(argvars);
15055 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15056 lnum = 0;
15057 else
15058 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15059 --lnum;
15060 rettv->vval.v_number = lnum;
15061}
15062
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015063#ifdef HAVE_STDARG_H
15064/* This dummy va_list is here because:
15065 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15066 * - locally in the function results in a "used before set" warning
15067 * - using va_start() to initialize it gives "function with fixed args" error */
15068static va_list ap;
15069#endif
15070
Bram Moolenaar8c711452005-01-14 21:53:12 +000015071/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015072 * "printf()" function
15073 */
15074 static void
15075f_printf(argvars, rettv)
15076 typval_T *argvars;
15077 typval_T *rettv;
15078{
15079 rettv->v_type = VAR_STRING;
15080 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015081#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015082 {
15083 char_u buf[NUMBUFLEN];
15084 int len;
15085 char_u *s;
15086 int saved_did_emsg = did_emsg;
15087 char *fmt;
15088
15089 /* Get the required length, allocate the buffer and do it for real. */
15090 did_emsg = FALSE;
15091 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015092 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015093 if (!did_emsg)
15094 {
15095 s = alloc(len + 1);
15096 if (s != NULL)
15097 {
15098 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015099 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015100 }
15101 }
15102 did_emsg |= saved_did_emsg;
15103 }
15104#endif
15105}
15106
15107/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015108 * "pumvisible()" function
15109 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015110 static void
15111f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015112 typval_T *argvars UNUSED;
15113 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015114{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015115#ifdef FEAT_INS_EXPAND
15116 if (pum_visible())
15117 rettv->vval.v_number = 1;
15118#endif
15119}
15120
Bram Moolenaardb913952012-06-29 12:54:53 +020015121#ifdef FEAT_PYTHON3
15122/*
15123 * "py3eval()" function
15124 */
15125 static void
15126f_py3eval(argvars, rettv)
15127 typval_T *argvars;
15128 typval_T *rettv;
15129{
15130 char_u *str;
15131 char_u buf[NUMBUFLEN];
15132
15133 str = get_tv_string_buf(&argvars[0], buf);
15134 do_py3eval(str, rettv);
15135}
15136#endif
15137
15138#ifdef FEAT_PYTHON
15139/*
15140 * "pyeval()" function
15141 */
15142 static void
15143f_pyeval(argvars, rettv)
15144 typval_T *argvars;
15145 typval_T *rettv;
15146{
15147 char_u *str;
15148 char_u buf[NUMBUFLEN];
15149
15150 str = get_tv_string_buf(&argvars[0], buf);
15151 do_pyeval(str, rettv);
15152}
15153#endif
15154
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015155/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015156 * "range()" function
15157 */
15158 static void
15159f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015160 typval_T *argvars;
15161 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015162{
15163 long start;
15164 long end;
15165 long stride = 1;
15166 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015167 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015169 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015170 if (argvars[1].v_type == VAR_UNKNOWN)
15171 {
15172 end = start - 1;
15173 start = 0;
15174 }
15175 else
15176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015177 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015178 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015179 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015180 }
15181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015182 if (error)
15183 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015184 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015185 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015186 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015187 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015188 else
15189 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015190 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015191 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015192 if (list_append_number(rettv->vval.v_list,
15193 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015194 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015195 }
15196}
15197
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015198/*
15199 * "readfile()" function
15200 */
15201 static void
15202f_readfile(argvars, rettv)
15203 typval_T *argvars;
15204 typval_T *rettv;
15205{
15206 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015207 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015208 char_u *fname;
15209 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015210 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15211 int io_size = sizeof(buf);
15212 int readlen; /* size of last fread() */
15213 char_u *prev = NULL; /* previously read bytes, if any */
15214 long prevlen = 0; /* length of data in prev */
15215 long prevsize = 0; /* size of prev buffer */
15216 long maxline = MAXLNUM;
15217 long cnt = 0;
15218 char_u *p; /* position in buf */
15219 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015221 if (argvars[1].v_type != VAR_UNKNOWN)
15222 {
15223 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15224 binary = TRUE;
15225 if (argvars[2].v_type != VAR_UNKNOWN)
15226 maxline = get_tv_number(&argvars[2]);
15227 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015228
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015229 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015230 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015231
15232 /* Always open the file in binary mode, library functions have a mind of
15233 * their own about CR-LF conversion. */
15234 fname = get_tv_string(&argvars[0]);
15235 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15236 {
15237 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15238 return;
15239 }
15240
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015241 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015242 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015243 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015244
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015245 /* This for loop processes what was read, but is also entered at end
15246 * of file so that either:
15247 * - an incomplete line gets written
15248 * - a "binary" file gets an empty line at the end if it ends in a
15249 * newline. */
15250 for (p = buf, start = buf;
15251 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15252 ++p)
15253 {
15254 if (*p == '\n' || readlen <= 0)
15255 {
15256 listitem_T *li;
15257 char_u *s = NULL;
15258 long_u len = p - start;
15259
15260 /* Finished a line. Remove CRs before NL. */
15261 if (readlen > 0 && !binary)
15262 {
15263 while (len > 0 && start[len - 1] == '\r')
15264 --len;
15265 /* removal may cross back to the "prev" string */
15266 if (len == 0)
15267 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15268 --prevlen;
15269 }
15270 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015271 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015272 else
15273 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015274 /* Change "prev" buffer to be the right size. This way
15275 * the bytes are only copied once, and very long lines are
15276 * allocated only once. */
15277 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015278 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015279 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015280 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015281 prev = NULL; /* the list will own the string */
15282 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015283 }
15284 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015285 if (s == NULL)
15286 {
15287 do_outofmem_msg((long_u) prevlen + len + 1);
15288 failed = TRUE;
15289 break;
15290 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015291
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015292 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015293 {
15294 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015295 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015296 break;
15297 }
15298 li->li_tv.v_type = VAR_STRING;
15299 li->li_tv.v_lock = 0;
15300 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015301 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015302
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015303 start = p + 1; /* step over newline */
15304 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015305 break;
15306 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015307 else if (*p == NUL)
15308 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015309#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015310 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15311 * when finding the BF and check the previous two bytes. */
15312 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015313 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015314 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15315 * + 1, these may be in the "prev" string. */
15316 char_u back1 = p >= buf + 1 ? p[-1]
15317 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15318 char_u back2 = p >= buf + 2 ? p[-2]
15319 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15320 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015321
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015322 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015323 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015324 char_u *dest = p - 2;
15325
15326 /* Usually a BOM is at the beginning of a file, and so at
15327 * the beginning of a line; then we can just step over it.
15328 */
15329 if (start == dest)
15330 start = p + 1;
15331 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015332 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015333 /* have to shuffle buf to close gap */
15334 int adjust_prevlen = 0;
15335
15336 if (dest < buf)
15337 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015338 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015339 dest = buf;
15340 }
15341 if (readlen > p - buf + 1)
15342 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15343 readlen -= 3 - adjust_prevlen;
15344 prevlen -= adjust_prevlen;
15345 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015346 }
15347 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015348 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015349#endif
15350 } /* for */
15351
15352 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15353 break;
15354 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015355 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015356 /* There's part of a line in buf, store it in "prev". */
15357 if (p - start + prevlen >= prevsize)
15358 {
15359 /* need bigger "prev" buffer */
15360 char_u *newprev;
15361
15362 /* A common use case is ordinary text files and "prev" gets a
15363 * fragment of a line, so the first allocation is made
15364 * small, to avoid repeatedly 'allocing' large and
15365 * 'reallocing' small. */
15366 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015367 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015368 else
15369 {
15370 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015371 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015372 prevsize = grow50pc > growmin ? grow50pc : growmin;
15373 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015374 newprev = prev == NULL ? alloc(prevsize)
15375 : vim_realloc(prev, prevsize);
15376 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015377 {
15378 do_outofmem_msg((long_u)prevsize);
15379 failed = TRUE;
15380 break;
15381 }
15382 prev = newprev;
15383 }
15384 /* Add the line part to end of "prev". */
15385 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015386 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015387 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015388 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015389
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015390 /*
15391 * For a negative line count use only the lines at the end of the file,
15392 * free the rest.
15393 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015394 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015395 while (cnt > -maxline)
15396 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015397 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015398 --cnt;
15399 }
15400
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015401 if (failed)
15402 {
15403 list_free(rettv->vval.v_list, TRUE);
15404 /* readfile doc says an empty list is returned on error */
15405 rettv->vval.v_list = list_alloc();
15406 }
15407
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015408 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015409 fclose(fd);
15410}
15411
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015412#if defined(FEAT_RELTIME)
15413static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15414
15415/*
15416 * Convert a List to proftime_T.
15417 * Return FAIL when there is something wrong.
15418 */
15419 static int
15420list2proftime(arg, tm)
15421 typval_T *arg;
15422 proftime_T *tm;
15423{
15424 long n1, n2;
15425 int error = FALSE;
15426
15427 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15428 || arg->vval.v_list->lv_len != 2)
15429 return FAIL;
15430 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15431 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15432# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015433 tm->HighPart = n1;
15434 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015435# else
15436 tm->tv_sec = n1;
15437 tm->tv_usec = n2;
15438# endif
15439 return error ? FAIL : OK;
15440}
15441#endif /* FEAT_RELTIME */
15442
15443/*
15444 * "reltime()" function
15445 */
15446 static void
15447f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015448 typval_T *argvars UNUSED;
15449 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015450{
15451#ifdef FEAT_RELTIME
15452 proftime_T res;
15453 proftime_T start;
15454
15455 if (argvars[0].v_type == VAR_UNKNOWN)
15456 {
15457 /* No arguments: get current time. */
15458 profile_start(&res);
15459 }
15460 else if (argvars[1].v_type == VAR_UNKNOWN)
15461 {
15462 if (list2proftime(&argvars[0], &res) == FAIL)
15463 return;
15464 profile_end(&res);
15465 }
15466 else
15467 {
15468 /* Two arguments: compute the difference. */
15469 if (list2proftime(&argvars[0], &start) == FAIL
15470 || list2proftime(&argvars[1], &res) == FAIL)
15471 return;
15472 profile_sub(&res, &start);
15473 }
15474
15475 if (rettv_list_alloc(rettv) == OK)
15476 {
15477 long n1, n2;
15478
15479# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015480 n1 = res.HighPart;
15481 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015482# else
15483 n1 = res.tv_sec;
15484 n2 = res.tv_usec;
15485# endif
15486 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15487 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15488 }
15489#endif
15490}
15491
15492/*
15493 * "reltimestr()" function
15494 */
15495 static void
15496f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015497 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015498 typval_T *rettv;
15499{
15500#ifdef FEAT_RELTIME
15501 proftime_T tm;
15502#endif
15503
15504 rettv->v_type = VAR_STRING;
15505 rettv->vval.v_string = NULL;
15506#ifdef FEAT_RELTIME
15507 if (list2proftime(&argvars[0], &tm) == OK)
15508 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15509#endif
15510}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015511
Bram Moolenaar0d660222005-01-07 21:51:51 +000015512#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15513static void make_connection __ARGS((void));
15514static int check_connection __ARGS((void));
15515
15516 static void
15517make_connection()
15518{
15519 if (X_DISPLAY == NULL
15520# ifdef FEAT_GUI
15521 && !gui.in_use
15522# endif
15523 )
15524 {
15525 x_force_connect = TRUE;
15526 setup_term_clip();
15527 x_force_connect = FALSE;
15528 }
15529}
15530
15531 static int
15532check_connection()
15533{
15534 make_connection();
15535 if (X_DISPLAY == NULL)
15536 {
15537 EMSG(_("E240: No connection to Vim server"));
15538 return FAIL;
15539 }
15540 return OK;
15541}
15542#endif
15543
15544#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015545static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015546
15547 static void
15548remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015549 typval_T *argvars;
15550 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015551 int expr;
15552{
15553 char_u *server_name;
15554 char_u *keys;
15555 char_u *r = NULL;
15556 char_u buf[NUMBUFLEN];
15557# ifdef WIN32
15558 HWND w;
15559# else
15560 Window w;
15561# endif
15562
15563 if (check_restricted() || check_secure())
15564 return;
15565
15566# ifdef FEAT_X11
15567 if (check_connection() == FAIL)
15568 return;
15569# endif
15570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015571 server_name = get_tv_string_chk(&argvars[0]);
15572 if (server_name == NULL)
15573 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015574 keys = get_tv_string_buf(&argvars[1], buf);
15575# ifdef WIN32
15576 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15577# else
15578 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15579 < 0)
15580# endif
15581 {
15582 if (r != NULL)
15583 EMSG(r); /* sending worked but evaluation failed */
15584 else
15585 EMSG2(_("E241: Unable to send to %s"), server_name);
15586 return;
15587 }
15588
15589 rettv->vval.v_string = r;
15590
15591 if (argvars[2].v_type != VAR_UNKNOWN)
15592 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015593 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015594 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015595 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015596
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015597 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015598 v.di_tv.v_type = VAR_STRING;
15599 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015600 idvar = get_tv_string_chk(&argvars[2]);
15601 if (idvar != NULL)
15602 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015603 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015604 }
15605}
15606#endif
15607
15608/*
15609 * "remote_expr()" function
15610 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015611 static void
15612f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015613 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015614 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015615{
15616 rettv->v_type = VAR_STRING;
15617 rettv->vval.v_string = NULL;
15618#ifdef FEAT_CLIENTSERVER
15619 remote_common(argvars, rettv, TRUE);
15620#endif
15621}
15622
15623/*
15624 * "remote_foreground()" function
15625 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015626 static void
15627f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015628 typval_T *argvars UNUSED;
15629 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015630{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015631#ifdef FEAT_CLIENTSERVER
15632# ifdef WIN32
15633 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015634 {
15635 char_u *server_name = get_tv_string_chk(&argvars[0]);
15636
15637 if (server_name != NULL)
15638 serverForeground(server_name);
15639 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640# else
15641 /* Send a foreground() expression to the server. */
15642 argvars[1].v_type = VAR_STRING;
15643 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15644 argvars[2].v_type = VAR_UNKNOWN;
15645 remote_common(argvars, rettv, TRUE);
15646 vim_free(argvars[1].vval.v_string);
15647# endif
15648#endif
15649}
15650
Bram Moolenaar0d660222005-01-07 21:51:51 +000015651 static void
15652f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015653 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015654 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015655{
15656#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015657 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015658 char_u *s = NULL;
15659# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015660 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015661# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015662 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015663
15664 if (check_restricted() || check_secure())
15665 {
15666 rettv->vval.v_number = -1;
15667 return;
15668 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015669 serverid = get_tv_string_chk(&argvars[0]);
15670 if (serverid == NULL)
15671 {
15672 rettv->vval.v_number = -1;
15673 return; /* type error; errmsg already given */
15674 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015675# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015676 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677 if (n == 0)
15678 rettv->vval.v_number = -1;
15679 else
15680 {
15681 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15682 rettv->vval.v_number = (s != NULL);
15683 }
15684# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015685 if (check_connection() == FAIL)
15686 return;
15687
15688 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015689 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015690# endif
15691
15692 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015694 char_u *retvar;
15695
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 v.di_tv.v_type = VAR_STRING;
15697 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015698 retvar = get_tv_string_chk(&argvars[1]);
15699 if (retvar != NULL)
15700 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015701 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015702 }
15703#else
15704 rettv->vval.v_number = -1;
15705#endif
15706}
15707
Bram Moolenaar0d660222005-01-07 21:51:51 +000015708 static void
15709f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015710 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015711 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015712{
15713 char_u *r = NULL;
15714
15715#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015716 char_u *serverid = get_tv_string_chk(&argvars[0]);
15717
15718 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015719 {
15720# ifdef WIN32
15721 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015722 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015723
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015724 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015725 if (n != 0)
15726 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15727 if (r == NULL)
15728# else
15729 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015730 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731# endif
15732 EMSG(_("E277: Unable to read a server reply"));
15733 }
15734#endif
15735 rettv->v_type = VAR_STRING;
15736 rettv->vval.v_string = r;
15737}
15738
15739/*
15740 * "remote_send()" function
15741 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015742 static void
15743f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015744 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015745 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015746{
15747 rettv->v_type = VAR_STRING;
15748 rettv->vval.v_string = NULL;
15749#ifdef FEAT_CLIENTSERVER
15750 remote_common(argvars, rettv, FALSE);
15751#endif
15752}
15753
15754/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015755 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015756 */
15757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015758f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015759 typval_T *argvars;
15760 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015761{
Bram Moolenaar33570922005-01-25 22:26:29 +000015762 list_T *l;
15763 listitem_T *item, *item2;
15764 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015765 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015766 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015767 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015768 dict_T *d;
15769 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015770 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015771
Bram Moolenaar8c711452005-01-14 21:53:12 +000015772 if (argvars[0].v_type == VAR_DICT)
15773 {
15774 if (argvars[2].v_type != VAR_UNKNOWN)
15775 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015776 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015777 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015779 key = get_tv_string_chk(&argvars[1]);
15780 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015782 di = dict_find(d, key, -1);
15783 if (di == NULL)
15784 EMSG2(_(e_dictkey), key);
15785 else
15786 {
15787 *rettv = di->di_tv;
15788 init_tv(&di->di_tv);
15789 dictitem_remove(d, di);
15790 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015792 }
15793 }
15794 else if (argvars[0].v_type != VAR_LIST)
15795 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015796 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015797 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015799 int error = FALSE;
15800
15801 idx = get_tv_number_chk(&argvars[1], &error);
15802 if (error)
15803 ; /* type error: do nothing, errmsg already given */
15804 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015805 EMSGN(_(e_listidx), idx);
15806 else
15807 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015808 if (argvars[2].v_type == VAR_UNKNOWN)
15809 {
15810 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015811 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015812 *rettv = item->li_tv;
15813 vim_free(item);
15814 }
15815 else
15816 {
15817 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015818 end = get_tv_number_chk(&argvars[2], &error);
15819 if (error)
15820 ; /* type error: do nothing */
15821 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015822 EMSGN(_(e_listidx), end);
15823 else
15824 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015825 int cnt = 0;
15826
15827 for (li = item; li != NULL; li = li->li_next)
15828 {
15829 ++cnt;
15830 if (li == item2)
15831 break;
15832 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015833 if (li == NULL) /* didn't find "item2" after "item" */
15834 EMSG(_(e_invrange));
15835 else
15836 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015837 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015838 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015839 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015840 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015841 l->lv_first = item;
15842 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015843 item->li_prev = NULL;
15844 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015845 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015846 }
15847 }
15848 }
15849 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015850 }
15851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852}
15853
15854/*
15855 * "rename({from}, {to})" function
15856 */
15857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015858f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015859 typval_T *argvars;
15860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861{
15862 char_u buf[NUMBUFLEN];
15863
15864 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015865 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015867 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15868 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869}
15870
15871/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015872 * "repeat()" function
15873 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015874 static void
15875f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015876 typval_T *argvars;
15877 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015878{
15879 char_u *p;
15880 int n;
15881 int slen;
15882 int len;
15883 char_u *r;
15884 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015885
15886 n = get_tv_number(&argvars[1]);
15887 if (argvars[0].v_type == VAR_LIST)
15888 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015889 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015890 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015891 if (list_extend(rettv->vval.v_list,
15892 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015893 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015894 }
15895 else
15896 {
15897 p = get_tv_string(&argvars[0]);
15898 rettv->v_type = VAR_STRING;
15899 rettv->vval.v_string = NULL;
15900
15901 slen = (int)STRLEN(p);
15902 len = slen * n;
15903 if (len <= 0)
15904 return;
15905
15906 r = alloc(len + 1);
15907 if (r != NULL)
15908 {
15909 for (i = 0; i < n; i++)
15910 mch_memmove(r + i * slen, p, (size_t)slen);
15911 r[len] = NUL;
15912 }
15913
15914 rettv->vval.v_string = r;
15915 }
15916}
15917
15918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919 * "resolve()" function
15920 */
15921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015922f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015923 typval_T *argvars;
15924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925{
15926 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015927#ifdef HAVE_READLINK
15928 char_u *buf = NULL;
15929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015931 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932#ifdef FEAT_SHORTCUT
15933 {
15934 char_u *v = NULL;
15935
15936 v = mch_resolve_shortcut(p);
15937 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015938 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015940 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941 }
15942#else
15943# ifdef HAVE_READLINK
15944 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945 char_u *cpy;
15946 int len;
15947 char_u *remain = NULL;
15948 char_u *q;
15949 int is_relative_to_current = FALSE;
15950 int has_trailing_pathsep = FALSE;
15951 int limit = 100;
15952
15953 p = vim_strsave(p);
15954
15955 if (p[0] == '.' && (vim_ispathsep(p[1])
15956 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15957 is_relative_to_current = TRUE;
15958
15959 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015960 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015961 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015962 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015963 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965
15966 q = getnextcomp(p);
15967 if (*q != NUL)
15968 {
15969 /* Separate the first path component in "p", and keep the
15970 * remainder (beginning with the path separator). */
15971 remain = vim_strsave(q - 1);
15972 q[-1] = NUL;
15973 }
15974
Bram Moolenaard9462e32011-04-11 21:35:11 +020015975 buf = alloc(MAXPATHL + 1);
15976 if (buf == NULL)
15977 goto fail;
15978
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979 for (;;)
15980 {
15981 for (;;)
15982 {
15983 len = readlink((char *)p, (char *)buf, MAXPATHL);
15984 if (len <= 0)
15985 break;
15986 buf[len] = NUL;
15987
15988 if (limit-- == 0)
15989 {
15990 vim_free(p);
15991 vim_free(remain);
15992 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015993 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 goto fail;
15995 }
15996
15997 /* Ensure that the result will have a trailing path separator
15998 * if the argument has one. */
15999 if (remain == NULL && has_trailing_pathsep)
16000 add_pathsep(buf);
16001
16002 /* Separate the first path component in the link value and
16003 * concatenate the remainders. */
16004 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16005 if (*q != NUL)
16006 {
16007 if (remain == NULL)
16008 remain = vim_strsave(q - 1);
16009 else
16010 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016011 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 if (cpy != NULL)
16013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014 vim_free(remain);
16015 remain = cpy;
16016 }
16017 }
16018 q[-1] = NUL;
16019 }
16020
16021 q = gettail(p);
16022 if (q > p && *q == NUL)
16023 {
16024 /* Ignore trailing path separator. */
16025 q[-1] = NUL;
16026 q = gettail(p);
16027 }
16028 if (q > p && !mch_isFullName(buf))
16029 {
16030 /* symlink is relative to directory of argument */
16031 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16032 if (cpy != NULL)
16033 {
16034 STRCPY(cpy, p);
16035 STRCPY(gettail(cpy), buf);
16036 vim_free(p);
16037 p = cpy;
16038 }
16039 }
16040 else
16041 {
16042 vim_free(p);
16043 p = vim_strsave(buf);
16044 }
16045 }
16046
16047 if (remain == NULL)
16048 break;
16049
16050 /* Append the first path component of "remain" to "p". */
16051 q = getnextcomp(remain + 1);
16052 len = q - remain - (*q != NUL);
16053 cpy = vim_strnsave(p, STRLEN(p) + len);
16054 if (cpy != NULL)
16055 {
16056 STRNCAT(cpy, remain, len);
16057 vim_free(p);
16058 p = cpy;
16059 }
16060 /* Shorten "remain". */
16061 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016062 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 else
16064 {
16065 vim_free(remain);
16066 remain = NULL;
16067 }
16068 }
16069
16070 /* If the result is a relative path name, make it explicitly relative to
16071 * the current directory if and only if the argument had this form. */
16072 if (!vim_ispathsep(*p))
16073 {
16074 if (is_relative_to_current
16075 && *p != NUL
16076 && !(p[0] == '.'
16077 && (p[1] == NUL
16078 || vim_ispathsep(p[1])
16079 || (p[1] == '.'
16080 && (p[2] == NUL
16081 || vim_ispathsep(p[2]))))))
16082 {
16083 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016084 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 if (cpy != NULL)
16086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087 vim_free(p);
16088 p = cpy;
16089 }
16090 }
16091 else if (!is_relative_to_current)
16092 {
16093 /* Strip leading "./". */
16094 q = p;
16095 while (q[0] == '.' && vim_ispathsep(q[1]))
16096 q += 2;
16097 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016098 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 }
16100 }
16101
16102 /* Ensure that the result will have no trailing path separator
16103 * if the argument had none. But keep "/" or "//". */
16104 if (!has_trailing_pathsep)
16105 {
16106 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016107 if (after_pathsep(p, q))
16108 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 }
16110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016111 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 }
16113# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016114 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115# endif
16116#endif
16117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016118 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119
16120#ifdef HAVE_READLINK
16121fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016122 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016124 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125}
16126
16127/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016128 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129 */
16130 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016131f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016132 typval_T *argvars;
16133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134{
Bram Moolenaar33570922005-01-25 22:26:29 +000016135 list_T *l;
16136 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137
Bram Moolenaar0d660222005-01-07 21:51:51 +000016138 if (argvars[0].v_type != VAR_LIST)
16139 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016140 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016141 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016142 {
16143 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016144 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016145 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016146 while (li != NULL)
16147 {
16148 ni = li->li_prev;
16149 list_append(l, li);
16150 li = ni;
16151 }
16152 rettv->vval.v_list = l;
16153 rettv->v_type = VAR_LIST;
16154 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016155 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157}
16158
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016159#define SP_NOMOVE 0x01 /* don't move cursor */
16160#define SP_REPEAT 0x02 /* repeat to find outer pair */
16161#define SP_RETCOUNT 0x04 /* return matchcount */
16162#define SP_SETPCMARK 0x08 /* set previous context mark */
16163#define SP_START 0x10 /* accept match at start position */
16164#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16165#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016166
Bram Moolenaar33570922005-01-25 22:26:29 +000016167static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168
16169/*
16170 * Get flags for a search function.
16171 * Possibly sets "p_ws".
16172 * Returns BACKWARD, FORWARD or zero (for an error).
16173 */
16174 static int
16175get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177 int *flagsp;
16178{
16179 int dir = FORWARD;
16180 char_u *flags;
16181 char_u nbuf[NUMBUFLEN];
16182 int mask;
16183
16184 if (varp->v_type != VAR_UNKNOWN)
16185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016186 flags = get_tv_string_buf_chk(varp, nbuf);
16187 if (flags == NULL)
16188 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189 while (*flags != NUL)
16190 {
16191 switch (*flags)
16192 {
16193 case 'b': dir = BACKWARD; break;
16194 case 'w': p_ws = TRUE; break;
16195 case 'W': p_ws = FALSE; break;
16196 default: mask = 0;
16197 if (flagsp != NULL)
16198 switch (*flags)
16199 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016200 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016201 case 'e': mask = SP_END; break;
16202 case 'm': mask = SP_RETCOUNT; break;
16203 case 'n': mask = SP_NOMOVE; break;
16204 case 'p': mask = SP_SUBPAT; break;
16205 case 'r': mask = SP_REPEAT; break;
16206 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016207 }
16208 if (mask == 0)
16209 {
16210 EMSG2(_(e_invarg2), flags);
16211 dir = 0;
16212 }
16213 else
16214 *flagsp |= mask;
16215 }
16216 if (dir == 0)
16217 break;
16218 ++flags;
16219 }
16220 }
16221 return dir;
16222}
16223
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016225 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016227 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016228search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016229 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016230 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016231 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016233 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234 char_u *pat;
16235 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016236 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 int save_p_ws = p_ws;
16238 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016239 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016240 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016241 proftime_T tm;
16242#ifdef FEAT_RELTIME
16243 long time_limit = 0;
16244#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016245 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016246 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016248 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016249 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016250 if (dir == 0)
16251 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016252 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016253 if (flags & SP_START)
16254 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016255 if (flags & SP_END)
16256 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016257
Bram Moolenaar76929292008-01-06 19:07:36 +000016258 /* Optional arguments: line number to stop searching and timeout. */
16259 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016260 {
16261 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16262 if (lnum_stop < 0)
16263 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016264#ifdef FEAT_RELTIME
16265 if (argvars[3].v_type != VAR_UNKNOWN)
16266 {
16267 time_limit = get_tv_number_chk(&argvars[3], NULL);
16268 if (time_limit < 0)
16269 goto theend;
16270 }
16271#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016272 }
16273
Bram Moolenaar76929292008-01-06 19:07:36 +000016274#ifdef FEAT_RELTIME
16275 /* Set the time limit, if there is one. */
16276 profile_setlimit(time_limit, &tm);
16277#endif
16278
Bram Moolenaar231334e2005-07-25 20:46:57 +000016279 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016280 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016281 * Check to make sure only those flags are set.
16282 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16283 * flags cannot be set. Check for that condition also.
16284 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016285 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016286 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016288 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016289 goto theend;
16290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016292 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016293 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016294 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016295 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016297 if (flags & SP_SUBPAT)
16298 retval = subpatnum;
16299 else
16300 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016301 if (flags & SP_SETPCMARK)
16302 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016304 if (match_pos != NULL)
16305 {
16306 /* Store the match cursor position */
16307 match_pos->lnum = pos.lnum;
16308 match_pos->col = pos.col + 1;
16309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 /* "/$" will put the cursor after the end of the line, may need to
16311 * correct that here */
16312 check_cursor();
16313 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016314
16315 /* If 'n' flag is used: restore cursor position. */
16316 if (flags & SP_NOMOVE)
16317 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016318 else
16319 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016320theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016322
16323 return retval;
16324}
16325
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016326#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016327
16328/*
16329 * round() is not in C90, use ceil() or floor() instead.
16330 */
16331 float_T
16332vim_round(f)
16333 float_T f;
16334{
16335 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16336}
16337
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016338/*
16339 * "round({float})" function
16340 */
16341 static void
16342f_round(argvars, rettv)
16343 typval_T *argvars;
16344 typval_T *rettv;
16345{
16346 float_T f;
16347
16348 rettv->v_type = VAR_FLOAT;
16349 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016350 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016351 else
16352 rettv->vval.v_float = 0.0;
16353}
16354#endif
16355
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016356/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016357 * "screenattr()" function
16358 */
16359 static void
16360f_screenattr(argvars, rettv)
16361 typval_T *argvars UNUSED;
16362 typval_T *rettv;
16363{
16364 int row;
16365 int col;
16366 int c;
16367
16368 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16369 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16370 if (row < 0 || row >= screen_Rows
16371 || col < 0 || col >= screen_Columns)
16372 c = -1;
16373 else
16374 c = ScreenAttrs[LineOffset[row] + col];
16375 rettv->vval.v_number = c;
16376}
16377
16378/*
16379 * "screenchar()" function
16380 */
16381 static void
16382f_screenchar(argvars, rettv)
16383 typval_T *argvars UNUSED;
16384 typval_T *rettv;
16385{
16386 int row;
16387 int col;
16388 int off;
16389 int c;
16390
16391 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16392 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16393 if (row < 0 || row >= screen_Rows
16394 || col < 0 || col >= screen_Columns)
16395 c = -1;
16396 else
16397 {
16398 off = LineOffset[row] + col;
16399#ifdef FEAT_MBYTE
16400 if (enc_utf8 && ScreenLinesUC[off] != 0)
16401 c = ScreenLinesUC[off];
16402 else
16403#endif
16404 c = ScreenLines[off];
16405 }
16406 rettv->vval.v_number = c;
16407}
16408
16409/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016410 * "screencol()" function
16411 *
16412 * First column is 1 to be consistent with virtcol().
16413 */
16414 static void
16415f_screencol(argvars, rettv)
16416 typval_T *argvars UNUSED;
16417 typval_T *rettv;
16418{
16419 rettv->vval.v_number = screen_screencol() + 1;
16420}
16421
16422/*
16423 * "screenrow()" function
16424 */
16425 static void
16426f_screenrow(argvars, rettv)
16427 typval_T *argvars UNUSED;
16428 typval_T *rettv;
16429{
16430 rettv->vval.v_number = screen_screenrow() + 1;
16431}
16432
16433/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016434 * "search()" function
16435 */
16436 static void
16437f_search(argvars, rettv)
16438 typval_T *argvars;
16439 typval_T *rettv;
16440{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016441 int flags = 0;
16442
16443 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444}
16445
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016447 * "searchdecl()" function
16448 */
16449 static void
16450f_searchdecl(argvars, rettv)
16451 typval_T *argvars;
16452 typval_T *rettv;
16453{
16454 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016455 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016456 int error = FALSE;
16457 char_u *name;
16458
16459 rettv->vval.v_number = 1; /* default: FAIL */
16460
16461 name = get_tv_string_chk(&argvars[0]);
16462 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016463 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016464 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016465 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16466 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16467 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016468 if (!error && name != NULL)
16469 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016470 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016471}
16472
16473/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016474 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016476 static int
16477searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016478 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016479 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480{
16481 char_u *spat, *mpat, *epat;
16482 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484 int dir;
16485 int flags = 0;
16486 char_u nbuf1[NUMBUFLEN];
16487 char_u nbuf2[NUMBUFLEN];
16488 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016489 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016490 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016491 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016494 spat = get_tv_string_chk(&argvars[0]);
16495 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16496 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16497 if (spat == NULL || mpat == NULL || epat == NULL)
16498 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500 /* Handle the optional fourth argument: flags */
16501 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016502 if (dir == 0)
16503 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016504
16505 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016506 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16507 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016508 if ((flags & (SP_END | SP_SUBPAT)) != 0
16509 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016510 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016511 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016512 goto theend;
16513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016515 /* Using 'r' implies 'W', otherwise it doesn't work. */
16516 if (flags & SP_REPEAT)
16517 p_ws = FALSE;
16518
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016519 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016520 if (argvars[3].v_type == VAR_UNKNOWN
16521 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522 skip = (char_u *)"";
16523 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016525 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016526 if (argvars[5].v_type != VAR_UNKNOWN)
16527 {
16528 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16529 if (lnum_stop < 0)
16530 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016531#ifdef FEAT_RELTIME
16532 if (argvars[6].v_type != VAR_UNKNOWN)
16533 {
16534 time_limit = get_tv_number_chk(&argvars[6], NULL);
16535 if (time_limit < 0)
16536 goto theend;
16537 }
16538#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016539 }
16540 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016541 if (skip == NULL)
16542 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016544 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016545 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016546
16547theend:
16548 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016549
16550 return retval;
16551}
16552
16553/*
16554 * "searchpair()" function
16555 */
16556 static void
16557f_searchpair(argvars, rettv)
16558 typval_T *argvars;
16559 typval_T *rettv;
16560{
16561 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16562}
16563
16564/*
16565 * "searchpairpos()" function
16566 */
16567 static void
16568f_searchpairpos(argvars, rettv)
16569 typval_T *argvars;
16570 typval_T *rettv;
16571{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016572 pos_T match_pos;
16573 int lnum = 0;
16574 int col = 0;
16575
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016576 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016577 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016578
16579 if (searchpair_cmn(argvars, &match_pos) > 0)
16580 {
16581 lnum = match_pos.lnum;
16582 col = match_pos.col;
16583 }
16584
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016585 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16586 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016587}
16588
16589/*
16590 * Search for a start/middle/end thing.
16591 * Used by searchpair(), see its documentation for the details.
16592 * Returns 0 or -1 for no match,
16593 */
16594 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016595do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16596 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016597 char_u *spat; /* start pattern */
16598 char_u *mpat; /* middle pattern */
16599 char_u *epat; /* end pattern */
16600 int dir; /* BACKWARD or FORWARD */
16601 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016602 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016603 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016604 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016605 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016606{
16607 char_u *save_cpo;
16608 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16609 long retval = 0;
16610 pos_T pos;
16611 pos_T firstpos;
16612 pos_T foundpos;
16613 pos_T save_cursor;
16614 pos_T save_pos;
16615 int n;
16616 int r;
16617 int nest = 1;
16618 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016619 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016620 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016621
16622 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16623 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016624 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016625
Bram Moolenaar76929292008-01-06 19:07:36 +000016626#ifdef FEAT_RELTIME
16627 /* Set the time limit, if there is one. */
16628 profile_setlimit(time_limit, &tm);
16629#endif
16630
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016631 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16632 * start/middle/end (pat3, for the top pair). */
16633 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16634 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16635 if (pat2 == NULL || pat3 == NULL)
16636 goto theend;
16637 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16638 if (*mpat == NUL)
16639 STRCPY(pat3, pat2);
16640 else
16641 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16642 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016643 if (flags & SP_START)
16644 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016645
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646 save_cursor = curwin->w_cursor;
16647 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016648 clearpos(&firstpos);
16649 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 pat = pat3;
16651 for (;;)
16652 {
16653 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016654 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16656 /* didn't find it or found the first match again: FAIL */
16657 break;
16658
16659 if (firstpos.lnum == 0)
16660 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016661 if (equalpos(pos, foundpos))
16662 {
16663 /* Found the same position again. Can happen with a pattern that
16664 * has "\zs" at the end and searching backwards. Advance one
16665 * character and try again. */
16666 if (dir == BACKWARD)
16667 decl(&pos);
16668 else
16669 incl(&pos);
16670 }
16671 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016673 /* clear the start flag to avoid getting stuck here */
16674 options &= ~SEARCH_START;
16675
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 /* If the skip pattern matches, ignore this match. */
16677 if (*skip != NUL)
16678 {
16679 save_pos = curwin->w_cursor;
16680 curwin->w_cursor = pos;
16681 r = eval_to_bool(skip, &err, NULL, FALSE);
16682 curwin->w_cursor = save_pos;
16683 if (err)
16684 {
16685 /* Evaluating {skip} caused an error, break here. */
16686 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016687 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688 break;
16689 }
16690 if (r)
16691 continue;
16692 }
16693
16694 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16695 {
16696 /* Found end when searching backwards or start when searching
16697 * forward: nested pair. */
16698 ++nest;
16699 pat = pat2; /* nested, don't search for middle */
16700 }
16701 else
16702 {
16703 /* Found end when searching forward or start when searching
16704 * backward: end of (nested) pair; or found middle in outer pair. */
16705 if (--nest == 1)
16706 pat = pat3; /* outer level, search for middle */
16707 }
16708
16709 if (nest == 0)
16710 {
16711 /* Found the match: return matchcount or line number. */
16712 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016713 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016715 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016716 if (flags & SP_SETPCMARK)
16717 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 curwin->w_cursor = pos;
16719 if (!(flags & SP_REPEAT))
16720 break;
16721 nest = 1; /* search for next unmatched */
16722 }
16723 }
16724
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016725 if (match_pos != NULL)
16726 {
16727 /* Store the match cursor position */
16728 match_pos->lnum = curwin->w_cursor.lnum;
16729 match_pos->col = curwin->w_cursor.col + 1;
16730 }
16731
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016733 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 curwin->w_cursor = save_cursor;
16735
16736theend:
16737 vim_free(pat2);
16738 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016739 if (p_cpo == empty_option)
16740 p_cpo = save_cpo;
16741 else
16742 /* Darn, evaluating the {skip} expression changed the value. */
16743 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016744
16745 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746}
16747
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016748/*
16749 * "searchpos()" function
16750 */
16751 static void
16752f_searchpos(argvars, rettv)
16753 typval_T *argvars;
16754 typval_T *rettv;
16755{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016756 pos_T match_pos;
16757 int lnum = 0;
16758 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016759 int n;
16760 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016761
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016762 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016763 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016764
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016765 n = search_cmn(argvars, &match_pos, &flags);
16766 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016767 {
16768 lnum = match_pos.lnum;
16769 col = match_pos.col;
16770 }
16771
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016772 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16773 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016774 if (flags & SP_SUBPAT)
16775 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016776}
16777
16778
Bram Moolenaar0d660222005-01-07 21:51:51 +000016779 static void
16780f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016781 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016784#ifdef FEAT_CLIENTSERVER
16785 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016786 char_u *server = get_tv_string_chk(&argvars[0]);
16787 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788
Bram Moolenaar0d660222005-01-07 21:51:51 +000016789 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016790 if (server == NULL || reply == NULL)
16791 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016792 if (check_restricted() || check_secure())
16793 return;
16794# ifdef FEAT_X11
16795 if (check_connection() == FAIL)
16796 return;
16797# endif
16798
16799 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016801 EMSG(_("E258: Unable to send to client"));
16802 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016804 rettv->vval.v_number = 0;
16805#else
16806 rettv->vval.v_number = -1;
16807#endif
16808}
16809
Bram Moolenaar0d660222005-01-07 21:51:51 +000016810 static void
16811f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016812 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016813 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016814{
16815 char_u *r = NULL;
16816
16817#ifdef FEAT_CLIENTSERVER
16818# ifdef WIN32
16819 r = serverGetVimNames();
16820# else
16821 make_connection();
16822 if (X_DISPLAY != NULL)
16823 r = serverGetVimNames(X_DISPLAY);
16824# endif
16825#endif
16826 rettv->v_type = VAR_STRING;
16827 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828}
16829
16830/*
16831 * "setbufvar()" function
16832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016834f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016835 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016836 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016837{
16838 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016841 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 char_u nbuf[NUMBUFLEN];
16843
16844 if (check_restricted() || check_secure())
16845 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016846 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16847 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016848 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 varp = &argvars[2];
16850
16851 if (buf != NULL && varname != NULL && varp != NULL)
16852 {
16853 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855
16856 if (*varname == '&')
16857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016858 long numval;
16859 char_u *strval;
16860 int error = FALSE;
16861
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016863 numval = get_tv_number_chk(varp, &error);
16864 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016865 if (!error && strval != NULL)
16866 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 }
16868 else
16869 {
16870 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16871 if (bufvarname != NULL)
16872 {
16873 STRCPY(bufvarname, "b:");
16874 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016875 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 vim_free(bufvarname);
16877 }
16878 }
16879
16880 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883}
16884
16885/*
16886 * "setcmdpos()" function
16887 */
16888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016889f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016890 typval_T *argvars;
16891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016893 int pos = (int)get_tv_number(&argvars[0]) - 1;
16894
16895 if (pos >= 0)
16896 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897}
16898
16899/*
16900 * "setline()" function
16901 */
16902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016903f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016904 typval_T *argvars;
16905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906{
16907 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016908 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016909 list_T *l = NULL;
16910 listitem_T *li = NULL;
16911 long added = 0;
16912 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016914 lnum = get_tv_lnum(&argvars[0]);
16915 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016917 l = argvars[1].vval.v_list;
16918 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016920 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016921 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016922
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016923 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016924 for (;;)
16925 {
16926 if (l != NULL)
16927 {
16928 /* list argument, get next string */
16929 if (li == NULL)
16930 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016931 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016932 li = li->li_next;
16933 }
16934
16935 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016936 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016937 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016938
16939 /* When coming here from Insert mode, sync undo, so that this can be
16940 * undone separately from what was previously inserted. */
16941 if (u_sync_once == 2)
16942 {
16943 u_sync_once = 1; /* notify that u_sync() was called */
16944 u_sync(TRUE);
16945 }
16946
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016947 if (lnum <= curbuf->b_ml.ml_line_count)
16948 {
16949 /* existing line, replace it */
16950 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16951 {
16952 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016953 if (lnum == curwin->w_cursor.lnum)
16954 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016955 rettv->vval.v_number = 0; /* OK */
16956 }
16957 }
16958 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16959 {
16960 /* lnum is one past the last line, append the line */
16961 ++added;
16962 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16963 rettv->vval.v_number = 0; /* OK */
16964 }
16965
16966 if (l == NULL) /* only one string argument */
16967 break;
16968 ++lnum;
16969 }
16970
16971 if (added > 0)
16972 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973}
16974
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016975static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16976
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016978 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016979 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016980 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016981set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016982 win_T *wp UNUSED;
16983 typval_T *list_arg UNUSED;
16984 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016985 typval_T *rettv;
16986{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016987#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016988 char_u *act;
16989 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016990#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016991
Bram Moolenaar2641f772005-03-25 21:58:17 +000016992 rettv->vval.v_number = -1;
16993
16994#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016995 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016996 EMSG(_(e_listreq));
16997 else
16998 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016999 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017000
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017001 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017002 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017003 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017004 if (act == NULL)
17005 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017006 if (*act == 'a' || *act == 'r')
17007 action = *act;
17008 }
17009
Bram Moolenaar81484f42012-12-05 15:16:47 +010017010 if (l != NULL && set_errorlist(wp, l, action,
17011 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017012 rettv->vval.v_number = 0;
17013 }
17014#endif
17015}
17016
17017/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017018 * "setloclist()" function
17019 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017020 static void
17021f_setloclist(argvars, rettv)
17022 typval_T *argvars;
17023 typval_T *rettv;
17024{
17025 win_T *win;
17026
17027 rettv->vval.v_number = -1;
17028
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017029 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017030 if (win != NULL)
17031 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17032}
17033
17034/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017035 * "setmatches()" function
17036 */
17037 static void
17038f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017039 typval_T *argvars UNUSED;
17040 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017041{
17042#ifdef FEAT_SEARCH_EXTRA
17043 list_T *l;
17044 listitem_T *li;
17045 dict_T *d;
17046
17047 rettv->vval.v_number = -1;
17048 if (argvars[0].v_type != VAR_LIST)
17049 {
17050 EMSG(_(e_listreq));
17051 return;
17052 }
17053 if ((l = argvars[0].vval.v_list) != NULL)
17054 {
17055
17056 /* To some extent make sure that we are dealing with a list from
17057 * "getmatches()". */
17058 li = l->lv_first;
17059 while (li != NULL)
17060 {
17061 if (li->li_tv.v_type != VAR_DICT
17062 || (d = li->li_tv.vval.v_dict) == NULL)
17063 {
17064 EMSG(_(e_invarg));
17065 return;
17066 }
17067 if (!(dict_find(d, (char_u *)"group", -1) != NULL
17068 && dict_find(d, (char_u *)"pattern", -1) != NULL
17069 && dict_find(d, (char_u *)"priority", -1) != NULL
17070 && dict_find(d, (char_u *)"id", -1) != NULL))
17071 {
17072 EMSG(_(e_invarg));
17073 return;
17074 }
17075 li = li->li_next;
17076 }
17077
17078 clear_matches(curwin);
17079 li = l->lv_first;
17080 while (li != NULL)
17081 {
17082 d = li->li_tv.vval.v_dict;
17083 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
17084 get_dict_string(d, (char_u *)"pattern", FALSE),
17085 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020017086 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017087 li = li->li_next;
17088 }
17089 rettv->vval.v_number = 0;
17090 }
17091#endif
17092}
17093
17094/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017095 * "setpos()" function
17096 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017097 static void
17098f_setpos(argvars, rettv)
17099 typval_T *argvars;
17100 typval_T *rettv;
17101{
17102 pos_T pos;
17103 int fnum;
17104 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017105 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017106
Bram Moolenaar08250432008-02-13 11:42:46 +000017107 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017108 name = get_tv_string_chk(argvars);
17109 if (name != NULL)
17110 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017111 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017112 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017113 if (--pos.col < 0)
17114 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017115 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017116 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017117 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017118 if (fnum == curbuf->b_fnum)
17119 {
17120 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017121 if (curswant >= 0)
17122 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017123 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017124 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017125 }
17126 else
17127 EMSG(_(e_invarg));
17128 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017129 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17130 {
17131 /* set mark */
17132 if (setmark_pos(name[1], &pos, fnum) == OK)
17133 rettv->vval.v_number = 0;
17134 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017135 else
17136 EMSG(_(e_invarg));
17137 }
17138 }
17139}
17140
17141/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017142 * "setqflist()" function
17143 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017144 static void
17145f_setqflist(argvars, rettv)
17146 typval_T *argvars;
17147 typval_T *rettv;
17148{
17149 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17150}
17151
17152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 * "setreg()" function
17154 */
17155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017156f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017157 typval_T *argvars;
17158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159{
17160 int regname;
17161 char_u *strregname;
17162 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017163 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 int append;
17165 char_u yank_type;
17166 long block_len;
17167
17168 block_len = -1;
17169 yank_type = MAUTO;
17170 append = FALSE;
17171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017172 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017173 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017175 if (strregname == NULL)
17176 return; /* type error; errmsg already given */
17177 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 if (regname == 0 || regname == '@')
17179 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017181 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017183 stropt = get_tv_string_chk(&argvars[2]);
17184 if (stropt == NULL)
17185 return; /* type error */
17186 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 switch (*stropt)
17188 {
17189 case 'a': case 'A': /* append */
17190 append = TRUE;
17191 break;
17192 case 'v': case 'c': /* character-wise selection */
17193 yank_type = MCHAR;
17194 break;
17195 case 'V': case 'l': /* line-wise selection */
17196 yank_type = MLINE;
17197 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 case 'b': case Ctrl_V: /* block-wise selection */
17199 yank_type = MBLOCK;
17200 if (VIM_ISDIGIT(stropt[1]))
17201 {
17202 ++stropt;
17203 block_len = getdigits(&stropt) - 1;
17204 --stropt;
17205 }
17206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207 }
17208 }
17209
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017210 if (argvars[1].v_type == VAR_LIST)
17211 {
17212 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017213 char_u **allocval;
17214 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017215 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017216 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017217 int len = argvars[1].vval.v_list->lv_len;
17218 listitem_T *li;
17219
Bram Moolenaar7d647822014-04-05 21:28:56 +020017220 /* First half: use for pointers to result lines; second half: use for
17221 * pointers to allocated copies. */
17222 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017223 if (lstval == NULL)
17224 return;
17225 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017226 allocval = lstval + len + 2;
17227 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017228
17229 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17230 li = li->li_next)
17231 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017232 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017233 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017234 goto free_lstval;
17235 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017236 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017237 /* Need to make a copy, next get_tv_string_buf_chk() will
17238 * overwrite the string. */
17239 strval = vim_strsave(buf);
17240 if (strval == NULL)
17241 goto free_lstval;
17242 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017243 }
17244 *curval++ = strval;
17245 }
17246 *curval++ = NULL;
17247
17248 write_reg_contents_lst(regname, lstval, -1,
17249 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017250free_lstval:
17251 while (curallocval > allocval)
17252 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017253 vim_free(lstval);
17254 }
17255 else
17256 {
17257 strval = get_tv_string_chk(&argvars[1]);
17258 if (strval == NULL)
17259 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017260 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017263 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264}
17265
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017266/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017267 * "settabvar()" function
17268 */
17269 static void
17270f_settabvar(argvars, rettv)
17271 typval_T *argvars;
17272 typval_T *rettv;
17273{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017274#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017275 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017276 tabpage_T *tp;
17277#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017278 char_u *varname, *tabvarname;
17279 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017280
17281 rettv->vval.v_number = 0;
17282
17283 if (check_restricted() || check_secure())
17284 return;
17285
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017286#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017287 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017288#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017289 varname = get_tv_string_chk(&argvars[1]);
17290 varp = &argvars[2];
17291
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017292 if (varname != NULL && varp != NULL
17293#ifdef FEAT_WINDOWS
17294 && tp != NULL
17295#endif
17296 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017297 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017298#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017299 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017300 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017301#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017302
17303 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17304 if (tabvarname != NULL)
17305 {
17306 STRCPY(tabvarname, "t:");
17307 STRCPY(tabvarname + 2, varname);
17308 set_var(tabvarname, varp, TRUE);
17309 vim_free(tabvarname);
17310 }
17311
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017312#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017313 /* Restore current tabpage */
17314 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017315 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017316#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017317 }
17318}
17319
17320/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017321 * "settabwinvar()" function
17322 */
17323 static void
17324f_settabwinvar(argvars, rettv)
17325 typval_T *argvars;
17326 typval_T *rettv;
17327{
17328 setwinvar(argvars, rettv, 1);
17329}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330
17331/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017332 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017336 typval_T *argvars;
17337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017339 setwinvar(argvars, rettv, 0);
17340}
17341
17342/*
17343 * "setwinvar()" and "settabwinvar()" functions
17344 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017345
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017346 static void
17347setwinvar(argvars, rettv, off)
17348 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017349 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017350 int off;
17351{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 win_T *win;
17353#ifdef FEAT_WINDOWS
17354 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017355 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356#endif
17357 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017358 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017360 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361
17362 if (check_restricted() || check_secure())
17363 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017364
17365#ifdef FEAT_WINDOWS
17366 if (off == 1)
17367 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17368 else
17369 tp = curtab;
17370#endif
17371 win = find_win_by_nr(&argvars[off], tp);
17372 varname = get_tv_string_chk(&argvars[off + 1]);
17373 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374
17375 if (win != NULL && varname != NULL && varp != NULL)
17376 {
17377#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017378 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017381 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017383 long numval;
17384 char_u *strval;
17385 int error = FALSE;
17386
17387 ++varname;
17388 numval = get_tv_number_chk(varp, &error);
17389 strval = get_tv_string_buf_chk(varp, nbuf);
17390 if (!error && strval != NULL)
17391 set_option_value(varname, numval, strval, OPT_LOCAL);
17392 }
17393 else
17394 {
17395 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17396 if (winvarname != NULL)
17397 {
17398 STRCPY(winvarname, "w:");
17399 STRCPY(winvarname + 2, varname);
17400 set_var(winvarname, varp, TRUE);
17401 vim_free(winvarname);
17402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 }
17404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017406 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407#endif
17408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409}
17410
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017411#ifdef FEAT_CRYPT
17412/*
17413 * "sha256({string})" function
17414 */
17415 static void
17416f_sha256(argvars, rettv)
17417 typval_T *argvars;
17418 typval_T *rettv;
17419{
17420 char_u *p;
17421
17422 p = get_tv_string(&argvars[0]);
17423 rettv->vval.v_string = vim_strsave(
17424 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17425 rettv->v_type = VAR_STRING;
17426}
17427#endif /* FEAT_CRYPT */
17428
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017430 * "shellescape({string})" function
17431 */
17432 static void
17433f_shellescape(argvars, rettv)
17434 typval_T *argvars;
17435 typval_T *rettv;
17436{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017437 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017438 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017439 rettv->v_type = VAR_STRING;
17440}
17441
17442/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017443 * shiftwidth() function
17444 */
17445 static void
17446f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017447 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017448 typval_T *rettv;
17449{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017450 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017451}
17452
17453/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017454 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 */
17456 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017457f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017458 typval_T *argvars;
17459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017461 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462
Bram Moolenaar0d660222005-01-07 21:51:51 +000017463 p = get_tv_string(&argvars[0]);
17464 rettv->vval.v_string = vim_strsave(p);
17465 simplify_filename(rettv->vval.v_string); /* simplify in place */
17466 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467}
17468
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017469#ifdef FEAT_FLOAT
17470/*
17471 * "sin()" function
17472 */
17473 static void
17474f_sin(argvars, rettv)
17475 typval_T *argvars;
17476 typval_T *rettv;
17477{
17478 float_T f;
17479
17480 rettv->v_type = VAR_FLOAT;
17481 if (get_float_arg(argvars, &f) == OK)
17482 rettv->vval.v_float = sin(f);
17483 else
17484 rettv->vval.v_float = 0.0;
17485}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017486
17487/*
17488 * "sinh()" function
17489 */
17490 static void
17491f_sinh(argvars, rettv)
17492 typval_T *argvars;
17493 typval_T *rettv;
17494{
17495 float_T f;
17496
17497 rettv->v_type = VAR_FLOAT;
17498 if (get_float_arg(argvars, &f) == OK)
17499 rettv->vval.v_float = sinh(f);
17500 else
17501 rettv->vval.v_float = 0.0;
17502}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017503#endif
17504
Bram Moolenaar0d660222005-01-07 21:51:51 +000017505static int
17506#ifdef __BORLANDC__
17507 _RTLENTRYF
17508#endif
17509 item_compare __ARGS((const void *s1, const void *s2));
17510static int
17511#ifdef __BORLANDC__
17512 _RTLENTRYF
17513#endif
17514 item_compare2 __ARGS((const void *s1, const void *s2));
17515
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017516/* struct used in the array that's given to qsort() */
17517typedef struct
17518{
17519 listitem_T *item;
17520 int idx;
17521} sortItem_T;
17522
Bram Moolenaar0d660222005-01-07 21:51:51 +000017523static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017524static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017525static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017526static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017527static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017528static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017529static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017530#define ITEM_COMPARE_FAIL 999
17531
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017533 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017535 static int
17536#ifdef __BORLANDC__
17537_RTLENTRYF
17538#endif
17539item_compare(s1, s2)
17540 const void *s1;
17541 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017543 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017544 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017545 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017546 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017547 int res;
17548 char_u numbuf1[NUMBUFLEN];
17549 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017551 si1 = (sortItem_T *)s1;
17552 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017553 tv1 = &si1->item->li_tv;
17554 tv2 = &si2->item->li_tv;
17555 /* tv2string() puts quotes around a string and allocates memory. Don't do
17556 * that for string variables. Use a single quote when comparing with a
17557 * non-string to do what the docs promise. */
17558 if (tv1->v_type == VAR_STRING)
17559 {
17560 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17561 p1 = (char_u *)"'";
17562 else
17563 p1 = tv1->vval.v_string;
17564 }
17565 else
17566 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17567 if (tv2->v_type == VAR_STRING)
17568 {
17569 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17570 p2 = (char_u *)"'";
17571 else
17572 p2 = tv2->vval.v_string;
17573 }
17574 else
17575 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017576 if (p1 == NULL)
17577 p1 = (char_u *)"";
17578 if (p2 == NULL)
17579 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017580 if (!item_compare_numeric)
17581 {
17582 if (item_compare_ic)
17583 res = STRICMP(p1, p2);
17584 else
17585 res = STRCMP(p1, p2);
17586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017588 {
17589 double n1, n2;
17590 n1 = strtod((char *)p1, (char **)&p1);
17591 n2 = strtod((char *)p2, (char **)&p2);
17592 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17593 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017594
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017595 /* When the result would be zero, compare the item indexes. Makes the
17596 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017597 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017598 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017599
Bram Moolenaar0d660222005-01-07 21:51:51 +000017600 vim_free(tofree1);
17601 vim_free(tofree2);
17602 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603}
17604
17605 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017606#ifdef __BORLANDC__
17607_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017609item_compare2(s1, s2)
17610 const void *s1;
17611 const void *s2;
17612{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017613 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017614 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017615 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017616 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017617 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017619 /* shortcut after failure in previous call; compare all items equal */
17620 if (item_compare_func_err)
17621 return 0;
17622
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017623 si1 = (sortItem_T *)s1;
17624 si2 = (sortItem_T *)s2;
17625
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017626 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017627 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017628 copy_tv(&si1->item->li_tv, &argv[0]);
17629 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017630
17631 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017632 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017633 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17634 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017635 clear_tv(&argv[0]);
17636 clear_tv(&argv[1]);
17637
17638 if (res == FAIL)
17639 res = ITEM_COMPARE_FAIL;
17640 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017641 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17642 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017643 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017644 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017645
17646 /* When the result would be zero, compare the pointers themselves. Makes
17647 * the sort stable. */
17648 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017649 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017650
Bram Moolenaar0d660222005-01-07 21:51:51 +000017651 return res;
17652}
17653
17654/*
17655 * "sort({list})" function
17656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017658do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017659 typval_T *argvars;
17660 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017661 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662{
Bram Moolenaar33570922005-01-25 22:26:29 +000017663 list_T *l;
17664 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017665 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017666 long len;
17667 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668
Bram Moolenaar0d660222005-01-07 21:51:51 +000017669 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017670 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 else
17672 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017673 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017674 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017675 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017676 return;
17677 rettv->vval.v_list = l;
17678 rettv->v_type = VAR_LIST;
17679 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680
Bram Moolenaar0d660222005-01-07 21:51:51 +000017681 len = list_len(l);
17682 if (len <= 1)
17683 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684
Bram Moolenaar0d660222005-01-07 21:51:51 +000017685 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017686 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017687 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017688 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017689 if (argvars[1].v_type != VAR_UNKNOWN)
17690 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017691 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017692 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017693 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017694 else
17695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017696 int error = FALSE;
17697
17698 i = get_tv_number_chk(&argvars[1], &error);
17699 if (error)
17700 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017701 if (i == 1)
17702 item_compare_ic = TRUE;
17703 else
17704 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017705 if (item_compare_func != NULL)
17706 {
17707 if (STRCMP(item_compare_func, "n") == 0)
17708 {
17709 item_compare_func = NULL;
17710 item_compare_numeric = TRUE;
17711 }
17712 else if (STRCMP(item_compare_func, "i") == 0)
17713 {
17714 item_compare_func = NULL;
17715 item_compare_ic = TRUE;
17716 }
17717 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017718 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017719
17720 if (argvars[2].v_type != VAR_UNKNOWN)
17721 {
17722 /* optional third argument: {dict} */
17723 if (argvars[2].v_type != VAR_DICT)
17724 {
17725 EMSG(_(e_dictreq));
17726 return;
17727 }
17728 item_compare_selfdict = argvars[2].vval.v_dict;
17729 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731
Bram Moolenaar0d660222005-01-07 21:51:51 +000017732 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017733 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017734 if (ptrs == NULL)
17735 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736
Bram Moolenaar327aa022014-03-25 18:24:23 +010017737 i = 0;
17738 if (sort)
17739 {
17740 /* sort(): ptrs will be the list to sort */
17741 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017742 {
17743 ptrs[i].item = li;
17744 ptrs[i].idx = i;
17745 ++i;
17746 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017747
17748 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017749 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017750 /* test the compare function */
17751 if (item_compare_func != NULL
17752 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017753 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017754 EMSG(_("E702: Sort compare function failed"));
17755 else
17756 {
17757 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017758 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017759 item_compare_func == NULL ? item_compare : item_compare2);
17760
17761 if (!item_compare_func_err)
17762 {
17763 /* Clear the List and append the items in sorted order. */
17764 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17765 l->lv_len = 0;
17766 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017767 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017768 }
17769 }
17770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017772 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017773 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17774
17775 /* f_uniq(): ptrs will be a stack of items to remove */
17776 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017777 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017778 item_compare_func_ptr = item_compare_func
17779 ? item_compare2 : item_compare;
17780
17781 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17782 li = li->li_next)
17783 {
17784 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17785 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017786 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017787 if (item_compare_func_err)
17788 {
17789 EMSG(_("E882: Uniq compare function failed"));
17790 break;
17791 }
17792 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017794 if (!item_compare_func_err)
17795 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017796 while (--i >= 0)
17797 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017798 li = ptrs[i].item->li_next;
17799 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017800 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017801 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017802 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017803 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017804 list_fix_watch(l, li);
17805 listitem_free(li);
17806 l->lv_len--;
17807 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017808 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017809 }
17810
17811 vim_free(ptrs);
17812 }
17813}
17814
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017815/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017816 * "sort({list})" function
17817 */
17818 static void
17819f_sort(argvars, rettv)
17820 typval_T *argvars;
17821 typval_T *rettv;
17822{
17823 do_sort_uniq(argvars, rettv, TRUE);
17824}
17825
17826/*
17827 * "uniq({list})" function
17828 */
17829 static void
17830f_uniq(argvars, rettv)
17831 typval_T *argvars;
17832 typval_T *rettv;
17833{
17834 do_sort_uniq(argvars, rettv, FALSE);
17835}
17836
17837/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017838 * "soundfold({word})" function
17839 */
17840 static void
17841f_soundfold(argvars, rettv)
17842 typval_T *argvars;
17843 typval_T *rettv;
17844{
17845 char_u *s;
17846
17847 rettv->v_type = VAR_STRING;
17848 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017849#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017850 rettv->vval.v_string = eval_soundfold(s);
17851#else
17852 rettv->vval.v_string = vim_strsave(s);
17853#endif
17854}
17855
17856/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017857 * "spellbadword()" function
17858 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017859 static void
17860f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017861 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017862 typval_T *rettv;
17863{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017864 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017865 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017866 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017867
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017868 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017869 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017870
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017871#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017872 if (argvars[0].v_type == VAR_UNKNOWN)
17873 {
17874 /* Find the start and length of the badly spelled word. */
17875 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17876 if (len != 0)
17877 word = ml_get_cursor();
17878 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017879 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017880 {
17881 char_u *str = get_tv_string_chk(&argvars[0]);
17882 int capcol = -1;
17883
17884 if (str != NULL)
17885 {
17886 /* Check the argument for spelling. */
17887 while (*str != NUL)
17888 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017889 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017890 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017891 {
17892 word = str;
17893 break;
17894 }
17895 str += len;
17896 }
17897 }
17898 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017899#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017900
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017901 list_append_string(rettv->vval.v_list, word, len);
17902 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017903 attr == HLF_SPB ? "bad" :
17904 attr == HLF_SPR ? "rare" :
17905 attr == HLF_SPL ? "local" :
17906 attr == HLF_SPC ? "caps" :
17907 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017908}
17909
17910/*
17911 * "spellsuggest()" function
17912 */
17913 static void
17914f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017915 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017916 typval_T *rettv;
17917{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017918#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017919 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017920 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017921 int maxcount;
17922 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017923 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017924 listitem_T *li;
17925 int need_capital = FALSE;
17926#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017927
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017928 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017929 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017930
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017931#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017932 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017933 {
17934 str = get_tv_string(&argvars[0]);
17935 if (argvars[1].v_type != VAR_UNKNOWN)
17936 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017937 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017938 if (maxcount <= 0)
17939 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017940 if (argvars[2].v_type != VAR_UNKNOWN)
17941 {
17942 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17943 if (typeerr)
17944 return;
17945 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017946 }
17947 else
17948 maxcount = 25;
17949
Bram Moolenaar4770d092006-01-12 23:22:24 +000017950 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017951
17952 for (i = 0; i < ga.ga_len; ++i)
17953 {
17954 str = ((char_u **)ga.ga_data)[i];
17955
17956 li = listitem_alloc();
17957 if (li == NULL)
17958 vim_free(str);
17959 else
17960 {
17961 li->li_tv.v_type = VAR_STRING;
17962 li->li_tv.v_lock = 0;
17963 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017964 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017965 }
17966 }
17967 ga_clear(&ga);
17968 }
17969#endif
17970}
17971
Bram Moolenaar0d660222005-01-07 21:51:51 +000017972 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017973f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017974 typval_T *argvars;
17975 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017976{
17977 char_u *str;
17978 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017979 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017980 regmatch_T regmatch;
17981 char_u patbuf[NUMBUFLEN];
17982 char_u *save_cpo;
17983 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017984 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017985 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017986 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017987
17988 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17989 save_cpo = p_cpo;
17990 p_cpo = (char_u *)"";
17991
17992 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017993 if (argvars[1].v_type != VAR_UNKNOWN)
17994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017995 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17996 if (pat == NULL)
17997 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017999 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018000 }
18001 if (pat == NULL || *pat == NUL)
18002 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018003
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018004 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018006 if (typeerr)
18007 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008
Bram Moolenaar0d660222005-01-07 21:51:51 +000018009 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18010 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018012 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018013 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018014 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018015 if (*str == NUL)
18016 match = FALSE; /* empty item at the end */
18017 else
18018 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018019 if (match)
18020 end = regmatch.startp[0];
18021 else
18022 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018023 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18024 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018025 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018026 if (list_append_string(rettv->vval.v_list, str,
18027 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018028 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018029 }
18030 if (!match)
18031 break;
18032 /* Advance to just after the match. */
18033 if (regmatch.endp[0] > str)
18034 col = 0;
18035 else
18036 {
18037 /* Don't get stuck at the same match. */
18038#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018039 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018040#else
18041 col = 1;
18042#endif
18043 }
18044 str = regmatch.endp[0];
18045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046
Bram Moolenaar473de612013-06-08 18:19:48 +020018047 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049
Bram Moolenaar0d660222005-01-07 21:51:51 +000018050 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051}
18052
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018053#ifdef FEAT_FLOAT
18054/*
18055 * "sqrt()" function
18056 */
18057 static void
18058f_sqrt(argvars, rettv)
18059 typval_T *argvars;
18060 typval_T *rettv;
18061{
18062 float_T f;
18063
18064 rettv->v_type = VAR_FLOAT;
18065 if (get_float_arg(argvars, &f) == OK)
18066 rettv->vval.v_float = sqrt(f);
18067 else
18068 rettv->vval.v_float = 0.0;
18069}
18070
18071/*
18072 * "str2float()" function
18073 */
18074 static void
18075f_str2float(argvars, rettv)
18076 typval_T *argvars;
18077 typval_T *rettv;
18078{
18079 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18080
18081 if (*p == '+')
18082 p = skipwhite(p + 1);
18083 (void)string2float(p, &rettv->vval.v_float);
18084 rettv->v_type = VAR_FLOAT;
18085}
18086#endif
18087
Bram Moolenaar2c932302006-03-18 21:42:09 +000018088/*
18089 * "str2nr()" function
18090 */
18091 static void
18092f_str2nr(argvars, rettv)
18093 typval_T *argvars;
18094 typval_T *rettv;
18095{
18096 int base = 10;
18097 char_u *p;
18098 long n;
18099
18100 if (argvars[1].v_type != VAR_UNKNOWN)
18101 {
18102 base = get_tv_number(&argvars[1]);
18103 if (base != 8 && base != 10 && base != 16)
18104 {
18105 EMSG(_(e_invarg));
18106 return;
18107 }
18108 }
18109
18110 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018111 if (*p == '+')
18112 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018113 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
18114 rettv->vval.v_number = n;
18115}
18116
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117#ifdef HAVE_STRFTIME
18118/*
18119 * "strftime({format}[, {time}])" function
18120 */
18121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018122f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018123 typval_T *argvars;
18124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125{
18126 char_u result_buf[256];
18127 struct tm *curtime;
18128 time_t seconds;
18129 char_u *p;
18130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018131 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018133 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018134 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 seconds = time(NULL);
18136 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018137 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 curtime = localtime(&seconds);
18139 /* MSVC returns NULL for an invalid value of seconds. */
18140 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018141 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142 else
18143 {
18144# ifdef FEAT_MBYTE
18145 vimconv_T conv;
18146 char_u *enc;
18147
18148 conv.vc_type = CONV_NONE;
18149 enc = enc_locale();
18150 convert_setup(&conv, p_enc, enc);
18151 if (conv.vc_type != CONV_NONE)
18152 p = string_convert(&conv, p, NULL);
18153# endif
18154 if (p != NULL)
18155 (void)strftime((char *)result_buf, sizeof(result_buf),
18156 (char *)p, curtime);
18157 else
18158 result_buf[0] = NUL;
18159
18160# ifdef FEAT_MBYTE
18161 if (conv.vc_type != CONV_NONE)
18162 vim_free(p);
18163 convert_setup(&conv, enc, p_enc);
18164 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018165 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 else
18167# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018168 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169
18170# ifdef FEAT_MBYTE
18171 /* Release conversion descriptors */
18172 convert_setup(&conv, NULL, NULL);
18173 vim_free(enc);
18174# endif
18175 }
18176}
18177#endif
18178
18179/*
18180 * "stridx()" function
18181 */
18182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018183f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018184 typval_T *argvars;
18185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186{
18187 char_u buf[NUMBUFLEN];
18188 char_u *needle;
18189 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018190 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018192 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018194 needle = get_tv_string_chk(&argvars[1]);
18195 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018196 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018197 if (needle == NULL || haystack == NULL)
18198 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199
Bram Moolenaar33570922005-01-25 22:26:29 +000018200 if (argvars[2].v_type != VAR_UNKNOWN)
18201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018202 int error = FALSE;
18203
18204 start_idx = get_tv_number_chk(&argvars[2], &error);
18205 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018206 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018207 if (start_idx >= 0)
18208 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018209 }
18210
18211 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18212 if (pos != NULL)
18213 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214}
18215
18216/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018217 * "string()" function
18218 */
18219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018220f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018221 typval_T *argvars;
18222 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018223{
18224 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018225 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018227 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018228 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018229 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018230 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018231 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232}
18233
18234/*
18235 * "strlen()" function
18236 */
18237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018238f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018239 typval_T *argvars;
18240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018242 rettv->vval.v_number = (varnumber_T)(STRLEN(
18243 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244}
18245
18246/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018247 * "strchars()" function
18248 */
18249 static void
18250f_strchars(argvars, rettv)
18251 typval_T *argvars;
18252 typval_T *rettv;
18253{
18254 char_u *s = get_tv_string(&argvars[0]);
18255#ifdef FEAT_MBYTE
18256 varnumber_T len = 0;
18257
18258 while (*s != NUL)
18259 {
18260 mb_cptr2char_adv(&s);
18261 ++len;
18262 }
18263 rettv->vval.v_number = len;
18264#else
18265 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18266#endif
18267}
18268
18269/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018270 * "strdisplaywidth()" function
18271 */
18272 static void
18273f_strdisplaywidth(argvars, rettv)
18274 typval_T *argvars;
18275 typval_T *rettv;
18276{
18277 char_u *s = get_tv_string(&argvars[0]);
18278 int col = 0;
18279
18280 if (argvars[1].v_type != VAR_UNKNOWN)
18281 col = get_tv_number(&argvars[1]);
18282
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018283 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018284}
18285
18286/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018287 * "strwidth()" function
18288 */
18289 static void
18290f_strwidth(argvars, rettv)
18291 typval_T *argvars;
18292 typval_T *rettv;
18293{
18294 char_u *s = get_tv_string(&argvars[0]);
18295
18296 rettv->vval.v_number = (varnumber_T)(
18297#ifdef FEAT_MBYTE
18298 mb_string2cells(s, -1)
18299#else
18300 STRLEN(s)
18301#endif
18302 );
18303}
18304
18305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 * "strpart()" function
18307 */
18308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018309f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018310 typval_T *argvars;
18311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312{
18313 char_u *p;
18314 int n;
18315 int len;
18316 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018317 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018319 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 slen = (int)STRLEN(p);
18321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018322 n = get_tv_number_chk(&argvars[1], &error);
18323 if (error)
18324 len = 0;
18325 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018326 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 else
18328 len = slen - n; /* default len: all bytes that are available. */
18329
18330 /*
18331 * Only return the overlap between the specified part and the actual
18332 * string.
18333 */
18334 if (n < 0)
18335 {
18336 len += n;
18337 n = 0;
18338 }
18339 else if (n > slen)
18340 n = slen;
18341 if (len < 0)
18342 len = 0;
18343 else if (n + len > slen)
18344 len = slen - n;
18345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018346 rettv->v_type = VAR_STRING;
18347 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348}
18349
18350/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018351 * "strridx()" function
18352 */
18353 static void
18354f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018355 typval_T *argvars;
18356 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018357{
18358 char_u buf[NUMBUFLEN];
18359 char_u *needle;
18360 char_u *haystack;
18361 char_u *rest;
18362 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018363 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018365 needle = get_tv_string_chk(&argvars[1]);
18366 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018367
18368 rettv->vval.v_number = -1;
18369 if (needle == NULL || haystack == NULL)
18370 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018371
18372 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018373 if (argvars[2].v_type != VAR_UNKNOWN)
18374 {
18375 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018376 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018377 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018378 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018379 }
18380 else
18381 end_idx = haystack_len;
18382
Bram Moolenaar0d660222005-01-07 21:51:51 +000018383 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018384 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018385 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018386 lastmatch = haystack + end_idx;
18387 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018388 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018389 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018390 for (rest = haystack; *rest != '\0'; ++rest)
18391 {
18392 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018393 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018394 break;
18395 lastmatch = rest;
18396 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018397 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018398
18399 if (lastmatch == NULL)
18400 rettv->vval.v_number = -1;
18401 else
18402 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18403}
18404
18405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 * "strtrans()" function
18407 */
18408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018409f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018410 typval_T *argvars;
18411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018413 rettv->v_type = VAR_STRING;
18414 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415}
18416
18417/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018418 * "submatch()" function
18419 */
18420 static void
18421f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018422 typval_T *argvars;
18423 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018424{
Bram Moolenaar41571762014-04-02 19:00:58 +020018425 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018426 int no;
18427 int retList = 0;
18428
18429 no = (int)get_tv_number_chk(&argvars[0], &error);
18430 if (error)
18431 return;
18432 error = FALSE;
18433 if (argvars[1].v_type != VAR_UNKNOWN)
18434 retList = get_tv_number_chk(&argvars[1], &error);
18435 if (error)
18436 return;
18437
18438 if (retList == 0)
18439 {
18440 rettv->v_type = VAR_STRING;
18441 rettv->vval.v_string = reg_submatch(no);
18442 }
18443 else
18444 {
18445 rettv->v_type = VAR_LIST;
18446 rettv->vval.v_list = reg_submatch_list(no);
18447 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018448}
18449
18450/*
18451 * "substitute()" function
18452 */
18453 static void
18454f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018455 typval_T *argvars;
18456 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018457{
18458 char_u patbuf[NUMBUFLEN];
18459 char_u subbuf[NUMBUFLEN];
18460 char_u flagsbuf[NUMBUFLEN];
18461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018462 char_u *str = get_tv_string_chk(&argvars[0]);
18463 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18464 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18465 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18466
Bram Moolenaar0d660222005-01-07 21:51:51 +000018467 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018468 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18469 rettv->vval.v_string = NULL;
18470 else
18471 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018472}
18473
18474/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018475 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018478f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018479 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481{
18482 int id = 0;
18483#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018484 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 long col;
18486 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018487 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018489 lnum = get_tv_lnum(argvars); /* -1 on type error */
18490 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18491 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018493 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018494 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018495 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496#endif
18497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018498 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499}
18500
18501/*
18502 * "synIDattr(id, what [, mode])" function
18503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018505f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018506 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508{
18509 char_u *p = NULL;
18510#ifdef FEAT_SYN_HL
18511 int id;
18512 char_u *what;
18513 char_u *mode;
18514 char_u modebuf[NUMBUFLEN];
18515 int modec;
18516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018517 id = get_tv_number(&argvars[0]);
18518 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018519 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018521 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018523 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 modec = 0; /* replace invalid with current */
18525 }
18526 else
18527 {
18528#ifdef FEAT_GUI
18529 if (gui.in_use)
18530 modec = 'g';
18531 else
18532#endif
18533 if (t_colors > 1)
18534 modec = 'c';
18535 else
18536 modec = 't';
18537 }
18538
18539
18540 switch (TOLOWER_ASC(what[0]))
18541 {
18542 case 'b':
18543 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18544 p = highlight_color(id, what, modec);
18545 else /* bold */
18546 p = highlight_has_attr(id, HL_BOLD, modec);
18547 break;
18548
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018549 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 p = highlight_color(id, what, modec);
18551 break;
18552
18553 case 'i':
18554 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18555 p = highlight_has_attr(id, HL_INVERSE, modec);
18556 else /* italic */
18557 p = highlight_has_attr(id, HL_ITALIC, modec);
18558 break;
18559
18560 case 'n': /* name */
18561 p = get_highlight_name(NULL, id - 1);
18562 break;
18563
18564 case 'r': /* reverse */
18565 p = highlight_has_attr(id, HL_INVERSE, modec);
18566 break;
18567
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018568 case 's':
18569 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18570 p = highlight_color(id, what, modec);
18571 else /* standout */
18572 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 break;
18574
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018575 case 'u':
18576 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18577 /* underline */
18578 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18579 else
18580 /* undercurl */
18581 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 break;
18583 }
18584
18585 if (p != NULL)
18586 p = vim_strsave(p);
18587#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018588 rettv->v_type = VAR_STRING;
18589 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590}
18591
18592/*
18593 * "synIDtrans(id)" function
18594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018596f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018597 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599{
18600 int id;
18601
18602#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018603 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604
18605 if (id > 0)
18606 id = syn_get_final_id(id);
18607 else
18608#endif
18609 id = 0;
18610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018611 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612}
18613
18614/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018615 * "synconcealed(lnum, col)" function
18616 */
18617 static void
18618f_synconcealed(argvars, rettv)
18619 typval_T *argvars UNUSED;
18620 typval_T *rettv;
18621{
18622#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18623 long lnum;
18624 long col;
18625 int syntax_flags = 0;
18626 int cchar;
18627 int matchid = 0;
18628 char_u str[NUMBUFLEN];
18629#endif
18630
18631 rettv->v_type = VAR_LIST;
18632 rettv->vval.v_list = NULL;
18633
18634#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18635 lnum = get_tv_lnum(argvars); /* -1 on type error */
18636 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18637
18638 vim_memset(str, NUL, sizeof(str));
18639
18640 if (rettv_list_alloc(rettv) != FAIL)
18641 {
18642 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18643 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18644 && curwin->w_p_cole > 0)
18645 {
18646 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18647 syntax_flags = get_syntax_info(&matchid);
18648
18649 /* get the conceal character */
18650 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18651 {
18652 cchar = syn_get_sub_char();
18653 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18654 cchar = lcs_conceal;
18655 if (cchar != NUL)
18656 {
18657# ifdef FEAT_MBYTE
18658 if (has_mbyte)
18659 (*mb_char2bytes)(cchar, str);
18660 else
18661# endif
18662 str[0] = cchar;
18663 }
18664 }
18665 }
18666
18667 list_append_number(rettv->vval.v_list,
18668 (syntax_flags & HL_CONCEAL) != 0);
18669 /* -1 to auto-determine strlen */
18670 list_append_string(rettv->vval.v_list, str, -1);
18671 list_append_number(rettv->vval.v_list, matchid);
18672 }
18673#endif
18674}
18675
18676/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018677 * "synstack(lnum, col)" function
18678 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018679 static void
18680f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018681 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018682 typval_T *rettv;
18683{
18684#ifdef FEAT_SYN_HL
18685 long lnum;
18686 long col;
18687 int i;
18688 int id;
18689#endif
18690
18691 rettv->v_type = VAR_LIST;
18692 rettv->vval.v_list = NULL;
18693
18694#ifdef FEAT_SYN_HL
18695 lnum = get_tv_lnum(argvars); /* -1 on type error */
18696 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18697
18698 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018699 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018700 && rettv_list_alloc(rettv) != FAIL)
18701 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018702 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018703 for (i = 0; ; ++i)
18704 {
18705 id = syn_get_stack_item(i);
18706 if (id < 0)
18707 break;
18708 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18709 break;
18710 }
18711 }
18712#endif
18713}
18714
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018716get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018717 typval_T *argvars;
18718 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018719 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018721 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018723 char_u *infile = NULL;
18724 char_u buf[NUMBUFLEN];
18725 int err = FALSE;
18726 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018727 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018728 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018730 rettv->v_type = VAR_STRING;
18731 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018732 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018733 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018734
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018735 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018736 {
18737 /*
18738 * Write the string to a temp file, to be used for input of the shell
18739 * command.
18740 */
18741 if ((infile = vim_tempname('i')) == NULL)
18742 {
18743 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018744 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018745 }
18746
18747 fd = mch_fopen((char *)infile, WRITEBIN);
18748 if (fd == NULL)
18749 {
18750 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018751 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018752 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018753 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018754 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018755 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18756 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018757 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018758 else
18759 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018760 size_t len;
18761
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018762 p = get_tv_string_buf_chk(&argvars[1], buf);
18763 if (p == NULL)
18764 {
18765 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018766 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018767 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018768 len = STRLEN(p);
18769 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018770 err = TRUE;
18771 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018772 if (fclose(fd) != 0)
18773 err = TRUE;
18774 if (err)
18775 {
18776 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018777 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018778 }
18779 }
18780
Bram Moolenaar52a72462014-08-29 15:53:52 +020018781 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18782 * echoes typeahead, that messes up the display. */
18783 if (!msg_silent)
18784 flags += SHELL_COOKED;
18785
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018786 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018788 int len;
18789 listitem_T *li;
18790 char_u *s = NULL;
18791 char_u *start;
18792 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018793 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794
Bram Moolenaar52a72462014-08-29 15:53:52 +020018795 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018796 if (res == NULL)
18797 goto errret;
18798
18799 list = list_alloc();
18800 if (list == NULL)
18801 goto errret;
18802
18803 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018805 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018806 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018807 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018808 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018809
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018810 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018811 if (s == NULL)
18812 goto errret;
18813
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018814 for (p = s; start < end; ++p, ++start)
18815 *p = *start == NUL ? NL : *start;
18816 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018817
18818 li = listitem_alloc();
18819 if (li == NULL)
18820 {
18821 vim_free(s);
18822 goto errret;
18823 }
18824 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010018825 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018826 li->li_tv.vval.v_string = s;
18827 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018829
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018830 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018831 rettv->v_type = VAR_LIST;
18832 rettv->vval.v_list = list;
18833 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018835 else
18836 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018837 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018838#ifdef USE_CR
18839 /* translate <CR> into <NL> */
18840 if (res != NULL)
18841 {
18842 char_u *s;
18843
18844 for (s = res; *s; ++s)
18845 {
18846 if (*s == CAR)
18847 *s = NL;
18848 }
18849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850#else
18851# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018852 /* translate <CR><NL> into <NL> */
18853 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018855 char_u *s, *d;
18856
18857 d = res;
18858 for (s = res; *s; ++s)
18859 {
18860 if (s[0] == CAR && s[1] == NL)
18861 ++s;
18862 *d++ = *s;
18863 }
18864 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866# endif
18867#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018868 rettv->vval.v_string = res;
18869 res = NULL;
18870 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018871
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018872errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018873 if (infile != NULL)
18874 {
18875 mch_remove(infile);
18876 vim_free(infile);
18877 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018878 if (res != NULL)
18879 vim_free(res);
18880 if (list != NULL)
18881 list_free(list, TRUE);
18882}
18883
18884/*
18885 * "system()" function
18886 */
18887 static void
18888f_system(argvars, rettv)
18889 typval_T *argvars;
18890 typval_T *rettv;
18891{
18892 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18893}
18894
18895/*
18896 * "systemlist()" function
18897 */
18898 static void
18899f_systemlist(argvars, rettv)
18900 typval_T *argvars;
18901 typval_T *rettv;
18902{
18903 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904}
18905
18906/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018907 * "tabpagebuflist()" function
18908 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018909 static void
18910f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018911 typval_T *argvars UNUSED;
18912 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018913{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018914#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018915 tabpage_T *tp;
18916 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018917
18918 if (argvars[0].v_type == VAR_UNKNOWN)
18919 wp = firstwin;
18920 else
18921 {
18922 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18923 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018924 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018925 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018926 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018927 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018928 for (; wp != NULL; wp = wp->w_next)
18929 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018930 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018931 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018932 }
18933#endif
18934}
18935
18936
18937/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018938 * "tabpagenr()" function
18939 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018940 static void
18941f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018942 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018943 typval_T *rettv;
18944{
18945 int nr = 1;
18946#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018947 char_u *arg;
18948
18949 if (argvars[0].v_type != VAR_UNKNOWN)
18950 {
18951 arg = get_tv_string_chk(&argvars[0]);
18952 nr = 0;
18953 if (arg != NULL)
18954 {
18955 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018956 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018957 else
18958 EMSG2(_(e_invexpr2), arg);
18959 }
18960 }
18961 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018962 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018963#endif
18964 rettv->vval.v_number = nr;
18965}
18966
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018967
18968#ifdef FEAT_WINDOWS
18969static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18970
18971/*
18972 * Common code for tabpagewinnr() and winnr().
18973 */
18974 static int
18975get_winnr(tp, argvar)
18976 tabpage_T *tp;
18977 typval_T *argvar;
18978{
18979 win_T *twin;
18980 int nr = 1;
18981 win_T *wp;
18982 char_u *arg;
18983
18984 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18985 if (argvar->v_type != VAR_UNKNOWN)
18986 {
18987 arg = get_tv_string_chk(argvar);
18988 if (arg == NULL)
18989 nr = 0; /* type error; errmsg already given */
18990 else if (STRCMP(arg, "$") == 0)
18991 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18992 else if (STRCMP(arg, "#") == 0)
18993 {
18994 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18995 if (twin == NULL)
18996 nr = 0;
18997 }
18998 else
18999 {
19000 EMSG2(_(e_invexpr2), arg);
19001 nr = 0;
19002 }
19003 }
19004
19005 if (nr > 0)
19006 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19007 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019008 {
19009 if (wp == NULL)
19010 {
19011 /* didn't find it in this tabpage */
19012 nr = 0;
19013 break;
19014 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019015 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019016 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019017 return nr;
19018}
19019#endif
19020
19021/*
19022 * "tabpagewinnr()" function
19023 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019024 static void
19025f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019026 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019027 typval_T *rettv;
19028{
19029 int nr = 1;
19030#ifdef FEAT_WINDOWS
19031 tabpage_T *tp;
19032
19033 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19034 if (tp == NULL)
19035 nr = 0;
19036 else
19037 nr = get_winnr(tp, &argvars[1]);
19038#endif
19039 rettv->vval.v_number = nr;
19040}
19041
19042
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019043/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019044 * "tagfiles()" function
19045 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019046 static void
19047f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019048 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019049 typval_T *rettv;
19050{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019051 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019052 tagname_T tn;
19053 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019054
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019055 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019056 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019057 fname = alloc(MAXPATHL);
19058 if (fname == NULL)
19059 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019060
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019061 for (first = TRUE; ; first = FALSE)
19062 if (get_tagfname(&tn, first, fname) == FAIL
19063 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019064 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019065 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019066 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019067}
19068
19069/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019070 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019071 */
19072 static void
19073f_taglist(argvars, rettv)
19074 typval_T *argvars;
19075 typval_T *rettv;
19076{
19077 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019078
19079 tag_pattern = get_tv_string(&argvars[0]);
19080
19081 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019082 if (*tag_pattern == NUL)
19083 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019084
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019085 if (rettv_list_alloc(rettv) == OK)
19086 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019087}
19088
19089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 * "tempname()" function
19091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019093f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019094 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096{
19097 static int x = 'A';
19098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019099 rettv->v_type = VAR_STRING;
19100 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101
19102 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19103 * names. Skip 'I' and 'O', they are used for shell redirection. */
19104 do
19105 {
19106 if (x == 'Z')
19107 x = '0';
19108 else if (x == '9')
19109 x = 'A';
19110 else
19111 {
19112#ifdef EBCDIC
19113 if (x == 'I')
19114 x = 'J';
19115 else if (x == 'R')
19116 x = 'S';
19117 else
19118#endif
19119 ++x;
19120 }
19121 } while (x == 'I' || x == 'O');
19122}
19123
19124/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019125 * "test(list)" function: Just checking the walls...
19126 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019127 static void
19128f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019129 typval_T *argvars UNUSED;
19130 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019131{
19132 /* Used for unit testing. Change the code below to your liking. */
19133#if 0
19134 listitem_T *li;
19135 list_T *l;
19136 char_u *bad, *good;
19137
19138 if (argvars[0].v_type != VAR_LIST)
19139 return;
19140 l = argvars[0].vval.v_list;
19141 if (l == NULL)
19142 return;
19143 li = l->lv_first;
19144 if (li == NULL)
19145 return;
19146 bad = get_tv_string(&li->li_tv);
19147 li = li->li_next;
19148 if (li == NULL)
19149 return;
19150 good = get_tv_string(&li->li_tv);
19151 rettv->vval.v_number = test_edit_score(bad, good);
19152#endif
19153}
19154
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019155#ifdef FEAT_FLOAT
19156/*
19157 * "tan()" function
19158 */
19159 static void
19160f_tan(argvars, rettv)
19161 typval_T *argvars;
19162 typval_T *rettv;
19163{
19164 float_T f;
19165
19166 rettv->v_type = VAR_FLOAT;
19167 if (get_float_arg(argvars, &f) == OK)
19168 rettv->vval.v_float = tan(f);
19169 else
19170 rettv->vval.v_float = 0.0;
19171}
19172
19173/*
19174 * "tanh()" function
19175 */
19176 static void
19177f_tanh(argvars, rettv)
19178 typval_T *argvars;
19179 typval_T *rettv;
19180{
19181 float_T f;
19182
19183 rettv->v_type = VAR_FLOAT;
19184 if (get_float_arg(argvars, &f) == OK)
19185 rettv->vval.v_float = tanh(f);
19186 else
19187 rettv->vval.v_float = 0.0;
19188}
19189#endif
19190
Bram Moolenaard52d9742005-08-21 22:20:28 +000019191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192 * "tolower(string)" function
19193 */
19194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019195f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019196 typval_T *argvars;
19197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198{
19199 char_u *p;
19200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201 p = vim_strsave(get_tv_string(&argvars[0]));
19202 rettv->v_type = VAR_STRING;
19203 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204
19205 if (p != NULL)
19206 while (*p != NUL)
19207 {
19208#ifdef FEAT_MBYTE
19209 int l;
19210
19211 if (enc_utf8)
19212 {
19213 int c, lc;
19214
19215 c = utf_ptr2char(p);
19216 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019217 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 /* TODO: reallocate string when byte count changes. */
19219 if (utf_char2len(lc) == l)
19220 utf_char2bytes(lc, p);
19221 p += l;
19222 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019223 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 p += l; /* skip multi-byte character */
19225 else
19226#endif
19227 {
19228 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19229 ++p;
19230 }
19231 }
19232}
19233
19234/*
19235 * "toupper(string)" function
19236 */
19237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019238f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019239 typval_T *argvars;
19240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019242 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019243 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244}
19245
19246/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019247 * "tr(string, fromstr, tostr)" function
19248 */
19249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019250f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019251 typval_T *argvars;
19252 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019253{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019254 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019255 char_u *fromstr;
19256 char_u *tostr;
19257 char_u *p;
19258#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019259 int inlen;
19260 int fromlen;
19261 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019262 int idx;
19263 char_u *cpstr;
19264 int cplen;
19265 int first = TRUE;
19266#endif
19267 char_u buf[NUMBUFLEN];
19268 char_u buf2[NUMBUFLEN];
19269 garray_T ga;
19270
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019271 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019272 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19273 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019274
19275 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019276 rettv->v_type = VAR_STRING;
19277 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019278 if (fromstr == NULL || tostr == NULL)
19279 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019280 ga_init2(&ga, (int)sizeof(char), 80);
19281
19282#ifdef FEAT_MBYTE
19283 if (!has_mbyte)
19284#endif
19285 /* not multi-byte: fromstr and tostr must be the same length */
19286 if (STRLEN(fromstr) != STRLEN(tostr))
19287 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019288#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019289error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019290#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019291 EMSG2(_(e_invarg2), fromstr);
19292 ga_clear(&ga);
19293 return;
19294 }
19295
19296 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019297 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019298 {
19299#ifdef FEAT_MBYTE
19300 if (has_mbyte)
19301 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019302 inlen = (*mb_ptr2len)(in_str);
19303 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019304 cplen = inlen;
19305 idx = 0;
19306 for (p = fromstr; *p != NUL; p += fromlen)
19307 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019308 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019309 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019310 {
19311 for (p = tostr; *p != NUL; p += tolen)
19312 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019313 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019314 if (idx-- == 0)
19315 {
19316 cplen = tolen;
19317 cpstr = p;
19318 break;
19319 }
19320 }
19321 if (*p == NUL) /* tostr is shorter than fromstr */
19322 goto error;
19323 break;
19324 }
19325 ++idx;
19326 }
19327
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019328 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019329 {
19330 /* Check that fromstr and tostr have the same number of
19331 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019332 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019333 first = FALSE;
19334 for (p = tostr; *p != NUL; p += tolen)
19335 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019336 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019337 --idx;
19338 }
19339 if (idx != 0)
19340 goto error;
19341 }
19342
19343 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019344 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019345 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019346
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019347 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019348 }
19349 else
19350#endif
19351 {
19352 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019353 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019354 if (p != NULL)
19355 ga_append(&ga, tostr[p - fromstr]);
19356 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019357 ga_append(&ga, *in_str);
19358 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019359 }
19360 }
19361
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019362 /* add a terminating NUL */
19363 ga_grow(&ga, 1);
19364 ga_append(&ga, NUL);
19365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019366 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019367}
19368
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019369#ifdef FEAT_FLOAT
19370/*
19371 * "trunc({float})" function
19372 */
19373 static void
19374f_trunc(argvars, rettv)
19375 typval_T *argvars;
19376 typval_T *rettv;
19377{
19378 float_T f;
19379
19380 rettv->v_type = VAR_FLOAT;
19381 if (get_float_arg(argvars, &f) == OK)
19382 /* trunc() is not in C90, use floor() or ceil() instead. */
19383 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19384 else
19385 rettv->vval.v_float = 0.0;
19386}
19387#endif
19388
Bram Moolenaar8299df92004-07-10 09:47:34 +000019389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 * "type(expr)" function
19391 */
19392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019393f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019394 typval_T *argvars;
19395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019397 int n;
19398
19399 switch (argvars[0].v_type)
19400 {
19401 case VAR_NUMBER: n = 0; break;
19402 case VAR_STRING: n = 1; break;
19403 case VAR_FUNC: n = 2; break;
19404 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019405 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019406#ifdef FEAT_FLOAT
19407 case VAR_FLOAT: n = 5; break;
19408#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019409 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19410 }
19411 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412}
19413
19414/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019415 * "undofile(name)" function
19416 */
19417 static void
19418f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019419 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019420 typval_T *rettv;
19421{
19422 rettv->v_type = VAR_STRING;
19423#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019424 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019425 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019426
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019427 if (*fname == NUL)
19428 {
19429 /* If there is no file name there will be no undo file. */
19430 rettv->vval.v_string = NULL;
19431 }
19432 else
19433 {
19434 char_u *ffname = FullName_save(fname, FALSE);
19435
19436 if (ffname != NULL)
19437 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19438 vim_free(ffname);
19439 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019440 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019441#else
19442 rettv->vval.v_string = NULL;
19443#endif
19444}
19445
19446/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019447 * "undotree()" function
19448 */
19449 static void
19450f_undotree(argvars, rettv)
19451 typval_T *argvars UNUSED;
19452 typval_T *rettv;
19453{
19454 if (rettv_dict_alloc(rettv) == OK)
19455 {
19456 dict_T *dict = rettv->vval.v_dict;
19457 list_T *list;
19458
Bram Moolenaar730cde92010-06-27 05:18:54 +020019459 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019460 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019461 dict_add_nr_str(dict, "save_last",
19462 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019463 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19464 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019465 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019466
19467 list = list_alloc();
19468 if (list != NULL)
19469 {
19470 u_eval_tree(curbuf->b_u_oldhead, list);
19471 dict_add_list(dict, "entries", list);
19472 }
19473 }
19474}
19475
19476/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019477 * "values(dict)" function
19478 */
19479 static void
19480f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019481 typval_T *argvars;
19482 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019483{
19484 dict_list(argvars, rettv, 1);
19485}
19486
19487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 * "virtcol(string)" function
19489 */
19490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019491f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019492 typval_T *argvars;
19493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494{
19495 colnr_T vcol = 0;
19496 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019497 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019499 fp = var2fpos(&argvars[0], FALSE, &fnum);
19500 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19501 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 {
19503 getvvcol(curwin, fp, NULL, NULL, &vcol);
19504 ++vcol;
19505 }
19506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019507 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508}
19509
19510/*
19511 * "visualmode()" function
19512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019514f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019515 typval_T *argvars UNUSED;
19516 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518 char_u str[2];
19519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019520 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 str[0] = curbuf->b_visual_mode_eval;
19522 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524
19525 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019526 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528}
19529
19530/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019531 * "wildmenumode()" function
19532 */
19533 static void
19534f_wildmenumode(argvars, rettv)
19535 typval_T *argvars UNUSED;
19536 typval_T *rettv UNUSED;
19537{
19538#ifdef FEAT_WILDMENU
19539 if (wild_menu_showing)
19540 rettv->vval.v_number = 1;
19541#endif
19542}
19543
19544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 * "winbufnr(nr)" function
19546 */
19547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019548f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019549 typval_T *argvars;
19550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551{
19552 win_T *wp;
19553
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019554 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019556 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019558 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559}
19560
19561/*
19562 * "wincol()" function
19563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019565f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019566 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568{
19569 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019570 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571}
19572
19573/*
19574 * "winheight(nr)" function
19575 */
19576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019577f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019578 typval_T *argvars;
19579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019580{
19581 win_T *wp;
19582
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019583 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019585 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019587 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588}
19589
19590/*
19591 * "winline()" function
19592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019594f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019595 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597{
19598 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019599 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600}
19601
19602/*
19603 * "winnr()" function
19604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019606f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019607 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609{
19610 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019611
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019613 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019615 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616}
19617
19618/*
19619 * "winrestcmd()" function
19620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019622f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019623 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625{
19626#ifdef FEAT_WINDOWS
19627 win_T *wp;
19628 int winnr = 1;
19629 garray_T ga;
19630 char_u buf[50];
19631
19632 ga_init2(&ga, (int)sizeof(char), 70);
19633 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19634 {
19635 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19636 ga_concat(&ga, buf);
19637# ifdef FEAT_VERTSPLIT
19638 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19639 ga_concat(&ga, buf);
19640# endif
19641 ++winnr;
19642 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019643 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019645 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019647 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019649 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650}
19651
19652/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019653 * "winrestview()" function
19654 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019655 static void
19656f_winrestview(argvars, rettv)
19657 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019658 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019659{
19660 dict_T *dict;
19661
19662 if (argvars[0].v_type != VAR_DICT
19663 || (dict = argvars[0].vval.v_dict) == NULL)
19664 EMSG(_(e_invarg));
19665 else
19666 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019667 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19668 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19669 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19670 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019671#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019672 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19673 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019674#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019675 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19676 {
19677 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19678 curwin->w_set_curswant = FALSE;
19679 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019680
Bram Moolenaar82c25852014-05-28 16:47:16 +020019681 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19682 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019683#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019684 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19685 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019686#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019687 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19688 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19689 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19690 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019691
19692 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019693 win_new_height(curwin, curwin->w_height);
19694# ifdef FEAT_VERTSPLIT
19695 win_new_width(curwin, W_WIDTH(curwin));
19696# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019697 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019698
Bram Moolenaarb851a962014-10-31 15:45:52 +010019699 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019700 curwin->w_topline = 1;
19701 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19702 curwin->w_topline = curbuf->b_ml.ml_line_count;
19703#ifdef FEAT_DIFF
19704 check_topfill(curwin, TRUE);
19705#endif
19706 }
19707}
19708
19709/*
19710 * "winsaveview()" function
19711 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019712 static void
19713f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019714 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019715 typval_T *rettv;
19716{
19717 dict_T *dict;
19718
Bram Moolenaara800b422010-06-27 01:15:55 +020019719 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019720 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019721 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019722
19723 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19724 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19725#ifdef FEAT_VIRTUALEDIT
19726 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19727#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019728 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019729 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19730
19731 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19732#ifdef FEAT_DIFF
19733 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19734#endif
19735 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19736 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19737}
19738
19739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740 * "winwidth(nr)" function
19741 */
19742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019743f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019744 typval_T *argvars;
19745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746{
19747 win_T *wp;
19748
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019749 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019751 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 else
19753#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019754 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019756 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757#endif
19758}
19759
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019761 * Write list of strings to file
19762 */
19763 static int
19764write_list(fd, list, binary)
19765 FILE *fd;
19766 list_T *list;
19767 int binary;
19768{
19769 listitem_T *li;
19770 int c;
19771 int ret = OK;
19772 char_u *s;
19773
19774 for (li = list->lv_first; li != NULL; li = li->li_next)
19775 {
19776 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19777 {
19778 if (*s == '\n')
19779 c = putc(NUL, fd);
19780 else
19781 c = putc(*s, fd);
19782 if (c == EOF)
19783 {
19784 ret = FAIL;
19785 break;
19786 }
19787 }
19788 if (!binary || li->li_next != NULL)
19789 if (putc('\n', fd) == EOF)
19790 {
19791 ret = FAIL;
19792 break;
19793 }
19794 if (ret == FAIL)
19795 {
19796 EMSG(_(e_write));
19797 break;
19798 }
19799 }
19800 return ret;
19801}
19802
19803/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019804 * "writefile()" function
19805 */
19806 static void
19807f_writefile(argvars, rettv)
19808 typval_T *argvars;
19809 typval_T *rettv;
19810{
19811 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019812 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019813 char_u *fname;
19814 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019815 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019816
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019817 if (check_restricted() || check_secure())
19818 return;
19819
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019820 if (argvars[0].v_type != VAR_LIST)
19821 {
19822 EMSG2(_(e_listarg), "writefile()");
19823 return;
19824 }
19825 if (argvars[0].vval.v_list == NULL)
19826 return;
19827
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019828 if (argvars[2].v_type != VAR_UNKNOWN)
19829 {
19830 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19831 binary = TRUE;
19832 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19833 append = TRUE;
19834 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019835
19836 /* Always open the file in binary mode, library functions have a mind of
19837 * their own about CR-LF conversion. */
19838 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019839 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19840 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019841 {
19842 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19843 ret = -1;
19844 }
19845 else
19846 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019847 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19848 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019849 fclose(fd);
19850 }
19851
19852 rettv->vval.v_number = ret;
19853}
19854
19855/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019856 * "xor(expr, expr)" function
19857 */
19858 static void
19859f_xor(argvars, rettv)
19860 typval_T *argvars;
19861 typval_T *rettv;
19862{
19863 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19864 ^ get_tv_number_chk(&argvars[1], NULL);
19865}
19866
19867
19868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019870 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 */
19872 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019873var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019874 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019875 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019876 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019878 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019880 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881
Bram Moolenaara5525202006-03-02 22:52:09 +000019882 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019883 if (varp->v_type == VAR_LIST)
19884 {
19885 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019886 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019887 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019888 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019889
19890 l = varp->vval.v_list;
19891 if (l == NULL)
19892 return NULL;
19893
19894 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019895 pos.lnum = list_find_nr(l, 0L, &error);
19896 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019897 return NULL; /* invalid line number */
19898
19899 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019900 pos.col = list_find_nr(l, 1L, &error);
19901 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019902 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019903 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019904
19905 /* We accept "$" for the column number: last column. */
19906 li = list_find(l, 1L);
19907 if (li != NULL && li->li_tv.v_type == VAR_STRING
19908 && li->li_tv.vval.v_string != NULL
19909 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19910 pos.col = len + 1;
19911
Bram Moolenaara5525202006-03-02 22:52:09 +000019912 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019913 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019914 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019915 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019916
Bram Moolenaara5525202006-03-02 22:52:09 +000019917#ifdef FEAT_VIRTUALEDIT
19918 /* Get the virtual offset. Defaults to zero. */
19919 pos.coladd = list_find_nr(l, 2L, &error);
19920 if (error)
19921 pos.coladd = 0;
19922#endif
19923
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019924 return &pos;
19925 }
19926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019927 name = get_tv_string_chk(varp);
19928 if (name == NULL)
19929 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019930 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019932 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19933 {
19934 if (VIsual_active)
19935 return &VIsual;
19936 return &curwin->w_cursor;
19937 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019938 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019940 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19942 return NULL;
19943 return pp;
19944 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019945
19946#ifdef FEAT_VIRTUALEDIT
19947 pos.coladd = 0;
19948#endif
19949
Bram Moolenaar477933c2007-07-17 14:32:23 +000019950 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019951 {
19952 pos.col = 0;
19953 if (name[1] == '0') /* "w0": first visible line */
19954 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019955 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019956 pos.lnum = curwin->w_topline;
19957 return &pos;
19958 }
19959 else if (name[1] == '$') /* "w$": last visible line */
19960 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019961 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019962 pos.lnum = curwin->w_botline - 1;
19963 return &pos;
19964 }
19965 }
19966 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019968 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 {
19970 pos.lnum = curbuf->b_ml.ml_line_count;
19971 pos.col = 0;
19972 }
19973 else
19974 {
19975 pos.lnum = curwin->w_cursor.lnum;
19976 pos.col = (colnr_T)STRLEN(ml_get_curline());
19977 }
19978 return &pos;
19979 }
19980 return NULL;
19981}
19982
19983/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019984 * Convert list in "arg" into a position and optional file number.
19985 * When "fnump" is NULL there is no file number, only 3 items.
19986 * Note that the column is passed on as-is, the caller may want to decrement
19987 * it to use 1 for the first column.
19988 * Return FAIL when conversion is not possible, doesn't check the position for
19989 * validity.
19990 */
19991 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020019992list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019993 typval_T *arg;
19994 pos_T *posp;
19995 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020019996 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019997{
19998 list_T *l = arg->vval.v_list;
19999 long i = 0;
20000 long n;
20001
Bram Moolenaar493c1782014-05-28 14:34:46 +020020002 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20003 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020004 if (arg->v_type != VAR_LIST
20005 || l == NULL
20006 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020007 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020008 return FAIL;
20009
20010 if (fnump != NULL)
20011 {
20012 n = list_find_nr(l, i++, NULL); /* fnum */
20013 if (n < 0)
20014 return FAIL;
20015 if (n == 0)
20016 n = curbuf->b_fnum; /* current buffer */
20017 *fnump = n;
20018 }
20019
20020 n = list_find_nr(l, i++, NULL); /* lnum */
20021 if (n < 0)
20022 return FAIL;
20023 posp->lnum = n;
20024
20025 n = list_find_nr(l, i++, NULL); /* col */
20026 if (n < 0)
20027 return FAIL;
20028 posp->col = n;
20029
20030#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020031 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020032 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020033 posp->coladd = 0;
20034 else
20035 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020036#endif
20037
Bram Moolenaar493c1782014-05-28 14:34:46 +020020038 if (curswantp != NULL)
20039 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20040
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020041 return OK;
20042}
20043
20044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 * Get the length of an environment variable name.
20046 * Advance "arg" to the first character after the name.
20047 * Return 0 for error.
20048 */
20049 static int
20050get_env_len(arg)
20051 char_u **arg;
20052{
20053 char_u *p;
20054 int len;
20055
20056 for (p = *arg; vim_isIDc(*p); ++p)
20057 ;
20058 if (p == *arg) /* no name found */
20059 return 0;
20060
20061 len = (int)(p - *arg);
20062 *arg = p;
20063 return len;
20064}
20065
20066/*
20067 * Get the length of the name of a function or internal variable.
20068 * "arg" is advanced to the first non-white character after the name.
20069 * Return 0 if something is wrong.
20070 */
20071 static int
20072get_id_len(arg)
20073 char_u **arg;
20074{
20075 char_u *p;
20076 int len;
20077
20078 /* Find the end of the name. */
20079 for (p = *arg; eval_isnamec(*p); ++p)
20080 ;
20081 if (p == *arg) /* no name found */
20082 return 0;
20083
20084 len = (int)(p - *arg);
20085 *arg = skipwhite(p);
20086
20087 return len;
20088}
20089
20090/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020091 * Get the length of the name of a variable or function.
20092 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020094 * Return -1 if curly braces expansion failed.
20095 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 * If the name contains 'magic' {}'s, expand them and return the
20097 * expanded name in an allocated string via 'alias' - caller must free.
20098 */
20099 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020100get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 char_u **arg;
20102 char_u **alias;
20103 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020104 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105{
20106 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 char_u *p;
20108 char_u *expr_start;
20109 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110
20111 *alias = NULL; /* default to no alias */
20112
20113 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20114 && (*arg)[2] == (int)KE_SNR)
20115 {
20116 /* hard coded <SNR>, already translated */
20117 *arg += 3;
20118 return get_id_len(arg) + 3;
20119 }
20120 len = eval_fname_script(*arg);
20121 if (len > 0)
20122 {
20123 /* literal "<SID>", "s:" or "<SNR>" */
20124 *arg += len;
20125 }
20126
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020128 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020130 p = find_name_end(*arg, &expr_start, &expr_end,
20131 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 if (expr_start != NULL)
20133 {
20134 char_u *temp_string;
20135
20136 if (!evaluate)
20137 {
20138 len += (int)(p - *arg);
20139 *arg = skipwhite(p);
20140 return len;
20141 }
20142
20143 /*
20144 * Include any <SID> etc in the expanded string:
20145 * Thus the -len here.
20146 */
20147 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20148 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020149 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 *alias = temp_string;
20151 *arg = skipwhite(p);
20152 return (int)STRLEN(temp_string);
20153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154
20155 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020156 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 EMSG2(_(e_invexpr2), *arg);
20158
20159 return len;
20160}
20161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020162/*
20163 * Find the end of a variable or function name, taking care of magic braces.
20164 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20165 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020166 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020167 * Return a pointer to just after the name. Equal to "arg" if there is no
20168 * valid name.
20169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020171find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172 char_u *arg;
20173 char_u **expr_start;
20174 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020175 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020177 int mb_nest = 0;
20178 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 char_u *p;
20180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020181 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020183 *expr_start = NULL;
20184 *expr_end = NULL;
20185 }
20186
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020187 /* Quick check for valid starting character. */
20188 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20189 return arg;
20190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020191 for (p = arg; *p != NUL
20192 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020193 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020194 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020195 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020196 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020197 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020198 if (*p == '\'')
20199 {
20200 /* skip over 'string' to avoid counting [ and ] inside it. */
20201 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20202 ;
20203 if (*p == NUL)
20204 break;
20205 }
20206 else if (*p == '"')
20207 {
20208 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20209 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20210 if (*p == '\\' && p[1] != NUL)
20211 ++p;
20212 if (*p == NUL)
20213 break;
20214 }
20215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020216 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 if (*p == '[')
20219 ++br_nest;
20220 else if (*p == ']')
20221 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020224 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020226 if (*p == '{')
20227 {
20228 mb_nest++;
20229 if (expr_start != NULL && *expr_start == NULL)
20230 *expr_start = p;
20231 }
20232 else if (*p == '}')
20233 {
20234 mb_nest--;
20235 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20236 *expr_end = p;
20237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239 }
20240
20241 return p;
20242}
20243
20244/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020245 * Expands out the 'magic' {}'s in a variable/function name.
20246 * Note that this can call itself recursively, to deal with
20247 * constructs like foo{bar}{baz}{bam}
20248 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20249 * "in_start" ^
20250 * "expr_start" ^
20251 * "expr_end" ^
20252 * "in_end" ^
20253 *
20254 * Returns a new allocated string, which the caller must free.
20255 * Returns NULL for failure.
20256 */
20257 static char_u *
20258make_expanded_name(in_start, expr_start, expr_end, in_end)
20259 char_u *in_start;
20260 char_u *expr_start;
20261 char_u *expr_end;
20262 char_u *in_end;
20263{
20264 char_u c1;
20265 char_u *retval = NULL;
20266 char_u *temp_result;
20267 char_u *nextcmd = NULL;
20268
20269 if (expr_end == NULL || in_end == NULL)
20270 return NULL;
20271 *expr_start = NUL;
20272 *expr_end = NUL;
20273 c1 = *in_end;
20274 *in_end = NUL;
20275
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020276 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020277 if (temp_result != NULL && nextcmd == NULL)
20278 {
20279 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20280 + (in_end - expr_end) + 1));
20281 if (retval != NULL)
20282 {
20283 STRCPY(retval, in_start);
20284 STRCAT(retval, temp_result);
20285 STRCAT(retval, expr_end + 1);
20286 }
20287 }
20288 vim_free(temp_result);
20289
20290 *in_end = c1; /* put char back for error messages */
20291 *expr_start = '{';
20292 *expr_end = '}';
20293
20294 if (retval != NULL)
20295 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020296 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020297 if (expr_start != NULL)
20298 {
20299 /* Further expansion! */
20300 temp_result = make_expanded_name(retval, expr_start,
20301 expr_end, temp_result);
20302 vim_free(retval);
20303 retval = temp_result;
20304 }
20305 }
20306
20307 return retval;
20308}
20309
20310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020312 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 */
20314 static int
20315eval_isnamec(c)
20316 int c;
20317{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020318 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20319}
20320
20321/*
20322 * Return TRUE if character "c" can be used as the first character in a
20323 * variable or function name (excluding '{' and '}').
20324 */
20325 static int
20326eval_isnamec1(c)
20327 int c;
20328{
20329 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330}
20331
20332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 * Set number v: variable to "val".
20334 */
20335 void
20336set_vim_var_nr(idx, val)
20337 int idx;
20338 long val;
20339{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020340 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341}
20342
20343/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020344 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 */
20346 long
20347get_vim_var_nr(idx)
20348 int idx;
20349{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020350 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351}
20352
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020353/*
20354 * Get string v: variable value. Uses a static buffer, can only be used once.
20355 */
20356 char_u *
20357get_vim_var_str(idx)
20358 int idx;
20359{
20360 return get_tv_string(&vimvars[idx].vv_tv);
20361}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020362
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020364 * Get List v: variable value. Caller must take care of reference count when
20365 * needed.
20366 */
20367 list_T *
20368get_vim_var_list(idx)
20369 int idx;
20370{
20371 return vimvars[idx].vv_list;
20372}
20373
20374/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020375 * Set v:char to character "c".
20376 */
20377 void
20378set_vim_var_char(c)
20379 int c;
20380{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020381 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020382
20383#ifdef FEAT_MBYTE
20384 if (has_mbyte)
20385 buf[(*mb_char2bytes)(c, buf)] = NUL;
20386 else
20387#endif
20388 {
20389 buf[0] = c;
20390 buf[1] = NUL;
20391 }
20392 set_vim_var_string(VV_CHAR, buf, -1);
20393}
20394
20395/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020396 * Set v:count to "count" and v:count1 to "count1".
20397 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 */
20399 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020400set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401 long count;
20402 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020403 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020405 if (set_prevcount)
20406 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020407 vimvars[VV_COUNT].vv_nr = count;
20408 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409}
20410
20411/*
20412 * Set string v: variable to a copy of "val".
20413 */
20414 void
20415set_vim_var_string(idx, val, len)
20416 int idx;
20417 char_u *val;
20418 int len; /* length of "val" to use or -1 (whole string) */
20419{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020420 /* Need to do this (at least) once, since we can't initialize a union.
20421 * Will always be invoked when "v:progname" is set. */
20422 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20423
Bram Moolenaare9a41262005-01-15 22:18:47 +000020424 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020426 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020428 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020430 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431}
20432
20433/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020434 * Set List v: variable to "val".
20435 */
20436 void
20437set_vim_var_list(idx, val)
20438 int idx;
20439 list_T *val;
20440{
20441 list_unref(vimvars[idx].vv_list);
20442 vimvars[idx].vv_list = val;
20443 if (val != NULL)
20444 ++val->lv_refcount;
20445}
20446
20447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 * Set v:register if needed.
20449 */
20450 void
20451set_reg_var(c)
20452 int c;
20453{
20454 char_u regname;
20455
20456 if (c == 0 || c == ' ')
20457 regname = '"';
20458 else
20459 regname = c;
20460 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020461 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 set_vim_var_string(VV_REG, &regname, 1);
20463}
20464
20465/*
20466 * Get or set v:exception. If "oldval" == NULL, return the current value.
20467 * Otherwise, restore the value to "oldval" and return NULL.
20468 * Must always be called in pairs to save and restore v:exception! Does not
20469 * take care of memory allocations.
20470 */
20471 char_u *
20472v_exception(oldval)
20473 char_u *oldval;
20474{
20475 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020476 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477
Bram Moolenaare9a41262005-01-15 22:18:47 +000020478 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 return NULL;
20480}
20481
20482/*
20483 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20484 * Otherwise, restore the value to "oldval" and return NULL.
20485 * Must always be called in pairs to save and restore v:throwpoint! Does not
20486 * take care of memory allocations.
20487 */
20488 char_u *
20489v_throwpoint(oldval)
20490 char_u *oldval;
20491{
20492 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020493 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494
Bram Moolenaare9a41262005-01-15 22:18:47 +000020495 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 return NULL;
20497}
20498
20499#if defined(FEAT_AUTOCMD) || defined(PROTO)
20500/*
20501 * Set v:cmdarg.
20502 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20503 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20504 * Must always be called in pairs!
20505 */
20506 char_u *
20507set_cmdarg(eap, oldarg)
20508 exarg_T *eap;
20509 char_u *oldarg;
20510{
20511 char_u *oldval;
20512 char_u *newval;
20513 unsigned len;
20514
Bram Moolenaare9a41262005-01-15 22:18:47 +000020515 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020516 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020518 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020519 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020520 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 }
20522
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020523 if (eap->force_bin == FORCE_BIN)
20524 len = 6;
20525 else if (eap->force_bin == FORCE_NOBIN)
20526 len = 8;
20527 else
20528 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020529
20530 if (eap->read_edit)
20531 len += 7;
20532
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020533 if (eap->force_ff != 0)
20534 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20535# ifdef FEAT_MBYTE
20536 if (eap->force_enc != 0)
20537 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020538 if (eap->bad_char != 0)
20539 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020540# endif
20541
20542 newval = alloc(len + 1);
20543 if (newval == NULL)
20544 return NULL;
20545
20546 if (eap->force_bin == FORCE_BIN)
20547 sprintf((char *)newval, " ++bin");
20548 else if (eap->force_bin == FORCE_NOBIN)
20549 sprintf((char *)newval, " ++nobin");
20550 else
20551 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020552
20553 if (eap->read_edit)
20554 STRCAT(newval, " ++edit");
20555
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020556 if (eap->force_ff != 0)
20557 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20558 eap->cmd + eap->force_ff);
20559# ifdef FEAT_MBYTE
20560 if (eap->force_enc != 0)
20561 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20562 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020563 if (eap->bad_char == BAD_KEEP)
20564 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20565 else if (eap->bad_char == BAD_DROP)
20566 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20567 else if (eap->bad_char != 0)
20568 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020569# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020570 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020571 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572}
20573#endif
20574
20575/*
20576 * Get the value of internal variable "name".
20577 * Return OK or FAIL.
20578 */
20579 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020580get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581 char_u *name;
20582 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020583 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020584 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020585 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586{
20587 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020588 typval_T *tv = NULL;
20589 typval_T atv;
20590 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592
20593 /* truncate the name, so that we can use strcmp() */
20594 cc = name[len];
20595 name[len] = NUL;
20596
20597 /*
20598 * Check for "b:changedtick".
20599 */
20600 if (STRCMP(name, "b:changedtick") == 0)
20601 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020602 atv.v_type = VAR_NUMBER;
20603 atv.vval.v_number = curbuf->b_changedtick;
20604 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 }
20606
20607 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 * Check for user-defined variables.
20609 */
20610 else
20611 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020612 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020614 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 }
20616
Bram Moolenaare9a41262005-01-15 22:18:47 +000020617 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020619 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020620 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 ret = FAIL;
20622 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020623 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020624 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625
20626 name[len] = cc;
20627
20628 return ret;
20629}
20630
20631/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020632 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20633 * Also handle function call with Funcref variable: func(expr)
20634 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20635 */
20636 static int
20637handle_subscript(arg, rettv, evaluate, verbose)
20638 char_u **arg;
20639 typval_T *rettv;
20640 int evaluate; /* do more than finding the end */
20641 int verbose; /* give error messages */
20642{
20643 int ret = OK;
20644 dict_T *selfdict = NULL;
20645 char_u *s;
20646 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020647 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020648
20649 while (ret == OK
20650 && (**arg == '['
20651 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020652 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020653 && !vim_iswhite(*(*arg - 1)))
20654 {
20655 if (**arg == '(')
20656 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020657 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020658 if (evaluate)
20659 {
20660 functv = *rettv;
20661 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020662
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020663 /* Invoke the function. Recursive! */
20664 s = functv.vval.v_string;
20665 }
20666 else
20667 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020668 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020669 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20670 &len, evaluate, selfdict);
20671
20672 /* Clear the funcref afterwards, so that deleting it while
20673 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020674 if (evaluate)
20675 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020676
20677 /* Stop the expression evaluation when immediately aborting on
20678 * error, or when an interrupt occurred or an exception was thrown
20679 * but not caught. */
20680 if (aborting())
20681 {
20682 if (ret == OK)
20683 clear_tv(rettv);
20684 ret = FAIL;
20685 }
20686 dict_unref(selfdict);
20687 selfdict = NULL;
20688 }
20689 else /* **arg == '[' || **arg == '.' */
20690 {
20691 dict_unref(selfdict);
20692 if (rettv->v_type == VAR_DICT)
20693 {
20694 selfdict = rettv->vval.v_dict;
20695 if (selfdict != NULL)
20696 ++selfdict->dv_refcount;
20697 }
20698 else
20699 selfdict = NULL;
20700 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20701 {
20702 clear_tv(rettv);
20703 ret = FAIL;
20704 }
20705 }
20706 }
20707 dict_unref(selfdict);
20708 return ret;
20709}
20710
20711/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020712 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020713 * value).
20714 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020715 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020716alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020717{
Bram Moolenaar33570922005-01-25 22:26:29 +000020718 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020719}
20720
20721/*
20722 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 * The string "s" must have been allocated, it is consumed.
20724 * Return NULL for out of memory, the variable otherwise.
20725 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020726 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020727alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 char_u *s;
20729{
Bram Moolenaar33570922005-01-25 22:26:29 +000020730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020732 rettv = alloc_tv();
20733 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020735 rettv->v_type = VAR_STRING;
20736 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 }
20738 else
20739 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020740 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741}
20742
20743/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020744 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020746 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020747free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020748 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749{
20750 if (varp != NULL)
20751 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020752 switch (varp->v_type)
20753 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020754 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020755 func_unref(varp->vval.v_string);
20756 /*FALLTHROUGH*/
20757 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020758 vim_free(varp->vval.v_string);
20759 break;
20760 case VAR_LIST:
20761 list_unref(varp->vval.v_list);
20762 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020763 case VAR_DICT:
20764 dict_unref(varp->vval.v_dict);
20765 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020766 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020767#ifdef FEAT_FLOAT
20768 case VAR_FLOAT:
20769#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020770 case VAR_UNKNOWN:
20771 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020772 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020773 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020774 break;
20775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 vim_free(varp);
20777 }
20778}
20779
20780/*
20781 * Free the memory for a variable value and set the value to NULL or 0.
20782 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020783 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020784clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020785 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786{
20787 if (varp != NULL)
20788 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020789 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020791 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020792 func_unref(varp->vval.v_string);
20793 /*FALLTHROUGH*/
20794 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020795 vim_free(varp->vval.v_string);
20796 varp->vval.v_string = NULL;
20797 break;
20798 case VAR_LIST:
20799 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020800 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020801 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020802 case VAR_DICT:
20803 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020804 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020805 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020806 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020807 varp->vval.v_number = 0;
20808 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020809#ifdef FEAT_FLOAT
20810 case VAR_FLOAT:
20811 varp->vval.v_float = 0.0;
20812 break;
20813#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020814 case VAR_UNKNOWN:
20815 break;
20816 default:
20817 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020819 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 }
20821}
20822
20823/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020824 * Set the value of a variable to NULL without freeing items.
20825 */
20826 static void
20827init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020828 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020829{
20830 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020831 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020832}
20833
20834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 * Get the number value of a variable.
20836 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020837 * For incompatible types, return 0.
20838 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20839 * caller of incompatible types: it sets *denote to TRUE if "denote"
20840 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 */
20842 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020843get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020844 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020846 int error = FALSE;
20847
20848 return get_tv_number_chk(varp, &error); /* return 0L on error */
20849}
20850
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020851 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020852get_tv_number_chk(varp, denote)
20853 typval_T *varp;
20854 int *denote;
20855{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020856 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020858 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020860 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020861 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020862#ifdef FEAT_FLOAT
20863 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020864 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020865 break;
20866#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020867 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020868 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020869 break;
20870 case VAR_STRING:
20871 if (varp->vval.v_string != NULL)
20872 vim_str2nr(varp->vval.v_string, NULL, NULL,
20873 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020874 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020875 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020876 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020877 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020878 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020879 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020880 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020881 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020882 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020883 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020885 if (denote == NULL) /* useful for values that must be unsigned */
20886 n = -1;
20887 else
20888 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020889 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890}
20891
20892/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020893 * Get the lnum from the first argument.
20894 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020895 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 */
20897 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020898get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020899 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900{
Bram Moolenaar33570922005-01-25 22:26:29 +000020901 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 linenr_T lnum;
20903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020904 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 if (lnum == 0) /* no valid number, try using line() */
20906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020907 rettv.v_type = VAR_NUMBER;
20908 f_line(argvars, &rettv);
20909 lnum = rettv.vval.v_number;
20910 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 }
20912 return lnum;
20913}
20914
20915/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020916 * Get the lnum from the first argument.
20917 * Also accepts "$", then "buf" is used.
20918 * Returns 0 on error.
20919 */
20920 static linenr_T
20921get_tv_lnum_buf(argvars, buf)
20922 typval_T *argvars;
20923 buf_T *buf;
20924{
20925 if (argvars[0].v_type == VAR_STRING
20926 && argvars[0].vval.v_string != NULL
20927 && argvars[0].vval.v_string[0] == '$'
20928 && buf != NULL)
20929 return buf->b_ml.ml_line_count;
20930 return get_tv_number_chk(&argvars[0], NULL);
20931}
20932
20933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 * Get the string value of a variable.
20935 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020936 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20937 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 * If the String variable has never been set, return an empty string.
20939 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020940 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20941 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 */
20943 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020944get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020945 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946{
20947 static char_u mybuf[NUMBUFLEN];
20948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020949 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950}
20951
20952 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020953get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020954 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020955 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020957 char_u *res = get_tv_string_buf_chk(varp, buf);
20958
20959 return res != NULL ? res : (char_u *)"";
20960}
20961
Bram Moolenaar7d647822014-04-05 21:28:56 +020020962/*
20963 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20964 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020965 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020966get_tv_string_chk(varp)
20967 typval_T *varp;
20968{
20969 static char_u mybuf[NUMBUFLEN];
20970
20971 return get_tv_string_buf_chk(varp, mybuf);
20972}
20973
20974 static char_u *
20975get_tv_string_buf_chk(varp, buf)
20976 typval_T *varp;
20977 char_u *buf;
20978{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020979 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020981 case VAR_NUMBER:
20982 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20983 return buf;
20984 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020985 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020986 break;
20987 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020988 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020989 break;
20990 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020991 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020992 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020993#ifdef FEAT_FLOAT
20994 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020995 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020996 break;
20997#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020998 case VAR_STRING:
20999 if (varp->vval.v_string != NULL)
21000 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021001 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021002 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021003 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021004 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021006 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007}
21008
21009/*
21010 * Find variable "name" in the list of variables.
21011 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021012 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021013 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021014 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021016 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021017find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021019 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021020 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021023 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024
Bram Moolenaara7043832005-01-21 11:56:39 +000021025 ht = find_var_ht(name, &varname);
21026 if (htp != NULL)
21027 *htp = ht;
21028 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021030 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031}
21032
21033/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021034 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021035 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021037 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021038find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021039 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021040 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021041 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021042 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021043{
Bram Moolenaar33570922005-01-25 22:26:29 +000021044 hashitem_T *hi;
21045
21046 if (*varname == NUL)
21047 {
21048 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021049 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021050 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021051 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021052 case 'g': return &globvars_var;
21053 case 'v': return &vimvars_var;
21054 case 'b': return &curbuf->b_bufvar;
21055 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021056#ifdef FEAT_WINDOWS
21057 case 't': return &curtab->tp_winvar;
21058#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021059 case 'l': return current_funccal == NULL
21060 ? NULL : &current_funccal->l_vars_var;
21061 case 'a': return current_funccal == NULL
21062 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021063 }
21064 return NULL;
21065 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021066
21067 hi = hash_find(ht, varname);
21068 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021069 {
21070 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021071 * worked find the variable again. Don't auto-load a script if it was
21072 * loaded already, otherwise it would be loaded every time when
21073 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021074 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021075 {
21076 /* Note: script_autoload() may make "hi" invalid. It must either
21077 * be obtained again or not used. */
21078 if (!script_autoload(varname, FALSE) || aborting())
21079 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021080 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021081 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021082 if (HASHITEM_EMPTY(hi))
21083 return NULL;
21084 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021085 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021086}
21087
21088/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021089 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021090 * Set "varname" to the start of name without ':'.
21091 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021092 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021093find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 char_u *name;
21095 char_u **varname;
21096{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021097 hashitem_T *hi;
21098
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 if (name[1] != ':')
21100 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021101 /* The name must not start with a colon or #. */
21102 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103 return NULL;
21104 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021105
21106 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021107 hi = hash_find(&compat_hashtab, name);
21108 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021109 return &compat_hashtab;
21110
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021112 return &globvarht; /* global variable */
21113 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 }
21115 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021116 if (*name == 'g') /* global variable */
21117 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021118 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21119 */
21120 if (vim_strchr(name + 2, ':') != NULL
21121 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021122 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021124 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021126 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021127#ifdef FEAT_WINDOWS
21128 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021129 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021130#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021131 if (*name == 'v') /* v: variable */
21132 return &vimvarht;
21133 if (*name == 'a' && current_funccal != NULL) /* function argument */
21134 return &current_funccal->l_avars.dv_hashtab;
21135 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21136 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 if (*name == 's' /* script variable */
21138 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21139 return &SCRIPT_VARS(current_SID);
21140 return NULL;
21141}
21142
21143/*
21144 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021145 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 * Returns NULL when it doesn't exist.
21147 */
21148 char_u *
21149get_var_value(name)
21150 char_u *name;
21151{
Bram Moolenaar33570922005-01-25 22:26:29 +000021152 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021154 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155 if (v == NULL)
21156 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021157 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158}
21159
21160/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021161 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 * sourcing this script and when executing functions defined in the script.
21163 */
21164 void
21165new_script_vars(id)
21166 scid_T id;
21167{
Bram Moolenaara7043832005-01-21 11:56:39 +000021168 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021169 hashtab_T *ht;
21170 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021171
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21173 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021174 /* Re-allocating ga_data means that an ht_array pointing to
21175 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021176 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021177 for (i = 1; i <= ga_scripts.ga_len; ++i)
21178 {
21179 ht = &SCRIPT_VARS(i);
21180 if (ht->ht_mask == HT_INIT_SIZE - 1)
21181 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021182 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021183 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021184 }
21185
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186 while (ga_scripts.ga_len < id)
21187 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021188 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021189 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021190 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 }
21193 }
21194}
21195
21196/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021197 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21198 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 */
21200 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021201init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021202 dict_T *dict;
21203 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021204 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205{
Bram Moolenaar33570922005-01-25 22:26:29 +000021206 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021207 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021208 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021209 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021210 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021211 dict_var->di_tv.vval.v_dict = dict;
21212 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021213 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021214 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21215 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216}
21217
21218/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021219 * Unreference a dictionary initialized by init_var_dict().
21220 */
21221 void
21222unref_var_dict(dict)
21223 dict_T *dict;
21224{
21225 /* Now the dict needs to be freed if no one else is using it, go back to
21226 * normal reference counting. */
21227 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21228 dict_unref(dict);
21229}
21230
21231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021233 * Frees all allocated variables and the value they contain.
21234 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 */
21236 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021237vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021238 hashtab_T *ht;
21239{
21240 vars_clear_ext(ht, TRUE);
21241}
21242
21243/*
21244 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21245 */
21246 static void
21247vars_clear_ext(ht, free_val)
21248 hashtab_T *ht;
21249 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250{
Bram Moolenaara7043832005-01-21 11:56:39 +000021251 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021252 hashitem_T *hi;
21253 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254
Bram Moolenaar33570922005-01-25 22:26:29 +000021255 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021256 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021257 for (hi = ht->ht_array; todo > 0; ++hi)
21258 {
21259 if (!HASHITEM_EMPTY(hi))
21260 {
21261 --todo;
21262
Bram Moolenaar33570922005-01-25 22:26:29 +000021263 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021264 * ht_array might change then. hash_clear() takes care of it
21265 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021266 v = HI2DI(hi);
21267 if (free_val)
21268 clear_tv(&v->di_tv);
21269 if ((v->di_flags & DI_FLAGS_FIX) == 0)
21270 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021271 }
21272 }
21273 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021274 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275}
21276
Bram Moolenaara7043832005-01-21 11:56:39 +000021277/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021278 * Delete a variable from hashtab "ht" at item "hi".
21279 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021282delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021283 hashtab_T *ht;
21284 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285{
Bram Moolenaar33570922005-01-25 22:26:29 +000021286 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021287
21288 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021289 clear_tv(&di->di_tv);
21290 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291}
21292
21293/*
21294 * List the value of one internal variable.
21295 */
21296 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021297list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021298 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021300 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021302 char_u *tofree;
21303 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021304 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021305
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021306 current_copyID += COPYID_INC;
21307 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021308 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021309 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021310 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311}
21312
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021314list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 char_u *prefix;
21316 char_u *name;
21317 int type;
21318 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021319 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320{
Bram Moolenaar31859182007-08-14 20:41:13 +000021321 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21322 msg_start();
21323 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 if (name != NULL) /* "a:" vars don't have a name stored */
21325 msg_puts(name);
21326 msg_putchar(' ');
21327 msg_advance(22);
21328 if (type == VAR_NUMBER)
21329 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021330 else if (type == VAR_FUNC)
21331 msg_putchar('*');
21332 else if (type == VAR_LIST)
21333 {
21334 msg_putchar('[');
21335 if (*string == '[')
21336 ++string;
21337 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021338 else if (type == VAR_DICT)
21339 {
21340 msg_putchar('{');
21341 if (*string == '{')
21342 ++string;
21343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 else
21345 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021346
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021348
21349 if (type == VAR_FUNC)
21350 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021351 if (*first)
21352 {
21353 msg_clr_eos();
21354 *first = FALSE;
21355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356}
21357
21358/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021359 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 * If the variable already exists, the value is updated.
21361 * Otherwise the variable is created.
21362 */
21363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021364set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021366 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021367 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368{
Bram Moolenaar33570922005-01-25 22:26:29 +000021369 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021371 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021373 ht = find_var_ht(name, &varname);
21374 if (ht == NULL || *varname == NUL)
21375 {
21376 EMSG2(_(e_illvar), name);
21377 return;
21378 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021379 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021380
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021381 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21382 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021383
Bram Moolenaar33570922005-01-25 22:26:29 +000021384 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021386 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021387 if (var_check_ro(v->di_flags, name)
21388 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021389 return;
21390 if (v->di_tv.v_type != tv->v_type
21391 && !((v->di_tv.v_type == VAR_STRING
21392 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021393 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021394 || tv->v_type == VAR_NUMBER))
21395#ifdef FEAT_FLOAT
21396 && !((v->di_tv.v_type == VAR_NUMBER
21397 || v->di_tv.v_type == VAR_FLOAT)
21398 && (tv->v_type == VAR_NUMBER
21399 || tv->v_type == VAR_FLOAT))
21400#endif
21401 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021402 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021403 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021404 return;
21405 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021406
21407 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021408 * Handle setting internal v: variables separately: we don't change
21409 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021410 */
21411 if (ht == &vimvarht)
21412 {
21413 if (v->di_tv.v_type == VAR_STRING)
21414 {
21415 vim_free(v->di_tv.vval.v_string);
21416 if (copy || tv->v_type != VAR_STRING)
21417 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21418 else
21419 {
21420 /* Take over the string to avoid an extra alloc/free. */
21421 v->di_tv.vval.v_string = tv->vval.v_string;
21422 tv->vval.v_string = NULL;
21423 }
21424 }
21425 else if (v->di_tv.v_type != VAR_NUMBER)
21426 EMSG2(_(e_intern2), "set_var()");
21427 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021428 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021429 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021430 if (STRCMP(varname, "searchforward") == 0)
21431 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021432#ifdef FEAT_SEARCH_EXTRA
21433 else if (STRCMP(varname, "hlsearch") == 0)
21434 {
21435 no_hlsearch = !v->di_tv.vval.v_number;
21436 redraw_all_later(SOME_VALID);
21437 }
21438#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021439 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021440 return;
21441 }
21442
21443 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444 }
21445 else /* add a new variable */
21446 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021447 /* Can't add "v:" variable. */
21448 if (ht == &vimvarht)
21449 {
21450 EMSG2(_(e_illvar), name);
21451 return;
21452 }
21453
Bram Moolenaar92124a32005-06-17 22:03:40 +000021454 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021455 if (!valid_varname(varname))
21456 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021457
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021458 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21459 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021460 if (v == NULL)
21461 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021462 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021463 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021465 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466 return;
21467 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021468 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021470
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021471 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021472 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021473 else
21474 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021475 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021476 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021477 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479}
21480
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021481/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021482 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021483 * Also give an error message.
21484 */
21485 static int
21486var_check_ro(flags, name)
21487 int flags;
21488 char_u *name;
21489{
21490 if (flags & DI_FLAGS_RO)
21491 {
21492 EMSG2(_(e_readonlyvar), name);
21493 return TRUE;
21494 }
21495 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21496 {
21497 EMSG2(_(e_readonlysbx), name);
21498 return TRUE;
21499 }
21500 return FALSE;
21501}
21502
21503/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021504 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21505 * Also give an error message.
21506 */
21507 static int
21508var_check_fixed(flags, name)
21509 int flags;
21510 char_u *name;
21511{
21512 if (flags & DI_FLAGS_FIX)
21513 {
21514 EMSG2(_("E795: Cannot delete variable %s"), name);
21515 return TRUE;
21516 }
21517 return FALSE;
21518}
21519
21520/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021521 * Check if a funcref is assigned to a valid variable name.
21522 * Return TRUE and give an error if not.
21523 */
21524 static int
21525var_check_func_name(name, new_var)
21526 char_u *name; /* points to start of variable name */
21527 int new_var; /* TRUE when creating the variable */
21528{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021529 /* Allow for w: b: s: and t:. */
21530 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021531 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21532 ? name[2] : name[0]))
21533 {
21534 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21535 name);
21536 return TRUE;
21537 }
21538 /* Don't allow hiding a function. When "v" is not NULL we might be
21539 * assigning another function to the same var, the type is checked
21540 * below. */
21541 if (new_var && function_exists(name))
21542 {
21543 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21544 name);
21545 return TRUE;
21546 }
21547 return FALSE;
21548}
21549
21550/*
21551 * Check if a variable name is valid.
21552 * Return FALSE and give an error if not.
21553 */
21554 static int
21555valid_varname(varname)
21556 char_u *varname;
21557{
21558 char_u *p;
21559
21560 for (p = varname; *p != NUL; ++p)
21561 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21562 && *p != AUTOLOAD_CHAR)
21563 {
21564 EMSG2(_(e_illvar), varname);
21565 return FALSE;
21566 }
21567 return TRUE;
21568}
21569
21570/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021571 * Return TRUE if typeval "tv" is set to be locked (immutable).
21572 * Also give an error message, using "name".
21573 */
21574 static int
21575tv_check_lock(lock, name)
21576 int lock;
21577 char_u *name;
21578{
21579 if (lock & VAR_LOCKED)
21580 {
21581 EMSG2(_("E741: Value is locked: %s"),
21582 name == NULL ? (char_u *)_("Unknown") : name);
21583 return TRUE;
21584 }
21585 if (lock & VAR_FIXED)
21586 {
21587 EMSG2(_("E742: Cannot change value of %s"),
21588 name == NULL ? (char_u *)_("Unknown") : name);
21589 return TRUE;
21590 }
21591 return FALSE;
21592}
21593
21594/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021595 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021596 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021597 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021598 * It is OK for "from" and "to" to point to the same item. This is used to
21599 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021600 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021601 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021602copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021603 typval_T *from;
21604 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021606 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021607 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021608 switch (from->v_type)
21609 {
21610 case VAR_NUMBER:
21611 to->vval.v_number = from->vval.v_number;
21612 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021613#ifdef FEAT_FLOAT
21614 case VAR_FLOAT:
21615 to->vval.v_float = from->vval.v_float;
21616 break;
21617#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021618 case VAR_STRING:
21619 case VAR_FUNC:
21620 if (from->vval.v_string == NULL)
21621 to->vval.v_string = NULL;
21622 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021624 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021625 if (from->v_type == VAR_FUNC)
21626 func_ref(to->vval.v_string);
21627 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021628 break;
21629 case VAR_LIST:
21630 if (from->vval.v_list == NULL)
21631 to->vval.v_list = NULL;
21632 else
21633 {
21634 to->vval.v_list = from->vval.v_list;
21635 ++to->vval.v_list->lv_refcount;
21636 }
21637 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021638 case VAR_DICT:
21639 if (from->vval.v_dict == NULL)
21640 to->vval.v_dict = NULL;
21641 else
21642 {
21643 to->vval.v_dict = from->vval.v_dict;
21644 ++to->vval.v_dict->dv_refcount;
21645 }
21646 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021647 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021648 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021649 break;
21650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651}
21652
21653/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021654 * Make a copy of an item.
21655 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021656 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21657 * reference to an already copied list/dict can be used.
21658 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021659 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021660 static int
21661item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021662 typval_T *from;
21663 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021664 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021665 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021666{
21667 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021668 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021669
Bram Moolenaar33570922005-01-25 22:26:29 +000021670 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021671 {
21672 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021673 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021674 }
21675 ++recurse;
21676
21677 switch (from->v_type)
21678 {
21679 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021680#ifdef FEAT_FLOAT
21681 case VAR_FLOAT:
21682#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021683 case VAR_STRING:
21684 case VAR_FUNC:
21685 copy_tv(from, to);
21686 break;
21687 case VAR_LIST:
21688 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021689 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021690 if (from->vval.v_list == NULL)
21691 to->vval.v_list = NULL;
21692 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21693 {
21694 /* use the copy made earlier */
21695 to->vval.v_list = from->vval.v_list->lv_copylist;
21696 ++to->vval.v_list->lv_refcount;
21697 }
21698 else
21699 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21700 if (to->vval.v_list == NULL)
21701 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021702 break;
21703 case VAR_DICT:
21704 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021705 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021706 if (from->vval.v_dict == NULL)
21707 to->vval.v_dict = NULL;
21708 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21709 {
21710 /* use the copy made earlier */
21711 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21712 ++to->vval.v_dict->dv_refcount;
21713 }
21714 else
21715 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21716 if (to->vval.v_dict == NULL)
21717 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021718 break;
21719 default:
21720 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021721 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021722 }
21723 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021724 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021725}
21726
21727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 * ":echo expr1 ..." print each argument separated with a space, add a
21729 * newline at the end.
21730 * ":echon expr1 ..." print each argument plain.
21731 */
21732 void
21733ex_echo(eap)
21734 exarg_T *eap;
21735{
21736 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021737 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021738 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 char_u *p;
21740 int needclr = TRUE;
21741 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021742 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743
21744 if (eap->skip)
21745 ++emsg_skip;
21746 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21747 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021748 /* If eval1() causes an error message the text from the command may
21749 * still need to be cleared. E.g., "echo 22,44". */
21750 need_clr_eos = needclr;
21751
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021753 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 {
21755 /*
21756 * Report the invalid expression unless the expression evaluation
21757 * has been cancelled due to an aborting error, an interrupt, or an
21758 * exception.
21759 */
21760 if (!aborting())
21761 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021762 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 break;
21764 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021765 need_clr_eos = FALSE;
21766
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 if (!eap->skip)
21768 {
21769 if (atstart)
21770 {
21771 atstart = FALSE;
21772 /* Call msg_start() after eval1(), evaluating the expression
21773 * may cause a message to appear. */
21774 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021775 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021776 /* Mark the saved text as finishing the line, so that what
21777 * follows is displayed on a new line when scrolling back
21778 * at the more prompt. */
21779 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 }
21783 else if (eap->cmdidx == CMD_echo)
21784 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021785 current_copyID += COPYID_INC;
21786 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021787 if (p != NULL)
21788 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021790 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021792 if (*p != TAB && needclr)
21793 {
21794 /* remove any text still there from the command */
21795 msg_clr_eos();
21796 needclr = FALSE;
21797 }
21798 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799 }
21800 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021801 {
21802#ifdef FEAT_MBYTE
21803 if (has_mbyte)
21804 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021805 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021806
21807 (void)msg_outtrans_len_attr(p, i, echo_attr);
21808 p += i - 1;
21809 }
21810 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021812 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021815 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021817 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 arg = skipwhite(arg);
21819 }
21820 eap->nextcmd = check_nextcmd(arg);
21821
21822 if (eap->skip)
21823 --emsg_skip;
21824 else
21825 {
21826 /* remove text that may still be there from the command */
21827 if (needclr)
21828 msg_clr_eos();
21829 if (eap->cmdidx == CMD_echo)
21830 msg_end();
21831 }
21832}
21833
21834/*
21835 * ":echohl {name}".
21836 */
21837 void
21838ex_echohl(eap)
21839 exarg_T *eap;
21840{
21841 int id;
21842
21843 id = syn_name2id(eap->arg);
21844 if (id == 0)
21845 echo_attr = 0;
21846 else
21847 echo_attr = syn_id2attr(id);
21848}
21849
21850/*
21851 * ":execute expr1 ..." execute the result of an expression.
21852 * ":echomsg expr1 ..." Print a message
21853 * ":echoerr expr1 ..." Print an error
21854 * Each gets spaces around each argument and a newline at the end for
21855 * echo commands
21856 */
21857 void
21858ex_execute(eap)
21859 exarg_T *eap;
21860{
21861 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021862 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 int ret = OK;
21864 char_u *p;
21865 garray_T ga;
21866 int len;
21867 int save_did_emsg;
21868
21869 ga_init2(&ga, 1, 80);
21870
21871 if (eap->skip)
21872 ++emsg_skip;
21873 while (*arg != NUL && *arg != '|' && *arg != '\n')
21874 {
21875 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021876 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 {
21878 /*
21879 * Report the invalid expression unless the expression evaluation
21880 * has been cancelled due to an aborting error, an interrupt, or an
21881 * exception.
21882 */
21883 if (!aborting())
21884 EMSG2(_(e_invexpr2), p);
21885 ret = FAIL;
21886 break;
21887 }
21888
21889 if (!eap->skip)
21890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021891 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 len = (int)STRLEN(p);
21893 if (ga_grow(&ga, len + 2) == FAIL)
21894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896 ret = FAIL;
21897 break;
21898 }
21899 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 ga.ga_len += len;
21903 }
21904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021905 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 arg = skipwhite(arg);
21907 }
21908
21909 if (ret != FAIL && ga.ga_data != NULL)
21910 {
21911 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021914 out_flush();
21915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 else if (eap->cmdidx == CMD_echoerr)
21917 {
21918 /* We don't want to abort following commands, restore did_emsg. */
21919 save_did_emsg = did_emsg;
21920 EMSG((char_u *)ga.ga_data);
21921 if (!force_abort)
21922 did_emsg = save_did_emsg;
21923 }
21924 else if (eap->cmdidx == CMD_execute)
21925 do_cmdline((char_u *)ga.ga_data,
21926 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21927 }
21928
21929 ga_clear(&ga);
21930
21931 if (eap->skip)
21932 --emsg_skip;
21933
21934 eap->nextcmd = check_nextcmd(arg);
21935}
21936
21937/*
21938 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21939 * "arg" points to the "&" or '+' when called, to "option" when returning.
21940 * Returns NULL when no option name found. Otherwise pointer to the char
21941 * after the option name.
21942 */
21943 static char_u *
21944find_option_end(arg, opt_flags)
21945 char_u **arg;
21946 int *opt_flags;
21947{
21948 char_u *p = *arg;
21949
21950 ++p;
21951 if (*p == 'g' && p[1] == ':')
21952 {
21953 *opt_flags = OPT_GLOBAL;
21954 p += 2;
21955 }
21956 else if (*p == 'l' && p[1] == ':')
21957 {
21958 *opt_flags = OPT_LOCAL;
21959 p += 2;
21960 }
21961 else
21962 *opt_flags = 0;
21963
21964 if (!ASCII_ISALPHA(*p))
21965 return NULL;
21966 *arg = p;
21967
21968 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21969 p += 4; /* termcap option */
21970 else
21971 while (ASCII_ISALPHA(*p))
21972 ++p;
21973 return p;
21974}
21975
21976/*
21977 * ":function"
21978 */
21979 void
21980ex_function(eap)
21981 exarg_T *eap;
21982{
21983 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021984 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 int j;
21986 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021988 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 char_u *name = NULL;
21990 char_u *p;
21991 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021992 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 garray_T newargs;
21994 garray_T newlines;
21995 int varargs = FALSE;
21996 int mustend = FALSE;
21997 int flags = 0;
21998 ufunc_T *fp;
21999 int indent;
22000 int nesting;
22001 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022002 dictitem_T *v;
22003 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022004 static int func_nr = 0; /* number for nameless function */
22005 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022006 hashtab_T *ht;
22007 int todo;
22008 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022009 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010
22011 /*
22012 * ":function" without argument: list functions.
22013 */
22014 if (ends_excmd(*eap->arg))
22015 {
22016 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022017 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022018 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022019 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022020 {
22021 if (!HASHITEM_EMPTY(hi))
22022 {
22023 --todo;
22024 fp = HI2UF(hi);
22025 if (!isdigit(*fp->uf_name))
22026 list_func_head(fp, FALSE);
22027 }
22028 }
22029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 eap->nextcmd = check_nextcmd(eap->arg);
22031 return;
22032 }
22033
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022034 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022035 * ":function /pat": list functions matching pattern.
22036 */
22037 if (*eap->arg == '/')
22038 {
22039 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22040 if (!eap->skip)
22041 {
22042 regmatch_T regmatch;
22043
22044 c = *p;
22045 *p = NUL;
22046 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22047 *p = c;
22048 if (regmatch.regprog != NULL)
22049 {
22050 regmatch.rm_ic = p_ic;
22051
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022052 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022053 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22054 {
22055 if (!HASHITEM_EMPTY(hi))
22056 {
22057 --todo;
22058 fp = HI2UF(hi);
22059 if (!isdigit(*fp->uf_name)
22060 && vim_regexec(&regmatch, fp->uf_name, 0))
22061 list_func_head(fp, FALSE);
22062 }
22063 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022064 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022065 }
22066 }
22067 if (*p == '/')
22068 ++p;
22069 eap->nextcmd = check_nextcmd(p);
22070 return;
22071 }
22072
22073 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022074 * Get the function name. There are these situations:
22075 * func normal function name
22076 * "name" == func, "fudi.fd_dict" == NULL
22077 * dict.func new dictionary entry
22078 * "name" == NULL, "fudi.fd_dict" set,
22079 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22080 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022081 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022082 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22083 * dict.func existing dict entry that's not a Funcref
22084 * "name" == NULL, "fudi.fd_dict" set,
22085 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022086 * s:func script-local function name
22087 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022090 name = trans_function_name(&p, eap->skip, 0, &fudi);
22091 paren = (vim_strchr(p, '(') != NULL);
22092 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093 {
22094 /*
22095 * Return on an invalid expression in braces, unless the expression
22096 * evaluation has been cancelled due to an aborting error, an
22097 * interrupt, or an exception.
22098 */
22099 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022101 if (!eap->skip && fudi.fd_newkey != NULL)
22102 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022103 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106 else
22107 eap->skip = TRUE;
22108 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022109
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 /* An error in a function call during evaluation of an expression in magic
22111 * braces should not cause the function not to be defined. */
22112 saved_did_emsg = did_emsg;
22113 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114
22115 /*
22116 * ":function func" with only function name: list function.
22117 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022118 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 {
22120 if (!ends_excmd(*skipwhite(p)))
22121 {
22122 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022123 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124 }
22125 eap->nextcmd = check_nextcmd(p);
22126 if (eap->nextcmd != NULL)
22127 *p = NUL;
22128 if (!eap->skip && !got_int)
22129 {
22130 fp = find_func(name);
22131 if (fp != NULL)
22132 {
22133 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022134 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022136 if (FUNCLINE(fp, j) == NULL)
22137 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138 msg_putchar('\n');
22139 msg_outnum((long)(j + 1));
22140 if (j < 9)
22141 msg_putchar(' ');
22142 if (j < 99)
22143 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022144 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 out_flush(); /* show a line at a time */
22146 ui_breakcheck();
22147 }
22148 if (!got_int)
22149 {
22150 msg_putchar('\n');
22151 msg_puts((char_u *)" endfunction");
22152 }
22153 }
22154 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022155 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022157 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 }
22159
22160 /*
22161 * ":function name(arg1, arg2)" Define function.
22162 */
22163 p = skipwhite(p);
22164 if (*p != '(')
22165 {
22166 if (!eap->skip)
22167 {
22168 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022169 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 }
22171 /* attempt to continue by skipping some text */
22172 if (vim_strchr(p, '(') != NULL)
22173 p = vim_strchr(p, '(');
22174 }
22175 p = skipwhite(p + 1);
22176
22177 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22178 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22179
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022180 if (!eap->skip)
22181 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022182 /* Check the name of the function. Unless it's a dictionary function
22183 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022184 if (name != NULL)
22185 arg = name;
22186 else
22187 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022188 if (arg != NULL && (fudi.fd_di == NULL
22189 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022190 {
22191 if (*arg == K_SPECIAL)
22192 j = 3;
22193 else
22194 j = 0;
22195 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22196 : eval_isnamec(arg[j])))
22197 ++j;
22198 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022199 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022200 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022201 /* Disallow using the g: dict. */
22202 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22203 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022204 }
22205
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 /*
22207 * Isolate the arguments: "arg1, arg2, ...)"
22208 */
22209 while (*p != ')')
22210 {
22211 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22212 {
22213 varargs = TRUE;
22214 p += 3;
22215 mustend = TRUE;
22216 }
22217 else
22218 {
22219 arg = p;
22220 while (ASCII_ISALNUM(*p) || *p == '_')
22221 ++p;
22222 if (arg == p || isdigit(*arg)
22223 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22224 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22225 {
22226 if (!eap->skip)
22227 EMSG2(_("E125: Illegal argument: %s"), arg);
22228 break;
22229 }
22230 if (ga_grow(&newargs, 1) == FAIL)
22231 goto erret;
22232 c = *p;
22233 *p = NUL;
22234 arg = vim_strsave(arg);
22235 if (arg == NULL)
22236 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022237
22238 /* Check for duplicate argument name. */
22239 for (i = 0; i < newargs.ga_len; ++i)
22240 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22241 {
22242 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022243 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022244 goto erret;
22245 }
22246
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22248 *p = c;
22249 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250 if (*p == ',')
22251 ++p;
22252 else
22253 mustend = TRUE;
22254 }
22255 p = skipwhite(p);
22256 if (mustend && *p != ')')
22257 {
22258 if (!eap->skip)
22259 EMSG2(_(e_invarg2), eap->arg);
22260 break;
22261 }
22262 }
22263 ++p; /* skip the ')' */
22264
Bram Moolenaare9a41262005-01-15 22:18:47 +000022265 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266 for (;;)
22267 {
22268 p = skipwhite(p);
22269 if (STRNCMP(p, "range", 5) == 0)
22270 {
22271 flags |= FC_RANGE;
22272 p += 5;
22273 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022274 else if (STRNCMP(p, "dict", 4) == 0)
22275 {
22276 flags |= FC_DICT;
22277 p += 4;
22278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279 else if (STRNCMP(p, "abort", 5) == 0)
22280 {
22281 flags |= FC_ABORT;
22282 p += 5;
22283 }
22284 else
22285 break;
22286 }
22287
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022288 /* When there is a line break use what follows for the function body.
22289 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22290 if (*p == '\n')
22291 line_arg = p + 1;
22292 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293 EMSG(_(e_trailing));
22294
22295 /*
22296 * Read the body of the function, until ":endfunction" is found.
22297 */
22298 if (KeyTyped)
22299 {
22300 /* Check if the function already exists, don't let the user type the
22301 * whole function before telling him it doesn't work! For a script we
22302 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022303 if (!eap->skip && !eap->forceit)
22304 {
22305 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22306 EMSG(_(e_funcdict));
22307 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022308 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022311 if (!eap->skip && did_emsg)
22312 goto erret;
22313
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 msg_putchar('\n'); /* don't overwrite the function name */
22315 cmdline_row = msg_row;
22316 }
22317
22318 indent = 2;
22319 nesting = 0;
22320 for (;;)
22321 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022322 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022323 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022324 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022325 saved_wait_return = FALSE;
22326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022328 sourcing_lnum_off = sourcing_lnum;
22329
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022330 if (line_arg != NULL)
22331 {
22332 /* Use eap->arg, split up in parts by line breaks. */
22333 theline = line_arg;
22334 p = vim_strchr(theline, '\n');
22335 if (p == NULL)
22336 line_arg += STRLEN(line_arg);
22337 else
22338 {
22339 *p = NUL;
22340 line_arg = p + 1;
22341 }
22342 }
22343 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344 theline = getcmdline(':', 0L, indent);
22345 else
22346 theline = eap->getline(':', eap->cookie, indent);
22347 if (KeyTyped)
22348 lines_left = Rows - 1;
22349 if (theline == NULL)
22350 {
22351 EMSG(_("E126: Missing :endfunction"));
22352 goto erret;
22353 }
22354
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022355 /* Detect line continuation: sourcing_lnum increased more than one. */
22356 if (sourcing_lnum > sourcing_lnum_off + 1)
22357 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22358 else
22359 sourcing_lnum_off = 0;
22360
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 if (skip_until != NULL)
22362 {
22363 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22364 * don't check for ":endfunc". */
22365 if (STRCMP(theline, skip_until) == 0)
22366 {
22367 vim_free(skip_until);
22368 skip_until = NULL;
22369 }
22370 }
22371 else
22372 {
22373 /* skip ':' and blanks*/
22374 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22375 ;
22376
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022377 /* Check for "endfunction". */
22378 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022380 if (line_arg == NULL)
22381 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 break;
22383 }
22384
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022385 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 * at "end". */
22387 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22388 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022389 else if (STRNCMP(p, "if", 2) == 0
22390 || STRNCMP(p, "wh", 2) == 0
22391 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 || STRNCMP(p, "try", 3) == 0)
22393 indent += 2;
22394
22395 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022396 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022398 if (*p == '!')
22399 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022401 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22402 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022404 ++nesting;
22405 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 }
22407 }
22408
22409 /* Check for ":append" or ":insert". */
22410 p = skip_range(p, NULL);
22411 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22412 || (p[0] == 'i'
22413 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22414 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22415 skip_until = vim_strsave((char_u *)".");
22416
22417 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22418 arg = skipwhite(skiptowhite(p));
22419 if (arg[0] == '<' && arg[1] =='<'
22420 && ((p[0] == 'p' && p[1] == 'y'
22421 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22422 || (p[0] == 'p' && p[1] == 'e'
22423 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22424 || (p[0] == 't' && p[1] == 'c'
22425 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022426 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22427 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22429 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022430 || (p[0] == 'm' && p[1] == 'z'
22431 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 ))
22433 {
22434 /* ":python <<" continues until a dot, like ":append" */
22435 p = skipwhite(arg + 2);
22436 if (*p == NUL)
22437 skip_until = vim_strsave((char_u *)".");
22438 else
22439 skip_until = vim_strsave(p);
22440 }
22441 }
22442
22443 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022444 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022445 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022446 if (line_arg == NULL)
22447 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022449 }
22450
22451 /* Copy the line to newly allocated memory. get_one_sourceline()
22452 * allocates 250 bytes per line, this saves 80% on average. The cost
22453 * is an extra alloc/free. */
22454 p = vim_strsave(theline);
22455 if (p != NULL)
22456 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022457 if (line_arg == NULL)
22458 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022459 theline = p;
22460 }
22461
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022462 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22463
22464 /* Add NULL lines for continuation lines, so that the line count is
22465 * equal to the index in the growarray. */
22466 while (sourcing_lnum_off-- > 0)
22467 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022468
22469 /* Check for end of eap->arg. */
22470 if (line_arg != NULL && *line_arg == NUL)
22471 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 }
22473
22474 /* Don't define the function when skipping commands or when an error was
22475 * detected. */
22476 if (eap->skip || did_emsg)
22477 goto erret;
22478
22479 /*
22480 * If there are no errors, add the function
22481 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022482 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022483 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022484 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022485 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022486 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022487 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022488 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022489 goto erret;
22490 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022491
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022492 fp = find_func(name);
22493 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022495 if (!eap->forceit)
22496 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022497 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022498 goto erret;
22499 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022500 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022501 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022502 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022503 name);
22504 goto erret;
22505 }
22506 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022507 ga_clear_strings(&(fp->uf_args));
22508 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022509 vim_free(name);
22510 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 }
22513 else
22514 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022515 char numbuf[20];
22516
22517 fp = NULL;
22518 if (fudi.fd_newkey == NULL && !eap->forceit)
22519 {
22520 EMSG(_(e_funcdict));
22521 goto erret;
22522 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022523 if (fudi.fd_di == NULL)
22524 {
22525 /* Can't add a function to a locked dictionary */
22526 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22527 goto erret;
22528 }
22529 /* Can't change an existing function if it is locked */
22530 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22531 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022532
22533 /* Give the function a sequential number. Can only be used with a
22534 * Funcref! */
22535 vim_free(name);
22536 sprintf(numbuf, "%d", ++func_nr);
22537 name = vim_strsave((char_u *)numbuf);
22538 if (name == NULL)
22539 goto erret;
22540 }
22541
22542 if (fp == NULL)
22543 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022544 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022545 {
22546 int slen, plen;
22547 char_u *scriptname;
22548
22549 /* Check that the autoload name matches the script name. */
22550 j = FAIL;
22551 if (sourcing_name != NULL)
22552 {
22553 scriptname = autoload_name(name);
22554 if (scriptname != NULL)
22555 {
22556 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022557 plen = (int)STRLEN(p);
22558 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022559 if (slen > plen && fnamecmp(p,
22560 sourcing_name + slen - plen) == 0)
22561 j = OK;
22562 vim_free(scriptname);
22563 }
22564 }
22565 if (j == FAIL)
22566 {
22567 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22568 goto erret;
22569 }
22570 }
22571
22572 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 if (fp == NULL)
22574 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022575
22576 if (fudi.fd_dict != NULL)
22577 {
22578 if (fudi.fd_di == NULL)
22579 {
22580 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022581 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022582 if (fudi.fd_di == NULL)
22583 {
22584 vim_free(fp);
22585 goto erret;
22586 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022587 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22588 {
22589 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022590 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022591 goto erret;
22592 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022593 }
22594 else
22595 /* overwrite existing dict entry */
22596 clear_tv(&fudi.fd_di->di_tv);
22597 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022598 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022599 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022600 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022601
22602 /* behave like "dict" was used */
22603 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022604 }
22605
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022607 STRCPY(fp->uf_name, name);
22608 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022610 fp->uf_args = newargs;
22611 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022612#ifdef FEAT_PROFILE
22613 fp->uf_tml_count = NULL;
22614 fp->uf_tml_total = NULL;
22615 fp->uf_tml_self = NULL;
22616 fp->uf_profiling = FALSE;
22617 if (prof_def_func())
22618 func_do_profile(fp);
22619#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022620 fp->uf_varargs = varargs;
22621 fp->uf_flags = flags;
22622 fp->uf_calls = 0;
22623 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022624 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625
22626erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 ga_clear_strings(&newargs);
22628 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022629ret_free:
22630 vim_free(skip_until);
22631 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022634 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635}
22636
22637/*
22638 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022639 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022641 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022642 * TFN_INT: internal function name OK
22643 * TFN_QUIET: be quiet
22644 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 * Advances "pp" to just after the function name (if no error).
22646 */
22647 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022648trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 char_u **pp;
22650 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022651 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022652 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022654 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 char_u *start;
22656 char_u *end;
22657 int lead;
22658 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022660 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022661
22662 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022663 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022665
22666 /* Check for hard coded <SNR>: already translated function ID (from a user
22667 * command). */
22668 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22669 && (*pp)[2] == (int)KE_SNR)
22670 {
22671 *pp += 3;
22672 len = get_id_len(pp) + 3;
22673 return vim_strnsave(start, len);
22674 }
22675
22676 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22677 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022679 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022681
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022682 /* Note that TFN_ flags use the same values as GLV_ flags. */
22683 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022684 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022685 if (end == start)
22686 {
22687 if (!skip)
22688 EMSG(_("E129: Function name required"));
22689 goto theend;
22690 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022691 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022692 {
22693 /*
22694 * Report an invalid expression in braces, unless the expression
22695 * evaluation has been cancelled due to an aborting error, an
22696 * interrupt, or an exception.
22697 */
22698 if (!aborting())
22699 {
22700 if (end != NULL)
22701 EMSG2(_(e_invarg2), start);
22702 }
22703 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022704 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022705 goto theend;
22706 }
22707
22708 if (lv.ll_tv != NULL)
22709 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022710 if (fdp != NULL)
22711 {
22712 fdp->fd_dict = lv.ll_dict;
22713 fdp->fd_newkey = lv.ll_newkey;
22714 lv.ll_newkey = NULL;
22715 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022716 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022717 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22718 {
22719 name = vim_strsave(lv.ll_tv->vval.v_string);
22720 *pp = end;
22721 }
22722 else
22723 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022724 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22725 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022726 EMSG(_(e_funcref));
22727 else
22728 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022729 name = NULL;
22730 }
22731 goto theend;
22732 }
22733
22734 if (lv.ll_name == NULL)
22735 {
22736 /* Error found, but continue after the function name. */
22737 *pp = end;
22738 goto theend;
22739 }
22740
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022741 /* Check if the name is a Funcref. If so, use the value. */
22742 if (lv.ll_exp_name != NULL)
22743 {
22744 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022745 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022746 if (name == lv.ll_exp_name)
22747 name = NULL;
22748 }
22749 else
22750 {
22751 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022752 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022753 if (name == *pp)
22754 name = NULL;
22755 }
22756 if (name != NULL)
22757 {
22758 name = vim_strsave(name);
22759 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022760 if (STRNCMP(name, "<SNR>", 5) == 0)
22761 {
22762 /* Change "<SNR>" to the byte sequence. */
22763 name[0] = K_SPECIAL;
22764 name[1] = KS_EXTRA;
22765 name[2] = (int)KE_SNR;
22766 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22767 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022768 goto theend;
22769 }
22770
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022771 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022772 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022773 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022774 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22775 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22776 {
22777 /* When there was "s:" already or the name expanded to get a
22778 * leading "s:" then remove it. */
22779 lv.ll_name += 2;
22780 len -= 2;
22781 lead = 2;
22782 }
22783 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022784 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022785 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022786 /* skip over "s:" and "g:" */
22787 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022788 lv.ll_name += 2;
22789 len = (int)(end - lv.ll_name);
22790 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022791
22792 /*
22793 * Copy the function name to allocated memory.
22794 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22795 * Accept <SNR>123_name() outside a script.
22796 */
22797 if (skip)
22798 lead = 0; /* do nothing */
22799 else if (lead > 0)
22800 {
22801 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022802 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22803 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022804 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022805 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022806 if (current_SID <= 0)
22807 {
22808 EMSG(_(e_usingsid));
22809 goto theend;
22810 }
22811 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22812 lead += (int)STRLEN(sid_buf);
22813 }
22814 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022815 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022816 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022817 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022818 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022819 goto theend;
22820 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022821 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022822 {
22823 char_u *cp = vim_strchr(lv.ll_name, ':');
22824
22825 if (cp != NULL && cp < end)
22826 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022827 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022828 goto theend;
22829 }
22830 }
22831
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022832 name = alloc((unsigned)(len + lead + 1));
22833 if (name != NULL)
22834 {
22835 if (lead > 0)
22836 {
22837 name[0] = K_SPECIAL;
22838 name[1] = KS_EXTRA;
22839 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022840 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022841 STRCPY(name + 3, sid_buf);
22842 }
22843 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022844 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022845 }
22846 *pp = end;
22847
22848theend:
22849 clear_lval(&lv);
22850 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851}
22852
22853/*
22854 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22855 * Return 2 if "p" starts with "s:".
22856 * Return 0 otherwise.
22857 */
22858 static int
22859eval_fname_script(p)
22860 char_u *p;
22861{
22862 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22863 || STRNICMP(p + 1, "SNR>", 4) == 0))
22864 return 5;
22865 if (p[0] == 's' && p[1] == ':')
22866 return 2;
22867 return 0;
22868}
22869
22870/*
22871 * Return TRUE if "p" starts with "<SID>" or "s:".
22872 * Only works if eval_fname_script() returned non-zero for "p"!
22873 */
22874 static int
22875eval_fname_sid(p)
22876 char_u *p;
22877{
22878 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22879}
22880
22881/*
22882 * List the head of the function: "name(arg1, arg2)".
22883 */
22884 static void
22885list_func_head(fp, indent)
22886 ufunc_T *fp;
22887 int indent;
22888{
22889 int j;
22890
22891 msg_start();
22892 if (indent)
22893 MSG_PUTS(" ");
22894 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022895 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 {
22897 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022898 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 }
22900 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022901 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022903 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 {
22905 if (j)
22906 MSG_PUTS(", ");
22907 msg_puts(FUNCARG(fp, j));
22908 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022909 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 {
22911 if (j)
22912 MSG_PUTS(", ");
22913 MSG_PUTS("...");
22914 }
22915 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022916 if (fp->uf_flags & FC_ABORT)
22917 MSG_PUTS(" abort");
22918 if (fp->uf_flags & FC_RANGE)
22919 MSG_PUTS(" range");
22920 if (fp->uf_flags & FC_DICT)
22921 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022922 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022923 if (p_verbose > 0)
22924 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925}
22926
22927/*
22928 * Find a function by name, return pointer to it in ufuncs.
22929 * Return NULL for unknown function.
22930 */
22931 static ufunc_T *
22932find_func(name)
22933 char_u *name;
22934{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022935 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022937 hi = hash_find(&func_hashtab, name);
22938 if (!HASHITEM_EMPTY(hi))
22939 return HI2UF(hi);
22940 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941}
22942
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022943#if defined(EXITFREE) || defined(PROTO)
22944 void
22945free_all_functions()
22946{
22947 hashitem_T *hi;
22948
22949 /* Need to start all over every time, because func_free() may change the
22950 * hash table. */
22951 while (func_hashtab.ht_used > 0)
22952 for (hi = func_hashtab.ht_array; ; ++hi)
22953 if (!HASHITEM_EMPTY(hi))
22954 {
22955 func_free(HI2UF(hi));
22956 break;
22957 }
22958}
22959#endif
22960
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022961 int
22962translated_function_exists(name)
22963 char_u *name;
22964{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022965 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022966 return find_internal_func(name) >= 0;
22967 return find_func(name) != NULL;
22968}
22969
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022970/*
22971 * Return TRUE if a function "name" exists.
22972 */
22973 static int
22974function_exists(name)
22975 char_u *name;
22976{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022977 char_u *nm = name;
22978 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022979 int n = FALSE;
22980
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022981 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22982 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022983 nm = skipwhite(nm);
22984
22985 /* Only accept "funcname", "funcname ", "funcname (..." and
22986 * "funcname(...", not "funcname!...". */
22987 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022988 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022989 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022990 return n;
22991}
22992
Bram Moolenaara1544c02013-05-30 12:35:52 +020022993 char_u *
22994get_expanded_name(name, check)
22995 char_u *name;
22996 int check;
22997{
22998 char_u *nm = name;
22999 char_u *p;
23000
23001 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23002
23003 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023004 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023005 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023006
Bram Moolenaara1544c02013-05-30 12:35:52 +020023007 vim_free(p);
23008 return NULL;
23009}
23010
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023011/*
23012 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023013 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23014 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023015 */
23016 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023017builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023018 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023019 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023020{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023021 char_u *p;
23022
23023 if (!ASCII_ISLOWER(name[0]))
23024 return FALSE;
23025 p = vim_strchr(name, AUTOLOAD_CHAR);
23026 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023027}
23028
Bram Moolenaar05159a02005-02-26 23:04:13 +000023029#if defined(FEAT_PROFILE) || defined(PROTO)
23030/*
23031 * Start profiling function "fp".
23032 */
23033 static void
23034func_do_profile(fp)
23035 ufunc_T *fp;
23036{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023037 int len = fp->uf_lines.ga_len;
23038
23039 if (len == 0)
23040 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023041 fp->uf_tm_count = 0;
23042 profile_zero(&fp->uf_tm_self);
23043 profile_zero(&fp->uf_tm_total);
23044 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023045 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023046 if (fp->uf_tml_total == NULL)
23047 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023048 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023049 if (fp->uf_tml_self == NULL)
23050 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023051 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023052 fp->uf_tml_idx = -1;
23053 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23054 || fp->uf_tml_self == NULL)
23055 return; /* out of memory */
23056
23057 fp->uf_profiling = TRUE;
23058}
23059
23060/*
23061 * Dump the profiling results for all functions in file "fd".
23062 */
23063 void
23064func_dump_profile(fd)
23065 FILE *fd;
23066{
23067 hashitem_T *hi;
23068 int todo;
23069 ufunc_T *fp;
23070 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023071 ufunc_T **sorttab;
23072 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023073
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023074 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023075 if (todo == 0)
23076 return; /* nothing to dump */
23077
Bram Moolenaar73830342005-02-28 22:48:19 +000023078 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
23079
Bram Moolenaar05159a02005-02-26 23:04:13 +000023080 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23081 {
23082 if (!HASHITEM_EMPTY(hi))
23083 {
23084 --todo;
23085 fp = HI2UF(hi);
23086 if (fp->uf_profiling)
23087 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023088 if (sorttab != NULL)
23089 sorttab[st_len++] = fp;
23090
Bram Moolenaar05159a02005-02-26 23:04:13 +000023091 if (fp->uf_name[0] == K_SPECIAL)
23092 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23093 else
23094 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23095 if (fp->uf_tm_count == 1)
23096 fprintf(fd, "Called 1 time\n");
23097 else
23098 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23099 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23100 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23101 fprintf(fd, "\n");
23102 fprintf(fd, "count total (s) self (s)\n");
23103
23104 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23105 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023106 if (FUNCLINE(fp, i) == NULL)
23107 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023108 prof_func_line(fd, fp->uf_tml_count[i],
23109 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023110 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23111 }
23112 fprintf(fd, "\n");
23113 }
23114 }
23115 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023116
23117 if (sorttab != NULL && st_len > 0)
23118 {
23119 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23120 prof_total_cmp);
23121 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23122 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23123 prof_self_cmp);
23124 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23125 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023126
23127 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023128}
Bram Moolenaar73830342005-02-28 22:48:19 +000023129
23130 static void
23131prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23132 FILE *fd;
23133 ufunc_T **sorttab;
23134 int st_len;
23135 char *title;
23136 int prefer_self; /* when equal print only self time */
23137{
23138 int i;
23139 ufunc_T *fp;
23140
23141 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23142 fprintf(fd, "count total (s) self (s) function\n");
23143 for (i = 0; i < 20 && i < st_len; ++i)
23144 {
23145 fp = sorttab[i];
23146 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23147 prefer_self);
23148 if (fp->uf_name[0] == K_SPECIAL)
23149 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23150 else
23151 fprintf(fd, " %s()\n", fp->uf_name);
23152 }
23153 fprintf(fd, "\n");
23154}
23155
23156/*
23157 * Print the count and times for one function or function line.
23158 */
23159 static void
23160prof_func_line(fd, count, total, self, prefer_self)
23161 FILE *fd;
23162 int count;
23163 proftime_T *total;
23164 proftime_T *self;
23165 int prefer_self; /* when equal print only self time */
23166{
23167 if (count > 0)
23168 {
23169 fprintf(fd, "%5d ", count);
23170 if (prefer_self && profile_equal(total, self))
23171 fprintf(fd, " ");
23172 else
23173 fprintf(fd, "%s ", profile_msg(total));
23174 if (!prefer_self && profile_equal(total, self))
23175 fprintf(fd, " ");
23176 else
23177 fprintf(fd, "%s ", profile_msg(self));
23178 }
23179 else
23180 fprintf(fd, " ");
23181}
23182
23183/*
23184 * Compare function for total time sorting.
23185 */
23186 static int
23187#ifdef __BORLANDC__
23188_RTLENTRYF
23189#endif
23190prof_total_cmp(s1, s2)
23191 const void *s1;
23192 const void *s2;
23193{
23194 ufunc_T *p1, *p2;
23195
23196 p1 = *(ufunc_T **)s1;
23197 p2 = *(ufunc_T **)s2;
23198 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23199}
23200
23201/*
23202 * Compare function for self time sorting.
23203 */
23204 static int
23205#ifdef __BORLANDC__
23206_RTLENTRYF
23207#endif
23208prof_self_cmp(s1, s2)
23209 const void *s1;
23210 const void *s2;
23211{
23212 ufunc_T *p1, *p2;
23213
23214 p1 = *(ufunc_T **)s1;
23215 p2 = *(ufunc_T **)s2;
23216 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23217}
23218
Bram Moolenaar05159a02005-02-26 23:04:13 +000023219#endif
23220
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023221/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023222 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023223 * Return TRUE if a package was loaded.
23224 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023225 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023226script_autoload(name, reload)
23227 char_u *name;
23228 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023229{
23230 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023231 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023232 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023233 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023234
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023235 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023236 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023237 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023238 return FALSE;
23239
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023240 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023241
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023242 /* Find the name in the list of previously loaded package names. Skip
23243 * "autoload/", it's always the same. */
23244 for (i = 0; i < ga_loaded.ga_len; ++i)
23245 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23246 break;
23247 if (!reload && i < ga_loaded.ga_len)
23248 ret = FALSE; /* was loaded already */
23249 else
23250 {
23251 /* Remember the name if it wasn't loaded already. */
23252 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23253 {
23254 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23255 tofree = NULL;
23256 }
23257
23258 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023259 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023260 ret = TRUE;
23261 }
23262
23263 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023264 return ret;
23265}
23266
23267/*
23268 * Return the autoload script name for a function or variable name.
23269 * Returns NULL when out of memory.
23270 */
23271 static char_u *
23272autoload_name(name)
23273 char_u *name;
23274{
23275 char_u *p;
23276 char_u *scriptname;
23277
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023278 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023279 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23280 if (scriptname == NULL)
23281 return FALSE;
23282 STRCPY(scriptname, "autoload/");
23283 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023284 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023285 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023286 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023287 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023288 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023289}
23290
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23292
23293/*
23294 * Function given to ExpandGeneric() to obtain the list of user defined
23295 * function names.
23296 */
23297 char_u *
23298get_user_func_name(xp, idx)
23299 expand_T *xp;
23300 int idx;
23301{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023302 static long_u done;
23303 static hashitem_T *hi;
23304 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305
23306 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023308 done = 0;
23309 hi = func_hashtab.ht_array;
23310 }
23311 if (done < func_hashtab.ht_used)
23312 {
23313 if (done++ > 0)
23314 ++hi;
23315 while (HASHITEM_EMPTY(hi))
23316 ++hi;
23317 fp = HI2UF(hi);
23318
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023319 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023320 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023321
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023322 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23323 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324
23325 cat_func_name(IObuff, fp);
23326 if (xp->xp_context != EXPAND_USER_FUNC)
23327 {
23328 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023329 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 STRCAT(IObuff, ")");
23331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023332 return IObuff;
23333 }
23334 return NULL;
23335}
23336
23337#endif /* FEAT_CMDL_COMPL */
23338
23339/*
23340 * Copy the function name of "fp" to buffer "buf".
23341 * "buf" must be able to hold the function name plus three bytes.
23342 * Takes care of script-local function names.
23343 */
23344 static void
23345cat_func_name(buf, fp)
23346 char_u *buf;
23347 ufunc_T *fp;
23348{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023349 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350 {
23351 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023352 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353 }
23354 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023355 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356}
23357
23358/*
23359 * ":delfunction {name}"
23360 */
23361 void
23362ex_delfunction(eap)
23363 exarg_T *eap;
23364{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023365 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 char_u *p;
23367 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023368 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369
23370 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023371 name = trans_function_name(&p, eap->skip, 0, &fudi);
23372 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023373 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023374 {
23375 if (fudi.fd_dict != NULL && !eap->skip)
23376 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 if (!ends_excmd(*skipwhite(p)))
23380 {
23381 vim_free(name);
23382 EMSG(_(e_trailing));
23383 return;
23384 }
23385 eap->nextcmd = check_nextcmd(p);
23386 if (eap->nextcmd != NULL)
23387 *p = NUL;
23388
23389 if (!eap->skip)
23390 fp = find_func(name);
23391 vim_free(name);
23392
23393 if (!eap->skip)
23394 {
23395 if (fp == NULL)
23396 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023397 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398 return;
23399 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023400 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 {
23402 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23403 return;
23404 }
23405
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023406 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023408 /* Delete the dict item that refers to the function, it will
23409 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023410 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023412 else
23413 func_free(fp);
23414 }
23415}
23416
23417/*
23418 * Free a function and remove it from the list of functions.
23419 */
23420 static void
23421func_free(fp)
23422 ufunc_T *fp;
23423{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023424 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023425
23426 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023427 ga_clear_strings(&(fp->uf_args));
23428 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023429#ifdef FEAT_PROFILE
23430 vim_free(fp->uf_tml_count);
23431 vim_free(fp->uf_tml_total);
23432 vim_free(fp->uf_tml_self);
23433#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023434
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023435 /* remove the function from the function hashtable */
23436 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23437 if (HASHITEM_EMPTY(hi))
23438 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023439 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023440 hash_remove(&func_hashtab, hi);
23441
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023442 vim_free(fp);
23443}
23444
23445/*
23446 * Unreference a Function: decrement the reference count and free it when it
23447 * becomes zero. Only for numbered functions.
23448 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023449 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023450func_unref(name)
23451 char_u *name;
23452{
23453 ufunc_T *fp;
23454
23455 if (name != NULL && isdigit(*name))
23456 {
23457 fp = find_func(name);
23458 if (fp == NULL)
23459 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023460 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023461 {
23462 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023463 * when "uf_calls" becomes zero. */
23464 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023465 func_free(fp);
23466 }
23467 }
23468}
23469
23470/*
23471 * Count a reference to a Function.
23472 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023473 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023474func_ref(name)
23475 char_u *name;
23476{
23477 ufunc_T *fp;
23478
23479 if (name != NULL && isdigit(*name))
23480 {
23481 fp = find_func(name);
23482 if (fp == NULL)
23483 EMSG2(_(e_intern2), "func_ref()");
23484 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023485 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 }
23487}
23488
23489/*
23490 * Call a user function.
23491 */
23492 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023493call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494 ufunc_T *fp; /* pointer to function */
23495 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023496 typval_T *argvars; /* arguments */
23497 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498 linenr_T firstline; /* first line of range */
23499 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023500 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501{
Bram Moolenaar33570922005-01-25 22:26:29 +000023502 char_u *save_sourcing_name;
23503 linenr_T save_sourcing_lnum;
23504 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023505 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023506 int save_did_emsg;
23507 static int depth = 0;
23508 dictitem_T *v;
23509 int fixvar_idx = 0; /* index in fixvar[] */
23510 int i;
23511 int ai;
23512 char_u numbuf[NUMBUFLEN];
23513 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023514#ifdef FEAT_PROFILE
23515 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023516 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518
23519 /* If depth of calling is getting too high, don't execute the function */
23520 if (depth >= p_mfd)
23521 {
23522 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023523 rettv->v_type = VAR_NUMBER;
23524 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525 return;
23526 }
23527 ++depth;
23528
23529 line_breakcheck(); /* check for CTRL-C hit */
23530
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023531 fc = (funccall_T *)alloc(sizeof(funccall_T));
23532 fc->caller = current_funccal;
23533 current_funccal = fc;
23534 fc->func = fp;
23535 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023536 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023537 fc->linenr = 0;
23538 fc->returned = FALSE;
23539 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023541 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23542 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543
Bram Moolenaar33570922005-01-25 22:26:29 +000023544 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023545 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023546 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23547 * each argument variable and saves a lot of time.
23548 */
23549 /*
23550 * Init l: variables.
23551 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023552 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023553 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023554 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023555 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23556 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023557 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023558 name = v->di_key;
23559 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023560 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023561 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023562 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023563 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023564 v->di_tv.vval.v_dict = selfdict;
23565 ++selfdict->dv_refcount;
23566 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023567
Bram Moolenaar33570922005-01-25 22:26:29 +000023568 /*
23569 * Init a: variables.
23570 * Set a:0 to "argcount".
23571 * Set a:000 to a list with room for the "..." arguments.
23572 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023573 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023574 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023575 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023576 /* Use "name" to avoid a warning from some compiler that checks the
23577 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023578 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023579 name = v->di_key;
23580 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023581 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023582 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023583 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023584 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023585 v->di_tv.vval.v_list = &fc->l_varlist;
23586 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23587 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23588 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023589
23590 /*
23591 * Set a:firstline to "firstline" and a:lastline to "lastline".
23592 * Set a:name to named arguments.
23593 * Set a:N to the "..." arguments.
23594 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023595 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023596 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023597 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023598 (varnumber_T)lastline);
23599 for (i = 0; i < argcount; ++i)
23600 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023601 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023602 if (ai < 0)
23603 /* named argument a:name */
23604 name = FUNCARG(fp, i);
23605 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023606 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023607 /* "..." argument a:1, a:2, etc. */
23608 sprintf((char *)numbuf, "%d", ai + 1);
23609 name = numbuf;
23610 }
23611 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23612 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023613 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023614 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23615 }
23616 else
23617 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023618 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23619 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023620 if (v == NULL)
23621 break;
23622 v->di_flags = DI_FLAGS_RO;
23623 }
23624 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023625 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023626
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023627 /* Note: the values are copied directly to avoid alloc/free.
23628 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023629 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023630 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023631
23632 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23633 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023634 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23635 fc->l_listitems[ai].li_tv = argvars[i];
23636 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023637 }
23638 }
23639
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 /* Don't redraw while executing the function. */
23641 ++RedrawingDisabled;
23642 save_sourcing_name = sourcing_name;
23643 save_sourcing_lnum = sourcing_lnum;
23644 sourcing_lnum = 1;
23645 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023646 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 if (sourcing_name != NULL)
23648 {
23649 if (save_sourcing_name != NULL
23650 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23651 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23652 else
23653 STRCPY(sourcing_name, "function ");
23654 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23655
23656 if (p_verbose >= 12)
23657 {
23658 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023659 verbose_enter_scroll();
23660
Bram Moolenaar555b2802005-05-19 21:08:39 +000023661 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662 if (p_verbose >= 14)
23663 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023665 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023666 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023667 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668
23669 msg_puts((char_u *)"(");
23670 for (i = 0; i < argcount; ++i)
23671 {
23672 if (i > 0)
23673 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023674 if (argvars[i].v_type == VAR_NUMBER)
23675 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 else
23677 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023678 /* Do not want errors such as E724 here. */
23679 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023680 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023681 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023682 if (s != NULL)
23683 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023684 if (vim_strsize(s) > MSG_BUF_CLEN)
23685 {
23686 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23687 s = buf;
23688 }
23689 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023690 vim_free(tofree);
23691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692 }
23693 }
23694 msg_puts((char_u *)")");
23695 }
23696 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023697
23698 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 --no_wait_return;
23700 }
23701 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023702#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023703 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023704 {
23705 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23706 func_do_profile(fp);
23707 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023708 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023709 {
23710 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023711 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023712 profile_zero(&fp->uf_tm_children);
23713 }
23714 script_prof_save(&wait_start);
23715 }
23716#endif
23717
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023719 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 save_did_emsg = did_emsg;
23721 did_emsg = FALSE;
23722
23723 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023724 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023725 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23726
23727 --RedrawingDisabled;
23728
23729 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023730 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023731 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023732 clear_tv(rettv);
23733 rettv->v_type = VAR_NUMBER;
23734 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735 }
23736
Bram Moolenaar05159a02005-02-26 23:04:13 +000023737#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023738 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023739 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023740 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023741 profile_end(&call_start);
23742 profile_sub_wait(&wait_start, &call_start);
23743 profile_add(&fp->uf_tm_total, &call_start);
23744 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023745 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023746 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023747 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23748 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023749 }
23750 }
23751#endif
23752
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753 /* when being verbose, mention the return value */
23754 if (p_verbose >= 12)
23755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023757 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023758
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023760 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023761 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023762 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023763 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023764 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023766 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023767 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023768 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023769 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023770
Bram Moolenaar555b2802005-05-19 21:08:39 +000023771 /* The value may be very long. Skip the middle part, so that we
23772 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023773 * truncate it at the end. Don't want errors such as E724 here. */
23774 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023775 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023776 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023777 if (s != NULL)
23778 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023779 if (vim_strsize(s) > MSG_BUF_CLEN)
23780 {
23781 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23782 s = buf;
23783 }
23784 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023785 vim_free(tofree);
23786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023787 }
23788 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023789
23790 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791 --no_wait_return;
23792 }
23793
23794 vim_free(sourcing_name);
23795 sourcing_name = save_sourcing_name;
23796 sourcing_lnum = save_sourcing_lnum;
23797 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023798#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023799 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023800 script_prof_restore(&wait_start);
23801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802
23803 if (p_verbose >= 12 && sourcing_name != NULL)
23804 {
23805 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023806 verbose_enter_scroll();
23807
Bram Moolenaar555b2802005-05-19 21:08:39 +000023808 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023810
23811 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812 --no_wait_return;
23813 }
23814
23815 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023816 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023818
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023819 /* If the a:000 list and the l: and a: dicts are not referenced we can
23820 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023821 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23822 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23823 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23824 {
23825 free_funccal(fc, FALSE);
23826 }
23827 else
23828 {
23829 hashitem_T *hi;
23830 listitem_T *li;
23831 int todo;
23832
23833 /* "fc" is still in use. This can happen when returning "a:000" or
23834 * assigning "l:" to a global variable.
23835 * Link "fc" in the list for garbage collection later. */
23836 fc->caller = previous_funccal;
23837 previous_funccal = fc;
23838
23839 /* Make a copy of the a: variables, since we didn't do that above. */
23840 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23841 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23842 {
23843 if (!HASHITEM_EMPTY(hi))
23844 {
23845 --todo;
23846 v = HI2DI(hi);
23847 copy_tv(&v->di_tv, &v->di_tv);
23848 }
23849 }
23850
23851 /* Make a copy of the a:000 items, since we didn't do that above. */
23852 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23853 copy_tv(&li->li_tv, &li->li_tv);
23854 }
23855}
23856
23857/*
23858 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023859 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023860 */
23861 static int
23862can_free_funccal(fc, copyID)
23863 funccall_T *fc;
23864 int copyID;
23865{
23866 return (fc->l_varlist.lv_copyID != copyID
23867 && fc->l_vars.dv_copyID != copyID
23868 && fc->l_avars.dv_copyID != copyID);
23869}
23870
23871/*
23872 * Free "fc" and what it contains.
23873 */
23874 static void
23875free_funccal(fc, free_val)
23876 funccall_T *fc;
23877 int free_val; /* a: vars were allocated */
23878{
23879 listitem_T *li;
23880
23881 /* The a: variables typevals may not have been allocated, only free the
23882 * allocated variables. */
23883 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23884
23885 /* free all l: variables */
23886 vars_clear(&fc->l_vars.dv_hashtab);
23887
23888 /* Free the a:000 variables if they were allocated. */
23889 if (free_val)
23890 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23891 clear_tv(&li->li_tv);
23892
23893 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894}
23895
23896/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023897 * Add a number variable "name" to dict "dp" with value "nr".
23898 */
23899 static void
23900add_nr_var(dp, v, name, nr)
23901 dict_T *dp;
23902 dictitem_T *v;
23903 char *name;
23904 varnumber_T nr;
23905{
23906 STRCPY(v->di_key, name);
23907 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23908 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23909 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023910 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023911 v->di_tv.vval.v_number = nr;
23912}
23913
23914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915 * ":return [expr]"
23916 */
23917 void
23918ex_return(eap)
23919 exarg_T *eap;
23920{
23921 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023922 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 int returning = FALSE;
23924
23925 if (current_funccal == NULL)
23926 {
23927 EMSG(_("E133: :return not inside a function"));
23928 return;
23929 }
23930
23931 if (eap->skip)
23932 ++emsg_skip;
23933
23934 eap->nextcmd = NULL;
23935 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023936 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 {
23938 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023939 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023941 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942 }
23943 /* It's safer to return also on error. */
23944 else if (!eap->skip)
23945 {
23946 /*
23947 * Return unless the expression evaluation has been cancelled due to an
23948 * aborting error, an interrupt, or an exception.
23949 */
23950 if (!aborting())
23951 returning = do_return(eap, FALSE, TRUE, NULL);
23952 }
23953
23954 /* When skipping or the return gets pending, advance to the next command
23955 * in this line (!returning). Otherwise, ignore the rest of the line.
23956 * Following lines will be ignored by get_func_line(). */
23957 if (returning)
23958 eap->nextcmd = NULL;
23959 else if (eap->nextcmd == NULL) /* no argument */
23960 eap->nextcmd = check_nextcmd(arg);
23961
23962 if (eap->skip)
23963 --emsg_skip;
23964}
23965
23966/*
23967 * Return from a function. Possibly makes the return pending. Also called
23968 * for a pending return at the ":endtry" or after returning from an extra
23969 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023970 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023971 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 * FALSE when the return gets pending.
23973 */
23974 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023975do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 exarg_T *eap;
23977 int reanimate;
23978 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023979 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980{
23981 int idx;
23982 struct condstack *cstack = eap->cstack;
23983
23984 if (reanimate)
23985 /* Undo the return. */
23986 current_funccal->returned = FALSE;
23987
23988 /*
23989 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23990 * not in its finally clause (which then is to be executed next) is found.
23991 * In this case, make the ":return" pending for execution at the ":endtry".
23992 * Otherwise, return normally.
23993 */
23994 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23995 if (idx >= 0)
23996 {
23997 cstack->cs_pending[idx] = CSTP_RETURN;
23998
23999 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024000 /* A pending return again gets pending. "rettv" points to an
24001 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024003 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 else
24005 {
24006 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024007 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024009 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024011 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 {
24013 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024014 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024015 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 else
24017 EMSG(_(e_outofmem));
24018 }
24019 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024020 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024021
24022 if (reanimate)
24023 {
24024 /* The pending return value could be overwritten by a ":return"
24025 * without argument in a finally clause; reset the default
24026 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024027 current_funccal->rettv->v_type = VAR_NUMBER;
24028 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024029 }
24030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024031 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 }
24033 else
24034 {
24035 current_funccal->returned = TRUE;
24036
24037 /* If the return is carried out now, store the return value. For
24038 * a return immediately after reanimation, the value is already
24039 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024040 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024042 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024043 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024045 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024046 }
24047 }
24048
24049 return idx < 0;
24050}
24051
24052/*
24053 * Free the variable with a pending return value.
24054 */
24055 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024056discard_pending_return(rettv)
24057 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058{
Bram Moolenaar33570922005-01-25 22:26:29 +000024059 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060}
24061
24062/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024063 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 * is an allocated string. Used by report_pending() for verbose messages.
24065 */
24066 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024067get_return_cmd(rettv)
24068 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024070 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024071 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024072 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024074 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024075 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024076 if (s == NULL)
24077 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024078
24079 STRCPY(IObuff, ":return ");
24080 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24081 if (STRLEN(s) + 8 >= IOSIZE)
24082 STRCPY(IObuff + IOSIZE - 4, "...");
24083 vim_free(tofree);
24084 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024085}
24086
24087/*
24088 * Get next function line.
24089 * Called by do_cmdline() to get the next line.
24090 * Returns allocated string, or NULL for end of function.
24091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024092 char_u *
24093get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024094 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024096 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097{
Bram Moolenaar33570922005-01-25 22:26:29 +000024098 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024099 ufunc_T *fp = fcp->func;
24100 char_u *retval;
24101 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024102
24103 /* If breakpoints have been added/deleted need to check for it. */
24104 if (fcp->dbg_tick != debug_tick)
24105 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024106 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 sourcing_lnum);
24108 fcp->dbg_tick = debug_tick;
24109 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024110#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024111 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024112 func_line_end(cookie);
24113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024114
Bram Moolenaar05159a02005-02-26 23:04:13 +000024115 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024116 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24117 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 retval = NULL;
24119 else
24120 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024121 /* Skip NULL lines (continuation lines). */
24122 while (fcp->linenr < gap->ga_len
24123 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24124 ++fcp->linenr;
24125 if (fcp->linenr >= gap->ga_len)
24126 retval = NULL;
24127 else
24128 {
24129 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24130 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024131#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024132 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024133 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024134#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136 }
24137
24138 /* Did we encounter a breakpoint? */
24139 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24140 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024141 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024143 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 sourcing_lnum);
24145 fcp->dbg_tick = debug_tick;
24146 }
24147
24148 return retval;
24149}
24150
Bram Moolenaar05159a02005-02-26 23:04:13 +000024151#if defined(FEAT_PROFILE) || defined(PROTO)
24152/*
24153 * Called when starting to read a function line.
24154 * "sourcing_lnum" must be correct!
24155 * When skipping lines it may not actually be executed, but we won't find out
24156 * until later and we need to store the time now.
24157 */
24158 void
24159func_line_start(cookie)
24160 void *cookie;
24161{
24162 funccall_T *fcp = (funccall_T *)cookie;
24163 ufunc_T *fp = fcp->func;
24164
24165 if (fp->uf_profiling && sourcing_lnum >= 1
24166 && sourcing_lnum <= fp->uf_lines.ga_len)
24167 {
24168 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024169 /* Skip continuation lines. */
24170 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24171 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024172 fp->uf_tml_execed = FALSE;
24173 profile_start(&fp->uf_tml_start);
24174 profile_zero(&fp->uf_tml_children);
24175 profile_get_wait(&fp->uf_tml_wait);
24176 }
24177}
24178
24179/*
24180 * Called when actually executing a function line.
24181 */
24182 void
24183func_line_exec(cookie)
24184 void *cookie;
24185{
24186 funccall_T *fcp = (funccall_T *)cookie;
24187 ufunc_T *fp = fcp->func;
24188
24189 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24190 fp->uf_tml_execed = TRUE;
24191}
24192
24193/*
24194 * Called when done with a function line.
24195 */
24196 void
24197func_line_end(cookie)
24198 void *cookie;
24199{
24200 funccall_T *fcp = (funccall_T *)cookie;
24201 ufunc_T *fp = fcp->func;
24202
24203 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24204 {
24205 if (fp->uf_tml_execed)
24206 {
24207 ++fp->uf_tml_count[fp->uf_tml_idx];
24208 profile_end(&fp->uf_tml_start);
24209 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024210 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024211 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24212 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024213 }
24214 fp->uf_tml_idx = -1;
24215 }
24216}
24217#endif
24218
Bram Moolenaar071d4272004-06-13 20:20:40 +000024219/*
24220 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024221 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222 */
24223 int
24224func_has_ended(cookie)
24225 void *cookie;
24226{
Bram Moolenaar33570922005-01-25 22:26:29 +000024227 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228
24229 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24230 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024231 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232 || fcp->returned);
24233}
24234
24235/*
24236 * return TRUE if cookie indicates a function which "abort"s on errors.
24237 */
24238 int
24239func_has_abort(cookie)
24240 void *cookie;
24241{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024242 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243}
24244
24245#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24246typedef enum
24247{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024248 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24249 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24250 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251} var_flavour_T;
24252
24253static var_flavour_T var_flavour __ARGS((char_u *varname));
24254
24255 static var_flavour_T
24256var_flavour(varname)
24257 char_u *varname;
24258{
24259 char_u *p = varname;
24260
24261 if (ASCII_ISUPPER(*p))
24262 {
24263 while (*(++p))
24264 if (ASCII_ISLOWER(*p))
24265 return VAR_FLAVOUR_SESSION;
24266 return VAR_FLAVOUR_VIMINFO;
24267 }
24268 else
24269 return VAR_FLAVOUR_DEFAULT;
24270}
24271#endif
24272
24273#if defined(FEAT_VIMINFO) || defined(PROTO)
24274/*
24275 * Restore global vars that start with a capital from the viminfo file
24276 */
24277 int
24278read_viminfo_varlist(virp, writing)
24279 vir_T *virp;
24280 int writing;
24281{
24282 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024283 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024284 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024285
24286 if (!writing && (find_viminfo_parameter('!') != NULL))
24287 {
24288 tab = vim_strchr(virp->vir_line + 1, '\t');
24289 if (tab != NULL)
24290 {
24291 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024292 switch (*tab)
24293 {
24294 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024295#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024296 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024297#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024298 case 'D': type = VAR_DICT; break;
24299 case 'L': type = VAR_LIST; break;
24300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301
24302 tab = vim_strchr(tab, '\t');
24303 if (tab != NULL)
24304 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024305 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024306 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024307 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024309#ifdef FEAT_FLOAT
24310 else if (type == VAR_FLOAT)
24311 (void)string2float(tab + 1, &tv.vval.v_float);
24312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024313 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024314 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024315 if (type == VAR_DICT || type == VAR_LIST)
24316 {
24317 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24318
24319 if (etv == NULL)
24320 /* Failed to parse back the dict or list, use it as a
24321 * string. */
24322 tv.v_type = VAR_STRING;
24323 else
24324 {
24325 vim_free(tv.vval.v_string);
24326 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024327 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024328 }
24329 }
24330
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024331 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024332
24333 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024334 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024335 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24336 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024337 }
24338 }
24339 }
24340
24341 return viminfo_readline(virp);
24342}
24343
24344/*
24345 * Write global vars that start with a capital to the viminfo file
24346 */
24347 void
24348write_viminfo_varlist(fp)
24349 FILE *fp;
24350{
Bram Moolenaar33570922005-01-25 22:26:29 +000024351 hashitem_T *hi;
24352 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024353 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024354 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024355 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024356 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024357 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358
24359 if (find_viminfo_parameter('!') == NULL)
24360 return;
24361
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024362 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024363
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024364 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024365 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024367 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024368 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024369 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024370 this_var = HI2DI(hi);
24371 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024372 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024373 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024374 {
24375 case VAR_STRING: s = "STR"; break;
24376 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024377#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024378 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024379#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024380 case VAR_DICT: s = "DIC"; break;
24381 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024382 default: continue;
24383 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024384 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024385 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024386 if (p != NULL)
24387 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024388 vim_free(tofree);
24389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024390 }
24391 }
24392}
24393#endif
24394
24395#if defined(FEAT_SESSION) || defined(PROTO)
24396 int
24397store_session_globals(fd)
24398 FILE *fd;
24399{
Bram Moolenaar33570922005-01-25 22:26:29 +000024400 hashitem_T *hi;
24401 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024402 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403 char_u *p, *t;
24404
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024405 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024406 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024407 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024408 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024410 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024411 this_var = HI2DI(hi);
24412 if ((this_var->di_tv.v_type == VAR_NUMBER
24413 || this_var->di_tv.v_type == VAR_STRING)
24414 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024415 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024416 /* Escape special characters with a backslash. Turn a LF and
24417 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024418 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024419 (char_u *)"\\\"\n\r");
24420 if (p == NULL) /* out of memory */
24421 break;
24422 for (t = p; *t != NUL; ++t)
24423 if (*t == '\n')
24424 *t = 'n';
24425 else if (*t == '\r')
24426 *t = 'r';
24427 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024428 this_var->di_key,
24429 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24430 : ' ',
24431 p,
24432 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24433 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024434 || put_eol(fd) == FAIL)
24435 {
24436 vim_free(p);
24437 return FAIL;
24438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439 vim_free(p);
24440 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024441#ifdef FEAT_FLOAT
24442 else if (this_var->di_tv.v_type == VAR_FLOAT
24443 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24444 {
24445 float_T f = this_var->di_tv.vval.v_float;
24446 int sign = ' ';
24447
24448 if (f < 0)
24449 {
24450 f = -f;
24451 sign = '-';
24452 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024453 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024454 this_var->di_key, sign, f) < 0)
24455 || put_eol(fd) == FAIL)
24456 return FAIL;
24457 }
24458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024459 }
24460 }
24461 return OK;
24462}
24463#endif
24464
Bram Moolenaar661b1822005-07-28 22:36:45 +000024465/*
24466 * Display script name where an item was last set.
24467 * Should only be invoked when 'verbose' is non-zero.
24468 */
24469 void
24470last_set_msg(scriptID)
24471 scid_T scriptID;
24472{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024473 char_u *p;
24474
Bram Moolenaar661b1822005-07-28 22:36:45 +000024475 if (scriptID != 0)
24476 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024477 p = home_replace_save(NULL, get_scriptname(scriptID));
24478 if (p != NULL)
24479 {
24480 verbose_enter();
24481 MSG_PUTS(_("\n\tLast set from "));
24482 MSG_PUTS(p);
24483 vim_free(p);
24484 verbose_leave();
24485 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024486 }
24487}
24488
Bram Moolenaard812df62008-11-09 12:46:09 +000024489/*
24490 * List v:oldfiles in a nice way.
24491 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024492 void
24493ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024494 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024495{
24496 list_T *l = vimvars[VV_OLDFILES].vv_list;
24497 listitem_T *li;
24498 int nr = 0;
24499
24500 if (l == NULL)
24501 msg((char_u *)_("No old files"));
24502 else
24503 {
24504 msg_start();
24505 msg_scroll = TRUE;
24506 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24507 {
24508 msg_outnum((long)++nr);
24509 MSG_PUTS(": ");
24510 msg_outtrans(get_tv_string(&li->li_tv));
24511 msg_putchar('\n');
24512 out_flush(); /* output one line at a time */
24513 ui_breakcheck();
24514 }
24515 /* Assume "got_int" was set to truncate the listing. */
24516 got_int = FALSE;
24517
24518#ifdef FEAT_BROWSE_CMD
24519 if (cmdmod.browse)
24520 {
24521 quit_more = FALSE;
24522 nr = prompt_for_number(FALSE);
24523 msg_starthere();
24524 if (nr > 0)
24525 {
24526 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24527 (long)nr);
24528
24529 if (p != NULL)
24530 {
24531 p = expand_env_save(p);
24532 eap->arg = p;
24533 eap->cmdidx = CMD_edit;
24534 cmdmod.browse = FALSE;
24535 do_exedit(eap, NULL);
24536 vim_free(p);
24537 }
24538 }
24539 }
24540#endif
24541 }
24542}
24543
Bram Moolenaar071d4272004-06-13 20:20:40 +000024544#endif /* FEAT_EVAL */
24545
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024547#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024548
24549#ifdef WIN3264
24550/*
24551 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24552 */
24553static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24554static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24555static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24556
24557/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024558 * Get the short path (8.3) for the filename in "fnamep".
24559 * Only works for a valid file name.
24560 * When the path gets longer "fnamep" is changed and the allocated buffer
24561 * is put in "bufp".
24562 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24563 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564 */
24565 static int
24566get_short_pathname(fnamep, bufp, fnamelen)
24567 char_u **fnamep;
24568 char_u **bufp;
24569 int *fnamelen;
24570{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024571 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024572 char_u *newbuf;
24573
24574 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575 l = GetShortPathName(*fnamep, *fnamep, len);
24576 if (l > len - 1)
24577 {
24578 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024579 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024580 newbuf = vim_strnsave(*fnamep, l);
24581 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583
24584 vim_free(*bufp);
24585 *fnamep = *bufp = newbuf;
24586
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024587 /* Really should always succeed, as the buffer is big enough. */
24588 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024589 }
24590
24591 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024592 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593}
24594
24595/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024596 * Get the short path (8.3) for the filename in "fname". The converted
24597 * path is returned in "bufp".
24598 *
24599 * Some of the directories specified in "fname" may not exist. This function
24600 * will shorten the existing directories at the beginning of the path and then
24601 * append the remaining non-existing path.
24602 *
24603 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024604 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024605 * bufp - Pointer to an allocated buffer for the filename.
24606 * fnamelen - Length of the filename pointed to by fname
24607 *
24608 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024609 */
24610 static int
24611shortpath_for_invalid_fname(fname, bufp, fnamelen)
24612 char_u **fname;
24613 char_u **bufp;
24614 int *fnamelen;
24615{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024616 char_u *short_fname, *save_fname, *pbuf_unused;
24617 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024619 int old_len, len;
24620 int new_len, sfx_len;
24621 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622
24623 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024624 old_len = *fnamelen;
24625 save_fname = vim_strnsave(*fname, old_len);
24626 pbuf_unused = NULL;
24627 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024628
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024629 endp = save_fname + old_len - 1; /* Find the end of the copy */
24630 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024632 /*
24633 * Try shortening the supplied path till it succeeds by removing one
24634 * directory at a time from the tail of the path.
24635 */
24636 len = 0;
24637 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024639 /* go back one path-separator */
24640 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24641 --endp;
24642 if (endp <= save_fname)
24643 break; /* processed the complete path */
24644
24645 /*
24646 * Replace the path separator with a NUL and try to shorten the
24647 * resulting path.
24648 */
24649 ch = *endp;
24650 *endp = 0;
24651 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024652 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024653 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24654 {
24655 retval = FAIL;
24656 goto theend;
24657 }
24658 *endp = ch; /* preserve the string */
24659
24660 if (len > 0)
24661 break; /* successfully shortened the path */
24662
24663 /* failed to shorten the path. Skip the path separator */
24664 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665 }
24666
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024667 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024669 /*
24670 * Succeeded in shortening the path. Now concatenate the shortened
24671 * path with the remaining path at the tail.
24672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024674 /* Compute the length of the new path. */
24675 sfx_len = (int)(save_endp - endp) + 1;
24676 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024678 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024680 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024681 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024682 /* There is not enough space in the currently allocated string,
24683 * copy it to a buffer big enough. */
24684 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024686 {
24687 retval = FAIL;
24688 goto theend;
24689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024690 }
24691 else
24692 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024693 /* Transfer short_fname to the main buffer (it's big enough),
24694 * unless get_short_pathname() did its work in-place. */
24695 *fname = *bufp = save_fname;
24696 if (short_fname != save_fname)
24697 vim_strncpy(save_fname, short_fname, len);
24698 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024699 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024700
24701 /* concat the not-shortened part of the path */
24702 vim_strncpy(*fname + len, endp, sfx_len);
24703 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024704 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024705
24706theend:
24707 vim_free(pbuf_unused);
24708 vim_free(save_fname);
24709
24710 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024711}
24712
24713/*
24714 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024715 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024716 */
24717 static int
24718shortpath_for_partial(fnamep, bufp, fnamelen)
24719 char_u **fnamep;
24720 char_u **bufp;
24721 int *fnamelen;
24722{
24723 int sepcount, len, tflen;
24724 char_u *p;
24725 char_u *pbuf, *tfname;
24726 int hasTilde;
24727
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024728 /* Count up the path separators from the RHS.. so we know which part
24729 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024730 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024731 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024732 if (vim_ispathsep(*p))
24733 ++sepcount;
24734
24735 /* Need full path first (use expand_env() to remove a "~/") */
24736 hasTilde = (**fnamep == '~');
24737 if (hasTilde)
24738 pbuf = tfname = expand_env_save(*fnamep);
24739 else
24740 pbuf = tfname = FullName_save(*fnamep, FALSE);
24741
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024742 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024743
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024744 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24745 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024746
24747 if (len == 0)
24748 {
24749 /* Don't have a valid filename, so shorten the rest of the
24750 * path if we can. This CAN give us invalid 8.3 filenames, but
24751 * there's not a lot of point in guessing what it might be.
24752 */
24753 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024754 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24755 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024756 }
24757
24758 /* Count the paths backward to find the beginning of the desired string. */
24759 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024760 {
24761#ifdef FEAT_MBYTE
24762 if (has_mbyte)
24763 p -= mb_head_off(tfname, p);
24764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765 if (vim_ispathsep(*p))
24766 {
24767 if (sepcount == 0 || (hasTilde && sepcount == 1))
24768 break;
24769 else
24770 sepcount --;
24771 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024773 if (hasTilde)
24774 {
24775 --p;
24776 if (p >= tfname)
24777 *p = '~';
24778 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024779 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780 }
24781 else
24782 ++p;
24783
24784 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24785 vim_free(*bufp);
24786 *fnamelen = (int)STRLEN(p);
24787 *bufp = pbuf;
24788 *fnamep = p;
24789
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024790 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791}
24792#endif /* WIN3264 */
24793
24794/*
24795 * Adjust a filename, according to a string of modifiers.
24796 * *fnamep must be NUL terminated when called. When returning, the length is
24797 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024798 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024799 * When there is an error, *fnamep is set to NULL.
24800 */
24801 int
24802modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24803 char_u *src; /* string with modifiers */
24804 int *usedlen; /* characters after src that are used */
24805 char_u **fnamep; /* file name so far */
24806 char_u **bufp; /* buffer for allocated file name or NULL */
24807 int *fnamelen; /* length of fnamep */
24808{
24809 int valid = 0;
24810 char_u *tail;
24811 char_u *s, *p, *pbuf;
24812 char_u dirname[MAXPATHL];
24813 int c;
24814 int has_fullname = 0;
24815#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024816 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024817 int has_shortname = 0;
24818#endif
24819
24820repeat:
24821 /* ":p" - full path/file_name */
24822 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24823 {
24824 has_fullname = 1;
24825
24826 valid |= VALID_PATH;
24827 *usedlen += 2;
24828
24829 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24830 if ((*fnamep)[0] == '~'
24831#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24832 && ((*fnamep)[1] == '/'
24833# ifdef BACKSLASH_IN_FILENAME
24834 || (*fnamep)[1] == '\\'
24835# endif
24836 || (*fnamep)[1] == NUL)
24837
24838#endif
24839 )
24840 {
24841 *fnamep = expand_env_save(*fnamep);
24842 vim_free(*bufp); /* free any allocated file name */
24843 *bufp = *fnamep;
24844 if (*fnamep == NULL)
24845 return -1;
24846 }
24847
24848 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024849 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850 {
24851 if (vim_ispathsep(*p)
24852 && p[1] == '.'
24853 && (p[2] == NUL
24854 || vim_ispathsep(p[2])
24855 || (p[2] == '.'
24856 && (p[3] == NUL || vim_ispathsep(p[3])))))
24857 break;
24858 }
24859
24860 /* FullName_save() is slow, don't use it when not needed. */
24861 if (*p != NUL || !vim_isAbsName(*fnamep))
24862 {
24863 *fnamep = FullName_save(*fnamep, *p != NUL);
24864 vim_free(*bufp); /* free any allocated file name */
24865 *bufp = *fnamep;
24866 if (*fnamep == NULL)
24867 return -1;
24868 }
24869
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024870#ifdef WIN3264
24871# if _WIN32_WINNT >= 0x0500
24872 if (vim_strchr(*fnamep, '~') != NULL)
24873 {
24874 /* Expand 8.3 filename to full path. Needed to make sure the same
24875 * file does not have two different names.
24876 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24877 p = alloc(_MAX_PATH + 1);
24878 if (p != NULL)
24879 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010024880 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024881 {
24882 vim_free(*bufp);
24883 *bufp = *fnamep = p;
24884 }
24885 else
24886 vim_free(p);
24887 }
24888 }
24889# endif
24890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024891 /* Append a path separator to a directory. */
24892 if (mch_isdir(*fnamep))
24893 {
24894 /* Make room for one or two extra characters. */
24895 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24896 vim_free(*bufp); /* free any allocated file name */
24897 *bufp = *fnamep;
24898 if (*fnamep == NULL)
24899 return -1;
24900 add_pathsep(*fnamep);
24901 }
24902 }
24903
24904 /* ":." - path relative to the current directory */
24905 /* ":~" - path relative to the home directory */
24906 /* ":8" - shortname path - postponed till after */
24907 while (src[*usedlen] == ':'
24908 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24909 {
24910 *usedlen += 2;
24911 if (c == '8')
24912 {
24913#ifdef WIN3264
24914 has_shortname = 1; /* Postpone this. */
24915#endif
24916 continue;
24917 }
24918 pbuf = NULL;
24919 /* Need full path first (use expand_env() to remove a "~/") */
24920 if (!has_fullname)
24921 {
24922 if (c == '.' && **fnamep == '~')
24923 p = pbuf = expand_env_save(*fnamep);
24924 else
24925 p = pbuf = FullName_save(*fnamep, FALSE);
24926 }
24927 else
24928 p = *fnamep;
24929
24930 has_fullname = 0;
24931
24932 if (p != NULL)
24933 {
24934 if (c == '.')
24935 {
24936 mch_dirname(dirname, MAXPATHL);
24937 s = shorten_fname(p, dirname);
24938 if (s != NULL)
24939 {
24940 *fnamep = s;
24941 if (pbuf != NULL)
24942 {
24943 vim_free(*bufp); /* free any allocated file name */
24944 *bufp = pbuf;
24945 pbuf = NULL;
24946 }
24947 }
24948 }
24949 else
24950 {
24951 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24952 /* Only replace it when it starts with '~' */
24953 if (*dirname == '~')
24954 {
24955 s = vim_strsave(dirname);
24956 if (s != NULL)
24957 {
24958 *fnamep = s;
24959 vim_free(*bufp);
24960 *bufp = s;
24961 }
24962 }
24963 }
24964 vim_free(pbuf);
24965 }
24966 }
24967
24968 tail = gettail(*fnamep);
24969 *fnamelen = (int)STRLEN(*fnamep);
24970
24971 /* ":h" - head, remove "/file_name", can be repeated */
24972 /* Don't remove the first "/" or "c:\" */
24973 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24974 {
24975 valid |= VALID_HEAD;
24976 *usedlen += 2;
24977 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024978 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024979 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024980 *fnamelen = (int)(tail - *fnamep);
24981#ifdef VMS
24982 if (*fnamelen > 0)
24983 *fnamelen += 1; /* the path separator is part of the path */
24984#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024985 if (*fnamelen == 0)
24986 {
24987 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24988 p = vim_strsave((char_u *)".");
24989 if (p == NULL)
24990 return -1;
24991 vim_free(*bufp);
24992 *bufp = *fnamep = tail = p;
24993 *fnamelen = 1;
24994 }
24995 else
24996 {
24997 while (tail > s && !after_pathsep(s, tail))
24998 mb_ptr_back(*fnamep, tail);
24999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000 }
25001
25002 /* ":8" - shortname */
25003 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25004 {
25005 *usedlen += 2;
25006#ifdef WIN3264
25007 has_shortname = 1;
25008#endif
25009 }
25010
25011#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025012 /*
25013 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025014 */
25015 if (has_shortname)
25016 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025017 /* Copy the string if it is shortened by :h and when it wasn't copied
25018 * yet, because we are going to change it in place. Avoids changing
25019 * the buffer name for "%:8". */
25020 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025021 {
25022 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025023 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024 return -1;
25025 vim_free(*bufp);
25026 *bufp = *fnamep = p;
25027 }
25028
25029 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025030 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031 if (!has_fullname && !vim_isAbsName(*fnamep))
25032 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025033 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025034 return -1;
25035 }
25036 else
25037 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025038 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039
Bram Moolenaardc935552011-08-17 15:23:23 +020025040 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025042 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 return -1;
25044
25045 if (l == 0)
25046 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025047 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025048 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025049 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025050 return -1;
25051 }
25052 *fnamelen = l;
25053 }
25054 }
25055#endif /* WIN3264 */
25056
25057 /* ":t" - tail, just the basename */
25058 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25059 {
25060 *usedlen += 2;
25061 *fnamelen -= (int)(tail - *fnamep);
25062 *fnamep = tail;
25063 }
25064
25065 /* ":e" - extension, can be repeated */
25066 /* ":r" - root, without extension, can be repeated */
25067 while (src[*usedlen] == ':'
25068 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25069 {
25070 /* find a '.' in the tail:
25071 * - for second :e: before the current fname
25072 * - otherwise: The last '.'
25073 */
25074 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25075 s = *fnamep - 2;
25076 else
25077 s = *fnamep + *fnamelen - 1;
25078 for ( ; s > tail; --s)
25079 if (s[0] == '.')
25080 break;
25081 if (src[*usedlen + 1] == 'e') /* :e */
25082 {
25083 if (s > tail)
25084 {
25085 *fnamelen += (int)(*fnamep - (s + 1));
25086 *fnamep = s + 1;
25087#ifdef VMS
25088 /* cut version from the extension */
25089 s = *fnamep + *fnamelen - 1;
25090 for ( ; s > *fnamep; --s)
25091 if (s[0] == ';')
25092 break;
25093 if (s > *fnamep)
25094 *fnamelen = s - *fnamep;
25095#endif
25096 }
25097 else if (*fnamep <= tail)
25098 *fnamelen = 0;
25099 }
25100 else /* :r */
25101 {
25102 if (s > tail) /* remove one extension */
25103 *fnamelen = (int)(s - *fnamep);
25104 }
25105 *usedlen += 2;
25106 }
25107
25108 /* ":s?pat?foo?" - substitute */
25109 /* ":gs?pat?foo?" - global substitute */
25110 if (src[*usedlen] == ':'
25111 && (src[*usedlen + 1] == 's'
25112 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25113 {
25114 char_u *str;
25115 char_u *pat;
25116 char_u *sub;
25117 int sep;
25118 char_u *flags;
25119 int didit = FALSE;
25120
25121 flags = (char_u *)"";
25122 s = src + *usedlen + 2;
25123 if (src[*usedlen + 1] == 'g')
25124 {
25125 flags = (char_u *)"g";
25126 ++s;
25127 }
25128
25129 sep = *s++;
25130 if (sep)
25131 {
25132 /* find end of pattern */
25133 p = vim_strchr(s, sep);
25134 if (p != NULL)
25135 {
25136 pat = vim_strnsave(s, (int)(p - s));
25137 if (pat != NULL)
25138 {
25139 s = p + 1;
25140 /* find end of substitution */
25141 p = vim_strchr(s, sep);
25142 if (p != NULL)
25143 {
25144 sub = vim_strnsave(s, (int)(p - s));
25145 str = vim_strnsave(*fnamep, *fnamelen);
25146 if (sub != NULL && str != NULL)
25147 {
25148 *usedlen = (int)(p + 1 - src);
25149 s = do_string_sub(str, pat, sub, flags);
25150 if (s != NULL)
25151 {
25152 *fnamep = s;
25153 *fnamelen = (int)STRLEN(s);
25154 vim_free(*bufp);
25155 *bufp = s;
25156 didit = TRUE;
25157 }
25158 }
25159 vim_free(sub);
25160 vim_free(str);
25161 }
25162 vim_free(pat);
25163 }
25164 }
25165 /* after using ":s", repeat all the modifiers */
25166 if (didit)
25167 goto repeat;
25168 }
25169 }
25170
Bram Moolenaar26df0922014-02-23 23:39:13 +010025171 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25172 {
25173 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25174 if (p == NULL)
25175 return -1;
25176 vim_free(*bufp);
25177 *bufp = *fnamep = p;
25178 *fnamelen = (int)STRLEN(p);
25179 *usedlen += 2;
25180 }
25181
Bram Moolenaar071d4272004-06-13 20:20:40 +000025182 return valid;
25183}
25184
25185/*
25186 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25187 * "flags" can be "g" to do a global substitute.
25188 * Returns an allocated string, NULL for error.
25189 */
25190 char_u *
25191do_string_sub(str, pat, sub, flags)
25192 char_u *str;
25193 char_u *pat;
25194 char_u *sub;
25195 char_u *flags;
25196{
25197 int sublen;
25198 regmatch_T regmatch;
25199 int i;
25200 int do_all;
25201 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025202 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025203 garray_T ga;
25204 char_u *ret;
25205 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025206 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207
25208 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25209 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025210 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025211
25212 ga_init2(&ga, 1, 200);
25213
25214 do_all = (flags[0] == 'g');
25215
25216 regmatch.rm_ic = p_ic;
25217 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25218 if (regmatch.regprog != NULL)
25219 {
25220 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025221 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025222 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25223 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025224 /* Skip empty match except for first match. */
25225 if (regmatch.startp[0] == regmatch.endp[0])
25226 {
25227 if (zero_width == regmatch.startp[0])
25228 {
25229 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025230 i = MB_PTR2LEN(tail);
25231 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25232 (size_t)i);
25233 ga.ga_len += i;
25234 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025235 continue;
25236 }
25237 zero_width = regmatch.startp[0];
25238 }
25239
Bram Moolenaar071d4272004-06-13 20:20:40 +000025240 /*
25241 * Get some space for a temporary buffer to do the substitution
25242 * into. It will contain:
25243 * - The text up to where the match is.
25244 * - The substituted text.
25245 * - The text after the match.
25246 */
25247 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025248 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25250 {
25251 ga_clear(&ga);
25252 break;
25253 }
25254
25255 /* copy the text up to where the match is */
25256 i = (int)(regmatch.startp[0] - tail);
25257 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25258 /* add the substituted text */
25259 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25260 + ga.ga_len + i, TRUE, TRUE, FALSE);
25261 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025262 tail = regmatch.endp[0];
25263 if (*tail == NUL)
25264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265 if (!do_all)
25266 break;
25267 }
25268
25269 if (ga.ga_data != NULL)
25270 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25271
Bram Moolenaar473de612013-06-08 18:19:48 +020025272 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 }
25274
25275 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25276 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025277 if (p_cpo == empty_option)
25278 p_cpo = save_cpo;
25279 else
25280 /* Darn, evaluating {sub} expression changed the value. */
25281 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282
25283 return ret;
25284}
25285
25286#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */