blob: 6d5d762f27e7e67a3ae1ddaf4249087bf77af8ce [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/*
5977 * Free a list, including all items it points to.
5978 * 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 Moolenaarc0a6fac2009-05-24 11:40:58 +00006818 int did_free;
6819 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{
6944 dict_T *dd;
6945 list_T *ll;
6946 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 Moolenaarc0a6fac2009-05-24 11:40:58 +00006952 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006954 /* Free the Dictionary and ordinary items it contains, but don't
6955 * recurse into Lists and Dictionaries, they will be in the list
6956 * of dicts or list of lists. */
6957 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 did_free = TRUE;
6959
6960 /* restart, next dict may also have been freed */
6961 dd = first_dict;
6962 }
6963 else
6964 dd = dd->dv_used_next;
6965
6966 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 * Go through the list of lists and free items without the copyID.
6968 * But don't free a list that has a watcher (used in a for loop), these
6969 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006970 */
6971 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6973 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006975 /* Free the List and ordinary items it contains, but don't recurse
6976 * into Lists and Dictionaries, they will be in the list of dicts
6977 * or list of lists. */
6978 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 did_free = TRUE;
6980
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006981 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982 ll = first_list;
6983 }
6984 else
6985 ll = ll->lv_used_next;
6986
6987 return did_free;
6988}
6989
6990/*
6991 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006992 * "list_stack" is used to add lists to be marked. Can be NULL.
6993 *
6994 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 int
6997set_ref_in_ht(ht, copyID, list_stack)
6998 hashtab_T *ht;
6999 int copyID;
7000 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001{
7002 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007003 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007005 hashtab_T *cur_ht;
7006 ht_stack_T *ht_stack = NULL;
7007 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007009 cur_ht = ht;
7010 for (;;)
7011 {
7012 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007014 /* Mark each item in the hashtab. If the item contains a hashtab
7015 * it is added to ht_stack, if it contains a list it is added to
7016 * list_stack. */
7017 todo = (int)cur_ht->ht_used;
7018 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7019 if (!HASHITEM_EMPTY(hi))
7020 {
7021 --todo;
7022 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7023 &ht_stack, list_stack);
7024 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007026
7027 if (ht_stack == NULL)
7028 break;
7029
7030 /* take an item from the stack */
7031 cur_ht = ht_stack->ht;
7032 tempitem = ht_stack;
7033 ht_stack = ht_stack->prev;
7034 free(tempitem);
7035 }
7036
7037 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038}
7039
7040/*
7041 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7043 *
7044 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046 int
7047set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 list_T *l;
7049 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 listitem_T *li;
7053 int abort = FALSE;
7054 list_T *cur_l;
7055 list_stack_T *list_stack = NULL;
7056 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 cur_l = l;
7059 for (;;)
7060 {
7061 if (!abort)
7062 /* Mark each item in the list. If the item contains a hashtab
7063 * it is added to ht_stack, if it contains a list it is added to
7064 * list_stack. */
7065 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7066 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7067 ht_stack, &list_stack);
7068 if (list_stack == NULL)
7069 break;
7070
7071 /* take an item from the stack */
7072 cur_l = list_stack->list;
7073 tempitem = list_stack;
7074 list_stack = list_stack->prev;
7075 free(tempitem);
7076 }
7077
7078 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079}
7080
7081/*
7082 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 * "list_stack" is used to add lists to be marked. Can be NULL.
7084 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7085 *
7086 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 int
7089set_ref_in_item(tv, copyID, ht_stack, list_stack)
7090 typval_T *tv;
7091 int copyID;
7092 ht_stack_T **ht_stack;
7093 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094{
7095 dict_T *dd;
7096 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007098
7099 switch (tv->v_type)
7100 {
7101 case VAR_DICT:
7102 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007103 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007104 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007105 /* Didn't see this dict yet. */
7106 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 if (ht_stack == NULL)
7108 {
7109 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7110 }
7111 else
7112 {
7113 ht_stack_T *newitem = (ht_stack_T*)malloc(
7114 sizeof(ht_stack_T));
7115 if (newitem == NULL)
7116 abort = TRUE;
7117 else
7118 {
7119 newitem->ht = &dd->dv_hashtab;
7120 newitem->prev = *ht_stack;
7121 *ht_stack = newitem;
7122 }
7123 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007124 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007125 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007126
7127 case VAR_LIST:
7128 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007129 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007130 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007131 /* Didn't see this list yet. */
7132 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007133 if (list_stack == NULL)
7134 {
7135 abort = set_ref_in_list(ll, copyID, ht_stack);
7136 }
7137 else
7138 {
7139 list_stack_T *newitem = (list_stack_T*)malloc(
7140 sizeof(list_stack_T));
7141 if (newitem == NULL)
7142 abort = TRUE;
7143 else
7144 {
7145 newitem->list = ll;
7146 newitem->prev = *list_stack;
7147 *list_stack = newitem;
7148 }
7149 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007150 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007151 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007152 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007153 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154}
7155
7156/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007157 * Allocate an empty header for a dictionary.
7158 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007159 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007160dict_alloc()
7161{
Bram Moolenaar33570922005-01-25 22:26:29 +00007162 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163
Bram Moolenaar33570922005-01-25 22:26:29 +00007164 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007165 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007166 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007167 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007168 if (first_dict != NULL)
7169 first_dict->dv_used_prev = d;
7170 d->dv_used_next = first_dict;
7171 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007172 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007173
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007175 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007176 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007177 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007178 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007179 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007180 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181}
7182
7183/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007184 * Allocate an empty dict for a return value.
7185 * Returns OK or FAIL.
7186 */
7187 static int
7188rettv_dict_alloc(rettv)
7189 typval_T *rettv;
7190{
7191 dict_T *d = dict_alloc();
7192
7193 if (d == NULL)
7194 return FAIL;
7195
7196 rettv->vval.v_dict = d;
7197 rettv->v_type = VAR_DICT;
7198 ++d->dv_refcount;
7199 return OK;
7200}
7201
7202
7203/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204 * Unreference a Dictionary: decrement the reference count and free it when it
7205 * becomes zero.
7206 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007207 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007209 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007211 if (d != NULL && --d->dv_refcount <= 0)
7212 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213}
7214
7215/*
7216 * Free a Dictionary, including all items it contains.
7217 * Ignores the reference count.
7218 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007219 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007220dict_free(d, recurse)
7221 dict_T *d;
7222 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007224 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007226 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007228 /* Remove the dict from the list of dicts for garbage collection. */
7229 if (d->dv_used_prev == NULL)
7230 first_dict = d->dv_used_next;
7231 else
7232 d->dv_used_prev->dv_used_next = d->dv_used_next;
7233 if (d->dv_used_next != NULL)
7234 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7235
7236 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007237 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007238 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007241 if (!HASHITEM_EMPTY(hi))
7242 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007243 /* Remove the item before deleting it, just in case there is
7244 * something recursive causing trouble. */
7245 di = HI2DI(hi);
7246 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007247 if (recurse || (di->di_tv.v_type != VAR_LIST
7248 && di->di_tv.v_type != VAR_DICT))
7249 clear_tv(&di->di_tv);
7250 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251 --todo;
7252 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 vim_free(d);
7256}
7257
7258/*
7259 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007260 * The "key" is copied to the new item.
7261 * Note that the value of the item "di_tv" still needs to be initialized!
7262 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007264 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007265dictitem_alloc(key)
7266 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267{
Bram Moolenaar33570922005-01-25 22:26:29 +00007268 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007270 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007272 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007274 di->di_flags = 0;
7275 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277}
7278
7279/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280 * Make a copy of a Dictionary item.
7281 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007283dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285{
Bram Moolenaar33570922005-01-25 22:26:29 +00007286 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007288 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7289 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290 if (di != NULL)
7291 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007292 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007293 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294 copy_tv(&org->di_tv, &di->di_tv);
7295 }
7296 return di;
7297}
7298
7299/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 * Remove item "item" from Dictionary "dict" and free it.
7301 */
7302 static void
7303dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 dict_T *dict;
7305 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306{
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 if (HASHITEM_EMPTY(hi))
7311 EMSG2(_(e_intern2), "dictitem_remove()");
7312 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 dictitem_free(item);
7315}
7316
7317/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007318 * Free a dict item. Also clears the value.
7319 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007320 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007323{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007324 clear_tv(&item->di_tv);
7325 vim_free(item);
7326}
7327
7328/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7330 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007331 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 * Returns NULL when out of memory.
7333 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007335dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007338 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339{
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 dict_T *copy;
7341 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007344
7345 if (orig == NULL)
7346 return NULL;
7347
7348 copy = dict_alloc();
7349 if (copy != NULL)
7350 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007351 if (copyID != 0)
7352 {
7353 orig->dv_copyID = copyID;
7354 orig->dv_copydict = copy;
7355 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007356 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007357 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007361 --todo;
7362
7363 di = dictitem_alloc(hi->hi_key);
7364 if (di == NULL)
7365 break;
7366 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007367 {
7368 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7369 copyID) == FAIL)
7370 {
7371 vim_free(di);
7372 break;
7373 }
7374 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007375 else
7376 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7377 if (dict_add(copy, di) == FAIL)
7378 {
7379 dictitem_free(di);
7380 break;
7381 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007384
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007386 if (todo > 0)
7387 {
7388 dict_unref(copy);
7389 copy = NULL;
7390 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007391 }
7392
7393 return copy;
7394}
7395
7396/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007398 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007400 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 dict_T *d;
7403 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404{
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406}
7407
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007409 * Add a number or string entry to dictionary "d".
7410 * When "str" is NULL use number "nr", otherwise use "str".
7411 * Returns FAIL when out of memory and when key already exists.
7412 */
7413 int
7414dict_add_nr_str(d, key, nr, str)
7415 dict_T *d;
7416 char *key;
7417 long nr;
7418 char_u *str;
7419{
7420 dictitem_T *item;
7421
7422 item = dictitem_alloc((char_u *)key);
7423 if (item == NULL)
7424 return FAIL;
7425 item->di_tv.v_lock = 0;
7426 if (str == NULL)
7427 {
7428 item->di_tv.v_type = VAR_NUMBER;
7429 item->di_tv.vval.v_number = nr;
7430 }
7431 else
7432 {
7433 item->di_tv.v_type = VAR_STRING;
7434 item->di_tv.vval.v_string = vim_strsave(str);
7435 }
7436 if (dict_add(d, item) == FAIL)
7437 {
7438 dictitem_free(item);
7439 return FAIL;
7440 }
7441 return OK;
7442}
7443
7444/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007445 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007446 * Returns FAIL when out of memory and when key already exists.
7447 */
7448 int
7449dict_add_list(d, key, list)
7450 dict_T *d;
7451 char *key;
7452 list_T *list;
7453{
7454 dictitem_T *item;
7455
7456 item = dictitem_alloc((char_u *)key);
7457 if (item == NULL)
7458 return FAIL;
7459 item->di_tv.v_lock = 0;
7460 item->di_tv.v_type = VAR_LIST;
7461 item->di_tv.vval.v_list = list;
7462 if (dict_add(d, item) == FAIL)
7463 {
7464 dictitem_free(item);
7465 return FAIL;
7466 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007467 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007468 return OK;
7469}
7470
7471/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007472 * Get the number of items in a Dictionary.
7473 */
7474 static long
7475dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007476 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478 if (d == NULL)
7479 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007480 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007481}
7482
7483/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 * Find item "key[len]" in Dictionary "d".
7485 * If "len" is negative use strlen(key).
7486 * Returns NULL when not found.
7487 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007488 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007489dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007490 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 char_u *key;
7492 int len;
7493{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494#define AKEYLEN 200
7495 char_u buf[AKEYLEN];
7496 char_u *akey;
7497 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007498 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 if (len < 0)
7501 akey = key;
7502 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007503 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007504 tofree = akey = vim_strnsave(key, len);
7505 if (akey == NULL)
7506 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007507 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007508 else
7509 {
7510 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007511 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512 akey = buf;
7513 }
7514
Bram Moolenaar33570922005-01-25 22:26:29 +00007515 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 vim_free(tofree);
7517 if (HASHITEM_EMPTY(hi))
7518 return NULL;
7519 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520}
7521
7522/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007523 * Get a string item from a dictionary.
7524 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007525 * Returns NULL if the entry doesn't exist or out of memory.
7526 */
7527 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007528get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007529 dict_T *d;
7530 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007531 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007532{
7533 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007534 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007535
7536 di = dict_find(d, key, -1);
7537 if (di == NULL)
7538 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007539 s = get_tv_string(&di->di_tv);
7540 if (save && s != NULL)
7541 s = vim_strsave(s);
7542 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007543}
7544
7545/*
7546 * Get a number item from a dictionary.
7547 * Returns 0 if the entry doesn't exist or out of memory.
7548 */
7549 long
7550get_dict_number(d, key)
7551 dict_T *d;
7552 char_u *key;
7553{
7554 dictitem_T *di;
7555
7556 di = dict_find(d, key, -1);
7557 if (di == NULL)
7558 return 0;
7559 return get_tv_number(&di->di_tv);
7560}
7561
7562/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 * Return an allocated string with the string representation of a Dictionary.
7564 * May return NULL.
7565 */
7566 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007567dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007569 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570{
7571 garray_T ga;
7572 int first = TRUE;
7573 char_u *tofree;
7574 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007578 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007580 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 return NULL;
7582 ga_init2(&ga, (int)sizeof(char), 80);
7583 ga_append(&ga, '{');
7584
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007585 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007586 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007588 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 --todo;
7591
7592 if (first)
7593 first = FALSE;
7594 else
7595 ga_concat(&ga, (char_u *)", ");
7596
7597 tofree = string_quote(hi->hi_key, FALSE);
7598 if (tofree != NULL)
7599 {
7600 ga_concat(&ga, tofree);
7601 vim_free(tofree);
7602 }
7603 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007604 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007605 if (s != NULL)
7606 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007608 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007610 line_breakcheck();
7611
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007614 if (todo > 0)
7615 {
7616 vim_free(ga.ga_data);
7617 return NULL;
7618 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619
7620 ga_append(&ga, '}');
7621 ga_append(&ga, NUL);
7622 return (char_u *)ga.ga_data;
7623}
7624
7625/*
7626 * Allocate a variable for a Dictionary and fill it from "*arg".
7627 * Return OK or FAIL. Returns NOTDONE for {expr}.
7628 */
7629 static int
7630get_dict_tv(arg, rettv, evaluate)
7631 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 int evaluate;
7634{
Bram Moolenaar33570922005-01-25 22:26:29 +00007635 dict_T *d = NULL;
7636 typval_T tvkey;
7637 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007638 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007639 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007641 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007642
7643 /*
7644 * First check if it's not a curly-braces thing: {expr}.
7645 * Must do this without evaluating, otherwise a function may be called
7646 * twice. Unfortunately this means we need to call eval1() twice for the
7647 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007650 if (*start != '}')
7651 {
7652 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7653 return FAIL;
7654 if (*start == '}')
7655 return NOTDONE;
7656 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657
7658 if (evaluate)
7659 {
7660 d = dict_alloc();
7661 if (d == NULL)
7662 return FAIL;
7663 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007664 tvkey.v_type = VAR_UNKNOWN;
7665 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666
7667 *arg = skipwhite(*arg + 1);
7668 while (**arg != '}' && **arg != NUL)
7669 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007670 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 goto failret;
7672 if (**arg != ':')
7673 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007674 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007675 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 goto failret;
7677 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007678 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007680 key = get_tv_string_buf_chk(&tvkey, buf);
7681 if (key == NULL || *key == NUL)
7682 {
7683 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7684 if (key != NULL)
7685 EMSG(_(e_emptykey));
7686 clear_tv(&tvkey);
7687 goto failret;
7688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690
7691 *arg = skipwhite(*arg + 1);
7692 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7693 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007694 if (evaluate)
7695 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 goto failret;
7697 }
7698 if (evaluate)
7699 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007700 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007701 if (item != NULL)
7702 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007703 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007704 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 clear_tv(&tv);
7706 goto failret;
7707 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007708 item = dictitem_alloc(key);
7709 clear_tv(&tvkey);
7710 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007713 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007714 if (dict_add(d, item) == FAIL)
7715 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 }
7717 }
7718
7719 if (**arg == '}')
7720 break;
7721 if (**arg != ',')
7722 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007723 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 goto failret;
7725 }
7726 *arg = skipwhite(*arg + 1);
7727 }
7728
7729 if (**arg != '}')
7730 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007731 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732failret:
7733 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007734 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 return FAIL;
7736 }
7737
7738 *arg = skipwhite(*arg + 1);
7739 if (evaluate)
7740 {
7741 rettv->v_type = VAR_DICT;
7742 rettv->vval.v_dict = d;
7743 ++d->dv_refcount;
7744 }
7745
7746 return OK;
7747}
7748
Bram Moolenaar8c711452005-01-14 21:53:12 +00007749/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007750 * Return a string with the string representation of a variable.
7751 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007752 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007754 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007755 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 */
7757 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007758echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007760 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007761 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007762 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007763{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007764 static int recurse = 0;
7765 char_u *r = NULL;
7766
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007768 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007769 if (!did_echo_string_emsg)
7770 {
7771 /* Only give this message once for a recursive call to avoid
7772 * flooding the user with errors. And stop iterating over lists
7773 * and dicts. */
7774 did_echo_string_emsg = TRUE;
7775 EMSG(_("E724: variable nested too deep for displaying"));
7776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007777 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007778 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007779 }
7780 ++recurse;
7781
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007782 switch (tv->v_type)
7783 {
7784 case VAR_FUNC:
7785 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007786 r = tv->vval.v_string;
7787 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007788
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007790 if (tv->vval.v_list == NULL)
7791 {
7792 *tofree = NULL;
7793 r = NULL;
7794 }
7795 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7796 {
7797 *tofree = NULL;
7798 r = (char_u *)"[...]";
7799 }
7800 else
7801 {
7802 tv->vval.v_list->lv_copyID = copyID;
7803 *tofree = list2string(tv, copyID);
7804 r = *tofree;
7805 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007807
Bram Moolenaar8c711452005-01-14 21:53:12 +00007808 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007809 if (tv->vval.v_dict == NULL)
7810 {
7811 *tofree = NULL;
7812 r = NULL;
7813 }
7814 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7815 {
7816 *tofree = NULL;
7817 r = (char_u *)"{...}";
7818 }
7819 else
7820 {
7821 tv->vval.v_dict->dv_copyID = copyID;
7822 *tofree = dict2string(tv, copyID);
7823 r = *tofree;
7824 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007827 case VAR_STRING:
7828 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829 *tofree = NULL;
7830 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007831 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007832
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007833#ifdef FEAT_FLOAT
7834 case VAR_FLOAT:
7835 *tofree = NULL;
7836 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7837 r = numbuf;
7838 break;
7839#endif
7840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007842 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007843 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007844 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845
Bram Moolenaar8502c702014-06-17 12:51:16 +02007846 if (--recurse == 0)
7847 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007848 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007849}
7850
7851/*
7852 * Return a string with the string representation of a variable.
7853 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7854 * "numbuf" is used for a number.
7855 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007856 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007857 */
7858 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007859tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007861 char_u **tofree;
7862 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007863 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007864{
7865 switch (tv->v_type)
7866 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007867 case VAR_FUNC:
7868 *tofree = string_quote(tv->vval.v_string, TRUE);
7869 return *tofree;
7870 case VAR_STRING:
7871 *tofree = string_quote(tv->vval.v_string, FALSE);
7872 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007873#ifdef FEAT_FLOAT
7874 case VAR_FLOAT:
7875 *tofree = NULL;
7876 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7877 return numbuf;
7878#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007881 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007882 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007885 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007886 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007887}
7888
7889/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007890 * Return string "str" in ' quotes, doubling ' characters.
7891 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007892 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893 */
7894 static char_u *
7895string_quote(str, function)
7896 char_u *str;
7897 int function;
7898{
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007900 char_u *p, *r, *s;
7901
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 len = (function ? 13 : 3);
7903 if (str != NULL)
7904 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007905 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 for (p = str; *p != NUL; mb_ptr_adv(p))
7907 if (*p == '\'')
7908 ++len;
7909 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007910 s = r = alloc(len);
7911 if (r != NULL)
7912 {
7913 if (function)
7914 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007915 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007916 r += 10;
7917 }
7918 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007919 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007920 if (str != NULL)
7921 for (p = str; *p != NUL; )
7922 {
7923 if (*p == '\'')
7924 *r++ = '\'';
7925 MB_COPY_CHAR(p, r);
7926 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007927 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007928 if (function)
7929 *r++ = ')';
7930 *r++ = NUL;
7931 }
7932 return s;
7933}
7934
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007935#ifdef FEAT_FLOAT
7936/*
7937 * Convert the string "text" to a floating point number.
7938 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7939 * this always uses a decimal point.
7940 * Returns the length of the text that was consumed.
7941 */
7942 static int
7943string2float(text, value)
7944 char_u *text;
7945 float_T *value; /* result stored here */
7946{
7947 char *s = (char *)text;
7948 float_T f;
7949
7950 f = strtod(s, &s);
7951 *value = f;
7952 return (int)((char_u *)s - text);
7953}
7954#endif
7955
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 * Get the value of an environment variable.
7958 * "arg" is pointing to the '$'. It is advanced to after the name.
7959 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007960 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 */
7962 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007963get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 int evaluate;
7967{
7968 char_u *string = NULL;
7969 int len;
7970 int cc;
7971 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007972 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973
7974 ++*arg;
7975 name = *arg;
7976 len = get_env_len(arg);
7977 if (evaluate)
7978 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007979 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007980 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007981
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007982 cc = name[len];
7983 name[len] = NUL;
7984 /* first try vim_getenv(), fast for normal environment vars */
7985 string = vim_getenv(name, &mustfree);
7986 if (string != NULL && *string != NUL)
7987 {
7988 if (!mustfree)
7989 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007991 else
7992 {
7993 if (mustfree)
7994 vim_free(string);
7995
7996 /* next try expanding things like $VIM and ${HOME} */
7997 string = expand_env_save(name - 1);
7998 if (string != NULL && *string == '$')
7999 {
8000 vim_free(string);
8001 string = NULL;
8002 }
8003 }
8004 name[len] = cc;
8005
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006 rettv->v_type = VAR_STRING;
8007 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 }
8009
8010 return OK;
8011}
8012
8013/*
8014 * Array with names and number of arguments of all internal functions
8015 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8016 */
8017static struct fst
8018{
8019 char *f_name; /* function name */
8020 char f_min_argc; /* minimal number of arguments */
8021 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008022 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008023 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024} functions[] =
8025{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008026#ifdef FEAT_FLOAT
8027 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008028 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008029#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008030 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008031 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 {"append", 2, 2, f_append},
8033 {"argc", 0, 0, f_argc},
8034 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008035 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008036 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008037#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008038 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008039 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008040 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008043 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {"bufexists", 1, 1, f_bufexists},
8045 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8046 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8047 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8048 {"buflisted", 1, 1, f_buflisted},
8049 {"bufloaded", 1, 1, f_bufloaded},
8050 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008051 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {"bufwinnr", 1, 1, f_bufwinnr},
8053 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008054 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008055 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008056 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008057#ifdef FEAT_FLOAT
8058 {"ceil", 1, 1, f_ceil},
8059#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008060 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008061 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008063 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008065#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008066 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008067 {"complete_add", 1, 1, f_complete_add},
8068 {"complete_check", 0, 0, f_complete_check},
8069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008071 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072#ifdef FEAT_FLOAT
8073 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008074 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008076 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008078 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008079 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"delete", 1, 1, f_delete},
8081 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008082 {"diff_filler", 1, 1, f_diff_filler},
8083 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008084 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008086 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"eventhandler", 0, 0, f_eventhandler},
8088 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008089 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008091#ifdef FEAT_FLOAT
8092 {"exp", 1, 1, f_exp},
8093#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008094 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008095 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008096 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8098 {"filereadable", 1, 1, f_filereadable},
8099 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008100 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008101 {"finddir", 1, 3, f_finddir},
8102 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008103#ifdef FEAT_FLOAT
8104 {"float2nr", 1, 1, f_float2nr},
8105 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008106 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008107#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008108 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"fnamemodify", 2, 2, f_fnamemodify},
8110 {"foldclosed", 1, 1, f_foldclosed},
8111 {"foldclosedend", 1, 1, f_foldclosedend},
8112 {"foldlevel", 1, 1, f_foldlevel},
8113 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008114 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008116 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008117 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008118 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008119 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008120 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"getchar", 0, 1, f_getchar},
8122 {"getcharmod", 0, 0, f_getcharmod},
8123 {"getcmdline", 0, 0, f_getcmdline},
8124 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008125 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008126 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008127 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008129 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008130 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"getfsize", 1, 1, f_getfsize},
8132 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008133 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008134 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008135 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008136 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008137 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008138 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008139 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008140 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008142 {"gettabvar", 2, 3, f_gettabvar},
8143 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"getwinposx", 0, 0, f_getwinposx},
8145 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008146 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008147 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02008148 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008150 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008151 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008152 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8154 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8155 {"histadd", 2, 2, f_histadd},
8156 {"histdel", 1, 2, f_histdel},
8157 {"histget", 1, 2, f_histget},
8158 {"histnr", 1, 1, f_histnr},
8159 {"hlID", 1, 1, f_hlID},
8160 {"hlexists", 1, 1, f_hlexists},
8161 {"hostname", 0, 0, f_hostname},
8162 {"iconv", 3, 3, f_iconv},
8163 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008164 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008165 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008167 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {"inputrestore", 0, 0, f_inputrestore},
8169 {"inputsave", 0, 0, f_inputsave},
8170 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008171 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008172 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008174 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008175 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008176 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008177 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008179 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"libcall", 3, 3, f_libcall},
8181 {"libcallnr", 3, 3, f_libcallnr},
8182 {"line", 1, 1, f_line},
8183 {"line2byte", 1, 1, f_line2byte},
8184 {"lispindent", 1, 1, f_lispindent},
8185 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008186#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008187 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008188 {"log10", 1, 1, f_log10},
8189#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008190#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008191 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008192#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008193 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008194 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008195 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008196 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008197 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008198 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008199 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008200 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008201 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008202 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008203 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008204 {"max", 1, 1, f_max},
8205 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008206#ifdef vim_mkdir
8207 {"mkdir", 1, 3, f_mkdir},
8208#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008209 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008210#ifdef FEAT_MZSCHEME
8211 {"mzeval", 1, 1, f_mzeval},
8212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008214 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008215 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008216 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008217#ifdef FEAT_FLOAT
8218 {"pow", 2, 2, f_pow},
8219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008221 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008222 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008223#ifdef FEAT_PYTHON3
8224 {"py3eval", 1, 1, f_py3eval},
8225#endif
8226#ifdef FEAT_PYTHON
8227 {"pyeval", 1, 1, f_pyeval},
8228#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008229 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008230 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008231 {"reltime", 0, 2, f_reltime},
8232 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {"remote_expr", 2, 3, f_remote_expr},
8234 {"remote_foreground", 1, 1, f_remote_foreground},
8235 {"remote_peek", 1, 2, f_remote_peek},
8236 {"remote_read", 1, 1, f_remote_read},
8237 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008238 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008240 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008242 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008243#ifdef FEAT_FLOAT
8244 {"round", 1, 1, f_round},
8245#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008246 {"screenattr", 2, 2, f_screenattr},
8247 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008248 {"screencol", 0, 0, f_screencol},
8249 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008250 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008251 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008252 {"searchpair", 3, 7, f_searchpair},
8253 {"searchpairpos", 3, 7, f_searchpairpos},
8254 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {"server2client", 2, 2, f_server2client},
8256 {"serverlist", 0, 0, f_serverlist},
8257 {"setbufvar", 3, 3, f_setbufvar},
8258 {"setcmdpos", 1, 1, f_setcmdpos},
8259 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008260 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008261 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008262 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008263 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008265 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008266 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008268#ifdef FEAT_CRYPT
8269 {"sha256", 1, 1, f_sha256},
8270#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008271 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008272 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008274#ifdef FEAT_FLOAT
8275 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008276 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008277#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008278 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008279 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008280 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008281 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008282 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008283#ifdef FEAT_FLOAT
8284 {"sqrt", 1, 1, f_sqrt},
8285 {"str2float", 1, 1, f_str2float},
8286#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008287 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008288 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008289 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290#ifdef HAVE_STRFTIME
8291 {"strftime", 1, 2, f_strftime},
8292#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008293 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008294 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {"strlen", 1, 1, f_strlen},
8296 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008297 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008299 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008300 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"substitute", 4, 4, f_substitute},
8302 {"synID", 3, 3, f_synID},
8303 {"synIDattr", 2, 3, f_synIDattr},
8304 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008305 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008306 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008307 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008308 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008309 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008310 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008311 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008312 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008313 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008314#ifdef FEAT_FLOAT
8315 {"tan", 1, 1, f_tan},
8316 {"tanh", 1, 1, f_tanh},
8317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008319 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {"tolower", 1, 1, f_tolower},
8321 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008322 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008323#ifdef FEAT_FLOAT
8324 {"trunc", 1, 1, f_trunc},
8325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008327 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008328 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008329 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008330 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {"virtcol", 1, 1, f_virtcol},
8332 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008333 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"winbufnr", 1, 1, f_winbufnr},
8335 {"wincol", 0, 0, f_wincol},
8336 {"winheight", 1, 1, f_winheight},
8337 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008338 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008340 {"winrestview", 1, 1, f_winrestview},
8341 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008343 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008344 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345};
8346
8347#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8348
8349/*
8350 * Function given to ExpandGeneric() to obtain the list of internal
8351 * or user defined function names.
8352 */
8353 char_u *
8354get_function_name(xp, idx)
8355 expand_T *xp;
8356 int idx;
8357{
8358 static int intidx = -1;
8359 char_u *name;
8360
8361 if (idx == 0)
8362 intidx = -1;
8363 if (intidx < 0)
8364 {
8365 name = get_user_func_name(xp, idx);
8366 if (name != NULL)
8367 return name;
8368 }
8369 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8370 {
8371 STRCPY(IObuff, functions[intidx].f_name);
8372 STRCAT(IObuff, "(");
8373 if (functions[intidx].f_max_argc == 0)
8374 STRCAT(IObuff, ")");
8375 return IObuff;
8376 }
8377
8378 return NULL;
8379}
8380
8381/*
8382 * Function given to ExpandGeneric() to obtain the list of internal or
8383 * user defined variable or function names.
8384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 char_u *
8386get_expr_name(xp, idx)
8387 expand_T *xp;
8388 int idx;
8389{
8390 static int intidx = -1;
8391 char_u *name;
8392
8393 if (idx == 0)
8394 intidx = -1;
8395 if (intidx < 0)
8396 {
8397 name = get_function_name(xp, idx);
8398 if (name != NULL)
8399 return name;
8400 }
8401 return get_user_var_name(xp, ++intidx);
8402}
8403
8404#endif /* FEAT_CMDL_COMPL */
8405
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008406#if defined(EBCDIC) || defined(PROTO)
8407/*
8408 * Compare struct fst by function name.
8409 */
8410 static int
8411compare_func_name(s1, s2)
8412 const void *s1;
8413 const void *s2;
8414{
8415 struct fst *p1 = (struct fst *)s1;
8416 struct fst *p2 = (struct fst *)s2;
8417
8418 return STRCMP(p1->f_name, p2->f_name);
8419}
8420
8421/*
8422 * Sort the function table by function name.
8423 * The sorting of the table above is ASCII dependant.
8424 * On machines using EBCDIC we have to sort it.
8425 */
8426 static void
8427sortFunctions()
8428{
8429 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8430
8431 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8432}
8433#endif
8434
8435
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436/*
8437 * Find internal function in table above.
8438 * Return index, or -1 if not found
8439 */
8440 static int
8441find_internal_func(name)
8442 char_u *name; /* name of the function */
8443{
8444 int first = 0;
8445 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8446 int cmp;
8447 int x;
8448
8449 /*
8450 * Find the function name in the table. Binary search.
8451 */
8452 while (first <= last)
8453 {
8454 x = first + ((unsigned)(last - first) >> 1);
8455 cmp = STRCMP(name, functions[x].f_name);
8456 if (cmp < 0)
8457 last = x - 1;
8458 else if (cmp > 0)
8459 first = x + 1;
8460 else
8461 return x;
8462 }
8463 return -1;
8464}
8465
8466/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008467 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8468 * name it contains, otherwise return "name".
8469 */
8470 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008471deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008472 char_u *name;
8473 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008474 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008475{
Bram Moolenaar33570922005-01-25 22:26:29 +00008476 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008477 int cc;
8478
8479 cc = name[*lenp];
8480 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008481 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008482 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008483 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008485 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008486 {
8487 *lenp = 0;
8488 return (char_u *)""; /* just in case */
8489 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008490 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008492 }
8493
8494 return name;
8495}
8496
8497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 * Allocate a variable for the result of a function.
8499 * Return OK or FAIL.
8500 */
8501 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008502get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8503 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 char_u *name; /* name of the function */
8505 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 char_u **arg; /* argument, pointing to the '(' */
8508 linenr_T firstline; /* first line of range */
8509 linenr_T lastline; /* last line of range */
8510 int *doesrange; /* return: function handled range */
8511 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008512 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513{
8514 char_u *argp;
8515 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008516 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 int argcount = 0; /* number of arguments found */
8518
8519 /*
8520 * Get the arguments.
8521 */
8522 argp = *arg;
8523 while (argcount < MAX_FUNC_ARGS)
8524 {
8525 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8526 if (*argp == ')' || *argp == ',' || *argp == NUL)
8527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8529 {
8530 ret = FAIL;
8531 break;
8532 }
8533 ++argcount;
8534 if (*argp != ',')
8535 break;
8536 }
8537 if (*argp == ')')
8538 ++argp;
8539 else
8540 ret = FAIL;
8541
8542 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008543 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008544 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 {
8547 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008548 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008549 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008550 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
8553 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008554 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555
8556 *arg = skipwhite(argp);
8557 return ret;
8558}
8559
8560
8561/*
8562 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008563 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008564 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 */
8566 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008567call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008568 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008569 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008573 typval_T *argvars; /* vars for arguments, must have "argcount"
8574 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 linenr_T firstline; /* first line of range */
8576 linenr_T lastline; /* last line of range */
8577 int *doesrange; /* return: function handled range */
8578 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008579 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580{
8581 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582#define ERROR_UNKNOWN 0
8583#define ERROR_TOOMANY 1
8584#define ERROR_TOOFEW 2
8585#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008586#define ERROR_DICT 4
8587#define ERROR_NONE 5
8588#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 int error = ERROR_NONE;
8590 int i;
8591 int llen;
8592 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593#define FLEN_FIXED 40
8594 char_u fname_buf[FLEN_FIXED + 1];
8595 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008596 char_u *name;
8597
8598 /* Make a copy of the name, if it comes from a funcref variable it could
8599 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008600 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008601 if (name == NULL)
8602 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603
8604 /*
8605 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8606 * Change <SNR>123_name() to K_SNR 123_name().
8607 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 llen = eval_fname_script(name);
8610 if (llen > 0)
8611 {
8612 fname_buf[0] = K_SPECIAL;
8613 fname_buf[1] = KS_EXTRA;
8614 fname_buf[2] = (int)KE_SNR;
8615 i = 3;
8616 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8617 {
8618 if (current_SID <= 0)
8619 error = ERROR_SCRIPT;
8620 else
8621 {
8622 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8623 i = (int)STRLEN(fname_buf);
8624 }
8625 }
8626 if (i + STRLEN(name + llen) < FLEN_FIXED)
8627 {
8628 STRCPY(fname_buf + i, name + llen);
8629 fname = fname_buf;
8630 }
8631 else
8632 {
8633 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8634 if (fname == NULL)
8635 error = ERROR_OTHER;
8636 else
8637 {
8638 mch_memmove(fname, fname_buf, (size_t)i);
8639 STRCPY(fname + i, name + llen);
8640 }
8641 }
8642 }
8643 else
8644 fname = name;
8645
8646 *doesrange = FALSE;
8647
8648
8649 /* execute the function if no errors detected and executing */
8650 if (evaluate && error == ERROR_NONE)
8651 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008652 char_u *rfname = fname;
8653
8654 /* Ignore "g:" before a function name. */
8655 if (fname[0] == 'g' && fname[1] == ':')
8656 rfname = fname + 2;
8657
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008658 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8659 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 error = ERROR_UNKNOWN;
8661
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008662 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {
8664 /*
8665 * User defined function.
8666 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008667 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008668
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008670 /* Trigger FuncUndefined event, may load the function. */
8671 if (fp == NULL
8672 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008673 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008674 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008676 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008677 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 }
8679#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008680 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008681 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008682 {
8683 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008684 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008685 }
8686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 if (fp != NULL)
8688 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008689 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008691 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008693 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008695 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008696 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 else
8698 {
8699 /*
8700 * Call the user function.
8701 * Save and restore search patterns, script variables and
8702 * redo buffer.
8703 */
8704 save_search_patterns();
8705 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008706 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008708 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008709 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8710 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8711 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008712 /* Function was unreferenced while being used, free it
8713 * now. */
8714 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 restoreRedobuff();
8716 restore_search_patterns();
8717 error = ERROR_NONE;
8718 }
8719 }
8720 }
8721 else
8722 {
8723 /*
8724 * Find the function name in the table, call its implementation.
8725 */
8726 i = find_internal_func(fname);
8727 if (i >= 0)
8728 {
8729 if (argcount < functions[i].f_min_argc)
8730 error = ERROR_TOOFEW;
8731 else if (argcount > functions[i].f_max_argc)
8732 error = ERROR_TOOMANY;
8733 else
8734 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008735 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 error = ERROR_NONE;
8738 }
8739 }
8740 }
8741 /*
8742 * The function call (or "FuncUndefined" autocommand sequence) might
8743 * have been aborted by an error, an interrupt, or an explicitly thrown
8744 * exception that has not been caught so far. This situation can be
8745 * tested for by calling aborting(). For an error in an internal
8746 * function or for the "E132" error in call_user_func(), however, the
8747 * throw point at which the "force_abort" flag (temporarily reset by
8748 * emsg()) is normally updated has not been reached yet. We need to
8749 * update that flag first to make aborting() reliable.
8750 */
8751 update_force_abort();
8752 }
8753 if (error == ERROR_NONE)
8754 ret = OK;
8755
8756 /*
8757 * Report an error unless the argument evaluation or function call has been
8758 * cancelled due to an aborting error, an interrupt, or an exception.
8759 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008760 if (!aborting())
8761 {
8762 switch (error)
8763 {
8764 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008765 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008766 break;
8767 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008768 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008769 break;
8770 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008771 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008772 name);
8773 break;
8774 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008775 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008776 name);
8777 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008778 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008779 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008780 name);
8781 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008782 }
8783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 if (fname != name && fname != fname_buf)
8786 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008787 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788
8789 return ret;
8790}
8791
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008792/*
8793 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008794 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008795 */
8796 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008797emsg_funcname(ermsg, name)
8798 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008799 char_u *name;
8800{
8801 char_u *p;
8802
8803 if (*name == K_SPECIAL)
8804 p = concat_str((char_u *)"<SNR>", name + 3);
8805 else
8806 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008807 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008808 if (p != name)
8809 vim_free(p);
8810}
8811
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008812/*
8813 * Return TRUE for a non-zero Number and a non-empty String.
8814 */
8815 static int
8816non_zero_arg(argvars)
8817 typval_T *argvars;
8818{
8819 return ((argvars[0].v_type == VAR_NUMBER
8820 && argvars[0].vval.v_number != 0)
8821 || (argvars[0].v_type == VAR_STRING
8822 && argvars[0].vval.v_string != NULL
8823 && *argvars[0].vval.v_string != NUL));
8824}
8825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826/*********************************************
8827 * Implementation of the built-in functions
8828 */
8829
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008830#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008831static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8832
8833/*
8834 * Get the float value of "argvars[0]" into "f".
8835 * Returns FAIL when the argument is not a Number or Float.
8836 */
8837 static int
8838get_float_arg(argvars, f)
8839 typval_T *argvars;
8840 float_T *f;
8841{
8842 if (argvars[0].v_type == VAR_FLOAT)
8843 {
8844 *f = argvars[0].vval.v_float;
8845 return OK;
8846 }
8847 if (argvars[0].v_type == VAR_NUMBER)
8848 {
8849 *f = (float_T)argvars[0].vval.v_number;
8850 return OK;
8851 }
8852 EMSG(_("E808: Number or Float required"));
8853 return FAIL;
8854}
8855
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008856/*
8857 * "abs(expr)" function
8858 */
8859 static void
8860f_abs(argvars, rettv)
8861 typval_T *argvars;
8862 typval_T *rettv;
8863{
8864 if (argvars[0].v_type == VAR_FLOAT)
8865 {
8866 rettv->v_type = VAR_FLOAT;
8867 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8868 }
8869 else
8870 {
8871 varnumber_T n;
8872 int error = FALSE;
8873
8874 n = get_tv_number_chk(&argvars[0], &error);
8875 if (error)
8876 rettv->vval.v_number = -1;
8877 else if (n > 0)
8878 rettv->vval.v_number = n;
8879 else
8880 rettv->vval.v_number = -n;
8881 }
8882}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008883
8884/*
8885 * "acos()" function
8886 */
8887 static void
8888f_acos(argvars, rettv)
8889 typval_T *argvars;
8890 typval_T *rettv;
8891{
8892 float_T f;
8893
8894 rettv->v_type = VAR_FLOAT;
8895 if (get_float_arg(argvars, &f) == OK)
8896 rettv->vval.v_float = acos(f);
8897 else
8898 rettv->vval.v_float = 0.0;
8899}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008900#endif
8901
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008903 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 */
8905 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008906f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008907 typval_T *argvars;
8908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
Bram Moolenaar33570922005-01-25 22:26:29 +00008910 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008912 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008913 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008915 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008916 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008917 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008919 }
8920 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008921 EMSG(_(e_listreq));
8922}
8923
8924/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008925 * "and(expr, expr)" function
8926 */
8927 static void
8928f_and(argvars, rettv)
8929 typval_T *argvars;
8930 typval_T *rettv;
8931{
8932 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8933 & get_tv_number_chk(&argvars[1], NULL);
8934}
8935
8936/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008937 * "append(lnum, string/list)" function
8938 */
8939 static void
8940f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008941 typval_T *argvars;
8942 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008943{
8944 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008945 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 list_T *l = NULL;
8947 listitem_T *li = NULL;
8948 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008949 long added = 0;
8950
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008951 /* When coming here from Insert mode, sync undo, so that this can be
8952 * undone separately from what was previously inserted. */
8953 if (u_sync_once == 2)
8954 {
8955 u_sync_once = 1; /* notify that u_sync() was called */
8956 u_sync(TRUE);
8957 }
8958
Bram Moolenaar0d660222005-01-07 21:51:51 +00008959 lnum = get_tv_lnum(argvars);
8960 if (lnum >= 0
8961 && lnum <= curbuf->b_ml.ml_line_count
8962 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008963 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008964 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008965 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008966 l = argvars[1].vval.v_list;
8967 if (l == NULL)
8968 return;
8969 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008971 for (;;)
8972 {
8973 if (l == NULL)
8974 tv = &argvars[1]; /* append a string */
8975 else if (li == NULL)
8976 break; /* end of list */
8977 else
8978 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008979 line = get_tv_string_chk(tv);
8980 if (line == NULL) /* type error */
8981 {
8982 rettv->vval.v_number = 1; /* Failed */
8983 break;
8984 }
8985 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008986 ++added;
8987 if (l == NULL)
8988 break;
8989 li = li->li_next;
8990 }
8991
8992 appended_lines_mark(lnum, added);
8993 if (curwin->w_cursor.lnum > lnum)
8994 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008996 else
8997 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998}
8999
9000/*
9001 * "argc()" function
9002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009005 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009008 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009}
9010
9011/*
9012 * "argidx()" function
9013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009016 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020}
9021
9022/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009023 * "arglistid()" function
9024 */
9025 static void
9026f_arglistid(argvars, rettv)
9027 typval_T *argvars UNUSED;
9028 typval_T *rettv;
9029{
9030 win_T *wp;
9031 tabpage_T *tp = NULL;
9032 long n;
9033
9034 rettv->vval.v_number = -1;
9035 if (argvars[0].v_type != VAR_UNKNOWN)
9036 {
9037 if (argvars[1].v_type != VAR_UNKNOWN)
9038 {
9039 n = get_tv_number(&argvars[1]);
9040 if (n >= 0)
9041 tp = find_tabpage(n);
9042 }
9043 else
9044 tp = curtab;
9045
9046 if (tp != NULL)
9047 {
9048 wp = find_win_by_nr(&argvars[0], tp);
9049 if (wp != NULL)
9050 rettv->vval.v_number = wp->w_alist->id;
9051 }
9052 }
9053 else
9054 rettv->vval.v_number = curwin->w_alist->id;
9055}
9056
9057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 * "argv(nr)" function
9059 */
9060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *argvars;
9063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065 int idx;
9066
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009067 if (argvars[0].v_type != VAR_UNKNOWN)
9068 {
9069 idx = get_tv_number_chk(&argvars[0], NULL);
9070 if (idx >= 0 && idx < ARGCOUNT)
9071 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9072 else
9073 rettv->vval.v_string = NULL;
9074 rettv->v_type = VAR_STRING;
9075 }
9076 else if (rettv_list_alloc(rettv) == OK)
9077 for (idx = 0; idx < ARGCOUNT; ++idx)
9078 list_append_string(rettv->vval.v_list,
9079 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080}
9081
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009082#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009083/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009084 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009085 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009086 static void
9087f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009088 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009089 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009090{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009091 float_T f;
9092
9093 rettv->v_type = VAR_FLOAT;
9094 if (get_float_arg(argvars, &f) == OK)
9095 rettv->vval.v_float = asin(f);
9096 else
9097 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009098}
9099
9100/*
9101 * "atan()" function
9102 */
9103 static void
9104f_atan(argvars, rettv)
9105 typval_T *argvars;
9106 typval_T *rettv;
9107{
9108 float_T f;
9109
9110 rettv->v_type = VAR_FLOAT;
9111 if (get_float_arg(argvars, &f) == OK)
9112 rettv->vval.v_float = atan(f);
9113 else
9114 rettv->vval.v_float = 0.0;
9115}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009116
9117/*
9118 * "atan2()" function
9119 */
9120 static void
9121f_atan2(argvars, rettv)
9122 typval_T *argvars;
9123 typval_T *rettv;
9124{
9125 float_T fx, fy;
9126
9127 rettv->v_type = VAR_FLOAT;
9128 if (get_float_arg(argvars, &fx) == OK
9129 && get_float_arg(&argvars[1], &fy) == OK)
9130 rettv->vval.v_float = atan2(fx, fy);
9131 else
9132 rettv->vval.v_float = 0.0;
9133}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009134#endif
9135
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136/*
9137 * "browse(save, title, initdir, default)" function
9138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009141 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
9144#ifdef FEAT_BROWSE
9145 int save;
9146 char_u *title;
9147 char_u *initdir;
9148 char_u *defname;
9149 char_u buf[NUMBUFLEN];
9150 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009151 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009153 save = get_tv_number_chk(&argvars[0], &error);
9154 title = get_tv_string_chk(&argvars[1]);
9155 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9156 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009158 if (error || title == NULL || initdir == NULL || defname == NULL)
9159 rettv->vval.v_string = NULL;
9160 else
9161 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009162 do_browse(save ? BROWSE_SAVE : 0,
9163 title, defname, NULL, initdir, NULL, curbuf);
9164#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009166#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009168}
9169
9170/*
9171 * "browsedir(title, initdir)" function
9172 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009175 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009177{
9178#ifdef FEAT_BROWSE
9179 char_u *title;
9180 char_u *initdir;
9181 char_u buf[NUMBUFLEN];
9182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 title = get_tv_string_chk(&argvars[0]);
9184 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009186 if (title == NULL || initdir == NULL)
9187 rettv->vval.v_string = NULL;
9188 else
9189 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009190 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195}
9196
Bram Moolenaar33570922005-01-25 22:26:29 +00009197static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009198
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199/*
9200 * Find a buffer by number or exact name.
9201 */
9202 static buf_T *
9203find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009204 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205{
9206 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009208 if (avar->v_type == VAR_NUMBER)
9209 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009210 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009212 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009213 if (buf == NULL)
9214 {
9215 /* No full path name match, try a match with a URL or a "nofile"
9216 * buffer, these don't use the full path. */
9217 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9218 if (buf->b_fname != NULL
9219 && (path_with_url(buf->b_fname)
9220#ifdef FEAT_QUICKFIX
9221 || bt_nofile(buf)
9222#endif
9223 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009224 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009225 break;
9226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 }
9228 return buf;
9229}
9230
9231/*
9232 * "bufexists(expr)" function
9233 */
9234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009235f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009236 typval_T *argvars;
9237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009239 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240}
9241
9242/*
9243 * "buflisted(expr)" function
9244 */
9245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009246f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009247 typval_T *argvars;
9248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249{
9250 buf_T *buf;
9251
9252 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254}
9255
9256/*
9257 * "bufloaded(expr)" function
9258 */
9259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009261 typval_T *argvars;
9262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263{
9264 buf_T *buf;
9265
9266 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009267 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268}
9269
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009270static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009271
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272/*
9273 * Get buffer by number or pattern.
9274 */
9275 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009276get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009277 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009278 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 int save_magic;
9282 char_u *save_cpo;
9283 buf_T *buf;
9284
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009285 if (tv->v_type == VAR_NUMBER)
9286 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009287 if (tv->v_type != VAR_STRING)
9288 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 if (name == NULL || *name == NUL)
9290 return curbuf;
9291 if (name[0] == '$' && name[1] == NUL)
9292 return lastbuf;
9293
9294 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9295 save_magic = p_magic;
9296 p_magic = TRUE;
9297 save_cpo = p_cpo;
9298 p_cpo = (char_u *)"";
9299
9300 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009301 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302
9303 p_magic = save_magic;
9304 p_cpo = save_cpo;
9305
9306 /* If not found, try expanding the name, like done for bufexists(). */
9307 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009308 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309
9310 return buf;
9311}
9312
9313/*
9314 * "bufname(expr)" function
9315 */
9316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009318 typval_T *argvars;
9319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 buf_T *buf;
9322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009325 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 --emsg_off;
9332}
9333
9334/*
9335 * "bufnr(expr)" function
9336 */
9337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009338f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009339 typval_T *argvars;
9340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341{
9342 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009343 int error = FALSE;
9344 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009346 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009348 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009349 --emsg_off;
9350
9351 /* If the buffer isn't found and the second argument is not zero create a
9352 * new buffer. */
9353 if (buf == NULL
9354 && argvars[1].v_type != VAR_UNKNOWN
9355 && get_tv_number_chk(&argvars[1], &error) != 0
9356 && !error
9357 && (name = get_tv_string_chk(&argvars[0])) != NULL
9358 && !error)
9359 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9360
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365}
9366
9367/*
9368 * "bufwinnr(nr)" function
9369 */
9370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009372 typval_T *argvars;
9373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
9375#ifdef FEAT_WINDOWS
9376 win_T *wp;
9377 int winnr = 0;
9378#endif
9379 buf_T *buf;
9380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009383 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384#ifdef FEAT_WINDOWS
9385 for (wp = firstwin; wp; wp = wp->w_next)
9386 {
9387 ++winnr;
9388 if (wp->w_buffer == buf)
9389 break;
9390 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394#endif
9395 --emsg_off;
9396}
9397
9398/*
9399 * "byte2line(byte)" function
9400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009403 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405{
9406#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408#else
9409 long boff = 0;
9410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009411 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 (linenr_T)0, &boff);
9417#endif
9418}
9419
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009420 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009421byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009422 typval_T *argvars;
9423 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009424 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009425{
9426#ifdef FEAT_MBYTE
9427 char_u *t;
9428#endif
9429 char_u *str;
9430 long idx;
9431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009432 str = get_tv_string_chk(&argvars[0]);
9433 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009435 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009436 return;
9437
9438#ifdef FEAT_MBYTE
9439 t = str;
9440 for ( ; idx > 0; idx--)
9441 {
9442 if (*t == NUL) /* EOL reached */
9443 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009444 if (enc_utf8 && comp)
9445 t += utf_ptr2len(t);
9446 else
9447 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009448 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009449 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009450#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009451 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009453#endif
9454}
9455
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009456/*
9457 * "byteidx()" function
9458 */
9459 static void
9460f_byteidx(argvars, rettv)
9461 typval_T *argvars;
9462 typval_T *rettv;
9463{
9464 byteidx(argvars, rettv, FALSE);
9465}
9466
9467/*
9468 * "byteidxcomp()" function
9469 */
9470 static void
9471f_byteidxcomp(argvars, rettv)
9472 typval_T *argvars;
9473 typval_T *rettv;
9474{
9475 byteidx(argvars, rettv, TRUE);
9476}
9477
Bram Moolenaardb913952012-06-29 12:54:53 +02009478 int
9479func_call(name, args, selfdict, rettv)
9480 char_u *name;
9481 typval_T *args;
9482 dict_T *selfdict;
9483 typval_T *rettv;
9484{
9485 listitem_T *item;
9486 typval_T argv[MAX_FUNC_ARGS + 1];
9487 int argc = 0;
9488 int dummy;
9489 int r = 0;
9490
9491 for (item = args->vval.v_list->lv_first; item != NULL;
9492 item = item->li_next)
9493 {
9494 if (argc == MAX_FUNC_ARGS)
9495 {
9496 EMSG(_("E699: Too many arguments"));
9497 break;
9498 }
9499 /* Make a copy of each argument. This is needed to be able to set
9500 * v_lock to VAR_FIXED in the copy without changing the original list.
9501 */
9502 copy_tv(&item->li_tv, &argv[argc++]);
9503 }
9504
9505 if (item == NULL)
9506 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9507 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9508 &dummy, TRUE, selfdict);
9509
9510 /* Free the arguments. */
9511 while (argc > 0)
9512 clear_tv(&argv[--argc]);
9513
9514 return r;
9515}
9516
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009517/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009518 * "call(func, arglist)" function
9519 */
9520 static void
9521f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009522 typval_T *argvars;
9523 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009524{
9525 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009526 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009527
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009528 if (argvars[1].v_type != VAR_LIST)
9529 {
9530 EMSG(_(e_listreq));
9531 return;
9532 }
9533 if (argvars[1].vval.v_list == NULL)
9534 return;
9535
9536 if (argvars[0].v_type == VAR_FUNC)
9537 func = argvars[0].vval.v_string;
9538 else
9539 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009540 if (*func == NUL)
9541 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009542
Bram Moolenaare9a41262005-01-15 22:18:47 +00009543 if (argvars[2].v_type != VAR_UNKNOWN)
9544 {
9545 if (argvars[2].v_type != VAR_DICT)
9546 {
9547 EMSG(_(e_dictreq));
9548 return;
9549 }
9550 selfdict = argvars[2].vval.v_dict;
9551 }
9552
Bram Moolenaardb913952012-06-29 12:54:53 +02009553 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009554}
9555
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009556#ifdef FEAT_FLOAT
9557/*
9558 * "ceil({float})" function
9559 */
9560 static void
9561f_ceil(argvars, rettv)
9562 typval_T *argvars;
9563 typval_T *rettv;
9564{
9565 float_T f;
9566
9567 rettv->v_type = VAR_FLOAT;
9568 if (get_float_arg(argvars, &f) == OK)
9569 rettv->vval.v_float = ceil(f);
9570 else
9571 rettv->vval.v_float = 0.0;
9572}
9573#endif
9574
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009575/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009576 * "changenr()" function
9577 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009578 static void
9579f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009580 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009581 typval_T *rettv;
9582{
9583 rettv->vval.v_number = curbuf->b_u_seq_cur;
9584}
9585
9586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 * "char2nr(string)" function
9588 */
9589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009590f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009591 typval_T *argvars;
9592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
9594#ifdef FEAT_MBYTE
9595 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009596 {
9597 int utf8 = 0;
9598
9599 if (argvars[1].v_type != VAR_UNKNOWN)
9600 utf8 = get_tv_number_chk(&argvars[1], NULL);
9601
9602 if (utf8)
9603 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9604 else
9605 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 else
9608#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610}
9611
9612/*
9613 * "cindent(lnum)" function
9614 */
9615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009617 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619{
9620#ifdef FEAT_CINDENT
9621 pos_T pos;
9622 linenr_T lnum;
9623
9624 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9627 {
9628 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 curwin->w_cursor = pos;
9631 }
9632 else
9633#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635}
9636
9637/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009638 * "clearmatches()" function
9639 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009640 static void
9641f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009642 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009643 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009644{
9645#ifdef FEAT_SEARCH_EXTRA
9646 clear_matches(curwin);
9647#endif
9648}
9649
9650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 * "col(string)" function
9652 */
9653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009654f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009655 typval_T *argvars;
9656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
9658 colnr_T col = 0;
9659 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009660 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009662 fp = var2fpos(&argvars[0], FALSE, &fnum);
9663 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 {
9665 if (fp->col == MAXCOL)
9666 {
9667 /* '> can be MAXCOL, get the length of the line then */
9668 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009669 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 else
9671 col = MAXCOL;
9672 }
9673 else
9674 {
9675 col = fp->col + 1;
9676#ifdef FEAT_VIRTUALEDIT
9677 /* col(".") when the cursor is on the NUL at the end of the line
9678 * because of "coladd" can be seen as an extra column. */
9679 if (virtual_active() && fp == &curwin->w_cursor)
9680 {
9681 char_u *p = ml_get_cursor();
9682
9683 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9684 curwin->w_virtcol - curwin->w_cursor.coladd))
9685 {
9686# ifdef FEAT_MBYTE
9687 int l;
9688
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009689 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 col += l;
9691# else
9692 if (*p != NUL && p[1] == NUL)
9693 ++col;
9694# endif
9695 }
9696 }
9697#endif
9698 }
9699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701}
9702
Bram Moolenaar572cb562005-08-05 21:35:02 +00009703#if defined(FEAT_INS_EXPAND)
9704/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009705 * "complete()" function
9706 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009707 static void
9708f_complete(argvars, rettv)
9709 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009710 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009711{
9712 int startcol;
9713
9714 if ((State & INSERT) == 0)
9715 {
9716 EMSG(_("E785: complete() can only be used in Insert mode"));
9717 return;
9718 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009719
9720 /* Check for undo allowed here, because if something was already inserted
9721 * the line was already saved for undo and this check isn't done. */
9722 if (!undo_allowed())
9723 return;
9724
Bram Moolenaarade00832006-03-10 21:46:58 +00009725 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9726 {
9727 EMSG(_(e_invarg));
9728 return;
9729 }
9730
9731 startcol = get_tv_number_chk(&argvars[0], NULL);
9732 if (startcol <= 0)
9733 return;
9734
9735 set_completion(startcol - 1, argvars[1].vval.v_list);
9736}
9737
9738/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009739 * "complete_add()" function
9740 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009741 static void
9742f_complete_add(argvars, rettv)
9743 typval_T *argvars;
9744 typval_T *rettv;
9745{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009746 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009747}
9748
9749/*
9750 * "complete_check()" function
9751 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009752 static void
9753f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009754 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009755 typval_T *rettv;
9756{
9757 int saved = RedrawingDisabled;
9758
9759 RedrawingDisabled = 0;
9760 ins_compl_check_keys(0);
9761 rettv->vval.v_number = compl_interrupted;
9762 RedrawingDisabled = saved;
9763}
9764#endif
9765
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766/*
9767 * "confirm(message, buttons[, default [, type]])" function
9768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009770f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009771 typval_T *argvars UNUSED;
9772 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773{
9774#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9775 char_u *message;
9776 char_u *buttons = NULL;
9777 char_u buf[NUMBUFLEN];
9778 char_u buf2[NUMBUFLEN];
9779 int def = 1;
9780 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 char_u *typestr;
9782 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 message = get_tv_string_chk(&argvars[0]);
9785 if (message == NULL)
9786 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009787 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009789 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9790 if (buttons == NULL)
9791 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009792 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009794 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009795 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009797 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9798 if (typestr == NULL)
9799 error = TRUE;
9800 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009802 switch (TOUPPER_ASC(*typestr))
9803 {
9804 case 'E': type = VIM_ERROR; break;
9805 case 'Q': type = VIM_QUESTION; break;
9806 case 'I': type = VIM_INFO; break;
9807 case 'W': type = VIM_WARNING; break;
9808 case 'G': type = VIM_GENERIC; break;
9809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 }
9811 }
9812 }
9813 }
9814
9815 if (buttons == NULL || *buttons == NUL)
9816 buttons = (char_u *)_("&Ok");
9817
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009818 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009819 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009820 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821#endif
9822}
9823
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009824/*
9825 * "copy()" function
9826 */
9827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009829 typval_T *argvars;
9830 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009831{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009832 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009835#ifdef FEAT_FLOAT
9836/*
9837 * "cos()" function
9838 */
9839 static void
9840f_cos(argvars, rettv)
9841 typval_T *argvars;
9842 typval_T *rettv;
9843{
9844 float_T f;
9845
9846 rettv->v_type = VAR_FLOAT;
9847 if (get_float_arg(argvars, &f) == OK)
9848 rettv->vval.v_float = cos(f);
9849 else
9850 rettv->vval.v_float = 0.0;
9851}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009852
9853/*
9854 * "cosh()" function
9855 */
9856 static void
9857f_cosh(argvars, rettv)
9858 typval_T *argvars;
9859 typval_T *rettv;
9860{
9861 float_T f;
9862
9863 rettv->v_type = VAR_FLOAT;
9864 if (get_float_arg(argvars, &f) == OK)
9865 rettv->vval.v_float = cosh(f);
9866 else
9867 rettv->vval.v_float = 0.0;
9868}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009869#endif
9870
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009872 * "count()" function
9873 */
9874 static void
9875f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009876 typval_T *argvars;
9877 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009878{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009879 long n = 0;
9880 int ic = FALSE;
9881
Bram Moolenaare9a41262005-01-15 22:18:47 +00009882 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009883 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009884 listitem_T *li;
9885 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009886 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009887
Bram Moolenaare9a41262005-01-15 22:18:47 +00009888 if ((l = argvars[0].vval.v_list) != NULL)
9889 {
9890 li = l->lv_first;
9891 if (argvars[2].v_type != VAR_UNKNOWN)
9892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 int error = FALSE;
9894
9895 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009896 if (argvars[3].v_type != VAR_UNKNOWN)
9897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898 idx = get_tv_number_chk(&argvars[3], &error);
9899 if (!error)
9900 {
9901 li = list_find(l, idx);
9902 if (li == NULL)
9903 EMSGN(_(e_listidx), idx);
9904 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009906 if (error)
9907 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009908 }
9909
9910 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009911 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 ++n;
9913 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009914 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009915 else if (argvars[0].v_type == VAR_DICT)
9916 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009917 int todo;
9918 dict_T *d;
9919 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009921 if ((d = argvars[0].vval.v_dict) != NULL)
9922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 int error = FALSE;
9924
Bram Moolenaare9a41262005-01-15 22:18:47 +00009925 if (argvars[2].v_type != VAR_UNKNOWN)
9926 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009927 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 if (argvars[3].v_type != VAR_UNKNOWN)
9929 EMSG(_(e_invarg));
9930 }
9931
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009932 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009933 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009934 {
9935 if (!HASHITEM_EMPTY(hi))
9936 {
9937 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009938 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009939 ++n;
9940 }
9941 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009942 }
9943 }
9944 else
9945 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009946 rettv->vval.v_number = n;
9947}
9948
9949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9951 *
9952 * Checks the existence of a cscope connection.
9953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009955f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009956 typval_T *argvars UNUSED;
9957 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958{
9959#ifdef FEAT_CSCOPE
9960 int num = 0;
9961 char_u *dbpath = NULL;
9962 char_u *prepend = NULL;
9963 char_u buf[NUMBUFLEN];
9964
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009965 if (argvars[0].v_type != VAR_UNKNOWN
9966 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968 num = (int)get_tv_number(&argvars[0]);
9969 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009970 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 }
9973
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975#endif
9976}
9977
9978/*
9979 * "cursor(lnum, col)" function
9980 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009981 * Moves the cursor to the specified line and column.
9982 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009986 typval_T *argvars;
9987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
9989 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009990#ifdef FEAT_VIRTUALEDIT
9991 long coladd = 0;
9992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009994 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009995 if (argvars[1].v_type == VAR_UNKNOWN)
9996 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009997 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +02009998 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009999
Bram Moolenaar493c1782014-05-28 14:34:46 +020010000 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010001 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010002 line = pos.lnum;
10003 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010004#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010005 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010006#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010007 if (curswant >= 0)
10008 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010009 }
10010 else
10011 {
10012 line = get_tv_lnum(argvars);
10013 col = get_tv_number_chk(&argvars[1], NULL);
10014#ifdef FEAT_VIRTUALEDIT
10015 if (argvars[2].v_type != VAR_UNKNOWN)
10016 coladd = get_tv_number_chk(&argvars[2], NULL);
10017#endif
10018 }
10019 if (line < 0 || col < 0
10020#ifdef FEAT_VIRTUALEDIT
10021 || coladd < 0
10022#endif
10023 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010024 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 if (line > 0)
10026 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 if (col > 0)
10028 curwin->w_cursor.col = col - 1;
10029#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010030 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031#endif
10032
10033 /* Make sure the cursor is in a valid position. */
10034 check_cursor();
10035#ifdef FEAT_MBYTE
10036 /* Correct cursor for multi-byte character. */
10037 if (has_mbyte)
10038 mb_adjust_cursor();
10039#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010040
10041 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010042 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043}
10044
10045/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010046 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 */
10048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010050 typval_T *argvars;
10051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010053 int noref = 0;
10054
10055 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010056 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010057 if (noref < 0 || noref > 1)
10058 EMSG(_(e_invarg));
10059 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010060 {
10061 current_copyID += COPYID_INC;
10062 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064}
10065
10066/*
10067 * "delete()" function
10068 */
10069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010071 typval_T *argvars;
10072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073{
10074 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079
10080/*
10081 * "did_filetype()" function
10082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010085 typval_T *argvars UNUSED;
10086 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
10088#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090#endif
10091}
10092
10093/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010094 * "diff_filler()" function
10095 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010098 typval_T *argvars UNUSED;
10099 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010100{
10101#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010103#endif
10104}
10105
10106/*
10107 * "diff_hlID()" function
10108 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010111 typval_T *argvars UNUSED;
10112 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010113{
10114#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010116 static linenr_T prev_lnum = 0;
10117 static int changedtick = 0;
10118 static int fnum = 0;
10119 static int change_start = 0;
10120 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010121 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010122 int filler_lines;
10123 int col;
10124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010125 if (lnum < 0) /* ignore type error in {lnum} arg */
10126 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010127 if (lnum != prev_lnum
10128 || changedtick != curbuf->b_changedtick
10129 || fnum != curbuf->b_fnum)
10130 {
10131 /* New line, buffer, change: need to get the values. */
10132 filler_lines = diff_check(curwin, lnum);
10133 if (filler_lines < 0)
10134 {
10135 if (filler_lines == -1)
10136 {
10137 change_start = MAXCOL;
10138 change_end = -1;
10139 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10140 hlID = HLF_ADD; /* added line */
10141 else
10142 hlID = HLF_CHD; /* changed line */
10143 }
10144 else
10145 hlID = HLF_ADD; /* added line */
10146 }
10147 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010148 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010149 prev_lnum = lnum;
10150 changedtick = curbuf->b_changedtick;
10151 fnum = curbuf->b_fnum;
10152 }
10153
10154 if (hlID == HLF_CHD || hlID == HLF_TXD)
10155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010156 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010157 if (col >= change_start && col <= change_end)
10158 hlID = HLF_TXD; /* changed text */
10159 else
10160 hlID = HLF_CHD; /* changed line */
10161 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010162 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010163#endif
10164}
10165
10166/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010167 * "empty({expr})" function
10168 */
10169 static void
10170f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010171 typval_T *argvars;
10172 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010173{
10174 int n;
10175
10176 switch (argvars[0].v_type)
10177 {
10178 case VAR_STRING:
10179 case VAR_FUNC:
10180 n = argvars[0].vval.v_string == NULL
10181 || *argvars[0].vval.v_string == NUL;
10182 break;
10183 case VAR_NUMBER:
10184 n = argvars[0].vval.v_number == 0;
10185 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010186#ifdef FEAT_FLOAT
10187 case VAR_FLOAT:
10188 n = argvars[0].vval.v_float == 0.0;
10189 break;
10190#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010191 case VAR_LIST:
10192 n = argvars[0].vval.v_list == NULL
10193 || argvars[0].vval.v_list->lv_first == NULL;
10194 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010195 case VAR_DICT:
10196 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010197 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010198 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010199 default:
10200 EMSG2(_(e_intern2), "f_empty()");
10201 n = 0;
10202 }
10203
10204 rettv->vval.v_number = n;
10205}
10206
10207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 * "escape({string}, {chars})" function
10209 */
10210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010211f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010212 typval_T *argvars;
10213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215 char_u buf[NUMBUFLEN];
10216
Bram Moolenaar758711c2005-02-02 23:11:38 +000010217 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10218 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010219 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220}
10221
10222/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010223 * "eval()" function
10224 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010225 static void
10226f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010227 typval_T *argvars;
10228 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010229{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010230 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010231
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010232 s = get_tv_string_chk(&argvars[0]);
10233 if (s != NULL)
10234 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010235
Bram Moolenaar615b9972015-01-14 17:15:05 +010010236 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10238 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010239 if (p != NULL && !aborting())
10240 EMSG2(_(e_invexpr2), p);
10241 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010242 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010243 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010245 else if (*s != NUL)
10246 EMSG(_(e_trailing));
10247}
10248
10249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 * "eventhandler()" function
10251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010253f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010254 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258}
10259
10260/*
10261 * "executable()" function
10262 */
10263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010265 typval_T *argvars;
10266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010268 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10269}
10270
10271/*
10272 * "exepath()" function
10273 */
10274 static void
10275f_exepath(argvars, rettv)
10276 typval_T *argvars;
10277 typval_T *rettv;
10278{
10279 char_u *p = NULL;
10280
10281 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10282 rettv->v_type = VAR_STRING;
10283 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284}
10285
10286/*
10287 * "exists()" function
10288 */
10289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010290f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010291 typval_T *argvars;
10292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293{
10294 char_u *p;
10295 char_u *name;
10296 int n = FALSE;
10297 int len = 0;
10298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 if (*p == '$') /* environment variable */
10301 {
10302 /* first try "normal" environment variables (fast) */
10303 if (mch_getenv(p + 1) != NULL)
10304 n = TRUE;
10305 else
10306 {
10307 /* try expanding things like $VIM and ${HOME} */
10308 p = expand_env_save(p);
10309 if (p != NULL && *p != '$')
10310 n = TRUE;
10311 vim_free(p);
10312 }
10313 }
10314 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010317 if (*skipwhite(p) != NUL)
10318 n = FALSE; /* trailing garbage */
10319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 else if (*p == '*') /* internal or user defined function */
10321 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010322 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 }
10324 else if (*p == ':')
10325 {
10326 n = cmd_exists(p + 1);
10327 }
10328 else if (*p == '#')
10329 {
10330#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010331 if (p[1] == '#')
10332 n = autocmd_supported(p + 2);
10333 else
10334 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335#endif
10336 }
10337 else /* internal variable */
10338 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010339 char_u *tofree;
10340 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010342 /* get_name_len() takes care of expanding curly braces */
10343 name = p;
10344 len = get_name_len(&p, &tofree, TRUE, FALSE);
10345 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010347 if (tofree != NULL)
10348 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010349 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010350 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010352 /* handle d.key, l[idx], f(expr) */
10353 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10354 if (n)
10355 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 }
10357 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010358 if (*p != NUL)
10359 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010361 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 }
10363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010364 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365}
10366
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010367#ifdef FEAT_FLOAT
10368/*
10369 * "exp()" function
10370 */
10371 static void
10372f_exp(argvars, rettv)
10373 typval_T *argvars;
10374 typval_T *rettv;
10375{
10376 float_T f;
10377
10378 rettv->v_type = VAR_FLOAT;
10379 if (get_float_arg(argvars, &f) == OK)
10380 rettv->vval.v_float = exp(f);
10381 else
10382 rettv->vval.v_float = 0.0;
10383}
10384#endif
10385
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386/*
10387 * "expand()" function
10388 */
10389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010391 typval_T *argvars;
10392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393{
10394 char_u *s;
10395 int len;
10396 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010397 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010399 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010400 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010402 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010403 if (argvars[1].v_type != VAR_UNKNOWN
10404 && argvars[2].v_type != VAR_UNKNOWN
10405 && get_tv_number_chk(&argvars[2], &error)
10406 && !error)
10407 {
10408 rettv->v_type = VAR_LIST;
10409 rettv->vval.v_list = NULL;
10410 }
10411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010412 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 if (*s == '%' || *s == '#' || *s == '<')
10414 {
10415 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010416 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010418 if (rettv->v_type == VAR_LIST)
10419 {
10420 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10421 list_append_string(rettv->vval.v_list, result, -1);
10422 else
10423 vim_free(result);
10424 }
10425 else
10426 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 }
10428 else
10429 {
10430 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010431 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 if (argvars[1].v_type != VAR_UNKNOWN
10433 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010434 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 if (!error)
10436 {
10437 ExpandInit(&xpc);
10438 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010439 if (p_wic)
10440 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010441 if (rettv->v_type == VAR_STRING)
10442 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10443 options, WILD_ALL);
10444 else if (rettv_list_alloc(rettv) != FAIL)
10445 {
10446 int i;
10447
10448 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10449 for (i = 0; i < xpc.xp_numfiles; i++)
10450 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10451 ExpandCleanup(&xpc);
10452 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 }
10454 else
10455 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 }
10457}
10458
10459/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010460 * Go over all entries in "d2" and add them to "d1".
10461 * When "action" is "error" then a duplicate key is an error.
10462 * When "action" is "force" then a duplicate key is overwritten.
10463 * Otherwise duplicate keys are ignored ("action" is "keep").
10464 */
10465 void
10466dict_extend(d1, d2, action)
10467 dict_T *d1;
10468 dict_T *d2;
10469 char_u *action;
10470{
10471 dictitem_T *di1;
10472 hashitem_T *hi2;
10473 int todo;
10474
10475 todo = (int)d2->dv_hashtab.ht_used;
10476 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10477 {
10478 if (!HASHITEM_EMPTY(hi2))
10479 {
10480 --todo;
10481 di1 = dict_find(d1, hi2->hi_key, -1);
10482 if (d1->dv_scope != 0)
10483 {
10484 /* Disallow replacing a builtin function in l: and g:.
10485 * Check the key to be valid when adding to any
10486 * scope. */
10487 if (d1->dv_scope == VAR_DEF_SCOPE
10488 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10489 && var_check_func_name(hi2->hi_key,
10490 di1 == NULL))
10491 break;
10492 if (!valid_varname(hi2->hi_key))
10493 break;
10494 }
10495 if (di1 == NULL)
10496 {
10497 di1 = dictitem_copy(HI2DI(hi2));
10498 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10499 dictitem_free(di1);
10500 }
10501 else if (*action == 'e')
10502 {
10503 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10504 break;
10505 }
10506 else if (*action == 'f' && HI2DI(hi2) != di1)
10507 {
10508 clear_tv(&di1->di_tv);
10509 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10510 }
10511 }
10512 }
10513}
10514
10515/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010516 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010517 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010518 */
10519 static void
10520f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010521 typval_T *argvars;
10522 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010523{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010524 char *arg_errmsg = N_("extend() argument");
10525
Bram Moolenaare9a41262005-01-15 22:18:47 +000010526 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010527 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010528 list_T *l1, *l2;
10529 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010530 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010531 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010532
Bram Moolenaare9a41262005-01-15 22:18:47 +000010533 l1 = argvars[0].vval.v_list;
10534 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010535 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010536 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010537 {
10538 if (argvars[2].v_type != VAR_UNKNOWN)
10539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010540 before = get_tv_number_chk(&argvars[2], &error);
10541 if (error)
10542 return; /* type error; errmsg already given */
10543
Bram Moolenaar758711c2005-02-02 23:11:38 +000010544 if (before == l1->lv_len)
10545 item = NULL;
10546 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010547 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010548 item = list_find(l1, before);
10549 if (item == NULL)
10550 {
10551 EMSGN(_(e_listidx), before);
10552 return;
10553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010554 }
10555 }
10556 else
10557 item = NULL;
10558 list_extend(l1, l2, item);
10559
Bram Moolenaare9a41262005-01-15 22:18:47 +000010560 copy_tv(&argvars[0], rettv);
10561 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010562 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10564 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010565 dict_T *d1, *d2;
10566 char_u *action;
10567 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568
10569 d1 = argvars[0].vval.v_dict;
10570 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010571 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010572 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 {
10574 /* Check the third argument. */
10575 if (argvars[2].v_type != VAR_UNKNOWN)
10576 {
10577 static char *(av[]) = {"keep", "force", "error"};
10578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 action = get_tv_string_chk(&argvars[2]);
10580 if (action == NULL)
10581 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 for (i = 0; i < 3; ++i)
10583 if (STRCMP(action, av[i]) == 0)
10584 break;
10585 if (i == 3)
10586 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010587 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588 return;
10589 }
10590 }
10591 else
10592 action = (char_u *)"force";
10593
Bram Moolenaara9922d62013-05-30 13:01:18 +020010594 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010595
Bram Moolenaare9a41262005-01-15 22:18:47 +000010596 copy_tv(&argvars[0], rettv);
10597 }
10598 }
10599 else
10600 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010601}
10602
10603/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010604 * "feedkeys()" function
10605 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010606 static void
10607f_feedkeys(argvars, rettv)
10608 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010609 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010610{
10611 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010612 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010613 char_u *keys, *flags;
10614 char_u nbuf[NUMBUFLEN];
10615 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010616 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010617
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010618 /* This is not allowed in the sandbox. If the commands would still be
10619 * executed in the sandbox it would be OK, but it probably happens later,
10620 * when "sandbox" is no longer set. */
10621 if (check_secure())
10622 return;
10623
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010624 keys = get_tv_string(&argvars[0]);
10625 if (*keys != NUL)
10626 {
10627 if (argvars[1].v_type != VAR_UNKNOWN)
10628 {
10629 flags = get_tv_string_buf(&argvars[1], nbuf);
10630 for ( ; *flags != NUL; ++flags)
10631 {
10632 switch (*flags)
10633 {
10634 case 'n': remap = FALSE; break;
10635 case 'm': remap = TRUE; break;
10636 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010637 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010638 }
10639 }
10640 }
10641
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010642 /* Need to escape K_SPECIAL and CSI before putting the string in the
10643 * typeahead buffer. */
10644 keys_esc = vim_strsave_escape_csi(keys);
10645 if (keys_esc != NULL)
10646 {
10647 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010648 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010649 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010650 if (vgetc_busy)
10651 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010652 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010653 }
10654}
10655
10656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 * "filereadable()" function
10658 */
10659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010661 typval_T *argvars;
10662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010664 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 char_u *p;
10666 int n;
10667
Bram Moolenaarc236c162008-07-13 17:41:49 +000010668#ifndef O_NONBLOCK
10669# define O_NONBLOCK 0
10670#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010671 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010672 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10673 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 {
10675 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010676 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 }
10678 else
10679 n = FALSE;
10680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682}
10683
10684/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010685 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 * rights to write into.
10687 */
10688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010690 typval_T *argvars;
10691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010693 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694}
10695
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010696static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010697
10698 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010699findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010700 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010701 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010702 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010703{
10704#ifdef FEAT_SEARCHPATH
10705 char_u *fname;
10706 char_u *fresult = NULL;
10707 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10708 char_u *p;
10709 char_u pathbuf[NUMBUFLEN];
10710 int count = 1;
10711 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010712 int error = FALSE;
10713#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010714
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010715 rettv->vval.v_string = NULL;
10716 rettv->v_type = VAR_STRING;
10717
10718#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010720
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010721 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010722 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010723 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10724 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010725 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010726 else
10727 {
10728 if (*p != NUL)
10729 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010731 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010732 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010733 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010734 }
10735
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010736 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10737 error = TRUE;
10738
10739 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 do
10742 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010743 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010744 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010745 fresult = find_file_in_path_option(first ? fname : NULL,
10746 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010747 0, first, path,
10748 find_what,
10749 curbuf->b_ffname,
10750 find_what == FINDFILE_DIR
10751 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010752 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010753
10754 if (fresult != NULL && rettv->v_type == VAR_LIST)
10755 list_append_string(rettv->vval.v_list, fresult, -1);
10756
10757 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010758 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010759
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010760 if (rettv->v_type == VAR_STRING)
10761 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010762#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010763}
10764
Bram Moolenaar33570922005-01-25 22:26:29 +000010765static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10766static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010767
10768/*
10769 * Implementation of map() and filter().
10770 */
10771 static void
10772filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010773 typval_T *argvars;
10774 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010775 int map;
10776{
10777 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010778 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010779 listitem_T *li, *nli;
10780 list_T *l = NULL;
10781 dictitem_T *di;
10782 hashtab_T *ht;
10783 hashitem_T *hi;
10784 dict_T *d = NULL;
10785 typval_T save_val;
10786 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010788 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010789 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10790 char *arg_errmsg = (map ? N_("map() argument")
10791 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010792 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010793 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010794
Bram Moolenaare9a41262005-01-15 22:18:47 +000010795 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010796 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010797 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010798 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010799 return;
10800 }
10801 else if (argvars[0].v_type == VAR_DICT)
10802 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010803 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010804 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010805 return;
10806 }
10807 else
10808 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010809 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010810 return;
10811 }
10812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010813 expr = get_tv_string_buf_chk(&argvars[1], buf);
10814 /* On type errors, the preceding call has already displayed an error
10815 * message. Avoid a misleading error message for an empty string that
10816 * was not passed as argument. */
10817 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010819 prepare_vimvar(VV_VAL, &save_val);
10820 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010821
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010822 /* We reset "did_emsg" to be able to detect whether an error
10823 * occurred during evaluation of the expression. */
10824 save_did_emsg = did_emsg;
10825 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010826
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010827 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010830 vimvars[VV_KEY].vv_type = VAR_STRING;
10831
10832 ht = &d->dv_hashtab;
10833 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010834 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010835 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010837 if (!HASHITEM_EMPTY(hi))
10838 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010839 int r;
10840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010841 --todo;
10842 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010843 if (tv_check_lock(di->di_tv.v_lock,
10844 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010845 break;
10846 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010847 r = filter_map_one(&di->di_tv, expr, map, &rem);
10848 clear_tv(&vimvars[VV_KEY].vv_tv);
10849 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010850 break;
10851 if (!map && rem)
10852 dictitem_remove(d, di);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 }
10854 }
10855 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010856 }
10857 else
10858 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010859 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010861 for (li = l->lv_first; li != NULL; li = nli)
10862 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010863 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010864 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010866 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010867 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010868 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010869 break;
10870 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010872 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010873 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010874 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010875
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010876 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010877 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010878
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010879 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010880 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010881
10882 copy_tv(&argvars[0], rettv);
10883}
10884
10885 static int
10886filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010887 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010888 char_u *expr;
10889 int map;
10890 int *remp;
10891{
Bram Moolenaar33570922005-01-25 22:26:29 +000010892 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010893 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010894 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010895
Bram Moolenaar33570922005-01-25 22:26:29 +000010896 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010897 s = expr;
10898 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010899 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010900 if (*s != NUL) /* check for trailing chars after expr */
10901 {
10902 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010903 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010904 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010905 }
10906 if (map)
10907 {
10908 /* map(): replace the list item value */
10909 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010910 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010911 *tv = rettv;
10912 }
10913 else
10914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 int error = FALSE;
10916
Bram Moolenaare9a41262005-01-15 22:18:47 +000010917 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010918 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010919 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 /* On type error, nothing has been removed; return FAIL to stop the
10921 * loop. The error message was given by get_tv_number_chk(). */
10922 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010923 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010924 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010925 retval = OK;
10926theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010927 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010928 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010929}
10930
10931/*
10932 * "filter()" function
10933 */
10934 static void
10935f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010936 typval_T *argvars;
10937 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010938{
10939 filter_map(argvars, rettv, FALSE);
10940}
10941
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010943 * "finddir({fname}[, {path}[, {count}]])" function
10944 */
10945 static void
10946f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010947 typval_T *argvars;
10948 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010949{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010950 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010951}
10952
10953/*
10954 * "findfile({fname}[, {path}[, {count}]])" function
10955 */
10956 static void
10957f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010958 typval_T *argvars;
10959 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010960{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010961 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010962}
10963
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010964#ifdef FEAT_FLOAT
10965/*
10966 * "float2nr({float})" function
10967 */
10968 static void
10969f_float2nr(argvars, rettv)
10970 typval_T *argvars;
10971 typval_T *rettv;
10972{
10973 float_T f;
10974
10975 if (get_float_arg(argvars, &f) == OK)
10976 {
10977 if (f < -0x7fffffff)
10978 rettv->vval.v_number = -0x7fffffff;
10979 else if (f > 0x7fffffff)
10980 rettv->vval.v_number = 0x7fffffff;
10981 else
10982 rettv->vval.v_number = (varnumber_T)f;
10983 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010984}
10985
10986/*
10987 * "floor({float})" function
10988 */
10989 static void
10990f_floor(argvars, rettv)
10991 typval_T *argvars;
10992 typval_T *rettv;
10993{
10994 float_T f;
10995
10996 rettv->v_type = VAR_FLOAT;
10997 if (get_float_arg(argvars, &f) == OK)
10998 rettv->vval.v_float = floor(f);
10999 else
11000 rettv->vval.v_float = 0.0;
11001}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011002
11003/*
11004 * "fmod()" function
11005 */
11006 static void
11007f_fmod(argvars, rettv)
11008 typval_T *argvars;
11009 typval_T *rettv;
11010{
11011 float_T fx, fy;
11012
11013 rettv->v_type = VAR_FLOAT;
11014 if (get_float_arg(argvars, &fx) == OK
11015 && get_float_arg(&argvars[1], &fy) == OK)
11016 rettv->vval.v_float = fmod(fx, fy);
11017 else
11018 rettv->vval.v_float = 0.0;
11019}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011020#endif
11021
Bram Moolenaar0d660222005-01-07 21:51:51 +000011022/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011023 * "fnameescape({string})" function
11024 */
11025 static void
11026f_fnameescape(argvars, rettv)
11027 typval_T *argvars;
11028 typval_T *rettv;
11029{
11030 rettv->vval.v_string = vim_strsave_fnameescape(
11031 get_tv_string(&argvars[0]), FALSE);
11032 rettv->v_type = VAR_STRING;
11033}
11034
11035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 * "fnamemodify({fname}, {mods})" function
11037 */
11038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011040 typval_T *argvars;
11041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042{
11043 char_u *fname;
11044 char_u *mods;
11045 int usedlen = 0;
11046 int len;
11047 char_u *fbuf = NULL;
11048 char_u buf[NUMBUFLEN];
11049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 fname = get_tv_string_chk(&argvars[0]);
11051 mods = get_tv_string_buf_chk(&argvars[1], buf);
11052 if (fname == NULL || mods == NULL)
11053 fname = NULL;
11054 else
11055 {
11056 len = (int)STRLEN(fname);
11057 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011060 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 vim_free(fbuf);
11066}
11067
Bram Moolenaar33570922005-01-25 22:26:29 +000011068static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069
11070/*
11071 * "foldclosed()" function
11072 */
11073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011075 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011076 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011077 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079#ifdef FEAT_FOLDING
11080 linenr_T lnum;
11081 linenr_T first, last;
11082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11085 {
11086 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11087 {
11088 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 return;
11093 }
11094 }
11095#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011096 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097}
11098
11099/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011100 * "foldclosed()" function
11101 */
11102 static void
11103f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011104 typval_T *argvars;
11105 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011106{
11107 foldclosed_both(argvars, rettv, FALSE);
11108}
11109
11110/*
11111 * "foldclosedend()" function
11112 */
11113 static void
11114f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011115 typval_T *argvars;
11116 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011117{
11118 foldclosed_both(argvars, rettv, TRUE);
11119}
11120
11121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 * "foldlevel()" function
11123 */
11124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011125f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011126 typval_T *argvars UNUSED;
11127 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128{
11129#ifdef FEAT_FOLDING
11130 linenr_T lnum;
11131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136}
11137
11138/*
11139 * "foldtext()" function
11140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011143 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145{
11146#ifdef FEAT_FOLDING
11147 linenr_T lnum;
11148 char_u *s;
11149 char_u *r;
11150 int len;
11151 char *txt;
11152#endif
11153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154 rettv->v_type = VAR_STRING;
11155 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011157 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11158 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11159 <= curbuf->b_ml.ml_line_count
11160 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 {
11162 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011163 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11164 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 {
11166 if (!linewhite(lnum))
11167 break;
11168 ++lnum;
11169 }
11170
11171 /* Find interesting text in this line. */
11172 s = skipwhite(ml_get(lnum));
11173 /* skip C comment-start */
11174 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011177 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011178 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011179 {
11180 s = skipwhite(ml_get(lnum + 1));
11181 if (*s == '*')
11182 s = skipwhite(s + 1);
11183 }
11184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 txt = _("+-%s%3ld lines: ");
11186 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011187 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 + 20 /* for %3ld */
11189 + STRLEN(s))); /* concatenated */
11190 if (r != NULL)
11191 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011192 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11193 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11194 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 len = (int)STRLEN(r);
11196 STRCAT(r, s);
11197 /* remove 'foldmarker' and 'commentstring' */
11198 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011199 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 }
11201 }
11202#endif
11203}
11204
11205/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011206 * "foldtextresult(lnum)" function
11207 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011210 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011211 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011212{
11213#ifdef FEAT_FOLDING
11214 linenr_T lnum;
11215 char_u *text;
11216 char_u buf[51];
11217 foldinfo_T foldinfo;
11218 int fold_count;
11219#endif
11220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 rettv->v_type = VAR_STRING;
11222 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011223#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011224 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225 /* treat illegal types and illegal string values for {lnum} the same */
11226 if (lnum < 0)
11227 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011228 fold_count = foldedCount(curwin, lnum, &foldinfo);
11229 if (fold_count > 0)
11230 {
11231 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11232 &foldinfo, buf);
11233 if (text == buf)
11234 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011235 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011236 }
11237#endif
11238}
11239
11240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 * "foreground()" function
11242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011245 typval_T *argvars UNUSED;
11246 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248#ifdef FEAT_GUI
11249 if (gui.in_use)
11250 gui_mch_set_foreground();
11251#else
11252# ifdef WIN32
11253 win32_set_foreground();
11254# endif
11255#endif
11256}
11257
11258/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011259 * "function()" function
11260 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 typval_T *argvars;
11264 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011265{
11266 char_u *s;
11267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011269 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011270 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011271 /* Don't check an autoload name for existence here. */
11272 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011273 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011274 else
11275 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011276 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011277 {
11278 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011279 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011280
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011281 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11282 * also be called from another script. Using trans_function_name()
11283 * would also work, but some plugins depend on the name being
11284 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011285 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011286 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011287 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011288 if (rettv->vval.v_string != NULL)
11289 {
11290 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011291 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011292 }
11293 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011294 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011295 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011297 }
11298}
11299
11300/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011301 * "garbagecollect()" function
11302 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011303 static void
11304f_garbagecollect(argvars, rettv)
11305 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011306 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011307{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011308 /* This is postponed until we are back at the toplevel, because we may be
11309 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11310 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011311
11312 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11313 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011314}
11315
11316/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011317 * "get()" function
11318 */
11319 static void
11320f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011321 typval_T *argvars;
11322 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011323{
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 listitem_T *li;
11325 list_T *l;
11326 dictitem_T *di;
11327 dict_T *d;
11328 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011329
Bram Moolenaare9a41262005-01-15 22:18:47 +000011330 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011331 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011332 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011334 int error = FALSE;
11335
11336 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11337 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011338 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011339 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011340 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011341 else if (argvars[0].v_type == VAR_DICT)
11342 {
11343 if ((d = argvars[0].vval.v_dict) != NULL)
11344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011345 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011346 if (di != NULL)
11347 tv = &di->di_tv;
11348 }
11349 }
11350 else
11351 EMSG2(_(e_listdictarg), "get()");
11352
11353 if (tv == NULL)
11354 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011355 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011356 copy_tv(&argvars[2], rettv);
11357 }
11358 else
11359 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011360}
11361
Bram Moolenaar342337a2005-07-21 21:11:17 +000011362static 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 +000011363
11364/*
11365 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011366 * Return a range (from start to end) of lines in rettv from the specified
11367 * buffer.
11368 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011369 */
11370 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011371get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011372 buf_T *buf;
11373 linenr_T start;
11374 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011375 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011376 typval_T *rettv;
11377{
11378 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011379
Bram Moolenaar959a1432013-12-14 12:17:38 +010011380 rettv->v_type = VAR_STRING;
11381 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011382 if (retlist && rettv_list_alloc(rettv) == FAIL)
11383 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011384
11385 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11386 return;
11387
11388 if (!retlist)
11389 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011390 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11391 p = ml_get_buf(buf, start, FALSE);
11392 else
11393 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011394 rettv->vval.v_string = vim_strsave(p);
11395 }
11396 else
11397 {
11398 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011399 return;
11400
11401 if (start < 1)
11402 start = 1;
11403 if (end > buf->b_ml.ml_line_count)
11404 end = buf->b_ml.ml_line_count;
11405 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011406 if (list_append_string(rettv->vval.v_list,
11407 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011408 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011409 }
11410}
11411
11412/*
11413 * "getbufline()" function
11414 */
11415 static void
11416f_getbufline(argvars, rettv)
11417 typval_T *argvars;
11418 typval_T *rettv;
11419{
11420 linenr_T lnum;
11421 linenr_T end;
11422 buf_T *buf;
11423
11424 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11425 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011426 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011427 --emsg_off;
11428
Bram Moolenaar661b1822005-07-28 22:36:45 +000011429 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011430 if (argvars[2].v_type == VAR_UNKNOWN)
11431 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011432 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011433 end = get_tv_lnum_buf(&argvars[2], buf);
11434
Bram Moolenaar342337a2005-07-21 21:11:17 +000011435 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011436}
11437
Bram Moolenaar0d660222005-01-07 21:51:51 +000011438/*
11439 * "getbufvar()" function
11440 */
11441 static void
11442f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011443 typval_T *argvars;
11444 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011445{
11446 buf_T *buf;
11447 buf_T *save_curbuf;
11448 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011449 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011450 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011452 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11453 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011454 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011455 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011456
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011457 rettv->v_type = VAR_STRING;
11458 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011459
11460 if (buf != NULL && varname != NULL)
11461 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011462 /* set curbuf to be our buf, temporarily */
11463 save_curbuf = curbuf;
11464 curbuf = buf;
11465
Bram Moolenaar0d660222005-01-07 21:51:51 +000011466 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011467 {
11468 if (get_option_tv(&varname, rettv, TRUE) == OK)
11469 done = TRUE;
11470 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011471 else if (STRCMP(varname, "changedtick") == 0)
11472 {
11473 rettv->v_type = VAR_NUMBER;
11474 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011475 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011476 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011477 else
11478 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011479 /* Look up the variable. */
11480 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11481 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11482 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011483 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011484 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011485 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011486 done = TRUE;
11487 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011488 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011489
11490 /* restore previous notion of curbuf */
11491 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011492 }
11493
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011494 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11495 /* use the default value */
11496 copy_tv(&argvars[2], rettv);
11497
Bram Moolenaar0d660222005-01-07 21:51:51 +000011498 --emsg_off;
11499}
11500
11501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 * "getchar()" function
11503 */
11504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011505f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011506 typval_T *argvars;
11507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508{
11509 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011510 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011512 /* Position the cursor. Needed after a message that ends in a space. */
11513 windgoto(msg_row, msg_col);
11514
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 ++no_mapping;
11516 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011517 for (;;)
11518 {
11519 if (argvars[0].v_type == VAR_UNKNOWN)
11520 /* getchar(): blocking wait. */
11521 n = safe_vgetc();
11522 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11523 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011524 n = vpeekc_any();
11525 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011526 /* illegal argument or getchar(0) and no char avail: return zero */
11527 n = 0;
11528 else
11529 /* getchar(0) and char avail: return char */
11530 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011531
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011532 if (n == K_IGNORE)
11533 continue;
11534 break;
11535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536 --no_mapping;
11537 --allow_keys;
11538
Bram Moolenaar219b8702006-11-01 14:32:36 +000011539 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11540 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11541 vimvars[VV_MOUSE_COL].vv_nr = 0;
11542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011543 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 if (IS_SPECIAL(n) || mod_mask != 0)
11545 {
11546 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11547 int i = 0;
11548
11549 /* Turn a special key into three bytes, plus modifier. */
11550 if (mod_mask != 0)
11551 {
11552 temp[i++] = K_SPECIAL;
11553 temp[i++] = KS_MODIFIER;
11554 temp[i++] = mod_mask;
11555 }
11556 if (IS_SPECIAL(n))
11557 {
11558 temp[i++] = K_SPECIAL;
11559 temp[i++] = K_SECOND(n);
11560 temp[i++] = K_THIRD(n);
11561 }
11562#ifdef FEAT_MBYTE
11563 else if (has_mbyte)
11564 i += (*mb_char2bytes)(n, temp + i);
11565#endif
11566 else
11567 temp[i++] = n;
11568 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569 rettv->v_type = VAR_STRING;
11570 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011571
11572#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011573 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011574 {
11575 int row = mouse_row;
11576 int col = mouse_col;
11577 win_T *win;
11578 linenr_T lnum;
11579# ifdef FEAT_WINDOWS
11580 win_T *wp;
11581# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011582 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011583
11584 if (row >= 0 && col >= 0)
11585 {
11586 /* Find the window at the mouse coordinates and compute the
11587 * text position. */
11588 win = mouse_find_win(&row, &col);
11589 (void)mouse_comp_pos(win, &row, &col, &lnum);
11590# ifdef FEAT_WINDOWS
11591 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011592 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011593# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011594 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011595 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11596 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11597 }
11598 }
11599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 }
11601}
11602
11603/*
11604 * "getcharmod()" function
11605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011608 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612}
11613
11614/*
11615 * "getcmdline()" function
11616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011618f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011619 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->v_type = VAR_STRING;
11623 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624}
11625
11626/*
11627 * "getcmdpos()" function
11628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011631 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011634 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635}
11636
11637/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011638 * "getcmdtype()" function
11639 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011640 static void
11641f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011642 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011643 typval_T *rettv;
11644{
11645 rettv->v_type = VAR_STRING;
11646 rettv->vval.v_string = alloc(2);
11647 if (rettv->vval.v_string != NULL)
11648 {
11649 rettv->vval.v_string[0] = get_cmdline_type();
11650 rettv->vval.v_string[1] = NUL;
11651 }
11652}
11653
11654/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011655 * "getcmdwintype()" function
11656 */
11657 static void
11658f_getcmdwintype(argvars, rettv)
11659 typval_T *argvars UNUSED;
11660 typval_T *rettv;
11661{
11662 rettv->v_type = VAR_STRING;
11663 rettv->vval.v_string = NULL;
11664#ifdef FEAT_CMDWIN
11665 rettv->vval.v_string = alloc(2);
11666 if (rettv->vval.v_string != NULL)
11667 {
11668 rettv->vval.v_string[0] = cmdwin_type;
11669 rettv->vval.v_string[1] = NUL;
11670 }
11671#endif
11672}
11673
11674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 * "getcwd()" function
11676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011678f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011679 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011682 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011685 rettv->vval.v_string = NULL;
11686 cwd = alloc(MAXPATHL);
11687 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011689 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11690 {
11691 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011693 if (rettv->vval.v_string != NULL)
11694 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011696 }
11697 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 }
11699}
11700
11701/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011702 * "getfontname()" function
11703 */
11704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011705f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011706 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011707 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 rettv->v_type = VAR_STRING;
11710 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011711#ifdef FEAT_GUI
11712 if (gui.in_use)
11713 {
11714 GuiFont font;
11715 char_u *name = NULL;
11716
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011717 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011718 {
11719 /* Get the "Normal" font. Either the name saved by
11720 * hl_set_font_name() or from the font ID. */
11721 font = gui.norm_font;
11722 name = hl_get_font_name();
11723 }
11724 else
11725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011726 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011727 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11728 return;
11729 font = gui_mch_get_font(name, FALSE);
11730 if (font == NOFONT)
11731 return; /* Invalid font name, return empty string. */
11732 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011734 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011735 gui_mch_free_font(font);
11736 }
11737#endif
11738}
11739
11740/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011741 * "getfperm({fname})" function
11742 */
11743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011744f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011745 typval_T *argvars;
11746 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011747{
11748 char_u *fname;
11749 struct stat st;
11750 char_u *perm = NULL;
11751 char_u flags[] = "rwx";
11752 int i;
11753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011756 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011757 if (mch_stat((char *)fname, &st) >= 0)
11758 {
11759 perm = vim_strsave((char_u *)"---------");
11760 if (perm != NULL)
11761 {
11762 for (i = 0; i < 9; i++)
11763 {
11764 if (st.st_mode & (1 << (8 - i)))
11765 perm[i] = flags[i % 3];
11766 }
11767 }
11768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011770}
11771
11772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 * "getfsize({fname})" function
11774 */
11775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011777 typval_T *argvars;
11778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779{
11780 char_u *fname;
11781 struct stat st;
11782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786
11787 if (mch_stat((char *)fname, &st) >= 0)
11788 {
11789 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011792 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011793 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011794
11795 /* non-perfect check for overflow */
11796 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11797 rettv->vval.v_number = -2;
11798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 }
11800 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802}
11803
11804/*
11805 * "getftime({fname})" function
11806 */
11807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011809 typval_T *argvars;
11810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811{
11812 char_u *fname;
11813 struct stat st;
11814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816
11817 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011820 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821}
11822
11823/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011824 * "getftype({fname})" function
11825 */
11826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011828 typval_T *argvars;
11829 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011830{
11831 char_u *fname;
11832 struct stat st;
11833 char_u *type = NULL;
11834 char *t;
11835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011838 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011839 if (mch_lstat((char *)fname, &st) >= 0)
11840 {
11841#ifdef S_ISREG
11842 if (S_ISREG(st.st_mode))
11843 t = "file";
11844 else if (S_ISDIR(st.st_mode))
11845 t = "dir";
11846# ifdef S_ISLNK
11847 else if (S_ISLNK(st.st_mode))
11848 t = "link";
11849# endif
11850# ifdef S_ISBLK
11851 else if (S_ISBLK(st.st_mode))
11852 t = "bdev";
11853# endif
11854# ifdef S_ISCHR
11855 else if (S_ISCHR(st.st_mode))
11856 t = "cdev";
11857# endif
11858# ifdef S_ISFIFO
11859 else if (S_ISFIFO(st.st_mode))
11860 t = "fifo";
11861# endif
11862# ifdef S_ISSOCK
11863 else if (S_ISSOCK(st.st_mode))
11864 t = "fifo";
11865# endif
11866 else
11867 t = "other";
11868#else
11869# ifdef S_IFMT
11870 switch (st.st_mode & S_IFMT)
11871 {
11872 case S_IFREG: t = "file"; break;
11873 case S_IFDIR: t = "dir"; break;
11874# ifdef S_IFLNK
11875 case S_IFLNK: t = "link"; break;
11876# endif
11877# ifdef S_IFBLK
11878 case S_IFBLK: t = "bdev"; break;
11879# endif
11880# ifdef S_IFCHR
11881 case S_IFCHR: t = "cdev"; break;
11882# endif
11883# ifdef S_IFIFO
11884 case S_IFIFO: t = "fifo"; break;
11885# endif
11886# ifdef S_IFSOCK
11887 case S_IFSOCK: t = "socket"; break;
11888# endif
11889 default: t = "other";
11890 }
11891# else
11892 if (mch_isdir(fname))
11893 t = "dir";
11894 else
11895 t = "file";
11896# endif
11897#endif
11898 type = vim_strsave((char_u *)t);
11899 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011901}
11902
11903/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011904 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011905 */
11906 static void
11907f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011908 typval_T *argvars;
11909 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011910{
11911 linenr_T lnum;
11912 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011913 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011914
11915 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011916 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011917 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011918 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011919 retlist = FALSE;
11920 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011921 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011922 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011923 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011924 retlist = TRUE;
11925 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011926
Bram Moolenaar342337a2005-07-21 21:11:17 +000011927 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011928}
11929
11930/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011931 * "getmatches()" function
11932 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011933 static void
11934f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011935 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011936 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011937{
11938#ifdef FEAT_SEARCH_EXTRA
11939 dict_T *dict;
11940 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011941 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011942
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011943 if (rettv_list_alloc(rettv) == OK)
11944 {
11945 while (cur != NULL)
11946 {
11947 dict = dict_alloc();
11948 if (dict == NULL)
11949 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011950 if (cur->match.regprog == NULL)
11951 {
11952 /* match added with matchaddpos() */
11953 for (i = 0; i < MAXPOSMATCH; ++i)
11954 {
11955 llpos_T *llpos;
11956 char buf[6];
11957 list_T *l;
11958
11959 llpos = &cur->pos.pos[i];
11960 if (llpos->lnum == 0)
11961 break;
11962 l = list_alloc();
11963 if (l == NULL)
11964 break;
11965 list_append_number(l, (varnumber_T)llpos->lnum);
11966 if (llpos->col > 0)
11967 {
11968 list_append_number(l, (varnumber_T)llpos->col);
11969 list_append_number(l, (varnumber_T)llpos->len);
11970 }
11971 sprintf(buf, "pos%d", i + 1);
11972 dict_add_list(dict, buf, l);
11973 }
11974 }
11975 else
11976 {
11977 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11978 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011979 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011980 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11981 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11982 list_append_dict(rettv->vval.v_list, dict);
11983 cur = cur->next;
11984 }
11985 }
11986#endif
11987}
11988
11989/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011990 * "getpid()" function
11991 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011992 static void
11993f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011994 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011995 typval_T *rettv;
11996{
11997 rettv->vval.v_number = mch_get_pid();
11998}
11999
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012000static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12001
12002/*
12003 * "getcurpos()" function
12004 */
12005 static void
12006f_getcurpos(argvars, rettv)
12007 typval_T *argvars;
12008 typval_T *rettv;
12009{
12010 getpos_both(argvars, rettv, TRUE);
12011}
12012
Bram Moolenaar18081e32008-02-20 19:11:07 +000012013/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012014 * "getpos(string)" function
12015 */
12016 static void
12017f_getpos(argvars, rettv)
12018 typval_T *argvars;
12019 typval_T *rettv;
12020{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012021 getpos_both(argvars, rettv, FALSE);
12022}
12023
12024 static void
12025getpos_both(argvars, rettv, getcurpos)
12026 typval_T *argvars;
12027 typval_T *rettv;
12028 int getcurpos;
12029{
Bram Moolenaara5525202006-03-02 22:52:09 +000012030 pos_T *fp;
12031 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012032 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012033
12034 if (rettv_list_alloc(rettv) == OK)
12035 {
12036 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012037 if (getcurpos)
12038 fp = &curwin->w_cursor;
12039 else
12040 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012041 if (fnum != -1)
12042 list_append_number(l, (varnumber_T)fnum);
12043 else
12044 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012045 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12046 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012047 list_append_number(l, (fp != NULL)
12048 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012049 : (varnumber_T)0);
12050 list_append_number(l,
12051#ifdef FEAT_VIRTUALEDIT
12052 (fp != NULL) ? (varnumber_T)fp->coladd :
12053#endif
12054 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012055 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012056 list_append_number(l, curwin->w_curswant == MAXCOL ?
12057 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012058 }
12059 else
12060 rettv->vval.v_number = FALSE;
12061}
12062
12063/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012064 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012065 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012066 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012067f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012068 typval_T *argvars UNUSED;
12069 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012070{
12071#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012072 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012073#endif
12074
Bram Moolenaar2641f772005-03-25 21:58:17 +000012075#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012076 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012077 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012078 wp = NULL;
12079 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12080 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012081 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012082 if (wp == NULL)
12083 return;
12084 }
12085
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012086 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012087 }
12088#endif
12089}
12090
12091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092 * "getreg()" function
12093 */
12094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 typval_T *argvars;
12097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098{
12099 char_u *strregname;
12100 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012101 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012102 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012103 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012105 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012107 strregname = get_tv_string_chk(&argvars[0]);
12108 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012109 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012111 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012112 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12113 return_list = get_tv_number_chk(&argvars[2], &error);
12114 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012117 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012118
12119 if (error)
12120 return;
12121
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 regname = (strregname == NULL ? '"' : *strregname);
12123 if (regname == 0)
12124 regname = '"';
12125
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012126 if (return_list)
12127 {
12128 rettv->v_type = VAR_LIST;
12129 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12130 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012131 if (rettv->vval.v_list != NULL)
12132 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012133 }
12134 else
12135 {
12136 rettv->v_type = VAR_STRING;
12137 rettv->vval.v_string = get_reg_contents(regname,
12138 arg2 ? GREG_EXPR_SRC : 0);
12139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140}
12141
12142/*
12143 * "getregtype()" function
12144 */
12145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012147 typval_T *argvars;
12148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149{
12150 char_u *strregname;
12151 int regname;
12152 char_u buf[NUMBUFLEN + 2];
12153 long reglen = 0;
12154
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012155 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012156 {
12157 strregname = get_tv_string_chk(&argvars[0]);
12158 if (strregname == NULL) /* type error; errmsg already given */
12159 {
12160 rettv->v_type = VAR_STRING;
12161 rettv->vval.v_string = NULL;
12162 return;
12163 }
12164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 else
12166 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012167 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168
12169 regname = (strregname == NULL ? '"' : *strregname);
12170 if (regname == 0)
12171 regname = '"';
12172
12173 buf[0] = NUL;
12174 buf[1] = NUL;
12175 switch (get_reg_type(regname, &reglen))
12176 {
12177 case MLINE: buf[0] = 'V'; break;
12178 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179 case MBLOCK:
12180 buf[0] = Ctrl_V;
12181 sprintf((char *)buf + 1, "%ld", reglen + 1);
12182 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184 rettv->v_type = VAR_STRING;
12185 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186}
12187
12188/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012189 * "gettabvar()" function
12190 */
12191 static void
12192f_gettabvar(argvars, rettv)
12193 typval_T *argvars;
12194 typval_T *rettv;
12195{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012196 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012197 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012198 dictitem_T *v;
12199 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012200 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012201
12202 rettv->v_type = VAR_STRING;
12203 rettv->vval.v_string = NULL;
12204
12205 varname = get_tv_string_chk(&argvars[1]);
12206 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12207 if (tp != NULL && varname != NULL)
12208 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012209 /* Set tp to be our tabpage, temporarily. Also set the window to the
12210 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012211 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12212 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012213 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012214 /* look up the variable */
12215 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12216 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12217 if (v != NULL)
12218 {
12219 copy_tv(&v->di_tv, rettv);
12220 done = TRUE;
12221 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012222 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012223
12224 /* restore previous notion of curwin */
12225 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012226 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012227
12228 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012229 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012230}
12231
12232/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012233 * "gettabwinvar()" function
12234 */
12235 static void
12236f_gettabwinvar(argvars, rettv)
12237 typval_T *argvars;
12238 typval_T *rettv;
12239{
12240 getwinvar(argvars, rettv, 1);
12241}
12242
12243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 * "getwinposx()" function
12245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012247f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012248 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252#ifdef FEAT_GUI
12253 if (gui.in_use)
12254 {
12255 int x, y;
12256
12257 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 }
12260#endif
12261}
12262
12263/*
12264 * "getwinposy()" function
12265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012268 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012271 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272#ifdef FEAT_GUI
12273 if (gui.in_use)
12274 {
12275 int x, y;
12276
12277 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279 }
12280#endif
12281}
12282
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012283/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012284 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012285 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012286 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012287find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012288 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012289 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012290{
12291#ifdef FEAT_WINDOWS
12292 win_T *wp;
12293#endif
12294 int nr;
12295
12296 nr = get_tv_number_chk(vp, NULL);
12297
12298#ifdef FEAT_WINDOWS
12299 if (nr < 0)
12300 return NULL;
12301 if (nr == 0)
12302 return curwin;
12303
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012304 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12305 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012306 if (--nr <= 0)
12307 break;
12308 return wp;
12309#else
12310 if (nr == 0 || nr == 1)
12311 return curwin;
12312 return NULL;
12313#endif
12314}
12315
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316/*
12317 * "getwinvar()" function
12318 */
12319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012320f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012321 typval_T *argvars;
12322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012324 getwinvar(argvars, rettv, 0);
12325}
12326
12327/*
12328 * getwinvar() and gettabwinvar()
12329 */
12330 static void
12331getwinvar(argvars, rettv, off)
12332 typval_T *argvars;
12333 typval_T *rettv;
12334 int off; /* 1 for gettabwinvar() */
12335{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 win_T *win, *oldcurwin;
12337 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012338 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012339 tabpage_T *tp = NULL;
12340 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012341 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012343#ifdef FEAT_WINDOWS
12344 if (off == 1)
12345 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12346 else
12347 tp = curtab;
12348#endif
12349 win = find_win_by_nr(&argvars[off], tp);
12350 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012351 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012353 rettv->v_type = VAR_STRING;
12354 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355
12356 if (win != NULL && varname != NULL)
12357 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012358 /* Set curwin to be our win, temporarily. Also set the tabpage,
12359 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012360 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012361 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012362 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012363 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012364 if (get_option_tv(&varname, rettv, 1) == OK)
12365 done = TRUE;
12366 }
12367 else
12368 {
12369 /* Look up the variable. */
12370 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12371 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12372 varname, FALSE);
12373 if (v != NULL)
12374 {
12375 copy_tv(&v->di_tv, rettv);
12376 done = TRUE;
12377 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012380
12381 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012382 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 }
12384
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012385 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12386 /* use the default return value */
12387 copy_tv(&argvars[off + 2], rettv);
12388
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 --emsg_off;
12390}
12391
12392/*
12393 * "glob()" function
12394 */
12395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012397 typval_T *argvars;
12398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012400 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012402 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012404 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012405 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012407 if (argvars[1].v_type != VAR_UNKNOWN)
12408 {
12409 if (get_tv_number_chk(&argvars[1], &error))
12410 options |= WILD_KEEP_ALL;
12411 if (argvars[2].v_type != VAR_UNKNOWN
12412 && get_tv_number_chk(&argvars[2], &error))
12413 {
12414 rettv->v_type = VAR_LIST;
12415 rettv->vval.v_list = NULL;
12416 }
12417 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012418 if (!error)
12419 {
12420 ExpandInit(&xpc);
12421 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012422 if (p_wic)
12423 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012424 if (rettv->v_type == VAR_STRING)
12425 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012426 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012427 else if (rettv_list_alloc(rettv) != FAIL)
12428 {
12429 int i;
12430
12431 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12432 NULL, options, WILD_ALL_KEEP);
12433 for (i = 0; i < xpc.xp_numfiles; i++)
12434 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12435
12436 ExpandCleanup(&xpc);
12437 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012438 }
12439 else
12440 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441}
12442
12443/*
12444 * "globpath()" function
12445 */
12446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012447f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012448 typval_T *argvars;
12449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012451 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012453 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012454 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012455 garray_T ga;
12456 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012458 /* When the optional second argument is non-zero, don't remove matches
12459 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012460 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012461 if (argvars[2].v_type != VAR_UNKNOWN)
12462 {
12463 if (get_tv_number_chk(&argvars[2], &error))
12464 flags |= WILD_KEEP_ALL;
12465 if (argvars[3].v_type != VAR_UNKNOWN
12466 && get_tv_number_chk(&argvars[3], &error))
12467 {
12468 rettv->v_type = VAR_LIST;
12469 rettv->vval.v_list = NULL;
12470 }
12471 }
12472 if (file != NULL && !error)
12473 {
12474 ga_init2(&ga, (int)sizeof(char_u *), 10);
12475 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12476 if (rettv->v_type == VAR_STRING)
12477 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12478 else if (rettv_list_alloc(rettv) != FAIL)
12479 for (i = 0; i < ga.ga_len; ++i)
12480 list_append_string(rettv->vval.v_list,
12481 ((char_u **)(ga.ga_data))[i], -1);
12482 ga_clear_strings(&ga);
12483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012484 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012485 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486}
12487
12488/*
12489 * "has()" function
12490 */
12491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012492f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012493 typval_T *argvars;
12494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495{
12496 int i;
12497 char_u *name;
12498 int n = FALSE;
12499 static char *(has_list[]) =
12500 {
12501#ifdef AMIGA
12502 "amiga",
12503# ifdef FEAT_ARP
12504 "arp",
12505# endif
12506#endif
12507#ifdef __BEOS__
12508 "beos",
12509#endif
12510#ifdef MSDOS
12511# ifdef DJGPP
12512 "dos32",
12513# else
12514 "dos16",
12515# endif
12516#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012517#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 "mac",
12519#endif
12520#if defined(MACOS_X_UNIX)
12521 "macunix",
12522#endif
12523#ifdef OS2
12524 "os2",
12525#endif
12526#ifdef __QNX__
12527 "qnx",
12528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529#ifdef UNIX
12530 "unix",
12531#endif
12532#ifdef VMS
12533 "vms",
12534#endif
12535#ifdef WIN16
12536 "win16",
12537#endif
12538#ifdef WIN32
12539 "win32",
12540#endif
12541#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12542 "win32unix",
12543#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012544#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545 "win64",
12546#endif
12547#ifdef EBCDIC
12548 "ebcdic",
12549#endif
12550#ifndef CASE_INSENSITIVE_FILENAME
12551 "fname_case",
12552#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012553#ifdef HAVE_ACL
12554 "acl",
12555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556#ifdef FEAT_ARABIC
12557 "arabic",
12558#endif
12559#ifdef FEAT_AUTOCMD
12560 "autocmd",
12561#endif
12562#ifdef FEAT_BEVAL
12563 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012564# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12565 "balloon_multiline",
12566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567#endif
12568#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12569 "builtin_terms",
12570# ifdef ALL_BUILTIN_TCAPS
12571 "all_builtin_terms",
12572# endif
12573#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012574#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12575 || defined(FEAT_GUI_W32) \
12576 || defined(FEAT_GUI_MOTIF))
12577 "browsefilter",
12578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579#ifdef FEAT_BYTEOFF
12580 "byte_offset",
12581#endif
12582#ifdef FEAT_CINDENT
12583 "cindent",
12584#endif
12585#ifdef FEAT_CLIENTSERVER
12586 "clientserver",
12587#endif
12588#ifdef FEAT_CLIPBOARD
12589 "clipboard",
12590#endif
12591#ifdef FEAT_CMDL_COMPL
12592 "cmdline_compl",
12593#endif
12594#ifdef FEAT_CMDHIST
12595 "cmdline_hist",
12596#endif
12597#ifdef FEAT_COMMENTS
12598 "comments",
12599#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012600#ifdef FEAT_CONCEAL
12601 "conceal",
12602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603#ifdef FEAT_CRYPT
12604 "cryptv",
12605#endif
12606#ifdef FEAT_CSCOPE
12607 "cscope",
12608#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012609#ifdef FEAT_CURSORBIND
12610 "cursorbind",
12611#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012612#ifdef CURSOR_SHAPE
12613 "cursorshape",
12614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615#ifdef DEBUG
12616 "debug",
12617#endif
12618#ifdef FEAT_CON_DIALOG
12619 "dialog_con",
12620#endif
12621#ifdef FEAT_GUI_DIALOG
12622 "dialog_gui",
12623#endif
12624#ifdef FEAT_DIFF
12625 "diff",
12626#endif
12627#ifdef FEAT_DIGRAPHS
12628 "digraphs",
12629#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012630#ifdef FEAT_DIRECTX
12631 "directx",
12632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633#ifdef FEAT_DND
12634 "dnd",
12635#endif
12636#ifdef FEAT_EMACS_TAGS
12637 "emacs_tags",
12638#endif
12639 "eval", /* always present, of course! */
12640#ifdef FEAT_EX_EXTRA
12641 "ex_extra",
12642#endif
12643#ifdef FEAT_SEARCH_EXTRA
12644 "extra_search",
12645#endif
12646#ifdef FEAT_FKMAP
12647 "farsi",
12648#endif
12649#ifdef FEAT_SEARCHPATH
12650 "file_in_path",
12651#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012652#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012653 "filterpipe",
12654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655#ifdef FEAT_FIND_ID
12656 "find_in_path",
12657#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012658#ifdef FEAT_FLOAT
12659 "float",
12660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661#ifdef FEAT_FOLDING
12662 "folding",
12663#endif
12664#ifdef FEAT_FOOTER
12665 "footer",
12666#endif
12667#if !defined(USE_SYSTEM) && defined(UNIX)
12668 "fork",
12669#endif
12670#ifdef FEAT_GETTEXT
12671 "gettext",
12672#endif
12673#ifdef FEAT_GUI
12674 "gui",
12675#endif
12676#ifdef FEAT_GUI_ATHENA
12677# ifdef FEAT_GUI_NEXTAW
12678 "gui_neXtaw",
12679# else
12680 "gui_athena",
12681# endif
12682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683#ifdef FEAT_GUI_GTK
12684 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012687#ifdef FEAT_GUI_GNOME
12688 "gui_gnome",
12689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690#ifdef FEAT_GUI_MAC
12691 "gui_mac",
12692#endif
12693#ifdef FEAT_GUI_MOTIF
12694 "gui_motif",
12695#endif
12696#ifdef FEAT_GUI_PHOTON
12697 "gui_photon",
12698#endif
12699#ifdef FEAT_GUI_W16
12700 "gui_win16",
12701#endif
12702#ifdef FEAT_GUI_W32
12703 "gui_win32",
12704#endif
12705#ifdef FEAT_HANGULIN
12706 "hangul_input",
12707#endif
12708#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12709 "iconv",
12710#endif
12711#ifdef FEAT_INS_EXPAND
12712 "insert_expand",
12713#endif
12714#ifdef FEAT_JUMPLIST
12715 "jumplist",
12716#endif
12717#ifdef FEAT_KEYMAP
12718 "keymap",
12719#endif
12720#ifdef FEAT_LANGMAP
12721 "langmap",
12722#endif
12723#ifdef FEAT_LIBCALL
12724 "libcall",
12725#endif
12726#ifdef FEAT_LINEBREAK
12727 "linebreak",
12728#endif
12729#ifdef FEAT_LISP
12730 "lispindent",
12731#endif
12732#ifdef FEAT_LISTCMDS
12733 "listcmds",
12734#endif
12735#ifdef FEAT_LOCALMAP
12736 "localmap",
12737#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012738#ifdef FEAT_LUA
12739# ifndef DYNAMIC_LUA
12740 "lua",
12741# endif
12742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743#ifdef FEAT_MENU
12744 "menu",
12745#endif
12746#ifdef FEAT_SESSION
12747 "mksession",
12748#endif
12749#ifdef FEAT_MODIFY_FNAME
12750 "modify_fname",
12751#endif
12752#ifdef FEAT_MOUSE
12753 "mouse",
12754#endif
12755#ifdef FEAT_MOUSESHAPE
12756 "mouseshape",
12757#endif
12758#if defined(UNIX) || defined(VMS)
12759# ifdef FEAT_MOUSE_DEC
12760 "mouse_dec",
12761# endif
12762# ifdef FEAT_MOUSE_GPM
12763 "mouse_gpm",
12764# endif
12765# ifdef FEAT_MOUSE_JSB
12766 "mouse_jsbterm",
12767# endif
12768# ifdef FEAT_MOUSE_NET
12769 "mouse_netterm",
12770# endif
12771# ifdef FEAT_MOUSE_PTERM
12772 "mouse_pterm",
12773# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012774# ifdef FEAT_MOUSE_SGR
12775 "mouse_sgr",
12776# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012777# ifdef FEAT_SYSMOUSE
12778 "mouse_sysmouse",
12779# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012780# ifdef FEAT_MOUSE_URXVT
12781 "mouse_urxvt",
12782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783# ifdef FEAT_MOUSE_XTERM
12784 "mouse_xterm",
12785# endif
12786#endif
12787#ifdef FEAT_MBYTE
12788 "multi_byte",
12789#endif
12790#ifdef FEAT_MBYTE_IME
12791 "multi_byte_ime",
12792#endif
12793#ifdef FEAT_MULTI_LANG
12794 "multi_lang",
12795#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012796#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012797#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012798 "mzscheme",
12799#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801#ifdef FEAT_OLE
12802 "ole",
12803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804#ifdef FEAT_PATH_EXTRA
12805 "path_extra",
12806#endif
12807#ifdef FEAT_PERL
12808#ifndef DYNAMIC_PERL
12809 "perl",
12810#endif
12811#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012812#ifdef FEAT_PERSISTENT_UNDO
12813 "persistent_undo",
12814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815#ifdef FEAT_PYTHON
12816#ifndef DYNAMIC_PYTHON
12817 "python",
12818#endif
12819#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012820#ifdef FEAT_PYTHON3
12821#ifndef DYNAMIC_PYTHON3
12822 "python3",
12823#endif
12824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825#ifdef FEAT_POSTSCRIPT
12826 "postscript",
12827#endif
12828#ifdef FEAT_PRINTER
12829 "printer",
12830#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012831#ifdef FEAT_PROFILE
12832 "profile",
12833#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012834#ifdef FEAT_RELTIME
12835 "reltime",
12836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837#ifdef FEAT_QUICKFIX
12838 "quickfix",
12839#endif
12840#ifdef FEAT_RIGHTLEFT
12841 "rightleft",
12842#endif
12843#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12844 "ruby",
12845#endif
12846#ifdef FEAT_SCROLLBIND
12847 "scrollbind",
12848#endif
12849#ifdef FEAT_CMDL_INFO
12850 "showcmd",
12851 "cmdline_info",
12852#endif
12853#ifdef FEAT_SIGNS
12854 "signs",
12855#endif
12856#ifdef FEAT_SMARTINDENT
12857 "smartindent",
12858#endif
12859#ifdef FEAT_SNIFF
12860 "sniff",
12861#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012862#ifdef STARTUPTIME
12863 "startuptime",
12864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865#ifdef FEAT_STL_OPT
12866 "statusline",
12867#endif
12868#ifdef FEAT_SUN_WORKSHOP
12869 "sun_workshop",
12870#endif
12871#ifdef FEAT_NETBEANS_INTG
12872 "netbeans_intg",
12873#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012874#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012875 "spell",
12876#endif
12877#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 "syntax",
12879#endif
12880#if defined(USE_SYSTEM) || !defined(UNIX)
12881 "system",
12882#endif
12883#ifdef FEAT_TAG_BINS
12884 "tag_binary",
12885#endif
12886#ifdef FEAT_TAG_OLDSTATIC
12887 "tag_old_static",
12888#endif
12889#ifdef FEAT_TAG_ANYWHITE
12890 "tag_any_white",
12891#endif
12892#ifdef FEAT_TCL
12893# ifndef DYNAMIC_TCL
12894 "tcl",
12895# endif
12896#endif
12897#ifdef TERMINFO
12898 "terminfo",
12899#endif
12900#ifdef FEAT_TERMRESPONSE
12901 "termresponse",
12902#endif
12903#ifdef FEAT_TEXTOBJ
12904 "textobjects",
12905#endif
12906#ifdef HAVE_TGETENT
12907 "tgetent",
12908#endif
12909#ifdef FEAT_TITLE
12910 "title",
12911#endif
12912#ifdef FEAT_TOOLBAR
12913 "toolbar",
12914#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012915#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12916 "unnamedplus",
12917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918#ifdef FEAT_USR_CMDS
12919 "user-commands", /* was accidentally included in 5.4 */
12920 "user_commands",
12921#endif
12922#ifdef FEAT_VIMINFO
12923 "viminfo",
12924#endif
12925#ifdef FEAT_VERTSPLIT
12926 "vertsplit",
12927#endif
12928#ifdef FEAT_VIRTUALEDIT
12929 "virtualedit",
12930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932#ifdef FEAT_VISUALEXTRA
12933 "visualextra",
12934#endif
12935#ifdef FEAT_VREPLACE
12936 "vreplace",
12937#endif
12938#ifdef FEAT_WILDIGN
12939 "wildignore",
12940#endif
12941#ifdef FEAT_WILDMENU
12942 "wildmenu",
12943#endif
12944#ifdef FEAT_WINDOWS
12945 "windows",
12946#endif
12947#ifdef FEAT_WAK
12948 "winaltkeys",
12949#endif
12950#ifdef FEAT_WRITEBACKUP
12951 "writebackup",
12952#endif
12953#ifdef FEAT_XIM
12954 "xim",
12955#endif
12956#ifdef FEAT_XFONTSET
12957 "xfontset",
12958#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012959#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012960 "xpm",
12961 "xpm_w32", /* for backward compatibility */
12962#else
12963# if defined(HAVE_XPM)
12964 "xpm",
12965# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967#ifdef USE_XSMP
12968 "xsmp",
12969#endif
12970#ifdef USE_XSMP_INTERACT
12971 "xsmp_interact",
12972#endif
12973#ifdef FEAT_XCLIPBOARD
12974 "xterm_clipboard",
12975#endif
12976#ifdef FEAT_XTERM_SAVE
12977 "xterm_save",
12978#endif
12979#if defined(UNIX) && defined(FEAT_X11)
12980 "X11",
12981#endif
12982 NULL
12983 };
12984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012985 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986 for (i = 0; has_list[i] != NULL; ++i)
12987 if (STRICMP(name, has_list[i]) == 0)
12988 {
12989 n = TRUE;
12990 break;
12991 }
12992
12993 if (n == FALSE)
12994 {
12995 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012996 {
12997 if (name[5] == '-'
12998 && STRLEN(name) > 11
12999 && vim_isdigit(name[6])
13000 && vim_isdigit(name[8])
13001 && vim_isdigit(name[10]))
13002 {
13003 int major = atoi((char *)name + 6);
13004 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013005
13006 /* Expect "patch-9.9.01234". */
13007 n = (major < VIM_VERSION_MAJOR
13008 || (major == VIM_VERSION_MAJOR
13009 && (minor < VIM_VERSION_MINOR
13010 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013011 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013012 }
13013 else
13014 n = has_patch(atoi((char *)name + 5));
13015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016 else if (STRICMP(name, "vim_starting") == 0)
13017 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013018#ifdef FEAT_MBYTE
13019 else if (STRICMP(name, "multi_byte_encoding") == 0)
13020 n = has_mbyte;
13021#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013022#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13023 else if (STRICMP(name, "balloon_multiline") == 0)
13024 n = multiline_balloon_available();
13025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026#ifdef DYNAMIC_TCL
13027 else if (STRICMP(name, "tcl") == 0)
13028 n = tcl_enabled(FALSE);
13029#endif
13030#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13031 else if (STRICMP(name, "iconv") == 0)
13032 n = iconv_enabled(FALSE);
13033#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013034#ifdef DYNAMIC_LUA
13035 else if (STRICMP(name, "lua") == 0)
13036 n = lua_enabled(FALSE);
13037#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013038#ifdef DYNAMIC_MZSCHEME
13039 else if (STRICMP(name, "mzscheme") == 0)
13040 n = mzscheme_enabled(FALSE);
13041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042#ifdef DYNAMIC_RUBY
13043 else if (STRICMP(name, "ruby") == 0)
13044 n = ruby_enabled(FALSE);
13045#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013046#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047#ifdef DYNAMIC_PYTHON
13048 else if (STRICMP(name, "python") == 0)
13049 n = python_enabled(FALSE);
13050#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013051#endif
13052#ifdef FEAT_PYTHON3
13053#ifdef DYNAMIC_PYTHON3
13054 else if (STRICMP(name, "python3") == 0)
13055 n = python3_enabled(FALSE);
13056#endif
13057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058#ifdef DYNAMIC_PERL
13059 else if (STRICMP(name, "perl") == 0)
13060 n = perl_enabled(FALSE);
13061#endif
13062#ifdef FEAT_GUI
13063 else if (STRICMP(name, "gui_running") == 0)
13064 n = (gui.in_use || gui.starting);
13065# ifdef FEAT_GUI_W32
13066 else if (STRICMP(name, "gui_win32s") == 0)
13067 n = gui_is_win32s();
13068# endif
13069# ifdef FEAT_BROWSE
13070 else if (STRICMP(name, "browse") == 0)
13071 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13072# endif
13073#endif
13074#ifdef FEAT_SYN_HL
13075 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013076 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077#endif
13078#if defined(WIN3264)
13079 else if (STRICMP(name, "win95") == 0)
13080 n = mch_windows95();
13081#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013082#ifdef FEAT_NETBEANS_INTG
13083 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013084 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086 }
13087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013088 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089}
13090
13091/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013092 * "has_key()" function
13093 */
13094 static void
13095f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013096 typval_T *argvars;
13097 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013098{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013099 if (argvars[0].v_type != VAR_DICT)
13100 {
13101 EMSG(_(e_dictreq));
13102 return;
13103 }
13104 if (argvars[0].vval.v_dict == NULL)
13105 return;
13106
13107 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013108 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013109}
13110
13111/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013112 * "haslocaldir()" function
13113 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013114 static void
13115f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013116 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013117 typval_T *rettv;
13118{
13119 rettv->vval.v_number = (curwin->w_localdir != NULL);
13120}
13121
13122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 * "hasmapto()" function
13124 */
13125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013126f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013127 typval_T *argvars;
13128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129{
13130 char_u *name;
13131 char_u *mode;
13132 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013133 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013135 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013136 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 mode = (char_u *)"nvo";
13138 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013140 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013141 if (argvars[2].v_type != VAR_UNKNOWN)
13142 abbr = get_tv_number(&argvars[2]);
13143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144
Bram Moolenaar2c932302006-03-18 21:42:09 +000013145 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013146 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013148 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149}
13150
13151/*
13152 * "histadd()" function
13153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013156 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158{
13159#ifdef FEAT_CMDHIST
13160 int histype;
13161 char_u *str;
13162 char_u buf[NUMBUFLEN];
13163#endif
13164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013165 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 if (check_restricted() || check_secure())
13167 return;
13168#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013169 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13170 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 if (histype >= 0)
13172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 if (*str != NUL)
13175 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013176 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013178 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179 return;
13180 }
13181 }
13182#endif
13183}
13184
13185/*
13186 * "histdel()" function
13187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013189f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013190 typval_T *argvars UNUSED;
13191 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192{
13193#ifdef FEAT_CMDHIST
13194 int n;
13195 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013196 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013198 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13199 if (str == NULL)
13200 n = 0;
13201 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013203 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013204 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013206 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 else
13209 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013210 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 get_tv_string_buf(&argvars[1], buf));
13212 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213#endif
13214}
13215
13216/*
13217 * "histget()" function
13218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013221 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223{
13224#ifdef FEAT_CMDHIST
13225 int type;
13226 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013227 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013229 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13230 if (str == NULL)
13231 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013233 {
13234 type = get_histtype(str);
13235 if (argvars[1].v_type == VAR_UNKNOWN)
13236 idx = get_history_idx(type);
13237 else
13238 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13239 /* -1 on type error */
13240 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246}
13247
13248/*
13249 * "histnr()" function
13250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013253 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255{
13256 int i;
13257
13258#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013259 char_u *history = get_tv_string_chk(&argvars[0]);
13260
13261 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 if (i >= HIST_CMD && i < HIST_COUNT)
13263 i = get_history_idx(i);
13264 else
13265#endif
13266 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013267 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268}
13269
13270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 * "highlightID(name)" function
13272 */
13273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013274f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013275 typval_T *argvars;
13276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279}
13280
13281/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013282 * "highlight_exists()" function
13283 */
13284 static void
13285f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013286 typval_T *argvars;
13287 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013288{
13289 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13290}
13291
13292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 * "hostname()" function
13294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013296f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013297 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299{
13300 char_u hostname[256];
13301
13302 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013303 rettv->v_type = VAR_STRING;
13304 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305}
13306
13307/*
13308 * iconv() function
13309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013312 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314{
13315#ifdef FEAT_MBYTE
13316 char_u buf1[NUMBUFLEN];
13317 char_u buf2[NUMBUFLEN];
13318 char_u *from, *to, *str;
13319 vimconv_T vimconv;
13320#endif
13321
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013322 rettv->v_type = VAR_STRING;
13323 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324
13325#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013326 str = get_tv_string(&argvars[0]);
13327 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13328 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 vimconv.vc_type = CONV_NONE;
13330 convert_setup(&vimconv, from, to);
13331
13332 /* If the encodings are equal, no conversion needed. */
13333 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013336 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337
13338 convert_setup(&vimconv, NULL, NULL);
13339 vim_free(from);
13340 vim_free(to);
13341#endif
13342}
13343
13344/*
13345 * "indent()" function
13346 */
13347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013348f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013349 typval_T *argvars;
13350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351{
13352 linenr_T lnum;
13353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013356 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013358 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359}
13360
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013361/*
13362 * "index()" function
13363 */
13364 static void
13365f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013366 typval_T *argvars;
13367 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013368{
Bram Moolenaar33570922005-01-25 22:26:29 +000013369 list_T *l;
13370 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013371 long idx = 0;
13372 int ic = FALSE;
13373
13374 rettv->vval.v_number = -1;
13375 if (argvars[0].v_type != VAR_LIST)
13376 {
13377 EMSG(_(e_listreq));
13378 return;
13379 }
13380 l = argvars[0].vval.v_list;
13381 if (l != NULL)
13382 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013383 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013384 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386 int error = FALSE;
13387
Bram Moolenaar758711c2005-02-02 23:11:38 +000013388 /* Start at specified item. Use the cached index that list_find()
13389 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013390 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013391 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013392 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013393 ic = get_tv_number_chk(&argvars[3], &error);
13394 if (error)
13395 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013396 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013397
Bram Moolenaar758711c2005-02-02 23:11:38 +000013398 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013399 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013400 {
13401 rettv->vval.v_number = idx;
13402 break;
13403 }
13404 }
13405}
13406
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407static int inputsecret_flag = 0;
13408
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013409static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13410
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013412 * This function is used by f_input() and f_inputdialog() functions. The third
13413 * argument to f_input() specifies the type of completion to use at the
13414 * prompt. The third argument to f_inputdialog() specifies the value to return
13415 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 */
13417 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013418get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013419 typval_T *argvars;
13420 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013421 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013423 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424 char_u *p = NULL;
13425 int c;
13426 char_u buf[NUMBUFLEN];
13427 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013428 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013429 int xp_type = EXPAND_NOTHING;
13430 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013432 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013433 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434
13435#ifdef NO_CONSOLE_INPUT
13436 /* While starting up, there is no place to enter text. */
13437 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439#endif
13440
13441 cmd_silent = FALSE; /* Want to see the prompt. */
13442 if (prompt != NULL)
13443 {
13444 /* Only the part of the message after the last NL is considered as
13445 * prompt for the command line */
13446 p = vim_strrchr(prompt, '\n');
13447 if (p == NULL)
13448 p = prompt;
13449 else
13450 {
13451 ++p;
13452 c = *p;
13453 *p = NUL;
13454 msg_start();
13455 msg_clr_eos();
13456 msg_puts_attr(prompt, echo_attr);
13457 msg_didout = FALSE;
13458 msg_starthere();
13459 *p = c;
13460 }
13461 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013463 if (argvars[1].v_type != VAR_UNKNOWN)
13464 {
13465 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13466 if (defstr != NULL)
13467 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013469 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013470 {
13471 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013472 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013473 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013474
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013475 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013476 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013477
Bram Moolenaar4463f292005-09-25 22:20:24 +000013478 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13479 if (xp_name == NULL)
13480 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013481
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013482 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013483
Bram Moolenaar4463f292005-09-25 22:20:24 +000013484 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13485 &xp_arg) == FAIL)
13486 return;
13487 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013488 }
13489
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013490 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013491 {
13492# ifdef FEAT_EX_EXTRA
13493 int save_ex_normal_busy = ex_normal_busy;
13494 ex_normal_busy = 0;
13495# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013497 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13498 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013499# ifdef FEAT_EX_EXTRA
13500 ex_normal_busy = save_ex_normal_busy;
13501# endif
13502 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013503 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013504 && argvars[1].v_type != VAR_UNKNOWN
13505 && argvars[2].v_type != VAR_UNKNOWN)
13506 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13507 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013508
13509 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013511 /* since the user typed this, no need to wait for return */
13512 need_wait_return = FALSE;
13513 msg_didout = FALSE;
13514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 cmd_silent = cmd_silent_save;
13516}
13517
13518/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013519 * "input()" function
13520 * Also handles inputsecret() when inputsecret is set.
13521 */
13522 static void
13523f_input(argvars, rettv)
13524 typval_T *argvars;
13525 typval_T *rettv;
13526{
13527 get_user_input(argvars, rettv, FALSE);
13528}
13529
13530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 * "inputdialog()" function
13532 */
13533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013534f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 typval_T *argvars;
13536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537{
13538#if defined(FEAT_GUI_TEXTDIALOG)
13539 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13540 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13541 {
13542 char_u *message;
13543 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013544 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546 message = get_tv_string_chk(&argvars[0]);
13547 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013548 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013549 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550 else
13551 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013552 if (message != NULL && defstr != NULL
13553 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013554 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556 else
13557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013558 if (message != NULL && defstr != NULL
13559 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013560 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 rettv->vval.v_string = vim_strsave(
13562 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013564 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 }
13568 else
13569#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013570 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571}
13572
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013573/*
13574 * "inputlist()" function
13575 */
13576 static void
13577f_inputlist(argvars, rettv)
13578 typval_T *argvars;
13579 typval_T *rettv;
13580{
13581 listitem_T *li;
13582 int selected;
13583 int mouse_used;
13584
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013585#ifdef NO_CONSOLE_INPUT
13586 /* While starting up, there is no place to enter text. */
13587 if (no_console_input())
13588 return;
13589#endif
13590 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13591 {
13592 EMSG2(_(e_listarg), "inputlist()");
13593 return;
13594 }
13595
13596 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013597 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013598 lines_left = Rows; /* avoid more prompt */
13599 msg_scroll = TRUE;
13600 msg_clr_eos();
13601
13602 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13603 {
13604 msg_puts(get_tv_string(&li->li_tv));
13605 msg_putchar('\n');
13606 }
13607
13608 /* Ask for choice. */
13609 selected = prompt_for_number(&mouse_used);
13610 if (mouse_used)
13611 selected -= lines_left;
13612
13613 rettv->vval.v_number = selected;
13614}
13615
13616
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13618
13619/*
13620 * "inputrestore()" function
13621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013624 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626{
13627 if (ga_userinput.ga_len > 0)
13628 {
13629 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13631 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013632 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 }
13634 else if (p_verbose > 1)
13635 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013636 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 }
13639}
13640
13641/*
13642 * "inputsave()" function
13643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013646 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013649 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650 if (ga_grow(&ga_userinput, 1) == OK)
13651 {
13652 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13653 + ga_userinput.ga_len);
13654 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013655 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 }
13657 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013658 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659}
13660
13661/*
13662 * "inputsecret()" function
13663 */
13664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013666 typval_T *argvars;
13667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668{
13669 ++cmdline_star;
13670 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 --cmdline_star;
13673 --inputsecret_flag;
13674}
13675
13676/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013677 * "insert()" function
13678 */
13679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013680f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 typval_T *argvars;
13682 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013683{
13684 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013685 listitem_T *item;
13686 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013687 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013688
13689 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013690 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013691 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013692 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013693 {
13694 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013695 before = get_tv_number_chk(&argvars[2], &error);
13696 if (error)
13697 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013698
Bram Moolenaar758711c2005-02-02 23:11:38 +000013699 if (before == l->lv_len)
13700 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013701 else
13702 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013703 item = list_find(l, before);
13704 if (item == NULL)
13705 {
13706 EMSGN(_(e_listidx), before);
13707 l = NULL;
13708 }
13709 }
13710 if (l != NULL)
13711 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013712 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013713 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013714 }
13715 }
13716}
13717
13718/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013719 * "invert(expr)" function
13720 */
13721 static void
13722f_invert(argvars, rettv)
13723 typval_T *argvars;
13724 typval_T *rettv;
13725{
13726 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13727}
13728
13729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 * "isdirectory()" function
13731 */
13732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013734 typval_T *argvars;
13735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013737 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738}
13739
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013740/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013741 * "islocked()" function
13742 */
13743 static void
13744f_islocked(argvars, rettv)
13745 typval_T *argvars;
13746 typval_T *rettv;
13747{
13748 lval_T lv;
13749 char_u *end;
13750 dictitem_T *di;
13751
13752 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013753 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13754 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013755 if (end != NULL && lv.ll_name != NULL)
13756 {
13757 if (*end != NUL)
13758 EMSG(_(e_trailing));
13759 else
13760 {
13761 if (lv.ll_tv == NULL)
13762 {
13763 if (check_changedtick(lv.ll_name))
13764 rettv->vval.v_number = 1; /* always locked */
13765 else
13766 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013767 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013768 if (di != NULL)
13769 {
13770 /* Consider a variable locked when:
13771 * 1. the variable itself is locked
13772 * 2. the value of the variable is locked.
13773 * 3. the List or Dict value is locked.
13774 */
13775 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13776 || tv_islocked(&di->di_tv));
13777 }
13778 }
13779 }
13780 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013781 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013782 else if (lv.ll_newkey != NULL)
13783 EMSG2(_(e_dictkey), lv.ll_newkey);
13784 else if (lv.ll_list != NULL)
13785 /* List item. */
13786 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13787 else
13788 /* Dictionary item. */
13789 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13790 }
13791 }
13792
13793 clear_lval(&lv);
13794}
13795
Bram Moolenaar33570922005-01-25 22:26:29 +000013796static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013797
13798/*
13799 * Turn a dict into a list:
13800 * "what" == 0: list of keys
13801 * "what" == 1: list of values
13802 * "what" == 2: list of items
13803 */
13804 static void
13805dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013806 typval_T *argvars;
13807 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013808 int what;
13809{
Bram Moolenaar33570922005-01-25 22:26:29 +000013810 list_T *l2;
13811 dictitem_T *di;
13812 hashitem_T *hi;
13813 listitem_T *li;
13814 listitem_T *li2;
13815 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013816 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013817
Bram Moolenaar8c711452005-01-14 21:53:12 +000013818 if (argvars[0].v_type != VAR_DICT)
13819 {
13820 EMSG(_(e_dictreq));
13821 return;
13822 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013823 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013824 return;
13825
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013826 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013827 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013828
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013829 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013830 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013831 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013832 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013833 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013834 --todo;
13835 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013836
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013837 li = listitem_alloc();
13838 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013839 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013840 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013841
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013842 if (what == 0)
13843 {
13844 /* keys() */
13845 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013846 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013847 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13848 }
13849 else if (what == 1)
13850 {
13851 /* values() */
13852 copy_tv(&di->di_tv, &li->li_tv);
13853 }
13854 else
13855 {
13856 /* items() */
13857 l2 = list_alloc();
13858 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013859 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013860 li->li_tv.vval.v_list = l2;
13861 if (l2 == NULL)
13862 break;
13863 ++l2->lv_refcount;
13864
13865 li2 = listitem_alloc();
13866 if (li2 == NULL)
13867 break;
13868 list_append(l2, li2);
13869 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013870 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013871 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13872
13873 li2 = listitem_alloc();
13874 if (li2 == NULL)
13875 break;
13876 list_append(l2, li2);
13877 copy_tv(&di->di_tv, &li2->li_tv);
13878 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013879 }
13880 }
13881}
13882
13883/*
13884 * "items(dict)" function
13885 */
13886 static void
13887f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013888 typval_T *argvars;
13889 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013890{
13891 dict_list(argvars, rettv, 2);
13892}
13893
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013895 * "join()" function
13896 */
13897 static void
13898f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013899 typval_T *argvars;
13900 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013901{
13902 garray_T ga;
13903 char_u *sep;
13904
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013905 if (argvars[0].v_type != VAR_LIST)
13906 {
13907 EMSG(_(e_listreq));
13908 return;
13909 }
13910 if (argvars[0].vval.v_list == NULL)
13911 return;
13912 if (argvars[1].v_type == VAR_UNKNOWN)
13913 sep = (char_u *)" ";
13914 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013915 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013916
13917 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013918
13919 if (sep != NULL)
13920 {
13921 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013922 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013923 ga_append(&ga, NUL);
13924 rettv->vval.v_string = (char_u *)ga.ga_data;
13925 }
13926 else
13927 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013928}
13929
13930/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013931 * "keys()" function
13932 */
13933 static void
13934f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013935 typval_T *argvars;
13936 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013937{
13938 dict_list(argvars, rettv, 0);
13939}
13940
13941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942 * "last_buffer_nr()" function.
13943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013945f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013946 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948{
13949 int n = 0;
13950 buf_T *buf;
13951
13952 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13953 if (n < buf->b_fnum)
13954 n = buf->b_fnum;
13955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013956 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013957}
13958
13959/*
13960 * "len()" function
13961 */
13962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013963f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013964 typval_T *argvars;
13965 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013966{
13967 switch (argvars[0].v_type)
13968 {
13969 case VAR_STRING:
13970 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013971 rettv->vval.v_number = (varnumber_T)STRLEN(
13972 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013973 break;
13974 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013976 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013977 case VAR_DICT:
13978 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13979 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013980 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013981 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013982 break;
13983 }
13984}
13985
Bram Moolenaar33570922005-01-25 22:26:29 +000013986static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013987
13988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013990 typval_T *argvars;
13991 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013992 int type;
13993{
13994#ifdef FEAT_LIBCALL
13995 char_u *string_in;
13996 char_u **string_result;
13997 int nr_result;
13998#endif
13999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014000 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014001 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014002 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014003
14004 if (check_restricted() || check_secure())
14005 return;
14006
14007#ifdef FEAT_LIBCALL
14008 /* The first two args must be strings, otherwise its meaningless */
14009 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14010 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014011 string_in = NULL;
14012 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014013 string_in = argvars[2].vval.v_string;
14014 if (type == VAR_NUMBER)
14015 string_result = NULL;
14016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014017 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014018 if (mch_libcall(argvars[0].vval.v_string,
14019 argvars[1].vval.v_string,
14020 string_in,
14021 argvars[2].vval.v_number,
14022 string_result,
14023 &nr_result) == OK
14024 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014025 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014026 }
14027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028}
14029
14030/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014031 * "libcall()" function
14032 */
14033 static void
14034f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014035 typval_T *argvars;
14036 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014037{
14038 libcall_common(argvars, rettv, VAR_STRING);
14039}
14040
14041/*
14042 * "libcallnr()" function
14043 */
14044 static void
14045f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014046 typval_T *argvars;
14047 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014048{
14049 libcall_common(argvars, rettv, VAR_NUMBER);
14050}
14051
14052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 * "line(string)" function
14054 */
14055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014056f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014057 typval_T *argvars;
14058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059{
14060 linenr_T lnum = 0;
14061 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014062 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014064 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 if (fp != NULL)
14066 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014067 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068}
14069
14070/*
14071 * "line2byte(lnum)" function
14072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014074f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014075 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077{
14078#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014079 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080#else
14081 linenr_T lnum;
14082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014083 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014087 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14088 if (rettv->vval.v_number >= 0)
14089 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090#endif
14091}
14092
14093/*
14094 * "lispindent(lnum)" function
14095 */
14096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014097f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014098 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100{
14101#ifdef FEAT_LISP
14102 pos_T pos;
14103 linenr_T lnum;
14104
14105 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014106 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14108 {
14109 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014110 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111 curwin->w_cursor = pos;
14112 }
14113 else
14114#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116}
14117
14118/*
14119 * "localtime()" function
14120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014122f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014123 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014126 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127}
14128
Bram Moolenaar33570922005-01-25 22:26:29 +000014129static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130
14131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014133 typval_T *argvars;
14134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 int exact;
14136{
14137 char_u *keys;
14138 char_u *which;
14139 char_u buf[NUMBUFLEN];
14140 char_u *keys_buf = NULL;
14141 char_u *rhs;
14142 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014143 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014144 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014145 mapblock_T *mp;
14146 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147
14148 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 rettv->v_type = VAR_STRING;
14150 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 if (*keys == NUL)
14154 return;
14155
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014156 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014159 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014160 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014161 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014162 if (argvars[3].v_type != VAR_UNKNOWN)
14163 get_dict = get_tv_number(&argvars[3]);
14164 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166 else
14167 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168 if (which == NULL)
14169 return;
14170
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171 mode = get_map_mode(&which, 0);
14172
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014173 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014174 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014176
14177 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014179 /* Return a string. */
14180 if (rhs != NULL)
14181 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182
Bram Moolenaarbd743252010-10-20 21:23:33 +020014183 }
14184 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14185 {
14186 /* Return a dictionary. */
14187 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14188 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14189 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190
Bram Moolenaarbd743252010-10-20 21:23:33 +020014191 dict_add_nr_str(dict, "lhs", 0L, lhs);
14192 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14193 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14194 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14195 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14196 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14197 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014198 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014199 dict_add_nr_str(dict, "mode", 0L, mapmode);
14200
14201 vim_free(lhs);
14202 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203 }
14204}
14205
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014206#ifdef FEAT_FLOAT
14207/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014208 * "log()" function
14209 */
14210 static void
14211f_log(argvars, rettv)
14212 typval_T *argvars;
14213 typval_T *rettv;
14214{
14215 float_T f;
14216
14217 rettv->v_type = VAR_FLOAT;
14218 if (get_float_arg(argvars, &f) == OK)
14219 rettv->vval.v_float = log(f);
14220 else
14221 rettv->vval.v_float = 0.0;
14222}
14223
14224/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014225 * "log10()" function
14226 */
14227 static void
14228f_log10(argvars, rettv)
14229 typval_T *argvars;
14230 typval_T *rettv;
14231{
14232 float_T f;
14233
14234 rettv->v_type = VAR_FLOAT;
14235 if (get_float_arg(argvars, &f) == OK)
14236 rettv->vval.v_float = log10(f);
14237 else
14238 rettv->vval.v_float = 0.0;
14239}
14240#endif
14241
Bram Moolenaar1dced572012-04-05 16:54:08 +020014242#ifdef FEAT_LUA
14243/*
14244 * "luaeval()" function
14245 */
14246 static void
14247f_luaeval(argvars, rettv)
14248 typval_T *argvars;
14249 typval_T *rettv;
14250{
14251 char_u *str;
14252 char_u buf[NUMBUFLEN];
14253
14254 str = get_tv_string_buf(&argvars[0], buf);
14255 do_luaeval(str, argvars + 1, rettv);
14256}
14257#endif
14258
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014260 * "map()" function
14261 */
14262 static void
14263f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014264 typval_T *argvars;
14265 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014266{
14267 filter_map(argvars, rettv, TRUE);
14268}
14269
14270/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014271 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272 */
14273 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014274f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014275 typval_T *argvars;
14276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014278 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279}
14280
14281/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014282 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 */
14284 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014285f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014286 typval_T *argvars;
14287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014289 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290}
14291
Bram Moolenaar33570922005-01-25 22:26:29 +000014292static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293
14294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014295find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014296 typval_T *argvars;
14297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298 int type;
14299{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014300 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014301 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014302 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 char_u *pat;
14304 regmatch_T regmatch;
14305 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014306 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 char_u *save_cpo;
14308 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014309 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014310 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014311 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014312 list_T *l = NULL;
14313 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014314 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014315 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316
14317 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14318 save_cpo = p_cpo;
14319 p_cpo = (char_u *)"";
14320
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014321 rettv->vval.v_number = -1;
14322 if (type == 3)
14323 {
14324 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014325 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014326 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014327 }
14328 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014330 rettv->v_type = VAR_STRING;
14331 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014334 if (argvars[0].v_type == VAR_LIST)
14335 {
14336 if ((l = argvars[0].vval.v_list) == NULL)
14337 goto theend;
14338 li = l->lv_first;
14339 }
14340 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014341 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014342 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014343 len = (long)STRLEN(str);
14344 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014346 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14347 if (pat == NULL)
14348 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014349
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014350 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 int error = FALSE;
14353
14354 start = get_tv_number_chk(&argvars[2], &error);
14355 if (error)
14356 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014357 if (l != NULL)
14358 {
14359 li = list_find(l, start);
14360 if (li == NULL)
14361 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014362 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014363 }
14364 else
14365 {
14366 if (start < 0)
14367 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014368 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014369 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014370 /* When "count" argument is there ignore matches before "start",
14371 * otherwise skip part of the string. Differs when pattern is "^"
14372 * or "\<". */
14373 if (argvars[3].v_type != VAR_UNKNOWN)
14374 startcol = start;
14375 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014376 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014377 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014378 len -= start;
14379 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014380 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014381
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014382 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014383 nth = get_tv_number_chk(&argvars[3], &error);
14384 if (error)
14385 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 }
14387
14388 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14389 if (regmatch.regprog != NULL)
14390 {
14391 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014392
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014393 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014394 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014395 if (l != NULL)
14396 {
14397 if (li == NULL)
14398 {
14399 match = FALSE;
14400 break;
14401 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014402 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014403 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014404 if (str == NULL)
14405 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014406 }
14407
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014408 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014409
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014410 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014411 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014412 if (l == NULL && !match)
14413 break;
14414
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014415 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014416 if (l != NULL)
14417 {
14418 li = li->li_next;
14419 ++idx;
14420 }
14421 else
14422 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014423#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014424 startcol = (colnr_T)(regmatch.startp[0]
14425 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014426#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014427 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014428#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014429 if (startcol > (colnr_T)len
14430 || str + startcol <= regmatch.startp[0])
14431 {
14432 match = FALSE;
14433 break;
14434 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014435 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014436 }
14437
14438 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014440 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014441 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014442 int i;
14443
14444 /* return list with matched string and submatches */
14445 for (i = 0; i < NSUBEXP; ++i)
14446 {
14447 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014448 {
14449 if (list_append_string(rettv->vval.v_list,
14450 (char_u *)"", 0) == FAIL)
14451 break;
14452 }
14453 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014454 regmatch.startp[i],
14455 (int)(regmatch.endp[i] - regmatch.startp[i]))
14456 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014457 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014458 }
14459 }
14460 else if (type == 2)
14461 {
14462 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014463 if (l != NULL)
14464 copy_tv(&li->li_tv, rettv);
14465 else
14466 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014468 }
14469 else if (l != NULL)
14470 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 else
14472 {
14473 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 (varnumber_T)(regmatch.startp[0] - str);
14476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014477 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014479 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 }
14481 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014482 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483 }
14484
14485theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014486 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 p_cpo = save_cpo;
14488}
14489
14490/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014491 * "match()" function
14492 */
14493 static void
14494f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014495 typval_T *argvars;
14496 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014497{
14498 find_some_match(argvars, rettv, 1);
14499}
14500
14501/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014502 * "matchadd()" function
14503 */
14504 static void
14505f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014506 typval_T *argvars UNUSED;
14507 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014508{
14509#ifdef FEAT_SEARCH_EXTRA
14510 char_u buf[NUMBUFLEN];
14511 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14512 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14513 int prio = 10; /* default priority */
14514 int id = -1;
14515 int error = FALSE;
14516
14517 rettv->vval.v_number = -1;
14518
14519 if (grp == NULL || pat == NULL)
14520 return;
14521 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014522 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014523 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014524 if (argvars[3].v_type != VAR_UNKNOWN)
14525 id = get_tv_number_chk(&argvars[3], &error);
14526 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014527 if (error == TRUE)
14528 return;
14529 if (id >= 1 && id <= 3)
14530 {
14531 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14532 return;
14533 }
14534
Bram Moolenaarb3414592014-06-17 17:48:32 +020014535 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14536#endif
14537}
14538
14539/*
14540 * "matchaddpos()" function
14541 */
14542 static void
14543f_matchaddpos(argvars, rettv)
14544 typval_T *argvars UNUSED;
14545 typval_T *rettv UNUSED;
14546{
14547#ifdef FEAT_SEARCH_EXTRA
14548 char_u buf[NUMBUFLEN];
14549 char_u *group;
14550 int prio = 10;
14551 int id = -1;
14552 int error = FALSE;
14553 list_T *l;
14554
14555 rettv->vval.v_number = -1;
14556
14557 group = get_tv_string_buf_chk(&argvars[0], buf);
14558 if (group == NULL)
14559 return;
14560
14561 if (argvars[1].v_type != VAR_LIST)
14562 {
14563 EMSG2(_(e_listarg), "matchaddpos()");
14564 return;
14565 }
14566 l = argvars[1].vval.v_list;
14567 if (l == NULL)
14568 return;
14569
14570 if (argvars[2].v_type != VAR_UNKNOWN)
14571 {
14572 prio = get_tv_number_chk(&argvars[2], &error);
14573 if (argvars[3].v_type != VAR_UNKNOWN)
14574 id = get_tv_number_chk(&argvars[3], &error);
14575 }
14576 if (error == TRUE)
14577 return;
14578
14579 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14580 if (id == 1 || id == 2)
14581 {
14582 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14583 return;
14584 }
14585
14586 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014587#endif
14588}
14589
14590/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014591 * "matcharg()" function
14592 */
14593 static void
14594f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014595 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014596 typval_T *rettv;
14597{
14598 if (rettv_list_alloc(rettv) == OK)
14599 {
14600#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014601 int id = get_tv_number(&argvars[0]);
14602 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014603
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014604 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014605 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014606 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14607 {
14608 list_append_string(rettv->vval.v_list,
14609 syn_id2name(m->hlg_id), -1);
14610 list_append_string(rettv->vval.v_list, m->pattern, -1);
14611 }
14612 else
14613 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014614 list_append_string(rettv->vval.v_list, NULL, -1);
14615 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014616 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014617 }
14618#endif
14619 }
14620}
14621
14622/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014623 * "matchdelete()" function
14624 */
14625 static void
14626f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014627 typval_T *argvars UNUSED;
14628 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014629{
14630#ifdef FEAT_SEARCH_EXTRA
14631 rettv->vval.v_number = match_delete(curwin,
14632 (int)get_tv_number(&argvars[0]), TRUE);
14633#endif
14634}
14635
14636/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014637 * "matchend()" function
14638 */
14639 static void
14640f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014641 typval_T *argvars;
14642 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014643{
14644 find_some_match(argvars, rettv, 0);
14645}
14646
14647/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014648 * "matchlist()" function
14649 */
14650 static void
14651f_matchlist(argvars, rettv)
14652 typval_T *argvars;
14653 typval_T *rettv;
14654{
14655 find_some_match(argvars, rettv, 3);
14656}
14657
14658/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014659 * "matchstr()" function
14660 */
14661 static void
14662f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014663 typval_T *argvars;
14664 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665{
14666 find_some_match(argvars, rettv, 2);
14667}
14668
Bram Moolenaar33570922005-01-25 22:26:29 +000014669static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014670
14671 static void
14672max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014673 typval_T *argvars;
14674 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014675 int domax;
14676{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014677 long n = 0;
14678 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014679 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014680
14681 if (argvars[0].v_type == VAR_LIST)
14682 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014683 list_T *l;
14684 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014685
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014686 l = argvars[0].vval.v_list;
14687 if (l != NULL)
14688 {
14689 li = l->lv_first;
14690 if (li != NULL)
14691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014692 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014693 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014694 {
14695 li = li->li_next;
14696 if (li == NULL)
14697 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014698 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014699 if (domax ? i > n : i < n)
14700 n = i;
14701 }
14702 }
14703 }
14704 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014705 else if (argvars[0].v_type == VAR_DICT)
14706 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014707 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014708 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014709 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014710 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014711
14712 d = argvars[0].vval.v_dict;
14713 if (d != NULL)
14714 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014715 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014716 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014717 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014718 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014719 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014720 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014721 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014722 if (first)
14723 {
14724 n = i;
14725 first = FALSE;
14726 }
14727 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014728 n = i;
14729 }
14730 }
14731 }
14732 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014733 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014734 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014735 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014736}
14737
14738/*
14739 * "max()" function
14740 */
14741 static void
14742f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014743 typval_T *argvars;
14744 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014745{
14746 max_min(argvars, rettv, TRUE);
14747}
14748
14749/*
14750 * "min()" function
14751 */
14752 static void
14753f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014754 typval_T *argvars;
14755 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014756{
14757 max_min(argvars, rettv, FALSE);
14758}
14759
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014760static int mkdir_recurse __ARGS((char_u *dir, int prot));
14761
14762/*
14763 * Create the directory in which "dir" is located, and higher levels when
14764 * needed.
14765 */
14766 static int
14767mkdir_recurse(dir, prot)
14768 char_u *dir;
14769 int prot;
14770{
14771 char_u *p;
14772 char_u *updir;
14773 int r = FAIL;
14774
14775 /* Get end of directory name in "dir".
14776 * We're done when it's "/" or "c:/". */
14777 p = gettail_sep(dir);
14778 if (p <= get_past_head(dir))
14779 return OK;
14780
14781 /* If the directory exists we're done. Otherwise: create it.*/
14782 updir = vim_strnsave(dir, (int)(p - dir));
14783 if (updir == NULL)
14784 return FAIL;
14785 if (mch_isdir(updir))
14786 r = OK;
14787 else if (mkdir_recurse(updir, prot) == OK)
14788 r = vim_mkdir_emsg(updir, prot);
14789 vim_free(updir);
14790 return r;
14791}
14792
14793#ifdef vim_mkdir
14794/*
14795 * "mkdir()" function
14796 */
14797 static void
14798f_mkdir(argvars, rettv)
14799 typval_T *argvars;
14800 typval_T *rettv;
14801{
14802 char_u *dir;
14803 char_u buf[NUMBUFLEN];
14804 int prot = 0755;
14805
14806 rettv->vval.v_number = FAIL;
14807 if (check_restricted() || check_secure())
14808 return;
14809
14810 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014811 if (*dir == NUL)
14812 rettv->vval.v_number = FAIL;
14813 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014814 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014815 if (*gettail(dir) == NUL)
14816 /* remove trailing slashes */
14817 *gettail_sep(dir) = NUL;
14818
14819 if (argvars[1].v_type != VAR_UNKNOWN)
14820 {
14821 if (argvars[2].v_type != VAR_UNKNOWN)
14822 prot = get_tv_number_chk(&argvars[2], NULL);
14823 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14824 mkdir_recurse(dir, prot);
14825 }
14826 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014827 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014828}
14829#endif
14830
Bram Moolenaar0d660222005-01-07 21:51:51 +000014831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832 * "mode()" function
14833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014835f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014836 typval_T *argvars;
14837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014839 char_u buf[3];
14840
14841 buf[1] = NUL;
14842 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 if (VIsual_active)
14845 {
14846 if (VIsual_select)
14847 buf[0] = VIsual_mode + 's' - 'v';
14848 else
14849 buf[0] = VIsual_mode;
14850 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014851 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014852 || State == CONFIRM)
14853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014855 if (State == ASKMORE)
14856 buf[1] = 'm';
14857 else if (State == CONFIRM)
14858 buf[1] = '?';
14859 }
14860 else if (State == EXTERNCMD)
14861 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 else if (State & INSERT)
14863 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014864#ifdef FEAT_VREPLACE
14865 if (State & VREPLACE_FLAG)
14866 {
14867 buf[0] = 'R';
14868 buf[1] = 'v';
14869 }
14870 else
14871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 if (State & REPLACE_FLAG)
14873 buf[0] = 'R';
14874 else
14875 buf[0] = 'i';
14876 }
14877 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014880 if (exmode_active)
14881 buf[1] = 'v';
14882 }
14883 else if (exmode_active)
14884 {
14885 buf[0] = 'c';
14886 buf[1] = 'e';
14887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014891 if (finish_op)
14892 buf[1] = 'o';
14893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014895 /* Clear out the minor mode when the argument is not a non-zero number or
14896 * non-empty string. */
14897 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014898 buf[1] = NUL;
14899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014900 rettv->vval.v_string = vim_strsave(buf);
14901 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902}
14903
Bram Moolenaar429fa852013-04-15 12:27:36 +020014904#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014905/*
14906 * "mzeval()" function
14907 */
14908 static void
14909f_mzeval(argvars, rettv)
14910 typval_T *argvars;
14911 typval_T *rettv;
14912{
14913 char_u *str;
14914 char_u buf[NUMBUFLEN];
14915
14916 str = get_tv_string_buf(&argvars[0], buf);
14917 do_mzeval(str, rettv);
14918}
Bram Moolenaar75676462013-01-30 14:55:42 +010014919
14920 void
14921mzscheme_call_vim(name, args, rettv)
14922 char_u *name;
14923 typval_T *args;
14924 typval_T *rettv;
14925{
14926 typval_T argvars[3];
14927
14928 argvars[0].v_type = VAR_STRING;
14929 argvars[0].vval.v_string = name;
14930 copy_tv(args, &argvars[1]);
14931 argvars[2].v_type = VAR_UNKNOWN;
14932 f_call(argvars, rettv);
14933 clear_tv(&argvars[1]);
14934}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014935#endif
14936
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014938 * "nextnonblank()" function
14939 */
14940 static void
14941f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014942 typval_T *argvars;
14943 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014944{
14945 linenr_T lnum;
14946
14947 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14948 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014949 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014950 {
14951 lnum = 0;
14952 break;
14953 }
14954 if (*skipwhite(ml_get(lnum)) != NUL)
14955 break;
14956 }
14957 rettv->vval.v_number = lnum;
14958}
14959
14960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 * "nr2char()" function
14962 */
14963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014964f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014965 typval_T *argvars;
14966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967{
14968 char_u buf[NUMBUFLEN];
14969
14970#ifdef FEAT_MBYTE
14971 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014972 {
14973 int utf8 = 0;
14974
14975 if (argvars[1].v_type != VAR_UNKNOWN)
14976 utf8 = get_tv_number_chk(&argvars[1], NULL);
14977 if (utf8)
14978 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14979 else
14980 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982 else
14983#endif
14984 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014985 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 buf[1] = NUL;
14987 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014988 rettv->v_type = VAR_STRING;
14989 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014990}
14991
14992/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014993 * "or(expr, expr)" function
14994 */
14995 static void
14996f_or(argvars, rettv)
14997 typval_T *argvars;
14998 typval_T *rettv;
14999{
15000 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15001 | get_tv_number_chk(&argvars[1], NULL);
15002}
15003
15004/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015005 * "pathshorten()" function
15006 */
15007 static void
15008f_pathshorten(argvars, rettv)
15009 typval_T *argvars;
15010 typval_T *rettv;
15011{
15012 char_u *p;
15013
15014 rettv->v_type = VAR_STRING;
15015 p = get_tv_string_chk(&argvars[0]);
15016 if (p == NULL)
15017 rettv->vval.v_string = NULL;
15018 else
15019 {
15020 p = vim_strsave(p);
15021 rettv->vval.v_string = p;
15022 if (p != NULL)
15023 shorten_dir(p);
15024 }
15025}
15026
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015027#ifdef FEAT_FLOAT
15028/*
15029 * "pow()" function
15030 */
15031 static void
15032f_pow(argvars, rettv)
15033 typval_T *argvars;
15034 typval_T *rettv;
15035{
15036 float_T fx, fy;
15037
15038 rettv->v_type = VAR_FLOAT;
15039 if (get_float_arg(argvars, &fx) == OK
15040 && get_float_arg(&argvars[1], &fy) == OK)
15041 rettv->vval.v_float = pow(fx, fy);
15042 else
15043 rettv->vval.v_float = 0.0;
15044}
15045#endif
15046
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015047/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015048 * "prevnonblank()" function
15049 */
15050 static void
15051f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015052 typval_T *argvars;
15053 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015054{
15055 linenr_T lnum;
15056
15057 lnum = get_tv_lnum(argvars);
15058 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15059 lnum = 0;
15060 else
15061 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15062 --lnum;
15063 rettv->vval.v_number = lnum;
15064}
15065
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015066#ifdef HAVE_STDARG_H
15067/* This dummy va_list is here because:
15068 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15069 * - locally in the function results in a "used before set" warning
15070 * - using va_start() to initialize it gives "function with fixed args" error */
15071static va_list ap;
15072#endif
15073
Bram Moolenaar8c711452005-01-14 21:53:12 +000015074/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015075 * "printf()" function
15076 */
15077 static void
15078f_printf(argvars, rettv)
15079 typval_T *argvars;
15080 typval_T *rettv;
15081{
15082 rettv->v_type = VAR_STRING;
15083 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015084#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015085 {
15086 char_u buf[NUMBUFLEN];
15087 int len;
15088 char_u *s;
15089 int saved_did_emsg = did_emsg;
15090 char *fmt;
15091
15092 /* Get the required length, allocate the buffer and do it for real. */
15093 did_emsg = FALSE;
15094 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015095 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015096 if (!did_emsg)
15097 {
15098 s = alloc(len + 1);
15099 if (s != NULL)
15100 {
15101 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015102 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015103 }
15104 }
15105 did_emsg |= saved_did_emsg;
15106 }
15107#endif
15108}
15109
15110/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015111 * "pumvisible()" function
15112 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015113 static void
15114f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015115 typval_T *argvars UNUSED;
15116 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015117{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015118#ifdef FEAT_INS_EXPAND
15119 if (pum_visible())
15120 rettv->vval.v_number = 1;
15121#endif
15122}
15123
Bram Moolenaardb913952012-06-29 12:54:53 +020015124#ifdef FEAT_PYTHON3
15125/*
15126 * "py3eval()" function
15127 */
15128 static void
15129f_py3eval(argvars, rettv)
15130 typval_T *argvars;
15131 typval_T *rettv;
15132{
15133 char_u *str;
15134 char_u buf[NUMBUFLEN];
15135
15136 str = get_tv_string_buf(&argvars[0], buf);
15137 do_py3eval(str, rettv);
15138}
15139#endif
15140
15141#ifdef FEAT_PYTHON
15142/*
15143 * "pyeval()" function
15144 */
15145 static void
15146f_pyeval(argvars, rettv)
15147 typval_T *argvars;
15148 typval_T *rettv;
15149{
15150 char_u *str;
15151 char_u buf[NUMBUFLEN];
15152
15153 str = get_tv_string_buf(&argvars[0], buf);
15154 do_pyeval(str, rettv);
15155}
15156#endif
15157
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015158/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015159 * "range()" function
15160 */
15161 static void
15162f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015163 typval_T *argvars;
15164 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015165{
15166 long start;
15167 long end;
15168 long stride = 1;
15169 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015170 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015172 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015173 if (argvars[1].v_type == VAR_UNKNOWN)
15174 {
15175 end = start - 1;
15176 start = 0;
15177 }
15178 else
15179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015180 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015181 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015182 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015183 }
15184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015185 if (error)
15186 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015187 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015188 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015189 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015190 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015191 else
15192 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015193 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015194 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015195 if (list_append_number(rettv->vval.v_list,
15196 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015197 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015198 }
15199}
15200
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015201/*
15202 * "readfile()" function
15203 */
15204 static void
15205f_readfile(argvars, rettv)
15206 typval_T *argvars;
15207 typval_T *rettv;
15208{
15209 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015210 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015211 char_u *fname;
15212 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015213 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15214 int io_size = sizeof(buf);
15215 int readlen; /* size of last fread() */
15216 char_u *prev = NULL; /* previously read bytes, if any */
15217 long prevlen = 0; /* length of data in prev */
15218 long prevsize = 0; /* size of prev buffer */
15219 long maxline = MAXLNUM;
15220 long cnt = 0;
15221 char_u *p; /* position in buf */
15222 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015223
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015224 if (argvars[1].v_type != VAR_UNKNOWN)
15225 {
15226 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15227 binary = TRUE;
15228 if (argvars[2].v_type != VAR_UNKNOWN)
15229 maxline = get_tv_number(&argvars[2]);
15230 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015231
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015232 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015233 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015234
15235 /* Always open the file in binary mode, library functions have a mind of
15236 * their own about CR-LF conversion. */
15237 fname = get_tv_string(&argvars[0]);
15238 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15239 {
15240 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15241 return;
15242 }
15243
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015244 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015245 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015246 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015247
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015248 /* This for loop processes what was read, but is also entered at end
15249 * of file so that either:
15250 * - an incomplete line gets written
15251 * - a "binary" file gets an empty line at the end if it ends in a
15252 * newline. */
15253 for (p = buf, start = buf;
15254 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15255 ++p)
15256 {
15257 if (*p == '\n' || readlen <= 0)
15258 {
15259 listitem_T *li;
15260 char_u *s = NULL;
15261 long_u len = p - start;
15262
15263 /* Finished a line. Remove CRs before NL. */
15264 if (readlen > 0 && !binary)
15265 {
15266 while (len > 0 && start[len - 1] == '\r')
15267 --len;
15268 /* removal may cross back to the "prev" string */
15269 if (len == 0)
15270 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15271 --prevlen;
15272 }
15273 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015274 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015275 else
15276 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015277 /* Change "prev" buffer to be the right size. This way
15278 * the bytes are only copied once, and very long lines are
15279 * allocated only once. */
15280 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015281 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015282 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015283 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015284 prev = NULL; /* the list will own the string */
15285 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015286 }
15287 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015288 if (s == NULL)
15289 {
15290 do_outofmem_msg((long_u) prevlen + len + 1);
15291 failed = TRUE;
15292 break;
15293 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015294
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015295 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015296 {
15297 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015298 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015299 break;
15300 }
15301 li->li_tv.v_type = VAR_STRING;
15302 li->li_tv.v_lock = 0;
15303 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015304 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015305
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015306 start = p + 1; /* step over newline */
15307 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015308 break;
15309 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015310 else if (*p == NUL)
15311 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015312#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015313 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15314 * when finding the BF and check the previous two bytes. */
15315 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015316 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015317 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15318 * + 1, these may be in the "prev" string. */
15319 char_u back1 = p >= buf + 1 ? p[-1]
15320 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15321 char_u back2 = p >= buf + 2 ? p[-2]
15322 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15323 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015324
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015325 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015326 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015327 char_u *dest = p - 2;
15328
15329 /* Usually a BOM is at the beginning of a file, and so at
15330 * the beginning of a line; then we can just step over it.
15331 */
15332 if (start == dest)
15333 start = p + 1;
15334 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015335 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015336 /* have to shuffle buf to close gap */
15337 int adjust_prevlen = 0;
15338
15339 if (dest < buf)
15340 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015341 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015342 dest = buf;
15343 }
15344 if (readlen > p - buf + 1)
15345 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15346 readlen -= 3 - adjust_prevlen;
15347 prevlen -= adjust_prevlen;
15348 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015349 }
15350 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015351 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015352#endif
15353 } /* for */
15354
15355 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15356 break;
15357 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015358 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015359 /* There's part of a line in buf, store it in "prev". */
15360 if (p - start + prevlen >= prevsize)
15361 {
15362 /* need bigger "prev" buffer */
15363 char_u *newprev;
15364
15365 /* A common use case is ordinary text files and "prev" gets a
15366 * fragment of a line, so the first allocation is made
15367 * small, to avoid repeatedly 'allocing' large and
15368 * 'reallocing' small. */
15369 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015370 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015371 else
15372 {
15373 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015374 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015375 prevsize = grow50pc > growmin ? grow50pc : growmin;
15376 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015377 newprev = prev == NULL ? alloc(prevsize)
15378 : vim_realloc(prev, prevsize);
15379 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015380 {
15381 do_outofmem_msg((long_u)prevsize);
15382 failed = TRUE;
15383 break;
15384 }
15385 prev = newprev;
15386 }
15387 /* Add the line part to end of "prev". */
15388 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015389 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015390 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015391 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015392
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015393 /*
15394 * For a negative line count use only the lines at the end of the file,
15395 * free the rest.
15396 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015397 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015398 while (cnt > -maxline)
15399 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015400 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015401 --cnt;
15402 }
15403
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015404 if (failed)
15405 {
15406 list_free(rettv->vval.v_list, TRUE);
15407 /* readfile doc says an empty list is returned on error */
15408 rettv->vval.v_list = list_alloc();
15409 }
15410
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015411 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015412 fclose(fd);
15413}
15414
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015415#if defined(FEAT_RELTIME)
15416static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15417
15418/*
15419 * Convert a List to proftime_T.
15420 * Return FAIL when there is something wrong.
15421 */
15422 static int
15423list2proftime(arg, tm)
15424 typval_T *arg;
15425 proftime_T *tm;
15426{
15427 long n1, n2;
15428 int error = FALSE;
15429
15430 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15431 || arg->vval.v_list->lv_len != 2)
15432 return FAIL;
15433 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15434 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15435# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015436 tm->HighPart = n1;
15437 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015438# else
15439 tm->tv_sec = n1;
15440 tm->tv_usec = n2;
15441# endif
15442 return error ? FAIL : OK;
15443}
15444#endif /* FEAT_RELTIME */
15445
15446/*
15447 * "reltime()" function
15448 */
15449 static void
15450f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015451 typval_T *argvars UNUSED;
15452 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015453{
15454#ifdef FEAT_RELTIME
15455 proftime_T res;
15456 proftime_T start;
15457
15458 if (argvars[0].v_type == VAR_UNKNOWN)
15459 {
15460 /* No arguments: get current time. */
15461 profile_start(&res);
15462 }
15463 else if (argvars[1].v_type == VAR_UNKNOWN)
15464 {
15465 if (list2proftime(&argvars[0], &res) == FAIL)
15466 return;
15467 profile_end(&res);
15468 }
15469 else
15470 {
15471 /* Two arguments: compute the difference. */
15472 if (list2proftime(&argvars[0], &start) == FAIL
15473 || list2proftime(&argvars[1], &res) == FAIL)
15474 return;
15475 profile_sub(&res, &start);
15476 }
15477
15478 if (rettv_list_alloc(rettv) == OK)
15479 {
15480 long n1, n2;
15481
15482# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015483 n1 = res.HighPart;
15484 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015485# else
15486 n1 = res.tv_sec;
15487 n2 = res.tv_usec;
15488# endif
15489 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15490 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15491 }
15492#endif
15493}
15494
15495/*
15496 * "reltimestr()" function
15497 */
15498 static void
15499f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015500 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015501 typval_T *rettv;
15502{
15503#ifdef FEAT_RELTIME
15504 proftime_T tm;
15505#endif
15506
15507 rettv->v_type = VAR_STRING;
15508 rettv->vval.v_string = NULL;
15509#ifdef FEAT_RELTIME
15510 if (list2proftime(&argvars[0], &tm) == OK)
15511 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15512#endif
15513}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015514
Bram Moolenaar0d660222005-01-07 21:51:51 +000015515#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15516static void make_connection __ARGS((void));
15517static int check_connection __ARGS((void));
15518
15519 static void
15520make_connection()
15521{
15522 if (X_DISPLAY == NULL
15523# ifdef FEAT_GUI
15524 && !gui.in_use
15525# endif
15526 )
15527 {
15528 x_force_connect = TRUE;
15529 setup_term_clip();
15530 x_force_connect = FALSE;
15531 }
15532}
15533
15534 static int
15535check_connection()
15536{
15537 make_connection();
15538 if (X_DISPLAY == NULL)
15539 {
15540 EMSG(_("E240: No connection to Vim server"));
15541 return FAIL;
15542 }
15543 return OK;
15544}
15545#endif
15546
15547#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015548static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015549
15550 static void
15551remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015552 typval_T *argvars;
15553 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015554 int expr;
15555{
15556 char_u *server_name;
15557 char_u *keys;
15558 char_u *r = NULL;
15559 char_u buf[NUMBUFLEN];
15560# ifdef WIN32
15561 HWND w;
15562# else
15563 Window w;
15564# endif
15565
15566 if (check_restricted() || check_secure())
15567 return;
15568
15569# ifdef FEAT_X11
15570 if (check_connection() == FAIL)
15571 return;
15572# endif
15573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015574 server_name = get_tv_string_chk(&argvars[0]);
15575 if (server_name == NULL)
15576 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015577 keys = get_tv_string_buf(&argvars[1], buf);
15578# ifdef WIN32
15579 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15580# else
15581 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15582 < 0)
15583# endif
15584 {
15585 if (r != NULL)
15586 EMSG(r); /* sending worked but evaluation failed */
15587 else
15588 EMSG2(_("E241: Unable to send to %s"), server_name);
15589 return;
15590 }
15591
15592 rettv->vval.v_string = r;
15593
15594 if (argvars[2].v_type != VAR_UNKNOWN)
15595 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015596 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015597 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015598 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015599
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015600 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015601 v.di_tv.v_type = VAR_STRING;
15602 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015603 idvar = get_tv_string_chk(&argvars[2]);
15604 if (idvar != NULL)
15605 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015606 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015607 }
15608}
15609#endif
15610
15611/*
15612 * "remote_expr()" function
15613 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015614 static void
15615f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015616 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015617 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015618{
15619 rettv->v_type = VAR_STRING;
15620 rettv->vval.v_string = NULL;
15621#ifdef FEAT_CLIENTSERVER
15622 remote_common(argvars, rettv, TRUE);
15623#endif
15624}
15625
15626/*
15627 * "remote_foreground()" function
15628 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015629 static void
15630f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015631 typval_T *argvars UNUSED;
15632 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015633{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015634#ifdef FEAT_CLIENTSERVER
15635# ifdef WIN32
15636 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 {
15638 char_u *server_name = get_tv_string_chk(&argvars[0]);
15639
15640 if (server_name != NULL)
15641 serverForeground(server_name);
15642 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015643# else
15644 /* Send a foreground() expression to the server. */
15645 argvars[1].v_type = VAR_STRING;
15646 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15647 argvars[2].v_type = VAR_UNKNOWN;
15648 remote_common(argvars, rettv, TRUE);
15649 vim_free(argvars[1].vval.v_string);
15650# endif
15651#endif
15652}
15653
Bram Moolenaar0d660222005-01-07 21:51:51 +000015654 static void
15655f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015656 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015657 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015658{
15659#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015660 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015661 char_u *s = NULL;
15662# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015663 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015664# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015665 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015666
15667 if (check_restricted() || check_secure())
15668 {
15669 rettv->vval.v_number = -1;
15670 return;
15671 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015672 serverid = get_tv_string_chk(&argvars[0]);
15673 if (serverid == NULL)
15674 {
15675 rettv->vval.v_number = -1;
15676 return; /* type error; errmsg already given */
15677 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015678# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015679 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015680 if (n == 0)
15681 rettv->vval.v_number = -1;
15682 else
15683 {
15684 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15685 rettv->vval.v_number = (s != NULL);
15686 }
15687# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015688 if (check_connection() == FAIL)
15689 return;
15690
15691 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015692 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015693# endif
15694
15695 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015697 char_u *retvar;
15698
Bram Moolenaar33570922005-01-25 22:26:29 +000015699 v.di_tv.v_type = VAR_STRING;
15700 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015701 retvar = get_tv_string_chk(&argvars[1]);
15702 if (retvar != NULL)
15703 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015704 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015705 }
15706#else
15707 rettv->vval.v_number = -1;
15708#endif
15709}
15710
Bram Moolenaar0d660222005-01-07 21:51:51 +000015711 static void
15712f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015713 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015714 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015715{
15716 char_u *r = NULL;
15717
15718#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015719 char_u *serverid = get_tv_string_chk(&argvars[0]);
15720
15721 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015722 {
15723# ifdef WIN32
15724 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015725 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015726
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015727 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015728 if (n != 0)
15729 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15730 if (r == NULL)
15731# else
15732 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015733 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015734# endif
15735 EMSG(_("E277: Unable to read a server reply"));
15736 }
15737#endif
15738 rettv->v_type = VAR_STRING;
15739 rettv->vval.v_string = r;
15740}
15741
15742/*
15743 * "remote_send()" function
15744 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015745 static void
15746f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015747 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015748 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749{
15750 rettv->v_type = VAR_STRING;
15751 rettv->vval.v_string = NULL;
15752#ifdef FEAT_CLIENTSERVER
15753 remote_common(argvars, rettv, FALSE);
15754#endif
15755}
15756
15757/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015758 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015759 */
15760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015761f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015762 typval_T *argvars;
15763 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015764{
Bram Moolenaar33570922005-01-25 22:26:29 +000015765 list_T *l;
15766 listitem_T *item, *item2;
15767 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015768 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015769 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015770 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015771 dict_T *d;
15772 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015773 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015774
Bram Moolenaar8c711452005-01-14 21:53:12 +000015775 if (argvars[0].v_type == VAR_DICT)
15776 {
15777 if (argvars[2].v_type != VAR_UNKNOWN)
15778 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015779 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015780 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015782 key = get_tv_string_chk(&argvars[1]);
15783 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015785 di = dict_find(d, key, -1);
15786 if (di == NULL)
15787 EMSG2(_(e_dictkey), key);
15788 else
15789 {
15790 *rettv = di->di_tv;
15791 init_tv(&di->di_tv);
15792 dictitem_remove(d, di);
15793 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015794 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015795 }
15796 }
15797 else if (argvars[0].v_type != VAR_LIST)
15798 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015799 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015800 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015802 int error = FALSE;
15803
15804 idx = get_tv_number_chk(&argvars[1], &error);
15805 if (error)
15806 ; /* type error: do nothing, errmsg already given */
15807 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015808 EMSGN(_(e_listidx), idx);
15809 else
15810 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015811 if (argvars[2].v_type == VAR_UNKNOWN)
15812 {
15813 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015814 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015815 *rettv = item->li_tv;
15816 vim_free(item);
15817 }
15818 else
15819 {
15820 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015821 end = get_tv_number_chk(&argvars[2], &error);
15822 if (error)
15823 ; /* type error: do nothing */
15824 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015825 EMSGN(_(e_listidx), end);
15826 else
15827 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015828 int cnt = 0;
15829
15830 for (li = item; li != NULL; li = li->li_next)
15831 {
15832 ++cnt;
15833 if (li == item2)
15834 break;
15835 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015836 if (li == NULL) /* didn't find "item2" after "item" */
15837 EMSG(_(e_invrange));
15838 else
15839 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015840 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015841 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015842 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015843 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015844 l->lv_first = item;
15845 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015846 item->li_prev = NULL;
15847 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015848 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015849 }
15850 }
15851 }
15852 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015853 }
15854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855}
15856
15857/*
15858 * "rename({from}, {to})" function
15859 */
15860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015861f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015862 typval_T *argvars;
15863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864{
15865 char_u buf[NUMBUFLEN];
15866
15867 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015868 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015870 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15871 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872}
15873
15874/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015875 * "repeat()" function
15876 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015877 static void
15878f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015879 typval_T *argvars;
15880 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015881{
15882 char_u *p;
15883 int n;
15884 int slen;
15885 int len;
15886 char_u *r;
15887 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015888
15889 n = get_tv_number(&argvars[1]);
15890 if (argvars[0].v_type == VAR_LIST)
15891 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015892 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015893 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015894 if (list_extend(rettv->vval.v_list,
15895 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015896 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015897 }
15898 else
15899 {
15900 p = get_tv_string(&argvars[0]);
15901 rettv->v_type = VAR_STRING;
15902 rettv->vval.v_string = NULL;
15903
15904 slen = (int)STRLEN(p);
15905 len = slen * n;
15906 if (len <= 0)
15907 return;
15908
15909 r = alloc(len + 1);
15910 if (r != NULL)
15911 {
15912 for (i = 0; i < n; i++)
15913 mch_memmove(r + i * slen, p, (size_t)slen);
15914 r[len] = NUL;
15915 }
15916
15917 rettv->vval.v_string = r;
15918 }
15919}
15920
15921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 * "resolve()" function
15923 */
15924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015925f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015926 typval_T *argvars;
15927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928{
15929 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015930#ifdef HAVE_READLINK
15931 char_u *buf = NULL;
15932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015934 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935#ifdef FEAT_SHORTCUT
15936 {
15937 char_u *v = NULL;
15938
15939 v = mch_resolve_shortcut(p);
15940 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015941 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015943 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944 }
15945#else
15946# ifdef HAVE_READLINK
15947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948 char_u *cpy;
15949 int len;
15950 char_u *remain = NULL;
15951 char_u *q;
15952 int is_relative_to_current = FALSE;
15953 int has_trailing_pathsep = FALSE;
15954 int limit = 100;
15955
15956 p = vim_strsave(p);
15957
15958 if (p[0] == '.' && (vim_ispathsep(p[1])
15959 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15960 is_relative_to_current = TRUE;
15961
15962 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015963 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015966 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968
15969 q = getnextcomp(p);
15970 if (*q != NUL)
15971 {
15972 /* Separate the first path component in "p", and keep the
15973 * remainder (beginning with the path separator). */
15974 remain = vim_strsave(q - 1);
15975 q[-1] = NUL;
15976 }
15977
Bram Moolenaard9462e32011-04-11 21:35:11 +020015978 buf = alloc(MAXPATHL + 1);
15979 if (buf == NULL)
15980 goto fail;
15981
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982 for (;;)
15983 {
15984 for (;;)
15985 {
15986 len = readlink((char *)p, (char *)buf, MAXPATHL);
15987 if (len <= 0)
15988 break;
15989 buf[len] = NUL;
15990
15991 if (limit-- == 0)
15992 {
15993 vim_free(p);
15994 vim_free(remain);
15995 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015996 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997 goto fail;
15998 }
15999
16000 /* Ensure that the result will have a trailing path separator
16001 * if the argument has one. */
16002 if (remain == NULL && has_trailing_pathsep)
16003 add_pathsep(buf);
16004
16005 /* Separate the first path component in the link value and
16006 * concatenate the remainders. */
16007 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16008 if (*q != NUL)
16009 {
16010 if (remain == NULL)
16011 remain = vim_strsave(q - 1);
16012 else
16013 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016014 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015 if (cpy != NULL)
16016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 vim_free(remain);
16018 remain = cpy;
16019 }
16020 }
16021 q[-1] = NUL;
16022 }
16023
16024 q = gettail(p);
16025 if (q > p && *q == NUL)
16026 {
16027 /* Ignore trailing path separator. */
16028 q[-1] = NUL;
16029 q = gettail(p);
16030 }
16031 if (q > p && !mch_isFullName(buf))
16032 {
16033 /* symlink is relative to directory of argument */
16034 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16035 if (cpy != NULL)
16036 {
16037 STRCPY(cpy, p);
16038 STRCPY(gettail(cpy), buf);
16039 vim_free(p);
16040 p = cpy;
16041 }
16042 }
16043 else
16044 {
16045 vim_free(p);
16046 p = vim_strsave(buf);
16047 }
16048 }
16049
16050 if (remain == NULL)
16051 break;
16052
16053 /* Append the first path component of "remain" to "p". */
16054 q = getnextcomp(remain + 1);
16055 len = q - remain - (*q != NUL);
16056 cpy = vim_strnsave(p, STRLEN(p) + len);
16057 if (cpy != NULL)
16058 {
16059 STRNCAT(cpy, remain, len);
16060 vim_free(p);
16061 p = cpy;
16062 }
16063 /* Shorten "remain". */
16064 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016065 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066 else
16067 {
16068 vim_free(remain);
16069 remain = NULL;
16070 }
16071 }
16072
16073 /* If the result is a relative path name, make it explicitly relative to
16074 * the current directory if and only if the argument had this form. */
16075 if (!vim_ispathsep(*p))
16076 {
16077 if (is_relative_to_current
16078 && *p != NUL
16079 && !(p[0] == '.'
16080 && (p[1] == NUL
16081 || vim_ispathsep(p[1])
16082 || (p[1] == '.'
16083 && (p[2] == NUL
16084 || vim_ispathsep(p[2]))))))
16085 {
16086 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016087 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 if (cpy != NULL)
16089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 vim_free(p);
16091 p = cpy;
16092 }
16093 }
16094 else if (!is_relative_to_current)
16095 {
16096 /* Strip leading "./". */
16097 q = p;
16098 while (q[0] == '.' && vim_ispathsep(q[1]))
16099 q += 2;
16100 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016101 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102 }
16103 }
16104
16105 /* Ensure that the result will have no trailing path separator
16106 * if the argument had none. But keep "/" or "//". */
16107 if (!has_trailing_pathsep)
16108 {
16109 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016110 if (after_pathsep(p, q))
16111 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 }
16113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016114 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 }
16116# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016117 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118# endif
16119#endif
16120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016121 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122
16123#ifdef HAVE_READLINK
16124fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016125 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016127 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128}
16129
16130/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016131 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 */
16133 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016134f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016135 typval_T *argvars;
16136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137{
Bram Moolenaar33570922005-01-25 22:26:29 +000016138 list_T *l;
16139 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140
Bram Moolenaar0d660222005-01-07 21:51:51 +000016141 if (argvars[0].v_type != VAR_LIST)
16142 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016143 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016144 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016145 {
16146 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016147 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016148 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016149 while (li != NULL)
16150 {
16151 ni = li->li_prev;
16152 list_append(l, li);
16153 li = ni;
16154 }
16155 rettv->vval.v_list = l;
16156 rettv->v_type = VAR_LIST;
16157 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016158 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160}
16161
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016162#define SP_NOMOVE 0x01 /* don't move cursor */
16163#define SP_REPEAT 0x02 /* repeat to find outer pair */
16164#define SP_RETCOUNT 0x04 /* return matchcount */
16165#define SP_SETPCMARK 0x08 /* set previous context mark */
16166#define SP_START 0x10 /* accept match at start position */
16167#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16168#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016169
Bram Moolenaar33570922005-01-25 22:26:29 +000016170static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171
16172/*
16173 * Get flags for a search function.
16174 * Possibly sets "p_ws".
16175 * Returns BACKWARD, FORWARD or zero (for an error).
16176 */
16177 static int
16178get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016179 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016180 int *flagsp;
16181{
16182 int dir = FORWARD;
16183 char_u *flags;
16184 char_u nbuf[NUMBUFLEN];
16185 int mask;
16186
16187 if (varp->v_type != VAR_UNKNOWN)
16188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016189 flags = get_tv_string_buf_chk(varp, nbuf);
16190 if (flags == NULL)
16191 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016192 while (*flags != NUL)
16193 {
16194 switch (*flags)
16195 {
16196 case 'b': dir = BACKWARD; break;
16197 case 'w': p_ws = TRUE; break;
16198 case 'W': p_ws = FALSE; break;
16199 default: mask = 0;
16200 if (flagsp != NULL)
16201 switch (*flags)
16202 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016203 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016204 case 'e': mask = SP_END; break;
16205 case 'm': mask = SP_RETCOUNT; break;
16206 case 'n': mask = SP_NOMOVE; break;
16207 case 'p': mask = SP_SUBPAT; break;
16208 case 'r': mask = SP_REPEAT; break;
16209 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016210 }
16211 if (mask == 0)
16212 {
16213 EMSG2(_(e_invarg2), flags);
16214 dir = 0;
16215 }
16216 else
16217 *flagsp |= mask;
16218 }
16219 if (dir == 0)
16220 break;
16221 ++flags;
16222 }
16223 }
16224 return dir;
16225}
16226
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016228 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016230 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016231search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016232 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016233 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016234 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016236 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 char_u *pat;
16238 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016239 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 int save_p_ws = p_ws;
16241 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016242 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016243 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016244 proftime_T tm;
16245#ifdef FEAT_RELTIME
16246 long time_limit = 0;
16247#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016248 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016249 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016251 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016252 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016253 if (dir == 0)
16254 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016255 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016256 if (flags & SP_START)
16257 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016258 if (flags & SP_END)
16259 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016260
Bram Moolenaar76929292008-01-06 19:07:36 +000016261 /* Optional arguments: line number to stop searching and timeout. */
16262 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016263 {
16264 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16265 if (lnum_stop < 0)
16266 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016267#ifdef FEAT_RELTIME
16268 if (argvars[3].v_type != VAR_UNKNOWN)
16269 {
16270 time_limit = get_tv_number_chk(&argvars[3], NULL);
16271 if (time_limit < 0)
16272 goto theend;
16273 }
16274#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016275 }
16276
Bram Moolenaar76929292008-01-06 19:07:36 +000016277#ifdef FEAT_RELTIME
16278 /* Set the time limit, if there is one. */
16279 profile_setlimit(time_limit, &tm);
16280#endif
16281
Bram Moolenaar231334e2005-07-25 20:46:57 +000016282 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016283 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016284 * Check to make sure only those flags are set.
16285 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16286 * flags cannot be set. Check for that condition also.
16287 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016288 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016289 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016291 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016292 goto theend;
16293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016295 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016296 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016297 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016298 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016300 if (flags & SP_SUBPAT)
16301 retval = subpatnum;
16302 else
16303 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016304 if (flags & SP_SETPCMARK)
16305 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016307 if (match_pos != NULL)
16308 {
16309 /* Store the match cursor position */
16310 match_pos->lnum = pos.lnum;
16311 match_pos->col = pos.col + 1;
16312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 /* "/$" will put the cursor after the end of the line, may need to
16314 * correct that here */
16315 check_cursor();
16316 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016317
16318 /* If 'n' flag is used: restore cursor position. */
16319 if (flags & SP_NOMOVE)
16320 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016321 else
16322 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016323theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016325
16326 return retval;
16327}
16328
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016329#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016330
16331/*
16332 * round() is not in C90, use ceil() or floor() instead.
16333 */
16334 float_T
16335vim_round(f)
16336 float_T f;
16337{
16338 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16339}
16340
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016341/*
16342 * "round({float})" function
16343 */
16344 static void
16345f_round(argvars, rettv)
16346 typval_T *argvars;
16347 typval_T *rettv;
16348{
16349 float_T f;
16350
16351 rettv->v_type = VAR_FLOAT;
16352 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016353 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016354 else
16355 rettv->vval.v_float = 0.0;
16356}
16357#endif
16358
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016359/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016360 * "screenattr()" function
16361 */
16362 static void
16363f_screenattr(argvars, rettv)
16364 typval_T *argvars UNUSED;
16365 typval_T *rettv;
16366{
16367 int row;
16368 int col;
16369 int c;
16370
16371 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16372 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16373 if (row < 0 || row >= screen_Rows
16374 || col < 0 || col >= screen_Columns)
16375 c = -1;
16376 else
16377 c = ScreenAttrs[LineOffset[row] + col];
16378 rettv->vval.v_number = c;
16379}
16380
16381/*
16382 * "screenchar()" function
16383 */
16384 static void
16385f_screenchar(argvars, rettv)
16386 typval_T *argvars UNUSED;
16387 typval_T *rettv;
16388{
16389 int row;
16390 int col;
16391 int off;
16392 int c;
16393
16394 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16395 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16396 if (row < 0 || row >= screen_Rows
16397 || col < 0 || col >= screen_Columns)
16398 c = -1;
16399 else
16400 {
16401 off = LineOffset[row] + col;
16402#ifdef FEAT_MBYTE
16403 if (enc_utf8 && ScreenLinesUC[off] != 0)
16404 c = ScreenLinesUC[off];
16405 else
16406#endif
16407 c = ScreenLines[off];
16408 }
16409 rettv->vval.v_number = c;
16410}
16411
16412/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016413 * "screencol()" function
16414 *
16415 * First column is 1 to be consistent with virtcol().
16416 */
16417 static void
16418f_screencol(argvars, rettv)
16419 typval_T *argvars UNUSED;
16420 typval_T *rettv;
16421{
16422 rettv->vval.v_number = screen_screencol() + 1;
16423}
16424
16425/*
16426 * "screenrow()" function
16427 */
16428 static void
16429f_screenrow(argvars, rettv)
16430 typval_T *argvars UNUSED;
16431 typval_T *rettv;
16432{
16433 rettv->vval.v_number = screen_screenrow() + 1;
16434}
16435
16436/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016437 * "search()" function
16438 */
16439 static void
16440f_search(argvars, rettv)
16441 typval_T *argvars;
16442 typval_T *rettv;
16443{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016444 int flags = 0;
16445
16446 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447}
16448
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016450 * "searchdecl()" function
16451 */
16452 static void
16453f_searchdecl(argvars, rettv)
16454 typval_T *argvars;
16455 typval_T *rettv;
16456{
16457 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016458 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016459 int error = FALSE;
16460 char_u *name;
16461
16462 rettv->vval.v_number = 1; /* default: FAIL */
16463
16464 name = get_tv_string_chk(&argvars[0]);
16465 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016466 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016467 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016468 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16469 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16470 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016471 if (!error && name != NULL)
16472 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016473 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016474}
16475
16476/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016477 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016479 static int
16480searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016481 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016482 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483{
16484 char_u *spat, *mpat, *epat;
16485 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487 int dir;
16488 int flags = 0;
16489 char_u nbuf1[NUMBUFLEN];
16490 char_u nbuf2[NUMBUFLEN];
16491 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016492 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016493 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016494 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016497 spat = get_tv_string_chk(&argvars[0]);
16498 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16499 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16500 if (spat == NULL || mpat == NULL || epat == NULL)
16501 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 /* Handle the optional fourth argument: flags */
16504 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016505 if (dir == 0)
16506 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016507
16508 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016509 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16510 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016511 if ((flags & (SP_END | SP_SUBPAT)) != 0
16512 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016513 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016514 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016515 goto theend;
16516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016518 /* Using 'r' implies 'W', otherwise it doesn't work. */
16519 if (flags & SP_REPEAT)
16520 p_ws = FALSE;
16521
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016522 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016523 if (argvars[3].v_type == VAR_UNKNOWN
16524 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 skip = (char_u *)"";
16526 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016528 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016529 if (argvars[5].v_type != VAR_UNKNOWN)
16530 {
16531 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16532 if (lnum_stop < 0)
16533 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016534#ifdef FEAT_RELTIME
16535 if (argvars[6].v_type != VAR_UNKNOWN)
16536 {
16537 time_limit = get_tv_number_chk(&argvars[6], NULL);
16538 if (time_limit < 0)
16539 goto theend;
16540 }
16541#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016542 }
16543 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016544 if (skip == NULL)
16545 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016547 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016548 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016549
16550theend:
16551 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016552
16553 return retval;
16554}
16555
16556/*
16557 * "searchpair()" function
16558 */
16559 static void
16560f_searchpair(argvars, rettv)
16561 typval_T *argvars;
16562 typval_T *rettv;
16563{
16564 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16565}
16566
16567/*
16568 * "searchpairpos()" function
16569 */
16570 static void
16571f_searchpairpos(argvars, rettv)
16572 typval_T *argvars;
16573 typval_T *rettv;
16574{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016575 pos_T match_pos;
16576 int lnum = 0;
16577 int col = 0;
16578
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016579 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016580 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016581
16582 if (searchpair_cmn(argvars, &match_pos) > 0)
16583 {
16584 lnum = match_pos.lnum;
16585 col = match_pos.col;
16586 }
16587
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016588 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16589 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016590}
16591
16592/*
16593 * Search for a start/middle/end thing.
16594 * Used by searchpair(), see its documentation for the details.
16595 * Returns 0 or -1 for no match,
16596 */
16597 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016598do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16599 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016600 char_u *spat; /* start pattern */
16601 char_u *mpat; /* middle pattern */
16602 char_u *epat; /* end pattern */
16603 int dir; /* BACKWARD or FORWARD */
16604 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016605 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016606 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016607 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016608 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016609{
16610 char_u *save_cpo;
16611 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16612 long retval = 0;
16613 pos_T pos;
16614 pos_T firstpos;
16615 pos_T foundpos;
16616 pos_T save_cursor;
16617 pos_T save_pos;
16618 int n;
16619 int r;
16620 int nest = 1;
16621 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016622 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016623 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016624
16625 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16626 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016627 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016628
Bram Moolenaar76929292008-01-06 19:07:36 +000016629#ifdef FEAT_RELTIME
16630 /* Set the time limit, if there is one. */
16631 profile_setlimit(time_limit, &tm);
16632#endif
16633
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016634 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16635 * start/middle/end (pat3, for the top pair). */
16636 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16637 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16638 if (pat2 == NULL || pat3 == NULL)
16639 goto theend;
16640 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16641 if (*mpat == NUL)
16642 STRCPY(pat3, pat2);
16643 else
16644 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16645 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016646 if (flags & SP_START)
16647 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016648
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 save_cursor = curwin->w_cursor;
16650 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016651 clearpos(&firstpos);
16652 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 pat = pat3;
16654 for (;;)
16655 {
16656 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016657 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16659 /* didn't find it or found the first match again: FAIL */
16660 break;
16661
16662 if (firstpos.lnum == 0)
16663 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016664 if (equalpos(pos, foundpos))
16665 {
16666 /* Found the same position again. Can happen with a pattern that
16667 * has "\zs" at the end and searching backwards. Advance one
16668 * character and try again. */
16669 if (dir == BACKWARD)
16670 decl(&pos);
16671 else
16672 incl(&pos);
16673 }
16674 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016676 /* clear the start flag to avoid getting stuck here */
16677 options &= ~SEARCH_START;
16678
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 /* If the skip pattern matches, ignore this match. */
16680 if (*skip != NUL)
16681 {
16682 save_pos = curwin->w_cursor;
16683 curwin->w_cursor = pos;
16684 r = eval_to_bool(skip, &err, NULL, FALSE);
16685 curwin->w_cursor = save_pos;
16686 if (err)
16687 {
16688 /* Evaluating {skip} caused an error, break here. */
16689 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016690 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691 break;
16692 }
16693 if (r)
16694 continue;
16695 }
16696
16697 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16698 {
16699 /* Found end when searching backwards or start when searching
16700 * forward: nested pair. */
16701 ++nest;
16702 pat = pat2; /* nested, don't search for middle */
16703 }
16704 else
16705 {
16706 /* Found end when searching forward or start when searching
16707 * backward: end of (nested) pair; or found middle in outer pair. */
16708 if (--nest == 1)
16709 pat = pat3; /* outer level, search for middle */
16710 }
16711
16712 if (nest == 0)
16713 {
16714 /* Found the match: return matchcount or line number. */
16715 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016716 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016718 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016719 if (flags & SP_SETPCMARK)
16720 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 curwin->w_cursor = pos;
16722 if (!(flags & SP_REPEAT))
16723 break;
16724 nest = 1; /* search for next unmatched */
16725 }
16726 }
16727
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016728 if (match_pos != NULL)
16729 {
16730 /* Store the match cursor position */
16731 match_pos->lnum = curwin->w_cursor.lnum;
16732 match_pos->col = curwin->w_cursor.col + 1;
16733 }
16734
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016736 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 curwin->w_cursor = save_cursor;
16738
16739theend:
16740 vim_free(pat2);
16741 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016742 if (p_cpo == empty_option)
16743 p_cpo = save_cpo;
16744 else
16745 /* Darn, evaluating the {skip} expression changed the value. */
16746 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016747
16748 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749}
16750
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016751/*
16752 * "searchpos()" function
16753 */
16754 static void
16755f_searchpos(argvars, rettv)
16756 typval_T *argvars;
16757 typval_T *rettv;
16758{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016759 pos_T match_pos;
16760 int lnum = 0;
16761 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016762 int n;
16763 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016764
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016765 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016766 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016767
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016768 n = search_cmn(argvars, &match_pos, &flags);
16769 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016770 {
16771 lnum = match_pos.lnum;
16772 col = match_pos.col;
16773 }
16774
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016775 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16776 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016777 if (flags & SP_SUBPAT)
16778 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016779}
16780
16781
Bram Moolenaar0d660222005-01-07 21:51:51 +000016782 static void
16783f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016784 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016787#ifdef FEAT_CLIENTSERVER
16788 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016789 char_u *server = get_tv_string_chk(&argvars[0]);
16790 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791
Bram Moolenaar0d660222005-01-07 21:51:51 +000016792 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016793 if (server == NULL || reply == NULL)
16794 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016795 if (check_restricted() || check_secure())
16796 return;
16797# ifdef FEAT_X11
16798 if (check_connection() == FAIL)
16799 return;
16800# endif
16801
16802 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016804 EMSG(_("E258: Unable to send to client"));
16805 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016807 rettv->vval.v_number = 0;
16808#else
16809 rettv->vval.v_number = -1;
16810#endif
16811}
16812
Bram Moolenaar0d660222005-01-07 21:51:51 +000016813 static void
16814f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016815 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016816 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016817{
16818 char_u *r = NULL;
16819
16820#ifdef FEAT_CLIENTSERVER
16821# ifdef WIN32
16822 r = serverGetVimNames();
16823# else
16824 make_connection();
16825 if (X_DISPLAY != NULL)
16826 r = serverGetVimNames(X_DISPLAY);
16827# endif
16828#endif
16829 rettv->v_type = VAR_STRING;
16830 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831}
16832
16833/*
16834 * "setbufvar()" function
16835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016837f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016838 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016839 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840{
16841 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016844 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 char_u nbuf[NUMBUFLEN];
16846
16847 if (check_restricted() || check_secure())
16848 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016849 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16850 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016851 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 varp = &argvars[2];
16853
16854 if (buf != NULL && varname != NULL && varp != NULL)
16855 {
16856 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858
16859 if (*varname == '&')
16860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016861 long numval;
16862 char_u *strval;
16863 int error = FALSE;
16864
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016866 numval = get_tv_number_chk(varp, &error);
16867 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016868 if (!error && strval != NULL)
16869 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 }
16871 else
16872 {
16873 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16874 if (bufvarname != NULL)
16875 {
16876 STRCPY(bufvarname, "b:");
16877 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016878 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879 vim_free(bufvarname);
16880 }
16881 }
16882
16883 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886}
16887
16888/*
16889 * "setcmdpos()" function
16890 */
16891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016892f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016893 typval_T *argvars;
16894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016896 int pos = (int)get_tv_number(&argvars[0]) - 1;
16897
16898 if (pos >= 0)
16899 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900}
16901
16902/*
16903 * "setline()" function
16904 */
16905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016906f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016907 typval_T *argvars;
16908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909{
16910 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016911 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016912 list_T *l = NULL;
16913 listitem_T *li = NULL;
16914 long added = 0;
16915 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016917 lnum = get_tv_lnum(&argvars[0]);
16918 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016920 l = argvars[1].vval.v_list;
16921 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016923 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016924 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016925
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016926 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016927 for (;;)
16928 {
16929 if (l != NULL)
16930 {
16931 /* list argument, get next string */
16932 if (li == NULL)
16933 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016934 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016935 li = li->li_next;
16936 }
16937
16938 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016939 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016940 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016941
16942 /* When coming here from Insert mode, sync undo, so that this can be
16943 * undone separately from what was previously inserted. */
16944 if (u_sync_once == 2)
16945 {
16946 u_sync_once = 1; /* notify that u_sync() was called */
16947 u_sync(TRUE);
16948 }
16949
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016950 if (lnum <= curbuf->b_ml.ml_line_count)
16951 {
16952 /* existing line, replace it */
16953 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16954 {
16955 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016956 if (lnum == curwin->w_cursor.lnum)
16957 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016958 rettv->vval.v_number = 0; /* OK */
16959 }
16960 }
16961 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16962 {
16963 /* lnum is one past the last line, append the line */
16964 ++added;
16965 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16966 rettv->vval.v_number = 0; /* OK */
16967 }
16968
16969 if (l == NULL) /* only one string argument */
16970 break;
16971 ++lnum;
16972 }
16973
16974 if (added > 0)
16975 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976}
16977
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016978static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16979
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016981 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016982 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016983 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016984set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016985 win_T *wp UNUSED;
16986 typval_T *list_arg UNUSED;
16987 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016988 typval_T *rettv;
16989{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016990#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016991 char_u *act;
16992 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016993#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016994
Bram Moolenaar2641f772005-03-25 21:58:17 +000016995 rettv->vval.v_number = -1;
16996
16997#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016998 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016999 EMSG(_(e_listreq));
17000 else
17001 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017002 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017003
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017004 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017005 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017006 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017007 if (act == NULL)
17008 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017009 if (*act == 'a' || *act == 'r')
17010 action = *act;
17011 }
17012
Bram Moolenaar81484f42012-12-05 15:16:47 +010017013 if (l != NULL && set_errorlist(wp, l, action,
17014 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017015 rettv->vval.v_number = 0;
17016 }
17017#endif
17018}
17019
17020/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017021 * "setloclist()" function
17022 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017023 static void
17024f_setloclist(argvars, rettv)
17025 typval_T *argvars;
17026 typval_T *rettv;
17027{
17028 win_T *win;
17029
17030 rettv->vval.v_number = -1;
17031
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017032 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017033 if (win != NULL)
17034 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17035}
17036
17037/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017038 * "setmatches()" function
17039 */
17040 static void
17041f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017042 typval_T *argvars UNUSED;
17043 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017044{
17045#ifdef FEAT_SEARCH_EXTRA
17046 list_T *l;
17047 listitem_T *li;
17048 dict_T *d;
17049
17050 rettv->vval.v_number = -1;
17051 if (argvars[0].v_type != VAR_LIST)
17052 {
17053 EMSG(_(e_listreq));
17054 return;
17055 }
17056 if ((l = argvars[0].vval.v_list) != NULL)
17057 {
17058
17059 /* To some extent make sure that we are dealing with a list from
17060 * "getmatches()". */
17061 li = l->lv_first;
17062 while (li != NULL)
17063 {
17064 if (li->li_tv.v_type != VAR_DICT
17065 || (d = li->li_tv.vval.v_dict) == NULL)
17066 {
17067 EMSG(_(e_invarg));
17068 return;
17069 }
17070 if (!(dict_find(d, (char_u *)"group", -1) != NULL
17071 && dict_find(d, (char_u *)"pattern", -1) != NULL
17072 && dict_find(d, (char_u *)"priority", -1) != NULL
17073 && dict_find(d, (char_u *)"id", -1) != NULL))
17074 {
17075 EMSG(_(e_invarg));
17076 return;
17077 }
17078 li = li->li_next;
17079 }
17080
17081 clear_matches(curwin);
17082 li = l->lv_first;
17083 while (li != NULL)
17084 {
17085 d = li->li_tv.vval.v_dict;
17086 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
17087 get_dict_string(d, (char_u *)"pattern", FALSE),
17088 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020017089 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017090 li = li->li_next;
17091 }
17092 rettv->vval.v_number = 0;
17093 }
17094#endif
17095}
17096
17097/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017098 * "setpos()" function
17099 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017100 static void
17101f_setpos(argvars, rettv)
17102 typval_T *argvars;
17103 typval_T *rettv;
17104{
17105 pos_T pos;
17106 int fnum;
17107 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017108 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017109
Bram Moolenaar08250432008-02-13 11:42:46 +000017110 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017111 name = get_tv_string_chk(argvars);
17112 if (name != NULL)
17113 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017114 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017115 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017116 if (--pos.col < 0)
17117 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017118 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017119 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017120 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017121 if (fnum == curbuf->b_fnum)
17122 {
17123 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017124 if (curswant >= 0)
17125 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017126 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017127 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017128 }
17129 else
17130 EMSG(_(e_invarg));
17131 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017132 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17133 {
17134 /* set mark */
17135 if (setmark_pos(name[1], &pos, fnum) == OK)
17136 rettv->vval.v_number = 0;
17137 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017138 else
17139 EMSG(_(e_invarg));
17140 }
17141 }
17142}
17143
17144/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017145 * "setqflist()" function
17146 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017147 static void
17148f_setqflist(argvars, rettv)
17149 typval_T *argvars;
17150 typval_T *rettv;
17151{
17152 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17153}
17154
17155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 * "setreg()" function
17157 */
17158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017159f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017160 typval_T *argvars;
17161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162{
17163 int regname;
17164 char_u *strregname;
17165 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017166 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 int append;
17168 char_u yank_type;
17169 long block_len;
17170
17171 block_len = -1;
17172 yank_type = MAUTO;
17173 append = FALSE;
17174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017175 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017176 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017178 if (strregname == NULL)
17179 return; /* type error; errmsg already given */
17180 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 if (regname == 0 || regname == '@')
17182 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017184 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017186 stropt = get_tv_string_chk(&argvars[2]);
17187 if (stropt == NULL)
17188 return; /* type error */
17189 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 switch (*stropt)
17191 {
17192 case 'a': case 'A': /* append */
17193 append = TRUE;
17194 break;
17195 case 'v': case 'c': /* character-wise selection */
17196 yank_type = MCHAR;
17197 break;
17198 case 'V': case 'l': /* line-wise selection */
17199 yank_type = MLINE;
17200 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201 case 'b': case Ctrl_V: /* block-wise selection */
17202 yank_type = MBLOCK;
17203 if (VIM_ISDIGIT(stropt[1]))
17204 {
17205 ++stropt;
17206 block_len = getdigits(&stropt) - 1;
17207 --stropt;
17208 }
17209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210 }
17211 }
17212
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017213 if (argvars[1].v_type == VAR_LIST)
17214 {
17215 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017216 char_u **allocval;
17217 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017218 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017219 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017220 int len = argvars[1].vval.v_list->lv_len;
17221 listitem_T *li;
17222
Bram Moolenaar7d647822014-04-05 21:28:56 +020017223 /* First half: use for pointers to result lines; second half: use for
17224 * pointers to allocated copies. */
17225 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017226 if (lstval == NULL)
17227 return;
17228 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017229 allocval = lstval + len + 2;
17230 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017231
17232 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17233 li = li->li_next)
17234 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017235 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017236 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017237 goto free_lstval;
17238 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017239 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017240 /* Need to make a copy, next get_tv_string_buf_chk() will
17241 * overwrite the string. */
17242 strval = vim_strsave(buf);
17243 if (strval == NULL)
17244 goto free_lstval;
17245 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017246 }
17247 *curval++ = strval;
17248 }
17249 *curval++ = NULL;
17250
17251 write_reg_contents_lst(regname, lstval, -1,
17252 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017253free_lstval:
17254 while (curallocval > allocval)
17255 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017256 vim_free(lstval);
17257 }
17258 else
17259 {
17260 strval = get_tv_string_chk(&argvars[1]);
17261 if (strval == NULL)
17262 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017263 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017266 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267}
17268
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017269/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017270 * "settabvar()" function
17271 */
17272 static void
17273f_settabvar(argvars, rettv)
17274 typval_T *argvars;
17275 typval_T *rettv;
17276{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017277#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017278 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017279 tabpage_T *tp;
17280#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017281 char_u *varname, *tabvarname;
17282 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017283
17284 rettv->vval.v_number = 0;
17285
17286 if (check_restricted() || check_secure())
17287 return;
17288
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017289#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017290 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017291#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017292 varname = get_tv_string_chk(&argvars[1]);
17293 varp = &argvars[2];
17294
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017295 if (varname != NULL && varp != NULL
17296#ifdef FEAT_WINDOWS
17297 && tp != NULL
17298#endif
17299 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017300 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017301#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017302 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017303 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017304#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017305
17306 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17307 if (tabvarname != NULL)
17308 {
17309 STRCPY(tabvarname, "t:");
17310 STRCPY(tabvarname + 2, varname);
17311 set_var(tabvarname, varp, TRUE);
17312 vim_free(tabvarname);
17313 }
17314
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017315#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017316 /* Restore current tabpage */
17317 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017318 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017319#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017320 }
17321}
17322
17323/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017324 * "settabwinvar()" function
17325 */
17326 static void
17327f_settabwinvar(argvars, rettv)
17328 typval_T *argvars;
17329 typval_T *rettv;
17330{
17331 setwinvar(argvars, rettv, 1);
17332}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333
17334/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017335 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017338f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017339 typval_T *argvars;
17340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017342 setwinvar(argvars, rettv, 0);
17343}
17344
17345/*
17346 * "setwinvar()" and "settabwinvar()" functions
17347 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017348
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017349 static void
17350setwinvar(argvars, rettv, off)
17351 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017352 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017353 int off;
17354{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 win_T *win;
17356#ifdef FEAT_WINDOWS
17357 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017358 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359#endif
17360 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017361 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017363 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364
17365 if (check_restricted() || check_secure())
17366 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017367
17368#ifdef FEAT_WINDOWS
17369 if (off == 1)
17370 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17371 else
17372 tp = curtab;
17373#endif
17374 win = find_win_by_nr(&argvars[off], tp);
17375 varname = get_tv_string_chk(&argvars[off + 1]);
17376 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377
17378 if (win != NULL && varname != NULL && varp != NULL)
17379 {
17380#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017381 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017384 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017386 long numval;
17387 char_u *strval;
17388 int error = FALSE;
17389
17390 ++varname;
17391 numval = get_tv_number_chk(varp, &error);
17392 strval = get_tv_string_buf_chk(varp, nbuf);
17393 if (!error && strval != NULL)
17394 set_option_value(varname, numval, strval, OPT_LOCAL);
17395 }
17396 else
17397 {
17398 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17399 if (winvarname != NULL)
17400 {
17401 STRCPY(winvarname, "w:");
17402 STRCPY(winvarname + 2, varname);
17403 set_var(winvarname, varp, TRUE);
17404 vim_free(winvarname);
17405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 }
17407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017409 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410#endif
17411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412}
17413
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017414#ifdef FEAT_CRYPT
17415/*
17416 * "sha256({string})" function
17417 */
17418 static void
17419f_sha256(argvars, rettv)
17420 typval_T *argvars;
17421 typval_T *rettv;
17422{
17423 char_u *p;
17424
17425 p = get_tv_string(&argvars[0]);
17426 rettv->vval.v_string = vim_strsave(
17427 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17428 rettv->v_type = VAR_STRING;
17429}
17430#endif /* FEAT_CRYPT */
17431
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017433 * "shellescape({string})" function
17434 */
17435 static void
17436f_shellescape(argvars, rettv)
17437 typval_T *argvars;
17438 typval_T *rettv;
17439{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017440 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017441 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017442 rettv->v_type = VAR_STRING;
17443}
17444
17445/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017446 * shiftwidth() function
17447 */
17448 static void
17449f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017450 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017451 typval_T *rettv;
17452{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017453 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017454}
17455
17456/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017457 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 */
17459 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017460f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 typval_T *argvars;
17462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017464 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465
Bram Moolenaar0d660222005-01-07 21:51:51 +000017466 p = get_tv_string(&argvars[0]);
17467 rettv->vval.v_string = vim_strsave(p);
17468 simplify_filename(rettv->vval.v_string); /* simplify in place */
17469 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470}
17471
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017472#ifdef FEAT_FLOAT
17473/*
17474 * "sin()" function
17475 */
17476 static void
17477f_sin(argvars, rettv)
17478 typval_T *argvars;
17479 typval_T *rettv;
17480{
17481 float_T f;
17482
17483 rettv->v_type = VAR_FLOAT;
17484 if (get_float_arg(argvars, &f) == OK)
17485 rettv->vval.v_float = sin(f);
17486 else
17487 rettv->vval.v_float = 0.0;
17488}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017489
17490/*
17491 * "sinh()" function
17492 */
17493 static void
17494f_sinh(argvars, rettv)
17495 typval_T *argvars;
17496 typval_T *rettv;
17497{
17498 float_T f;
17499
17500 rettv->v_type = VAR_FLOAT;
17501 if (get_float_arg(argvars, &f) == OK)
17502 rettv->vval.v_float = sinh(f);
17503 else
17504 rettv->vval.v_float = 0.0;
17505}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017506#endif
17507
Bram Moolenaar0d660222005-01-07 21:51:51 +000017508static int
17509#ifdef __BORLANDC__
17510 _RTLENTRYF
17511#endif
17512 item_compare __ARGS((const void *s1, const void *s2));
17513static int
17514#ifdef __BORLANDC__
17515 _RTLENTRYF
17516#endif
17517 item_compare2 __ARGS((const void *s1, const void *s2));
17518
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017519/* struct used in the array that's given to qsort() */
17520typedef struct
17521{
17522 listitem_T *item;
17523 int idx;
17524} sortItem_T;
17525
Bram Moolenaar0d660222005-01-07 21:51:51 +000017526static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017527static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017528static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017529static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017530static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017531static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017532static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017533#define ITEM_COMPARE_FAIL 999
17534
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017536 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017538 static int
17539#ifdef __BORLANDC__
17540_RTLENTRYF
17541#endif
17542item_compare(s1, s2)
17543 const void *s1;
17544 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017546 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017547 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017548 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017549 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017550 int res;
17551 char_u numbuf1[NUMBUFLEN];
17552 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017554 si1 = (sortItem_T *)s1;
17555 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017556 tv1 = &si1->item->li_tv;
17557 tv2 = &si2->item->li_tv;
17558 /* tv2string() puts quotes around a string and allocates memory. Don't do
17559 * that for string variables. Use a single quote when comparing with a
17560 * non-string to do what the docs promise. */
17561 if (tv1->v_type == VAR_STRING)
17562 {
17563 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17564 p1 = (char_u *)"'";
17565 else
17566 p1 = tv1->vval.v_string;
17567 }
17568 else
17569 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17570 if (tv2->v_type == VAR_STRING)
17571 {
17572 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17573 p2 = (char_u *)"'";
17574 else
17575 p2 = tv2->vval.v_string;
17576 }
17577 else
17578 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017579 if (p1 == NULL)
17580 p1 = (char_u *)"";
17581 if (p2 == NULL)
17582 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017583 if (!item_compare_numeric)
17584 {
17585 if (item_compare_ic)
17586 res = STRICMP(p1, p2);
17587 else
17588 res = STRCMP(p1, p2);
17589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017591 {
17592 double n1, n2;
17593 n1 = strtod((char *)p1, (char **)&p1);
17594 n2 = strtod((char *)p2, (char **)&p2);
17595 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17596 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017597
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017598 /* When the result would be zero, compare the item indexes. Makes the
17599 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017600 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017601 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017602
Bram Moolenaar0d660222005-01-07 21:51:51 +000017603 vim_free(tofree1);
17604 vim_free(tofree2);
17605 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606}
17607
17608 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017609#ifdef __BORLANDC__
17610_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017612item_compare2(s1, s2)
17613 const void *s1;
17614 const void *s2;
17615{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017616 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017617 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017618 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017619 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017620 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017622 /* shortcut after failure in previous call; compare all items equal */
17623 if (item_compare_func_err)
17624 return 0;
17625
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017626 si1 = (sortItem_T *)s1;
17627 si2 = (sortItem_T *)s2;
17628
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017629 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017630 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017631 copy_tv(&si1->item->li_tv, &argv[0]);
17632 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017633
17634 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017635 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017636 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17637 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017638 clear_tv(&argv[0]);
17639 clear_tv(&argv[1]);
17640
17641 if (res == FAIL)
17642 res = ITEM_COMPARE_FAIL;
17643 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017644 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17645 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017646 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017647 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017648
17649 /* When the result would be zero, compare the pointers themselves. Makes
17650 * the sort stable. */
17651 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017652 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017653
Bram Moolenaar0d660222005-01-07 21:51:51 +000017654 return res;
17655}
17656
17657/*
17658 * "sort({list})" function
17659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017661do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017662 typval_T *argvars;
17663 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017664 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665{
Bram Moolenaar33570922005-01-25 22:26:29 +000017666 list_T *l;
17667 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017668 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017669 long len;
17670 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671
Bram Moolenaar0d660222005-01-07 21:51:51 +000017672 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017673 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 else
17675 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017676 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017677 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017678 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017679 return;
17680 rettv->vval.v_list = l;
17681 rettv->v_type = VAR_LIST;
17682 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683
Bram Moolenaar0d660222005-01-07 21:51:51 +000017684 len = list_len(l);
17685 if (len <= 1)
17686 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687
Bram Moolenaar0d660222005-01-07 21:51:51 +000017688 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017689 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017690 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017691 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017692 if (argvars[1].v_type != VAR_UNKNOWN)
17693 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017694 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017695 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017696 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017697 else
17698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017699 int error = FALSE;
17700
17701 i = get_tv_number_chk(&argvars[1], &error);
17702 if (error)
17703 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017704 if (i == 1)
17705 item_compare_ic = TRUE;
17706 else
17707 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017708 if (item_compare_func != NULL)
17709 {
17710 if (STRCMP(item_compare_func, "n") == 0)
17711 {
17712 item_compare_func = NULL;
17713 item_compare_numeric = TRUE;
17714 }
17715 else if (STRCMP(item_compare_func, "i") == 0)
17716 {
17717 item_compare_func = NULL;
17718 item_compare_ic = TRUE;
17719 }
17720 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017721 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017722
17723 if (argvars[2].v_type != VAR_UNKNOWN)
17724 {
17725 /* optional third argument: {dict} */
17726 if (argvars[2].v_type != VAR_DICT)
17727 {
17728 EMSG(_(e_dictreq));
17729 return;
17730 }
17731 item_compare_selfdict = argvars[2].vval.v_dict;
17732 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734
Bram Moolenaar0d660222005-01-07 21:51:51 +000017735 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017736 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017737 if (ptrs == NULL)
17738 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739
Bram Moolenaar327aa022014-03-25 18:24:23 +010017740 i = 0;
17741 if (sort)
17742 {
17743 /* sort(): ptrs will be the list to sort */
17744 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017745 {
17746 ptrs[i].item = li;
17747 ptrs[i].idx = i;
17748 ++i;
17749 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017750
17751 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017752 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017753 /* test the compare function */
17754 if (item_compare_func != NULL
17755 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017756 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017757 EMSG(_("E702: Sort compare function failed"));
17758 else
17759 {
17760 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017761 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017762 item_compare_func == NULL ? item_compare : item_compare2);
17763
17764 if (!item_compare_func_err)
17765 {
17766 /* Clear the List and append the items in sorted order. */
17767 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17768 l->lv_len = 0;
17769 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017770 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017771 }
17772 }
17773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017775 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017776 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17777
17778 /* f_uniq(): ptrs will be a stack of items to remove */
17779 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017780 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017781 item_compare_func_ptr = item_compare_func
17782 ? item_compare2 : item_compare;
17783
17784 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17785 li = li->li_next)
17786 {
17787 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17788 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017789 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017790 if (item_compare_func_err)
17791 {
17792 EMSG(_("E882: Uniq compare function failed"));
17793 break;
17794 }
17795 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017797 if (!item_compare_func_err)
17798 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017799 while (--i >= 0)
17800 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017801 li = ptrs[i].item->li_next;
17802 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017803 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017804 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017805 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017806 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017807 list_fix_watch(l, li);
17808 listitem_free(li);
17809 l->lv_len--;
17810 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017811 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017812 }
17813
17814 vim_free(ptrs);
17815 }
17816}
17817
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017818/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017819 * "sort({list})" function
17820 */
17821 static void
17822f_sort(argvars, rettv)
17823 typval_T *argvars;
17824 typval_T *rettv;
17825{
17826 do_sort_uniq(argvars, rettv, TRUE);
17827}
17828
17829/*
17830 * "uniq({list})" function
17831 */
17832 static void
17833f_uniq(argvars, rettv)
17834 typval_T *argvars;
17835 typval_T *rettv;
17836{
17837 do_sort_uniq(argvars, rettv, FALSE);
17838}
17839
17840/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017841 * "soundfold({word})" function
17842 */
17843 static void
17844f_soundfold(argvars, rettv)
17845 typval_T *argvars;
17846 typval_T *rettv;
17847{
17848 char_u *s;
17849
17850 rettv->v_type = VAR_STRING;
17851 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017852#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017853 rettv->vval.v_string = eval_soundfold(s);
17854#else
17855 rettv->vval.v_string = vim_strsave(s);
17856#endif
17857}
17858
17859/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017860 * "spellbadword()" function
17861 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017862 static void
17863f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017864 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017865 typval_T *rettv;
17866{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017867 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017868 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017869 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017870
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017871 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017872 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017873
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017874#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017875 if (argvars[0].v_type == VAR_UNKNOWN)
17876 {
17877 /* Find the start and length of the badly spelled word. */
17878 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17879 if (len != 0)
17880 word = ml_get_cursor();
17881 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017882 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017883 {
17884 char_u *str = get_tv_string_chk(&argvars[0]);
17885 int capcol = -1;
17886
17887 if (str != NULL)
17888 {
17889 /* Check the argument for spelling. */
17890 while (*str != NUL)
17891 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017892 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017893 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017894 {
17895 word = str;
17896 break;
17897 }
17898 str += len;
17899 }
17900 }
17901 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017902#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017903
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017904 list_append_string(rettv->vval.v_list, word, len);
17905 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017906 attr == HLF_SPB ? "bad" :
17907 attr == HLF_SPR ? "rare" :
17908 attr == HLF_SPL ? "local" :
17909 attr == HLF_SPC ? "caps" :
17910 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017911}
17912
17913/*
17914 * "spellsuggest()" function
17915 */
17916 static void
17917f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017918 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017919 typval_T *rettv;
17920{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017921#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017922 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017923 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017924 int maxcount;
17925 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017926 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017927 listitem_T *li;
17928 int need_capital = FALSE;
17929#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017930
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017931 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017932 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017933
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017934#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017935 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017936 {
17937 str = get_tv_string(&argvars[0]);
17938 if (argvars[1].v_type != VAR_UNKNOWN)
17939 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017940 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017941 if (maxcount <= 0)
17942 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017943 if (argvars[2].v_type != VAR_UNKNOWN)
17944 {
17945 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17946 if (typeerr)
17947 return;
17948 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017949 }
17950 else
17951 maxcount = 25;
17952
Bram Moolenaar4770d092006-01-12 23:22:24 +000017953 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017954
17955 for (i = 0; i < ga.ga_len; ++i)
17956 {
17957 str = ((char_u **)ga.ga_data)[i];
17958
17959 li = listitem_alloc();
17960 if (li == NULL)
17961 vim_free(str);
17962 else
17963 {
17964 li->li_tv.v_type = VAR_STRING;
17965 li->li_tv.v_lock = 0;
17966 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017967 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017968 }
17969 }
17970 ga_clear(&ga);
17971 }
17972#endif
17973}
17974
Bram Moolenaar0d660222005-01-07 21:51:51 +000017975 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017976f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017977 typval_T *argvars;
17978 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017979{
17980 char_u *str;
17981 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017982 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017983 regmatch_T regmatch;
17984 char_u patbuf[NUMBUFLEN];
17985 char_u *save_cpo;
17986 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017987 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017988 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017989 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017990
17991 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17992 save_cpo = p_cpo;
17993 p_cpo = (char_u *)"";
17994
17995 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017996 if (argvars[1].v_type != VAR_UNKNOWN)
17997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017998 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17999 if (pat == NULL)
18000 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018001 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018002 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018003 }
18004 if (pat == NULL || *pat == NUL)
18005 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018006
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018007 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018009 if (typeerr)
18010 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011
Bram Moolenaar0d660222005-01-07 21:51:51 +000018012 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18013 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018015 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018016 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018017 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018018 if (*str == NUL)
18019 match = FALSE; /* empty item at the end */
18020 else
18021 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018022 if (match)
18023 end = regmatch.startp[0];
18024 else
18025 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018026 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18027 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018028 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018029 if (list_append_string(rettv->vval.v_list, str,
18030 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018031 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018032 }
18033 if (!match)
18034 break;
18035 /* Advance to just after the match. */
18036 if (regmatch.endp[0] > str)
18037 col = 0;
18038 else
18039 {
18040 /* Don't get stuck at the same match. */
18041#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018042 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018043#else
18044 col = 1;
18045#endif
18046 }
18047 str = regmatch.endp[0];
18048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049
Bram Moolenaar473de612013-06-08 18:19:48 +020018050 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052
Bram Moolenaar0d660222005-01-07 21:51:51 +000018053 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054}
18055
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018056#ifdef FEAT_FLOAT
18057/*
18058 * "sqrt()" function
18059 */
18060 static void
18061f_sqrt(argvars, rettv)
18062 typval_T *argvars;
18063 typval_T *rettv;
18064{
18065 float_T f;
18066
18067 rettv->v_type = VAR_FLOAT;
18068 if (get_float_arg(argvars, &f) == OK)
18069 rettv->vval.v_float = sqrt(f);
18070 else
18071 rettv->vval.v_float = 0.0;
18072}
18073
18074/*
18075 * "str2float()" function
18076 */
18077 static void
18078f_str2float(argvars, rettv)
18079 typval_T *argvars;
18080 typval_T *rettv;
18081{
18082 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18083
18084 if (*p == '+')
18085 p = skipwhite(p + 1);
18086 (void)string2float(p, &rettv->vval.v_float);
18087 rettv->v_type = VAR_FLOAT;
18088}
18089#endif
18090
Bram Moolenaar2c932302006-03-18 21:42:09 +000018091/*
18092 * "str2nr()" function
18093 */
18094 static void
18095f_str2nr(argvars, rettv)
18096 typval_T *argvars;
18097 typval_T *rettv;
18098{
18099 int base = 10;
18100 char_u *p;
18101 long n;
18102
18103 if (argvars[1].v_type != VAR_UNKNOWN)
18104 {
18105 base = get_tv_number(&argvars[1]);
18106 if (base != 8 && base != 10 && base != 16)
18107 {
18108 EMSG(_(e_invarg));
18109 return;
18110 }
18111 }
18112
18113 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018114 if (*p == '+')
18115 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018116 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
18117 rettv->vval.v_number = n;
18118}
18119
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120#ifdef HAVE_STRFTIME
18121/*
18122 * "strftime({format}[, {time}])" function
18123 */
18124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018125f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018126 typval_T *argvars;
18127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128{
18129 char_u result_buf[256];
18130 struct tm *curtime;
18131 time_t seconds;
18132 char_u *p;
18133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018134 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018136 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018137 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 seconds = time(NULL);
18139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018140 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141 curtime = localtime(&seconds);
18142 /* MSVC returns NULL for an invalid value of seconds. */
18143 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018144 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 else
18146 {
18147# ifdef FEAT_MBYTE
18148 vimconv_T conv;
18149 char_u *enc;
18150
18151 conv.vc_type = CONV_NONE;
18152 enc = enc_locale();
18153 convert_setup(&conv, p_enc, enc);
18154 if (conv.vc_type != CONV_NONE)
18155 p = string_convert(&conv, p, NULL);
18156# endif
18157 if (p != NULL)
18158 (void)strftime((char *)result_buf, sizeof(result_buf),
18159 (char *)p, curtime);
18160 else
18161 result_buf[0] = NUL;
18162
18163# ifdef FEAT_MBYTE
18164 if (conv.vc_type != CONV_NONE)
18165 vim_free(p);
18166 convert_setup(&conv, enc, p_enc);
18167 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018168 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169 else
18170# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018171 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172
18173# ifdef FEAT_MBYTE
18174 /* Release conversion descriptors */
18175 convert_setup(&conv, NULL, NULL);
18176 vim_free(enc);
18177# endif
18178 }
18179}
18180#endif
18181
18182/*
18183 * "stridx()" function
18184 */
18185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018186f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018187 typval_T *argvars;
18188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189{
18190 char_u buf[NUMBUFLEN];
18191 char_u *needle;
18192 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018193 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018195 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018197 needle = get_tv_string_chk(&argvars[1]);
18198 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018199 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018200 if (needle == NULL || haystack == NULL)
18201 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202
Bram Moolenaar33570922005-01-25 22:26:29 +000018203 if (argvars[2].v_type != VAR_UNKNOWN)
18204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018205 int error = FALSE;
18206
18207 start_idx = get_tv_number_chk(&argvars[2], &error);
18208 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018209 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018210 if (start_idx >= 0)
18211 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018212 }
18213
18214 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18215 if (pos != NULL)
18216 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217}
18218
18219/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018220 * "string()" function
18221 */
18222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018223f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018224 typval_T *argvars;
18225 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018226{
18227 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018228 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018230 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018231 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018232 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018233 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018234 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235}
18236
18237/*
18238 * "strlen()" function
18239 */
18240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018241f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018242 typval_T *argvars;
18243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018245 rettv->vval.v_number = (varnumber_T)(STRLEN(
18246 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247}
18248
18249/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018250 * "strchars()" function
18251 */
18252 static void
18253f_strchars(argvars, rettv)
18254 typval_T *argvars;
18255 typval_T *rettv;
18256{
18257 char_u *s = get_tv_string(&argvars[0]);
18258#ifdef FEAT_MBYTE
18259 varnumber_T len = 0;
18260
18261 while (*s != NUL)
18262 {
18263 mb_cptr2char_adv(&s);
18264 ++len;
18265 }
18266 rettv->vval.v_number = len;
18267#else
18268 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18269#endif
18270}
18271
18272/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018273 * "strdisplaywidth()" function
18274 */
18275 static void
18276f_strdisplaywidth(argvars, rettv)
18277 typval_T *argvars;
18278 typval_T *rettv;
18279{
18280 char_u *s = get_tv_string(&argvars[0]);
18281 int col = 0;
18282
18283 if (argvars[1].v_type != VAR_UNKNOWN)
18284 col = get_tv_number(&argvars[1]);
18285
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018286 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018287}
18288
18289/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018290 * "strwidth()" function
18291 */
18292 static void
18293f_strwidth(argvars, rettv)
18294 typval_T *argvars;
18295 typval_T *rettv;
18296{
18297 char_u *s = get_tv_string(&argvars[0]);
18298
18299 rettv->vval.v_number = (varnumber_T)(
18300#ifdef FEAT_MBYTE
18301 mb_string2cells(s, -1)
18302#else
18303 STRLEN(s)
18304#endif
18305 );
18306}
18307
18308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 * "strpart()" function
18310 */
18311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018312f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018313 typval_T *argvars;
18314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315{
18316 char_u *p;
18317 int n;
18318 int len;
18319 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018320 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018322 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 slen = (int)STRLEN(p);
18324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018325 n = get_tv_number_chk(&argvars[1], &error);
18326 if (error)
18327 len = 0;
18328 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018329 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 else
18331 len = slen - n; /* default len: all bytes that are available. */
18332
18333 /*
18334 * Only return the overlap between the specified part and the actual
18335 * string.
18336 */
18337 if (n < 0)
18338 {
18339 len += n;
18340 n = 0;
18341 }
18342 else if (n > slen)
18343 n = slen;
18344 if (len < 0)
18345 len = 0;
18346 else if (n + len > slen)
18347 len = slen - n;
18348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018349 rettv->v_type = VAR_STRING;
18350 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351}
18352
18353/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018354 * "strridx()" function
18355 */
18356 static void
18357f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018358 typval_T *argvars;
18359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018360{
18361 char_u buf[NUMBUFLEN];
18362 char_u *needle;
18363 char_u *haystack;
18364 char_u *rest;
18365 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018366 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018368 needle = get_tv_string_chk(&argvars[1]);
18369 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018370
18371 rettv->vval.v_number = -1;
18372 if (needle == NULL || haystack == NULL)
18373 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018374
18375 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018376 if (argvars[2].v_type != VAR_UNKNOWN)
18377 {
18378 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018379 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018380 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018381 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018382 }
18383 else
18384 end_idx = haystack_len;
18385
Bram Moolenaar0d660222005-01-07 21:51:51 +000018386 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018387 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018388 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018389 lastmatch = haystack + end_idx;
18390 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018391 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018392 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018393 for (rest = haystack; *rest != '\0'; ++rest)
18394 {
18395 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018396 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018397 break;
18398 lastmatch = rest;
18399 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018400 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018401
18402 if (lastmatch == NULL)
18403 rettv->vval.v_number = -1;
18404 else
18405 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18406}
18407
18408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 * "strtrans()" function
18410 */
18411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018412f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018413 typval_T *argvars;
18414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018416 rettv->v_type = VAR_STRING;
18417 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418}
18419
18420/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018421 * "submatch()" function
18422 */
18423 static void
18424f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018425 typval_T *argvars;
18426 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018427{
Bram Moolenaar41571762014-04-02 19:00:58 +020018428 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018429 int no;
18430 int retList = 0;
18431
18432 no = (int)get_tv_number_chk(&argvars[0], &error);
18433 if (error)
18434 return;
18435 error = FALSE;
18436 if (argvars[1].v_type != VAR_UNKNOWN)
18437 retList = get_tv_number_chk(&argvars[1], &error);
18438 if (error)
18439 return;
18440
18441 if (retList == 0)
18442 {
18443 rettv->v_type = VAR_STRING;
18444 rettv->vval.v_string = reg_submatch(no);
18445 }
18446 else
18447 {
18448 rettv->v_type = VAR_LIST;
18449 rettv->vval.v_list = reg_submatch_list(no);
18450 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018451}
18452
18453/*
18454 * "substitute()" function
18455 */
18456 static void
18457f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018458 typval_T *argvars;
18459 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018460{
18461 char_u patbuf[NUMBUFLEN];
18462 char_u subbuf[NUMBUFLEN];
18463 char_u flagsbuf[NUMBUFLEN];
18464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018465 char_u *str = get_tv_string_chk(&argvars[0]);
18466 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18467 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18468 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18469
Bram Moolenaar0d660222005-01-07 21:51:51 +000018470 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018471 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18472 rettv->vval.v_string = NULL;
18473 else
18474 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018475}
18476
18477/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018478 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018481f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018482 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484{
18485 int id = 0;
18486#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018487 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 long col;
18489 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018490 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018492 lnum = get_tv_lnum(argvars); /* -1 on type error */
18493 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18494 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018496 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018497 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018498 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499#endif
18500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018501 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502}
18503
18504/*
18505 * "synIDattr(id, what [, mode])" function
18506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018508f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018509 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511{
18512 char_u *p = NULL;
18513#ifdef FEAT_SYN_HL
18514 int id;
18515 char_u *what;
18516 char_u *mode;
18517 char_u modebuf[NUMBUFLEN];
18518 int modec;
18519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018520 id = get_tv_number(&argvars[0]);
18521 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018522 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018524 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018526 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 modec = 0; /* replace invalid with current */
18528 }
18529 else
18530 {
18531#ifdef FEAT_GUI
18532 if (gui.in_use)
18533 modec = 'g';
18534 else
18535#endif
18536 if (t_colors > 1)
18537 modec = 'c';
18538 else
18539 modec = 't';
18540 }
18541
18542
18543 switch (TOLOWER_ASC(what[0]))
18544 {
18545 case 'b':
18546 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18547 p = highlight_color(id, what, modec);
18548 else /* bold */
18549 p = highlight_has_attr(id, HL_BOLD, modec);
18550 break;
18551
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018552 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 p = highlight_color(id, what, modec);
18554 break;
18555
18556 case 'i':
18557 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18558 p = highlight_has_attr(id, HL_INVERSE, modec);
18559 else /* italic */
18560 p = highlight_has_attr(id, HL_ITALIC, modec);
18561 break;
18562
18563 case 'n': /* name */
18564 p = get_highlight_name(NULL, id - 1);
18565 break;
18566
18567 case 'r': /* reverse */
18568 p = highlight_has_attr(id, HL_INVERSE, modec);
18569 break;
18570
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018571 case 's':
18572 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18573 p = highlight_color(id, what, modec);
18574 else /* standout */
18575 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 break;
18577
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018578 case 'u':
18579 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18580 /* underline */
18581 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18582 else
18583 /* undercurl */
18584 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 break;
18586 }
18587
18588 if (p != NULL)
18589 p = vim_strsave(p);
18590#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018591 rettv->v_type = VAR_STRING;
18592 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593}
18594
18595/*
18596 * "synIDtrans(id)" function
18597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018599f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018600 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602{
18603 int id;
18604
18605#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018606 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607
18608 if (id > 0)
18609 id = syn_get_final_id(id);
18610 else
18611#endif
18612 id = 0;
18613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018614 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615}
18616
18617/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018618 * "synconcealed(lnum, col)" function
18619 */
18620 static void
18621f_synconcealed(argvars, rettv)
18622 typval_T *argvars UNUSED;
18623 typval_T *rettv;
18624{
18625#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18626 long lnum;
18627 long col;
18628 int syntax_flags = 0;
18629 int cchar;
18630 int matchid = 0;
18631 char_u str[NUMBUFLEN];
18632#endif
18633
18634 rettv->v_type = VAR_LIST;
18635 rettv->vval.v_list = NULL;
18636
18637#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18638 lnum = get_tv_lnum(argvars); /* -1 on type error */
18639 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18640
18641 vim_memset(str, NUL, sizeof(str));
18642
18643 if (rettv_list_alloc(rettv) != FAIL)
18644 {
18645 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18646 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18647 && curwin->w_p_cole > 0)
18648 {
18649 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18650 syntax_flags = get_syntax_info(&matchid);
18651
18652 /* get the conceal character */
18653 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18654 {
18655 cchar = syn_get_sub_char();
18656 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18657 cchar = lcs_conceal;
18658 if (cchar != NUL)
18659 {
18660# ifdef FEAT_MBYTE
18661 if (has_mbyte)
18662 (*mb_char2bytes)(cchar, str);
18663 else
18664# endif
18665 str[0] = cchar;
18666 }
18667 }
18668 }
18669
18670 list_append_number(rettv->vval.v_list,
18671 (syntax_flags & HL_CONCEAL) != 0);
18672 /* -1 to auto-determine strlen */
18673 list_append_string(rettv->vval.v_list, str, -1);
18674 list_append_number(rettv->vval.v_list, matchid);
18675 }
18676#endif
18677}
18678
18679/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018680 * "synstack(lnum, col)" function
18681 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018682 static void
18683f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018684 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018685 typval_T *rettv;
18686{
18687#ifdef FEAT_SYN_HL
18688 long lnum;
18689 long col;
18690 int i;
18691 int id;
18692#endif
18693
18694 rettv->v_type = VAR_LIST;
18695 rettv->vval.v_list = NULL;
18696
18697#ifdef FEAT_SYN_HL
18698 lnum = get_tv_lnum(argvars); /* -1 on type error */
18699 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18700
18701 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018702 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018703 && rettv_list_alloc(rettv) != FAIL)
18704 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018705 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018706 for (i = 0; ; ++i)
18707 {
18708 id = syn_get_stack_item(i);
18709 if (id < 0)
18710 break;
18711 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18712 break;
18713 }
18714 }
18715#endif
18716}
18717
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018719get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018720 typval_T *argvars;
18721 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018722 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018724 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018726 char_u *infile = NULL;
18727 char_u buf[NUMBUFLEN];
18728 int err = FALSE;
18729 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018730 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018731 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018733 rettv->v_type = VAR_STRING;
18734 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018735 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018736 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018737
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018738 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018739 {
18740 /*
18741 * Write the string to a temp file, to be used for input of the shell
18742 * command.
18743 */
18744 if ((infile = vim_tempname('i')) == NULL)
18745 {
18746 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018747 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018748 }
18749
18750 fd = mch_fopen((char *)infile, WRITEBIN);
18751 if (fd == NULL)
18752 {
18753 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018754 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018755 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018756 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018757 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018758 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18759 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018760 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018761 else
18762 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018763 size_t len;
18764
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018765 p = get_tv_string_buf_chk(&argvars[1], buf);
18766 if (p == NULL)
18767 {
18768 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018769 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018770 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018771 len = STRLEN(p);
18772 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018773 err = TRUE;
18774 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018775 if (fclose(fd) != 0)
18776 err = TRUE;
18777 if (err)
18778 {
18779 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018780 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018781 }
18782 }
18783
Bram Moolenaar52a72462014-08-29 15:53:52 +020018784 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18785 * echoes typeahead, that messes up the display. */
18786 if (!msg_silent)
18787 flags += SHELL_COOKED;
18788
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018789 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018791 int len;
18792 listitem_T *li;
18793 char_u *s = NULL;
18794 char_u *start;
18795 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018796 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797
Bram Moolenaar52a72462014-08-29 15:53:52 +020018798 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018799 if (res == NULL)
18800 goto errret;
18801
18802 list = list_alloc();
18803 if (list == NULL)
18804 goto errret;
18805
18806 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018808 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018809 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018810 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018811 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018812
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018813 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018814 if (s == NULL)
18815 goto errret;
18816
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018817 for (p = s; start < end; ++p, ++start)
18818 *p = *start == NUL ? NL : *start;
18819 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018820
18821 li = listitem_alloc();
18822 if (li == NULL)
18823 {
18824 vim_free(s);
18825 goto errret;
18826 }
18827 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010018828 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018829 li->li_tv.vval.v_string = s;
18830 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018832
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018833 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018834 rettv->v_type = VAR_LIST;
18835 rettv->vval.v_list = list;
18836 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018838 else
18839 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018840 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018841#ifdef USE_CR
18842 /* translate <CR> into <NL> */
18843 if (res != NULL)
18844 {
18845 char_u *s;
18846
18847 for (s = res; *s; ++s)
18848 {
18849 if (*s == CAR)
18850 *s = NL;
18851 }
18852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853#else
18854# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018855 /* translate <CR><NL> into <NL> */
18856 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018858 char_u *s, *d;
18859
18860 d = res;
18861 for (s = res; *s; ++s)
18862 {
18863 if (s[0] == CAR && s[1] == NL)
18864 ++s;
18865 *d++ = *s;
18866 }
18867 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018869# endif
18870#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018871 rettv->vval.v_string = res;
18872 res = NULL;
18873 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018874
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018875errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018876 if (infile != NULL)
18877 {
18878 mch_remove(infile);
18879 vim_free(infile);
18880 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018881 if (res != NULL)
18882 vim_free(res);
18883 if (list != NULL)
18884 list_free(list, TRUE);
18885}
18886
18887/*
18888 * "system()" function
18889 */
18890 static void
18891f_system(argvars, rettv)
18892 typval_T *argvars;
18893 typval_T *rettv;
18894{
18895 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18896}
18897
18898/*
18899 * "systemlist()" function
18900 */
18901 static void
18902f_systemlist(argvars, rettv)
18903 typval_T *argvars;
18904 typval_T *rettv;
18905{
18906 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907}
18908
18909/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018910 * "tabpagebuflist()" function
18911 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018912 static void
18913f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018914 typval_T *argvars UNUSED;
18915 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018916{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018917#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018918 tabpage_T *tp;
18919 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018920
18921 if (argvars[0].v_type == VAR_UNKNOWN)
18922 wp = firstwin;
18923 else
18924 {
18925 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18926 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018927 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018928 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018929 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018930 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018931 for (; wp != NULL; wp = wp->w_next)
18932 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018933 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018934 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018935 }
18936#endif
18937}
18938
18939
18940/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018941 * "tabpagenr()" function
18942 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018943 static void
18944f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018945 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018946 typval_T *rettv;
18947{
18948 int nr = 1;
18949#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018950 char_u *arg;
18951
18952 if (argvars[0].v_type != VAR_UNKNOWN)
18953 {
18954 arg = get_tv_string_chk(&argvars[0]);
18955 nr = 0;
18956 if (arg != NULL)
18957 {
18958 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018959 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018960 else
18961 EMSG2(_(e_invexpr2), arg);
18962 }
18963 }
18964 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018965 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018966#endif
18967 rettv->vval.v_number = nr;
18968}
18969
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018970
18971#ifdef FEAT_WINDOWS
18972static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18973
18974/*
18975 * Common code for tabpagewinnr() and winnr().
18976 */
18977 static int
18978get_winnr(tp, argvar)
18979 tabpage_T *tp;
18980 typval_T *argvar;
18981{
18982 win_T *twin;
18983 int nr = 1;
18984 win_T *wp;
18985 char_u *arg;
18986
18987 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18988 if (argvar->v_type != VAR_UNKNOWN)
18989 {
18990 arg = get_tv_string_chk(argvar);
18991 if (arg == NULL)
18992 nr = 0; /* type error; errmsg already given */
18993 else if (STRCMP(arg, "$") == 0)
18994 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18995 else if (STRCMP(arg, "#") == 0)
18996 {
18997 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18998 if (twin == NULL)
18999 nr = 0;
19000 }
19001 else
19002 {
19003 EMSG2(_(e_invexpr2), arg);
19004 nr = 0;
19005 }
19006 }
19007
19008 if (nr > 0)
19009 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19010 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019011 {
19012 if (wp == NULL)
19013 {
19014 /* didn't find it in this tabpage */
19015 nr = 0;
19016 break;
19017 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019018 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019019 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019020 return nr;
19021}
19022#endif
19023
19024/*
19025 * "tabpagewinnr()" function
19026 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019027 static void
19028f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019029 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019030 typval_T *rettv;
19031{
19032 int nr = 1;
19033#ifdef FEAT_WINDOWS
19034 tabpage_T *tp;
19035
19036 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19037 if (tp == NULL)
19038 nr = 0;
19039 else
19040 nr = get_winnr(tp, &argvars[1]);
19041#endif
19042 rettv->vval.v_number = nr;
19043}
19044
19045
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019046/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019047 * "tagfiles()" function
19048 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019049 static void
19050f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019051 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019052 typval_T *rettv;
19053{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019054 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019055 tagname_T tn;
19056 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019057
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019058 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019059 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019060 fname = alloc(MAXPATHL);
19061 if (fname == NULL)
19062 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019063
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019064 for (first = TRUE; ; first = FALSE)
19065 if (get_tagfname(&tn, first, fname) == FAIL
19066 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019067 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019068 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019069 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019070}
19071
19072/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019073 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019074 */
19075 static void
19076f_taglist(argvars, rettv)
19077 typval_T *argvars;
19078 typval_T *rettv;
19079{
19080 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019081
19082 tag_pattern = get_tv_string(&argvars[0]);
19083
19084 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019085 if (*tag_pattern == NUL)
19086 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019087
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019088 if (rettv_list_alloc(rettv) == OK)
19089 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019090}
19091
19092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 * "tempname()" function
19094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019096f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019097 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099{
19100 static int x = 'A';
19101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019102 rettv->v_type = VAR_STRING;
19103 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104
19105 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19106 * names. Skip 'I' and 'O', they are used for shell redirection. */
19107 do
19108 {
19109 if (x == 'Z')
19110 x = '0';
19111 else if (x == '9')
19112 x = 'A';
19113 else
19114 {
19115#ifdef EBCDIC
19116 if (x == 'I')
19117 x = 'J';
19118 else if (x == 'R')
19119 x = 'S';
19120 else
19121#endif
19122 ++x;
19123 }
19124 } while (x == 'I' || x == 'O');
19125}
19126
19127/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019128 * "test(list)" function: Just checking the walls...
19129 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019130 static void
19131f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019132 typval_T *argvars UNUSED;
19133 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019134{
19135 /* Used for unit testing. Change the code below to your liking. */
19136#if 0
19137 listitem_T *li;
19138 list_T *l;
19139 char_u *bad, *good;
19140
19141 if (argvars[0].v_type != VAR_LIST)
19142 return;
19143 l = argvars[0].vval.v_list;
19144 if (l == NULL)
19145 return;
19146 li = l->lv_first;
19147 if (li == NULL)
19148 return;
19149 bad = get_tv_string(&li->li_tv);
19150 li = li->li_next;
19151 if (li == NULL)
19152 return;
19153 good = get_tv_string(&li->li_tv);
19154 rettv->vval.v_number = test_edit_score(bad, good);
19155#endif
19156}
19157
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019158#ifdef FEAT_FLOAT
19159/*
19160 * "tan()" function
19161 */
19162 static void
19163f_tan(argvars, rettv)
19164 typval_T *argvars;
19165 typval_T *rettv;
19166{
19167 float_T f;
19168
19169 rettv->v_type = VAR_FLOAT;
19170 if (get_float_arg(argvars, &f) == OK)
19171 rettv->vval.v_float = tan(f);
19172 else
19173 rettv->vval.v_float = 0.0;
19174}
19175
19176/*
19177 * "tanh()" function
19178 */
19179 static void
19180f_tanh(argvars, rettv)
19181 typval_T *argvars;
19182 typval_T *rettv;
19183{
19184 float_T f;
19185
19186 rettv->v_type = VAR_FLOAT;
19187 if (get_float_arg(argvars, &f) == OK)
19188 rettv->vval.v_float = tanh(f);
19189 else
19190 rettv->vval.v_float = 0.0;
19191}
19192#endif
19193
Bram Moolenaard52d9742005-08-21 22:20:28 +000019194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 * "tolower(string)" function
19196 */
19197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019198f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019199 typval_T *argvars;
19200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201{
19202 char_u *p;
19203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 p = vim_strsave(get_tv_string(&argvars[0]));
19205 rettv->v_type = VAR_STRING;
19206 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207
19208 if (p != NULL)
19209 while (*p != NUL)
19210 {
19211#ifdef FEAT_MBYTE
19212 int l;
19213
19214 if (enc_utf8)
19215 {
19216 int c, lc;
19217
19218 c = utf_ptr2char(p);
19219 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019220 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 /* TODO: reallocate string when byte count changes. */
19222 if (utf_char2len(lc) == l)
19223 utf_char2bytes(lc, p);
19224 p += l;
19225 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019226 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 p += l; /* skip multi-byte character */
19228 else
19229#endif
19230 {
19231 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19232 ++p;
19233 }
19234 }
19235}
19236
19237/*
19238 * "toupper(string)" function
19239 */
19240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019241f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019242 typval_T *argvars;
19243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019245 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019246 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247}
19248
19249/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019250 * "tr(string, fromstr, tostr)" function
19251 */
19252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019253f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019254 typval_T *argvars;
19255 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019256{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019257 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019258 char_u *fromstr;
19259 char_u *tostr;
19260 char_u *p;
19261#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019262 int inlen;
19263 int fromlen;
19264 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019265 int idx;
19266 char_u *cpstr;
19267 int cplen;
19268 int first = TRUE;
19269#endif
19270 char_u buf[NUMBUFLEN];
19271 char_u buf2[NUMBUFLEN];
19272 garray_T ga;
19273
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019274 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019275 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19276 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019277
19278 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019279 rettv->v_type = VAR_STRING;
19280 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019281 if (fromstr == NULL || tostr == NULL)
19282 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019283 ga_init2(&ga, (int)sizeof(char), 80);
19284
19285#ifdef FEAT_MBYTE
19286 if (!has_mbyte)
19287#endif
19288 /* not multi-byte: fromstr and tostr must be the same length */
19289 if (STRLEN(fromstr) != STRLEN(tostr))
19290 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019291#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019292error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019293#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019294 EMSG2(_(e_invarg2), fromstr);
19295 ga_clear(&ga);
19296 return;
19297 }
19298
19299 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019300 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019301 {
19302#ifdef FEAT_MBYTE
19303 if (has_mbyte)
19304 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019305 inlen = (*mb_ptr2len)(in_str);
19306 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019307 cplen = inlen;
19308 idx = 0;
19309 for (p = fromstr; *p != NUL; p += fromlen)
19310 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019311 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019312 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019313 {
19314 for (p = tostr; *p != NUL; p += tolen)
19315 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019316 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019317 if (idx-- == 0)
19318 {
19319 cplen = tolen;
19320 cpstr = p;
19321 break;
19322 }
19323 }
19324 if (*p == NUL) /* tostr is shorter than fromstr */
19325 goto error;
19326 break;
19327 }
19328 ++idx;
19329 }
19330
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019331 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019332 {
19333 /* Check that fromstr and tostr have the same number of
19334 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019335 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019336 first = FALSE;
19337 for (p = tostr; *p != NUL; p += tolen)
19338 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019339 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019340 --idx;
19341 }
19342 if (idx != 0)
19343 goto error;
19344 }
19345
19346 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019347 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019348 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019349
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019350 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019351 }
19352 else
19353#endif
19354 {
19355 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019356 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019357 if (p != NULL)
19358 ga_append(&ga, tostr[p - fromstr]);
19359 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019360 ga_append(&ga, *in_str);
19361 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019362 }
19363 }
19364
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019365 /* add a terminating NUL */
19366 ga_grow(&ga, 1);
19367 ga_append(&ga, NUL);
19368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019369 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019370}
19371
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019372#ifdef FEAT_FLOAT
19373/*
19374 * "trunc({float})" function
19375 */
19376 static void
19377f_trunc(argvars, rettv)
19378 typval_T *argvars;
19379 typval_T *rettv;
19380{
19381 float_T f;
19382
19383 rettv->v_type = VAR_FLOAT;
19384 if (get_float_arg(argvars, &f) == OK)
19385 /* trunc() is not in C90, use floor() or ceil() instead. */
19386 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19387 else
19388 rettv->vval.v_float = 0.0;
19389}
19390#endif
19391
Bram Moolenaar8299df92004-07-10 09:47:34 +000019392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393 * "type(expr)" function
19394 */
19395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019396f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019397 typval_T *argvars;
19398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019400 int n;
19401
19402 switch (argvars[0].v_type)
19403 {
19404 case VAR_NUMBER: n = 0; break;
19405 case VAR_STRING: n = 1; break;
19406 case VAR_FUNC: n = 2; break;
19407 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019408 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019409#ifdef FEAT_FLOAT
19410 case VAR_FLOAT: n = 5; break;
19411#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019412 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19413 }
19414 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415}
19416
19417/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019418 * "undofile(name)" function
19419 */
19420 static void
19421f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019422 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019423 typval_T *rettv;
19424{
19425 rettv->v_type = VAR_STRING;
19426#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019427 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019428 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019429
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019430 if (*fname == NUL)
19431 {
19432 /* If there is no file name there will be no undo file. */
19433 rettv->vval.v_string = NULL;
19434 }
19435 else
19436 {
19437 char_u *ffname = FullName_save(fname, FALSE);
19438
19439 if (ffname != NULL)
19440 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19441 vim_free(ffname);
19442 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019443 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019444#else
19445 rettv->vval.v_string = NULL;
19446#endif
19447}
19448
19449/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019450 * "undotree()" function
19451 */
19452 static void
19453f_undotree(argvars, rettv)
19454 typval_T *argvars UNUSED;
19455 typval_T *rettv;
19456{
19457 if (rettv_dict_alloc(rettv) == OK)
19458 {
19459 dict_T *dict = rettv->vval.v_dict;
19460 list_T *list;
19461
Bram Moolenaar730cde92010-06-27 05:18:54 +020019462 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019463 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019464 dict_add_nr_str(dict, "save_last",
19465 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019466 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19467 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019468 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019469
19470 list = list_alloc();
19471 if (list != NULL)
19472 {
19473 u_eval_tree(curbuf->b_u_oldhead, list);
19474 dict_add_list(dict, "entries", list);
19475 }
19476 }
19477}
19478
19479/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019480 * "values(dict)" function
19481 */
19482 static void
19483f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019484 typval_T *argvars;
19485 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019486{
19487 dict_list(argvars, rettv, 1);
19488}
19489
19490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 * "virtcol(string)" function
19492 */
19493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019494f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019495 typval_T *argvars;
19496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497{
19498 colnr_T vcol = 0;
19499 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019500 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019502 fp = var2fpos(&argvars[0], FALSE, &fnum);
19503 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19504 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 {
19506 getvvcol(curwin, fp, NULL, NULL, &vcol);
19507 ++vcol;
19508 }
19509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019510 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511}
19512
19513/*
19514 * "visualmode()" function
19515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019517f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019518 typval_T *argvars UNUSED;
19519 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 char_u str[2];
19522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 str[0] = curbuf->b_visual_mode_eval;
19525 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019526 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527
19528 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019529 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531}
19532
19533/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019534 * "wildmenumode()" function
19535 */
19536 static void
19537f_wildmenumode(argvars, rettv)
19538 typval_T *argvars UNUSED;
19539 typval_T *rettv UNUSED;
19540{
19541#ifdef FEAT_WILDMENU
19542 if (wild_menu_showing)
19543 rettv->vval.v_number = 1;
19544#endif
19545}
19546
19547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 * "winbufnr(nr)" function
19549 */
19550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019551f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019552 typval_T *argvars;
19553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554{
19555 win_T *wp;
19556
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019557 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019559 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019561 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562}
19563
19564/*
19565 * "wincol()" function
19566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019568f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019569 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571{
19572 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019573 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574}
19575
19576/*
19577 * "winheight(nr)" function
19578 */
19579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019580f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019581 typval_T *argvars;
19582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583{
19584 win_T *wp;
19585
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019586 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019588 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019590 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591}
19592
19593/*
19594 * "winline()" function
19595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019597f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019598 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600{
19601 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019602 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603}
19604
19605/*
19606 * "winnr()" function
19607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019609f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019610 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612{
19613 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019614
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019616 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019618 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619}
19620
19621/*
19622 * "winrestcmd()" function
19623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019625f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019626 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628{
19629#ifdef FEAT_WINDOWS
19630 win_T *wp;
19631 int winnr = 1;
19632 garray_T ga;
19633 char_u buf[50];
19634
19635 ga_init2(&ga, (int)sizeof(char), 70);
19636 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19637 {
19638 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19639 ga_concat(&ga, buf);
19640# ifdef FEAT_VERTSPLIT
19641 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19642 ga_concat(&ga, buf);
19643# endif
19644 ++winnr;
19645 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019646 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019648 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019650 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019652 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653}
19654
19655/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019656 * "winrestview()" function
19657 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019658 static void
19659f_winrestview(argvars, rettv)
19660 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019661 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019662{
19663 dict_T *dict;
19664
19665 if (argvars[0].v_type != VAR_DICT
19666 || (dict = argvars[0].vval.v_dict) == NULL)
19667 EMSG(_(e_invarg));
19668 else
19669 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019670 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19671 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19672 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19673 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019674#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019675 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19676 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019677#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019678 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19679 {
19680 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19681 curwin->w_set_curswant = FALSE;
19682 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019683
Bram Moolenaar82c25852014-05-28 16:47:16 +020019684 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19685 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019686#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019687 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19688 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019689#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019690 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19691 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19692 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19693 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019694
19695 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019696 win_new_height(curwin, curwin->w_height);
19697# ifdef FEAT_VERTSPLIT
19698 win_new_width(curwin, W_WIDTH(curwin));
19699# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019700 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019701
Bram Moolenaarb851a962014-10-31 15:45:52 +010019702 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019703 curwin->w_topline = 1;
19704 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19705 curwin->w_topline = curbuf->b_ml.ml_line_count;
19706#ifdef FEAT_DIFF
19707 check_topfill(curwin, TRUE);
19708#endif
19709 }
19710}
19711
19712/*
19713 * "winsaveview()" function
19714 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019715 static void
19716f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019717 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019718 typval_T *rettv;
19719{
19720 dict_T *dict;
19721
Bram Moolenaara800b422010-06-27 01:15:55 +020019722 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019723 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019724 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019725
19726 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19727 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19728#ifdef FEAT_VIRTUALEDIT
19729 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19730#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019731 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019732 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19733
19734 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19735#ifdef FEAT_DIFF
19736 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19737#endif
19738 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19739 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19740}
19741
19742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 * "winwidth(nr)" function
19744 */
19745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019746f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 typval_T *argvars;
19748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749{
19750 win_T *wp;
19751
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019752 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019754 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 else
19756#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019759 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760#endif
19761}
19762
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019764 * Write list of strings to file
19765 */
19766 static int
19767write_list(fd, list, binary)
19768 FILE *fd;
19769 list_T *list;
19770 int binary;
19771{
19772 listitem_T *li;
19773 int c;
19774 int ret = OK;
19775 char_u *s;
19776
19777 for (li = list->lv_first; li != NULL; li = li->li_next)
19778 {
19779 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19780 {
19781 if (*s == '\n')
19782 c = putc(NUL, fd);
19783 else
19784 c = putc(*s, fd);
19785 if (c == EOF)
19786 {
19787 ret = FAIL;
19788 break;
19789 }
19790 }
19791 if (!binary || li->li_next != NULL)
19792 if (putc('\n', fd) == EOF)
19793 {
19794 ret = FAIL;
19795 break;
19796 }
19797 if (ret == FAIL)
19798 {
19799 EMSG(_(e_write));
19800 break;
19801 }
19802 }
19803 return ret;
19804}
19805
19806/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019807 * "writefile()" function
19808 */
19809 static void
19810f_writefile(argvars, rettv)
19811 typval_T *argvars;
19812 typval_T *rettv;
19813{
19814 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019815 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019816 char_u *fname;
19817 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019818 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019819
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019820 if (check_restricted() || check_secure())
19821 return;
19822
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019823 if (argvars[0].v_type != VAR_LIST)
19824 {
19825 EMSG2(_(e_listarg), "writefile()");
19826 return;
19827 }
19828 if (argvars[0].vval.v_list == NULL)
19829 return;
19830
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019831 if (argvars[2].v_type != VAR_UNKNOWN)
19832 {
19833 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19834 binary = TRUE;
19835 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19836 append = TRUE;
19837 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019838
19839 /* Always open the file in binary mode, library functions have a mind of
19840 * their own about CR-LF conversion. */
19841 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019842 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19843 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019844 {
19845 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19846 ret = -1;
19847 }
19848 else
19849 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019850 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19851 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019852 fclose(fd);
19853 }
19854
19855 rettv->vval.v_number = ret;
19856}
19857
19858/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019859 * "xor(expr, expr)" function
19860 */
19861 static void
19862f_xor(argvars, rettv)
19863 typval_T *argvars;
19864 typval_T *rettv;
19865{
19866 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19867 ^ get_tv_number_chk(&argvars[1], NULL);
19868}
19869
19870
19871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019873 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 */
19875 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019876var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019877 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019878 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019879 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019881 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019883 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884
Bram Moolenaara5525202006-03-02 22:52:09 +000019885 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019886 if (varp->v_type == VAR_LIST)
19887 {
19888 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019889 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019890 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019891 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019892
19893 l = varp->vval.v_list;
19894 if (l == NULL)
19895 return NULL;
19896
19897 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019898 pos.lnum = list_find_nr(l, 0L, &error);
19899 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019900 return NULL; /* invalid line number */
19901
19902 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019903 pos.col = list_find_nr(l, 1L, &error);
19904 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019905 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019906 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019907
19908 /* We accept "$" for the column number: last column. */
19909 li = list_find(l, 1L);
19910 if (li != NULL && li->li_tv.v_type == VAR_STRING
19911 && li->li_tv.vval.v_string != NULL
19912 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19913 pos.col = len + 1;
19914
Bram Moolenaara5525202006-03-02 22:52:09 +000019915 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019916 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019917 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019918 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019919
Bram Moolenaara5525202006-03-02 22:52:09 +000019920#ifdef FEAT_VIRTUALEDIT
19921 /* Get the virtual offset. Defaults to zero. */
19922 pos.coladd = list_find_nr(l, 2L, &error);
19923 if (error)
19924 pos.coladd = 0;
19925#endif
19926
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019927 return &pos;
19928 }
19929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019930 name = get_tv_string_chk(varp);
19931 if (name == NULL)
19932 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019933 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019935 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19936 {
19937 if (VIsual_active)
19938 return &VIsual;
19939 return &curwin->w_cursor;
19940 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019941 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019943 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19945 return NULL;
19946 return pp;
19947 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019948
19949#ifdef FEAT_VIRTUALEDIT
19950 pos.coladd = 0;
19951#endif
19952
Bram Moolenaar477933c2007-07-17 14:32:23 +000019953 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019954 {
19955 pos.col = 0;
19956 if (name[1] == '0') /* "w0": first visible line */
19957 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019958 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019959 pos.lnum = curwin->w_topline;
19960 return &pos;
19961 }
19962 else if (name[1] == '$') /* "w$": last visible line */
19963 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019964 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019965 pos.lnum = curwin->w_botline - 1;
19966 return &pos;
19967 }
19968 }
19969 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019971 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 {
19973 pos.lnum = curbuf->b_ml.ml_line_count;
19974 pos.col = 0;
19975 }
19976 else
19977 {
19978 pos.lnum = curwin->w_cursor.lnum;
19979 pos.col = (colnr_T)STRLEN(ml_get_curline());
19980 }
19981 return &pos;
19982 }
19983 return NULL;
19984}
19985
19986/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019987 * Convert list in "arg" into a position and optional file number.
19988 * When "fnump" is NULL there is no file number, only 3 items.
19989 * Note that the column is passed on as-is, the caller may want to decrement
19990 * it to use 1 for the first column.
19991 * Return FAIL when conversion is not possible, doesn't check the position for
19992 * validity.
19993 */
19994 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020019995list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019996 typval_T *arg;
19997 pos_T *posp;
19998 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020019999 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020000{
20001 list_T *l = arg->vval.v_list;
20002 long i = 0;
20003 long n;
20004
Bram Moolenaar493c1782014-05-28 14:34:46 +020020005 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20006 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020007 if (arg->v_type != VAR_LIST
20008 || l == NULL
20009 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020010 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020011 return FAIL;
20012
20013 if (fnump != NULL)
20014 {
20015 n = list_find_nr(l, i++, NULL); /* fnum */
20016 if (n < 0)
20017 return FAIL;
20018 if (n == 0)
20019 n = curbuf->b_fnum; /* current buffer */
20020 *fnump = n;
20021 }
20022
20023 n = list_find_nr(l, i++, NULL); /* lnum */
20024 if (n < 0)
20025 return FAIL;
20026 posp->lnum = n;
20027
20028 n = list_find_nr(l, i++, NULL); /* col */
20029 if (n < 0)
20030 return FAIL;
20031 posp->col = n;
20032
20033#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020034 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020035 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020036 posp->coladd = 0;
20037 else
20038 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020039#endif
20040
Bram Moolenaar493c1782014-05-28 14:34:46 +020020041 if (curswantp != NULL)
20042 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20043
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020044 return OK;
20045}
20046
20047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 * Get the length of an environment variable name.
20049 * Advance "arg" to the first character after the name.
20050 * Return 0 for error.
20051 */
20052 static int
20053get_env_len(arg)
20054 char_u **arg;
20055{
20056 char_u *p;
20057 int len;
20058
20059 for (p = *arg; vim_isIDc(*p); ++p)
20060 ;
20061 if (p == *arg) /* no name found */
20062 return 0;
20063
20064 len = (int)(p - *arg);
20065 *arg = p;
20066 return len;
20067}
20068
20069/*
20070 * Get the length of the name of a function or internal variable.
20071 * "arg" is advanced to the first non-white character after the name.
20072 * Return 0 if something is wrong.
20073 */
20074 static int
20075get_id_len(arg)
20076 char_u **arg;
20077{
20078 char_u *p;
20079 int len;
20080
20081 /* Find the end of the name. */
20082 for (p = *arg; eval_isnamec(*p); ++p)
20083 ;
20084 if (p == *arg) /* no name found */
20085 return 0;
20086
20087 len = (int)(p - *arg);
20088 *arg = skipwhite(p);
20089
20090 return len;
20091}
20092
20093/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020094 * Get the length of the name of a variable or function.
20095 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020097 * Return -1 if curly braces expansion failed.
20098 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 * If the name contains 'magic' {}'s, expand them and return the
20100 * expanded name in an allocated string via 'alias' - caller must free.
20101 */
20102 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020103get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 char_u **arg;
20105 char_u **alias;
20106 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020107 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108{
20109 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 char_u *p;
20111 char_u *expr_start;
20112 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113
20114 *alias = NULL; /* default to no alias */
20115
20116 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20117 && (*arg)[2] == (int)KE_SNR)
20118 {
20119 /* hard coded <SNR>, already translated */
20120 *arg += 3;
20121 return get_id_len(arg) + 3;
20122 }
20123 len = eval_fname_script(*arg);
20124 if (len > 0)
20125 {
20126 /* literal "<SID>", "s:" or "<SNR>" */
20127 *arg += len;
20128 }
20129
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020131 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020133 p = find_name_end(*arg, &expr_start, &expr_end,
20134 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 if (expr_start != NULL)
20136 {
20137 char_u *temp_string;
20138
20139 if (!evaluate)
20140 {
20141 len += (int)(p - *arg);
20142 *arg = skipwhite(p);
20143 return len;
20144 }
20145
20146 /*
20147 * Include any <SID> etc in the expanded string:
20148 * Thus the -len here.
20149 */
20150 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20151 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020152 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 *alias = temp_string;
20154 *arg = skipwhite(p);
20155 return (int)STRLEN(temp_string);
20156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157
20158 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020159 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 EMSG2(_(e_invexpr2), *arg);
20161
20162 return len;
20163}
20164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020165/*
20166 * Find the end of a variable or function name, taking care of magic braces.
20167 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20168 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020169 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020170 * Return a pointer to just after the name. Equal to "arg" if there is no
20171 * valid name.
20172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020174find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175 char_u *arg;
20176 char_u **expr_start;
20177 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020178 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020180 int mb_nest = 0;
20181 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 char_u *p;
20183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020184 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020186 *expr_start = NULL;
20187 *expr_end = NULL;
20188 }
20189
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020190 /* Quick check for valid starting character. */
20191 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20192 return arg;
20193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020194 for (p = arg; *p != NUL
20195 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020196 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020197 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020198 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020199 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020200 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020201 if (*p == '\'')
20202 {
20203 /* skip over 'string' to avoid counting [ and ] inside it. */
20204 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20205 ;
20206 if (*p == NUL)
20207 break;
20208 }
20209 else if (*p == '"')
20210 {
20211 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20212 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20213 if (*p == '\\' && p[1] != NUL)
20214 ++p;
20215 if (*p == NUL)
20216 break;
20217 }
20218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020219 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020221 if (*p == '[')
20222 ++br_nest;
20223 else if (*p == ']')
20224 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020227 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020229 if (*p == '{')
20230 {
20231 mb_nest++;
20232 if (expr_start != NULL && *expr_start == NULL)
20233 *expr_start = p;
20234 }
20235 else if (*p == '}')
20236 {
20237 mb_nest--;
20238 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20239 *expr_end = p;
20240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 }
20243
20244 return p;
20245}
20246
20247/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020248 * Expands out the 'magic' {}'s in a variable/function name.
20249 * Note that this can call itself recursively, to deal with
20250 * constructs like foo{bar}{baz}{bam}
20251 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20252 * "in_start" ^
20253 * "expr_start" ^
20254 * "expr_end" ^
20255 * "in_end" ^
20256 *
20257 * Returns a new allocated string, which the caller must free.
20258 * Returns NULL for failure.
20259 */
20260 static char_u *
20261make_expanded_name(in_start, expr_start, expr_end, in_end)
20262 char_u *in_start;
20263 char_u *expr_start;
20264 char_u *expr_end;
20265 char_u *in_end;
20266{
20267 char_u c1;
20268 char_u *retval = NULL;
20269 char_u *temp_result;
20270 char_u *nextcmd = NULL;
20271
20272 if (expr_end == NULL || in_end == NULL)
20273 return NULL;
20274 *expr_start = NUL;
20275 *expr_end = NUL;
20276 c1 = *in_end;
20277 *in_end = NUL;
20278
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020279 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020280 if (temp_result != NULL && nextcmd == NULL)
20281 {
20282 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20283 + (in_end - expr_end) + 1));
20284 if (retval != NULL)
20285 {
20286 STRCPY(retval, in_start);
20287 STRCAT(retval, temp_result);
20288 STRCAT(retval, expr_end + 1);
20289 }
20290 }
20291 vim_free(temp_result);
20292
20293 *in_end = c1; /* put char back for error messages */
20294 *expr_start = '{';
20295 *expr_end = '}';
20296
20297 if (retval != NULL)
20298 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020299 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020300 if (expr_start != NULL)
20301 {
20302 /* Further expansion! */
20303 temp_result = make_expanded_name(retval, expr_start,
20304 expr_end, temp_result);
20305 vim_free(retval);
20306 retval = temp_result;
20307 }
20308 }
20309
20310 return retval;
20311}
20312
20313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020315 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 */
20317 static int
20318eval_isnamec(c)
20319 int c;
20320{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020321 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20322}
20323
20324/*
20325 * Return TRUE if character "c" can be used as the first character in a
20326 * variable or function name (excluding '{' and '}').
20327 */
20328 static int
20329eval_isnamec1(c)
20330 int c;
20331{
20332 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333}
20334
20335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336 * Set number v: variable to "val".
20337 */
20338 void
20339set_vim_var_nr(idx, val)
20340 int idx;
20341 long val;
20342{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020343 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344}
20345
20346/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020347 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 */
20349 long
20350get_vim_var_nr(idx)
20351 int idx;
20352{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020353 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354}
20355
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020356/*
20357 * Get string v: variable value. Uses a static buffer, can only be used once.
20358 */
20359 char_u *
20360get_vim_var_str(idx)
20361 int idx;
20362{
20363 return get_tv_string(&vimvars[idx].vv_tv);
20364}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020365
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020367 * Get List v: variable value. Caller must take care of reference count when
20368 * needed.
20369 */
20370 list_T *
20371get_vim_var_list(idx)
20372 int idx;
20373{
20374 return vimvars[idx].vv_list;
20375}
20376
20377/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020378 * Set v:char to character "c".
20379 */
20380 void
20381set_vim_var_char(c)
20382 int c;
20383{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020384 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020385
20386#ifdef FEAT_MBYTE
20387 if (has_mbyte)
20388 buf[(*mb_char2bytes)(c, buf)] = NUL;
20389 else
20390#endif
20391 {
20392 buf[0] = c;
20393 buf[1] = NUL;
20394 }
20395 set_vim_var_string(VV_CHAR, buf, -1);
20396}
20397
20398/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020399 * Set v:count to "count" and v:count1 to "count1".
20400 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401 */
20402 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020403set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 long count;
20405 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020406 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020408 if (set_prevcount)
20409 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020410 vimvars[VV_COUNT].vv_nr = count;
20411 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412}
20413
20414/*
20415 * Set string v: variable to a copy of "val".
20416 */
20417 void
20418set_vim_var_string(idx, val, len)
20419 int idx;
20420 char_u *val;
20421 int len; /* length of "val" to use or -1 (whole string) */
20422{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020423 /* Need to do this (at least) once, since we can't initialize a union.
20424 * Will always be invoked when "v:progname" is set. */
20425 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20426
Bram Moolenaare9a41262005-01-15 22:18:47 +000020427 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020429 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020431 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020433 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434}
20435
20436/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020437 * Set List v: variable to "val".
20438 */
20439 void
20440set_vim_var_list(idx, val)
20441 int idx;
20442 list_T *val;
20443{
20444 list_unref(vimvars[idx].vv_list);
20445 vimvars[idx].vv_list = val;
20446 if (val != NULL)
20447 ++val->lv_refcount;
20448}
20449
20450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 * Set v:register if needed.
20452 */
20453 void
20454set_reg_var(c)
20455 int c;
20456{
20457 char_u regname;
20458
20459 if (c == 0 || c == ' ')
20460 regname = '"';
20461 else
20462 regname = c;
20463 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020464 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465 set_vim_var_string(VV_REG, &regname, 1);
20466}
20467
20468/*
20469 * Get or set v:exception. If "oldval" == NULL, return the current value.
20470 * Otherwise, restore the value to "oldval" and return NULL.
20471 * Must always be called in pairs to save and restore v:exception! Does not
20472 * take care of memory allocations.
20473 */
20474 char_u *
20475v_exception(oldval)
20476 char_u *oldval;
20477{
20478 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020479 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020480
Bram Moolenaare9a41262005-01-15 22:18:47 +000020481 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 return NULL;
20483}
20484
20485/*
20486 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20487 * Otherwise, restore the value to "oldval" and return NULL.
20488 * Must always be called in pairs to save and restore v:throwpoint! Does not
20489 * take care of memory allocations.
20490 */
20491 char_u *
20492v_throwpoint(oldval)
20493 char_u *oldval;
20494{
20495 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020496 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497
Bram Moolenaare9a41262005-01-15 22:18:47 +000020498 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 return NULL;
20500}
20501
20502#if defined(FEAT_AUTOCMD) || defined(PROTO)
20503/*
20504 * Set v:cmdarg.
20505 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20506 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20507 * Must always be called in pairs!
20508 */
20509 char_u *
20510set_cmdarg(eap, oldarg)
20511 exarg_T *eap;
20512 char_u *oldarg;
20513{
20514 char_u *oldval;
20515 char_u *newval;
20516 unsigned len;
20517
Bram Moolenaare9a41262005-01-15 22:18:47 +000020518 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020519 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020521 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020522 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020523 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 }
20525
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020526 if (eap->force_bin == FORCE_BIN)
20527 len = 6;
20528 else if (eap->force_bin == FORCE_NOBIN)
20529 len = 8;
20530 else
20531 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020532
20533 if (eap->read_edit)
20534 len += 7;
20535
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020536 if (eap->force_ff != 0)
20537 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20538# ifdef FEAT_MBYTE
20539 if (eap->force_enc != 0)
20540 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020541 if (eap->bad_char != 0)
20542 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020543# endif
20544
20545 newval = alloc(len + 1);
20546 if (newval == NULL)
20547 return NULL;
20548
20549 if (eap->force_bin == FORCE_BIN)
20550 sprintf((char *)newval, " ++bin");
20551 else if (eap->force_bin == FORCE_NOBIN)
20552 sprintf((char *)newval, " ++nobin");
20553 else
20554 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020555
20556 if (eap->read_edit)
20557 STRCAT(newval, " ++edit");
20558
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020559 if (eap->force_ff != 0)
20560 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20561 eap->cmd + eap->force_ff);
20562# ifdef FEAT_MBYTE
20563 if (eap->force_enc != 0)
20564 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20565 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020566 if (eap->bad_char == BAD_KEEP)
20567 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20568 else if (eap->bad_char == BAD_DROP)
20569 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20570 else if (eap->bad_char != 0)
20571 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020572# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020573 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020574 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575}
20576#endif
20577
20578/*
20579 * Get the value of internal variable "name".
20580 * Return OK or FAIL.
20581 */
20582 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020583get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 char_u *name;
20585 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020586 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020587 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020588 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589{
20590 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020591 typval_T *tv = NULL;
20592 typval_T atv;
20593 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595
20596 /* truncate the name, so that we can use strcmp() */
20597 cc = name[len];
20598 name[len] = NUL;
20599
20600 /*
20601 * Check for "b:changedtick".
20602 */
20603 if (STRCMP(name, "b:changedtick") == 0)
20604 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020605 atv.v_type = VAR_NUMBER;
20606 atv.vval.v_number = curbuf->b_changedtick;
20607 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 }
20609
20610 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 * Check for user-defined variables.
20612 */
20613 else
20614 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020615 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020617 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 }
20619
Bram Moolenaare9a41262005-01-15 22:18:47 +000020620 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020622 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020623 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 ret = FAIL;
20625 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020626 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020627 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628
20629 name[len] = cc;
20630
20631 return ret;
20632}
20633
20634/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020635 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20636 * Also handle function call with Funcref variable: func(expr)
20637 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20638 */
20639 static int
20640handle_subscript(arg, rettv, evaluate, verbose)
20641 char_u **arg;
20642 typval_T *rettv;
20643 int evaluate; /* do more than finding the end */
20644 int verbose; /* give error messages */
20645{
20646 int ret = OK;
20647 dict_T *selfdict = NULL;
20648 char_u *s;
20649 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020650 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020651
20652 while (ret == OK
20653 && (**arg == '['
20654 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020655 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020656 && !vim_iswhite(*(*arg - 1)))
20657 {
20658 if (**arg == '(')
20659 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020660 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020661 if (evaluate)
20662 {
20663 functv = *rettv;
20664 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020665
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020666 /* Invoke the function. Recursive! */
20667 s = functv.vval.v_string;
20668 }
20669 else
20670 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020671 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020672 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20673 &len, evaluate, selfdict);
20674
20675 /* Clear the funcref afterwards, so that deleting it while
20676 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020677 if (evaluate)
20678 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020679
20680 /* Stop the expression evaluation when immediately aborting on
20681 * error, or when an interrupt occurred or an exception was thrown
20682 * but not caught. */
20683 if (aborting())
20684 {
20685 if (ret == OK)
20686 clear_tv(rettv);
20687 ret = FAIL;
20688 }
20689 dict_unref(selfdict);
20690 selfdict = NULL;
20691 }
20692 else /* **arg == '[' || **arg == '.' */
20693 {
20694 dict_unref(selfdict);
20695 if (rettv->v_type == VAR_DICT)
20696 {
20697 selfdict = rettv->vval.v_dict;
20698 if (selfdict != NULL)
20699 ++selfdict->dv_refcount;
20700 }
20701 else
20702 selfdict = NULL;
20703 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20704 {
20705 clear_tv(rettv);
20706 ret = FAIL;
20707 }
20708 }
20709 }
20710 dict_unref(selfdict);
20711 return ret;
20712}
20713
20714/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020715 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020716 * value).
20717 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020718 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020719alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020720{
Bram Moolenaar33570922005-01-25 22:26:29 +000020721 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020722}
20723
20724/*
20725 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 * The string "s" must have been allocated, it is consumed.
20727 * Return NULL for out of memory, the variable otherwise.
20728 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020729 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020730alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 char_u *s;
20732{
Bram Moolenaar33570922005-01-25 22:26:29 +000020733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020735 rettv = alloc_tv();
20736 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020738 rettv->v_type = VAR_STRING;
20739 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 }
20741 else
20742 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020743 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744}
20745
20746/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020747 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020749 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020750free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020751 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752{
20753 if (varp != NULL)
20754 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020755 switch (varp->v_type)
20756 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020757 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020758 func_unref(varp->vval.v_string);
20759 /*FALLTHROUGH*/
20760 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020761 vim_free(varp->vval.v_string);
20762 break;
20763 case VAR_LIST:
20764 list_unref(varp->vval.v_list);
20765 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020766 case VAR_DICT:
20767 dict_unref(varp->vval.v_dict);
20768 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020769 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020770#ifdef FEAT_FLOAT
20771 case VAR_FLOAT:
20772#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020773 case VAR_UNKNOWN:
20774 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020775 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020776 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020777 break;
20778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 vim_free(varp);
20780 }
20781}
20782
20783/*
20784 * Free the memory for a variable value and set the value to NULL or 0.
20785 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020786 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020787clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020788 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789{
20790 if (varp != NULL)
20791 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020792 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020794 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020795 func_unref(varp->vval.v_string);
20796 /*FALLTHROUGH*/
20797 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020798 vim_free(varp->vval.v_string);
20799 varp->vval.v_string = NULL;
20800 break;
20801 case VAR_LIST:
20802 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020803 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020804 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020805 case VAR_DICT:
20806 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020807 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020808 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020809 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020810 varp->vval.v_number = 0;
20811 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020812#ifdef FEAT_FLOAT
20813 case VAR_FLOAT:
20814 varp->vval.v_float = 0.0;
20815 break;
20816#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020817 case VAR_UNKNOWN:
20818 break;
20819 default:
20820 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020822 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 }
20824}
20825
20826/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020827 * Set the value of a variable to NULL without freeing items.
20828 */
20829 static void
20830init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020831 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020832{
20833 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020834 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020835}
20836
20837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 * Get the number value of a variable.
20839 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020840 * For incompatible types, return 0.
20841 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20842 * caller of incompatible types: it sets *denote to TRUE if "denote"
20843 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 */
20845 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020846get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020847 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020849 int error = FALSE;
20850
20851 return get_tv_number_chk(varp, &error); /* return 0L on error */
20852}
20853
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020854 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020855get_tv_number_chk(varp, denote)
20856 typval_T *varp;
20857 int *denote;
20858{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020859 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020861 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020863 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020864 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020865#ifdef FEAT_FLOAT
20866 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020867 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020868 break;
20869#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020870 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020871 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020872 break;
20873 case VAR_STRING:
20874 if (varp->vval.v_string != NULL)
20875 vim_str2nr(varp->vval.v_string, NULL, NULL,
20876 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020877 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020878 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020879 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020880 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020881 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020882 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020883 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020884 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020885 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020886 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020888 if (denote == NULL) /* useful for values that must be unsigned */
20889 n = -1;
20890 else
20891 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020892 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893}
20894
20895/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020896 * Get the lnum from the first argument.
20897 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020898 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 */
20900 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020901get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020902 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903{
Bram Moolenaar33570922005-01-25 22:26:29 +000020904 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 linenr_T lnum;
20906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020907 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 if (lnum == 0) /* no valid number, try using line() */
20909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020910 rettv.v_type = VAR_NUMBER;
20911 f_line(argvars, &rettv);
20912 lnum = rettv.vval.v_number;
20913 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 }
20915 return lnum;
20916}
20917
20918/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020919 * Get the lnum from the first argument.
20920 * Also accepts "$", then "buf" is used.
20921 * Returns 0 on error.
20922 */
20923 static linenr_T
20924get_tv_lnum_buf(argvars, buf)
20925 typval_T *argvars;
20926 buf_T *buf;
20927{
20928 if (argvars[0].v_type == VAR_STRING
20929 && argvars[0].vval.v_string != NULL
20930 && argvars[0].vval.v_string[0] == '$'
20931 && buf != NULL)
20932 return buf->b_ml.ml_line_count;
20933 return get_tv_number_chk(&argvars[0], NULL);
20934}
20935
20936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 * Get the string value of a variable.
20938 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020939 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20940 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 * If the String variable has never been set, return an empty string.
20942 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020943 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20944 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 */
20946 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020947get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020948 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949{
20950 static char_u mybuf[NUMBUFLEN];
20951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020952 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953}
20954
20955 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020956get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020957 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020958 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020960 char_u *res = get_tv_string_buf_chk(varp, buf);
20961
20962 return res != NULL ? res : (char_u *)"";
20963}
20964
Bram Moolenaar7d647822014-04-05 21:28:56 +020020965/*
20966 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20967 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020968 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020969get_tv_string_chk(varp)
20970 typval_T *varp;
20971{
20972 static char_u mybuf[NUMBUFLEN];
20973
20974 return get_tv_string_buf_chk(varp, mybuf);
20975}
20976
20977 static char_u *
20978get_tv_string_buf_chk(varp, buf)
20979 typval_T *varp;
20980 char_u *buf;
20981{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020982 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020984 case VAR_NUMBER:
20985 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20986 return buf;
20987 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020988 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020989 break;
20990 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020991 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020992 break;
20993 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020994 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020995 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020996#ifdef FEAT_FLOAT
20997 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020998 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020999 break;
21000#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021001 case VAR_STRING:
21002 if (varp->vval.v_string != NULL)
21003 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021004 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021005 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021006 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021007 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021009 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010}
21011
21012/*
21013 * Find variable "name" in the list of variables.
21014 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021015 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021016 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021017 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021019 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021020find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021022 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021023 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021026 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027
Bram Moolenaara7043832005-01-21 11:56:39 +000021028 ht = find_var_ht(name, &varname);
21029 if (htp != NULL)
21030 *htp = ht;
21031 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021033 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034}
21035
21036/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021037 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021038 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021040 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021041find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021042 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021043 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021044 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021045 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021046{
Bram Moolenaar33570922005-01-25 22:26:29 +000021047 hashitem_T *hi;
21048
21049 if (*varname == NUL)
21050 {
21051 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021052 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021053 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021054 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021055 case 'g': return &globvars_var;
21056 case 'v': return &vimvars_var;
21057 case 'b': return &curbuf->b_bufvar;
21058 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021059#ifdef FEAT_WINDOWS
21060 case 't': return &curtab->tp_winvar;
21061#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021062 case 'l': return current_funccal == NULL
21063 ? NULL : &current_funccal->l_vars_var;
21064 case 'a': return current_funccal == NULL
21065 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021066 }
21067 return NULL;
21068 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021069
21070 hi = hash_find(ht, varname);
21071 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021072 {
21073 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021074 * worked find the variable again. Don't auto-load a script if it was
21075 * loaded already, otherwise it would be loaded every time when
21076 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021077 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021078 {
21079 /* Note: script_autoload() may make "hi" invalid. It must either
21080 * be obtained again or not used. */
21081 if (!script_autoload(varname, FALSE) || aborting())
21082 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021083 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021084 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021085 if (HASHITEM_EMPTY(hi))
21086 return NULL;
21087 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021088 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021089}
21090
21091/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021092 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021093 * Set "varname" to the start of name without ':'.
21094 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021095 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021096find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 char_u *name;
21098 char_u **varname;
21099{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021100 hashitem_T *hi;
21101
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 if (name[1] != ':')
21103 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021104 /* The name must not start with a colon or #. */
21105 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 return NULL;
21107 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021108
21109 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021110 hi = hash_find(&compat_hashtab, name);
21111 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021112 return &compat_hashtab;
21113
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021115 return &globvarht; /* global variable */
21116 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 }
21118 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021119 if (*name == 'g') /* global variable */
21120 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021121 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21122 */
21123 if (vim_strchr(name + 2, ':') != NULL
21124 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021125 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021127 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021129 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021130#ifdef FEAT_WINDOWS
21131 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021132 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021133#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021134 if (*name == 'v') /* v: variable */
21135 return &vimvarht;
21136 if (*name == 'a' && current_funccal != NULL) /* function argument */
21137 return &current_funccal->l_avars.dv_hashtab;
21138 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21139 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 if (*name == 's' /* script variable */
21141 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21142 return &SCRIPT_VARS(current_SID);
21143 return NULL;
21144}
21145
21146/*
21147 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021148 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 * Returns NULL when it doesn't exist.
21150 */
21151 char_u *
21152get_var_value(name)
21153 char_u *name;
21154{
Bram Moolenaar33570922005-01-25 22:26:29 +000021155 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021157 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 if (v == NULL)
21159 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021160 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161}
21162
21163/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021164 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 * sourcing this script and when executing functions defined in the script.
21166 */
21167 void
21168new_script_vars(id)
21169 scid_T id;
21170{
Bram Moolenaara7043832005-01-21 11:56:39 +000021171 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021172 hashtab_T *ht;
21173 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021174
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21176 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021177 /* Re-allocating ga_data means that an ht_array pointing to
21178 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021179 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021180 for (i = 1; i <= ga_scripts.ga_len; ++i)
21181 {
21182 ht = &SCRIPT_VARS(i);
21183 if (ht->ht_mask == HT_INIT_SIZE - 1)
21184 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021185 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021186 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021187 }
21188
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 while (ga_scripts.ga_len < id)
21190 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021191 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021192 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021193 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 }
21196 }
21197}
21198
21199/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021200 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21201 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 */
21203 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021204init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021205 dict_T *dict;
21206 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021207 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208{
Bram Moolenaar33570922005-01-25 22:26:29 +000021209 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021210 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021211 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021212 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021213 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021214 dict_var->di_tv.vval.v_dict = dict;
21215 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021216 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021217 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21218 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219}
21220
21221/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021222 * Unreference a dictionary initialized by init_var_dict().
21223 */
21224 void
21225unref_var_dict(dict)
21226 dict_T *dict;
21227{
21228 /* Now the dict needs to be freed if no one else is using it, go back to
21229 * normal reference counting. */
21230 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21231 dict_unref(dict);
21232}
21233
21234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021236 * Frees all allocated variables and the value they contain.
21237 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238 */
21239 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021240vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021241 hashtab_T *ht;
21242{
21243 vars_clear_ext(ht, TRUE);
21244}
21245
21246/*
21247 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21248 */
21249 static void
21250vars_clear_ext(ht, free_val)
21251 hashtab_T *ht;
21252 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253{
Bram Moolenaara7043832005-01-21 11:56:39 +000021254 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021255 hashitem_T *hi;
21256 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257
Bram Moolenaar33570922005-01-25 22:26:29 +000021258 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021259 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021260 for (hi = ht->ht_array; todo > 0; ++hi)
21261 {
21262 if (!HASHITEM_EMPTY(hi))
21263 {
21264 --todo;
21265
Bram Moolenaar33570922005-01-25 22:26:29 +000021266 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021267 * ht_array might change then. hash_clear() takes care of it
21268 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021269 v = HI2DI(hi);
21270 if (free_val)
21271 clear_tv(&v->di_tv);
21272 if ((v->di_flags & DI_FLAGS_FIX) == 0)
21273 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021274 }
21275 }
21276 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021277 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278}
21279
Bram Moolenaara7043832005-01-21 11:56:39 +000021280/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021281 * Delete a variable from hashtab "ht" at item "hi".
21282 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021285delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021286 hashtab_T *ht;
21287 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288{
Bram Moolenaar33570922005-01-25 22:26:29 +000021289 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021290
21291 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021292 clear_tv(&di->di_tv);
21293 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294}
21295
21296/*
21297 * List the value of one internal variable.
21298 */
21299 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021300list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021301 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021303 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021305 char_u *tofree;
21306 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021307 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021308
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021309 current_copyID += COPYID_INC;
21310 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021311 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021312 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021313 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314}
21315
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021317list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 char_u *prefix;
21319 char_u *name;
21320 int type;
21321 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021322 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323{
Bram Moolenaar31859182007-08-14 20:41:13 +000021324 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21325 msg_start();
21326 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 if (name != NULL) /* "a:" vars don't have a name stored */
21328 msg_puts(name);
21329 msg_putchar(' ');
21330 msg_advance(22);
21331 if (type == VAR_NUMBER)
21332 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021333 else if (type == VAR_FUNC)
21334 msg_putchar('*');
21335 else if (type == VAR_LIST)
21336 {
21337 msg_putchar('[');
21338 if (*string == '[')
21339 ++string;
21340 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021341 else if (type == VAR_DICT)
21342 {
21343 msg_putchar('{');
21344 if (*string == '{')
21345 ++string;
21346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 else
21348 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021349
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021351
21352 if (type == VAR_FUNC)
21353 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021354 if (*first)
21355 {
21356 msg_clr_eos();
21357 *first = FALSE;
21358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359}
21360
21361/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021362 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 * If the variable already exists, the value is updated.
21364 * Otherwise the variable is created.
21365 */
21366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021367set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021369 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021370 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371{
Bram Moolenaar33570922005-01-25 22:26:29 +000021372 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021374 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021376 ht = find_var_ht(name, &varname);
21377 if (ht == NULL || *varname == NUL)
21378 {
21379 EMSG2(_(e_illvar), name);
21380 return;
21381 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021382 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021383
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021384 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21385 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021386
Bram Moolenaar33570922005-01-25 22:26:29 +000021387 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021389 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021390 if (var_check_ro(v->di_flags, name)
21391 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021392 return;
21393 if (v->di_tv.v_type != tv->v_type
21394 && !((v->di_tv.v_type == VAR_STRING
21395 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021396 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021397 || tv->v_type == VAR_NUMBER))
21398#ifdef FEAT_FLOAT
21399 && !((v->di_tv.v_type == VAR_NUMBER
21400 || v->di_tv.v_type == VAR_FLOAT)
21401 && (tv->v_type == VAR_NUMBER
21402 || tv->v_type == VAR_FLOAT))
21403#endif
21404 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021405 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021406 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021407 return;
21408 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021409
21410 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021411 * Handle setting internal v: variables separately: we don't change
21412 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021413 */
21414 if (ht == &vimvarht)
21415 {
21416 if (v->di_tv.v_type == VAR_STRING)
21417 {
21418 vim_free(v->di_tv.vval.v_string);
21419 if (copy || tv->v_type != VAR_STRING)
21420 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21421 else
21422 {
21423 /* Take over the string to avoid an extra alloc/free. */
21424 v->di_tv.vval.v_string = tv->vval.v_string;
21425 tv->vval.v_string = NULL;
21426 }
21427 }
21428 else if (v->di_tv.v_type != VAR_NUMBER)
21429 EMSG2(_(e_intern2), "set_var()");
21430 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021431 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021432 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021433 if (STRCMP(varname, "searchforward") == 0)
21434 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021435#ifdef FEAT_SEARCH_EXTRA
21436 else if (STRCMP(varname, "hlsearch") == 0)
21437 {
21438 no_hlsearch = !v->di_tv.vval.v_number;
21439 redraw_all_later(SOME_VALID);
21440 }
21441#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021442 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021443 return;
21444 }
21445
21446 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 }
21448 else /* add a new variable */
21449 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021450 /* Can't add "v:" variable. */
21451 if (ht == &vimvarht)
21452 {
21453 EMSG2(_(e_illvar), name);
21454 return;
21455 }
21456
Bram Moolenaar92124a32005-06-17 22:03:40 +000021457 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021458 if (!valid_varname(varname))
21459 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021460
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021461 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21462 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021463 if (v == NULL)
21464 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021465 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021466 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021468 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 return;
21470 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021471 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021473
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021474 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021475 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021476 else
21477 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021478 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021479 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021480 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482}
21483
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021484/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021485 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021486 * Also give an error message.
21487 */
21488 static int
21489var_check_ro(flags, name)
21490 int flags;
21491 char_u *name;
21492{
21493 if (flags & DI_FLAGS_RO)
21494 {
21495 EMSG2(_(e_readonlyvar), name);
21496 return TRUE;
21497 }
21498 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21499 {
21500 EMSG2(_(e_readonlysbx), name);
21501 return TRUE;
21502 }
21503 return FALSE;
21504}
21505
21506/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021507 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21508 * Also give an error message.
21509 */
21510 static int
21511var_check_fixed(flags, name)
21512 int flags;
21513 char_u *name;
21514{
21515 if (flags & DI_FLAGS_FIX)
21516 {
21517 EMSG2(_("E795: Cannot delete variable %s"), name);
21518 return TRUE;
21519 }
21520 return FALSE;
21521}
21522
21523/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021524 * Check if a funcref is assigned to a valid variable name.
21525 * Return TRUE and give an error if not.
21526 */
21527 static int
21528var_check_func_name(name, new_var)
21529 char_u *name; /* points to start of variable name */
21530 int new_var; /* TRUE when creating the variable */
21531{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021532 /* Allow for w: b: s: and t:. */
21533 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021534 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21535 ? name[2] : name[0]))
21536 {
21537 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21538 name);
21539 return TRUE;
21540 }
21541 /* Don't allow hiding a function. When "v" is not NULL we might be
21542 * assigning another function to the same var, the type is checked
21543 * below. */
21544 if (new_var && function_exists(name))
21545 {
21546 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21547 name);
21548 return TRUE;
21549 }
21550 return FALSE;
21551}
21552
21553/*
21554 * Check if a variable name is valid.
21555 * Return FALSE and give an error if not.
21556 */
21557 static int
21558valid_varname(varname)
21559 char_u *varname;
21560{
21561 char_u *p;
21562
21563 for (p = varname; *p != NUL; ++p)
21564 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21565 && *p != AUTOLOAD_CHAR)
21566 {
21567 EMSG2(_(e_illvar), varname);
21568 return FALSE;
21569 }
21570 return TRUE;
21571}
21572
21573/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021574 * Return TRUE if typeval "tv" is set to be locked (immutable).
21575 * Also give an error message, using "name".
21576 */
21577 static int
21578tv_check_lock(lock, name)
21579 int lock;
21580 char_u *name;
21581{
21582 if (lock & VAR_LOCKED)
21583 {
21584 EMSG2(_("E741: Value is locked: %s"),
21585 name == NULL ? (char_u *)_("Unknown") : name);
21586 return TRUE;
21587 }
21588 if (lock & VAR_FIXED)
21589 {
21590 EMSG2(_("E742: Cannot change value of %s"),
21591 name == NULL ? (char_u *)_("Unknown") : name);
21592 return TRUE;
21593 }
21594 return FALSE;
21595}
21596
21597/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021598 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021599 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021600 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021601 * It is OK for "from" and "to" to point to the same item. This is used to
21602 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021603 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021604 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021605copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021606 typval_T *from;
21607 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021609 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021610 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021611 switch (from->v_type)
21612 {
21613 case VAR_NUMBER:
21614 to->vval.v_number = from->vval.v_number;
21615 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021616#ifdef FEAT_FLOAT
21617 case VAR_FLOAT:
21618 to->vval.v_float = from->vval.v_float;
21619 break;
21620#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021621 case VAR_STRING:
21622 case VAR_FUNC:
21623 if (from->vval.v_string == NULL)
21624 to->vval.v_string = NULL;
21625 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021626 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021627 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021628 if (from->v_type == VAR_FUNC)
21629 func_ref(to->vval.v_string);
21630 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021631 break;
21632 case VAR_LIST:
21633 if (from->vval.v_list == NULL)
21634 to->vval.v_list = NULL;
21635 else
21636 {
21637 to->vval.v_list = from->vval.v_list;
21638 ++to->vval.v_list->lv_refcount;
21639 }
21640 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021641 case VAR_DICT:
21642 if (from->vval.v_dict == NULL)
21643 to->vval.v_dict = NULL;
21644 else
21645 {
21646 to->vval.v_dict = from->vval.v_dict;
21647 ++to->vval.v_dict->dv_refcount;
21648 }
21649 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021650 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021651 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021652 break;
21653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654}
21655
21656/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021657 * Make a copy of an item.
21658 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021659 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21660 * reference to an already copied list/dict can be used.
21661 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021662 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021663 static int
21664item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021665 typval_T *from;
21666 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021667 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021668 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021669{
21670 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021671 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021672
Bram Moolenaar33570922005-01-25 22:26:29 +000021673 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021674 {
21675 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021676 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021677 }
21678 ++recurse;
21679
21680 switch (from->v_type)
21681 {
21682 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021683#ifdef FEAT_FLOAT
21684 case VAR_FLOAT:
21685#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021686 case VAR_STRING:
21687 case VAR_FUNC:
21688 copy_tv(from, to);
21689 break;
21690 case VAR_LIST:
21691 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021692 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021693 if (from->vval.v_list == NULL)
21694 to->vval.v_list = NULL;
21695 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21696 {
21697 /* use the copy made earlier */
21698 to->vval.v_list = from->vval.v_list->lv_copylist;
21699 ++to->vval.v_list->lv_refcount;
21700 }
21701 else
21702 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21703 if (to->vval.v_list == NULL)
21704 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021705 break;
21706 case VAR_DICT:
21707 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021708 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021709 if (from->vval.v_dict == NULL)
21710 to->vval.v_dict = NULL;
21711 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21712 {
21713 /* use the copy made earlier */
21714 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21715 ++to->vval.v_dict->dv_refcount;
21716 }
21717 else
21718 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21719 if (to->vval.v_dict == NULL)
21720 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021721 break;
21722 default:
21723 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021724 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021725 }
21726 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021727 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021728}
21729
21730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731 * ":echo expr1 ..." print each argument separated with a space, add a
21732 * newline at the end.
21733 * ":echon expr1 ..." print each argument plain.
21734 */
21735 void
21736ex_echo(eap)
21737 exarg_T *eap;
21738{
21739 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021740 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021741 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 char_u *p;
21743 int needclr = TRUE;
21744 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021745 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746
21747 if (eap->skip)
21748 ++emsg_skip;
21749 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21750 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021751 /* If eval1() causes an error message the text from the command may
21752 * still need to be cleared. E.g., "echo 22,44". */
21753 need_clr_eos = needclr;
21754
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021756 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 {
21758 /*
21759 * Report the invalid expression unless the expression evaluation
21760 * has been cancelled due to an aborting error, an interrupt, or an
21761 * exception.
21762 */
21763 if (!aborting())
21764 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021765 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766 break;
21767 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021768 need_clr_eos = FALSE;
21769
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770 if (!eap->skip)
21771 {
21772 if (atstart)
21773 {
21774 atstart = FALSE;
21775 /* Call msg_start() after eval1(), evaluating the expression
21776 * may cause a message to appear. */
21777 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021778 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021779 /* Mark the saved text as finishing the line, so that what
21780 * follows is displayed on a new line when scrolling back
21781 * at the more prompt. */
21782 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 }
21786 else if (eap->cmdidx == CMD_echo)
21787 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021788 current_copyID += COPYID_INC;
21789 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021790 if (p != NULL)
21791 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021793 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021795 if (*p != TAB && needclr)
21796 {
21797 /* remove any text still there from the command */
21798 msg_clr_eos();
21799 needclr = FALSE;
21800 }
21801 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 }
21803 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021804 {
21805#ifdef FEAT_MBYTE
21806 if (has_mbyte)
21807 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021808 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021809
21810 (void)msg_outtrans_len_attr(p, i, echo_attr);
21811 p += i - 1;
21812 }
21813 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021815 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021818 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021820 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021821 arg = skipwhite(arg);
21822 }
21823 eap->nextcmd = check_nextcmd(arg);
21824
21825 if (eap->skip)
21826 --emsg_skip;
21827 else
21828 {
21829 /* remove text that may still be there from the command */
21830 if (needclr)
21831 msg_clr_eos();
21832 if (eap->cmdidx == CMD_echo)
21833 msg_end();
21834 }
21835}
21836
21837/*
21838 * ":echohl {name}".
21839 */
21840 void
21841ex_echohl(eap)
21842 exarg_T *eap;
21843{
21844 int id;
21845
21846 id = syn_name2id(eap->arg);
21847 if (id == 0)
21848 echo_attr = 0;
21849 else
21850 echo_attr = syn_id2attr(id);
21851}
21852
21853/*
21854 * ":execute expr1 ..." execute the result of an expression.
21855 * ":echomsg expr1 ..." Print a message
21856 * ":echoerr expr1 ..." Print an error
21857 * Each gets spaces around each argument and a newline at the end for
21858 * echo commands
21859 */
21860 void
21861ex_execute(eap)
21862 exarg_T *eap;
21863{
21864 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021865 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866 int ret = OK;
21867 char_u *p;
21868 garray_T ga;
21869 int len;
21870 int save_did_emsg;
21871
21872 ga_init2(&ga, 1, 80);
21873
21874 if (eap->skip)
21875 ++emsg_skip;
21876 while (*arg != NUL && *arg != '|' && *arg != '\n')
21877 {
21878 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021879 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 {
21881 /*
21882 * Report the invalid expression unless the expression evaluation
21883 * has been cancelled due to an aborting error, an interrupt, or an
21884 * exception.
21885 */
21886 if (!aborting())
21887 EMSG2(_(e_invexpr2), p);
21888 ret = FAIL;
21889 break;
21890 }
21891
21892 if (!eap->skip)
21893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021894 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895 len = (int)STRLEN(p);
21896 if (ga_grow(&ga, len + 2) == FAIL)
21897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021898 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 ret = FAIL;
21900 break;
21901 }
21902 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 ga.ga_len += len;
21906 }
21907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 arg = skipwhite(arg);
21910 }
21911
21912 if (ret != FAIL && ga.ga_data != NULL)
21913 {
21914 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021917 out_flush();
21918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 else if (eap->cmdidx == CMD_echoerr)
21920 {
21921 /* We don't want to abort following commands, restore did_emsg. */
21922 save_did_emsg = did_emsg;
21923 EMSG((char_u *)ga.ga_data);
21924 if (!force_abort)
21925 did_emsg = save_did_emsg;
21926 }
21927 else if (eap->cmdidx == CMD_execute)
21928 do_cmdline((char_u *)ga.ga_data,
21929 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21930 }
21931
21932 ga_clear(&ga);
21933
21934 if (eap->skip)
21935 --emsg_skip;
21936
21937 eap->nextcmd = check_nextcmd(arg);
21938}
21939
21940/*
21941 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21942 * "arg" points to the "&" or '+' when called, to "option" when returning.
21943 * Returns NULL when no option name found. Otherwise pointer to the char
21944 * after the option name.
21945 */
21946 static char_u *
21947find_option_end(arg, opt_flags)
21948 char_u **arg;
21949 int *opt_flags;
21950{
21951 char_u *p = *arg;
21952
21953 ++p;
21954 if (*p == 'g' && p[1] == ':')
21955 {
21956 *opt_flags = OPT_GLOBAL;
21957 p += 2;
21958 }
21959 else if (*p == 'l' && p[1] == ':')
21960 {
21961 *opt_flags = OPT_LOCAL;
21962 p += 2;
21963 }
21964 else
21965 *opt_flags = 0;
21966
21967 if (!ASCII_ISALPHA(*p))
21968 return NULL;
21969 *arg = p;
21970
21971 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21972 p += 4; /* termcap option */
21973 else
21974 while (ASCII_ISALPHA(*p))
21975 ++p;
21976 return p;
21977}
21978
21979/*
21980 * ":function"
21981 */
21982 void
21983ex_function(eap)
21984 exarg_T *eap;
21985{
21986 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021987 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 int j;
21989 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021991 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 char_u *name = NULL;
21993 char_u *p;
21994 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021995 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 garray_T newargs;
21997 garray_T newlines;
21998 int varargs = FALSE;
21999 int mustend = FALSE;
22000 int flags = 0;
22001 ufunc_T *fp;
22002 int indent;
22003 int nesting;
22004 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 dictitem_T *v;
22006 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022007 static int func_nr = 0; /* number for nameless function */
22008 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022009 hashtab_T *ht;
22010 int todo;
22011 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022012 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013
22014 /*
22015 * ":function" without argument: list functions.
22016 */
22017 if (ends_excmd(*eap->arg))
22018 {
22019 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022020 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022021 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022022 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022023 {
22024 if (!HASHITEM_EMPTY(hi))
22025 {
22026 --todo;
22027 fp = HI2UF(hi);
22028 if (!isdigit(*fp->uf_name))
22029 list_func_head(fp, FALSE);
22030 }
22031 }
22032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 eap->nextcmd = check_nextcmd(eap->arg);
22034 return;
22035 }
22036
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022037 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022038 * ":function /pat": list functions matching pattern.
22039 */
22040 if (*eap->arg == '/')
22041 {
22042 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22043 if (!eap->skip)
22044 {
22045 regmatch_T regmatch;
22046
22047 c = *p;
22048 *p = NUL;
22049 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22050 *p = c;
22051 if (regmatch.regprog != NULL)
22052 {
22053 regmatch.rm_ic = p_ic;
22054
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022055 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022056 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22057 {
22058 if (!HASHITEM_EMPTY(hi))
22059 {
22060 --todo;
22061 fp = HI2UF(hi);
22062 if (!isdigit(*fp->uf_name)
22063 && vim_regexec(&regmatch, fp->uf_name, 0))
22064 list_func_head(fp, FALSE);
22065 }
22066 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022067 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022068 }
22069 }
22070 if (*p == '/')
22071 ++p;
22072 eap->nextcmd = check_nextcmd(p);
22073 return;
22074 }
22075
22076 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022077 * Get the function name. There are these situations:
22078 * func normal function name
22079 * "name" == func, "fudi.fd_dict" == NULL
22080 * dict.func new dictionary entry
22081 * "name" == NULL, "fudi.fd_dict" set,
22082 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22083 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022084 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022085 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22086 * dict.func existing dict entry that's not a Funcref
22087 * "name" == NULL, "fudi.fd_dict" set,
22088 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022089 * s:func script-local function name
22090 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022093 name = trans_function_name(&p, eap->skip, 0, &fudi);
22094 paren = (vim_strchr(p, '(') != NULL);
22095 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 {
22097 /*
22098 * Return on an invalid expression in braces, unless the expression
22099 * evaluation has been cancelled due to an aborting error, an
22100 * interrupt, or an exception.
22101 */
22102 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022103 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022104 if (!eap->skip && fudi.fd_newkey != NULL)
22105 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022106 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109 else
22110 eap->skip = TRUE;
22111 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022112
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113 /* An error in a function call during evaluation of an expression in magic
22114 * braces should not cause the function not to be defined. */
22115 saved_did_emsg = did_emsg;
22116 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117
22118 /*
22119 * ":function func" with only function name: list function.
22120 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022121 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122 {
22123 if (!ends_excmd(*skipwhite(p)))
22124 {
22125 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022126 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 }
22128 eap->nextcmd = check_nextcmd(p);
22129 if (eap->nextcmd != NULL)
22130 *p = NUL;
22131 if (!eap->skip && !got_int)
22132 {
22133 fp = find_func(name);
22134 if (fp != NULL)
22135 {
22136 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022137 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022139 if (FUNCLINE(fp, j) == NULL)
22140 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 msg_putchar('\n');
22142 msg_outnum((long)(j + 1));
22143 if (j < 9)
22144 msg_putchar(' ');
22145 if (j < 99)
22146 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022147 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148 out_flush(); /* show a line at a time */
22149 ui_breakcheck();
22150 }
22151 if (!got_int)
22152 {
22153 msg_putchar('\n');
22154 msg_puts((char_u *)" endfunction");
22155 }
22156 }
22157 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022158 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022160 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 }
22162
22163 /*
22164 * ":function name(arg1, arg2)" Define function.
22165 */
22166 p = skipwhite(p);
22167 if (*p != '(')
22168 {
22169 if (!eap->skip)
22170 {
22171 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022172 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 }
22174 /* attempt to continue by skipping some text */
22175 if (vim_strchr(p, '(') != NULL)
22176 p = vim_strchr(p, '(');
22177 }
22178 p = skipwhite(p + 1);
22179
22180 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22181 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22182
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022183 if (!eap->skip)
22184 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022185 /* Check the name of the function. Unless it's a dictionary function
22186 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022187 if (name != NULL)
22188 arg = name;
22189 else
22190 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022191 if (arg != NULL && (fudi.fd_di == NULL
22192 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022193 {
22194 if (*arg == K_SPECIAL)
22195 j = 3;
22196 else
22197 j = 0;
22198 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22199 : eval_isnamec(arg[j])))
22200 ++j;
22201 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022202 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022203 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022204 /* Disallow using the g: dict. */
22205 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22206 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022207 }
22208
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 /*
22210 * Isolate the arguments: "arg1, arg2, ...)"
22211 */
22212 while (*p != ')')
22213 {
22214 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22215 {
22216 varargs = TRUE;
22217 p += 3;
22218 mustend = TRUE;
22219 }
22220 else
22221 {
22222 arg = p;
22223 while (ASCII_ISALNUM(*p) || *p == '_')
22224 ++p;
22225 if (arg == p || isdigit(*arg)
22226 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22227 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22228 {
22229 if (!eap->skip)
22230 EMSG2(_("E125: Illegal argument: %s"), arg);
22231 break;
22232 }
22233 if (ga_grow(&newargs, 1) == FAIL)
22234 goto erret;
22235 c = *p;
22236 *p = NUL;
22237 arg = vim_strsave(arg);
22238 if (arg == NULL)
22239 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022240
22241 /* Check for duplicate argument name. */
22242 for (i = 0; i < newargs.ga_len; ++i)
22243 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22244 {
22245 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022246 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022247 goto erret;
22248 }
22249
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22251 *p = c;
22252 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 if (*p == ',')
22254 ++p;
22255 else
22256 mustend = TRUE;
22257 }
22258 p = skipwhite(p);
22259 if (mustend && *p != ')')
22260 {
22261 if (!eap->skip)
22262 EMSG2(_(e_invarg2), eap->arg);
22263 break;
22264 }
22265 }
22266 ++p; /* skip the ')' */
22267
Bram Moolenaare9a41262005-01-15 22:18:47 +000022268 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 for (;;)
22270 {
22271 p = skipwhite(p);
22272 if (STRNCMP(p, "range", 5) == 0)
22273 {
22274 flags |= FC_RANGE;
22275 p += 5;
22276 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022277 else if (STRNCMP(p, "dict", 4) == 0)
22278 {
22279 flags |= FC_DICT;
22280 p += 4;
22281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 else if (STRNCMP(p, "abort", 5) == 0)
22283 {
22284 flags |= FC_ABORT;
22285 p += 5;
22286 }
22287 else
22288 break;
22289 }
22290
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022291 /* When there is a line break use what follows for the function body.
22292 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22293 if (*p == '\n')
22294 line_arg = p + 1;
22295 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 EMSG(_(e_trailing));
22297
22298 /*
22299 * Read the body of the function, until ":endfunction" is found.
22300 */
22301 if (KeyTyped)
22302 {
22303 /* Check if the function already exists, don't let the user type the
22304 * whole function before telling him it doesn't work! For a script we
22305 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022306 if (!eap->skip && !eap->forceit)
22307 {
22308 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22309 EMSG(_(e_funcdict));
22310 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022311 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022314 if (!eap->skip && did_emsg)
22315 goto erret;
22316
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 msg_putchar('\n'); /* don't overwrite the function name */
22318 cmdline_row = msg_row;
22319 }
22320
22321 indent = 2;
22322 nesting = 0;
22323 for (;;)
22324 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022325 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022326 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022327 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022328 saved_wait_return = FALSE;
22329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022331 sourcing_lnum_off = sourcing_lnum;
22332
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022333 if (line_arg != NULL)
22334 {
22335 /* Use eap->arg, split up in parts by line breaks. */
22336 theline = line_arg;
22337 p = vim_strchr(theline, '\n');
22338 if (p == NULL)
22339 line_arg += STRLEN(line_arg);
22340 else
22341 {
22342 *p = NUL;
22343 line_arg = p + 1;
22344 }
22345 }
22346 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347 theline = getcmdline(':', 0L, indent);
22348 else
22349 theline = eap->getline(':', eap->cookie, indent);
22350 if (KeyTyped)
22351 lines_left = Rows - 1;
22352 if (theline == NULL)
22353 {
22354 EMSG(_("E126: Missing :endfunction"));
22355 goto erret;
22356 }
22357
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022358 /* Detect line continuation: sourcing_lnum increased more than one. */
22359 if (sourcing_lnum > sourcing_lnum_off + 1)
22360 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22361 else
22362 sourcing_lnum_off = 0;
22363
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 if (skip_until != NULL)
22365 {
22366 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22367 * don't check for ":endfunc". */
22368 if (STRCMP(theline, skip_until) == 0)
22369 {
22370 vim_free(skip_until);
22371 skip_until = NULL;
22372 }
22373 }
22374 else
22375 {
22376 /* skip ':' and blanks*/
22377 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22378 ;
22379
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022380 /* Check for "endfunction". */
22381 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022383 if (line_arg == NULL)
22384 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 break;
22386 }
22387
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022388 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 * at "end". */
22390 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22391 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022392 else if (STRNCMP(p, "if", 2) == 0
22393 || STRNCMP(p, "wh", 2) == 0
22394 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 || STRNCMP(p, "try", 3) == 0)
22396 indent += 2;
22397
22398 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022399 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022401 if (*p == '!')
22402 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022404 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22405 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022407 ++nesting;
22408 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 }
22410 }
22411
22412 /* Check for ":append" or ":insert". */
22413 p = skip_range(p, NULL);
22414 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22415 || (p[0] == 'i'
22416 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22417 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22418 skip_until = vim_strsave((char_u *)".");
22419
22420 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22421 arg = skipwhite(skiptowhite(p));
22422 if (arg[0] == '<' && arg[1] =='<'
22423 && ((p[0] == 'p' && p[1] == 'y'
22424 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22425 || (p[0] == 'p' && p[1] == 'e'
22426 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22427 || (p[0] == 't' && p[1] == 'c'
22428 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022429 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22430 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22432 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022433 || (p[0] == 'm' && p[1] == 'z'
22434 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435 ))
22436 {
22437 /* ":python <<" continues until a dot, like ":append" */
22438 p = skipwhite(arg + 2);
22439 if (*p == NUL)
22440 skip_until = vim_strsave((char_u *)".");
22441 else
22442 skip_until = vim_strsave(p);
22443 }
22444 }
22445
22446 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022447 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022448 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022449 if (line_arg == NULL)
22450 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022452 }
22453
22454 /* Copy the line to newly allocated memory. get_one_sourceline()
22455 * allocates 250 bytes per line, this saves 80% on average. The cost
22456 * is an extra alloc/free. */
22457 p = vim_strsave(theline);
22458 if (p != NULL)
22459 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022460 if (line_arg == NULL)
22461 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022462 theline = p;
22463 }
22464
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022465 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22466
22467 /* Add NULL lines for continuation lines, so that the line count is
22468 * equal to the index in the growarray. */
22469 while (sourcing_lnum_off-- > 0)
22470 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022471
22472 /* Check for end of eap->arg. */
22473 if (line_arg != NULL && *line_arg == NUL)
22474 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 }
22476
22477 /* Don't define the function when skipping commands or when an error was
22478 * detected. */
22479 if (eap->skip || did_emsg)
22480 goto erret;
22481
22482 /*
22483 * If there are no errors, add the function
22484 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022485 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022486 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022487 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022488 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022489 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022490 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022491 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022492 goto erret;
22493 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022494
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022495 fp = find_func(name);
22496 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022498 if (!eap->forceit)
22499 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022500 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022501 goto erret;
22502 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022503 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022504 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022505 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022506 name);
22507 goto erret;
22508 }
22509 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022510 ga_clear_strings(&(fp->uf_args));
22511 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022512 vim_free(name);
22513 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515 }
22516 else
22517 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022518 char numbuf[20];
22519
22520 fp = NULL;
22521 if (fudi.fd_newkey == NULL && !eap->forceit)
22522 {
22523 EMSG(_(e_funcdict));
22524 goto erret;
22525 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022526 if (fudi.fd_di == NULL)
22527 {
22528 /* Can't add a function to a locked dictionary */
22529 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22530 goto erret;
22531 }
22532 /* Can't change an existing function if it is locked */
22533 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22534 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022535
22536 /* Give the function a sequential number. Can only be used with a
22537 * Funcref! */
22538 vim_free(name);
22539 sprintf(numbuf, "%d", ++func_nr);
22540 name = vim_strsave((char_u *)numbuf);
22541 if (name == NULL)
22542 goto erret;
22543 }
22544
22545 if (fp == NULL)
22546 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022547 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022548 {
22549 int slen, plen;
22550 char_u *scriptname;
22551
22552 /* Check that the autoload name matches the script name. */
22553 j = FAIL;
22554 if (sourcing_name != NULL)
22555 {
22556 scriptname = autoload_name(name);
22557 if (scriptname != NULL)
22558 {
22559 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022560 plen = (int)STRLEN(p);
22561 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022562 if (slen > plen && fnamecmp(p,
22563 sourcing_name + slen - plen) == 0)
22564 j = OK;
22565 vim_free(scriptname);
22566 }
22567 }
22568 if (j == FAIL)
22569 {
22570 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22571 goto erret;
22572 }
22573 }
22574
22575 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 if (fp == NULL)
22577 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022578
22579 if (fudi.fd_dict != NULL)
22580 {
22581 if (fudi.fd_di == NULL)
22582 {
22583 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022584 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022585 if (fudi.fd_di == NULL)
22586 {
22587 vim_free(fp);
22588 goto erret;
22589 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022590 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22591 {
22592 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022593 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022594 goto erret;
22595 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022596 }
22597 else
22598 /* overwrite existing dict entry */
22599 clear_tv(&fudi.fd_di->di_tv);
22600 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022601 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022602 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022603 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022604
22605 /* behave like "dict" was used */
22606 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022607 }
22608
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022610 STRCPY(fp->uf_name, name);
22611 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022613 fp->uf_args = newargs;
22614 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022615#ifdef FEAT_PROFILE
22616 fp->uf_tml_count = NULL;
22617 fp->uf_tml_total = NULL;
22618 fp->uf_tml_self = NULL;
22619 fp->uf_profiling = FALSE;
22620 if (prof_def_func())
22621 func_do_profile(fp);
22622#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022623 fp->uf_varargs = varargs;
22624 fp->uf_flags = flags;
22625 fp->uf_calls = 0;
22626 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022627 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628
22629erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 ga_clear_strings(&newargs);
22631 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022632ret_free:
22633 vim_free(skip_until);
22634 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022637 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638}
22639
22640/*
22641 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022642 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022644 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022645 * TFN_INT: internal function name OK
22646 * TFN_QUIET: be quiet
22647 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022648 * Advances "pp" to just after the function name (if no error).
22649 */
22650 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022651trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 char_u **pp;
22653 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022654 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022655 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022657 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 char_u *start;
22659 char_u *end;
22660 int lead;
22661 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022663 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022664
22665 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022666 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022668
22669 /* Check for hard coded <SNR>: already translated function ID (from a user
22670 * command). */
22671 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22672 && (*pp)[2] == (int)KE_SNR)
22673 {
22674 *pp += 3;
22675 len = get_id_len(pp) + 3;
22676 return vim_strnsave(start, len);
22677 }
22678
22679 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22680 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022682 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022684
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022685 /* Note that TFN_ flags use the same values as GLV_ flags. */
22686 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022687 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022688 if (end == start)
22689 {
22690 if (!skip)
22691 EMSG(_("E129: Function name required"));
22692 goto theend;
22693 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022694 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022695 {
22696 /*
22697 * Report an invalid expression in braces, unless the expression
22698 * evaluation has been cancelled due to an aborting error, an
22699 * interrupt, or an exception.
22700 */
22701 if (!aborting())
22702 {
22703 if (end != NULL)
22704 EMSG2(_(e_invarg2), start);
22705 }
22706 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022707 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022708 goto theend;
22709 }
22710
22711 if (lv.ll_tv != NULL)
22712 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022713 if (fdp != NULL)
22714 {
22715 fdp->fd_dict = lv.ll_dict;
22716 fdp->fd_newkey = lv.ll_newkey;
22717 lv.ll_newkey = NULL;
22718 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022719 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022720 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22721 {
22722 name = vim_strsave(lv.ll_tv->vval.v_string);
22723 *pp = end;
22724 }
22725 else
22726 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022727 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22728 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022729 EMSG(_(e_funcref));
22730 else
22731 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022732 name = NULL;
22733 }
22734 goto theend;
22735 }
22736
22737 if (lv.ll_name == NULL)
22738 {
22739 /* Error found, but continue after the function name. */
22740 *pp = end;
22741 goto theend;
22742 }
22743
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022744 /* Check if the name is a Funcref. If so, use the value. */
22745 if (lv.ll_exp_name != NULL)
22746 {
22747 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022748 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022749 if (name == lv.ll_exp_name)
22750 name = NULL;
22751 }
22752 else
22753 {
22754 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022755 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022756 if (name == *pp)
22757 name = NULL;
22758 }
22759 if (name != NULL)
22760 {
22761 name = vim_strsave(name);
22762 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022763 if (STRNCMP(name, "<SNR>", 5) == 0)
22764 {
22765 /* Change "<SNR>" to the byte sequence. */
22766 name[0] = K_SPECIAL;
22767 name[1] = KS_EXTRA;
22768 name[2] = (int)KE_SNR;
22769 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22770 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022771 goto theend;
22772 }
22773
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022774 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022775 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022776 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022777 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22778 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22779 {
22780 /* When there was "s:" already or the name expanded to get a
22781 * leading "s:" then remove it. */
22782 lv.ll_name += 2;
22783 len -= 2;
22784 lead = 2;
22785 }
22786 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022787 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022788 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022789 /* skip over "s:" and "g:" */
22790 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022791 lv.ll_name += 2;
22792 len = (int)(end - lv.ll_name);
22793 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022794
22795 /*
22796 * Copy the function name to allocated memory.
22797 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22798 * Accept <SNR>123_name() outside a script.
22799 */
22800 if (skip)
22801 lead = 0; /* do nothing */
22802 else if (lead > 0)
22803 {
22804 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022805 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22806 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022807 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022808 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022809 if (current_SID <= 0)
22810 {
22811 EMSG(_(e_usingsid));
22812 goto theend;
22813 }
22814 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22815 lead += (int)STRLEN(sid_buf);
22816 }
22817 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022818 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022819 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022820 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022821 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022822 goto theend;
22823 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022824 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022825 {
22826 char_u *cp = vim_strchr(lv.ll_name, ':');
22827
22828 if (cp != NULL && cp < end)
22829 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022830 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022831 goto theend;
22832 }
22833 }
22834
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022835 name = alloc((unsigned)(len + lead + 1));
22836 if (name != NULL)
22837 {
22838 if (lead > 0)
22839 {
22840 name[0] = K_SPECIAL;
22841 name[1] = KS_EXTRA;
22842 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022843 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022844 STRCPY(name + 3, sid_buf);
22845 }
22846 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022847 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022848 }
22849 *pp = end;
22850
22851theend:
22852 clear_lval(&lv);
22853 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854}
22855
22856/*
22857 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22858 * Return 2 if "p" starts with "s:".
22859 * Return 0 otherwise.
22860 */
22861 static int
22862eval_fname_script(p)
22863 char_u *p;
22864{
22865 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22866 || STRNICMP(p + 1, "SNR>", 4) == 0))
22867 return 5;
22868 if (p[0] == 's' && p[1] == ':')
22869 return 2;
22870 return 0;
22871}
22872
22873/*
22874 * Return TRUE if "p" starts with "<SID>" or "s:".
22875 * Only works if eval_fname_script() returned non-zero for "p"!
22876 */
22877 static int
22878eval_fname_sid(p)
22879 char_u *p;
22880{
22881 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22882}
22883
22884/*
22885 * List the head of the function: "name(arg1, arg2)".
22886 */
22887 static void
22888list_func_head(fp, indent)
22889 ufunc_T *fp;
22890 int indent;
22891{
22892 int j;
22893
22894 msg_start();
22895 if (indent)
22896 MSG_PUTS(" ");
22897 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022898 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 {
22900 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022901 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 }
22903 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022904 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022906 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 {
22908 if (j)
22909 MSG_PUTS(", ");
22910 msg_puts(FUNCARG(fp, j));
22911 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022912 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 {
22914 if (j)
22915 MSG_PUTS(", ");
22916 MSG_PUTS("...");
22917 }
22918 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022919 if (fp->uf_flags & FC_ABORT)
22920 MSG_PUTS(" abort");
22921 if (fp->uf_flags & FC_RANGE)
22922 MSG_PUTS(" range");
22923 if (fp->uf_flags & FC_DICT)
22924 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022925 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022926 if (p_verbose > 0)
22927 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928}
22929
22930/*
22931 * Find a function by name, return pointer to it in ufuncs.
22932 * Return NULL for unknown function.
22933 */
22934 static ufunc_T *
22935find_func(name)
22936 char_u *name;
22937{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022938 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022940 hi = hash_find(&func_hashtab, name);
22941 if (!HASHITEM_EMPTY(hi))
22942 return HI2UF(hi);
22943 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944}
22945
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022946#if defined(EXITFREE) || defined(PROTO)
22947 void
22948free_all_functions()
22949{
22950 hashitem_T *hi;
22951
22952 /* Need to start all over every time, because func_free() may change the
22953 * hash table. */
22954 while (func_hashtab.ht_used > 0)
22955 for (hi = func_hashtab.ht_array; ; ++hi)
22956 if (!HASHITEM_EMPTY(hi))
22957 {
22958 func_free(HI2UF(hi));
22959 break;
22960 }
22961}
22962#endif
22963
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022964 int
22965translated_function_exists(name)
22966 char_u *name;
22967{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022968 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022969 return find_internal_func(name) >= 0;
22970 return find_func(name) != NULL;
22971}
22972
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022973/*
22974 * Return TRUE if a function "name" exists.
22975 */
22976 static int
22977function_exists(name)
22978 char_u *name;
22979{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022980 char_u *nm = name;
22981 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022982 int n = FALSE;
22983
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022984 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22985 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022986 nm = skipwhite(nm);
22987
22988 /* Only accept "funcname", "funcname ", "funcname (..." and
22989 * "funcname(...", not "funcname!...". */
22990 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022991 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022992 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022993 return n;
22994}
22995
Bram Moolenaara1544c02013-05-30 12:35:52 +020022996 char_u *
22997get_expanded_name(name, check)
22998 char_u *name;
22999 int check;
23000{
23001 char_u *nm = name;
23002 char_u *p;
23003
23004 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23005
23006 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023007 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023008 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023009
Bram Moolenaara1544c02013-05-30 12:35:52 +020023010 vim_free(p);
23011 return NULL;
23012}
23013
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023014/*
23015 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023016 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23017 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023018 */
23019 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023020builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023021 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023022 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023023{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023024 char_u *p;
23025
23026 if (!ASCII_ISLOWER(name[0]))
23027 return FALSE;
23028 p = vim_strchr(name, AUTOLOAD_CHAR);
23029 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023030}
23031
Bram Moolenaar05159a02005-02-26 23:04:13 +000023032#if defined(FEAT_PROFILE) || defined(PROTO)
23033/*
23034 * Start profiling function "fp".
23035 */
23036 static void
23037func_do_profile(fp)
23038 ufunc_T *fp;
23039{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023040 int len = fp->uf_lines.ga_len;
23041
23042 if (len == 0)
23043 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023044 fp->uf_tm_count = 0;
23045 profile_zero(&fp->uf_tm_self);
23046 profile_zero(&fp->uf_tm_total);
23047 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023048 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023049 if (fp->uf_tml_total == NULL)
23050 fp->uf_tml_total = (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 if (fp->uf_tml_self == NULL)
23053 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023054 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023055 fp->uf_tml_idx = -1;
23056 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23057 || fp->uf_tml_self == NULL)
23058 return; /* out of memory */
23059
23060 fp->uf_profiling = TRUE;
23061}
23062
23063/*
23064 * Dump the profiling results for all functions in file "fd".
23065 */
23066 void
23067func_dump_profile(fd)
23068 FILE *fd;
23069{
23070 hashitem_T *hi;
23071 int todo;
23072 ufunc_T *fp;
23073 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023074 ufunc_T **sorttab;
23075 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023076
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023077 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023078 if (todo == 0)
23079 return; /* nothing to dump */
23080
Bram Moolenaar73830342005-02-28 22:48:19 +000023081 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
23082
Bram Moolenaar05159a02005-02-26 23:04:13 +000023083 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23084 {
23085 if (!HASHITEM_EMPTY(hi))
23086 {
23087 --todo;
23088 fp = HI2UF(hi);
23089 if (fp->uf_profiling)
23090 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023091 if (sorttab != NULL)
23092 sorttab[st_len++] = fp;
23093
Bram Moolenaar05159a02005-02-26 23:04:13 +000023094 if (fp->uf_name[0] == K_SPECIAL)
23095 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23096 else
23097 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23098 if (fp->uf_tm_count == 1)
23099 fprintf(fd, "Called 1 time\n");
23100 else
23101 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23102 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23103 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23104 fprintf(fd, "\n");
23105 fprintf(fd, "count total (s) self (s)\n");
23106
23107 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23108 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023109 if (FUNCLINE(fp, i) == NULL)
23110 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023111 prof_func_line(fd, fp->uf_tml_count[i],
23112 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023113 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23114 }
23115 fprintf(fd, "\n");
23116 }
23117 }
23118 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023119
23120 if (sorttab != NULL && st_len > 0)
23121 {
23122 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23123 prof_total_cmp);
23124 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23125 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23126 prof_self_cmp);
23127 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23128 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023129
23130 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023131}
Bram Moolenaar73830342005-02-28 22:48:19 +000023132
23133 static void
23134prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23135 FILE *fd;
23136 ufunc_T **sorttab;
23137 int st_len;
23138 char *title;
23139 int prefer_self; /* when equal print only self time */
23140{
23141 int i;
23142 ufunc_T *fp;
23143
23144 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23145 fprintf(fd, "count total (s) self (s) function\n");
23146 for (i = 0; i < 20 && i < st_len; ++i)
23147 {
23148 fp = sorttab[i];
23149 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23150 prefer_self);
23151 if (fp->uf_name[0] == K_SPECIAL)
23152 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23153 else
23154 fprintf(fd, " %s()\n", fp->uf_name);
23155 }
23156 fprintf(fd, "\n");
23157}
23158
23159/*
23160 * Print the count and times for one function or function line.
23161 */
23162 static void
23163prof_func_line(fd, count, total, self, prefer_self)
23164 FILE *fd;
23165 int count;
23166 proftime_T *total;
23167 proftime_T *self;
23168 int prefer_self; /* when equal print only self time */
23169{
23170 if (count > 0)
23171 {
23172 fprintf(fd, "%5d ", count);
23173 if (prefer_self && profile_equal(total, self))
23174 fprintf(fd, " ");
23175 else
23176 fprintf(fd, "%s ", profile_msg(total));
23177 if (!prefer_self && profile_equal(total, self))
23178 fprintf(fd, " ");
23179 else
23180 fprintf(fd, "%s ", profile_msg(self));
23181 }
23182 else
23183 fprintf(fd, " ");
23184}
23185
23186/*
23187 * Compare function for total time sorting.
23188 */
23189 static int
23190#ifdef __BORLANDC__
23191_RTLENTRYF
23192#endif
23193prof_total_cmp(s1, s2)
23194 const void *s1;
23195 const void *s2;
23196{
23197 ufunc_T *p1, *p2;
23198
23199 p1 = *(ufunc_T **)s1;
23200 p2 = *(ufunc_T **)s2;
23201 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23202}
23203
23204/*
23205 * Compare function for self time sorting.
23206 */
23207 static int
23208#ifdef __BORLANDC__
23209_RTLENTRYF
23210#endif
23211prof_self_cmp(s1, s2)
23212 const void *s1;
23213 const void *s2;
23214{
23215 ufunc_T *p1, *p2;
23216
23217 p1 = *(ufunc_T **)s1;
23218 p2 = *(ufunc_T **)s2;
23219 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23220}
23221
Bram Moolenaar05159a02005-02-26 23:04:13 +000023222#endif
23223
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023224/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023225 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023226 * Return TRUE if a package was loaded.
23227 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023228 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023229script_autoload(name, reload)
23230 char_u *name;
23231 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023232{
23233 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023234 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023235 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023236 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023237
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023238 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023239 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023240 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023241 return FALSE;
23242
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023243 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023244
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023245 /* Find the name in the list of previously loaded package names. Skip
23246 * "autoload/", it's always the same. */
23247 for (i = 0; i < ga_loaded.ga_len; ++i)
23248 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23249 break;
23250 if (!reload && i < ga_loaded.ga_len)
23251 ret = FALSE; /* was loaded already */
23252 else
23253 {
23254 /* Remember the name if it wasn't loaded already. */
23255 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23256 {
23257 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23258 tofree = NULL;
23259 }
23260
23261 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023262 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023263 ret = TRUE;
23264 }
23265
23266 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023267 return ret;
23268}
23269
23270/*
23271 * Return the autoload script name for a function or variable name.
23272 * Returns NULL when out of memory.
23273 */
23274 static char_u *
23275autoload_name(name)
23276 char_u *name;
23277{
23278 char_u *p;
23279 char_u *scriptname;
23280
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023281 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023282 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23283 if (scriptname == NULL)
23284 return FALSE;
23285 STRCPY(scriptname, "autoload/");
23286 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023287 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023288 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023289 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023290 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023291 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023292}
23293
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23295
23296/*
23297 * Function given to ExpandGeneric() to obtain the list of user defined
23298 * function names.
23299 */
23300 char_u *
23301get_user_func_name(xp, idx)
23302 expand_T *xp;
23303 int idx;
23304{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023305 static long_u done;
23306 static hashitem_T *hi;
23307 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308
23309 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023311 done = 0;
23312 hi = func_hashtab.ht_array;
23313 }
23314 if (done < func_hashtab.ht_used)
23315 {
23316 if (done++ > 0)
23317 ++hi;
23318 while (HASHITEM_EMPTY(hi))
23319 ++hi;
23320 fp = HI2UF(hi);
23321
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023322 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023323 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023324
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023325 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23326 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327
23328 cat_func_name(IObuff, fp);
23329 if (xp->xp_context != EXPAND_USER_FUNC)
23330 {
23331 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023332 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333 STRCAT(IObuff, ")");
23334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023335 return IObuff;
23336 }
23337 return NULL;
23338}
23339
23340#endif /* FEAT_CMDL_COMPL */
23341
23342/*
23343 * Copy the function name of "fp" to buffer "buf".
23344 * "buf" must be able to hold the function name plus three bytes.
23345 * Takes care of script-local function names.
23346 */
23347 static void
23348cat_func_name(buf, fp)
23349 char_u *buf;
23350 ufunc_T *fp;
23351{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023352 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353 {
23354 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023355 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356 }
23357 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023358 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023359}
23360
23361/*
23362 * ":delfunction {name}"
23363 */
23364 void
23365ex_delfunction(eap)
23366 exarg_T *eap;
23367{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023368 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369 char_u *p;
23370 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023371 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372
23373 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023374 name = trans_function_name(&p, eap->skip, 0, &fudi);
23375 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023377 {
23378 if (fudi.fd_dict != NULL && !eap->skip)
23379 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 if (!ends_excmd(*skipwhite(p)))
23383 {
23384 vim_free(name);
23385 EMSG(_(e_trailing));
23386 return;
23387 }
23388 eap->nextcmd = check_nextcmd(p);
23389 if (eap->nextcmd != NULL)
23390 *p = NUL;
23391
23392 if (!eap->skip)
23393 fp = find_func(name);
23394 vim_free(name);
23395
23396 if (!eap->skip)
23397 {
23398 if (fp == NULL)
23399 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023400 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 return;
23402 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023403 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404 {
23405 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23406 return;
23407 }
23408
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023409 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023411 /* Delete the dict item that refers to the function, it will
23412 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023413 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023415 else
23416 func_free(fp);
23417 }
23418}
23419
23420/*
23421 * Free a function and remove it from the list of functions.
23422 */
23423 static void
23424func_free(fp)
23425 ufunc_T *fp;
23426{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023427 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023428
23429 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023430 ga_clear_strings(&(fp->uf_args));
23431 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023432#ifdef FEAT_PROFILE
23433 vim_free(fp->uf_tml_count);
23434 vim_free(fp->uf_tml_total);
23435 vim_free(fp->uf_tml_self);
23436#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023437
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023438 /* remove the function from the function hashtable */
23439 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23440 if (HASHITEM_EMPTY(hi))
23441 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023442 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023443 hash_remove(&func_hashtab, hi);
23444
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023445 vim_free(fp);
23446}
23447
23448/*
23449 * Unreference a Function: decrement the reference count and free it when it
23450 * becomes zero. Only for numbered functions.
23451 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023452 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023453func_unref(name)
23454 char_u *name;
23455{
23456 ufunc_T *fp;
23457
23458 if (name != NULL && isdigit(*name))
23459 {
23460 fp = find_func(name);
23461 if (fp == NULL)
23462 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023463 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023464 {
23465 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023466 * when "uf_calls" becomes zero. */
23467 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023468 func_free(fp);
23469 }
23470 }
23471}
23472
23473/*
23474 * Count a reference to a Function.
23475 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023476 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023477func_ref(name)
23478 char_u *name;
23479{
23480 ufunc_T *fp;
23481
23482 if (name != NULL && isdigit(*name))
23483 {
23484 fp = find_func(name);
23485 if (fp == NULL)
23486 EMSG2(_(e_intern2), "func_ref()");
23487 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023488 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 }
23490}
23491
23492/*
23493 * Call a user function.
23494 */
23495 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023496call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497 ufunc_T *fp; /* pointer to function */
23498 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023499 typval_T *argvars; /* arguments */
23500 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 linenr_T firstline; /* first line of range */
23502 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023503 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023504{
Bram Moolenaar33570922005-01-25 22:26:29 +000023505 char_u *save_sourcing_name;
23506 linenr_T save_sourcing_lnum;
23507 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023508 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023509 int save_did_emsg;
23510 static int depth = 0;
23511 dictitem_T *v;
23512 int fixvar_idx = 0; /* index in fixvar[] */
23513 int i;
23514 int ai;
23515 char_u numbuf[NUMBUFLEN];
23516 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023517#ifdef FEAT_PROFILE
23518 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023519 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521
23522 /* If depth of calling is getting too high, don't execute the function */
23523 if (depth >= p_mfd)
23524 {
23525 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023526 rettv->v_type = VAR_NUMBER;
23527 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 return;
23529 }
23530 ++depth;
23531
23532 line_breakcheck(); /* check for CTRL-C hit */
23533
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023534 fc = (funccall_T *)alloc(sizeof(funccall_T));
23535 fc->caller = current_funccal;
23536 current_funccal = fc;
23537 fc->func = fp;
23538 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023539 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023540 fc->linenr = 0;
23541 fc->returned = FALSE;
23542 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023544 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23545 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546
Bram Moolenaar33570922005-01-25 22:26:29 +000023547 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023548 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023549 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23550 * each argument variable and saves a lot of time.
23551 */
23552 /*
23553 * Init l: variables.
23554 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023555 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023556 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023557 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023558 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23559 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023560 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023561 name = v->di_key;
23562 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023563 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023564 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023565 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023566 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023567 v->di_tv.vval.v_dict = selfdict;
23568 ++selfdict->dv_refcount;
23569 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023570
Bram Moolenaar33570922005-01-25 22:26:29 +000023571 /*
23572 * Init a: variables.
23573 * Set a:0 to "argcount".
23574 * Set a:000 to a list with room for the "..." arguments.
23575 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023576 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023577 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023578 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023579 /* Use "name" to avoid a warning from some compiler that checks the
23580 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023581 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023582 name = v->di_key;
23583 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023584 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023585 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023586 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023587 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023588 v->di_tv.vval.v_list = &fc->l_varlist;
23589 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23590 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23591 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023592
23593 /*
23594 * Set a:firstline to "firstline" and a:lastline to "lastline".
23595 * Set a:name to named arguments.
23596 * Set a:N to the "..." arguments.
23597 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023598 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023599 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023600 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023601 (varnumber_T)lastline);
23602 for (i = 0; i < argcount; ++i)
23603 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023604 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023605 if (ai < 0)
23606 /* named argument a:name */
23607 name = FUNCARG(fp, i);
23608 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023609 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023610 /* "..." argument a:1, a:2, etc. */
23611 sprintf((char *)numbuf, "%d", ai + 1);
23612 name = numbuf;
23613 }
23614 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23615 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023616 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023617 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23618 }
23619 else
23620 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023621 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23622 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023623 if (v == NULL)
23624 break;
23625 v->di_flags = DI_FLAGS_RO;
23626 }
23627 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023628 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023629
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023630 /* Note: the values are copied directly to avoid alloc/free.
23631 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023632 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023633 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023634
23635 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23636 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023637 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23638 fc->l_listitems[ai].li_tv = argvars[i];
23639 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023640 }
23641 }
23642
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 /* Don't redraw while executing the function. */
23644 ++RedrawingDisabled;
23645 save_sourcing_name = sourcing_name;
23646 save_sourcing_lnum = sourcing_lnum;
23647 sourcing_lnum = 1;
23648 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023649 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 if (sourcing_name != NULL)
23651 {
23652 if (save_sourcing_name != NULL
23653 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23654 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23655 else
23656 STRCPY(sourcing_name, "function ");
23657 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23658
23659 if (p_verbose >= 12)
23660 {
23661 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023662 verbose_enter_scroll();
23663
Bram Moolenaar555b2802005-05-19 21:08:39 +000023664 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 if (p_verbose >= 14)
23666 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023668 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023669 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023670 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671
23672 msg_puts((char_u *)"(");
23673 for (i = 0; i < argcount; ++i)
23674 {
23675 if (i > 0)
23676 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023677 if (argvars[i].v_type == VAR_NUMBER)
23678 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 else
23680 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023681 /* Do not want errors such as E724 here. */
23682 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023683 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023684 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023685 if (s != NULL)
23686 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023687 if (vim_strsize(s) > MSG_BUF_CLEN)
23688 {
23689 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23690 s = buf;
23691 }
23692 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023693 vim_free(tofree);
23694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695 }
23696 }
23697 msg_puts((char_u *)")");
23698 }
23699 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023700
23701 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 --no_wait_return;
23703 }
23704 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023705#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023706 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023707 {
23708 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23709 func_do_profile(fp);
23710 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023711 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023712 {
23713 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023714 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023715 profile_zero(&fp->uf_tm_children);
23716 }
23717 script_prof_save(&wait_start);
23718 }
23719#endif
23720
Bram Moolenaar071d4272004-06-13 20:20:40 +000023721 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023722 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723 save_did_emsg = did_emsg;
23724 did_emsg = FALSE;
23725
23726 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023727 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23729
23730 --RedrawingDisabled;
23731
23732 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023733 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023735 clear_tv(rettv);
23736 rettv->v_type = VAR_NUMBER;
23737 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 }
23739
Bram Moolenaar05159a02005-02-26 23:04:13 +000023740#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023741 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023742 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023743 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023744 profile_end(&call_start);
23745 profile_sub_wait(&wait_start, &call_start);
23746 profile_add(&fp->uf_tm_total, &call_start);
23747 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023748 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023749 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023750 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23751 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023752 }
23753 }
23754#endif
23755
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756 /* when being verbose, mention the return value */
23757 if (p_verbose >= 12)
23758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023760 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023761
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023763 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023764 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023765 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023766 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023767 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023769 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023770 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023771 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023772 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023773
Bram Moolenaar555b2802005-05-19 21:08:39 +000023774 /* The value may be very long. Skip the middle part, so that we
23775 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023776 * truncate it at the end. Don't want errors such as E724 here. */
23777 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023778 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023779 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023780 if (s != NULL)
23781 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023782 if (vim_strsize(s) > MSG_BUF_CLEN)
23783 {
23784 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23785 s = buf;
23786 }
23787 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023788 vim_free(tofree);
23789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023790 }
23791 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023792
23793 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794 --no_wait_return;
23795 }
23796
23797 vim_free(sourcing_name);
23798 sourcing_name = save_sourcing_name;
23799 sourcing_lnum = save_sourcing_lnum;
23800 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023801#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023802 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023803 script_prof_restore(&wait_start);
23804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805
23806 if (p_verbose >= 12 && sourcing_name != NULL)
23807 {
23808 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023809 verbose_enter_scroll();
23810
Bram Moolenaar555b2802005-05-19 21:08:39 +000023811 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023813
23814 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815 --no_wait_return;
23816 }
23817
23818 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023819 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023821
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023822 /* If the a:000 list and the l: and a: dicts are not referenced we can
23823 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023824 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23825 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23826 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23827 {
23828 free_funccal(fc, FALSE);
23829 }
23830 else
23831 {
23832 hashitem_T *hi;
23833 listitem_T *li;
23834 int todo;
23835
23836 /* "fc" is still in use. This can happen when returning "a:000" or
23837 * assigning "l:" to a global variable.
23838 * Link "fc" in the list for garbage collection later. */
23839 fc->caller = previous_funccal;
23840 previous_funccal = fc;
23841
23842 /* Make a copy of the a: variables, since we didn't do that above. */
23843 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23844 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23845 {
23846 if (!HASHITEM_EMPTY(hi))
23847 {
23848 --todo;
23849 v = HI2DI(hi);
23850 copy_tv(&v->di_tv, &v->di_tv);
23851 }
23852 }
23853
23854 /* Make a copy of the a:000 items, since we didn't do that above. */
23855 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23856 copy_tv(&li->li_tv, &li->li_tv);
23857 }
23858}
23859
23860/*
23861 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023862 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023863 */
23864 static int
23865can_free_funccal(fc, copyID)
23866 funccall_T *fc;
23867 int copyID;
23868{
23869 return (fc->l_varlist.lv_copyID != copyID
23870 && fc->l_vars.dv_copyID != copyID
23871 && fc->l_avars.dv_copyID != copyID);
23872}
23873
23874/*
23875 * Free "fc" and what it contains.
23876 */
23877 static void
23878free_funccal(fc, free_val)
23879 funccall_T *fc;
23880 int free_val; /* a: vars were allocated */
23881{
23882 listitem_T *li;
23883
23884 /* The a: variables typevals may not have been allocated, only free the
23885 * allocated variables. */
23886 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23887
23888 /* free all l: variables */
23889 vars_clear(&fc->l_vars.dv_hashtab);
23890
23891 /* Free the a:000 variables if they were allocated. */
23892 if (free_val)
23893 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23894 clear_tv(&li->li_tv);
23895
23896 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897}
23898
23899/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023900 * Add a number variable "name" to dict "dp" with value "nr".
23901 */
23902 static void
23903add_nr_var(dp, v, name, nr)
23904 dict_T *dp;
23905 dictitem_T *v;
23906 char *name;
23907 varnumber_T nr;
23908{
23909 STRCPY(v->di_key, name);
23910 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23911 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23912 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023913 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023914 v->di_tv.vval.v_number = nr;
23915}
23916
23917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918 * ":return [expr]"
23919 */
23920 void
23921ex_return(eap)
23922 exarg_T *eap;
23923{
23924 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023925 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 int returning = FALSE;
23927
23928 if (current_funccal == NULL)
23929 {
23930 EMSG(_("E133: :return not inside a function"));
23931 return;
23932 }
23933
23934 if (eap->skip)
23935 ++emsg_skip;
23936
23937 eap->nextcmd = NULL;
23938 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023939 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 {
23941 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023942 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023944 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023945 }
23946 /* It's safer to return also on error. */
23947 else if (!eap->skip)
23948 {
23949 /*
23950 * Return unless the expression evaluation has been cancelled due to an
23951 * aborting error, an interrupt, or an exception.
23952 */
23953 if (!aborting())
23954 returning = do_return(eap, FALSE, TRUE, NULL);
23955 }
23956
23957 /* When skipping or the return gets pending, advance to the next command
23958 * in this line (!returning). Otherwise, ignore the rest of the line.
23959 * Following lines will be ignored by get_func_line(). */
23960 if (returning)
23961 eap->nextcmd = NULL;
23962 else if (eap->nextcmd == NULL) /* no argument */
23963 eap->nextcmd = check_nextcmd(arg);
23964
23965 if (eap->skip)
23966 --emsg_skip;
23967}
23968
23969/*
23970 * Return from a function. Possibly makes the return pending. Also called
23971 * for a pending return at the ":endtry" or after returning from an extra
23972 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023973 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023974 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023975 * FALSE when the return gets pending.
23976 */
23977 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023978do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 exarg_T *eap;
23980 int reanimate;
23981 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023982 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983{
23984 int idx;
23985 struct condstack *cstack = eap->cstack;
23986
23987 if (reanimate)
23988 /* Undo the return. */
23989 current_funccal->returned = FALSE;
23990
23991 /*
23992 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23993 * not in its finally clause (which then is to be executed next) is found.
23994 * In this case, make the ":return" pending for execution at the ":endtry".
23995 * Otherwise, return normally.
23996 */
23997 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23998 if (idx >= 0)
23999 {
24000 cstack->cs_pending[idx] = CSTP_RETURN;
24001
24002 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024003 /* A pending return again gets pending. "rettv" points to an
24004 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024006 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024007 else
24008 {
24009 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024010 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024011 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024012 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024014 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024015 {
24016 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024017 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024018 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 else
24020 EMSG(_(e_outofmem));
24021 }
24022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024023 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024
24025 if (reanimate)
24026 {
24027 /* The pending return value could be overwritten by a ":return"
24028 * without argument in a finally clause; reset the default
24029 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024030 current_funccal->rettv->v_type = VAR_NUMBER;
24031 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 }
24033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024034 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 }
24036 else
24037 {
24038 current_funccal->returned = TRUE;
24039
24040 /* If the return is carried out now, store the return value. For
24041 * a return immediately after reanimation, the value is already
24042 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024043 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024045 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024046 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024048 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 }
24050 }
24051
24052 return idx < 0;
24053}
24054
24055/*
24056 * Free the variable with a pending return value.
24057 */
24058 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024059discard_pending_return(rettv)
24060 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061{
Bram Moolenaar33570922005-01-25 22:26:29 +000024062 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063}
24064
24065/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024066 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024067 * is an allocated string. Used by report_pending() for verbose messages.
24068 */
24069 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024070get_return_cmd(rettv)
24071 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024073 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024074 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024075 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024077 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024078 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024079 if (s == NULL)
24080 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024081
24082 STRCPY(IObuff, ":return ");
24083 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24084 if (STRLEN(s) + 8 >= IOSIZE)
24085 STRCPY(IObuff + IOSIZE - 4, "...");
24086 vim_free(tofree);
24087 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088}
24089
24090/*
24091 * Get next function line.
24092 * Called by do_cmdline() to get the next line.
24093 * Returns allocated string, or NULL for end of function.
24094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095 char_u *
24096get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024097 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024099 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100{
Bram Moolenaar33570922005-01-25 22:26:29 +000024101 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024102 ufunc_T *fp = fcp->func;
24103 char_u *retval;
24104 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024105
24106 /* If breakpoints have been added/deleted need to check for it. */
24107 if (fcp->dbg_tick != debug_tick)
24108 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024109 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110 sourcing_lnum);
24111 fcp->dbg_tick = debug_tick;
24112 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024113#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024114 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024115 func_line_end(cookie);
24116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117
Bram Moolenaar05159a02005-02-26 23:04:13 +000024118 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024119 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24120 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 retval = NULL;
24122 else
24123 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024124 /* Skip NULL lines (continuation lines). */
24125 while (fcp->linenr < gap->ga_len
24126 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24127 ++fcp->linenr;
24128 if (fcp->linenr >= gap->ga_len)
24129 retval = NULL;
24130 else
24131 {
24132 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24133 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024134#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024135 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024136 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024137#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024139 }
24140
24141 /* Did we encounter a breakpoint? */
24142 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24143 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024144 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024146 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147 sourcing_lnum);
24148 fcp->dbg_tick = debug_tick;
24149 }
24150
24151 return retval;
24152}
24153
Bram Moolenaar05159a02005-02-26 23:04:13 +000024154#if defined(FEAT_PROFILE) || defined(PROTO)
24155/*
24156 * Called when starting to read a function line.
24157 * "sourcing_lnum" must be correct!
24158 * When skipping lines it may not actually be executed, but we won't find out
24159 * until later and we need to store the time now.
24160 */
24161 void
24162func_line_start(cookie)
24163 void *cookie;
24164{
24165 funccall_T *fcp = (funccall_T *)cookie;
24166 ufunc_T *fp = fcp->func;
24167
24168 if (fp->uf_profiling && sourcing_lnum >= 1
24169 && sourcing_lnum <= fp->uf_lines.ga_len)
24170 {
24171 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024172 /* Skip continuation lines. */
24173 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24174 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024175 fp->uf_tml_execed = FALSE;
24176 profile_start(&fp->uf_tml_start);
24177 profile_zero(&fp->uf_tml_children);
24178 profile_get_wait(&fp->uf_tml_wait);
24179 }
24180}
24181
24182/*
24183 * Called when actually executing a function line.
24184 */
24185 void
24186func_line_exec(cookie)
24187 void *cookie;
24188{
24189 funccall_T *fcp = (funccall_T *)cookie;
24190 ufunc_T *fp = fcp->func;
24191
24192 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24193 fp->uf_tml_execed = TRUE;
24194}
24195
24196/*
24197 * Called when done with a function line.
24198 */
24199 void
24200func_line_end(cookie)
24201 void *cookie;
24202{
24203 funccall_T *fcp = (funccall_T *)cookie;
24204 ufunc_T *fp = fcp->func;
24205
24206 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24207 {
24208 if (fp->uf_tml_execed)
24209 {
24210 ++fp->uf_tml_count[fp->uf_tml_idx];
24211 profile_end(&fp->uf_tml_start);
24212 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024213 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024214 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24215 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024216 }
24217 fp->uf_tml_idx = -1;
24218 }
24219}
24220#endif
24221
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222/*
24223 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024224 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 */
24226 int
24227func_has_ended(cookie)
24228 void *cookie;
24229{
Bram Moolenaar33570922005-01-25 22:26:29 +000024230 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024231
24232 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24233 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024234 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024235 || fcp->returned);
24236}
24237
24238/*
24239 * return TRUE if cookie indicates a function which "abort"s on errors.
24240 */
24241 int
24242func_has_abort(cookie)
24243 void *cookie;
24244{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024245 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246}
24247
24248#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24249typedef enum
24250{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024251 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24252 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24253 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254} var_flavour_T;
24255
24256static var_flavour_T var_flavour __ARGS((char_u *varname));
24257
24258 static var_flavour_T
24259var_flavour(varname)
24260 char_u *varname;
24261{
24262 char_u *p = varname;
24263
24264 if (ASCII_ISUPPER(*p))
24265 {
24266 while (*(++p))
24267 if (ASCII_ISLOWER(*p))
24268 return VAR_FLAVOUR_SESSION;
24269 return VAR_FLAVOUR_VIMINFO;
24270 }
24271 else
24272 return VAR_FLAVOUR_DEFAULT;
24273}
24274#endif
24275
24276#if defined(FEAT_VIMINFO) || defined(PROTO)
24277/*
24278 * Restore global vars that start with a capital from the viminfo file
24279 */
24280 int
24281read_viminfo_varlist(virp, writing)
24282 vir_T *virp;
24283 int writing;
24284{
24285 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024286 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024287 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288
24289 if (!writing && (find_viminfo_parameter('!') != NULL))
24290 {
24291 tab = vim_strchr(virp->vir_line + 1, '\t');
24292 if (tab != NULL)
24293 {
24294 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024295 switch (*tab)
24296 {
24297 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024298#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024299 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024300#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024301 case 'D': type = VAR_DICT; break;
24302 case 'L': type = VAR_LIST; break;
24303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304
24305 tab = vim_strchr(tab, '\t');
24306 if (tab != NULL)
24307 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024308 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024309 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024310 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024312#ifdef FEAT_FLOAT
24313 else if (type == VAR_FLOAT)
24314 (void)string2float(tab + 1, &tv.vval.v_float);
24315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024317 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024318 if (type == VAR_DICT || type == VAR_LIST)
24319 {
24320 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24321
24322 if (etv == NULL)
24323 /* Failed to parse back the dict or list, use it as a
24324 * string. */
24325 tv.v_type = VAR_STRING;
24326 else
24327 {
24328 vim_free(tv.vval.v_string);
24329 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024330 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024331 }
24332 }
24333
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024334 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024335
24336 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024337 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024338 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24339 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340 }
24341 }
24342 }
24343
24344 return viminfo_readline(virp);
24345}
24346
24347/*
24348 * Write global vars that start with a capital to the viminfo file
24349 */
24350 void
24351write_viminfo_varlist(fp)
24352 FILE *fp;
24353{
Bram Moolenaar33570922005-01-25 22:26:29 +000024354 hashitem_T *hi;
24355 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024356 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024357 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024358 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024359 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024360 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361
24362 if (find_viminfo_parameter('!') == NULL)
24363 return;
24364
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024365 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024366
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024367 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024368 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024370 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024372 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024373 this_var = HI2DI(hi);
24374 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024375 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024376 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024377 {
24378 case VAR_STRING: s = "STR"; break;
24379 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024380#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024381 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024382#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024383 case VAR_DICT: s = "DIC"; break;
24384 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024385 default: continue;
24386 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024387 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024388 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024389 if (p != NULL)
24390 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024391 vim_free(tofree);
24392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024393 }
24394 }
24395}
24396#endif
24397
24398#if defined(FEAT_SESSION) || defined(PROTO)
24399 int
24400store_session_globals(fd)
24401 FILE *fd;
24402{
Bram Moolenaar33570922005-01-25 22:26:29 +000024403 hashitem_T *hi;
24404 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024405 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 char_u *p, *t;
24407
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024408 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024409 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024411 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024413 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024414 this_var = HI2DI(hi);
24415 if ((this_var->di_tv.v_type == VAR_NUMBER
24416 || this_var->di_tv.v_type == VAR_STRING)
24417 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024418 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024419 /* Escape special characters with a backslash. Turn a LF and
24420 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024421 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024422 (char_u *)"\\\"\n\r");
24423 if (p == NULL) /* out of memory */
24424 break;
24425 for (t = p; *t != NUL; ++t)
24426 if (*t == '\n')
24427 *t = 'n';
24428 else if (*t == '\r')
24429 *t = 'r';
24430 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024431 this_var->di_key,
24432 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24433 : ' ',
24434 p,
24435 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24436 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024437 || put_eol(fd) == FAIL)
24438 {
24439 vim_free(p);
24440 return FAIL;
24441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442 vim_free(p);
24443 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024444#ifdef FEAT_FLOAT
24445 else if (this_var->di_tv.v_type == VAR_FLOAT
24446 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24447 {
24448 float_T f = this_var->di_tv.vval.v_float;
24449 int sign = ' ';
24450
24451 if (f < 0)
24452 {
24453 f = -f;
24454 sign = '-';
24455 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024456 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024457 this_var->di_key, sign, f) < 0)
24458 || put_eol(fd) == FAIL)
24459 return FAIL;
24460 }
24461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462 }
24463 }
24464 return OK;
24465}
24466#endif
24467
Bram Moolenaar661b1822005-07-28 22:36:45 +000024468/*
24469 * Display script name where an item was last set.
24470 * Should only be invoked when 'verbose' is non-zero.
24471 */
24472 void
24473last_set_msg(scriptID)
24474 scid_T scriptID;
24475{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024476 char_u *p;
24477
Bram Moolenaar661b1822005-07-28 22:36:45 +000024478 if (scriptID != 0)
24479 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024480 p = home_replace_save(NULL, get_scriptname(scriptID));
24481 if (p != NULL)
24482 {
24483 verbose_enter();
24484 MSG_PUTS(_("\n\tLast set from "));
24485 MSG_PUTS(p);
24486 vim_free(p);
24487 verbose_leave();
24488 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024489 }
24490}
24491
Bram Moolenaard812df62008-11-09 12:46:09 +000024492/*
24493 * List v:oldfiles in a nice way.
24494 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024495 void
24496ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024497 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024498{
24499 list_T *l = vimvars[VV_OLDFILES].vv_list;
24500 listitem_T *li;
24501 int nr = 0;
24502
24503 if (l == NULL)
24504 msg((char_u *)_("No old files"));
24505 else
24506 {
24507 msg_start();
24508 msg_scroll = TRUE;
24509 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24510 {
24511 msg_outnum((long)++nr);
24512 MSG_PUTS(": ");
24513 msg_outtrans(get_tv_string(&li->li_tv));
24514 msg_putchar('\n');
24515 out_flush(); /* output one line at a time */
24516 ui_breakcheck();
24517 }
24518 /* Assume "got_int" was set to truncate the listing. */
24519 got_int = FALSE;
24520
24521#ifdef FEAT_BROWSE_CMD
24522 if (cmdmod.browse)
24523 {
24524 quit_more = FALSE;
24525 nr = prompt_for_number(FALSE);
24526 msg_starthere();
24527 if (nr > 0)
24528 {
24529 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24530 (long)nr);
24531
24532 if (p != NULL)
24533 {
24534 p = expand_env_save(p);
24535 eap->arg = p;
24536 eap->cmdidx = CMD_edit;
24537 cmdmod.browse = FALSE;
24538 do_exedit(eap, NULL);
24539 vim_free(p);
24540 }
24541 }
24542 }
24543#endif
24544 }
24545}
24546
Bram Moolenaar071d4272004-06-13 20:20:40 +000024547#endif /* FEAT_EVAL */
24548
Bram Moolenaar071d4272004-06-13 20:20:40 +000024549
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024550#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024551
24552#ifdef WIN3264
24553/*
24554 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24555 */
24556static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24557static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24558static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24559
24560/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024561 * Get the short path (8.3) for the filename in "fnamep".
24562 * Only works for a valid file name.
24563 * When the path gets longer "fnamep" is changed and the allocated buffer
24564 * is put in "bufp".
24565 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24566 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 */
24568 static int
24569get_short_pathname(fnamep, bufp, fnamelen)
24570 char_u **fnamep;
24571 char_u **bufp;
24572 int *fnamelen;
24573{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024574 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575 char_u *newbuf;
24576
24577 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 l = GetShortPathName(*fnamep, *fnamep, len);
24579 if (l > len - 1)
24580 {
24581 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024582 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583 newbuf = vim_strnsave(*fnamep, l);
24584 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024585 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586
24587 vim_free(*bufp);
24588 *fnamep = *bufp = newbuf;
24589
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024590 /* Really should always succeed, as the buffer is big enough. */
24591 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024592 }
24593
24594 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024595 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596}
24597
24598/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024599 * Get the short path (8.3) for the filename in "fname". The converted
24600 * path is returned in "bufp".
24601 *
24602 * Some of the directories specified in "fname" may not exist. This function
24603 * will shorten the existing directories at the beginning of the path and then
24604 * append the remaining non-existing path.
24605 *
24606 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024607 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024608 * bufp - Pointer to an allocated buffer for the filename.
24609 * fnamelen - Length of the filename pointed to by fname
24610 *
24611 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612 */
24613 static int
24614shortpath_for_invalid_fname(fname, bufp, fnamelen)
24615 char_u **fname;
24616 char_u **bufp;
24617 int *fnamelen;
24618{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024619 char_u *short_fname, *save_fname, *pbuf_unused;
24620 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024622 int old_len, len;
24623 int new_len, sfx_len;
24624 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625
24626 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024627 old_len = *fnamelen;
24628 save_fname = vim_strnsave(*fname, old_len);
24629 pbuf_unused = NULL;
24630 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024632 endp = save_fname + old_len - 1; /* Find the end of the copy */
24633 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024635 /*
24636 * Try shortening the supplied path till it succeeds by removing one
24637 * directory at a time from the tail of the path.
24638 */
24639 len = 0;
24640 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024642 /* go back one path-separator */
24643 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24644 --endp;
24645 if (endp <= save_fname)
24646 break; /* processed the complete path */
24647
24648 /*
24649 * Replace the path separator with a NUL and try to shorten the
24650 * resulting path.
24651 */
24652 ch = *endp;
24653 *endp = 0;
24654 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024655 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024656 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24657 {
24658 retval = FAIL;
24659 goto theend;
24660 }
24661 *endp = ch; /* preserve the string */
24662
24663 if (len > 0)
24664 break; /* successfully shortened the path */
24665
24666 /* failed to shorten the path. Skip the path separator */
24667 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668 }
24669
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024670 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024671 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024672 /*
24673 * Succeeded in shortening the path. Now concatenate the shortened
24674 * path with the remaining path at the tail.
24675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024676
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024677 /* Compute the length of the new path. */
24678 sfx_len = (int)(save_endp - endp) + 1;
24679 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024681 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024683 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024684 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024685 /* There is not enough space in the currently allocated string,
24686 * copy it to a buffer big enough. */
24687 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024688 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024689 {
24690 retval = FAIL;
24691 goto theend;
24692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 }
24694 else
24695 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024696 /* Transfer short_fname to the main buffer (it's big enough),
24697 * unless get_short_pathname() did its work in-place. */
24698 *fname = *bufp = save_fname;
24699 if (short_fname != save_fname)
24700 vim_strncpy(save_fname, short_fname, len);
24701 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024703
24704 /* concat the not-shortened part of the path */
24705 vim_strncpy(*fname + len, endp, sfx_len);
24706 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024708
24709theend:
24710 vim_free(pbuf_unused);
24711 vim_free(save_fname);
24712
24713 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714}
24715
24716/*
24717 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024718 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719 */
24720 static int
24721shortpath_for_partial(fnamep, bufp, fnamelen)
24722 char_u **fnamep;
24723 char_u **bufp;
24724 int *fnamelen;
24725{
24726 int sepcount, len, tflen;
24727 char_u *p;
24728 char_u *pbuf, *tfname;
24729 int hasTilde;
24730
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024731 /* Count up the path separators from the RHS.. so we know which part
24732 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024734 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024735 if (vim_ispathsep(*p))
24736 ++sepcount;
24737
24738 /* Need full path first (use expand_env() to remove a "~/") */
24739 hasTilde = (**fnamep == '~');
24740 if (hasTilde)
24741 pbuf = tfname = expand_env_save(*fnamep);
24742 else
24743 pbuf = tfname = FullName_save(*fnamep, FALSE);
24744
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024745 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024746
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024747 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24748 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024749
24750 if (len == 0)
24751 {
24752 /* Don't have a valid filename, so shorten the rest of the
24753 * path if we can. This CAN give us invalid 8.3 filenames, but
24754 * there's not a lot of point in guessing what it might be.
24755 */
24756 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024757 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24758 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024759 }
24760
24761 /* Count the paths backward to find the beginning of the desired string. */
24762 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024763 {
24764#ifdef FEAT_MBYTE
24765 if (has_mbyte)
24766 p -= mb_head_off(tfname, p);
24767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768 if (vim_ispathsep(*p))
24769 {
24770 if (sepcount == 0 || (hasTilde && sepcount == 1))
24771 break;
24772 else
24773 sepcount --;
24774 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024776 if (hasTilde)
24777 {
24778 --p;
24779 if (p >= tfname)
24780 *p = '~';
24781 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024782 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783 }
24784 else
24785 ++p;
24786
24787 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24788 vim_free(*bufp);
24789 *fnamelen = (int)STRLEN(p);
24790 *bufp = pbuf;
24791 *fnamep = p;
24792
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024793 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024794}
24795#endif /* WIN3264 */
24796
24797/*
24798 * Adjust a filename, according to a string of modifiers.
24799 * *fnamep must be NUL terminated when called. When returning, the length is
24800 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024801 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802 * When there is an error, *fnamep is set to NULL.
24803 */
24804 int
24805modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24806 char_u *src; /* string with modifiers */
24807 int *usedlen; /* characters after src that are used */
24808 char_u **fnamep; /* file name so far */
24809 char_u **bufp; /* buffer for allocated file name or NULL */
24810 int *fnamelen; /* length of fnamep */
24811{
24812 int valid = 0;
24813 char_u *tail;
24814 char_u *s, *p, *pbuf;
24815 char_u dirname[MAXPATHL];
24816 int c;
24817 int has_fullname = 0;
24818#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024819 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820 int has_shortname = 0;
24821#endif
24822
24823repeat:
24824 /* ":p" - full path/file_name */
24825 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24826 {
24827 has_fullname = 1;
24828
24829 valid |= VALID_PATH;
24830 *usedlen += 2;
24831
24832 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24833 if ((*fnamep)[0] == '~'
24834#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24835 && ((*fnamep)[1] == '/'
24836# ifdef BACKSLASH_IN_FILENAME
24837 || (*fnamep)[1] == '\\'
24838# endif
24839 || (*fnamep)[1] == NUL)
24840
24841#endif
24842 )
24843 {
24844 *fnamep = expand_env_save(*fnamep);
24845 vim_free(*bufp); /* free any allocated file name */
24846 *bufp = *fnamep;
24847 if (*fnamep == NULL)
24848 return -1;
24849 }
24850
24851 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024852 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853 {
24854 if (vim_ispathsep(*p)
24855 && p[1] == '.'
24856 && (p[2] == NUL
24857 || vim_ispathsep(p[2])
24858 || (p[2] == '.'
24859 && (p[3] == NUL || vim_ispathsep(p[3])))))
24860 break;
24861 }
24862
24863 /* FullName_save() is slow, don't use it when not needed. */
24864 if (*p != NUL || !vim_isAbsName(*fnamep))
24865 {
24866 *fnamep = FullName_save(*fnamep, *p != NUL);
24867 vim_free(*bufp); /* free any allocated file name */
24868 *bufp = *fnamep;
24869 if (*fnamep == NULL)
24870 return -1;
24871 }
24872
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024873#ifdef WIN3264
24874# if _WIN32_WINNT >= 0x0500
24875 if (vim_strchr(*fnamep, '~') != NULL)
24876 {
24877 /* Expand 8.3 filename to full path. Needed to make sure the same
24878 * file does not have two different names.
24879 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24880 p = alloc(_MAX_PATH + 1);
24881 if (p != NULL)
24882 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010024883 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024884 {
24885 vim_free(*bufp);
24886 *bufp = *fnamep = p;
24887 }
24888 else
24889 vim_free(p);
24890 }
24891 }
24892# endif
24893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894 /* Append a path separator to a directory. */
24895 if (mch_isdir(*fnamep))
24896 {
24897 /* Make room for one or two extra characters. */
24898 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24899 vim_free(*bufp); /* free any allocated file name */
24900 *bufp = *fnamep;
24901 if (*fnamep == NULL)
24902 return -1;
24903 add_pathsep(*fnamep);
24904 }
24905 }
24906
24907 /* ":." - path relative to the current directory */
24908 /* ":~" - path relative to the home directory */
24909 /* ":8" - shortname path - postponed till after */
24910 while (src[*usedlen] == ':'
24911 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24912 {
24913 *usedlen += 2;
24914 if (c == '8')
24915 {
24916#ifdef WIN3264
24917 has_shortname = 1; /* Postpone this. */
24918#endif
24919 continue;
24920 }
24921 pbuf = NULL;
24922 /* Need full path first (use expand_env() to remove a "~/") */
24923 if (!has_fullname)
24924 {
24925 if (c == '.' && **fnamep == '~')
24926 p = pbuf = expand_env_save(*fnamep);
24927 else
24928 p = pbuf = FullName_save(*fnamep, FALSE);
24929 }
24930 else
24931 p = *fnamep;
24932
24933 has_fullname = 0;
24934
24935 if (p != NULL)
24936 {
24937 if (c == '.')
24938 {
24939 mch_dirname(dirname, MAXPATHL);
24940 s = shorten_fname(p, dirname);
24941 if (s != NULL)
24942 {
24943 *fnamep = s;
24944 if (pbuf != NULL)
24945 {
24946 vim_free(*bufp); /* free any allocated file name */
24947 *bufp = pbuf;
24948 pbuf = NULL;
24949 }
24950 }
24951 }
24952 else
24953 {
24954 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24955 /* Only replace it when it starts with '~' */
24956 if (*dirname == '~')
24957 {
24958 s = vim_strsave(dirname);
24959 if (s != NULL)
24960 {
24961 *fnamep = s;
24962 vim_free(*bufp);
24963 *bufp = s;
24964 }
24965 }
24966 }
24967 vim_free(pbuf);
24968 }
24969 }
24970
24971 tail = gettail(*fnamep);
24972 *fnamelen = (int)STRLEN(*fnamep);
24973
24974 /* ":h" - head, remove "/file_name", can be repeated */
24975 /* Don't remove the first "/" or "c:\" */
24976 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24977 {
24978 valid |= VALID_HEAD;
24979 *usedlen += 2;
24980 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024981 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024982 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024983 *fnamelen = (int)(tail - *fnamep);
24984#ifdef VMS
24985 if (*fnamelen > 0)
24986 *fnamelen += 1; /* the path separator is part of the path */
24987#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024988 if (*fnamelen == 0)
24989 {
24990 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24991 p = vim_strsave((char_u *)".");
24992 if (p == NULL)
24993 return -1;
24994 vim_free(*bufp);
24995 *bufp = *fnamep = tail = p;
24996 *fnamelen = 1;
24997 }
24998 else
24999 {
25000 while (tail > s && !after_pathsep(s, tail))
25001 mb_ptr_back(*fnamep, tail);
25002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025003 }
25004
25005 /* ":8" - shortname */
25006 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25007 {
25008 *usedlen += 2;
25009#ifdef WIN3264
25010 has_shortname = 1;
25011#endif
25012 }
25013
25014#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025015 /*
25016 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017 */
25018 if (has_shortname)
25019 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025020 /* Copy the string if it is shortened by :h and when it wasn't copied
25021 * yet, because we are going to change it in place. Avoids changing
25022 * the buffer name for "%:8". */
25023 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024 {
25025 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025026 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027 return -1;
25028 vim_free(*bufp);
25029 *bufp = *fnamep = p;
25030 }
25031
25032 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025033 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025034 if (!has_fullname && !vim_isAbsName(*fnamep))
25035 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025036 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025037 return -1;
25038 }
25039 else
25040 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025041 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042
Bram Moolenaardc935552011-08-17 15:23:23 +020025043 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025044 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025045 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046 return -1;
25047
25048 if (l == 0)
25049 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025050 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025051 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025052 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025053 return -1;
25054 }
25055 *fnamelen = l;
25056 }
25057 }
25058#endif /* WIN3264 */
25059
25060 /* ":t" - tail, just the basename */
25061 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25062 {
25063 *usedlen += 2;
25064 *fnamelen -= (int)(tail - *fnamep);
25065 *fnamep = tail;
25066 }
25067
25068 /* ":e" - extension, can be repeated */
25069 /* ":r" - root, without extension, can be repeated */
25070 while (src[*usedlen] == ':'
25071 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25072 {
25073 /* find a '.' in the tail:
25074 * - for second :e: before the current fname
25075 * - otherwise: The last '.'
25076 */
25077 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25078 s = *fnamep - 2;
25079 else
25080 s = *fnamep + *fnamelen - 1;
25081 for ( ; s > tail; --s)
25082 if (s[0] == '.')
25083 break;
25084 if (src[*usedlen + 1] == 'e') /* :e */
25085 {
25086 if (s > tail)
25087 {
25088 *fnamelen += (int)(*fnamep - (s + 1));
25089 *fnamep = s + 1;
25090#ifdef VMS
25091 /* cut version from the extension */
25092 s = *fnamep + *fnamelen - 1;
25093 for ( ; s > *fnamep; --s)
25094 if (s[0] == ';')
25095 break;
25096 if (s > *fnamep)
25097 *fnamelen = s - *fnamep;
25098#endif
25099 }
25100 else if (*fnamep <= tail)
25101 *fnamelen = 0;
25102 }
25103 else /* :r */
25104 {
25105 if (s > tail) /* remove one extension */
25106 *fnamelen = (int)(s - *fnamep);
25107 }
25108 *usedlen += 2;
25109 }
25110
25111 /* ":s?pat?foo?" - substitute */
25112 /* ":gs?pat?foo?" - global substitute */
25113 if (src[*usedlen] == ':'
25114 && (src[*usedlen + 1] == 's'
25115 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25116 {
25117 char_u *str;
25118 char_u *pat;
25119 char_u *sub;
25120 int sep;
25121 char_u *flags;
25122 int didit = FALSE;
25123
25124 flags = (char_u *)"";
25125 s = src + *usedlen + 2;
25126 if (src[*usedlen + 1] == 'g')
25127 {
25128 flags = (char_u *)"g";
25129 ++s;
25130 }
25131
25132 sep = *s++;
25133 if (sep)
25134 {
25135 /* find end of pattern */
25136 p = vim_strchr(s, sep);
25137 if (p != NULL)
25138 {
25139 pat = vim_strnsave(s, (int)(p - s));
25140 if (pat != NULL)
25141 {
25142 s = p + 1;
25143 /* find end of substitution */
25144 p = vim_strchr(s, sep);
25145 if (p != NULL)
25146 {
25147 sub = vim_strnsave(s, (int)(p - s));
25148 str = vim_strnsave(*fnamep, *fnamelen);
25149 if (sub != NULL && str != NULL)
25150 {
25151 *usedlen = (int)(p + 1 - src);
25152 s = do_string_sub(str, pat, sub, flags);
25153 if (s != NULL)
25154 {
25155 *fnamep = s;
25156 *fnamelen = (int)STRLEN(s);
25157 vim_free(*bufp);
25158 *bufp = s;
25159 didit = TRUE;
25160 }
25161 }
25162 vim_free(sub);
25163 vim_free(str);
25164 }
25165 vim_free(pat);
25166 }
25167 }
25168 /* after using ":s", repeat all the modifiers */
25169 if (didit)
25170 goto repeat;
25171 }
25172 }
25173
Bram Moolenaar26df0922014-02-23 23:39:13 +010025174 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25175 {
25176 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25177 if (p == NULL)
25178 return -1;
25179 vim_free(*bufp);
25180 *bufp = *fnamep = p;
25181 *fnamelen = (int)STRLEN(p);
25182 *usedlen += 2;
25183 }
25184
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185 return valid;
25186}
25187
25188/*
25189 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25190 * "flags" can be "g" to do a global substitute.
25191 * Returns an allocated string, NULL for error.
25192 */
25193 char_u *
25194do_string_sub(str, pat, sub, flags)
25195 char_u *str;
25196 char_u *pat;
25197 char_u *sub;
25198 char_u *flags;
25199{
25200 int sublen;
25201 regmatch_T regmatch;
25202 int i;
25203 int do_all;
25204 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025205 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 garray_T ga;
25207 char_u *ret;
25208 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025209 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210
25211 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25212 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025213 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214
25215 ga_init2(&ga, 1, 200);
25216
25217 do_all = (flags[0] == 'g');
25218
25219 regmatch.rm_ic = p_ic;
25220 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25221 if (regmatch.regprog != NULL)
25222 {
25223 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025224 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25226 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025227 /* Skip empty match except for first match. */
25228 if (regmatch.startp[0] == regmatch.endp[0])
25229 {
25230 if (zero_width == regmatch.startp[0])
25231 {
25232 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025233 i = MB_PTR2LEN(tail);
25234 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25235 (size_t)i);
25236 ga.ga_len += i;
25237 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025238 continue;
25239 }
25240 zero_width = regmatch.startp[0];
25241 }
25242
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243 /*
25244 * Get some space for a temporary buffer to do the substitution
25245 * into. It will contain:
25246 * - The text up to where the match is.
25247 * - The substituted text.
25248 * - The text after the match.
25249 */
25250 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025251 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25253 {
25254 ga_clear(&ga);
25255 break;
25256 }
25257
25258 /* copy the text up to where the match is */
25259 i = (int)(regmatch.startp[0] - tail);
25260 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25261 /* add the substituted text */
25262 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25263 + ga.ga_len + i, TRUE, TRUE, FALSE);
25264 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025265 tail = regmatch.endp[0];
25266 if (*tail == NUL)
25267 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268 if (!do_all)
25269 break;
25270 }
25271
25272 if (ga.ga_data != NULL)
25273 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25274
Bram Moolenaar473de612013-06-08 18:19:48 +020025275 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276 }
25277
25278 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25279 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025280 if (p_cpo == empty_option)
25281 p_cpo = save_cpo;
25282 else
25283 /* Darn, evaluating {sub} expression changed the value. */
25284 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285
25286 return ret;
25287}
25288
25289#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */