blob: 2f7f3f39acc9efb5bebaa37feb450796d6b9ad79 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000367};
368
369/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000370#define vv_type vv_di.di_tv.v_type
371#define vv_nr vv_di.di_tv.vval.v_number
372#define vv_float vv_di.di_tv.vval.v_float
373#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000374#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000376
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200377static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000378#define vimvarht vimvardict.dv_hashtab
379
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
381static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
383static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
384static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000385static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
386static void list_glob_vars __ARGS((int *first));
387static void list_buf_vars __ARGS((int *first));
388static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_vim_vars __ARGS((int *first));
393static void list_script_vars __ARGS((int *first));
394static void list_func_vars __ARGS((int *first));
395static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
397static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100398static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000399static void clear_lval __ARGS((lval_T *lp));
400static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
401static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
403static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
404static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
405static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
406static void item_lock __ARGS((typval_T *tv, int deep, int lock));
407static int tv_islocked __ARGS((typval_T *tv));
408
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
410static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000415static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
416static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000417
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000418static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000423static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100425static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
426static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
427static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000428static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000430static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
432static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000433static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100435static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000437static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200438static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
440static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000441static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
446static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000448#ifdef FEAT_FLOAT
449static int string2float __ARGS((char_u *text, float_T *value));
450#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
452static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100453static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200455static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000456static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000457static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000459#ifdef FEAT_FLOAT
460static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200461static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100464static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200468static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200471static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000472static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200473static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100484static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100486static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000487static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#ifdef FEAT_FLOAT
489static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
490#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000491static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000494static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000496#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000497static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
500#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#ifdef FEAT_FLOAT
504static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200505static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000506#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
510static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200520static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200522#ifdef FEAT_FLOAT
523static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
524#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000527static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000528static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000533#ifdef FEAT_FLOAT
534static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200536static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000537#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000538static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000547static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000549static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000555static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200556static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200566static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000567static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000568static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200571static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000572static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100578static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000581static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000595static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100600static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000602static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200615static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000616static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
617#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200618#ifdef FEAT_LUA
619static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200626static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000634#ifdef vim_mkdir
635static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100638#ifdef FEAT_MZSCHEME
639static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100643static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000644static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#ifdef FEAT_FLOAT
646static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000649static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000650static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200651#ifdef FEAT_PYTHON3
652static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
654#ifdef FEAT_PYTHON
655static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200674static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100676static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000679static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000681static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000688static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000689static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000690static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000691static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200693static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000694static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100696#ifdef FEAT_CRYPT
697static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
698#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000699static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200700static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200704static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000707static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000708static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#ifdef FEAT_FLOAT
712static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000715static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717#ifdef HAVE_STRFTIME
718static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
719#endif
720static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200726static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000733static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200734static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200736static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000737static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000738static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000739static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000740static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000741static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000743static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200744#ifdef FEAT_FLOAT
745static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
752static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
753#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200755static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200756static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100757static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100761static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000768static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100772static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773
Bram Moolenaar493c1782014-05-28 14:34:46 +0200774static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000775static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static int get_env_len __ARGS((char_u **arg));
777static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000779static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
780#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
781#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
782 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000783static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000785static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000787static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static typval_T *alloc_tv __ARGS((void));
789static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static void init_tv __ARGS((typval_T *varp));
791static long get_tv_number __ARGS((typval_T *varp));
792static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000793static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *get_tv_string __ARGS((typval_T *varp));
795static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000796static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100797static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
798static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
800static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
801static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000802static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
803static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200805static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
806static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200807static int var_check_func_name __ARGS((char_u *name, int new_var));
808static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200809static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000810static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
812static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
813static int eval_fname_script __ARGS((char_u *p));
814static int eval_fname_sid __ARGS((char_u *p));
815static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static ufunc_T *find_func __ARGS((char_u *name));
817static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200818static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
820static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000821static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
822static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
823static int
824# ifdef __BORLANDC__
825 _RTLENTRYF
826# endif
827 prof_total_cmp __ARGS((const void *s1, const void *s2));
828static int
829# ifdef __BORLANDC__
830 _RTLENTRYF
831# endif
832 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200834static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000835static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000836static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000838static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000839static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
840static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000841static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000842static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
843static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000844static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000845static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000846static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200847static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200848static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200850
851#ifdef EBCDIC
852static int compare_func_name __ARGS((const void *s1, const void *s2));
853static void sortFunctions __ARGS(());
854#endif
855
Bram Moolenaar33570922005-01-25 22:26:29 +0000856/*
857 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000858 */
859 void
860eval_init()
861{
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 int i;
863 struct vimvar *p;
864
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200865 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
866 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200867 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000869 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870
871 for (i = 0; i < VV_LEN; ++i)
872 {
873 p = &vimvars[i];
874 STRCPY(p->vv_di.di_key, p->vv_name);
875 if (p->vv_flags & VV_RO)
876 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
877 else if (p->vv_flags & VV_RO_SBX)
878 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
879 else
880 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000881
882 /* add to v: scope dict, unless the value is not always available */
883 if (p->vv_type != VAR_UNKNOWN)
884 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000886 /* add to compat scope dict */
887 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000889 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100890 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200891 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200892
893#ifdef EBCDIC
894 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100895 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200896 */
897 sortFunctions();
898#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000899}
900
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000901#if defined(EXITFREE) || defined(PROTO)
902 void
903eval_clear()
904{
905 int i;
906 struct vimvar *p;
907
908 for (i = 0; i < VV_LEN; ++i)
909 {
910 p = &vimvars[i];
911 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000913 vim_free(p->vv_str);
914 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000915 }
916 else if (p->vv_di.di_tv.v_type == VAR_LIST)
917 {
918 list_unref(p->vv_list);
919 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921 }
922 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000923 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924 hash_clear(&compat_hashtab);
925
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100927# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200928 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100929# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930
931 /* global variables */
932 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000933
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000934 /* autoloaded script names */
935 ga_clear_strings(&ga_loaded);
936
Bram Moolenaarcca74132013-09-25 21:00:28 +0200937 /* Script-local variables. First clear all the variables and in a second
938 * loop free the scriptvar_T, because a variable in one script might hold
939 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200940 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200941 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200942 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200943 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200944 ga_clear(&ga_scripts);
945
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000946 /* unreferenced lists and dicts */
947 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000948
949 /* functions */
950 free_all_functions();
951 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000952}
953#endif
954
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * Return the name of the executed function.
957 */
958 char_u *
959func_name(cookie)
960 void *cookie;
961{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000962 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the next breakpoint line for a funccall cookie.
967 */
968 linenr_T *
969func_breakpoint(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the address holding the debug tick for a funccall cookie.
977 */
978 int *
979func_dbg_tick(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/*
986 * Return the nesting level for a funccall cookie.
987 */
988 int
989func_level(cookie)
990 void *cookie;
991{
Bram Moolenaar33570922005-01-25 22:26:29 +0000992 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993}
994
995/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000996funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000998/* pointer to list of previously used funccal, still around because some
999 * item in it is still being used. */
1000funccall_T *previous_funccal = NULL;
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * Return TRUE when a function was ended by a ":return" command.
1004 */
1005 int
1006current_func_returned()
1007{
1008 return current_funccal->returned;
1009}
1010
1011
1012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 * Set an internal variable to a string value. Creates the variable if it does
1014 * not already exist.
1015 */
1016 void
1017set_internal_string_var(name, value)
1018 char_u *name;
1019 char_u *value;
1020{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001022 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 val = vim_strsave(value);
1025 if (val != NULL)
1026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001028 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001030 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 }
1034}
1035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001037static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038static char_u *redir_endp = NULL;
1039static char_u *redir_varname = NULL;
1040
1041/*
1042 * Start recording command output to a variable
1043 * Returns OK if successfully completed the setup. FAIL otherwise.
1044 */
1045 int
1046var_redir_start(name, append)
1047 char_u *name;
1048 int append; /* append to an existing variable */
1049{
1050 int save_emsg;
1051 int err;
1052 typval_T tv;
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 {
1057 EMSG(_(e_invarg));
1058 return FAIL;
1059 }
1060
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001061 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 redir_varname = vim_strsave(name);
1063 if (redir_varname == NULL)
1064 return FAIL;
1065
1066 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1067 if (redir_lval == NULL)
1068 {
1069 var_redir_stop();
1070 return FAIL;
1071 }
1072
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001073 /* The output is stored in growarray "redir_ga" until redirection ends. */
1074 ga_init2(&redir_ga, (int)sizeof(char), 500);
1075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001077 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001078 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1080 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001081 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 if (redir_endp != NULL && *redir_endp != NUL)
1083 /* Trailing characters are present after the variable name */
1084 EMSG(_(e_trailing));
1085 else
1086 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
1091
1092 /* check if we can write to the variable: set it to or append an empty
1093 * string */
1094 save_emsg = did_emsg;
1095 did_emsg = FALSE;
1096 tv.v_type = VAR_STRING;
1097 tv.vval.v_string = (char_u *)"";
1098 if (append)
1099 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1100 else
1101 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001102 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001104 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 if (err)
1106 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 var_redir_stop();
1109 return FAIL;
1110 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 * Append "value[value_len]" to the variable set by var_redir_start().
1117 * The actual appending is postponed until redirection ends, because the value
1118 * appended may in fact be the string we write to, changing it may cause freed
1119 * memory to be used:
1120 * :redir => foo
1121 * :let foo
1122 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 */
1124 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130
1131 if (redir_lval == NULL)
1132 return;
1133
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001139 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 {
1141 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001142 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 }
1144 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146}
1147
1148/*
1149 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 */
1152 void
1153var_redir_stop()
1154{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 typval_T tv;
1156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 if (redir_lval != NULL)
1158 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* If there was no error: assign the text to the variable. */
1160 if (redir_endp != NULL)
1161 {
1162 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1163 tv.v_type = VAR_STRING;
1164 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 /* Call get_lval() again, if it's inside a Dict or List it may
1166 * have changed. */
1167 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001168 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001169 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1170 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1171 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001174 /* free the collected output */
1175 vim_free(redir_ga.ga_data);
1176 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 vim_free(redir_lval);
1179 redir_lval = NULL;
1180 }
1181 vim_free(redir_varname);
1182 redir_varname = NULL;
1183}
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185# if defined(FEAT_MBYTE) || defined(PROTO)
1186 int
1187eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1188 char_u *enc_from;
1189 char_u *enc_to;
1190 char_u *fname_from;
1191 char_u *fname_to;
1192{
1193 int err = FALSE;
1194
1195 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1196 set_vim_var_string(VV_CC_TO, enc_to, -1);
1197 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1198 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1199 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1200 err = TRUE;
1201 set_vim_var_string(VV_CC_FROM, NULL, -1);
1202 set_vim_var_string(VV_CC_TO, NULL, -1);
1203 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1204 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1205
1206 if (err)
1207 return FAIL;
1208 return OK;
1209}
1210# endif
1211
1212# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1213 int
1214eval_printexpr(fname, args)
1215 char_u *fname;
1216 char_u *args;
1217{
1218 int err = FALSE;
1219
1220 set_vim_var_string(VV_FNAME_IN, fname, -1);
1221 set_vim_var_string(VV_CMDARG, args, -1);
1222 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1223 err = TRUE;
1224 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1225 set_vim_var_string(VV_CMDARG, NULL, -1);
1226
1227 if (err)
1228 {
1229 mch_remove(fname);
1230 return FAIL;
1231 }
1232 return OK;
1233}
1234# endif
1235
1236# if defined(FEAT_DIFF) || defined(PROTO)
1237 void
1238eval_diff(origfile, newfile, outfile)
1239 char_u *origfile;
1240 char_u *newfile;
1241 char_u *outfile;
1242{
1243 int err = FALSE;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253
1254 void
1255eval_patch(origfile, difffile, outfile)
1256 char_u *origfile;
1257 char_u *difffile;
1258 char_u *outfile;
1259{
1260 int err;
1261
1262 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1263 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1264 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1265 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1266 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1267 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1268 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1269}
1270# endif
1271
1272/*
1273 * Top level evaluation function, returning a boolean.
1274 * Sets "error" to TRUE if there was an error.
1275 * Return TRUE or FALSE.
1276 */
1277 int
1278eval_to_bool(arg, error, nextcmd, skip)
1279 char_u *arg;
1280 int *error;
1281 char_u **nextcmd;
1282 int skip; /* only parse, don't execute */
1283{
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int retval = FALSE;
1286
1287 if (skip)
1288 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001289 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 else
1292 {
1293 *error = FALSE;
1294 if (!skip)
1295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001296 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 }
1300 if (skip)
1301 --emsg_skip;
1302
1303 return retval;
1304}
1305
1306/*
1307 * Top level evaluation function, returning a string. If "skip" is TRUE,
1308 * only parsing to "nextcmd" is done, without reporting errors. Return
1309 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1310 */
1311 char_u *
1312eval_to_string_skip(arg, nextcmd, skip)
1313 char_u *arg;
1314 char_u **nextcmd;
1315 int skip; /* only parse, don't execute */
1316{
Bram Moolenaar33570922005-01-25 22:26:29 +00001317 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 char_u *retval;
1319
1320 if (skip)
1321 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 retval = NULL;
1324 else
1325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 retval = vim_strsave(get_tv_string(&tv));
1327 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 if (skip)
1330 --emsg_skip;
1331
1332 return retval;
1333}
1334
1335/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001336 * Skip over an expression at "*pp".
1337 * Return FAIL for an error, OK otherwise.
1338 */
1339 int
1340skip_expr(pp)
1341 char_u **pp;
1342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344
1345 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001347}
1348
1349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001351 * When "convert" is TRUE convert a List into a sequence of lines and convert
1352 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * Return pointer to allocated memory, or NULL for failure.
1354 */
1355 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 char_u *arg;
1358 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001364#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 retval = NULL;
1370 else
1371 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001373 {
1374 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001375 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001376 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001377 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001378 if (tv.vval.v_list->lv_len > 0)
1379 ga_append(&ga, NL);
1380 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 ga_append(&ga, NUL);
1382 retval = (char_u *)ga.ga_data;
1383 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384#ifdef FEAT_FLOAT
1385 else if (convert && tv.v_type == VAR_FLOAT)
1386 {
1387 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1388 retval = vim_strsave(numbuf);
1389 }
1390#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 else
1392 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395
1396 return retval;
1397}
1398
1399/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 * Call eval_to_string() without using current local variables and using
1401 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
1403 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 char_u *arg;
1406 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 char_u *retval;
1410 void *save_funccalp;
1411
1412 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 if (use_sandbox)
1414 ++sandbox;
1415 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001417 if (use_sandbox)
1418 --sandbox;
1419 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 restore_funccal(save_funccalp);
1421 return retval;
1422}
1423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424/*
1425 * Top level evaluation function, returning a number.
1426 * Evaluates "expr" silently.
1427 * Returns -1 for an error.
1428 */
1429 int
1430eval_to_number(expr)
1431 char_u *expr;
1432{
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001435 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 ++emsg_off;
1438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 retval = -1;
1441 else
1442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 --emsg_off;
1447
1448 return retval;
1449}
1450
Bram Moolenaara40058a2005-07-11 22:42:07 +00001451/*
1452 * Prepare v: variable "idx" to be used.
1453 * Save the current typeval in "save_tv".
1454 * When not used yet add the variable to the v: hashtable.
1455 */
1456 static void
1457prepare_vimvar(idx, save_tv)
1458 int idx;
1459 typval_T *save_tv;
1460{
1461 *save_tv = vimvars[idx].vv_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1464}
1465
1466/*
1467 * Restore v: variable "idx" to typeval "save_tv".
1468 * When no longer defined, remove the variable from the v: hashtable.
1469 */
1470 static void
1471restore_vimvar(idx, save_tv)
1472 int idx;
1473 typval_T *save_tv;
1474{
1475 hashitem_T *hi;
1476
Bram Moolenaara40058a2005-07-11 22:42:07 +00001477 vimvars[idx].vv_tv = *save_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 {
1480 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1481 if (HASHITEM_EMPTY(hi))
1482 EMSG2(_(e_intern2), "restore_vimvar()");
1483 else
1484 hash_remove(&vimvarht, hi);
1485 }
1486}
1487
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001488#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001489/*
1490 * Evaluate an expression to a list with suggestions.
1491 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001492 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001493 */
1494 list_T *
1495eval_spell_expr(badword, expr)
1496 char_u *badword;
1497 char_u *expr;
1498{
1499 typval_T save_val;
1500 typval_T rettv;
1501 list_T *list = NULL;
1502 char_u *p = skipwhite(expr);
1503
1504 /* Set "v:val" to the bad word. */
1505 prepare_vimvar(VV_VAL, &save_val);
1506 vimvars[VV_VAL].vv_type = VAR_STRING;
1507 vimvars[VV_VAL].vv_str = badword;
1508 if (p_verbose == 0)
1509 ++emsg_off;
1510
1511 if (eval1(&p, &rettv, TRUE) == OK)
1512 {
1513 if (rettv.v_type != VAR_LIST)
1514 clear_tv(&rettv);
1515 else
1516 list = rettv.vval.v_list;
1517 }
1518
1519 if (p_verbose == 0)
1520 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001521 restore_vimvar(VV_VAL, &save_val);
1522
1523 return list;
1524}
1525
1526/*
1527 * "list" is supposed to contain two items: a word and a number. Return the
1528 * word in "pp" and the number as the return value.
1529 * Return -1 if anything isn't right.
1530 * Used to get the good word and score from the eval_spell_expr() result.
1531 */
1532 int
1533get_spellword(list, pp)
1534 list_T *list;
1535 char_u **pp;
1536{
1537 listitem_T *li;
1538
1539 li = list->lv_first;
1540 if (li == NULL)
1541 return -1;
1542 *pp = get_tv_string(&li->li_tv);
1543
1544 li = li->li_next;
1545 if (li == NULL)
1546 return -1;
1547 return get_tv_number(&li->li_tv);
1548}
1549#endif
1550
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001551/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 * Top level evaluation function.
1553 * Returns an allocated typval_T with the result.
1554 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 */
1556 typval_T *
1557eval_expr(arg, nextcmd)
1558 char_u *arg;
1559 char_u **nextcmd;
1560{
1561 typval_T *tv;
1562
1563 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001564 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001565 {
1566 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001567 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568 }
1569
1570 return tv;
1571}
1572
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576 * Uses argv[argc] for the function arguments. Only Number and String
1577 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001580 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001581call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 char_u *func;
1583 int argc;
1584 char_u **argv;
1585 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001586 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar33570922005-01-25 22:26:29 +00001589 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 long n;
1591 int len;
1592 int i;
1593 int doesrange;
1594 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001597 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 for (i = 0; i < argc; i++)
1602 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001603 /* Pass a NULL or empty argument as an empty string */
1604 if (argv[i] == NULL || *argv[i] == NUL)
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001608 continue;
1609 }
1610
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001611 if (str_arg_only)
1612 len = 0;
1613 else
1614 /* Recognize a number argument, the others must be strings. */
1615 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (len != 0 && len == (int)STRLEN(argv[i]))
1617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001618 argvars[i].v_type = VAR_NUMBER;
1619 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 else
1622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001623 argvars[i].v_type = VAR_STRING;
1624 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626 }
1627
1628 if (safe)
1629 {
1630 save_funccalp = save_funccal();
1631 ++sandbox;
1632 }
1633
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1635 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (safe)
1639 {
1640 --sandbox;
1641 restore_funccal(save_funccalp);
1642 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 vim_free(argvars);
1644
1645 if (ret == FAIL)
1646 clear_tv(rettv);
1647
1648 return ret;
1649}
1650
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001651/*
1652 * Call vimL function "func" and return the result as a number.
1653 * Returns -1 when calling the function fails.
1654 * Uses argv[argc] for the function arguments.
1655 */
1656 long
1657call_func_retnr(func, argc, argv, safe)
1658 char_u *func;
1659 int argc;
1660 char_u **argv;
1661 int safe; /* use the sandbox */
1662{
1663 typval_T rettv;
1664 long retval;
1665
1666 /* All arguments are passed as strings, no conversion to number. */
1667 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1668 return -1;
1669
1670 retval = get_tv_number_chk(&rettv, NULL);
1671 clear_tv(&rettv);
1672 return retval;
1673}
1674
1675#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1676 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1677
Bram Moolenaar4f688582007-07-24 12:34:30 +00001678# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001680 * Call vimL function "func" and return the result as a string.
1681 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 * Uses argv[argc] for the function arguments.
1683 */
1684 void *
1685call_func_retstr(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001692 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001694 /* All arguments are passed as strings, no conversion to number. */
1695 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 return NULL;
1697
1698 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return retval;
1701}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001705 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001707 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 */
1709 void *
1710call_func_retlist(func, argc, argv, safe)
1711 char_u *func;
1712 int argc;
1713 char_u **argv;
1714 int safe; /* use the sandbox */
1715{
1716 typval_T rettv;
1717
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001718 /* All arguments are passed as strings, no conversion to number. */
1719 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 return NULL;
1721
1722 if (rettv.v_type != VAR_LIST)
1723 {
1724 clear_tv(&rettv);
1725 return NULL;
1726 }
1727
1728 return rettv.vval.v_list;
1729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
1732/*
1733 * Save the current function call pointer, and set it to NULL.
1734 * Used when executing autocommands and for ":source".
1735 */
1736 void *
1737save_funccal()
1738{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 current_funccal = NULL;
1742 return (void *)fc;
1743}
1744
1745 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001746restore_funccal(vfc)
1747 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001749 funccall_T *fc = (funccall_T *)vfc;
1750
1751 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754#if defined(FEAT_PROFILE) || defined(PROTO)
1755/*
1756 * Prepare profiling for entering a child or something else that is not
1757 * counted for the script/function itself.
1758 * Should always be called in pair with prof_child_exit().
1759 */
1760 void
1761prof_child_enter(tm)
1762 proftime_T *tm; /* place to store waittime */
1763{
1764 funccall_T *fc = current_funccal;
1765
1766 if (fc != NULL && fc->func->uf_profiling)
1767 profile_start(&fc->prof_child);
1768 script_prof_save(tm);
1769}
1770
1771/*
1772 * Take care of time spent in a child.
1773 * Should always be called after prof_child_enter().
1774 */
1775 void
1776prof_child_exit(tm)
1777 proftime_T *tm; /* where waittime was stored */
1778{
1779 funccall_T *fc = current_funccal;
1780
1781 if (fc != NULL && fc->func->uf_profiling)
1782 {
1783 profile_end(&fc->prof_child);
1784 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1785 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1786 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1787 }
1788 script_prof_restore(tm);
1789}
1790#endif
1791
1792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_FOLDING
1794/*
1795 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1796 * it in "*cp". Doesn't give error messages.
1797 */
1798 int
1799eval_foldexpr(arg, cp)
1800 char_u *arg;
1801 int *cp;
1802{
Bram Moolenaar33570922005-01-25 22:26:29 +00001803 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 int retval;
1805 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001806 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1807 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 ++sandbox;
1812 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 if (tv.v_type == VAR_NUMBER)
1820 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001821 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 retval = 0;
1823 else
1824 {
1825 /* If the result is a string, check if there is a non-digit before
1826 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!VIM_ISDIGIT(*s) && *s != '-')
1829 *cp = *s++;
1830 retval = atol((char *)s);
1831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001835 if (use_sandbox)
1836 --sandbox;
1837 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 return retval;
1840}
1841#endif
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 * ":let" list all variable values
1845 * ":let var1 var2" list variable values
1846 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 * ":let var += expr" assignment command.
1848 * ":let var -= expr" assignment command.
1849 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 */
1852 void
1853ex_let(eap)
1854 exarg_T *eap;
1855{
1856 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 int var_count = 0;
1861 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001863 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaardb552d602006-03-23 22:59:57 +00001866 argend = skip_var_list(arg, &var_count, &semicolon);
1867 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001869 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1870 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001871 expr = skipwhite(argend);
1872 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1873 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001875 /*
1876 * ":let" without "=": list variables
1877 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 if (*arg == '[')
1879 EMSG(_(e_invarg));
1880 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001886 list_glob_vars(&first);
1887 list_buf_vars(&first);
1888 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001889#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001891#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001892 list_script_vars(&first);
1893 list_func_vars(&first);
1894 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 eap->nextcmd = check_nextcmd(arg);
1897 }
1898 else
1899 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op[0] = '=';
1901 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1905 op[0] = *expr; /* +=, -= or .= */
1906 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 else
1909 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (eap->skip)
1912 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (eap->skip)
1915 {
1916 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 --emsg_skip;
1919 }
1920 else if (i != FAIL)
1921 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 }
1927}
1928
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929/*
1930 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1931 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 * When "nextchars" is not NULL it points to a string with characters that
1933 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1934 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 * Returns OK or FAIL;
1936 */
1937 static int
1938ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1939 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int copy; /* copy values from "tv", don't move */
1942 int semicolon; /* from skip_var_list() */
1943 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945{
1946 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 listitem_T *item;
1950 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951
1952 if (*arg != '[')
1953 {
1954 /*
1955 * ":let var = expr" or ":for var in list"
1956 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 return OK;
1960 }
1961
1962 /*
1963 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1964 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001965 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 {
1967 EMSG(_(e_listreq));
1968 return FAIL;
1969 }
1970
1971 i = list_len(l);
1972 if (semicolon == 0 && var_count < i)
1973 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001974 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 return FAIL;
1976 }
1977 if (var_count - semicolon > i)
1978 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001979 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 return FAIL;
1981 }
1982
1983 item = l->lv_first;
1984 while (*arg != ']')
1985 {
1986 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 item = item->li_next;
1989 if (arg == NULL)
1990 return FAIL;
1991
1992 arg = skipwhite(arg);
1993 if (*arg == ';')
1994 {
1995 /* Put the rest of the list (may be empty) in the var after ';'.
1996 * Create a new list for this. */
1997 l = list_alloc();
1998 if (l == NULL)
1999 return FAIL;
2000 while (item != NULL)
2001 {
2002 list_append_tv(l, &item->li_tv);
2003 item = item->li_next;
2004 }
2005
2006 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002007 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 ltv.vval.v_list = l;
2009 l->lv_refcount = 1;
2010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2012 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 clear_tv(&ltv);
2014 if (arg == NULL)
2015 return FAIL;
2016 break;
2017 }
2018 else if (*arg != ',' && *arg != ']')
2019 {
2020 EMSG2(_(e_intern2), "ex_let_vars()");
2021 return FAIL;
2022 }
2023 }
2024
2025 return OK;
2026}
2027
2028/*
2029 * Skip over assignable variable "var" or list of variables "[var, var]".
2030 * Used for ":let varvar = expr" and ":for varvar in expr".
2031 * For "[var, var]" increment "*var_count" for each variable.
2032 * for "[var, var; var]" set "semicolon".
2033 * Return NULL for an error.
2034 */
2035 static char_u *
2036skip_var_list(arg, var_count, semicolon)
2037 char_u *arg;
2038 int *var_count;
2039 int *semicolon;
2040{
2041 char_u *p, *s;
2042
2043 if (*arg == '[')
2044 {
2045 /* "[var, var]": find the matching ']'. */
2046 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002047 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 {
2049 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2050 s = skip_var_one(p);
2051 if (s == p)
2052 {
2053 EMSG2(_(e_invarg2), p);
2054 return NULL;
2055 }
2056 ++*var_count;
2057
2058 p = skipwhite(s);
2059 if (*p == ']')
2060 break;
2061 else if (*p == ';')
2062 {
2063 if (*semicolon == 1)
2064 {
2065 EMSG(_("Double ; in list of variables"));
2066 return NULL;
2067 }
2068 *semicolon = 1;
2069 }
2070 else if (*p != ',')
2071 {
2072 EMSG2(_(e_invarg2), p);
2073 return NULL;
2074 }
2075 }
2076 return p + 1;
2077 }
2078 else
2079 return skip_var_one(arg);
2080}
2081
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002083 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002084 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 static char_u *
2087skip_var_one(arg)
2088 char_u *arg;
2089{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002090 if (*arg == '@' && arg[1] != NUL)
2091 return arg + 2;
2092 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2093 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094}
2095
Bram Moolenaara7043832005-01-21 11:56:39 +00002096/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 * List variables for hashtab "ht" with prefix "prefix".
2098 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002104 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 hashitem_T *hi;
2108 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 int todo;
2110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002111 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2113 {
2114 if (!HASHITEM_EMPTY(hi))
2115 {
2116 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 di = HI2DI(hi);
2118 if (empty || di->di_tv.v_type != VAR_STRING
2119 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
2122 }
2123}
2124
2125/*
2126 * List global variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_glob_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
2135/*
2136 * List buffer variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_buf_vars(first)
2140 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142 char_u numbuf[NUMBUFLEN];
2143
Bram Moolenaar429fa852013-04-15 12:27:36 +02002144 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146
2147 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2149 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
2152/*
2153 * List window variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_win_vars(first)
2157 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002159 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161}
2162
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163#ifdef FEAT_WINDOWS
2164/*
2165 * List tab page variables.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_tab_vars(first)
2169 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002171 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173}
2174#endif
2175
Bram Moolenaara7043832005-01-21 11:56:39 +00002176/*
2177 * List Vim variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_vim_vars(first)
2181 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184}
2185
2186/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002187 * List script-local variables, if there is a script.
2188 */
2189 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_script_vars(first)
2191 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192{
2193 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2195 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196}
2197
2198/*
2199 * List function variables, if there is a function.
2200 */
2201 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202list_func_vars(first)
2203 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204{
2205 if (current_funccal != NULL)
2206 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208}
2209
2210/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 * List variables in "arg".
2212 */
2213 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 exarg_T *eap;
2216 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002260 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
2519check_changedtick(arg)
2520 char_u *arg;
2521{
2522 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2523 {
2524 EMSG2(_(e_readonlyvar), arg);
2525 return TRUE;
2526 }
2527 return FALSE;
2528}
2529
2530/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 * Get an lval: variable, Dict item or List item that can be assigned a value
2532 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2533 * "name.key", "name.key[expr]" etc.
2534 * Indexing only works if "name" is an existing List or Dictionary.
2535 * "name" points to the start of the name.
2536 * If "rettv" is not NULL it points to the value to be assigned.
2537 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2538 * wrong; must end in space or cmd separator.
2539 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002540 * flags:
2541 * GLV_QUIET: do not give error messages
2542 * GLV_NO_AUTOLOAD: do not use script autoloading
2543 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002545 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 */
2548 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002549get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 typval_T *rettv;
2552 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 int unlet;
2554 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002555 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002556 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 char_u *expr_start, *expr_end;
2560 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 dictitem_T *v;
2562 typval_T var1;
2563 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573
2574 if (skip)
2575 {
2576 /* When skipping just find the end of the name. */
2577 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002578 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 }
2580
2581 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002582 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (expr_start != NULL)
2584 {
2585 /* Don't expand the name when we already know there is an error. */
2586 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2587 && *p != '[' && *p != '.')
2588 {
2589 EMSG(_(e_trailing));
2590 return NULL;
2591 }
2592
2593 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2594 if (lp->ll_exp_name == NULL)
2595 {
2596 /* Report an invalid expression in braces, unless the
2597 * expression evaluation has been cancelled due to an
2598 * aborting error, an interrupt, or an exception. */
2599 if (!aborting() && !quiet)
2600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002601 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 EMSG2(_(e_invarg2), name);
2603 return NULL;
2604 }
2605 }
2606 lp->ll_name = lp->ll_exp_name;
2607 }
2608 else
2609 lp->ll_name = name;
2610
2611 /* Without [idx] or .key we are done. */
2612 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2613 return p;
2614
2615 cc = *p;
2616 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002617 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (v == NULL && !quiet)
2619 EMSG2(_(e_undefvar), lp->ll_name);
2620 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 if (v == NULL)
2622 return NULL;
2623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /*
2625 * Loop until no more [idx] or .key is following.
2626 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2631 && !(lp->ll_tv->v_type == VAR_DICT
2632 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
2635 EMSG(_("E689: Can only index a List or Dictionary"));
2636 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (!quiet)
2641 EMSG(_("E708: [:] must come last"));
2642 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 len = -1;
2646 if (*p == '.')
2647 {
2648 key = p + 1;
2649 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2650 ;
2651 if (len == 0)
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_(e_emptykey));
2655 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657 p = key + len;
2658 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 else
2660 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 if (*p == ':')
2664 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 else
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 empty1 = FALSE;
2668 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 if (get_tv_string_chk(&var1) == NULL)
2671 {
2672 /* not a number or string */
2673 clear_tv(&var1);
2674 return NULL;
2675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
2677
2678 /* Optionally get the second index [ :expr]. */
2679 if (*p == ':')
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 if (!empty1)
2686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (rettv != NULL && (rettv->v_type != VAR_LIST
2690 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698 p = skipwhite(p + 1);
2699 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 else
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 if (get_tv_string_chk(&var2) == NULL)
2711 {
2712 /* not a number or string */
2713 if (!empty1)
2714 clear_tv(&var1);
2715 clear_tv(&var2);
2716 return NULL;
2717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
2727 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (!empty1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
2734
2735 /* Skip to past ']'. */
2736 ++p;
2737 }
2738
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
2741 if (len == -1)
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (*key == NUL)
2746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
2748 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
2752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 lp->ll_list = NULL;
2754 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002755 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002757 /* When assigning to a scope dictionary check that a function and
2758 * variable name is valid (only variable name unless it is l: or
2759 * g: dictionary). Disallow overwriting a builtin function. */
2760 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002762 int prevval;
2763 int wrong;
2764
2765 if (len != -1)
2766 {
2767 prevval = key[len];
2768 key[len] = NUL;
2769 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002770 else
2771 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002772 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2773 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 || !valid_varname(key);
2776 if (len != -1)
2777 key[len] = prevval;
2778 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779 return NULL;
2780 }
2781
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002784 /* Can't add "v:" variable. */
2785 if (lp->ll_dict == &vimvardict)
2786 {
2787 EMSG2(_(e_illvar), name);
2788 return NULL;
2789 }
2790
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002791 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 if (len == -1)
2797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 p = NULL;
2808 break;
2809 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002811 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002812 return NULL;
2813
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 if (len == -1)
2815 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 }
2818 else
2819 {
2820 /*
2821 * Get the number and item for the only or first index of the List.
2822 */
2823 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 else
2826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var1);
2829 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002830 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_list = lp->ll_tv->vval.v_list;
2832 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2833 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002835 if (lp->ll_n1 < 0)
2836 {
2837 lp->ll_n1 = 0;
2838 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2839 }
2840 }
2841 if (lp->ll_li == NULL)
2842 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 if (!quiet)
2846 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 }
2849
2850 /*
2851 * May need to find the item or absolute index for the second
2852 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 * When no index given: "lp->ll_empty2" is TRUE.
2854 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002858 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 {
2865 if (!quiet)
2866 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 }
2871
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2873 if (lp->ll_n1 < 0)
2874 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2875 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002881 }
2882
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002885 }
2886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return p;
2888}
2889
2890/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 */
2893 static void
2894clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 vim_free(lp->ll_exp_name);
2898 vim_free(lp->ll_newkey);
2899}
2900
2901/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 */
2906 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913{
2914 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 listitem_T *ri;
2916 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917
2918 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 cc = *endp;
2923 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 if (op != NULL && *op != '=')
2925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002929 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002930 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931 {
2932 if (tv_op(&tv, rettv, op) == OK)
2933 set_var(lp->ll_name, &tv, FALSE);
2934 clear_tv(&tv);
2935 }
2936 }
2937 else
2938 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002942 else if (tv_check_lock(lp->ll_newkey == NULL
2943 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002944 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002945 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 else if (lp->ll_range)
2947 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002948 listitem_T *ll_li = lp->ll_li;
2949 int ll_n1 = lp->ll_n1;
2950
2951 /*
2952 * Check whether any of the list items is locked
2953 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002954 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002955 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002956 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002957 return;
2958 ri = ri->li_next;
2959 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2960 break;
2961 ll_li = ll_li->li_next;
2962 ++ll_n1;
2963 }
2964
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 /*
2966 * Assign the List values to the list items.
2967 */
2968 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002969 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 if (op != NULL && *op != '=')
2971 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2972 else
2973 {
2974 clear_tv(&lp->ll_li->li_tv);
2975 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 ri = ri->li_next;
2978 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2979 break;
2980 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002983 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 ri = NULL;
2986 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 lp->ll_li = lp->ll_li->li_next;
2990 ++lp->ll_n1;
2991 }
2992 if (ri != NULL)
2993 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 else if (lp->ll_empty2
2995 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 : lp->ll_n1 != lp->ll_n2)
2997 EMSG(_("E711: List value has not enough items"));
2998 }
2999 else
3000 {
3001 /*
3002 * Assign to a List or Dictionary item.
3003 */
3004 if (lp->ll_newkey != NULL)
3005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 if (op != NULL && *op != '=')
3007 {
3008 EMSG2(_(e_letwrong), op);
3009 return;
3010 }
3011
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003013 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 if (di == NULL)
3015 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003016 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3017 {
3018 vim_free(di);
3019 return;
3020 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003022 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 else if (op != NULL && *op != '=')
3024 {
3025 tv_op(lp->ll_tv, rettv, op);
3026 return;
3027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003028 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003030
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 /*
3032 * Assign the value to the variable or list item.
3033 */
3034 if (copy)
3035 copy_tv(rettv, lp->ll_tv);
3036 else
3037 {
3038 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003039 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 }
3042 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003043}
3044
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3047 * Returns OK or FAIL.
3048 */
3049 static int
3050tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003051 typval_T *tv1;
3052 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 char_u *op;
3054{
3055 long n;
3056 char_u numbuf[NUMBUFLEN];
3057 char_u *s;
3058
3059 /* Can't do anything with a Funcref or a Dict on the right. */
3060 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3061 {
3062 switch (tv1->v_type)
3063 {
3064 case VAR_DICT:
3065 case VAR_FUNC:
3066 break;
3067
3068 case VAR_LIST:
3069 if (*op != '+' || tv2->v_type != VAR_LIST)
3070 break;
3071 /* List += List */
3072 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3073 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3074 return OK;
3075
3076 case VAR_NUMBER:
3077 case VAR_STRING:
3078 if (tv2->v_type == VAR_LIST)
3079 break;
3080 if (*op == '+' || *op == '-')
3081 {
3082 /* nr += nr or nr -= nr*/
3083 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084#ifdef FEAT_FLOAT
3085 if (tv2->v_type == VAR_FLOAT)
3086 {
3087 float_T f = n;
3088
3089 if (*op == '+')
3090 f += tv2->vval.v_float;
3091 else
3092 f -= tv2->vval.v_float;
3093 clear_tv(tv1);
3094 tv1->v_type = VAR_FLOAT;
3095 tv1->vval.v_float = f;
3096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098#endif
3099 {
3100 if (*op == '+')
3101 n += get_tv_number(tv2);
3102 else
3103 n -= get_tv_number(tv2);
3104 clear_tv(tv1);
3105 tv1->v_type = VAR_NUMBER;
3106 tv1->vval.v_number = n;
3107 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003108 }
3109 else
3110 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111 if (tv2->v_type == VAR_FLOAT)
3112 break;
3113
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 /* str .= str */
3115 s = get_tv_string(tv1);
3116 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3117 clear_tv(tv1);
3118 tv1->v_type = VAR_STRING;
3119 tv1->vval.v_string = s;
3120 }
3121 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122
3123#ifdef FEAT_FLOAT
3124 case VAR_FLOAT:
3125 {
3126 float_T f;
3127
3128 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3129 && tv2->v_type != VAR_NUMBER
3130 && tv2->v_type != VAR_STRING))
3131 break;
3132 if (tv2->v_type == VAR_FLOAT)
3133 f = tv2->vval.v_float;
3134 else
3135 f = get_tv_number(tv2);
3136 if (*op == '+')
3137 tv1->vval.v_float += f;
3138 else
3139 tv1->vval.v_float -= f;
3140 }
3141 return OK;
3142#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003143 }
3144 }
3145
3146 EMSG2(_(e_letwrong), op);
3147 return FAIL;
3148}
3149
3150/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151 * Add a watcher to a list.
3152 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003153 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003155 list_T *l;
3156 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157{
3158 lw->lw_next = l->lv_watch;
3159 l->lv_watch = lw;
3160}
3161
3162/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003163 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 * No warning when it isn't found...
3165 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003166 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 list_T *l;
3169 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170{
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172
3173 lwp = &l->lv_watch;
3174 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3175 {
3176 if (lw == lwrem)
3177 {
3178 *lwp = lw->lw_next;
3179 break;
3180 }
3181 lwp = &lw->lw_next;
3182 }
3183}
3184
3185/*
3186 * Just before removing an item from a list: advance watchers to the next
3187 * item.
3188 */
3189 static void
3190list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 list_T *l;
3192 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193{
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195
3196 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3197 if (lw->lw_item == item)
3198 lw->lw_item = item->li_next;
3199}
3200
3201/*
3202 * Evaluate the expression used in a ":for var in expr" command.
3203 * "arg" points to "var".
3204 * Set "*errp" to TRUE for an error, FALSE otherwise;
3205 * Return a pointer that holds the info. Null when there is an error.
3206 */
3207 void *
3208eval_for_line(arg, errp, nextcmdp, skip)
3209 char_u *arg;
3210 int *errp;
3211 char_u **nextcmdp;
3212 int skip;
3213{
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 typval_T tv;
3217 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218
3219 *errp = TRUE; /* default: there is an error */
3220
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 if (fi == NULL)
3223 return NULL;
3224
3225 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3226 if (expr == NULL)
3227 return fi;
3228
3229 expr = skipwhite(expr);
3230 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3231 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003232 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 return fi;
3234 }
3235
3236 if (skip)
3237 ++emsg_skip;
3238 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3239 {
3240 *errp = FALSE;
3241 if (!skip)
3242 {
3243 l = tv.vval.v_list;
3244 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003245 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003247 clear_tv(&tv);
3248 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 else
3250 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003251 /* No need to increment the refcount, it's already set for the
3252 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 fi->fi_list = l;
3254 list_add_watch(l, &fi->fi_lw);
3255 fi->fi_lw.lw_item = l->lv_first;
3256 }
3257 }
3258 }
3259 if (skip)
3260 --emsg_skip;
3261
3262 return fi;
3263}
3264
3265/*
3266 * Use the first item in a ":for" list. Advance to the next.
3267 * Assign the values to the variable (list). "arg" points to the first one.
3268 * Return TRUE when a valid item was found, FALSE when at end of list or
3269 * something wrong.
3270 */
3271 int
3272next_for_item(fi_void, arg)
3273 void *fi_void;
3274 char_u *arg;
3275{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003276 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279
3280 item = fi->fi_lw.lw_item;
3281 if (item == NULL)
3282 result = FALSE;
3283 else
3284 {
3285 fi->fi_lw.lw_item = item->li_next;
3286 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3287 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3288 }
3289 return result;
3290}
3291
3292/*
3293 * Free the structure used to store info used by ":for".
3294 */
3295 void
3296free_for_info(fi_void)
3297 void *fi_void;
3298{
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003301 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003302 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003304 list_unref(fi->fi_list);
3305 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 vim_free(fi);
3307}
3308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3310
3311 void
3312set_context_for_expression(xp, arg, cmdidx)
3313 expand_T *xp;
3314 char_u *arg;
3315 cmdidx_T cmdidx;
3316{
3317 int got_eq = FALSE;
3318 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 if (cmdidx == CMD_let)
3322 {
3323 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003324 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 {
3326 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003327 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328 {
3329 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331 if (vim_iswhite(*p))
3332 break;
3333 }
3334 return;
3335 }
3336 }
3337 else
3338 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3339 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 while ((xp->xp_pattern = vim_strpbrk(arg,
3341 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3342 {
3343 c = *xp->xp_pattern;
3344 if (c == '&')
3345 {
3346 c = xp->xp_pattern[1];
3347 if (c == '&')
3348 {
3349 ++xp->xp_pattern;
3350 xp->xp_context = cmdidx != CMD_let || got_eq
3351 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3352 }
3353 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003356 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3357 xp->xp_pattern += 2;
3358
3359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
3361 else if (c == '$')
3362 {
3363 /* environment variable */
3364 xp->xp_context = EXPAND_ENV_VARS;
3365 }
3366 else if (c == '=')
3367 {
3368 got_eq = TRUE;
3369 xp->xp_context = EXPAND_EXPRESSION;
3370 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003371 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 && xp->xp_context == EXPAND_FUNCTIONS
3373 && vim_strchr(xp->xp_pattern, '(') == NULL)
3374 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003375 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 break;
3377 }
3378 else if (cmdidx != CMD_let || got_eq)
3379 {
3380 if (c == '"') /* string */
3381 {
3382 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3383 if (c == '\\' && xp->xp_pattern[1] != NUL)
3384 ++xp->xp_pattern;
3385 xp->xp_context = EXPAND_NOTHING;
3386 }
3387 else if (c == '\'') /* literal string */
3388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003389 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3391 /* skip */ ;
3392 xp->xp_context = EXPAND_NOTHING;
3393 }
3394 else if (c == '|')
3395 {
3396 if (xp->xp_pattern[1] == '|')
3397 {
3398 ++xp->xp_pattern;
3399 xp->xp_context = EXPAND_EXPRESSION;
3400 }
3401 else
3402 xp->xp_context = EXPAND_COMMANDS;
3403 }
3404 else
3405 xp->xp_context = EXPAND_EXPRESSION;
3406 }
3407 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003408 /* Doesn't look like something valid, expand as an expression
3409 * anyway. */
3410 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 arg = xp->xp_pattern;
3412 if (*arg != NUL)
3413 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3414 /* skip */ ;
3415 }
3416 xp->xp_pattern = arg;
3417}
3418
3419#endif /* FEAT_CMDL_COMPL */
3420
3421/*
3422 * ":1,25call func(arg1, arg2)" function call.
3423 */
3424 void
3425ex_call(eap)
3426 exarg_T *eap;
3427{
3428 char_u *arg = eap->arg;
3429 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003431 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 linenr_T lnum;
3435 int doesrange;
3436 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003437 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003439 if (eap->skip)
3440 {
3441 /* trans_function_name() doesn't work well when skipping, use eval0()
3442 * instead to skip to any following command, e.g. for:
3443 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003444 ++emsg_skip;
3445 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3446 clear_tv(&rettv);
3447 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003448 return;
3449 }
3450
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003451 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003452 if (fudi.fd_newkey != NULL)
3453 {
3454 /* Still need to give an error message for missing key. */
3455 EMSG2(_(e_dictkey), fudi.fd_newkey);
3456 vim_free(fudi.fd_newkey);
3457 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003458 if (tofree == NULL)
3459 return;
3460
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003461 /* Increase refcount on dictionary, it could get deleted when evaluating
3462 * the arguments. */
3463 if (fudi.fd_dict != NULL)
3464 ++fudi.fd_dict->dv_refcount;
3465
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003466 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003467 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003468 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
Bram Moolenaar532c7802005-01-27 14:44:31 +00003470 /* Skip white space to allow ":call func ()". Not good, but required for
3471 * backward compatibility. */
3472 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003473 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 if (*startarg != '(')
3476 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003477 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 goto end;
3479 }
3480
3481 /*
3482 * When skipping, evaluate the function once, to find the end of the
3483 * arguments.
3484 * When the function takes a range, this is discovered after the first
3485 * call, and the loop is broken.
3486 */
3487 if (eap->skip)
3488 {
3489 ++emsg_skip;
3490 lnum = eap->line2; /* do it once, also with an invalid range */
3491 }
3492 else
3493 lnum = eap->line1;
3494 for ( ; lnum <= eap->line2; ++lnum)
3495 {
3496 if (!eap->skip && eap->addr_count > 0)
3497 {
3498 curwin->w_cursor.lnum = lnum;
3499 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003500#ifdef FEAT_VIRTUALEDIT
3501 curwin->w_cursor.coladd = 0;
3502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003505 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003506 eap->line1, eap->line2, &doesrange,
3507 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 failed = TRUE;
3510 break;
3511 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003512
3513 /* Handle a function returning a Funcref, Dictionary or List. */
3514 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3515 {
3516 failed = TRUE;
3517 break;
3518 }
3519
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003520 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (doesrange || eap->skip)
3522 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003525 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003526 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003527 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (aborting())
3529 break;
3530 }
3531 if (eap->skip)
3532 --emsg_skip;
3533
3534 if (!failed)
3535 {
3536 /* Check for trailing illegal characters and a following command. */
3537 if (!ends_excmd(*arg))
3538 {
3539 emsg_severe = TRUE;
3540 EMSG(_(e_trailing));
3541 }
3542 else
3543 eap->nextcmd = check_nextcmd(arg);
3544 }
3545
3546end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003547 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003548 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549}
3550
3551/*
3552 * ":unlet[!] var1 ... " command.
3553 */
3554 void
3555ex_unlet(eap)
3556 exarg_T *eap;
3557{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003558 ex_unletlock(eap, eap->arg, 0);
3559}
3560
3561/*
3562 * ":lockvar" and ":unlockvar" commands
3563 */
3564 void
3565ex_lockvar(eap)
3566 exarg_T *eap;
3567{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 int deep = 2;
3570
3571 if (eap->forceit)
3572 deep = -1;
3573 else if (vim_isdigit(*arg))
3574 {
3575 deep = getdigits(&arg);
3576 arg = skipwhite(arg);
3577 }
3578
3579 ex_unletlock(eap, arg, deep);
3580}
3581
3582/*
3583 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3584 */
3585 static void
3586ex_unletlock(eap, argstart, deep)
3587 exarg_T *eap;
3588 char_u *argstart;
3589 int deep;
3590{
3591 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
3596 do
3597 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003599 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003600 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 if (lv.ll_name == NULL)
3602 error = TRUE; /* error but continue parsing */
3603 if (name_end == NULL || (!vim_iswhite(*name_end)
3604 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 if (name_end != NULL)
3607 {
3608 emsg_severe = TRUE;
3609 EMSG(_(e_trailing));
3610 }
3611 if (!(eap->skip || error))
3612 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 break;
3614 }
3615
3616 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003617 {
3618 if (eap->cmdidx == CMD_unlet)
3619 {
3620 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3621 error = TRUE;
3622 }
3623 else
3624 {
3625 if (do_lock_var(&lv, name_end, deep,
3626 eap->cmdidx == CMD_lockvar) == FAIL)
3627 error = TRUE;
3628 }
3629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 if (!eap->skip)
3632 clear_lval(&lv);
3633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 arg = skipwhite(name_end);
3635 } while (!ends_excmd(*arg));
3636
3637 eap->nextcmd = check_nextcmd(arg);
3638}
3639
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003642 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003643 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003644 int forceit;
3645{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 int ret = OK;
3647 int cc;
3648
3649 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 cc = *name_end;
3652 *name_end = NUL;
3653
3654 /* Normal name or expanded name. */
3655 if (check_changedtick(lp->ll_name))
3656 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003661 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003662 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003663 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003664 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003665 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 else if (lp->ll_range)
3667 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003668 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003669 listitem_T *ll_li = lp->ll_li;
3670 int ll_n1 = lp->ll_n1;
3671
3672 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3673 {
3674 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003676 return FAIL;
3677 ll_li = li;
3678 ++ll_n1;
3679 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680
3681 /* Delete a range of List items. */
3682 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3683 {
3684 li = lp->ll_li->li_next;
3685 listitem_remove(lp->ll_list, lp->ll_li);
3686 lp->ll_li = li;
3687 ++lp->ll_n1;
3688 }
3689 }
3690 else
3691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693 /* unlet a List item. */
3694 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003696 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003697 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 }
3699
3700 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003701}
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703/*
3704 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003705 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 */
3707 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003708do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003710 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
Bram Moolenaar33570922005-01-25 22:26:29 +00003712 hashtab_T *ht;
3713 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003714 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003715 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003716 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 ht = find_var_ht(name, &varname);
3719 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003721 if (ht == &globvarht)
3722 d = &globvardict;
3723 else if (current_funccal != NULL
3724 && ht == &current_funccal->l_vars.dv_hashtab)
3725 d = &current_funccal->l_vars;
3726 else
3727 {
3728 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3729 d = di->di_tv.vval.v_dict;
3730 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 hi = hash_find(ht, varname);
3732 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003733 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003734 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003735 if (var_check_fixed(di->di_flags, name, FALSE)
3736 || var_check_ro(di->di_flags, name, FALSE)
3737 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003738 return FAIL;
3739 delete_var(ht, hi);
3740 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003743 if (forceit)
3744 return OK;
3745 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 return FAIL;
3747}
3748
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749/*
3750 * Lock or unlock variable indicated by "lp".
3751 * "deep" is the levels to go (-1 for unlimited);
3752 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3753 */
3754 static int
3755do_lock_var(lp, name_end, deep, lock)
3756 lval_T *lp;
3757 char_u *name_end;
3758 int deep;
3759 int lock;
3760{
3761 int ret = OK;
3762 int cc;
3763 dictitem_T *di;
3764
3765 if (deep == 0) /* nothing to do */
3766 return OK;
3767
3768 if (lp->ll_tv == NULL)
3769 {
3770 cc = *name_end;
3771 *name_end = NUL;
3772
3773 /* Normal name or expanded name. */
3774 if (check_changedtick(lp->ll_name))
3775 ret = FAIL;
3776 else
3777 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003778 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003779 if (di == NULL)
3780 ret = FAIL;
3781 else
3782 {
3783 if (lock)
3784 di->di_flags |= DI_FLAGS_LOCK;
3785 else
3786 di->di_flags &= ~DI_FLAGS_LOCK;
3787 item_lock(&di->di_tv, deep, lock);
3788 }
3789 }
3790 *name_end = cc;
3791 }
3792 else if (lp->ll_range)
3793 {
3794 listitem_T *li = lp->ll_li;
3795
3796 /* (un)lock a range of List items. */
3797 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3798 {
3799 item_lock(&li->li_tv, deep, lock);
3800 li = li->li_next;
3801 ++lp->ll_n1;
3802 }
3803 }
3804 else if (lp->ll_list != NULL)
3805 /* (un)lock a List item. */
3806 item_lock(&lp->ll_li->li_tv, deep, lock);
3807 else
3808 /* un(lock) a Dictionary item. */
3809 item_lock(&lp->ll_di->di_tv, deep, lock);
3810
3811 return ret;
3812}
3813
3814/*
3815 * Lock or unlock an item. "deep" is nr of levels to go.
3816 */
3817 static void
3818item_lock(tv, deep, lock)
3819 typval_T *tv;
3820 int deep;
3821 int lock;
3822{
3823 static int recurse = 0;
3824 list_T *l;
3825 listitem_T *li;
3826 dict_T *d;
3827 hashitem_T *hi;
3828 int todo;
3829
3830 if (recurse >= DICT_MAXNEST)
3831 {
3832 EMSG(_("E743: variable nested too deep for (un)lock"));
3833 return;
3834 }
3835 if (deep == 0)
3836 return;
3837 ++recurse;
3838
3839 /* lock/unlock the item itself */
3840 if (lock)
3841 tv->v_lock |= VAR_LOCKED;
3842 else
3843 tv->v_lock &= ~VAR_LOCKED;
3844
3845 switch (tv->v_type)
3846 {
3847 case VAR_LIST:
3848 if ((l = tv->vval.v_list) != NULL)
3849 {
3850 if (lock)
3851 l->lv_lock |= VAR_LOCKED;
3852 else
3853 l->lv_lock &= ~VAR_LOCKED;
3854 if (deep < 0 || deep > 1)
3855 /* recursive: lock/unlock the items the List contains */
3856 for (li = l->lv_first; li != NULL; li = li->li_next)
3857 item_lock(&li->li_tv, deep - 1, lock);
3858 }
3859 break;
3860 case VAR_DICT:
3861 if ((d = tv->vval.v_dict) != NULL)
3862 {
3863 if (lock)
3864 d->dv_lock |= VAR_LOCKED;
3865 else
3866 d->dv_lock &= ~VAR_LOCKED;
3867 if (deep < 0 || deep > 1)
3868 {
3869 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003870 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003871 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3872 {
3873 if (!HASHITEM_EMPTY(hi))
3874 {
3875 --todo;
3876 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3877 }
3878 }
3879 }
3880 }
3881 }
3882 --recurse;
3883}
3884
Bram Moolenaara40058a2005-07-11 22:42:07 +00003885/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003886 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3887 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003888 */
3889 static int
3890tv_islocked(tv)
3891 typval_T *tv;
3892{
3893 return (tv->v_lock & VAR_LOCKED)
3894 || (tv->v_type == VAR_LIST
3895 && tv->vval.v_list != NULL
3896 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3897 || (tv->v_type == VAR_DICT
3898 && tv->vval.v_dict != NULL
3899 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3900}
3901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3903/*
3904 * Delete all "menutrans_" variables.
3905 */
3906 void
3907del_menutrans_vars()
3908{
Bram Moolenaar33570922005-01-25 22:26:29 +00003909 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003910 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
Bram Moolenaar33570922005-01-25 22:26:29 +00003912 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003913 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003915 {
3916 if (!HASHITEM_EMPTY(hi))
3917 {
3918 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003919 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3920 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003921 }
3922 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003923 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924}
3925#endif
3926
3927#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3928
3929/*
3930 * Local string buffer for the next two functions to store a variable name
3931 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3932 * get_user_var_name().
3933 */
3934
3935static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3936
3937static char_u *varnamebuf = NULL;
3938static int varnamebuflen = 0;
3939
3940/*
3941 * Function to concatenate a prefix and a variable name.
3942 */
3943 static char_u *
3944cat_prefix_varname(prefix, name)
3945 int prefix;
3946 char_u *name;
3947{
3948 int len;
3949
3950 len = (int)STRLEN(name) + 3;
3951 if (len > varnamebuflen)
3952 {
3953 vim_free(varnamebuf);
3954 len += 10; /* some additional space */
3955 varnamebuf = alloc(len);
3956 if (varnamebuf == NULL)
3957 {
3958 varnamebuflen = 0;
3959 return NULL;
3960 }
3961 varnamebuflen = len;
3962 }
3963 *varnamebuf = prefix;
3964 varnamebuf[1] = ':';
3965 STRCPY(varnamebuf + 2, name);
3966 return varnamebuf;
3967}
3968
3969/*
3970 * Function given to ExpandGeneric() to obtain the list of user defined
3971 * (global/buffer/window/built-in) variable names.
3972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 char_u *
3974get_user_var_name(xp, idx)
3975 expand_T *xp;
3976 int idx;
3977{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003978 static long_u gdone;
3979 static long_u bdone;
3980 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003981#ifdef FEAT_WINDOWS
3982 static long_u tdone;
3983#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003984 static int vidx;
3985 static hashitem_T *hi;
3986 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
3988 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003989 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003990 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003991#ifdef FEAT_WINDOWS
3992 tdone = 0;
3993#endif
3994 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003995
3996 /* Global variables */
3997 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003999 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004001 else
4002 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 while (HASHITEM_EMPTY(hi))
4004 ++hi;
4005 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4006 return cat_prefix_varname('g', hi->hi_key);
4007 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004009
4010 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004011 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004012 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004014 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004016 else
4017 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 while (HASHITEM_EMPTY(hi))
4019 ++hi;
4020 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004022 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 return (char_u *)"b:changedtick";
4026 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004027
4028 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004029 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004030 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004032 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004033 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004034 else
4035 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004036 while (HASHITEM_EMPTY(hi))
4037 ++hi;
4038 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004040
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004041#ifdef FEAT_WINDOWS
4042 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004043 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004044 if (tdone < ht->ht_used)
4045 {
4046 if (tdone++ == 0)
4047 hi = ht->ht_array;
4048 else
4049 ++hi;
4050 while (HASHITEM_EMPTY(hi))
4051 ++hi;
4052 return cat_prefix_varname('t', hi->hi_key);
4053 }
4054#endif
4055
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 /* v: variables */
4057 if (vidx < VV_LEN)
4058 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060 vim_free(varnamebuf);
4061 varnamebuf = NULL;
4062 varnamebuflen = 0;
4063 return NULL;
4064}
4065
4066#endif /* FEAT_CMDL_COMPL */
4067
4068/*
4069 * types for expressions.
4070 */
4071typedef enum
4072{
4073 TYPE_UNKNOWN = 0
4074 , TYPE_EQUAL /* == */
4075 , TYPE_NEQUAL /* != */
4076 , TYPE_GREATER /* > */
4077 , TYPE_GEQUAL /* >= */
4078 , TYPE_SMALLER /* < */
4079 , TYPE_SEQUAL /* <= */
4080 , TYPE_MATCH /* =~ */
4081 , TYPE_NOMATCH /* !~ */
4082} exptype_T;
4083
4084/*
4085 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4088 */
4089
4090/*
4091 * Handle zero level expression.
4092 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004094 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 * Return OK or FAIL.
4096 */
4097 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 char_u **nextcmd;
4102 int evaluate;
4103{
4104 int ret;
4105 char_u *p;
4106
4107 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 if (ret == FAIL || !ends_excmd(*p))
4110 {
4111 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 /*
4114 * Report the invalid expression unless the expression evaluation has
4115 * been cancelled due to an aborting error, an interrupt, or an
4116 * exception.
4117 */
4118 if (!aborting())
4119 EMSG2(_(e_invexpr2), arg);
4120 ret = FAIL;
4121 }
4122 if (nextcmd != NULL)
4123 *nextcmd = check_nextcmd(p);
4124
4125 return ret;
4126}
4127
4128/*
4129 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004130 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 *
4132 * "arg" must point to the first non-white of the expression.
4133 * "arg" is advanced to the next non-white after the recognized expression.
4134 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004135 * Note: "rettv.v_lock" is not set.
4136 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 * Return OK or FAIL.
4138 */
4139 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 int evaluate;
4144{
4145 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004146 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
4148 /*
4149 * Get the first variable.
4150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 return FAIL;
4153
4154 if ((*arg)[0] == '?')
4155 {
4156 result = FALSE;
4157 if (evaluate)
4158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159 int error = FALSE;
4160
4161 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 if (error)
4165 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167
4168 /*
4169 * Get the second variable.
4170 */
4171 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 return FAIL;
4174
4175 /*
4176 * Check for the ":".
4177 */
4178 if ((*arg)[0] != ':')
4179 {
4180 EMSG(_("E109: Missing ':' after '?'"));
4181 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 return FAIL;
4184 }
4185
4186 /*
4187 * Get the third variable.
4188 */
4189 *arg = skipwhite(*arg + 1);
4190 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4191 {
4192 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 return FAIL;
4195 }
4196 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199
4200 return OK;
4201}
4202
4203/*
4204 * Handle first level expression:
4205 * expr2 || expr2 || expr2 logical OR
4206 *
4207 * "arg" must point to the first non-white of the expression.
4208 * "arg" is advanced to the next non-white after the recognized expression.
4209 *
4210 * Return OK or FAIL.
4211 */
4212 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 int evaluate;
4217{
Bram Moolenaar33570922005-01-25 22:26:29 +00004218 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 long result;
4220 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 /*
4224 * Get the first variable.
4225 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return FAIL;
4228
4229 /*
4230 * Repeat until there is no following "||".
4231 */
4232 first = TRUE;
4233 result = FALSE;
4234 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4235 {
4236 if (evaluate && first)
4237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 if (error)
4242 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 first = FALSE;
4244 }
4245
4246 /*
4247 * Get the second variable.
4248 */
4249 *arg = skipwhite(*arg + 2);
4250 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4251 return FAIL;
4252
4253 /*
4254 * Compute the result.
4255 */
4256 if (evaluate && !result)
4257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (error)
4262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 if (evaluate)
4265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 rettv->v_type = VAR_NUMBER;
4267 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 }
4270
4271 return OK;
4272}
4273
4274/*
4275 * Handle second level expression:
4276 * expr3 && expr3 && expr3 logical AND
4277 *
4278 * "arg" must point to the first non-white of the expression.
4279 * "arg" is advanced to the next non-white after the recognized expression.
4280 *
4281 * Return OK or FAIL.
4282 */
4283 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 int evaluate;
4288{
Bram Moolenaar33570922005-01-25 22:26:29 +00004289 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 long result;
4291 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004292 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
4294 /*
4295 * Get the first variable.
4296 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 return FAIL;
4299
4300 /*
4301 * Repeat until there is no following "&&".
4302 */
4303 first = TRUE;
4304 result = TRUE;
4305 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4306 {
4307 if (evaluate && first)
4308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 if (error)
4313 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 first = FALSE;
4315 }
4316
4317 /*
4318 * Get the second variable.
4319 */
4320 *arg = skipwhite(*arg + 2);
4321 if (eval4(arg, &var2, evaluate && result) == FAIL)
4322 return FAIL;
4323
4324 /*
4325 * Compute the result.
4326 */
4327 if (evaluate && result)
4328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (error)
4333 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
4335 if (evaluate)
4336 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 rettv->v_type = VAR_NUMBER;
4338 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340 }
4341
4342 return OK;
4343}
4344
4345/*
4346 * Handle third level expression:
4347 * var1 == var2
4348 * var1 =~ var2
4349 * var1 != var2
4350 * var1 !~ var2
4351 * var1 > var2
4352 * var1 >= var2
4353 * var1 < var2
4354 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004355 * var1 is var2
4356 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 *
4358 * "arg" must point to the first non-white of the expression.
4359 * "arg" is advanced to the next non-white after the recognized expression.
4360 *
4361 * Return OK or FAIL.
4362 */
4363 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 int evaluate;
4368{
Bram Moolenaar33570922005-01-25 22:26:29 +00004369 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 char_u *p;
4371 int i;
4372 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004373 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 int len = 2;
4375 long n1, n2;
4376 char_u *s1, *s2;
4377 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4378 regmatch_T regmatch;
4379 int ic;
4380 char_u *save_cpo;
4381
4382 /*
4383 * Get the first variable.
4384 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 return FAIL;
4387
4388 p = *arg;
4389 switch (p[0])
4390 {
4391 case '=': if (p[1] == '=')
4392 type = TYPE_EQUAL;
4393 else if (p[1] == '~')
4394 type = TYPE_MATCH;
4395 break;
4396 case '!': if (p[1] == '=')
4397 type = TYPE_NEQUAL;
4398 else if (p[1] == '~')
4399 type = TYPE_NOMATCH;
4400 break;
4401 case '>': if (p[1] != '=')
4402 {
4403 type = TYPE_GREATER;
4404 len = 1;
4405 }
4406 else
4407 type = TYPE_GEQUAL;
4408 break;
4409 case '<': if (p[1] != '=')
4410 {
4411 type = TYPE_SMALLER;
4412 len = 1;
4413 }
4414 else
4415 type = TYPE_SEQUAL;
4416 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004417 case 'i': if (p[1] == 's')
4418 {
4419 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4420 len = 5;
4421 if (!vim_isIDc(p[len]))
4422 {
4423 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4424 type_is = TRUE;
4425 }
4426 }
4427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429
4430 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004431 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 */
4433 if (type != TYPE_UNKNOWN)
4434 {
4435 /* extra question mark appended: ignore case */
4436 if (p[len] == '?')
4437 {
4438 ic = TRUE;
4439 ++len;
4440 }
4441 /* extra '#' appended: match case */
4442 else if (p[len] == '#')
4443 {
4444 ic = FALSE;
4445 ++len;
4446 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004447 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 else
4449 ic = p_ic;
4450
4451 /*
4452 * Get the second variable.
4453 */
4454 *arg = skipwhite(p + len);
4455 if (eval5(arg, &var2, evaluate) == FAIL)
4456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 return FAIL;
4459 }
4460
4461 if (evaluate)
4462 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004463 if (type_is && rettv->v_type != var2.v_type)
4464 {
4465 /* For "is" a different type always means FALSE, for "notis"
4466 * it means TRUE. */
4467 n1 = (type == TYPE_NEQUAL);
4468 }
4469 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4470 {
4471 if (type_is)
4472 {
4473 n1 = (rettv->v_type == var2.v_type
4474 && rettv->vval.v_list == var2.vval.v_list);
4475 if (type == TYPE_NEQUAL)
4476 n1 = !n1;
4477 }
4478 else if (rettv->v_type != var2.v_type
4479 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4480 {
4481 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004482 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004484 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004485 clear_tv(rettv);
4486 clear_tv(&var2);
4487 return FAIL;
4488 }
4489 else
4490 {
4491 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004492 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4493 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004494 if (type == TYPE_NEQUAL)
4495 n1 = !n1;
4496 }
4497 }
4498
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004499 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4500 {
4501 if (type_is)
4502 {
4503 n1 = (rettv->v_type == var2.v_type
4504 && rettv->vval.v_dict == var2.vval.v_dict);
4505 if (type == TYPE_NEQUAL)
4506 n1 = !n1;
4507 }
4508 else if (rettv->v_type != var2.v_type
4509 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4510 {
4511 if (rettv->v_type != var2.v_type)
4512 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4513 else
4514 EMSG(_("E736: Invalid operation for Dictionary"));
4515 clear_tv(rettv);
4516 clear_tv(&var2);
4517 return FAIL;
4518 }
4519 else
4520 {
4521 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004522 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4523 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004524 if (type == TYPE_NEQUAL)
4525 n1 = !n1;
4526 }
4527 }
4528
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004529 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4530 {
4531 if (rettv->v_type != var2.v_type
4532 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4533 {
4534 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004535 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004536 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004537 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004538 clear_tv(rettv);
4539 clear_tv(&var2);
4540 return FAIL;
4541 }
4542 else
4543 {
4544 /* Compare two Funcrefs for being equal or unequal. */
4545 if (rettv->vval.v_string == NULL
4546 || var2.vval.v_string == NULL)
4547 n1 = FALSE;
4548 else
4549 n1 = STRCMP(rettv->vval.v_string,
4550 var2.vval.v_string) == 0;
4551 if (type == TYPE_NEQUAL)
4552 n1 = !n1;
4553 }
4554 }
4555
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004556#ifdef FEAT_FLOAT
4557 /*
4558 * If one of the two variables is a float, compare as a float.
4559 * When using "=~" or "!~", always compare as string.
4560 */
4561 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4562 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4563 {
4564 float_T f1, f2;
4565
4566 if (rettv->v_type == VAR_FLOAT)
4567 f1 = rettv->vval.v_float;
4568 else
4569 f1 = get_tv_number(rettv);
4570 if (var2.v_type == VAR_FLOAT)
4571 f2 = var2.vval.v_float;
4572 else
4573 f2 = get_tv_number(&var2);
4574 n1 = FALSE;
4575 switch (type)
4576 {
4577 case TYPE_EQUAL: n1 = (f1 == f2); break;
4578 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4579 case TYPE_GREATER: n1 = (f1 > f2); break;
4580 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4581 case TYPE_SMALLER: n1 = (f1 < f2); break;
4582 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4583 case TYPE_UNKNOWN:
4584 case TYPE_MATCH:
4585 case TYPE_NOMATCH: break; /* avoid gcc warning */
4586 }
4587 }
4588#endif
4589
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 /*
4591 * If one of the two variables is a number, compare as a number.
4592 * When using "=~" or "!~", always compare as string.
4593 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004594 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597 n1 = get_tv_number(rettv);
4598 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 switch (type)
4600 {
4601 case TYPE_EQUAL: n1 = (n1 == n2); break;
4602 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4603 case TYPE_GREATER: n1 = (n1 > n2); break;
4604 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4605 case TYPE_SMALLER: n1 = (n1 < n2); break;
4606 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4607 case TYPE_UNKNOWN:
4608 case TYPE_MATCH:
4609 case TYPE_NOMATCH: break; /* avoid gcc warning */
4610 }
4611 }
4612 else
4613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 s1 = get_tv_string_buf(rettv, buf1);
4615 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4617 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4618 else
4619 i = 0;
4620 n1 = FALSE;
4621 switch (type)
4622 {
4623 case TYPE_EQUAL: n1 = (i == 0); break;
4624 case TYPE_NEQUAL: n1 = (i != 0); break;
4625 case TYPE_GREATER: n1 = (i > 0); break;
4626 case TYPE_GEQUAL: n1 = (i >= 0); break;
4627 case TYPE_SMALLER: n1 = (i < 0); break;
4628 case TYPE_SEQUAL: n1 = (i <= 0); break;
4629
4630 case TYPE_MATCH:
4631 case TYPE_NOMATCH:
4632 /* avoid 'l' flag in 'cpoptions' */
4633 save_cpo = p_cpo;
4634 p_cpo = (char_u *)"";
4635 regmatch.regprog = vim_regcomp(s2,
4636 RE_MAGIC + RE_STRING);
4637 regmatch.rm_ic = ic;
4638 if (regmatch.regprog != NULL)
4639 {
4640 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004641 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 if (type == TYPE_NOMATCH)
4643 n1 = !n1;
4644 }
4645 p_cpo = save_cpo;
4646 break;
4647
4648 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4649 }
4650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004651 clear_tv(rettv);
4652 clear_tv(&var2);
4653 rettv->v_type = VAR_NUMBER;
4654 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 }
4657
4658 return OK;
4659}
4660
4661/*
4662 * Handle fourth level expression:
4663 * + number addition
4664 * - number subtraction
4665 * . string concatenation
4666 *
4667 * "arg" must point to the first non-white of the expression.
4668 * "arg" is advanced to the next non-white after the recognized expression.
4669 *
4670 * Return OK or FAIL.
4671 */
4672 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004673eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 int evaluate;
4677{
Bram Moolenaar33570922005-01-25 22:26:29 +00004678 typval_T var2;
4679 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 int op;
4681 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004682#ifdef FEAT_FLOAT
4683 float_T f1 = 0, f2 = 0;
4684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 char_u *s1, *s2;
4686 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4687 char_u *p;
4688
4689 /*
4690 * Get the first variable.
4691 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004692 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 return FAIL;
4694
4695 /*
4696 * Repeat computing, until no '+', '-' or '.' is following.
4697 */
4698 for (;;)
4699 {
4700 op = **arg;
4701 if (op != '+' && op != '-' && op != '.')
4702 break;
4703
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704 if ((op != '+' || rettv->v_type != VAR_LIST)
4705#ifdef FEAT_FLOAT
4706 && (op == '.' || rettv->v_type != VAR_FLOAT)
4707#endif
4708 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 {
4710 /* For "list + ...", an illegal use of the first operand as
4711 * a number cannot be determined before evaluating the 2nd
4712 * operand: if this is also a list, all is ok.
4713 * For "something . ...", "something - ..." or "non-list + ...",
4714 * we know that the first operand needs to be a string or number
4715 * without evaluating the 2nd operand. So check before to avoid
4716 * side effects after an error. */
4717 if (evaluate && get_tv_string_chk(rettv) == NULL)
4718 {
4719 clear_tv(rettv);
4720 return FAIL;
4721 }
4722 }
4723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 /*
4725 * Get the second variable.
4726 */
4727 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004728 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 return FAIL;
4732 }
4733
4734 if (evaluate)
4735 {
4736 /*
4737 * Compute the result.
4738 */
4739 if (op == '.')
4740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4742 s2 = get_tv_string_buf_chk(&var2, buf2);
4743 if (s2 == NULL) /* type error ? */
4744 {
4745 clear_tv(rettv);
4746 clear_tv(&var2);
4747 return FAIL;
4748 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004749 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 clear_tv(rettv);
4751 rettv->v_type = VAR_STRING;
4752 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004754 else if (op == '+' && rettv->v_type == VAR_LIST
4755 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004756 {
4757 /* concatenate Lists */
4758 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4759 &var3) == FAIL)
4760 {
4761 clear_tv(rettv);
4762 clear_tv(&var2);
4763 return FAIL;
4764 }
4765 clear_tv(rettv);
4766 *rettv = var3;
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 else
4769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770 int error = FALSE;
4771
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004772#ifdef FEAT_FLOAT
4773 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004774 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004775 f1 = rettv->vval.v_float;
4776 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004778 else
4779#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004780 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004781 n1 = get_tv_number_chk(rettv, &error);
4782 if (error)
4783 {
4784 /* This can only happen for "list + non-list". For
4785 * "non-list + ..." or "something - ...", we returned
4786 * before evaluating the 2nd operand. */
4787 clear_tv(rettv);
4788 return FAIL;
4789 }
4790#ifdef FEAT_FLOAT
4791 if (var2.v_type == VAR_FLOAT)
4792 f1 = n1;
4793#endif
4794 }
4795#ifdef FEAT_FLOAT
4796 if (var2.v_type == VAR_FLOAT)
4797 {
4798 f2 = var2.vval.v_float;
4799 n2 = 0;
4800 }
4801 else
4802#endif
4803 {
4804 n2 = get_tv_number_chk(&var2, &error);
4805 if (error)
4806 {
4807 clear_tv(rettv);
4808 clear_tv(&var2);
4809 return FAIL;
4810 }
4811#ifdef FEAT_FLOAT
4812 if (rettv->v_type == VAR_FLOAT)
4813 f2 = n2;
4814#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004816 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817
4818#ifdef FEAT_FLOAT
4819 /* If there is a float on either side the result is a float. */
4820 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4821 {
4822 if (op == '+')
4823 f1 = f1 + f2;
4824 else
4825 f1 = f1 - f2;
4826 rettv->v_type = VAR_FLOAT;
4827 rettv->vval.v_float = f1;
4828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830#endif
4831 {
4832 if (op == '+')
4833 n1 = n1 + n2;
4834 else
4835 n1 = n1 - n2;
4836 rettv->v_type = VAR_NUMBER;
4837 rettv->vval.v_number = n1;
4838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 }
4843 return OK;
4844}
4845
4846/*
4847 * Handle fifth level expression:
4848 * * number multiplication
4849 * / number division
4850 * % number modulo
4851 *
4852 * "arg" must point to the first non-white of the expression.
4853 * "arg" is advanced to the next non-white after the recognized expression.
4854 *
4855 * Return OK or FAIL.
4856 */
4857 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004858eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004862 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
Bram Moolenaar33570922005-01-25 22:26:29 +00004864 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 int op;
4866 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867#ifdef FEAT_FLOAT
4868 int use_float = FALSE;
4869 float_T f1 = 0, f2;
4870#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004871 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
4873 /*
4874 * Get the first variable.
4875 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004876 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 return FAIL;
4878
4879 /*
4880 * Repeat computing, until no '*', '/' or '%' is following.
4881 */
4882 for (;;)
4883 {
4884 op = **arg;
4885 if (op != '*' && op != '/' && op != '%')
4886 break;
4887
4888 if (evaluate)
4889 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890#ifdef FEAT_FLOAT
4891 if (rettv->v_type == VAR_FLOAT)
4892 {
4893 f1 = rettv->vval.v_float;
4894 use_float = TRUE;
4895 n1 = 0;
4896 }
4897 else
4898#endif
4899 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004901 if (error)
4902 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 else
4905 n1 = 0;
4906
4907 /*
4908 * Get the second variable.
4909 */
4910 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004911 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 return FAIL;
4913
4914 if (evaluate)
4915 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916#ifdef FEAT_FLOAT
4917 if (var2.v_type == VAR_FLOAT)
4918 {
4919 if (!use_float)
4920 {
4921 f1 = n1;
4922 use_float = TRUE;
4923 }
4924 f2 = var2.vval.v_float;
4925 n2 = 0;
4926 }
4927 else
4928#endif
4929 {
4930 n2 = get_tv_number_chk(&var2, &error);
4931 clear_tv(&var2);
4932 if (error)
4933 return FAIL;
4934#ifdef FEAT_FLOAT
4935 if (use_float)
4936 f2 = n2;
4937#endif
4938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 /*
4941 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004942 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004944#ifdef FEAT_FLOAT
4945 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947 if (op == '*')
4948 f1 = f1 * f2;
4949 else if (op == '/')
4950 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004951# ifdef VMS
4952 /* VMS crashes on divide by zero, work around it */
4953 if (f2 == 0.0)
4954 {
4955 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004956 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004957 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004958 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004959 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004960 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004961 }
4962 else
4963 f1 = f1 / f2;
4964# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004965 /* We rely on the floating point library to handle divide
4966 * by zero to result in "inf" and not a crash. */
4967 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004972 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004973 return FAIL;
4974 }
4975 rettv->v_type = VAR_FLOAT;
4976 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981 if (op == '*')
4982 n1 = n1 * n2;
4983 else if (op == '/')
4984 {
4985 if (n2 == 0) /* give an error message? */
4986 {
4987 if (n1 == 0)
4988 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4989 else if (n1 < 0)
4990 n1 = -0x7fffffffL;
4991 else
4992 n1 = 0x7fffffffL;
4993 }
4994 else
4995 n1 = n1 / n2;
4996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004998 {
4999 if (n2 == 0) /* give an error message? */
5000 n1 = 0;
5001 else
5002 n1 = n1 % n2;
5003 }
5004 rettv->v_type = VAR_NUMBER;
5005 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008 }
5009
5010 return OK;
5011}
5012
5013/*
5014 * Handle sixth level expression:
5015 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005016 * "string" string constant
5017 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 * &option-name option value
5019 * @r register contents
5020 * identifier variable value
5021 * function() function call
5022 * $VAR environment variable
5023 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005024 * [expr, expr] List
5025 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 *
5027 * Also handle:
5028 * ! in front logical NOT
5029 * - in front unary minus
5030 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 * trailing [] subscript in String or List
5032 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 *
5034 * "arg" must point to the first non-white of the expression.
5035 * "arg" is advanced to the next non-white after the recognized expression.
5036 *
5037 * Return OK or FAIL.
5038 */
5039 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005040eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005044 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 long n;
5047 int len;
5048 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 char_u *start_leader, *end_leader;
5050 int ret = OK;
5051 char_u *alias;
5052
5053 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005055 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005057 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
5059 /*
5060 * Skip '!' and '-' characters. They are handled later.
5061 */
5062 start_leader = *arg;
5063 while (**arg == '!' || **arg == '-' || **arg == '+')
5064 *arg = skipwhite(*arg + 1);
5065 end_leader = *arg;
5066
5067 switch (**arg)
5068 {
5069 /*
5070 * Number constant.
5071 */
5072 case '0':
5073 case '1':
5074 case '2':
5075 case '3':
5076 case '4':
5077 case '5':
5078 case '6':
5079 case '7':
5080 case '8':
5081 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005082 {
5083#ifdef FEAT_FLOAT
5084 char_u *p = skipdigits(*arg + 1);
5085 int get_float = FALSE;
5086
5087 /* We accept a float when the format matches
5088 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005089 * strict to avoid backwards compatibility problems.
5090 * Don't look for a float after the "." operator, so that
5091 * ":let vers = 1.2.3" doesn't fail. */
5092 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005094 get_float = TRUE;
5095 p = skipdigits(p + 2);
5096 if (*p == 'e' || *p == 'E')
5097 {
5098 ++p;
5099 if (*p == '-' || *p == '+')
5100 ++p;
5101 if (!vim_isdigit(*p))
5102 get_float = FALSE;
5103 else
5104 p = skipdigits(p + 1);
5105 }
5106 if (ASCII_ISALPHA(*p) || *p == '.')
5107 get_float = FALSE;
5108 }
5109 if (get_float)
5110 {
5111 float_T f;
5112
5113 *arg += string2float(*arg, &f);
5114 if (evaluate)
5115 {
5116 rettv->v_type = VAR_FLOAT;
5117 rettv->vval.v_float = f;
5118 }
5119 }
5120 else
5121#endif
5122 {
5123 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5124 *arg += len;
5125 if (evaluate)
5126 {
5127 rettv->v_type = VAR_NUMBER;
5128 rettv->vval.v_number = n;
5129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 /*
5135 * String constant: "string".
5136 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005137 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 break;
5139
5140 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005143 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144 break;
5145
5146 /*
5147 * List: [expr, expr]
5148 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 break;
5151
5152 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 * Dictionary: {key: val, key: val}
5154 */
5155 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5156 break;
5157
5158 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005159 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005161 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 break;
5163
5164 /*
5165 * Environment variable: $VAR.
5166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169
5170 /*
5171 * Register contents: @r.
5172 */
5173 case '@': ++*arg;
5174 if (evaluate)
5175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005177 rettv->vval.v_string = get_reg_contents(**arg,
5178 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180 if (**arg != NUL)
5181 ++*arg;
5182 break;
5183
5184 /*
5185 * nested expression: (expression).
5186 */
5187 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005188 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 if (**arg == ')')
5190 ++*arg;
5191 else if (ret == OK)
5192 {
5193 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005194 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 ret = FAIL;
5196 }
5197 break;
5198
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 break;
5201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202
5203 if (ret == NOTDONE)
5204 {
5205 /*
5206 * Must be a variable or function name.
5207 * Can also be a curly-braces kind of name: {expr}.
5208 */
5209 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005210 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 if (alias != NULL)
5212 s = alias;
5213
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005214 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 ret = FAIL;
5216 else
5217 {
5218 if (**arg == '(') /* recursive! */
5219 {
5220 /* If "s" is the name of a variable of type VAR_FUNC
5221 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005222 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223
5224 /* Invoke the function. */
5225 ret = get_func_tv(s, len, rettv, arg,
5226 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005227 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005228
5229 /* If evaluate is FALSE rettv->v_type was not set in
5230 * get_func_tv, but it's needed in handle_subscript() to parse
5231 * what follows. So set it here. */
5232 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5233 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005234 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005235 rettv->v_type = VAR_FUNC;
5236 }
5237
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 /* Stop the expression evaluation when immediately
5239 * aborting on error, or when an interrupt occurred or
5240 * an exception was thrown but not caught. */
5241 if (aborting())
5242 {
5243 if (ret == OK)
5244 clear_tv(rettv);
5245 ret = FAIL;
5246 }
5247 }
5248 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005249 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005250 else
5251 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005253 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 }
5255
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 *arg = skipwhite(*arg);
5257
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005258 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5259 * expr(expr). */
5260 if (ret == OK)
5261 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
5263 /*
5264 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5265 */
5266 if (ret == OK && evaluate && end_leader > start_leader)
5267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005268 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005269 int val = 0;
5270#ifdef FEAT_FLOAT
5271 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005272
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005273 if (rettv->v_type == VAR_FLOAT)
5274 f = rettv->vval.v_float;
5275 else
5276#endif
5277 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005278 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 clear_tv(rettv);
5281 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 else
5284 {
5285 while (end_leader > start_leader)
5286 {
5287 --end_leader;
5288 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005289 {
5290#ifdef FEAT_FLOAT
5291 if (rettv->v_type == VAR_FLOAT)
5292 f = !f;
5293 else
5294#endif
5295 val = !val;
5296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005298 {
5299#ifdef FEAT_FLOAT
5300 if (rettv->v_type == VAR_FLOAT)
5301 f = -f;
5302 else
5303#endif
5304 val = -val;
5305 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005307#ifdef FEAT_FLOAT
5308 if (rettv->v_type == VAR_FLOAT)
5309 {
5310 clear_tv(rettv);
5311 rettv->vval.v_float = f;
5312 }
5313 else
5314#endif
5315 {
5316 clear_tv(rettv);
5317 rettv->v_type = VAR_NUMBER;
5318 rettv->vval.v_number = val;
5319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
5322
5323 return ret;
5324}
5325
5326/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005327 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5328 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5330 */
5331 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005332eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005334 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005336 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337{
5338 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005339 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 long len = -1;
5342 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005344 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005346 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005348 if (verbose)
5349 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 return FAIL;
5351 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005352#ifdef FEAT_FLOAT
5353 else if (rettv->v_type == VAR_FLOAT)
5354 {
5355 if (verbose)
5356 EMSG(_(e_float_as_string));
5357 return FAIL;
5358 }
5359#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 /*
5364 * dict.name
5365 */
5366 key = *arg + 1;
5367 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5368 ;
5369 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 }
5373 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005375 /*
5376 * something[idx]
5377 *
5378 * Get the (first) variable from inside the [].
5379 */
5380 *arg = skipwhite(*arg + 1);
5381 if (**arg == ':')
5382 empty1 = TRUE;
5383 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5384 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005385 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5386 {
5387 /* not a number or string */
5388 clear_tv(&var1);
5389 return FAIL;
5390 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391
5392 /*
5393 * Get the second variable from inside the [:].
5394 */
5395 if (**arg == ':')
5396 {
5397 range = TRUE;
5398 *arg = skipwhite(*arg + 1);
5399 if (**arg == ']')
5400 empty2 = TRUE;
5401 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005403 if (!empty1)
5404 clear_tv(&var1);
5405 return FAIL;
5406 }
5407 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5408 {
5409 /* not a number or string */
5410 if (!empty1)
5411 clear_tv(&var1);
5412 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005413 return FAIL;
5414 }
5415 }
5416
5417 /* Check for the ']'. */
5418 if (**arg != ']')
5419 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005420 if (verbose)
5421 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422 clear_tv(&var1);
5423 if (range)
5424 clear_tv(&var2);
5425 return FAIL;
5426 }
5427 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 }
5429
5430 if (evaluate)
5431 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005432 n1 = 0;
5433 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005435 n1 = get_tv_number(&var1);
5436 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 }
5438 if (range)
5439 {
5440 if (empty2)
5441 n2 = -1;
5442 else
5443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444 n2 = get_tv_number(&var2);
5445 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 }
5447 }
5448
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005449 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 {
5451 case VAR_NUMBER:
5452 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005453 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 len = (long)STRLEN(s);
5455 if (range)
5456 {
5457 /* The resulting variable is a substring. If the indexes
5458 * are out of range the result is empty. */
5459 if (n1 < 0)
5460 {
5461 n1 = len + n1;
5462 if (n1 < 0)
5463 n1 = 0;
5464 }
5465 if (n2 < 0)
5466 n2 = len + n2;
5467 else if (n2 >= len)
5468 n2 = len;
5469 if (n1 >= len || n2 < 0 || n1 > n2)
5470 s = NULL;
5471 else
5472 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5473 }
5474 else
5475 {
5476 /* The resulting variable is a string of a single
5477 * character. If the index is too big or negative the
5478 * result is empty. */
5479 if (n1 >= len || n1 < 0)
5480 s = NULL;
5481 else
5482 s = vim_strnsave(s + n1, 1);
5483 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005484 clear_tv(rettv);
5485 rettv->v_type = VAR_STRING;
5486 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 break;
5488
5489 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 if (n1 < 0)
5492 n1 = len + n1;
5493 if (!empty1 && (n1 < 0 || n1 >= len))
5494 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005495 /* For a range we allow invalid values and return an empty
5496 * list. A list index out of range is an error. */
5497 if (!range)
5498 {
5499 if (verbose)
5500 EMSGN(_(e_listidx), n1);
5501 return FAIL;
5502 }
5503 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 }
5505 if (range)
5506 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005507 list_T *l;
5508 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509
5510 if (n2 < 0)
5511 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005512 else if (n2 >= len)
5513 n2 = len - 1;
5514 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005515 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 l = list_alloc();
5517 if (l == NULL)
5518 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 n1 <= n2; ++n1)
5521 {
5522 if (list_append_tv(l, &item->li_tv) == FAIL)
5523 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005524 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 return FAIL;
5526 }
5527 item = item->li_next;
5528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 clear_tv(rettv);
5530 rettv->v_type = VAR_LIST;
5531 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005532 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 }
5534 else
5535 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005536 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 clear_tv(rettv);
5538 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005539 }
5540 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005541
5542 case VAR_DICT:
5543 if (range)
5544 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005545 if (verbose)
5546 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005547 if (len == -1)
5548 clear_tv(&var1);
5549 return FAIL;
5550 }
5551 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005553
5554 if (len == -1)
5555 {
5556 key = get_tv_string(&var1);
5557 if (*key == NUL)
5558 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005559 if (verbose)
5560 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561 clear_tv(&var1);
5562 return FAIL;
5563 }
5564 }
5565
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005566 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005567
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005568 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005569 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570 if (len == -1)
5571 clear_tv(&var1);
5572 if (item == NULL)
5573 return FAIL;
5574
5575 copy_tv(&item->di_tv, &var1);
5576 clear_tv(rettv);
5577 *rettv = var1;
5578 }
5579 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 }
5581 }
5582
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583 return OK;
5584}
5585
5586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 * Get an option value.
5588 * "arg" points to the '&' or '+' before the option name.
5589 * "arg" is advanced to character after the option name.
5590 * Return OK or FAIL.
5591 */
5592 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005595 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 int evaluate;
5597{
5598 char_u *option_end;
5599 long numval;
5600 char_u *stringval;
5601 int opt_type;
5602 int c;
5603 int working = (**arg == '+'); /* has("+option") */
5604 int ret = OK;
5605 int opt_flags;
5606
5607 /*
5608 * Isolate the option name and find its value.
5609 */
5610 option_end = find_option_end(arg, &opt_flags);
5611 if (option_end == NULL)
5612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 EMSG2(_("E112: Option name missing: %s"), *arg);
5615 return FAIL;
5616 }
5617
5618 if (!evaluate)
5619 {
5620 *arg = option_end;
5621 return OK;
5622 }
5623
5624 c = *option_end;
5625 *option_end = NUL;
5626 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005627 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
5629 if (opt_type == -3) /* invalid name */
5630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 EMSG2(_("E113: Unknown option: %s"), *arg);
5633 ret = FAIL;
5634 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 {
5637 if (opt_type == -2) /* hidden string option */
5638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 rettv->v_type = VAR_STRING;
5640 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642 else if (opt_type == -1) /* hidden number option */
5643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 rettv->v_type = VAR_NUMBER;
5645 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
5647 else if (opt_type == 1) /* number option */
5648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 rettv->v_type = VAR_NUMBER;
5650 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652 else /* string option */
5653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005654 rettv->v_type = VAR_STRING;
5655 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
5657 }
5658 else if (working && (opt_type == -2 || opt_type == -1))
5659 ret = FAIL;
5660
5661 *option_end = c; /* put back for error messages */
5662 *arg = option_end;
5663
5664 return ret;
5665}
5666
5667/*
5668 * Allocate a variable for a string constant.
5669 * Return OK or FAIL.
5670 */
5671 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 int evaluate;
5676{
5677 char_u *p;
5678 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 int extra = 0;
5680
5681 /*
5682 * Find the end of the string, skipping backslashed characters.
5683 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005684 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
5686 if (*p == '\\' && p[1] != NUL)
5687 {
5688 ++p;
5689 /* A "\<x>" form occupies at least 4 characters, and produces up
5690 * to 6 characters: reserve space for 2 extra */
5691 if (*p == '<')
5692 extra += 2;
5693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695
5696 if (*p != '"')
5697 {
5698 EMSG2(_("E114: Missing quote: %s"), *arg);
5699 return FAIL;
5700 }
5701
5702 /* If only parsing, set *arg and return here */
5703 if (!evaluate)
5704 {
5705 *arg = p + 1;
5706 return OK;
5707 }
5708
5709 /*
5710 * Copy the string into allocated memory, handling backslashed
5711 * characters.
5712 */
5713 name = alloc((unsigned)(p - *arg + extra));
5714 if (name == NULL)
5715 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 rettv->v_type = VAR_STRING;
5717 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 if (*p == '\\')
5722 {
5723 switch (*++p)
5724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 case 'b': *name++ = BS; ++p; break;
5726 case 'e': *name++ = ESC; ++p; break;
5727 case 'f': *name++ = FF; ++p; break;
5728 case 'n': *name++ = NL; ++p; break;
5729 case 'r': *name++ = CAR; ++p; break;
5730 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732 case 'X': /* hex: "\x1", "\x12" */
5733 case 'x':
5734 case 'u': /* Unicode: "\u0023" */
5735 case 'U':
5736 if (vim_isxdigit(p[1]))
5737 {
5738 int n, nr;
5739 int c = toupper(*p);
5740
5741 if (c == 'X')
5742 n = 2;
5743 else
5744 n = 4;
5745 nr = 0;
5746 while (--n >= 0 && vim_isxdigit(p[1]))
5747 {
5748 ++p;
5749 nr = (nr << 4) + hex2nr(*p);
5750 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752#ifdef FEAT_MBYTE
5753 /* For "\u" store the number according to
5754 * 'encoding'. */
5755 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 else
5758#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 break;
5762
5763 /* octal: "\1", "\12", "\123" */
5764 case '0':
5765 case '1':
5766 case '2':
5767 case '3':
5768 case '4':
5769 case '5':
5770 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 case '7': *name = *p++ - '0';
5772 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 *name = (*name << 3) + *p++ - '0';
5775 if (*p >= '0' && *p <= '7')
5776 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 break;
5780
5781 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 if (extra != 0)
5784 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 break;
5787 }
5788 /* FALLTHROUGH */
5789
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 break;
5792 }
5793 }
5794 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 *arg = p + 1;
5800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 return OK;
5802}
5803
5804/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 * Return OK or FAIL.
5807 */
5808 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005809get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 int evaluate;
5813{
5814 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005816 int reduce = 0;
5817
5818 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 */
5821 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5822 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005824 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005826 break;
5827 ++reduce;
5828 ++p;
5829 }
5830 }
5831
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 return FAIL;
5836 }
5837
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 if (!evaluate)
5840 {
5841 *arg = p + 1;
5842 return OK;
5843 }
5844
5845 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 */
5848 str = alloc((unsigned)((p - *arg) - reduce));
5849 if (str == NULL)
5850 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 rettv->v_type = VAR_STRING;
5852 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 break;
5860 ++p;
5861 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 *arg = p + 1;
5866
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 return OK;
5868}
5869
5870/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 * Allocate a variable for a List and fill it from "*arg".
5872 * Return OK or FAIL.
5873 */
5874 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005875get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878 int evaluate;
5879{
Bram Moolenaar33570922005-01-25 22:26:29 +00005880 list_T *l = NULL;
5881 typval_T tv;
5882 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883
5884 if (evaluate)
5885 {
5886 l = list_alloc();
5887 if (l == NULL)
5888 return FAIL;
5889 }
5890
5891 *arg = skipwhite(*arg + 1);
5892 while (**arg != ']' && **arg != NUL)
5893 {
5894 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5895 goto failret;
5896 if (evaluate)
5897 {
5898 item = listitem_alloc();
5899 if (item != NULL)
5900 {
5901 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005902 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 list_append(l, item);
5904 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005905 else
5906 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 }
5908
5909 if (**arg == ']')
5910 break;
5911 if (**arg != ',')
5912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005913 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 goto failret;
5915 }
5916 *arg = skipwhite(*arg + 1);
5917 }
5918
5919 if (**arg != ']')
5920 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922failret:
5923 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005924 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 return FAIL;
5926 }
5927
5928 *arg = skipwhite(*arg + 1);
5929 if (evaluate)
5930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005931 rettv->v_type = VAR_LIST;
5932 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 ++l->lv_refcount;
5934 }
5935
5936 return OK;
5937}
5938
5939/*
5940 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005941 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005943 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944list_alloc()
5945{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005946 list_T *l;
5947
5948 l = (list_T *)alloc_clear(sizeof(list_T));
5949 if (l != NULL)
5950 {
5951 /* Prepend the list to the list of lists for garbage collection. */
5952 if (first_list != NULL)
5953 first_list->lv_used_prev = l;
5954 l->lv_used_prev = NULL;
5955 l->lv_used_next = first_list;
5956 first_list = l;
5957 }
5958 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959}
5960
5961/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005962 * Allocate an empty list for a return value.
5963 * Returns OK or FAIL.
5964 */
5965 static int
5966rettv_list_alloc(rettv)
5967 typval_T *rettv;
5968{
5969 list_T *l = list_alloc();
5970
5971 if (l == NULL)
5972 return FAIL;
5973
5974 rettv->vval.v_list = l;
5975 rettv->v_type = VAR_LIST;
5976 ++l->lv_refcount;
5977 return OK;
5978}
5979
5980/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 * Unreference a list: decrement the reference count and free it when it
5982 * becomes zero.
5983 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005984 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005988 if (l != NULL && --l->lv_refcount <= 0)
5989 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990}
5991
5992/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005993 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 * Ignores the reference count.
5995 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005996 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005997list_free(l, recurse)
5998 list_T *l;
5999 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000{
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006003 /* Remove the list from the list of lists for garbage collection. */
6004 if (l->lv_used_prev == NULL)
6005 first_list = l->lv_used_next;
6006 else
6007 l->lv_used_prev->lv_used_next = l->lv_used_next;
6008 if (l->lv_used_next != NULL)
6009 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6010
Bram Moolenaard9fba312005-06-26 22:34:35 +00006011 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006013 /* Remove the item before deleting it. */
6014 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006015 if (recurse || (item->li_tv.v_type != VAR_LIST
6016 && item->li_tv.v_type != VAR_DICT))
6017 clear_tv(&item->li_tv);
6018 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 }
6020 vim_free(l);
6021}
6022
6023/*
6024 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006025 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006027 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028listitem_alloc()
6029{
Bram Moolenaar33570922005-01-25 22:26:29 +00006030 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031}
6032
6033/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006034 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006036 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006040 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041 vim_free(item);
6042}
6043
6044/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006045 * Remove a list item from a List and free it. Also clears the value.
6046 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006047 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006048listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 list_T *l;
6050 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006051{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006052 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006053 listitem_free(item);
6054}
6055
6056/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057 * Get the number of items in a list.
6058 */
6059 static long
6060list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 if (l == NULL)
6064 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006065 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066}
6067
6068/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006069 * Return TRUE when two lists have exactly the same values.
6070 */
6071 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006072list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006073 list_T *l1;
6074 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006077{
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006079
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006080 if (l1 == NULL || l2 == NULL)
6081 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006082 if (l1 == l2)
6083 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006084 if (list_len(l1) != list_len(l2))
6085 return FALSE;
6086
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087 for (item1 = l1->lv_first, item2 = l2->lv_first;
6088 item1 != NULL && item2 != NULL;
6089 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006091 return FALSE;
6092 return item1 == NULL && item2 == NULL;
6093}
6094
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006095#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6096 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006097/*
6098 * Return the dictitem that an entry in a hashtable points to.
6099 */
6100 dictitem_T *
6101dict_lookup(hi)
6102 hashitem_T *hi;
6103{
6104 return HI2DI(hi);
6105}
6106#endif
6107
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006109 * Return TRUE when two dictionaries have exactly the same key/values.
6110 */
6111 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006112dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 dict_T *d1;
6114 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006115 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006116 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006117{
Bram Moolenaar33570922005-01-25 22:26:29 +00006118 hashitem_T *hi;
6119 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006120 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006121
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006122 if (d1 == NULL || d2 == NULL)
6123 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006124 if (d1 == d2)
6125 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006126 if (dict_len(d1) != dict_len(d2))
6127 return FALSE;
6128
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006129 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006132 if (!HASHITEM_EMPTY(hi))
6133 {
6134 item2 = dict_find(d2, hi->hi_key, -1);
6135 if (item2 == NULL)
6136 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006137 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006138 return FALSE;
6139 --todo;
6140 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141 }
6142 return TRUE;
6143}
6144
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006145static int tv_equal_recurse_limit;
6146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006147/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006148 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006149 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006150 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006151 */
6152 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006153tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006154 typval_T *tv1;
6155 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006156 int ic; /* ignore case */
6157 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006158{
6159 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006160 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006161 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006162 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006163
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006164 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006165 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006166
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006167 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168 * recursiveness to a limit. We guess they are equal then.
6169 * A fixed limit has the problem of still taking an awful long time.
6170 * Reduce the limit every time running into it. That should work fine for
6171 * deeply linked structures that are not recursively linked and catch
6172 * recursiveness quickly. */
6173 if (!recursive)
6174 tv_equal_recurse_limit = 1000;
6175 if (recursive_cnt >= tv_equal_recurse_limit)
6176 {
6177 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006178 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006180
6181 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006183 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184 ++recursive_cnt;
6185 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6186 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006187 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006188
6189 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190 ++recursive_cnt;
6191 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6192 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006193 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006194
6195 case VAR_FUNC:
6196 return (tv1->vval.v_string != NULL
6197 && tv2->vval.v_string != NULL
6198 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6199
6200 case VAR_NUMBER:
6201 return tv1->vval.v_number == tv2->vval.v_number;
6202
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006203#ifdef FEAT_FLOAT
6204 case VAR_FLOAT:
6205 return tv1->vval.v_float == tv2->vval.v_float;
6206#endif
6207
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006208 case VAR_STRING:
6209 s1 = get_tv_string_buf(tv1, buf1);
6210 s2 = get_tv_string_buf(tv2, buf2);
6211 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006212 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213
6214 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006215 return TRUE;
6216}
6217
6218/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006219 * Locate item with index "n" in list "l" and return it.
6220 * A negative index is counted from the end; -1 is the last item.
6221 * Returns NULL when "n" is out of range.
6222 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006223 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006225 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006226 long n;
6227{
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006229 long idx;
6230
6231 if (l == NULL)
6232 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006233
6234 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006235 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006236 n = l->lv_len + n;
6237
6238 /* Check for index out of range. */
6239 if (n < 0 || n >= l->lv_len)
6240 return NULL;
6241
6242 /* When there is a cached index may start search from there. */
6243 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006244 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006245 if (n < l->lv_idx / 2)
6246 {
6247 /* closest to the start of the list */
6248 item = l->lv_first;
6249 idx = 0;
6250 }
6251 else if (n > (l->lv_idx + l->lv_len) / 2)
6252 {
6253 /* closest to the end of the list */
6254 item = l->lv_last;
6255 idx = l->lv_len - 1;
6256 }
6257 else
6258 {
6259 /* closest to the cached index */
6260 item = l->lv_idx_item;
6261 idx = l->lv_idx;
6262 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 }
6264 else
6265 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006266 if (n < l->lv_len / 2)
6267 {
6268 /* closest to the start of the list */
6269 item = l->lv_first;
6270 idx = 0;
6271 }
6272 else
6273 {
6274 /* closest to the end of the list */
6275 item = l->lv_last;
6276 idx = l->lv_len - 1;
6277 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279
6280 while (n > idx)
6281 {
6282 /* search forward */
6283 item = item->li_next;
6284 ++idx;
6285 }
6286 while (n < idx)
6287 {
6288 /* search backward */
6289 item = item->li_prev;
6290 --idx;
6291 }
6292
6293 /* cache the used index */
6294 l->lv_idx = idx;
6295 l->lv_idx_item = item;
6296
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 return item;
6298}
6299
6300/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006301 * Get list item "l[idx]" as a number.
6302 */
6303 static long
6304list_find_nr(l, idx, errorp)
6305 list_T *l;
6306 long idx;
6307 int *errorp; /* set to TRUE when something wrong */
6308{
6309 listitem_T *li;
6310
6311 li = list_find(l, idx);
6312 if (li == NULL)
6313 {
6314 if (errorp != NULL)
6315 *errorp = TRUE;
6316 return -1L;
6317 }
6318 return get_tv_number_chk(&li->li_tv, errorp);
6319}
6320
6321/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006322 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6323 */
6324 char_u *
6325list_find_str(l, idx)
6326 list_T *l;
6327 long idx;
6328{
6329 listitem_T *li;
6330
6331 li = list_find(l, idx - 1);
6332 if (li == NULL)
6333 {
6334 EMSGN(_(e_listidx), idx);
6335 return NULL;
6336 }
6337 return get_tv_string(&li->li_tv);
6338}
6339
6340/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006341 * Locate "item" list "l" and return its index.
6342 * Returns -1 when "item" is not in the list.
6343 */
6344 static long
6345list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 list_T *l;
6347 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006348{
6349 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006351
6352 if (l == NULL)
6353 return -1;
6354 idx = 0;
6355 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6356 ++idx;
6357 if (li == NULL)
6358 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006359 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006360}
6361
6362/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006363 * Append item "item" to the end of list "l".
6364 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006365 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006366list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 list_T *l;
6368 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006369{
6370 if (l->lv_last == NULL)
6371 {
6372 /* empty list */
6373 l->lv_first = item;
6374 l->lv_last = item;
6375 item->li_prev = NULL;
6376 }
6377 else
6378 {
6379 l->lv_last->li_next = item;
6380 item->li_prev = l->lv_last;
6381 l->lv_last = item;
6382 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006383 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384 item->li_next = NULL;
6385}
6386
6387/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006389 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006391 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 list_T *l;
6394 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006395{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006396 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397
Bram Moolenaar05159a02005-02-26 23:04:13 +00006398 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006400 copy_tv(tv, &li->li_tv);
6401 list_append(l, li);
6402 return OK;
6403}
6404
6405/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006406 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006407 * Return FAIL when out of memory.
6408 */
6409 int
6410list_append_dict(list, dict)
6411 list_T *list;
6412 dict_T *dict;
6413{
6414 listitem_T *li = listitem_alloc();
6415
6416 if (li == NULL)
6417 return FAIL;
6418 li->li_tv.v_type = VAR_DICT;
6419 li->li_tv.v_lock = 0;
6420 li->li_tv.vval.v_dict = dict;
6421 list_append(list, li);
6422 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 return OK;
6424}
6425
6426/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006427 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006428 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006429 * Returns FAIL when out of memory.
6430 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006431 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006432list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006433 list_T *l;
6434 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006435 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006436{
6437 listitem_T *li = listitem_alloc();
6438
6439 if (li == NULL)
6440 return FAIL;
6441 list_append(l, li);
6442 li->li_tv.v_type = VAR_STRING;
6443 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006444 if (str == NULL)
6445 li->li_tv.vval.v_string = NULL;
6446 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006447 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448 return FAIL;
6449 return OK;
6450}
6451
6452/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006453 * Append "n" to list "l".
6454 * Returns FAIL when out of memory.
6455 */
6456 static int
6457list_append_number(l, n)
6458 list_T *l;
6459 varnumber_T n;
6460{
6461 listitem_T *li;
6462
6463 li = listitem_alloc();
6464 if (li == NULL)
6465 return FAIL;
6466 li->li_tv.v_type = VAR_NUMBER;
6467 li->li_tv.v_lock = 0;
6468 li->li_tv.vval.v_number = n;
6469 list_append(l, li);
6470 return OK;
6471}
6472
6473/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006474 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475 * If "item" is NULL append at the end.
6476 * Return FAIL when out of memory.
6477 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006478 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006480 list_T *l;
6481 typval_T *tv;
6482 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006483{
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006485
6486 if (ni == NULL)
6487 return FAIL;
6488 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006489 list_insert(l, ni, item);
6490 return OK;
6491}
6492
6493 void
6494list_insert(l, ni, item)
6495 list_T *l;
6496 listitem_T *ni;
6497 listitem_T *item;
6498{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499 if (item == NULL)
6500 /* Append new item at end of list. */
6501 list_append(l, ni);
6502 else
6503 {
6504 /* Insert new item before existing item. */
6505 ni->li_prev = item->li_prev;
6506 ni->li_next = item;
6507 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006508 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006510 ++l->lv_idx;
6511 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006513 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006514 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006515 l->lv_idx_item = NULL;
6516 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006518 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520}
6521
6522/*
6523 * Extend "l1" with "l2".
6524 * If "bef" is NULL append at the end, otherwise insert before this item.
6525 * Returns FAIL when out of memory.
6526 */
6527 static int
6528list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006529 list_T *l1;
6530 list_T *l2;
6531 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532{
Bram Moolenaar33570922005-01-25 22:26:29 +00006533 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006534 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006536 /* We also quit the loop when we have inserted the original item count of
6537 * the list, avoid a hang when we extend a list with itself. */
6538 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6540 return FAIL;
6541 return OK;
6542}
6543
6544/*
6545 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6546 * Return FAIL when out of memory.
6547 */
6548 static int
6549list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006550 list_T *l1;
6551 list_T *l2;
6552 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553{
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006556 if (l1 == NULL || l2 == NULL)
6557 return FAIL;
6558
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 if (l == NULL)
6562 return FAIL;
6563 tv->v_type = VAR_LIST;
6564 tv->vval.v_list = l;
6565
6566 /* append all items from the second list */
6567 return list_extend(l, l2, NULL);
6568}
6569
6570/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 * Returns NULL when out of memory.
6575 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006577list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581{
Bram Moolenaar33570922005-01-25 22:26:29 +00006582 list_T *copy;
6583 listitem_T *item;
6584 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585
6586 if (orig == NULL)
6587 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588
6589 copy = list_alloc();
6590 if (copy != NULL)
6591 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006592 if (copyID != 0)
6593 {
6594 /* Do this before adding the items, because one of the items may
6595 * refer back to this list. */
6596 orig->lv_copyID = copyID;
6597 orig->lv_copylist = copy;
6598 }
6599 for (item = orig->lv_first; item != NULL && !got_int;
6600 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601 {
6602 ni = listitem_alloc();
6603 if (ni == NULL)
6604 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006606 {
6607 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6608 {
6609 vim_free(ni);
6610 break;
6611 }
6612 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006614 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615 list_append(copy, ni);
6616 }
6617 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006618 if (item != NULL)
6619 {
6620 list_unref(copy);
6621 copy = NULL;
6622 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 }
6624
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 return copy;
6626}
6627
6628/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006629 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006630 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006631 * This used to be called list_remove, but that conflicts with a Sun header
6632 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006634 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006635vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006636 list_T *l;
6637 listitem_T *item;
6638 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006639{
Bram Moolenaar33570922005-01-25 22:26:29 +00006640 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006642 /* notify watchers */
6643 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006645 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646 list_fix_watch(l, ip);
6647 if (ip == item2)
6648 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006650
6651 if (item2->li_next == NULL)
6652 l->lv_last = item->li_prev;
6653 else
6654 item2->li_next->li_prev = item->li_prev;
6655 if (item->li_prev == NULL)
6656 l->lv_first = item2->li_next;
6657 else
6658 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006659 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660}
6661
6662/*
6663 * Return an allocated string with the string representation of a list.
6664 * May return NULL.
6665 */
6666 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006667list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006668 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006669 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670{
6671 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672
6673 if (tv->vval.v_list == NULL)
6674 return NULL;
6675 ga_init2(&ga, (int)sizeof(char), 80);
6676 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006677 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006678 {
6679 vim_free(ga.ga_data);
6680 return NULL;
6681 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682 ga_append(&ga, ']');
6683 ga_append(&ga, NUL);
6684 return (char_u *)ga.ga_data;
6685}
6686
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006687typedef struct join_S {
6688 char_u *s;
6689 char_u *tofree;
6690} join_T;
6691
6692 static int
6693list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6694 garray_T *gap; /* to store the result in */
6695 list_T *l;
6696 char_u *sep;
6697 int echo_style;
6698 int copyID;
6699 garray_T *join_gap; /* to keep each list item string */
6700{
6701 int i;
6702 join_T *p;
6703 int len;
6704 int sumlen = 0;
6705 int first = TRUE;
6706 char_u *tofree;
6707 char_u numbuf[NUMBUFLEN];
6708 listitem_T *item;
6709 char_u *s;
6710
6711 /* Stringify each item in the list. */
6712 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6713 {
6714 if (echo_style)
6715 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6716 else
6717 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6718 if (s == NULL)
6719 return FAIL;
6720
6721 len = (int)STRLEN(s);
6722 sumlen += len;
6723
6724 ga_grow(join_gap, 1);
6725 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6726 if (tofree != NULL || s != numbuf)
6727 {
6728 p->s = s;
6729 p->tofree = tofree;
6730 }
6731 else
6732 {
6733 p->s = vim_strnsave(s, len);
6734 p->tofree = p->s;
6735 }
6736
6737 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006738 if (did_echo_string_emsg) /* recursion error, bail out */
6739 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006740 }
6741
6742 /* Allocate result buffer with its total size, avoid re-allocation and
6743 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6744 if (join_gap->ga_len >= 2)
6745 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6746 if (ga_grow(gap, sumlen + 2) == FAIL)
6747 return FAIL;
6748
6749 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6750 {
6751 if (first)
6752 first = FALSE;
6753 else
6754 ga_concat(gap, sep);
6755 p = ((join_T *)join_gap->ga_data) + i;
6756
6757 if (p->s != NULL)
6758 ga_concat(gap, p->s);
6759 line_breakcheck();
6760 }
6761
6762 return OK;
6763}
6764
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006765/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006766 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006767 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006768 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006769 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006770 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006771list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006772 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006773 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006775 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006776 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006777{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006778 garray_T join_ga;
6779 int retval;
6780 join_T *p;
6781 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782
Bram Moolenaard39a7512015-04-16 22:51:22 +02006783 if (l->lv_len < 1)
6784 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6786 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6787
6788 /* Dispose each item in join_ga. */
6789 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791 p = (join_T *)join_ga.ga_data;
6792 for (i = 0; i < join_ga.ga_len; ++i)
6793 {
6794 vim_free(p->tofree);
6795 ++p;
6796 }
6797 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006799
6800 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801}
6802
6803/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006804 * Garbage collection for lists and dictionaries.
6805 *
6806 * We use reference counts to be able to free most items right away when they
6807 * are no longer used. But for composite items it's possible that it becomes
6808 * unused while the reference count is > 0: When there is a recursive
6809 * reference. Example:
6810 * :let l = [1, 2, 3]
6811 * :let d = {9: l}
6812 * :let l[1] = d
6813 *
6814 * Since this is quite unusual we handle this with garbage collection: every
6815 * once in a while find out which lists and dicts are not referenced from any
6816 * variable.
6817 *
6818 * Here is a good reference text about garbage collection (refers to Python
6819 * but it applies to all reference-counting mechanisms):
6820 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006821 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006822
6823/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006824 * Do garbage collection for lists and dicts.
6825 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006826 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006827 int
6828garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006830 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006831 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 buf_T *buf;
6833 win_T *wp;
6834 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006835 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006836 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006837 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006838#ifdef FEAT_WINDOWS
6839 tabpage_T *tp;
6840#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006842 /* Only do this once. */
6843 want_garbage_collect = FALSE;
6844 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006845 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006846
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847 /* We advance by two because we add one for items referenced through
6848 * previous_funccal. */
6849 current_copyID += COPYID_INC;
6850 copyID = current_copyID;
6851
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852 /*
6853 * 1. Go through all accessible variables and mark all lists and dicts
6854 * with copyID.
6855 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006856
6857 /* Don't free variables in the previous_funccal list unless they are only
6858 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006859 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6861 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006862 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6863 NULL);
6864 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6865 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866 }
6867
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006868 /* script-local variables */
6869 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006870 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006871
6872 /* buffer-local variables */
6873 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6875 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876
6877 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006878 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6880 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006881#ifdef FEAT_AUTOCMD
6882 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6884 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006885#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006887#ifdef FEAT_WINDOWS
6888 /* tabpage-local variables */
6889 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6891 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006892#endif
6893
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006895 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896
6897 /* function-local variables */
6898 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6899 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6901 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 }
6903
Bram Moolenaard812df62008-11-09 12:46:09 +00006904 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006906
Bram Moolenaar1dced572012-04-05 16:54:08 +02006907#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006909#endif
6910
Bram Moolenaardb913952012-06-29 12:54:53 +02006911#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006913#endif
6914
6915#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006917#endif
6918
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006919 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006920 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 /*
6922 * 2. Free lists and dictionaries that are not referenced.
6923 */
6924 did_free = free_unref_items(copyID);
6925
6926 /*
6927 * 3. Check if any funccal can be freed now.
6928 */
6929 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006930 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 if (can_free_funccal(*pfc, copyID))
6932 {
6933 fc = *pfc;
6934 *pfc = fc->caller;
6935 free_funccal(fc, TRUE);
6936 did_free = TRUE;
6937 did_free_funccal = TRUE;
6938 }
6939 else
6940 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006941 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 if (did_free_funccal)
6943 /* When a funccal was freed some more items might be garbage
6944 * collected, so run again. */
6945 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006946 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006947 else if (p_verbose > 0)
6948 {
6949 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6950 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951
6952 return did_free;
6953}
6954
6955/*
6956 * Free lists and dictionaries that are no longer referenced.
6957 */
6958 static int
6959free_unref_items(copyID)
6960 int copyID;
6961{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006962 dict_T *dd, *dd_next;
6963 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 int did_free = FALSE;
6965
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968 */
6969 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006970 {
6971 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006974 /* Free the Dictionary and ordinary items it contains, but don't
6975 * recurse into Lists and Dictionaries, they will be in the list
6976 * of dicts or list of lists. */
6977 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006980 dd = dd_next;
6981 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982
6983 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984 * Go through the list of lists and free items without the copyID.
6985 * But don't free a list that has a watcher (used in a for loop), these
6986 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987 */
6988 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006989 {
6990 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6992 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006994 /* Free the List and ordinary items it contains, but don't recurse
6995 * into Lists and Dictionaries, they will be in the list of dicts
6996 * or list of lists. */
6997 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007000 ll = ll_next;
7001 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 return did_free;
7003}
7004
7005/*
7006 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007007 * "list_stack" is used to add lists to be marked. Can be NULL.
7008 *
7009 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007011 int
7012set_ref_in_ht(ht, copyID, list_stack)
7013 hashtab_T *ht;
7014 int copyID;
7015 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016{
7017 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007018 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 hashtab_T *cur_ht;
7021 ht_stack_T *ht_stack = NULL;
7022 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024 cur_ht = ht;
7025 for (;;)
7026 {
7027 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 /* Mark each item in the hashtab. If the item contains a hashtab
7030 * it is added to ht_stack, if it contains a list it is added to
7031 * list_stack. */
7032 todo = (int)cur_ht->ht_used;
7033 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7034 if (!HASHITEM_EMPTY(hi))
7035 {
7036 --todo;
7037 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7038 &ht_stack, list_stack);
7039 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041
7042 if (ht_stack == NULL)
7043 break;
7044
7045 /* take an item from the stack */
7046 cur_ht = ht_stack->ht;
7047 tempitem = ht_stack;
7048 ht_stack = ht_stack->prev;
7049 free(tempitem);
7050 }
7051
7052 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053}
7054
7055/*
7056 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7058 *
7059 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007061 int
7062set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 list_T *l;
7064 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 listitem_T *li;
7068 int abort = FALSE;
7069 list_T *cur_l;
7070 list_stack_T *list_stack = NULL;
7071 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007072
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 cur_l = l;
7074 for (;;)
7075 {
7076 if (!abort)
7077 /* Mark each item in the list. If the item contains a hashtab
7078 * it is added to ht_stack, if it contains a list it is added to
7079 * list_stack. */
7080 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7081 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7082 ht_stack, &list_stack);
7083 if (list_stack == NULL)
7084 break;
7085
7086 /* take an item from the stack */
7087 cur_l = list_stack->list;
7088 tempitem = list_stack;
7089 list_stack = list_stack->prev;
7090 free(tempitem);
7091 }
7092
7093 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094}
7095
7096/*
7097 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007098 * "list_stack" is used to add lists to be marked. Can be NULL.
7099 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7100 *
7101 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 int
7104set_ref_in_item(tv, copyID, ht_stack, list_stack)
7105 typval_T *tv;
7106 int copyID;
7107 ht_stack_T **ht_stack;
7108 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109{
7110 dict_T *dd;
7111 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007112 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007113
7114 switch (tv->v_type)
7115 {
7116 case VAR_DICT:
7117 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007118 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007119 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 /* Didn't see this dict yet. */
7121 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007122 if (ht_stack == NULL)
7123 {
7124 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7125 }
7126 else
7127 {
7128 ht_stack_T *newitem = (ht_stack_T*)malloc(
7129 sizeof(ht_stack_T));
7130 if (newitem == NULL)
7131 abort = TRUE;
7132 else
7133 {
7134 newitem->ht = &dd->dv_hashtab;
7135 newitem->prev = *ht_stack;
7136 *ht_stack = newitem;
7137 }
7138 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007139 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007140 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007141
7142 case VAR_LIST:
7143 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007144 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007145 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007146 /* Didn't see this list yet. */
7147 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007148 if (list_stack == NULL)
7149 {
7150 abort = set_ref_in_list(ll, copyID, ht_stack);
7151 }
7152 else
7153 {
7154 list_stack_T *newitem = (list_stack_T*)malloc(
7155 sizeof(list_stack_T));
7156 if (newitem == NULL)
7157 abort = TRUE;
7158 else
7159 {
7160 newitem->list = ll;
7161 newitem->prev = *list_stack;
7162 *list_stack = newitem;
7163 }
7164 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007165 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007166 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007167 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007168 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007169}
7170
7171/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 * Allocate an empty header for a dictionary.
7173 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007174 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175dict_alloc()
7176{
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007178
Bram Moolenaar33570922005-01-25 22:26:29 +00007179 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007180 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007181 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007182 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007183 if (first_dict != NULL)
7184 first_dict->dv_used_prev = d;
7185 d->dv_used_next = first_dict;
7186 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007187 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007190 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007191 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007192 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007193 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007196}
7197
7198/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007199 * Allocate an empty dict for a return value.
7200 * Returns OK or FAIL.
7201 */
7202 static int
7203rettv_dict_alloc(rettv)
7204 typval_T *rettv;
7205{
7206 dict_T *d = dict_alloc();
7207
7208 if (d == NULL)
7209 return FAIL;
7210
7211 rettv->vval.v_dict = d;
7212 rettv->v_type = VAR_DICT;
7213 ++d->dv_refcount;
7214 return OK;
7215}
7216
7217
7218/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 * Unreference a Dictionary: decrement the reference count and free it when it
7220 * becomes zero.
7221 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007222 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007224 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007226 if (d != NULL && --d->dv_refcount <= 0)
7227 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228}
7229
7230/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007231 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 * Ignores the reference count.
7233 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007234 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007235dict_free(d, recurse)
7236 dict_T *d;
7237 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007240 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007241 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007243 /* Remove the dict from the list of dicts for garbage collection. */
7244 if (d->dv_used_prev == NULL)
7245 first_dict = d->dv_used_next;
7246 else
7247 d->dv_used_prev->dv_used_next = d->dv_used_next;
7248 if (d->dv_used_next != NULL)
7249 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7250
7251 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007252 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007253 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007256 if (!HASHITEM_EMPTY(hi))
7257 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007258 /* Remove the item before deleting it, just in case there is
7259 * something recursive causing trouble. */
7260 di = HI2DI(hi);
7261 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007262 if (recurse || (di->di_tv.v_type != VAR_LIST
7263 && di->di_tv.v_type != VAR_DICT))
7264 clear_tv(&di->di_tv);
7265 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007266 --todo;
7267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 vim_free(d);
7271}
7272
7273/*
7274 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275 * The "key" is copied to the new item.
7276 * Note that the value of the item "di_tv" still needs to be initialized!
7277 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007279 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280dictitem_alloc(key)
7281 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282{
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007285 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007289 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007290 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007291 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292}
7293
7294/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295 * Make a copy of a Dictionary item.
7296 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300{
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7304 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 if (di != NULL)
7306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007308 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309 copy_tv(&org->di_tv, &di->di_tv);
7310 }
7311 return di;
7312}
7313
7314/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 * Remove item "item" from Dictionary "dict" and free it.
7316 */
7317 static void
7318dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 dict_T *dict;
7320 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321{
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 if (HASHITEM_EMPTY(hi))
7326 EMSG2(_(e_intern2), "dictitem_remove()");
7327 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 dictitem_free(item);
7330}
7331
7332/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333 * Free a dict item. Also clears the value.
7334 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007335 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007340 if (item->di_flags & DI_FLAGS_ALLOC)
7341 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342}
7343
7344/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7346 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007347 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348 * Returns NULL when out of memory.
7349 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007351dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007354 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355{
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 dict_T *copy;
7357 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360
7361 if (orig == NULL)
7362 return NULL;
7363
7364 copy = dict_alloc();
7365 if (copy != NULL)
7366 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007367 if (copyID != 0)
7368 {
7369 orig->dv_copyID = copyID;
7370 orig->dv_copydict = copy;
7371 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007372 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007373 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007375 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377 --todo;
7378
7379 di = dictitem_alloc(hi->hi_key);
7380 if (di == NULL)
7381 break;
7382 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007383 {
7384 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7385 copyID) == FAIL)
7386 {
7387 vim_free(di);
7388 break;
7389 }
7390 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 else
7392 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7393 if (dict_add(copy, di) == FAIL)
7394 {
7395 dictitem_free(di);
7396 break;
7397 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007402 if (todo > 0)
7403 {
7404 dict_unref(copy);
7405 copy = NULL;
7406 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007407 }
7408
7409 return copy;
7410}
7411
7412/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007414 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007416 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007418 dict_T *d;
7419 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420{
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422}
7423
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007425 * Add a number or string entry to dictionary "d".
7426 * When "str" is NULL use number "nr", otherwise use "str".
7427 * Returns FAIL when out of memory and when key already exists.
7428 */
7429 int
7430dict_add_nr_str(d, key, nr, str)
7431 dict_T *d;
7432 char *key;
7433 long nr;
7434 char_u *str;
7435{
7436 dictitem_T *item;
7437
7438 item = dictitem_alloc((char_u *)key);
7439 if (item == NULL)
7440 return FAIL;
7441 item->di_tv.v_lock = 0;
7442 if (str == NULL)
7443 {
7444 item->di_tv.v_type = VAR_NUMBER;
7445 item->di_tv.vval.v_number = nr;
7446 }
7447 else
7448 {
7449 item->di_tv.v_type = VAR_STRING;
7450 item->di_tv.vval.v_string = vim_strsave(str);
7451 }
7452 if (dict_add(d, item) == FAIL)
7453 {
7454 dictitem_free(item);
7455 return FAIL;
7456 }
7457 return OK;
7458}
7459
7460/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007461 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007462 * Returns FAIL when out of memory and when key already exists.
7463 */
7464 int
7465dict_add_list(d, key, list)
7466 dict_T *d;
7467 char *key;
7468 list_T *list;
7469{
7470 dictitem_T *item;
7471
7472 item = dictitem_alloc((char_u *)key);
7473 if (item == NULL)
7474 return FAIL;
7475 item->di_tv.v_lock = 0;
7476 item->di_tv.v_type = VAR_LIST;
7477 item->di_tv.vval.v_list = list;
7478 if (dict_add(d, item) == FAIL)
7479 {
7480 dictitem_free(item);
7481 return FAIL;
7482 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007483 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007484 return OK;
7485}
7486
7487/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007488 * Get the number of items in a Dictionary.
7489 */
7490 static long
7491dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007494 if (d == NULL)
7495 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007496 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497}
7498
7499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 * Find item "key[len]" in Dictionary "d".
7501 * If "len" is negative use strlen(key).
7502 * Returns NULL when not found.
7503 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007504 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007506 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 char_u *key;
7508 int len;
7509{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510#define AKEYLEN 200
7511 char_u buf[AKEYLEN];
7512 char_u *akey;
7513 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 if (len < 0)
7517 akey = key;
7518 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007519 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520 tofree = akey = vim_strnsave(key, len);
7521 if (akey == NULL)
7522 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007523 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 else
7525 {
7526 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007527 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007528 akey = buf;
7529 }
7530
Bram Moolenaar33570922005-01-25 22:26:29 +00007531 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 vim_free(tofree);
7533 if (HASHITEM_EMPTY(hi))
7534 return NULL;
7535 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536}
7537
7538/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007539 * Get a string item from a dictionary.
7540 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541 * Returns NULL if the entry doesn't exist or out of memory.
7542 */
7543 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007544get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007545 dict_T *d;
7546 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007547 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007548{
7549 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007550 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007551
7552 di = dict_find(d, key, -1);
7553 if (di == NULL)
7554 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007555 s = get_tv_string(&di->di_tv);
7556 if (save && s != NULL)
7557 s = vim_strsave(s);
7558 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007559}
7560
7561/*
7562 * Get a number item from a dictionary.
7563 * Returns 0 if the entry doesn't exist or out of memory.
7564 */
7565 long
7566get_dict_number(d, key)
7567 dict_T *d;
7568 char_u *key;
7569{
7570 dictitem_T *di;
7571
7572 di = dict_find(d, key, -1);
7573 if (di == NULL)
7574 return 0;
7575 return get_tv_number(&di->di_tv);
7576}
7577
7578/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 * Return an allocated string with the string representation of a Dictionary.
7580 * May return NULL.
7581 */
7582 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007583dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007585 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586{
7587 garray_T ga;
7588 int first = TRUE;
7589 char_u *tofree;
7590 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007596 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 return NULL;
7598 ga_init2(&ga, (int)sizeof(char), 80);
7599 ga_append(&ga, '{');
7600
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007601 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007602 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007604 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007606 --todo;
7607
7608 if (first)
7609 first = FALSE;
7610 else
7611 ga_concat(&ga, (char_u *)", ");
7612
7613 tofree = string_quote(hi->hi_key, FALSE);
7614 if (tofree != NULL)
7615 {
7616 ga_concat(&ga, tofree);
7617 vim_free(tofree);
7618 }
7619 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007621 if (s != NULL)
7622 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007624 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007625 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007626 line_breakcheck();
7627
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007630 if (todo > 0)
7631 {
7632 vim_free(ga.ga_data);
7633 return NULL;
7634 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635
7636 ga_append(&ga, '}');
7637 ga_append(&ga, NUL);
7638 return (char_u *)ga.ga_data;
7639}
7640
7641/*
7642 * Allocate a variable for a Dictionary and fill it from "*arg".
7643 * Return OK or FAIL. Returns NOTDONE for {expr}.
7644 */
7645 static int
7646get_dict_tv(arg, rettv, evaluate)
7647 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007648 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649 int evaluate;
7650{
Bram Moolenaar33570922005-01-25 22:26:29 +00007651 dict_T *d = NULL;
7652 typval_T tvkey;
7653 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007654 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007655 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658
7659 /*
7660 * First check if it's not a curly-braces thing: {expr}.
7661 * Must do this without evaluating, otherwise a function may be called
7662 * twice. Unfortunately this means we need to call eval1() twice for the
7663 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007664 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 if (*start != '}')
7667 {
7668 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7669 return FAIL;
7670 if (*start == '}')
7671 return NOTDONE;
7672 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673
7674 if (evaluate)
7675 {
7676 d = dict_alloc();
7677 if (d == NULL)
7678 return FAIL;
7679 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007680 tvkey.v_type = VAR_UNKNOWN;
7681 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682
7683 *arg = skipwhite(*arg + 1);
7684 while (**arg != '}' && **arg != NUL)
7685 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007686 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 goto failret;
7688 if (**arg != ':')
7689 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007690 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 goto failret;
7693 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007694 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007696 key = get_tv_string_buf_chk(&tvkey, buf);
7697 if (key == NULL || *key == NUL)
7698 {
7699 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7700 if (key != NULL)
7701 EMSG(_(e_emptykey));
7702 clear_tv(&tvkey);
7703 goto failret;
7704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007706
7707 *arg = skipwhite(*arg + 1);
7708 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7709 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007710 if (evaluate)
7711 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 goto failret;
7713 }
7714 if (evaluate)
7715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007716 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 if (item != NULL)
7718 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007719 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007720 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 clear_tv(&tv);
7722 goto failret;
7723 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007724 item = dictitem_alloc(key);
7725 clear_tv(&tvkey);
7726 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007729 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007730 if (dict_add(d, item) == FAIL)
7731 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 }
7733 }
7734
7735 if (**arg == '}')
7736 break;
7737 if (**arg != ',')
7738 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007739 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 goto failret;
7741 }
7742 *arg = skipwhite(*arg + 1);
7743 }
7744
7745 if (**arg != '}')
7746 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007747 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007748failret:
7749 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007750 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 return FAIL;
7752 }
7753
7754 *arg = skipwhite(*arg + 1);
7755 if (evaluate)
7756 {
7757 rettv->v_type = VAR_DICT;
7758 rettv->vval.v_dict = d;
7759 ++d->dv_refcount;
7760 }
7761
7762 return OK;
7763}
7764
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007766 * Return a string with the string representation of a variable.
7767 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007768 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007769 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007770 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007771 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007772 */
7773 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007774echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007776 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007777 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007778 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007780 static int recurse = 0;
7781 char_u *r = NULL;
7782
Bram Moolenaar33570922005-01-25 22:26:29 +00007783 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007784 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007785 if (!did_echo_string_emsg)
7786 {
7787 /* Only give this message once for a recursive call to avoid
7788 * flooding the user with errors. And stop iterating over lists
7789 * and dicts. */
7790 did_echo_string_emsg = TRUE;
7791 EMSG(_("E724: variable nested too deep for displaying"));
7792 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007793 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007794 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007795 }
7796 ++recurse;
7797
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007798 switch (tv->v_type)
7799 {
7800 case VAR_FUNC:
7801 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 r = tv->vval.v_string;
7803 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007804
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007805 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007806 if (tv->vval.v_list == NULL)
7807 {
7808 *tofree = NULL;
7809 r = NULL;
7810 }
7811 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7812 {
7813 *tofree = NULL;
7814 r = (char_u *)"[...]";
7815 }
7816 else
7817 {
7818 tv->vval.v_list->lv_copyID = copyID;
7819 *tofree = list2string(tv, copyID);
7820 r = *tofree;
7821 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007823
Bram Moolenaar8c711452005-01-14 21:53:12 +00007824 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007825 if (tv->vval.v_dict == NULL)
7826 {
7827 *tofree = NULL;
7828 r = NULL;
7829 }
7830 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7831 {
7832 *tofree = NULL;
7833 r = (char_u *)"{...}";
7834 }
7835 else
7836 {
7837 tv->vval.v_dict->dv_copyID = copyID;
7838 *tofree = dict2string(tv, copyID);
7839 r = *tofree;
7840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007843 case VAR_STRING:
7844 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 *tofree = NULL;
7846 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007847 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007848
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849#ifdef FEAT_FLOAT
7850 case VAR_FLOAT:
7851 *tofree = NULL;
7852 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7853 r = numbuf;
7854 break;
7855#endif
7856
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007858 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007859 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007860 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007861
Bram Moolenaar8502c702014-06-17 12:51:16 +02007862 if (--recurse == 0)
7863 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007865}
7866
7867/*
7868 * Return a string with the string representation of a variable.
7869 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7870 * "numbuf" is used for a number.
7871 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007872 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007873 */
7874 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007875tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007876 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007877 char_u **tofree;
7878 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007879 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880{
7881 switch (tv->v_type)
7882 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883 case VAR_FUNC:
7884 *tofree = string_quote(tv->vval.v_string, TRUE);
7885 return *tofree;
7886 case VAR_STRING:
7887 *tofree = string_quote(tv->vval.v_string, FALSE);
7888 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007889#ifdef FEAT_FLOAT
7890 case VAR_FLOAT:
7891 *tofree = NULL;
7892 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7893 return numbuf;
7894#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007897 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007900 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007901 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007902 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007903}
7904
7905/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 * Return string "str" in ' quotes, doubling ' characters.
7907 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007908 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909 */
7910 static char_u *
7911string_quote(str, function)
7912 char_u *str;
7913 int function;
7914{
Bram Moolenaar33570922005-01-25 22:26:29 +00007915 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007916 char_u *p, *r, *s;
7917
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 len = (function ? 13 : 3);
7919 if (str != NULL)
7920 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007921 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 for (p = str; *p != NUL; mb_ptr_adv(p))
7923 if (*p == '\'')
7924 ++len;
7925 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926 s = r = alloc(len);
7927 if (r != NULL)
7928 {
7929 if (function)
7930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007932 r += 10;
7933 }
7934 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 if (str != NULL)
7937 for (p = str; *p != NUL; )
7938 {
7939 if (*p == '\'')
7940 *r++ = '\'';
7941 MB_COPY_CHAR(p, r);
7942 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944 if (function)
7945 *r++ = ')';
7946 *r++ = NUL;
7947 }
7948 return s;
7949}
7950
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007951#ifdef FEAT_FLOAT
7952/*
7953 * Convert the string "text" to a floating point number.
7954 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7955 * this always uses a decimal point.
7956 * Returns the length of the text that was consumed.
7957 */
7958 static int
7959string2float(text, value)
7960 char_u *text;
7961 float_T *value; /* result stored here */
7962{
7963 char *s = (char *)text;
7964 float_T f;
7965
7966 f = strtod(s, &s);
7967 *value = f;
7968 return (int)((char_u *)s - text);
7969}
7970#endif
7971
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 * Get the value of an environment variable.
7974 * "arg" is pointing to the '$'. It is advanced to after the name.
7975 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007976 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 */
7978 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007979get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 int evaluate;
7983{
7984 char_u *string = NULL;
7985 int len;
7986 int cc;
7987 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007988 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 ++*arg;
7991 name = *arg;
7992 len = get_env_len(arg);
7993 if (evaluate)
7994 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007995 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007996 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007997
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007998 cc = name[len];
7999 name[len] = NUL;
8000 /* first try vim_getenv(), fast for normal environment vars */
8001 string = vim_getenv(name, &mustfree);
8002 if (string != NULL && *string != NUL)
8003 {
8004 if (!mustfree)
8005 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008007 else
8008 {
8009 if (mustfree)
8010 vim_free(string);
8011
8012 /* next try expanding things like $VIM and ${HOME} */
8013 string = expand_env_save(name - 1);
8014 if (string != NULL && *string == '$')
8015 {
8016 vim_free(string);
8017 string = NULL;
8018 }
8019 }
8020 name[len] = cc;
8021
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022 rettv->v_type = VAR_STRING;
8023 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025
8026 return OK;
8027}
8028
8029/*
8030 * Array with names and number of arguments of all internal functions
8031 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8032 */
8033static struct fst
8034{
8035 char *f_name; /* function name */
8036 char f_min_argc; /* minimal number of arguments */
8037 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008039 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040} functions[] =
8041{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008042#ifdef FEAT_FLOAT
8043 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008044 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008045#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008046 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008047 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {"append", 2, 2, f_append},
8049 {"argc", 0, 0, f_argc},
8050 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008051 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008052 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008053#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008054 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008056 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008059 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"bufexists", 1, 1, f_bufexists},
8061 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8062 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8063 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8064 {"buflisted", 1, 1, f_buflisted},
8065 {"bufloaded", 1, 1, f_bufloaded},
8066 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008067 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"bufwinnr", 1, 1, f_bufwinnr},
8069 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008070 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008071 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008072 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073#ifdef FEAT_FLOAT
8074 {"ceil", 1, 1, f_ceil},
8075#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008076 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008077 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008079 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008081#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008082 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008083 {"complete_add", 1, 1, f_complete_add},
8084 {"complete_check", 0, 0, f_complete_check},
8085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008087 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088#ifdef FEAT_FLOAT
8089 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008090 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008091#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008092 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008094 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008095 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"delete", 1, 1, f_delete},
8097 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008098 {"diff_filler", 1, 1, f_diff_filler},
8099 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008100 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008102 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"eventhandler", 0, 0, f_eventhandler},
8104 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008105 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008107#ifdef FEAT_FLOAT
8108 {"exp", 1, 1, f_exp},
8109#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008110 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008111 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008112 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8114 {"filereadable", 1, 1, f_filereadable},
8115 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008116 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008117 {"finddir", 1, 3, f_finddir},
8118 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008119#ifdef FEAT_FLOAT
8120 {"float2nr", 1, 1, f_float2nr},
8121 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008122 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008123#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008124 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"fnamemodify", 2, 2, f_fnamemodify},
8126 {"foldclosed", 1, 1, f_foldclosed},
8127 {"foldclosedend", 1, 1, f_foldclosedend},
8128 {"foldlevel", 1, 1, f_foldlevel},
8129 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008130 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008132 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008133 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008134 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008135 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008136 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"getchar", 0, 1, f_getchar},
8138 {"getcharmod", 0, 0, f_getcharmod},
8139 {"getcmdline", 0, 0, f_getcmdline},
8140 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008141 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008142 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008143 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008145 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008146 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"getfsize", 1, 1, f_getfsize},
8148 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008149 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008150 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008151 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008152 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008153 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008154 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008155 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008156 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008158 {"gettabvar", 2, 3, f_gettabvar},
8159 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"getwinposx", 0, 0, f_getwinposx},
8161 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008162 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008163 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008164 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008165 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008167 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008168 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008169 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8171 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8172 {"histadd", 2, 2, f_histadd},
8173 {"histdel", 1, 2, f_histdel},
8174 {"histget", 1, 2, f_histget},
8175 {"histnr", 1, 1, f_histnr},
8176 {"hlID", 1, 1, f_hlID},
8177 {"hlexists", 1, 1, f_hlexists},
8178 {"hostname", 0, 0, f_hostname},
8179 {"iconv", 3, 3, f_iconv},
8180 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008181 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008182 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008184 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"inputrestore", 0, 0, f_inputrestore},
8186 {"inputsave", 0, 0, f_inputsave},
8187 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008188 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008189 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008191 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008192 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008193 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008194 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008196 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {"libcall", 3, 3, f_libcall},
8198 {"libcallnr", 3, 3, f_libcallnr},
8199 {"line", 1, 1, f_line},
8200 {"line2byte", 1, 1, f_line2byte},
8201 {"lispindent", 1, 1, f_lispindent},
8202 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008203#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008204 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008205 {"log10", 1, 1, f_log10},
8206#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008207#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008208 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008209#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008210 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008211 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008212 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008213 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008214 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008215 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008216 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008217 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008218 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008219 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008220 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008221 {"max", 1, 1, f_max},
8222 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008223#ifdef vim_mkdir
8224 {"mkdir", 1, 3, f_mkdir},
8225#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008226 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008227#ifdef FEAT_MZSCHEME
8228 {"mzeval", 1, 1, f_mzeval},
8229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008231 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008232 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008233 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008234#ifdef FEAT_FLOAT
8235 {"pow", 2, 2, f_pow},
8236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008238 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008239 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008240#ifdef FEAT_PYTHON3
8241 {"py3eval", 1, 1, f_py3eval},
8242#endif
8243#ifdef FEAT_PYTHON
8244 {"pyeval", 1, 1, f_pyeval},
8245#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008246 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008247 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008248 {"reltime", 0, 2, f_reltime},
8249 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"remote_expr", 2, 3, f_remote_expr},
8251 {"remote_foreground", 1, 1, f_remote_foreground},
8252 {"remote_peek", 1, 2, f_remote_peek},
8253 {"remote_read", 1, 1, f_remote_read},
8254 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008255 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008257 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008259 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008260#ifdef FEAT_FLOAT
8261 {"round", 1, 1, f_round},
8262#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008263 {"screenattr", 2, 2, f_screenattr},
8264 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008265 {"screencol", 0, 0, f_screencol},
8266 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008267 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008268 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008269 {"searchpair", 3, 7, f_searchpair},
8270 {"searchpairpos", 3, 7, f_searchpairpos},
8271 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"server2client", 2, 2, f_server2client},
8273 {"serverlist", 0, 0, f_serverlist},
8274 {"setbufvar", 3, 3, f_setbufvar},
8275 {"setcmdpos", 1, 1, f_setcmdpos},
8276 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008277 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008278 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008279 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008280 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008282 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008283 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008285#ifdef FEAT_CRYPT
8286 {"sha256", 1, 1, f_sha256},
8287#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008288 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008289 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008291#ifdef FEAT_FLOAT
8292 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008293 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008294#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008295 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008296 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008297 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008298 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008299 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008300#ifdef FEAT_FLOAT
8301 {"sqrt", 1, 1, f_sqrt},
8302 {"str2float", 1, 1, f_str2float},
8303#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008304 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008305 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008306 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307#ifdef HAVE_STRFTIME
8308 {"strftime", 1, 2, f_strftime},
8309#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008310 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"strlen", 1, 1, f_strlen},
8313 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008314 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008316 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008317 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {"substitute", 4, 4, f_substitute},
8319 {"synID", 3, 3, f_synID},
8320 {"synIDattr", 2, 3, f_synIDattr},
8321 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008322 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008323 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008324 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008325 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008326 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008327 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008328 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008329 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008330 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008331#ifdef FEAT_FLOAT
8332 {"tan", 1, 1, f_tan},
8333 {"tanh", 1, 1, f_tanh},
8334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008336 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {"tolower", 1, 1, f_tolower},
8338 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008339 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008340#ifdef FEAT_FLOAT
8341 {"trunc", 1, 1, f_trunc},
8342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008344 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008345 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008346 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008347 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"virtcol", 1, 1, f_virtcol},
8349 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008350 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {"winbufnr", 1, 1, f_winbufnr},
8352 {"wincol", 0, 0, f_wincol},
8353 {"winheight", 1, 1, f_winheight},
8354 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008355 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008357 {"winrestview", 1, 1, f_winrestview},
8358 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008360 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008361 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362};
8363
8364#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8365
8366/*
8367 * Function given to ExpandGeneric() to obtain the list of internal
8368 * or user defined function names.
8369 */
8370 char_u *
8371get_function_name(xp, idx)
8372 expand_T *xp;
8373 int idx;
8374{
8375 static int intidx = -1;
8376 char_u *name;
8377
8378 if (idx == 0)
8379 intidx = -1;
8380 if (intidx < 0)
8381 {
8382 name = get_user_func_name(xp, idx);
8383 if (name != NULL)
8384 return name;
8385 }
8386 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8387 {
8388 STRCPY(IObuff, functions[intidx].f_name);
8389 STRCAT(IObuff, "(");
8390 if (functions[intidx].f_max_argc == 0)
8391 STRCAT(IObuff, ")");
8392 return IObuff;
8393 }
8394
8395 return NULL;
8396}
8397
8398/*
8399 * Function given to ExpandGeneric() to obtain the list of internal or
8400 * user defined variable or function names.
8401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 char_u *
8403get_expr_name(xp, idx)
8404 expand_T *xp;
8405 int idx;
8406{
8407 static int intidx = -1;
8408 char_u *name;
8409
8410 if (idx == 0)
8411 intidx = -1;
8412 if (intidx < 0)
8413 {
8414 name = get_function_name(xp, idx);
8415 if (name != NULL)
8416 return name;
8417 }
8418 return get_user_var_name(xp, ++intidx);
8419}
8420
8421#endif /* FEAT_CMDL_COMPL */
8422
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008423#if defined(EBCDIC) || defined(PROTO)
8424/*
8425 * Compare struct fst by function name.
8426 */
8427 static int
8428compare_func_name(s1, s2)
8429 const void *s1;
8430 const void *s2;
8431{
8432 struct fst *p1 = (struct fst *)s1;
8433 struct fst *p2 = (struct fst *)s2;
8434
8435 return STRCMP(p1->f_name, p2->f_name);
8436}
8437
8438/*
8439 * Sort the function table by function name.
8440 * The sorting of the table above is ASCII dependant.
8441 * On machines using EBCDIC we have to sort it.
8442 */
8443 static void
8444sortFunctions()
8445{
8446 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8447
8448 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8449}
8450#endif
8451
8452
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453/*
8454 * Find internal function in table above.
8455 * Return index, or -1 if not found
8456 */
8457 static int
8458find_internal_func(name)
8459 char_u *name; /* name of the function */
8460{
8461 int first = 0;
8462 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8463 int cmp;
8464 int x;
8465
8466 /*
8467 * Find the function name in the table. Binary search.
8468 */
8469 while (first <= last)
8470 {
8471 x = first + ((unsigned)(last - first) >> 1);
8472 cmp = STRCMP(name, functions[x].f_name);
8473 if (cmp < 0)
8474 last = x - 1;
8475 else if (cmp > 0)
8476 first = x + 1;
8477 else
8478 return x;
8479 }
8480 return -1;
8481}
8482
8483/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8485 * name it contains, otherwise return "name".
8486 */
8487 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008488deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008489 char_u *name;
8490 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008491 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008492{
Bram Moolenaar33570922005-01-25 22:26:29 +00008493 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008494 int cc;
8495
8496 cc = name[*lenp];
8497 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008498 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008502 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008503 {
8504 *lenp = 0;
8505 return (char_u *)""; /* just in case */
8506 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008507 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008508 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509 }
8510
8511 return name;
8512}
8513
8514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 * Allocate a variable for the result of a function.
8516 * Return OK or FAIL.
8517 */
8518 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8520 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 char_u *name; /* name of the function */
8522 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 char_u **arg; /* argument, pointing to the '(' */
8525 linenr_T firstline; /* first line of range */
8526 linenr_T lastline; /* last line of range */
8527 int *doesrange; /* return: function handled range */
8528 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 char_u *argp;
8532 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008533 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 int argcount = 0; /* number of arguments found */
8535
8536 /*
8537 * Get the arguments.
8538 */
8539 argp = *arg;
8540 while (argcount < MAX_FUNC_ARGS)
8541 {
8542 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8543 if (*argp == ')' || *argp == ',' || *argp == NUL)
8544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8546 {
8547 ret = FAIL;
8548 break;
8549 }
8550 ++argcount;
8551 if (*argp != ',')
8552 break;
8553 }
8554 if (*argp == ')')
8555 ++argp;
8556 else
8557 ret = FAIL;
8558
8559 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008560 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008561 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008563 {
8564 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008565 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008567 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
8570 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572
8573 *arg = skipwhite(argp);
8574 return ret;
8575}
8576
8577
8578/*
8579 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008580 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008581 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 */
8583 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008584call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008585 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008586 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008590 typval_T *argvars; /* vars for arguments, must have "argcount"
8591 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 linenr_T firstline; /* first line of range */
8593 linenr_T lastline; /* last line of range */
8594 int *doesrange; /* return: function handled range */
8595 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008596 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599#define ERROR_UNKNOWN 0
8600#define ERROR_TOOMANY 1
8601#define ERROR_TOOFEW 2
8602#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008603#define ERROR_DICT 4
8604#define ERROR_NONE 5
8605#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 int error = ERROR_NONE;
8607 int i;
8608 int llen;
8609 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610#define FLEN_FIXED 40
8611 char_u fname_buf[FLEN_FIXED + 1];
8612 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008613 char_u *name;
8614
8615 /* Make a copy of the name, if it comes from a funcref variable it could
8616 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008617 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008618 if (name == NULL)
8619 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
8621 /*
8622 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8623 * Change <SNR>123_name() to K_SNR 123_name().
8624 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 llen = eval_fname_script(name);
8627 if (llen > 0)
8628 {
8629 fname_buf[0] = K_SPECIAL;
8630 fname_buf[1] = KS_EXTRA;
8631 fname_buf[2] = (int)KE_SNR;
8632 i = 3;
8633 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8634 {
8635 if (current_SID <= 0)
8636 error = ERROR_SCRIPT;
8637 else
8638 {
8639 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8640 i = (int)STRLEN(fname_buf);
8641 }
8642 }
8643 if (i + STRLEN(name + llen) < FLEN_FIXED)
8644 {
8645 STRCPY(fname_buf + i, name + llen);
8646 fname = fname_buf;
8647 }
8648 else
8649 {
8650 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8651 if (fname == NULL)
8652 error = ERROR_OTHER;
8653 else
8654 {
8655 mch_memmove(fname, fname_buf, (size_t)i);
8656 STRCPY(fname + i, name + llen);
8657 }
8658 }
8659 }
8660 else
8661 fname = name;
8662
8663 *doesrange = FALSE;
8664
8665
8666 /* execute the function if no errors detected and executing */
8667 if (evaluate && error == ERROR_NONE)
8668 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008669 char_u *rfname = fname;
8670
8671 /* Ignore "g:" before a function name. */
8672 if (fname[0] == 'g' && fname[1] == ':')
8673 rfname = fname + 2;
8674
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008675 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8676 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 error = ERROR_UNKNOWN;
8678
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008679 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 {
8681 /*
8682 * User defined function.
8683 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008684 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008685
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008687 /* Trigger FuncUndefined event, may load the function. */
8688 if (fp == NULL
8689 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008690 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008691 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008693 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008694 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 }
8696#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008697 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008698 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008699 {
8700 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008701 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008702 }
8703
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 if (fp != NULL)
8705 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008706 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008708 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008710 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008712 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008713 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 else
8715 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008716 int did_save_redo = FALSE;
8717
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 /*
8719 * Call the user function.
8720 * Save and restore search patterns, script variables and
8721 * redo buffer.
8722 */
8723 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008724 if (!ins_compl_active())
8725 {
8726 saveRedobuff();
8727 did_save_redo = TRUE;
8728 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008729 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008731 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008732 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8733 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8734 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008735 /* Function was unreferenced while being used, free it
8736 * now. */
8737 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008738 if (did_save_redo)
8739 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 restore_search_patterns();
8741 error = ERROR_NONE;
8742 }
8743 }
8744 }
8745 else
8746 {
8747 /*
8748 * Find the function name in the table, call its implementation.
8749 */
8750 i = find_internal_func(fname);
8751 if (i >= 0)
8752 {
8753 if (argcount < functions[i].f_min_argc)
8754 error = ERROR_TOOFEW;
8755 else if (argcount > functions[i].f_max_argc)
8756 error = ERROR_TOOMANY;
8757 else
8758 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008759 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 error = ERROR_NONE;
8762 }
8763 }
8764 }
8765 /*
8766 * The function call (or "FuncUndefined" autocommand sequence) might
8767 * have been aborted by an error, an interrupt, or an explicitly thrown
8768 * exception that has not been caught so far. This situation can be
8769 * tested for by calling aborting(). For an error in an internal
8770 * function or for the "E132" error in call_user_func(), however, the
8771 * throw point at which the "force_abort" flag (temporarily reset by
8772 * emsg()) is normally updated has not been reached yet. We need to
8773 * update that flag first to make aborting() reliable.
8774 */
8775 update_force_abort();
8776 }
8777 if (error == ERROR_NONE)
8778 ret = OK;
8779
8780 /*
8781 * Report an error unless the argument evaluation or function call has been
8782 * cancelled due to an aborting error, an interrupt, or an exception.
8783 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008784 if (!aborting())
8785 {
8786 switch (error)
8787 {
8788 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008789 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008790 break;
8791 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008792 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008793 break;
8794 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008795 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008796 name);
8797 break;
8798 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008799 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008800 name);
8801 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008802 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008803 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008804 name);
8805 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008806 }
8807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 if (fname != name && fname != fname_buf)
8810 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008811 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812
8813 return ret;
8814}
8815
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008816/*
8817 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008818 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008819 */
8820 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008821emsg_funcname(ermsg, name)
8822 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008823 char_u *name;
8824{
8825 char_u *p;
8826
8827 if (*name == K_SPECIAL)
8828 p = concat_str((char_u *)"<SNR>", name + 3);
8829 else
8830 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008831 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008832 if (p != name)
8833 vim_free(p);
8834}
8835
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008836/*
8837 * Return TRUE for a non-zero Number and a non-empty String.
8838 */
8839 static int
8840non_zero_arg(argvars)
8841 typval_T *argvars;
8842{
8843 return ((argvars[0].v_type == VAR_NUMBER
8844 && argvars[0].vval.v_number != 0)
8845 || (argvars[0].v_type == VAR_STRING
8846 && argvars[0].vval.v_string != NULL
8847 && *argvars[0].vval.v_string != NUL));
8848}
8849
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850/*********************************************
8851 * Implementation of the built-in functions
8852 */
8853
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008854#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008855static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8856
8857/*
8858 * Get the float value of "argvars[0]" into "f".
8859 * Returns FAIL when the argument is not a Number or Float.
8860 */
8861 static int
8862get_float_arg(argvars, f)
8863 typval_T *argvars;
8864 float_T *f;
8865{
8866 if (argvars[0].v_type == VAR_FLOAT)
8867 {
8868 *f = argvars[0].vval.v_float;
8869 return OK;
8870 }
8871 if (argvars[0].v_type == VAR_NUMBER)
8872 {
8873 *f = (float_T)argvars[0].vval.v_number;
8874 return OK;
8875 }
8876 EMSG(_("E808: Number or Float required"));
8877 return FAIL;
8878}
8879
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880/*
8881 * "abs(expr)" function
8882 */
8883 static void
8884f_abs(argvars, rettv)
8885 typval_T *argvars;
8886 typval_T *rettv;
8887{
8888 if (argvars[0].v_type == VAR_FLOAT)
8889 {
8890 rettv->v_type = VAR_FLOAT;
8891 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8892 }
8893 else
8894 {
8895 varnumber_T n;
8896 int error = FALSE;
8897
8898 n = get_tv_number_chk(&argvars[0], &error);
8899 if (error)
8900 rettv->vval.v_number = -1;
8901 else if (n > 0)
8902 rettv->vval.v_number = n;
8903 else
8904 rettv->vval.v_number = -n;
8905 }
8906}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008907
8908/*
8909 * "acos()" function
8910 */
8911 static void
8912f_acos(argvars, rettv)
8913 typval_T *argvars;
8914 typval_T *rettv;
8915{
8916 float_T f;
8917
8918 rettv->v_type = VAR_FLOAT;
8919 if (get_float_arg(argvars, &f) == OK)
8920 rettv->vval.v_float = acos(f);
8921 else
8922 rettv->vval.v_float = 0.0;
8923}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008924#endif
8925
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008927 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 */
8929 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008930f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008931 typval_T *argvars;
8932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933{
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008937 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008939 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008940 && !tv_check_lock(l->lv_lock,
8941 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008942 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008944 }
8945 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008946 EMSG(_(e_listreq));
8947}
8948
8949/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008950 * "and(expr, expr)" function
8951 */
8952 static void
8953f_and(argvars, rettv)
8954 typval_T *argvars;
8955 typval_T *rettv;
8956{
8957 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8958 & get_tv_number_chk(&argvars[1], NULL);
8959}
8960
8961/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008962 * "append(lnum, string/list)" function
8963 */
8964 static void
8965f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 typval_T *argvars;
8967 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008968{
8969 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008970 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 list_T *l = NULL;
8972 listitem_T *li = NULL;
8973 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008974 long added = 0;
8975
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008976 /* When coming here from Insert mode, sync undo, so that this can be
8977 * undone separately from what was previously inserted. */
8978 if (u_sync_once == 2)
8979 {
8980 u_sync_once = 1; /* notify that u_sync() was called */
8981 u_sync(TRUE);
8982 }
8983
Bram Moolenaar0d660222005-01-07 21:51:51 +00008984 lnum = get_tv_lnum(argvars);
8985 if (lnum >= 0
8986 && lnum <= curbuf->b_ml.ml_line_count
8987 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008988 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008990 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008991 l = argvars[1].vval.v_list;
8992 if (l == NULL)
8993 return;
8994 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008995 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008996 for (;;)
8997 {
8998 if (l == NULL)
8999 tv = &argvars[1]; /* append a string */
9000 else if (li == NULL)
9001 break; /* end of list */
9002 else
9003 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009004 line = get_tv_string_chk(tv);
9005 if (line == NULL) /* type error */
9006 {
9007 rettv->vval.v_number = 1; /* Failed */
9008 break;
9009 }
9010 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009011 ++added;
9012 if (l == NULL)
9013 break;
9014 li = li->li_next;
9015 }
9016
9017 appended_lines_mark(lnum, added);
9018 if (curwin->w_cursor.lnum > lnum)
9019 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021 else
9022 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023}
9024
9025/*
9026 * "argc()" function
9027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009030 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009033 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034}
9035
9036/*
9037 * "argidx()" function
9038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009040f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009041 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009044 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045}
9046
9047/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009048 * "arglistid()" function
9049 */
9050 static void
9051f_arglistid(argvars, rettv)
9052 typval_T *argvars UNUSED;
9053 typval_T *rettv;
9054{
9055 win_T *wp;
9056 tabpage_T *tp = NULL;
9057 long n;
9058
9059 rettv->vval.v_number = -1;
9060 if (argvars[0].v_type != VAR_UNKNOWN)
9061 {
9062 if (argvars[1].v_type != VAR_UNKNOWN)
9063 {
9064 n = get_tv_number(&argvars[1]);
9065 if (n >= 0)
9066 tp = find_tabpage(n);
9067 }
9068 else
9069 tp = curtab;
9070
9071 if (tp != NULL)
9072 {
9073 wp = find_win_by_nr(&argvars[0], tp);
9074 if (wp != NULL)
9075 rettv->vval.v_number = wp->w_alist->id;
9076 }
9077 }
9078 else
9079 rettv->vval.v_number = curwin->w_alist->id;
9080}
9081
9082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 * "argv(nr)" function
9084 */
9085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009087 typval_T *argvars;
9088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089{
9090 int idx;
9091
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009092 if (argvars[0].v_type != VAR_UNKNOWN)
9093 {
9094 idx = get_tv_number_chk(&argvars[0], NULL);
9095 if (idx >= 0 && idx < ARGCOUNT)
9096 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9097 else
9098 rettv->vval.v_string = NULL;
9099 rettv->v_type = VAR_STRING;
9100 }
9101 else if (rettv_list_alloc(rettv) == OK)
9102 for (idx = 0; idx < ARGCOUNT; ++idx)
9103 list_append_string(rettv->vval.v_list,
9104 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105}
9106
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009107#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009108/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009109 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009110 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009111 static void
9112f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009113 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009114 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009115{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009116 float_T f;
9117
9118 rettv->v_type = VAR_FLOAT;
9119 if (get_float_arg(argvars, &f) == OK)
9120 rettv->vval.v_float = asin(f);
9121 else
9122 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009123}
9124
9125/*
9126 * "atan()" function
9127 */
9128 static void
9129f_atan(argvars, rettv)
9130 typval_T *argvars;
9131 typval_T *rettv;
9132{
9133 float_T f;
9134
9135 rettv->v_type = VAR_FLOAT;
9136 if (get_float_arg(argvars, &f) == OK)
9137 rettv->vval.v_float = atan(f);
9138 else
9139 rettv->vval.v_float = 0.0;
9140}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009141
9142/*
9143 * "atan2()" function
9144 */
9145 static void
9146f_atan2(argvars, rettv)
9147 typval_T *argvars;
9148 typval_T *rettv;
9149{
9150 float_T fx, fy;
9151
9152 rettv->v_type = VAR_FLOAT;
9153 if (get_float_arg(argvars, &fx) == OK
9154 && get_float_arg(&argvars[1], &fy) == OK)
9155 rettv->vval.v_float = atan2(fx, fy);
9156 else
9157 rettv->vval.v_float = 0.0;
9158}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009159#endif
9160
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161/*
9162 * "browse(save, title, initdir, default)" function
9163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009166 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
9169#ifdef FEAT_BROWSE
9170 int save;
9171 char_u *title;
9172 char_u *initdir;
9173 char_u *defname;
9174 char_u buf[NUMBUFLEN];
9175 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009178 save = get_tv_number_chk(&argvars[0], &error);
9179 title = get_tv_string_chk(&argvars[1]);
9180 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9181 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 if (error || title == NULL || initdir == NULL || defname == NULL)
9184 rettv->vval.v_string = NULL;
9185 else
9186 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009187 do_browse(save ? BROWSE_SAVE : 0,
9188 title, defname, NULL, initdir, NULL, curbuf);
9189#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009190 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009191#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009193}
9194
9195/*
9196 * "browsedir(title, initdir)" function
9197 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009200 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009201 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009202{
9203#ifdef FEAT_BROWSE
9204 char_u *title;
9205 char_u *initdir;
9206 char_u buf[NUMBUFLEN];
9207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009208 title = get_tv_string_chk(&argvars[0]);
9209 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009211 if (title == NULL || initdir == NULL)
9212 rettv->vval.v_string = NULL;
9213 else
9214 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009215 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220}
9221
Bram Moolenaar33570922005-01-25 22:26:29 +00009222static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009223
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224/*
9225 * Find a buffer by number or exact name.
9226 */
9227 static buf_T *
9228find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009229 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230{
9231 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009233 if (avar->v_type == VAR_NUMBER)
9234 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009235 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009237 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009238 if (buf == NULL)
9239 {
9240 /* No full path name match, try a match with a URL or a "nofile"
9241 * buffer, these don't use the full path. */
9242 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9243 if (buf->b_fname != NULL
9244 && (path_with_url(buf->b_fname)
9245#ifdef FEAT_QUICKFIX
9246 || bt_nofile(buf)
9247#endif
9248 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009249 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009250 break;
9251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 }
9253 return buf;
9254}
9255
9256/*
9257 * "bufexists(expr)" function
9258 */
9259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260f_bufexists(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009264 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265}
9266
9267/*
9268 * "buflisted(expr)" function
9269 */
9270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009271f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009272 typval_T *argvars;
9273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274{
9275 buf_T *buf;
9276
9277 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009278 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279}
9280
9281/*
9282 * "bufloaded(expr)" function
9283 */
9284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009285f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009286 typval_T *argvars;
9287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288{
9289 buf_T *buf;
9290
9291 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293}
9294
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009295static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009296
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297/*
9298 * Get buffer by number or pattern.
9299 */
9300 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009301get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009302 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009303 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 int save_magic;
9307 char_u *save_cpo;
9308 buf_T *buf;
9309
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310 if (tv->v_type == VAR_NUMBER)
9311 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009312 if (tv->v_type != VAR_STRING)
9313 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 if (name == NULL || *name == NUL)
9315 return curbuf;
9316 if (name[0] == '$' && name[1] == NUL)
9317 return lastbuf;
9318
9319 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9320 save_magic = p_magic;
9321 p_magic = TRUE;
9322 save_cpo = p_cpo;
9323 p_cpo = (char_u *)"";
9324
9325 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009326 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327
9328 p_magic = save_magic;
9329 p_cpo = save_cpo;
9330
9331 /* If not found, try expanding the name, like done for bufexists(). */
9332 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009333 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334
9335 return buf;
9336}
9337
9338/*
9339 * "bufname(expr)" function
9340 */
9341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009343 typval_T *argvars;
9344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345{
9346 buf_T *buf;
9347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009348 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009350 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 --emsg_off;
9357}
9358
9359/*
9360 * "bufnr(expr)" function
9361 */
9362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009363f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009364 typval_T *argvars;
9365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366{
9367 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009368 int error = FALSE;
9369 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009371 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009373 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009374 --emsg_off;
9375
9376 /* If the buffer isn't found and the second argument is not zero create a
9377 * new buffer. */
9378 if (buf == NULL
9379 && argvars[1].v_type != VAR_UNKNOWN
9380 && get_tv_number_chk(&argvars[1], &error) != 0
9381 && !error
9382 && (name = get_tv_string_chk(&argvars[0])) != NULL
9383 && !error)
9384 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9385
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009389 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390}
9391
9392/*
9393 * "bufwinnr(nr)" function
9394 */
9395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009397 typval_T *argvars;
9398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399{
9400#ifdef FEAT_WINDOWS
9401 win_T *wp;
9402 int winnr = 0;
9403#endif
9404 buf_T *buf;
9405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009406 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009408 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409#ifdef FEAT_WINDOWS
9410 for (wp = firstwin; wp; wp = wp->w_next)
9411 {
9412 ++winnr;
9413 if (wp->w_buffer == buf)
9414 break;
9415 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419#endif
9420 --emsg_off;
9421}
9422
9423/*
9424 * "byte2line(byte)" function
9425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009428 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433#else
9434 long boff = 0;
9435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009436 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 (linenr_T)0, &boff);
9442#endif
9443}
9444
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009445 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009446byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009447 typval_T *argvars;
9448 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009449 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009450{
9451#ifdef FEAT_MBYTE
9452 char_u *t;
9453#endif
9454 char_u *str;
9455 long idx;
9456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009457 str = get_tv_string_chk(&argvars[0]);
9458 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009460 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009461 return;
9462
9463#ifdef FEAT_MBYTE
9464 t = str;
9465 for ( ; idx > 0; idx--)
9466 {
9467 if (*t == NUL) /* EOL reached */
9468 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009469 if (enc_utf8 && comp)
9470 t += utf_ptr2len(t);
9471 else
9472 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009473 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009474 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009475#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009476 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009478#endif
9479}
9480
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009481/*
9482 * "byteidx()" function
9483 */
9484 static void
9485f_byteidx(argvars, rettv)
9486 typval_T *argvars;
9487 typval_T *rettv;
9488{
9489 byteidx(argvars, rettv, FALSE);
9490}
9491
9492/*
9493 * "byteidxcomp()" function
9494 */
9495 static void
9496f_byteidxcomp(argvars, rettv)
9497 typval_T *argvars;
9498 typval_T *rettv;
9499{
9500 byteidx(argvars, rettv, TRUE);
9501}
9502
Bram Moolenaardb913952012-06-29 12:54:53 +02009503 int
9504func_call(name, args, selfdict, rettv)
9505 char_u *name;
9506 typval_T *args;
9507 dict_T *selfdict;
9508 typval_T *rettv;
9509{
9510 listitem_T *item;
9511 typval_T argv[MAX_FUNC_ARGS + 1];
9512 int argc = 0;
9513 int dummy;
9514 int r = 0;
9515
9516 for (item = args->vval.v_list->lv_first; item != NULL;
9517 item = item->li_next)
9518 {
9519 if (argc == MAX_FUNC_ARGS)
9520 {
9521 EMSG(_("E699: Too many arguments"));
9522 break;
9523 }
9524 /* Make a copy of each argument. This is needed to be able to set
9525 * v_lock to VAR_FIXED in the copy without changing the original list.
9526 */
9527 copy_tv(&item->li_tv, &argv[argc++]);
9528 }
9529
9530 if (item == NULL)
9531 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9532 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9533 &dummy, TRUE, selfdict);
9534
9535 /* Free the arguments. */
9536 while (argc > 0)
9537 clear_tv(&argv[--argc]);
9538
9539 return r;
9540}
9541
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009542/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009543 * "call(func, arglist)" function
9544 */
9545 static void
9546f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009547 typval_T *argvars;
9548 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009549{
9550 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009551 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009552
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009553 if (argvars[1].v_type != VAR_LIST)
9554 {
9555 EMSG(_(e_listreq));
9556 return;
9557 }
9558 if (argvars[1].vval.v_list == NULL)
9559 return;
9560
9561 if (argvars[0].v_type == VAR_FUNC)
9562 func = argvars[0].vval.v_string;
9563 else
9564 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009565 if (*func == NUL)
9566 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009567
Bram Moolenaare9a41262005-01-15 22:18:47 +00009568 if (argvars[2].v_type != VAR_UNKNOWN)
9569 {
9570 if (argvars[2].v_type != VAR_DICT)
9571 {
9572 EMSG(_(e_dictreq));
9573 return;
9574 }
9575 selfdict = argvars[2].vval.v_dict;
9576 }
9577
Bram Moolenaardb913952012-06-29 12:54:53 +02009578 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009579}
9580
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009581#ifdef FEAT_FLOAT
9582/*
9583 * "ceil({float})" function
9584 */
9585 static void
9586f_ceil(argvars, rettv)
9587 typval_T *argvars;
9588 typval_T *rettv;
9589{
9590 float_T f;
9591
9592 rettv->v_type = VAR_FLOAT;
9593 if (get_float_arg(argvars, &f) == OK)
9594 rettv->vval.v_float = ceil(f);
9595 else
9596 rettv->vval.v_float = 0.0;
9597}
9598#endif
9599
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009600/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009601 * "changenr()" function
9602 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009603 static void
9604f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009605 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009606 typval_T *rettv;
9607{
9608 rettv->vval.v_number = curbuf->b_u_seq_cur;
9609}
9610
9611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 * "char2nr(string)" function
9613 */
9614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009615f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009616 typval_T *argvars;
9617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
9619#ifdef FEAT_MBYTE
9620 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009621 {
9622 int utf8 = 0;
9623
9624 if (argvars[1].v_type != VAR_UNKNOWN)
9625 utf8 = get_tv_number_chk(&argvars[1], NULL);
9626
9627 if (utf8)
9628 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9629 else
9630 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 else
9633#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635}
9636
9637/*
9638 * "cindent(lnum)" function
9639 */
9640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009642 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644{
9645#ifdef FEAT_CINDENT
9646 pos_T pos;
9647 linenr_T lnum;
9648
9649 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9652 {
9653 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009654 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 curwin->w_cursor = pos;
9656 }
9657 else
9658#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660}
9661
9662/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009663 * "clearmatches()" function
9664 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009665 static void
9666f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009667 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009668 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009669{
9670#ifdef FEAT_SEARCH_EXTRA
9671 clear_matches(curwin);
9672#endif
9673}
9674
9675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 * "col(string)" function
9677 */
9678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009680 typval_T *argvars;
9681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
9683 colnr_T col = 0;
9684 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009685 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009687 fp = var2fpos(&argvars[0], FALSE, &fnum);
9688 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 {
9690 if (fp->col == MAXCOL)
9691 {
9692 /* '> can be MAXCOL, get the length of the line then */
9693 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009694 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 else
9696 col = MAXCOL;
9697 }
9698 else
9699 {
9700 col = fp->col + 1;
9701#ifdef FEAT_VIRTUALEDIT
9702 /* col(".") when the cursor is on the NUL at the end of the line
9703 * because of "coladd" can be seen as an extra column. */
9704 if (virtual_active() && fp == &curwin->w_cursor)
9705 {
9706 char_u *p = ml_get_cursor();
9707
9708 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9709 curwin->w_virtcol - curwin->w_cursor.coladd))
9710 {
9711# ifdef FEAT_MBYTE
9712 int l;
9713
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009714 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 col += l;
9716# else
9717 if (*p != NUL && p[1] == NUL)
9718 ++col;
9719# endif
9720 }
9721 }
9722#endif
9723 }
9724 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726}
9727
Bram Moolenaar572cb562005-08-05 21:35:02 +00009728#if defined(FEAT_INS_EXPAND)
9729/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009730 * "complete()" function
9731 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009732 static void
9733f_complete(argvars, rettv)
9734 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009735 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009736{
9737 int startcol;
9738
9739 if ((State & INSERT) == 0)
9740 {
9741 EMSG(_("E785: complete() can only be used in Insert mode"));
9742 return;
9743 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009744
9745 /* Check for undo allowed here, because if something was already inserted
9746 * the line was already saved for undo and this check isn't done. */
9747 if (!undo_allowed())
9748 return;
9749
Bram Moolenaarade00832006-03-10 21:46:58 +00009750 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9751 {
9752 EMSG(_(e_invarg));
9753 return;
9754 }
9755
9756 startcol = get_tv_number_chk(&argvars[0], NULL);
9757 if (startcol <= 0)
9758 return;
9759
9760 set_completion(startcol - 1, argvars[1].vval.v_list);
9761}
9762
9763/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009764 * "complete_add()" function
9765 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009766 static void
9767f_complete_add(argvars, rettv)
9768 typval_T *argvars;
9769 typval_T *rettv;
9770{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009771 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009772}
9773
9774/*
9775 * "complete_check()" function
9776 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009777 static void
9778f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009779 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009780 typval_T *rettv;
9781{
9782 int saved = RedrawingDisabled;
9783
9784 RedrawingDisabled = 0;
9785 ins_compl_check_keys(0);
9786 rettv->vval.v_number = compl_interrupted;
9787 RedrawingDisabled = saved;
9788}
9789#endif
9790
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791/*
9792 * "confirm(message, buttons[, default [, type]])" function
9793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009796 typval_T *argvars UNUSED;
9797 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798{
9799#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9800 char_u *message;
9801 char_u *buttons = NULL;
9802 char_u buf[NUMBUFLEN];
9803 char_u buf2[NUMBUFLEN];
9804 int def = 1;
9805 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009806 char_u *typestr;
9807 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009809 message = get_tv_string_chk(&argvars[0]);
9810 if (message == NULL)
9811 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009812 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009814 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9815 if (buttons == NULL)
9816 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009817 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009819 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009820 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009822 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9823 if (typestr == NULL)
9824 error = TRUE;
9825 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009827 switch (TOUPPER_ASC(*typestr))
9828 {
9829 case 'E': type = VIM_ERROR; break;
9830 case 'Q': type = VIM_QUESTION; break;
9831 case 'I': type = VIM_INFO; break;
9832 case 'W': type = VIM_WARNING; break;
9833 case 'G': type = VIM_GENERIC; break;
9834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 }
9836 }
9837 }
9838 }
9839
9840 if (buttons == NULL || *buttons == NUL)
9841 buttons = (char_u *)_("&Ok");
9842
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009843 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009844 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009845 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846#endif
9847}
9848
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009849/*
9850 * "copy()" function
9851 */
9852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009854 typval_T *argvars;
9855 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009856{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009857 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009858}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009860#ifdef FEAT_FLOAT
9861/*
9862 * "cos()" function
9863 */
9864 static void
9865f_cos(argvars, rettv)
9866 typval_T *argvars;
9867 typval_T *rettv;
9868{
9869 float_T f;
9870
9871 rettv->v_type = VAR_FLOAT;
9872 if (get_float_arg(argvars, &f) == OK)
9873 rettv->vval.v_float = cos(f);
9874 else
9875 rettv->vval.v_float = 0.0;
9876}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009877
9878/*
9879 * "cosh()" function
9880 */
9881 static void
9882f_cosh(argvars, rettv)
9883 typval_T *argvars;
9884 typval_T *rettv;
9885{
9886 float_T f;
9887
9888 rettv->v_type = VAR_FLOAT;
9889 if (get_float_arg(argvars, &f) == OK)
9890 rettv->vval.v_float = cosh(f);
9891 else
9892 rettv->vval.v_float = 0.0;
9893}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009894#endif
9895
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009897 * "count()" function
9898 */
9899 static void
9900f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009901 typval_T *argvars;
9902 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009903{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009904 long n = 0;
9905 int ic = FALSE;
9906
Bram Moolenaare9a41262005-01-15 22:18:47 +00009907 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009908 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 listitem_T *li;
9910 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009911 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009912
Bram Moolenaare9a41262005-01-15 22:18:47 +00009913 if ((l = argvars[0].vval.v_list) != NULL)
9914 {
9915 li = l->lv_first;
9916 if (argvars[2].v_type != VAR_UNKNOWN)
9917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009918 int error = FALSE;
9919
9920 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 if (argvars[3].v_type != VAR_UNKNOWN)
9922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 idx = get_tv_number_chk(&argvars[3], &error);
9924 if (!error)
9925 {
9926 li = list_find(l, idx);
9927 if (li == NULL)
9928 EMSGN(_(e_listidx), idx);
9929 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009930 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009931 if (error)
9932 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009933 }
9934
9935 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009936 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009937 ++n;
9938 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009939 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009940 else if (argvars[0].v_type == VAR_DICT)
9941 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009942 int todo;
9943 dict_T *d;
9944 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009945
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009946 if ((d = argvars[0].vval.v_dict) != NULL)
9947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 int error = FALSE;
9949
Bram Moolenaare9a41262005-01-15 22:18:47 +00009950 if (argvars[2].v_type != VAR_UNKNOWN)
9951 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009952 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009953 if (argvars[3].v_type != VAR_UNKNOWN)
9954 EMSG(_(e_invarg));
9955 }
9956
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009957 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009958 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009959 {
9960 if (!HASHITEM_EMPTY(hi))
9961 {
9962 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009963 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009964 ++n;
9965 }
9966 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009967 }
9968 }
9969 else
9970 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009971 rettv->vval.v_number = n;
9972}
9973
9974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9976 *
9977 * Checks the existence of a cscope connection.
9978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009980f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009981 typval_T *argvars UNUSED;
9982 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983{
9984#ifdef FEAT_CSCOPE
9985 int num = 0;
9986 char_u *dbpath = NULL;
9987 char_u *prepend = NULL;
9988 char_u buf[NUMBUFLEN];
9989
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009990 if (argvars[0].v_type != VAR_UNKNOWN
9991 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009993 num = (int)get_tv_number(&argvars[0]);
9994 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009995 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 }
9998
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009999 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000#endif
10001}
10002
10003/*
10004 * "cursor(lnum, col)" function
10005 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010006 * Moves the cursor to the specified line and column.
10007 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010011 typval_T *argvars;
10012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013{
10014 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010015#ifdef FEAT_VIRTUALEDIT
10016 long coladd = 0;
10017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010019 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010020 if (argvars[1].v_type == VAR_UNKNOWN)
10021 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010022 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010023 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010024
Bram Moolenaar493c1782014-05-28 14:34:46 +020010025 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010026 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010027 line = pos.lnum;
10028 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010029#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010030 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010031#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010032 if (curswant >= 0)
10033 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010034 }
10035 else
10036 {
10037 line = get_tv_lnum(argvars);
10038 col = get_tv_number_chk(&argvars[1], NULL);
10039#ifdef FEAT_VIRTUALEDIT
10040 if (argvars[2].v_type != VAR_UNKNOWN)
10041 coladd = get_tv_number_chk(&argvars[2], NULL);
10042#endif
10043 }
10044 if (line < 0 || col < 0
10045#ifdef FEAT_VIRTUALEDIT
10046 || coladd < 0
10047#endif
10048 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010049 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 if (line > 0)
10051 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 if (col > 0)
10053 curwin->w_cursor.col = col - 1;
10054#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010055 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056#endif
10057
10058 /* Make sure the cursor is in a valid position. */
10059 check_cursor();
10060#ifdef FEAT_MBYTE
10061 /* Correct cursor for multi-byte character. */
10062 if (has_mbyte)
10063 mb_adjust_cursor();
10064#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010065
10066 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010067 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068}
10069
10070/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010071 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 */
10073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010074f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010075 typval_T *argvars;
10076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010078 int noref = 0;
10079
10080 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010081 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010082 if (noref < 0 || noref > 1)
10083 EMSG(_(e_invarg));
10084 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010085 {
10086 current_copyID += COPYID_INC;
10087 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089}
10090
10091/*
10092 * "delete()" function
10093 */
10094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010096 typval_T *argvars;
10097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098{
10099 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103}
10104
10105/*
10106 * "did_filetype()" function
10107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010109f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010110 typval_T *argvars UNUSED;
10111 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112{
10113#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115#endif
10116}
10117
10118/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010119 * "diff_filler()" function
10120 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010123 typval_T *argvars UNUSED;
10124 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010125{
10126#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010128#endif
10129}
10130
10131/*
10132 * "diff_hlID()" function
10133 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010136 typval_T *argvars UNUSED;
10137 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010138{
10139#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010141 static linenr_T prev_lnum = 0;
10142 static int changedtick = 0;
10143 static int fnum = 0;
10144 static int change_start = 0;
10145 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010146 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010147 int filler_lines;
10148 int col;
10149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 if (lnum < 0) /* ignore type error in {lnum} arg */
10151 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010152 if (lnum != prev_lnum
10153 || changedtick != curbuf->b_changedtick
10154 || fnum != curbuf->b_fnum)
10155 {
10156 /* New line, buffer, change: need to get the values. */
10157 filler_lines = diff_check(curwin, lnum);
10158 if (filler_lines < 0)
10159 {
10160 if (filler_lines == -1)
10161 {
10162 change_start = MAXCOL;
10163 change_end = -1;
10164 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10165 hlID = HLF_ADD; /* added line */
10166 else
10167 hlID = HLF_CHD; /* changed line */
10168 }
10169 else
10170 hlID = HLF_ADD; /* added line */
10171 }
10172 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010173 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010174 prev_lnum = lnum;
10175 changedtick = curbuf->b_changedtick;
10176 fnum = curbuf->b_fnum;
10177 }
10178
10179 if (hlID == HLF_CHD || hlID == HLF_TXD)
10180 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010181 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010182 if (col >= change_start && col <= change_end)
10183 hlID = HLF_TXD; /* changed text */
10184 else
10185 hlID = HLF_CHD; /* changed line */
10186 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010187 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010188#endif
10189}
10190
10191/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010192 * "empty({expr})" function
10193 */
10194 static void
10195f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010196 typval_T *argvars;
10197 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010198{
10199 int n;
10200
10201 switch (argvars[0].v_type)
10202 {
10203 case VAR_STRING:
10204 case VAR_FUNC:
10205 n = argvars[0].vval.v_string == NULL
10206 || *argvars[0].vval.v_string == NUL;
10207 break;
10208 case VAR_NUMBER:
10209 n = argvars[0].vval.v_number == 0;
10210 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010211#ifdef FEAT_FLOAT
10212 case VAR_FLOAT:
10213 n = argvars[0].vval.v_float == 0.0;
10214 break;
10215#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010216 case VAR_LIST:
10217 n = argvars[0].vval.v_list == NULL
10218 || argvars[0].vval.v_list->lv_first == NULL;
10219 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010220 case VAR_DICT:
10221 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010222 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010223 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010224 default:
10225 EMSG2(_(e_intern2), "f_empty()");
10226 n = 0;
10227 }
10228
10229 rettv->vval.v_number = n;
10230}
10231
10232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 * "escape({string}, {chars})" function
10234 */
10235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010236f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010237 typval_T *argvars;
10238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239{
10240 char_u buf[NUMBUFLEN];
10241
Bram Moolenaar758711c2005-02-02 23:11:38 +000010242 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10243 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010244 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245}
10246
10247/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010248 * "eval()" function
10249 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010250 static void
10251f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010252 typval_T *argvars;
10253 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010254{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010255 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010257 s = get_tv_string_chk(&argvars[0]);
10258 if (s != NULL)
10259 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010260
Bram Moolenaar615b9972015-01-14 17:15:05 +010010261 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010262 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10263 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010264 if (p != NULL && !aborting())
10265 EMSG2(_(e_invexpr2), p);
10266 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010268 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010269 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010270 else if (*s != NUL)
10271 EMSG(_(e_trailing));
10272}
10273
10274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 * "eventhandler()" function
10276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010278f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010279 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010282 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283}
10284
10285/*
10286 * "executable()" function
10287 */
10288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010289f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010290 typval_T *argvars;
10291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010293 char_u *name = get_tv_string(&argvars[0]);
10294
10295 /* Check in $PATH and also check directly if there is a directory name. */
10296 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10297 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010298}
10299
10300/*
10301 * "exepath()" function
10302 */
10303 static void
10304f_exepath(argvars, rettv)
10305 typval_T *argvars;
10306 typval_T *rettv;
10307{
10308 char_u *p = NULL;
10309
Bram Moolenaarb5971142015-03-21 17:32:19 +010010310 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010311 rettv->v_type = VAR_STRING;
10312 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313}
10314
10315/*
10316 * "exists()" function
10317 */
10318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010319f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010320 typval_T *argvars;
10321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322{
10323 char_u *p;
10324 char_u *name;
10325 int n = FALSE;
10326 int len = 0;
10327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010328 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 if (*p == '$') /* environment variable */
10330 {
10331 /* first try "normal" environment variables (fast) */
10332 if (mch_getenv(p + 1) != NULL)
10333 n = TRUE;
10334 else
10335 {
10336 /* try expanding things like $VIM and ${HOME} */
10337 p = expand_env_save(p);
10338 if (p != NULL && *p != '$')
10339 n = TRUE;
10340 vim_free(p);
10341 }
10342 }
10343 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010345 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010346 if (*skipwhite(p) != NUL)
10347 n = FALSE; /* trailing garbage */
10348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 else if (*p == '*') /* internal or user defined function */
10350 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010351 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 }
10353 else if (*p == ':')
10354 {
10355 n = cmd_exists(p + 1);
10356 }
10357 else if (*p == '#')
10358 {
10359#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010360 if (p[1] == '#')
10361 n = autocmd_supported(p + 2);
10362 else
10363 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364#endif
10365 }
10366 else /* internal variable */
10367 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010368 char_u *tofree;
10369 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010371 /* get_name_len() takes care of expanding curly braces */
10372 name = p;
10373 len = get_name_len(&p, &tofree, TRUE, FALSE);
10374 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010376 if (tofree != NULL)
10377 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010378 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010379 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010381 /* handle d.key, l[idx], f(expr) */
10382 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10383 if (n)
10384 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 }
10386 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010387 if (*p != NUL)
10388 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010390 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 }
10392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394}
10395
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010396#ifdef FEAT_FLOAT
10397/*
10398 * "exp()" function
10399 */
10400 static void
10401f_exp(argvars, rettv)
10402 typval_T *argvars;
10403 typval_T *rettv;
10404{
10405 float_T f;
10406
10407 rettv->v_type = VAR_FLOAT;
10408 if (get_float_arg(argvars, &f) == OK)
10409 rettv->vval.v_float = exp(f);
10410 else
10411 rettv->vval.v_float = 0.0;
10412}
10413#endif
10414
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415/*
10416 * "expand()" function
10417 */
10418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010419f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010420 typval_T *argvars;
10421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422{
10423 char_u *s;
10424 int len;
10425 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010426 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010429 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010431 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010432 if (argvars[1].v_type != VAR_UNKNOWN
10433 && argvars[2].v_type != VAR_UNKNOWN
10434 && get_tv_number_chk(&argvars[2], &error)
10435 && !error)
10436 {
10437 rettv->v_type = VAR_LIST;
10438 rettv->vval.v_list = NULL;
10439 }
10440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 if (*s == '%' || *s == '#' || *s == '<')
10443 {
10444 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010445 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010447 if (rettv->v_type == VAR_LIST)
10448 {
10449 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10450 list_append_string(rettv->vval.v_list, result, -1);
10451 else
10452 vim_free(result);
10453 }
10454 else
10455 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 }
10457 else
10458 {
10459 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010460 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010461 if (argvars[1].v_type != VAR_UNKNOWN
10462 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010463 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010464 if (!error)
10465 {
10466 ExpandInit(&xpc);
10467 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010468 if (p_wic)
10469 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010470 if (rettv->v_type == VAR_STRING)
10471 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10472 options, WILD_ALL);
10473 else if (rettv_list_alloc(rettv) != FAIL)
10474 {
10475 int i;
10476
10477 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10478 for (i = 0; i < xpc.xp_numfiles; i++)
10479 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10480 ExpandCleanup(&xpc);
10481 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 }
10483 else
10484 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 }
10486}
10487
10488/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010489 * Go over all entries in "d2" and add them to "d1".
10490 * When "action" is "error" then a duplicate key is an error.
10491 * When "action" is "force" then a duplicate key is overwritten.
10492 * Otherwise duplicate keys are ignored ("action" is "keep").
10493 */
10494 void
10495dict_extend(d1, d2, action)
10496 dict_T *d1;
10497 dict_T *d2;
10498 char_u *action;
10499{
10500 dictitem_T *di1;
10501 hashitem_T *hi2;
10502 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010503 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010504
10505 todo = (int)d2->dv_hashtab.ht_used;
10506 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10507 {
10508 if (!HASHITEM_EMPTY(hi2))
10509 {
10510 --todo;
10511 di1 = dict_find(d1, hi2->hi_key, -1);
10512 if (d1->dv_scope != 0)
10513 {
10514 /* Disallow replacing a builtin function in l: and g:.
10515 * Check the key to be valid when adding to any
10516 * scope. */
10517 if (d1->dv_scope == VAR_DEF_SCOPE
10518 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10519 && var_check_func_name(hi2->hi_key,
10520 di1 == NULL))
10521 break;
10522 if (!valid_varname(hi2->hi_key))
10523 break;
10524 }
10525 if (di1 == NULL)
10526 {
10527 di1 = dictitem_copy(HI2DI(hi2));
10528 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10529 dictitem_free(di1);
10530 }
10531 else if (*action == 'e')
10532 {
10533 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10534 break;
10535 }
10536 else if (*action == 'f' && HI2DI(hi2) != di1)
10537 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010538 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10539 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010540 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010541 clear_tv(&di1->di_tv);
10542 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10543 }
10544 }
10545 }
10546}
10547
10548/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010549 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010550 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010551 */
10552 static void
10553f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010554 typval_T *argvars;
10555 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010556{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010557 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010558
Bram Moolenaare9a41262005-01-15 22:18:47 +000010559 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010560 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010561 list_T *l1, *l2;
10562 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010565
Bram Moolenaare9a41262005-01-15 22:18:47 +000010566 l1 = argvars[0].vval.v_list;
10567 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010568 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010569 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 {
10571 if (argvars[2].v_type != VAR_UNKNOWN)
10572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010573 before = get_tv_number_chk(&argvars[2], &error);
10574 if (error)
10575 return; /* type error; errmsg already given */
10576
Bram Moolenaar758711c2005-02-02 23:11:38 +000010577 if (before == l1->lv_len)
10578 item = NULL;
10579 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010581 item = list_find(l1, before);
10582 if (item == NULL)
10583 {
10584 EMSGN(_(e_listidx), before);
10585 return;
10586 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 }
10588 }
10589 else
10590 item = NULL;
10591 list_extend(l1, l2, item);
10592
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 copy_tv(&argvars[0], rettv);
10594 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010595 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010596 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10597 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010598 dict_T *d1, *d2;
10599 char_u *action;
10600 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601
10602 d1 = argvars[0].vval.v_dict;
10603 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010604 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010605 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010606 {
10607 /* Check the third argument. */
10608 if (argvars[2].v_type != VAR_UNKNOWN)
10609 {
10610 static char *(av[]) = {"keep", "force", "error"};
10611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 action = get_tv_string_chk(&argvars[2]);
10613 if (action == NULL)
10614 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010615 for (i = 0; i < 3; ++i)
10616 if (STRCMP(action, av[i]) == 0)
10617 break;
10618 if (i == 3)
10619 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010620 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010621 return;
10622 }
10623 }
10624 else
10625 action = (char_u *)"force";
10626
Bram Moolenaara9922d62013-05-30 13:01:18 +020010627 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010628
Bram Moolenaare9a41262005-01-15 22:18:47 +000010629 copy_tv(&argvars[0], rettv);
10630 }
10631 }
10632 else
10633 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010634}
10635
10636/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010637 * "feedkeys()" function
10638 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010639 static void
10640f_feedkeys(argvars, rettv)
10641 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010642 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010643{
10644 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010645 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010646 char_u *keys, *flags;
10647 char_u nbuf[NUMBUFLEN];
10648 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010649 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010650
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010651 /* This is not allowed in the sandbox. If the commands would still be
10652 * executed in the sandbox it would be OK, but it probably happens later,
10653 * when "sandbox" is no longer set. */
10654 if (check_secure())
10655 return;
10656
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010657 keys = get_tv_string(&argvars[0]);
10658 if (*keys != NUL)
10659 {
10660 if (argvars[1].v_type != VAR_UNKNOWN)
10661 {
10662 flags = get_tv_string_buf(&argvars[1], nbuf);
10663 for ( ; *flags != NUL; ++flags)
10664 {
10665 switch (*flags)
10666 {
10667 case 'n': remap = FALSE; break;
10668 case 'm': remap = TRUE; break;
10669 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010670 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010671 }
10672 }
10673 }
10674
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010675 /* Need to escape K_SPECIAL and CSI before putting the string in the
10676 * typeahead buffer. */
10677 keys_esc = vim_strsave_escape_csi(keys);
10678 if (keys_esc != NULL)
10679 {
10680 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010681 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010682 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010683 if (vgetc_busy)
10684 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010685 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010686 }
10687}
10688
10689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 * "filereadable()" function
10691 */
10692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010694 typval_T *argvars;
10695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010697 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 char_u *p;
10699 int n;
10700
Bram Moolenaarc236c162008-07-13 17:41:49 +000010701#ifndef O_NONBLOCK
10702# define O_NONBLOCK 0
10703#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010705 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10706 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 {
10708 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010709 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 }
10711 else
10712 n = FALSE;
10713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715}
10716
10717/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010718 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 * rights to write into.
10720 */
10721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010722f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010723 typval_T *argvars;
10724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010726 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727}
10728
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010729static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010730
10731 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010732findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010733 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010734 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010735 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010736{
10737#ifdef FEAT_SEARCHPATH
10738 char_u *fname;
10739 char_u *fresult = NULL;
10740 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10741 char_u *p;
10742 char_u pathbuf[NUMBUFLEN];
10743 int count = 1;
10744 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010745 int error = FALSE;
10746#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010747
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010748 rettv->vval.v_string = NULL;
10749 rettv->v_type = VAR_STRING;
10750
10751#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010753
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010754 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010756 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10757 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010758 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010759 else
10760 {
10761 if (*p != NUL)
10762 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010764 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010765 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010766 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010767 }
10768
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010769 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10770 error = TRUE;
10771
10772 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010774 do
10775 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010776 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010777 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010778 fresult = find_file_in_path_option(first ? fname : NULL,
10779 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010780 0, first, path,
10781 find_what,
10782 curbuf->b_ffname,
10783 find_what == FINDFILE_DIR
10784 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010785 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010786
10787 if (fresult != NULL && rettv->v_type == VAR_LIST)
10788 list_append_string(rettv->vval.v_list, fresult, -1);
10789
10790 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010792
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010793 if (rettv->v_type == VAR_STRING)
10794 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010795#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010796}
10797
Bram Moolenaar33570922005-01-25 22:26:29 +000010798static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10799static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010800
10801/*
10802 * Implementation of map() and filter().
10803 */
10804 static void
10805filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010806 typval_T *argvars;
10807 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010808 int map;
10809{
10810 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010811 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010812 listitem_T *li, *nli;
10813 list_T *l = NULL;
10814 dictitem_T *di;
10815 hashtab_T *ht;
10816 hashitem_T *hi;
10817 dict_T *d = NULL;
10818 typval_T save_val;
10819 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010821 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010822 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010823 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010824 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010825 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010826 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010827
Bram Moolenaare9a41262005-01-15 22:18:47 +000010828 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010829 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010830 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010831 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010832 return;
10833 }
10834 else if (argvars[0].v_type == VAR_DICT)
10835 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010836 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010837 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838 return;
10839 }
10840 else
10841 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010842 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010843 return;
10844 }
10845
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010846 expr = get_tv_string_buf_chk(&argvars[1], buf);
10847 /* On type errors, the preceding call has already displayed an error
10848 * message. Avoid a misleading error message for an empty string that
10849 * was not passed as argument. */
10850 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010852 prepare_vimvar(VV_VAL, &save_val);
10853 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010854
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010855 /* We reset "did_emsg" to be able to detect whether an error
10856 * occurred during evaluation of the expression. */
10857 save_did_emsg = did_emsg;
10858 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010859
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010860 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010861 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010863 vimvars[VV_KEY].vv_type = VAR_STRING;
10864
10865 ht = &d->dv_hashtab;
10866 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010867 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010870 if (!HASHITEM_EMPTY(hi))
10871 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010872 int r;
10873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 --todo;
10875 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010876 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020010877 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10878 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010879 break;
10880 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010881 r = filter_map_one(&di->di_tv, expr, map, &rem);
10882 clear_tv(&vimvars[VV_KEY].vv_tv);
10883 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010884 break;
10885 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010886 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010887 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10888 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010889 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010890 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010891 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010892 }
10893 }
10894 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010895 }
10896 else
10897 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010898 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900 for (li = l->lv_first; li != NULL; li = nli)
10901 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010902 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010903 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010904 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010905 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010906 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010907 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010908 break;
10909 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010910 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010911 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010912 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010913 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010914
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010915 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010916 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010917
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010918 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010919 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010920
10921 copy_tv(&argvars[0], rettv);
10922}
10923
10924 static int
10925filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010926 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010927 char_u *expr;
10928 int map;
10929 int *remp;
10930{
Bram Moolenaar33570922005-01-25 22:26:29 +000010931 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010932 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010933 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010934
Bram Moolenaar33570922005-01-25 22:26:29 +000010935 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010936 s = expr;
10937 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010938 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010939 if (*s != NUL) /* check for trailing chars after expr */
10940 {
10941 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010942 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010943 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010944 }
10945 if (map)
10946 {
10947 /* map(): replace the list item value */
10948 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010949 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010950 *tv = rettv;
10951 }
10952 else
10953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 int error = FALSE;
10955
Bram Moolenaare9a41262005-01-15 22:18:47 +000010956 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010958 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 /* On type error, nothing has been removed; return FAIL to stop the
10960 * loop. The error message was given by get_tv_number_chk(). */
10961 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010962 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010963 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010964 retval = OK;
10965theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010966 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010967 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010968}
10969
10970/*
10971 * "filter()" function
10972 */
10973 static void
10974f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010975 typval_T *argvars;
10976 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010977{
10978 filter_map(argvars, rettv, FALSE);
10979}
10980
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010981/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010982 * "finddir({fname}[, {path}[, {count}]])" function
10983 */
10984 static void
10985f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010986 typval_T *argvars;
10987 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010988{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010989 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010990}
10991
10992/*
10993 * "findfile({fname}[, {path}[, {count}]])" function
10994 */
10995 static void
10996f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010997 typval_T *argvars;
10998 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010999{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011000 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011001}
11002
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011003#ifdef FEAT_FLOAT
11004/*
11005 * "float2nr({float})" function
11006 */
11007 static void
11008f_float2nr(argvars, rettv)
11009 typval_T *argvars;
11010 typval_T *rettv;
11011{
11012 float_T f;
11013
11014 if (get_float_arg(argvars, &f) == OK)
11015 {
11016 if (f < -0x7fffffff)
11017 rettv->vval.v_number = -0x7fffffff;
11018 else if (f > 0x7fffffff)
11019 rettv->vval.v_number = 0x7fffffff;
11020 else
11021 rettv->vval.v_number = (varnumber_T)f;
11022 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011023}
11024
11025/*
11026 * "floor({float})" function
11027 */
11028 static void
11029f_floor(argvars, rettv)
11030 typval_T *argvars;
11031 typval_T *rettv;
11032{
11033 float_T f;
11034
11035 rettv->v_type = VAR_FLOAT;
11036 if (get_float_arg(argvars, &f) == OK)
11037 rettv->vval.v_float = floor(f);
11038 else
11039 rettv->vval.v_float = 0.0;
11040}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011041
11042/*
11043 * "fmod()" function
11044 */
11045 static void
11046f_fmod(argvars, rettv)
11047 typval_T *argvars;
11048 typval_T *rettv;
11049{
11050 float_T fx, fy;
11051
11052 rettv->v_type = VAR_FLOAT;
11053 if (get_float_arg(argvars, &fx) == OK
11054 && get_float_arg(&argvars[1], &fy) == OK)
11055 rettv->vval.v_float = fmod(fx, fy);
11056 else
11057 rettv->vval.v_float = 0.0;
11058}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011059#endif
11060
Bram Moolenaar0d660222005-01-07 21:51:51 +000011061/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011062 * "fnameescape({string})" function
11063 */
11064 static void
11065f_fnameescape(argvars, rettv)
11066 typval_T *argvars;
11067 typval_T *rettv;
11068{
11069 rettv->vval.v_string = vim_strsave_fnameescape(
11070 get_tv_string(&argvars[0]), FALSE);
11071 rettv->v_type = VAR_STRING;
11072}
11073
11074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 * "fnamemodify({fname}, {mods})" function
11076 */
11077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011078f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011079 typval_T *argvars;
11080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081{
11082 char_u *fname;
11083 char_u *mods;
11084 int usedlen = 0;
11085 int len;
11086 char_u *fbuf = NULL;
11087 char_u buf[NUMBUFLEN];
11088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 fname = get_tv_string_chk(&argvars[0]);
11090 mods = get_tv_string_buf_chk(&argvars[1], buf);
11091 if (fname == NULL || mods == NULL)
11092 fname = NULL;
11093 else
11094 {
11095 len = (int)STRLEN(fname);
11096 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011099 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011103 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 vim_free(fbuf);
11105}
11106
Bram Moolenaar33570922005-01-25 22:26:29 +000011107static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108
11109/*
11110 * "foldclosed()" function
11111 */
11112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011114 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011115 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011116 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117{
11118#ifdef FEAT_FOLDING
11119 linenr_T lnum;
11120 linenr_T first, last;
11121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11124 {
11125 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11126 {
11127 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011128 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011130 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 return;
11132 }
11133 }
11134#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136}
11137
11138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011139 * "foldclosed()" function
11140 */
11141 static void
11142f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011143 typval_T *argvars;
11144 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011145{
11146 foldclosed_both(argvars, rettv, FALSE);
11147}
11148
11149/*
11150 * "foldclosedend()" function
11151 */
11152 static void
11153f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011154 typval_T *argvars;
11155 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011156{
11157 foldclosed_both(argvars, rettv, TRUE);
11158}
11159
11160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 * "foldlevel()" function
11162 */
11163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011165 typval_T *argvars UNUSED;
11166 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167{
11168#ifdef FEAT_FOLDING
11169 linenr_T lnum;
11170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175}
11176
11177/*
11178 * "foldtext()" function
11179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011182 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184{
11185#ifdef FEAT_FOLDING
11186 linenr_T lnum;
11187 char_u *s;
11188 char_u *r;
11189 int len;
11190 char *txt;
11191#endif
11192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193 rettv->v_type = VAR_STRING;
11194 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011196 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11197 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11198 <= curbuf->b_ml.ml_line_count
11199 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 {
11201 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011202 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11203 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 {
11205 if (!linewhite(lnum))
11206 break;
11207 ++lnum;
11208 }
11209
11210 /* Find interesting text in this line. */
11211 s = skipwhite(ml_get(lnum));
11212 /* skip C comment-start */
11213 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011216 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011217 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011218 {
11219 s = skipwhite(ml_get(lnum + 1));
11220 if (*s == '*')
11221 s = skipwhite(s + 1);
11222 }
11223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 txt = _("+-%s%3ld lines: ");
11225 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011226 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 + 20 /* for %3ld */
11228 + STRLEN(s))); /* concatenated */
11229 if (r != NULL)
11230 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011231 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11232 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11233 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 len = (int)STRLEN(r);
11235 STRCAT(r, s);
11236 /* remove 'foldmarker' and 'commentstring' */
11237 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011238 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239 }
11240 }
11241#endif
11242}
11243
11244/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011245 * "foldtextresult(lnum)" function
11246 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011249 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011250 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011251{
11252#ifdef FEAT_FOLDING
11253 linenr_T lnum;
11254 char_u *text;
11255 char_u buf[51];
11256 foldinfo_T foldinfo;
11257 int fold_count;
11258#endif
11259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011260 rettv->v_type = VAR_STRING;
11261 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011262#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011264 /* treat illegal types and illegal string values for {lnum} the same */
11265 if (lnum < 0)
11266 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011267 fold_count = foldedCount(curwin, lnum, &foldinfo);
11268 if (fold_count > 0)
11269 {
11270 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11271 &foldinfo, buf);
11272 if (text == buf)
11273 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011274 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011275 }
11276#endif
11277}
11278
11279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 * "foreground()" function
11281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011284 typval_T *argvars UNUSED;
11285 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287#ifdef FEAT_GUI
11288 if (gui.in_use)
11289 gui_mch_set_foreground();
11290#else
11291# ifdef WIN32
11292 win32_set_foreground();
11293# endif
11294#endif
11295}
11296
11297/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011298 * "function()" function
11299 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011301f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011302 typval_T *argvars;
11303 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011304{
11305 char_u *s;
11306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011308 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011309 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011310 /* Don't check an autoload name for existence here. */
11311 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011312 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011313 else
11314 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011315 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011316 {
11317 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011318 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011319
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011320 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11321 * also be called from another script. Using trans_function_name()
11322 * would also work, but some plugins depend on the name being
11323 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011324 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011325 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011326 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011327 if (rettv->vval.v_string != NULL)
11328 {
11329 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011330 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011331 }
11332 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011333 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011334 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011336 }
11337}
11338
11339/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011340 * "garbagecollect()" function
11341 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011342 static void
11343f_garbagecollect(argvars, rettv)
11344 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011345 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011346{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011347 /* This is postponed until we are back at the toplevel, because we may be
11348 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11349 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011350
11351 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11352 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011353}
11354
11355/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011356 * "get()" function
11357 */
11358 static void
11359f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011360 typval_T *argvars;
11361 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011362{
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 listitem_T *li;
11364 list_T *l;
11365 dictitem_T *di;
11366 dict_T *d;
11367 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011368
Bram Moolenaare9a41262005-01-15 22:18:47 +000011369 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011370 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011371 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 int error = FALSE;
11374
11375 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11376 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011377 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011378 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011379 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011380 else if (argvars[0].v_type == VAR_DICT)
11381 {
11382 if ((d = argvars[0].vval.v_dict) != NULL)
11383 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011384 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011385 if (di != NULL)
11386 tv = &di->di_tv;
11387 }
11388 }
11389 else
11390 EMSG2(_(e_listdictarg), "get()");
11391
11392 if (tv == NULL)
11393 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011394 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011395 copy_tv(&argvars[2], rettv);
11396 }
11397 else
11398 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011399}
11400
Bram Moolenaar342337a2005-07-21 21:11:17 +000011401static 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 +000011402
11403/*
11404 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011405 * Return a range (from start to end) of lines in rettv from the specified
11406 * buffer.
11407 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011408 */
11409 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011410get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011411 buf_T *buf;
11412 linenr_T start;
11413 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011414 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011415 typval_T *rettv;
11416{
11417 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011418
Bram Moolenaar959a1432013-12-14 12:17:38 +010011419 rettv->v_type = VAR_STRING;
11420 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011421 if (retlist && rettv_list_alloc(rettv) == FAIL)
11422 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011423
11424 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11425 return;
11426
11427 if (!retlist)
11428 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011429 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11430 p = ml_get_buf(buf, start, FALSE);
11431 else
11432 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011433 rettv->vval.v_string = vim_strsave(p);
11434 }
11435 else
11436 {
11437 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011438 return;
11439
11440 if (start < 1)
11441 start = 1;
11442 if (end > buf->b_ml.ml_line_count)
11443 end = buf->b_ml.ml_line_count;
11444 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011445 if (list_append_string(rettv->vval.v_list,
11446 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011447 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011448 }
11449}
11450
11451/*
11452 * "getbufline()" function
11453 */
11454 static void
11455f_getbufline(argvars, rettv)
11456 typval_T *argvars;
11457 typval_T *rettv;
11458{
11459 linenr_T lnum;
11460 linenr_T end;
11461 buf_T *buf;
11462
11463 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11464 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011465 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011466 --emsg_off;
11467
Bram Moolenaar661b1822005-07-28 22:36:45 +000011468 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011469 if (argvars[2].v_type == VAR_UNKNOWN)
11470 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011471 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011472 end = get_tv_lnum_buf(&argvars[2], buf);
11473
Bram Moolenaar342337a2005-07-21 21:11:17 +000011474 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011475}
11476
Bram Moolenaar0d660222005-01-07 21:51:51 +000011477/*
11478 * "getbufvar()" function
11479 */
11480 static void
11481f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 typval_T *argvars;
11483 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011484{
11485 buf_T *buf;
11486 buf_T *save_curbuf;
11487 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011488 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011489 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011490
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011491 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11492 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011493 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011494 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011495
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011496 rettv->v_type = VAR_STRING;
11497 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011498
11499 if (buf != NULL && varname != NULL)
11500 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011501 /* set curbuf to be our buf, temporarily */
11502 save_curbuf = curbuf;
11503 curbuf = buf;
11504
Bram Moolenaar0d660222005-01-07 21:51:51 +000011505 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011506 {
11507 if (get_option_tv(&varname, rettv, TRUE) == OK)
11508 done = TRUE;
11509 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011510 else if (STRCMP(varname, "changedtick") == 0)
11511 {
11512 rettv->v_type = VAR_NUMBER;
11513 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011514 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011515 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011516 else
11517 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011518 /* Look up the variable. */
11519 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11520 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11521 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011522 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011523 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011524 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011525 done = TRUE;
11526 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011527 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011528
11529 /* restore previous notion of curbuf */
11530 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011531 }
11532
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011533 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11534 /* use the default value */
11535 copy_tv(&argvars[2], rettv);
11536
Bram Moolenaar0d660222005-01-07 21:51:51 +000011537 --emsg_off;
11538}
11539
11540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 * "getchar()" function
11542 */
11543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011545 typval_T *argvars;
11546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547{
11548 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011549 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011551 /* Position the cursor. Needed after a message that ends in a space. */
11552 windgoto(msg_row, msg_col);
11553
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 ++no_mapping;
11555 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011556 for (;;)
11557 {
11558 if (argvars[0].v_type == VAR_UNKNOWN)
11559 /* getchar(): blocking wait. */
11560 n = safe_vgetc();
11561 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11562 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011563 n = vpeekc_any();
11564 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011565 /* illegal argument or getchar(0) and no char avail: return zero */
11566 n = 0;
11567 else
11568 /* getchar(0) and char avail: return char */
11569 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011570
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011571 if (n == K_IGNORE)
11572 continue;
11573 break;
11574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575 --no_mapping;
11576 --allow_keys;
11577
Bram Moolenaar219b8702006-11-01 14:32:36 +000011578 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11579 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11580 vimvars[VV_MOUSE_COL].vv_nr = 0;
11581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011582 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583 if (IS_SPECIAL(n) || mod_mask != 0)
11584 {
11585 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11586 int i = 0;
11587
11588 /* Turn a special key into three bytes, plus modifier. */
11589 if (mod_mask != 0)
11590 {
11591 temp[i++] = K_SPECIAL;
11592 temp[i++] = KS_MODIFIER;
11593 temp[i++] = mod_mask;
11594 }
11595 if (IS_SPECIAL(n))
11596 {
11597 temp[i++] = K_SPECIAL;
11598 temp[i++] = K_SECOND(n);
11599 temp[i++] = K_THIRD(n);
11600 }
11601#ifdef FEAT_MBYTE
11602 else if (has_mbyte)
11603 i += (*mb_char2bytes)(n, temp + i);
11604#endif
11605 else
11606 temp[i++] = n;
11607 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011608 rettv->v_type = VAR_STRING;
11609 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011610
11611#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011612 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011613 {
11614 int row = mouse_row;
11615 int col = mouse_col;
11616 win_T *win;
11617 linenr_T lnum;
11618# ifdef FEAT_WINDOWS
11619 win_T *wp;
11620# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011621 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011622
11623 if (row >= 0 && col >= 0)
11624 {
11625 /* Find the window at the mouse coordinates and compute the
11626 * text position. */
11627 win = mouse_find_win(&row, &col);
11628 (void)mouse_comp_pos(win, &row, &col, &lnum);
11629# ifdef FEAT_WINDOWS
11630 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011631 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011632# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011633 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011634 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11635 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11636 }
11637 }
11638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 }
11640}
11641
11642/*
11643 * "getcharmod()" function
11644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011646f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011647 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011650 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651}
11652
11653/*
11654 * "getcmdline()" function
11655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011658 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->v_type = VAR_STRING;
11662 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663}
11664
11665/*
11666 * "getcmdpos()" function
11667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011669f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011670 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674}
11675
11676/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011677 * "getcmdtype()" function
11678 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011679 static void
11680f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011681 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011682 typval_T *rettv;
11683{
11684 rettv->v_type = VAR_STRING;
11685 rettv->vval.v_string = alloc(2);
11686 if (rettv->vval.v_string != NULL)
11687 {
11688 rettv->vval.v_string[0] = get_cmdline_type();
11689 rettv->vval.v_string[1] = NUL;
11690 }
11691}
11692
11693/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011694 * "getcmdwintype()" function
11695 */
11696 static void
11697f_getcmdwintype(argvars, rettv)
11698 typval_T *argvars UNUSED;
11699 typval_T *rettv;
11700{
11701 rettv->v_type = VAR_STRING;
11702 rettv->vval.v_string = NULL;
11703#ifdef FEAT_CMDWIN
11704 rettv->vval.v_string = alloc(2);
11705 if (rettv->vval.v_string != NULL)
11706 {
11707 rettv->vval.v_string[0] = cmdwin_type;
11708 rettv->vval.v_string[1] = NUL;
11709 }
11710#endif
11711}
11712
11713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 * "getcwd()" function
11715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011717f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011718 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011721 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011723 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011724 rettv->vval.v_string = NULL;
11725 cwd = alloc(MAXPATHL);
11726 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011728 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11729 {
11730 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011732 if (rettv->vval.v_string != NULL)
11733 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011735 }
11736 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 }
11738}
11739
11740/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011741 * "getfontname()" function
11742 */
11743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011744f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011745 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011746 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011747{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011748 rettv->v_type = VAR_STRING;
11749 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011750#ifdef FEAT_GUI
11751 if (gui.in_use)
11752 {
11753 GuiFont font;
11754 char_u *name = NULL;
11755
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011756 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011757 {
11758 /* Get the "Normal" font. Either the name saved by
11759 * hl_set_font_name() or from the font ID. */
11760 font = gui.norm_font;
11761 name = hl_get_font_name();
11762 }
11763 else
11764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011766 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11767 return;
11768 font = gui_mch_get_font(name, FALSE);
11769 if (font == NOFONT)
11770 return; /* Invalid font name, return empty string. */
11771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011772 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011773 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011774 gui_mch_free_font(font);
11775 }
11776#endif
11777}
11778
11779/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011780 * "getfperm({fname})" function
11781 */
11782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011784 typval_T *argvars;
11785 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011786{
11787 char_u *fname;
11788 struct stat st;
11789 char_u *perm = NULL;
11790 char_u flags[] = "rwx";
11791 int i;
11792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011793 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011796 if (mch_stat((char *)fname, &st) >= 0)
11797 {
11798 perm = vim_strsave((char_u *)"---------");
11799 if (perm != NULL)
11800 {
11801 for (i = 0; i < 9; i++)
11802 {
11803 if (st.st_mode & (1 << (8 - i)))
11804 perm[i] = flags[i % 3];
11805 }
11806 }
11807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011809}
11810
11811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 * "getfsize({fname})" function
11813 */
11814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011816 typval_T *argvars;
11817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818{
11819 char_u *fname;
11820 struct stat st;
11821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011822 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825
11826 if (mch_stat((char *)fname, &st) >= 0)
11827 {
11828 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011833
11834 /* non-perfect check for overflow */
11835 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11836 rettv->vval.v_number = -2;
11837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 }
11839 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011840 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841}
11842
11843/*
11844 * "getftime({fname})" function
11845 */
11846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011848 typval_T *argvars;
11849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850{
11851 char_u *fname;
11852 struct stat st;
11853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855
11856 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860}
11861
11862/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011863 * "getftype({fname})" function
11864 */
11865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 typval_T *argvars;
11868 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011869{
11870 char_u *fname;
11871 struct stat st;
11872 char_u *type = NULL;
11873 char *t;
11874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011878 if (mch_lstat((char *)fname, &st) >= 0)
11879 {
11880#ifdef S_ISREG
11881 if (S_ISREG(st.st_mode))
11882 t = "file";
11883 else if (S_ISDIR(st.st_mode))
11884 t = "dir";
11885# ifdef S_ISLNK
11886 else if (S_ISLNK(st.st_mode))
11887 t = "link";
11888# endif
11889# ifdef S_ISBLK
11890 else if (S_ISBLK(st.st_mode))
11891 t = "bdev";
11892# endif
11893# ifdef S_ISCHR
11894 else if (S_ISCHR(st.st_mode))
11895 t = "cdev";
11896# endif
11897# ifdef S_ISFIFO
11898 else if (S_ISFIFO(st.st_mode))
11899 t = "fifo";
11900# endif
11901# ifdef S_ISSOCK
11902 else if (S_ISSOCK(st.st_mode))
11903 t = "fifo";
11904# endif
11905 else
11906 t = "other";
11907#else
11908# ifdef S_IFMT
11909 switch (st.st_mode & S_IFMT)
11910 {
11911 case S_IFREG: t = "file"; break;
11912 case S_IFDIR: t = "dir"; break;
11913# ifdef S_IFLNK
11914 case S_IFLNK: t = "link"; break;
11915# endif
11916# ifdef S_IFBLK
11917 case S_IFBLK: t = "bdev"; break;
11918# endif
11919# ifdef S_IFCHR
11920 case S_IFCHR: t = "cdev"; break;
11921# endif
11922# ifdef S_IFIFO
11923 case S_IFIFO: t = "fifo"; break;
11924# endif
11925# ifdef S_IFSOCK
11926 case S_IFSOCK: t = "socket"; break;
11927# endif
11928 default: t = "other";
11929 }
11930# else
11931 if (mch_isdir(fname))
11932 t = "dir";
11933 else
11934 t = "file";
11935# endif
11936#endif
11937 type = vim_strsave((char_u *)t);
11938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011939 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011940}
11941
11942/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011943 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011944 */
11945 static void
11946f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011947 typval_T *argvars;
11948 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011949{
11950 linenr_T lnum;
11951 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011952 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011953
11954 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011955 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011956 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011957 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011958 retlist = FALSE;
11959 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011960 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011961 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011962 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011963 retlist = TRUE;
11964 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011965
Bram Moolenaar342337a2005-07-21 21:11:17 +000011966 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011967}
11968
11969/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011970 * "getmatches()" function
11971 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011972 static void
11973f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011974 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011975 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011976{
11977#ifdef FEAT_SEARCH_EXTRA
11978 dict_T *dict;
11979 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011980 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011981
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011982 if (rettv_list_alloc(rettv) == OK)
11983 {
11984 while (cur != NULL)
11985 {
11986 dict = dict_alloc();
11987 if (dict == NULL)
11988 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011989 if (cur->match.regprog == NULL)
11990 {
11991 /* match added with matchaddpos() */
11992 for (i = 0; i < MAXPOSMATCH; ++i)
11993 {
11994 llpos_T *llpos;
11995 char buf[6];
11996 list_T *l;
11997
11998 llpos = &cur->pos.pos[i];
11999 if (llpos->lnum == 0)
12000 break;
12001 l = list_alloc();
12002 if (l == NULL)
12003 break;
12004 list_append_number(l, (varnumber_T)llpos->lnum);
12005 if (llpos->col > 0)
12006 {
12007 list_append_number(l, (varnumber_T)llpos->col);
12008 list_append_number(l, (varnumber_T)llpos->len);
12009 }
12010 sprintf(buf, "pos%d", i + 1);
12011 dict_add_list(dict, buf, l);
12012 }
12013 }
12014 else
12015 {
12016 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12017 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012018 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012019 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12020 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
12021 list_append_dict(rettv->vval.v_list, dict);
12022 cur = cur->next;
12023 }
12024 }
12025#endif
12026}
12027
12028/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012029 * "getpid()" function
12030 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012031 static void
12032f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012033 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012034 typval_T *rettv;
12035{
12036 rettv->vval.v_number = mch_get_pid();
12037}
12038
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012039static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12040
12041/*
12042 * "getcurpos()" function
12043 */
12044 static void
12045f_getcurpos(argvars, rettv)
12046 typval_T *argvars;
12047 typval_T *rettv;
12048{
12049 getpos_both(argvars, rettv, TRUE);
12050}
12051
Bram Moolenaar18081e32008-02-20 19:11:07 +000012052/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012053 * "getpos(string)" function
12054 */
12055 static void
12056f_getpos(argvars, rettv)
12057 typval_T *argvars;
12058 typval_T *rettv;
12059{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012060 getpos_both(argvars, rettv, FALSE);
12061}
12062
12063 static void
12064getpos_both(argvars, rettv, getcurpos)
12065 typval_T *argvars;
12066 typval_T *rettv;
12067 int getcurpos;
12068{
Bram Moolenaara5525202006-03-02 22:52:09 +000012069 pos_T *fp;
12070 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012071 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012072
12073 if (rettv_list_alloc(rettv) == OK)
12074 {
12075 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012076 if (getcurpos)
12077 fp = &curwin->w_cursor;
12078 else
12079 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012080 if (fnum != -1)
12081 list_append_number(l, (varnumber_T)fnum);
12082 else
12083 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012084 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12085 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012086 list_append_number(l, (fp != NULL)
12087 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012088 : (varnumber_T)0);
12089 list_append_number(l,
12090#ifdef FEAT_VIRTUALEDIT
12091 (fp != NULL) ? (varnumber_T)fp->coladd :
12092#endif
12093 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012094 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012095 list_append_number(l, curwin->w_curswant == MAXCOL ?
12096 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012097 }
12098 else
12099 rettv->vval.v_number = FALSE;
12100}
12101
12102/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012103 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012104 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012105 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012106f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012107 typval_T *argvars UNUSED;
12108 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012109{
12110#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012111 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012112#endif
12113
Bram Moolenaar2641f772005-03-25 21:58:17 +000012114#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012115 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012116 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012117 wp = NULL;
12118 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12119 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012120 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012121 if (wp == NULL)
12122 return;
12123 }
12124
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012125 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012126 }
12127#endif
12128}
12129
12130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 * "getreg()" function
12132 */
12133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012135 typval_T *argvars;
12136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137{
12138 char_u *strregname;
12139 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012140 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012141 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012142 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012144 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012146 strregname = get_tv_string_chk(&argvars[0]);
12147 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012148 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012150 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012151 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12152 return_list = get_tv_number_chk(&argvars[2], &error);
12153 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012156 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012157
12158 if (error)
12159 return;
12160
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 regname = (strregname == NULL ? '"' : *strregname);
12162 if (regname == 0)
12163 regname = '"';
12164
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012165 if (return_list)
12166 {
12167 rettv->v_type = VAR_LIST;
12168 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12169 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012170 if (rettv->vval.v_list != NULL)
12171 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012172 }
12173 else
12174 {
12175 rettv->v_type = VAR_STRING;
12176 rettv->vval.v_string = get_reg_contents(regname,
12177 arg2 ? GREG_EXPR_SRC : 0);
12178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179}
12180
12181/*
12182 * "getregtype()" function
12183 */
12184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012185f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012186 typval_T *argvars;
12187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188{
12189 char_u *strregname;
12190 int regname;
12191 char_u buf[NUMBUFLEN + 2];
12192 long reglen = 0;
12193
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012194 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012195 {
12196 strregname = get_tv_string_chk(&argvars[0]);
12197 if (strregname == NULL) /* type error; errmsg already given */
12198 {
12199 rettv->v_type = VAR_STRING;
12200 rettv->vval.v_string = NULL;
12201 return;
12202 }
12203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 else
12205 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012206 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207
12208 regname = (strregname == NULL ? '"' : *strregname);
12209 if (regname == 0)
12210 regname = '"';
12211
12212 buf[0] = NUL;
12213 buf[1] = NUL;
12214 switch (get_reg_type(regname, &reglen))
12215 {
12216 case MLINE: buf[0] = 'V'; break;
12217 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 case MBLOCK:
12219 buf[0] = Ctrl_V;
12220 sprintf((char *)buf + 1, "%ld", reglen + 1);
12221 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012223 rettv->v_type = VAR_STRING;
12224 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225}
12226
12227/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012228 * "gettabvar()" function
12229 */
12230 static void
12231f_gettabvar(argvars, rettv)
12232 typval_T *argvars;
12233 typval_T *rettv;
12234{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012235 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012236 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012237 dictitem_T *v;
12238 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012239 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012240
12241 rettv->v_type = VAR_STRING;
12242 rettv->vval.v_string = NULL;
12243
12244 varname = get_tv_string_chk(&argvars[1]);
12245 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12246 if (tp != NULL && varname != NULL)
12247 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012248 /* Set tp to be our tabpage, temporarily. Also set the window to the
12249 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012250 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12251 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012252 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012253 /* look up the variable */
12254 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12255 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12256 if (v != NULL)
12257 {
12258 copy_tv(&v->di_tv, rettv);
12259 done = TRUE;
12260 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012261 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012262
12263 /* restore previous notion of curwin */
12264 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012265 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012266
12267 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012268 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012269}
12270
12271/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012272 * "gettabwinvar()" function
12273 */
12274 static void
12275f_gettabwinvar(argvars, rettv)
12276 typval_T *argvars;
12277 typval_T *rettv;
12278{
12279 getwinvar(argvars, rettv, 1);
12280}
12281
12282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 * "getwinposx()" function
12284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012286f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012287 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291#ifdef FEAT_GUI
12292 if (gui.in_use)
12293 {
12294 int x, y;
12295
12296 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 }
12299#endif
12300}
12301
12302/*
12303 * "getwinposy()" function
12304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012306f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012307 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012310 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311#ifdef FEAT_GUI
12312 if (gui.in_use)
12313 {
12314 int x, y;
12315
12316 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 }
12319#endif
12320}
12321
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012322/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012323 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012324 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012325 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012326find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012327 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012328 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012329{
12330#ifdef FEAT_WINDOWS
12331 win_T *wp;
12332#endif
12333 int nr;
12334
12335 nr = get_tv_number_chk(vp, NULL);
12336
12337#ifdef FEAT_WINDOWS
12338 if (nr < 0)
12339 return NULL;
12340 if (nr == 0)
12341 return curwin;
12342
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012343 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12344 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012345 if (--nr <= 0)
12346 break;
12347 return wp;
12348#else
12349 if (nr == 0 || nr == 1)
12350 return curwin;
12351 return NULL;
12352#endif
12353}
12354
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355/*
12356 * "getwinvar()" function
12357 */
12358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012359f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012360 typval_T *argvars;
12361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012363 getwinvar(argvars, rettv, 0);
12364}
12365
12366/*
12367 * getwinvar() and gettabwinvar()
12368 */
12369 static void
12370getwinvar(argvars, rettv, off)
12371 typval_T *argvars;
12372 typval_T *rettv;
12373 int off; /* 1 for gettabwinvar() */
12374{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 win_T *win, *oldcurwin;
12376 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012377 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012378 tabpage_T *tp = NULL;
12379 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012380 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012382#ifdef FEAT_WINDOWS
12383 if (off == 1)
12384 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12385 else
12386 tp = curtab;
12387#endif
12388 win = find_win_by_nr(&argvars[off], tp);
12389 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012390 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012392 rettv->v_type = VAR_STRING;
12393 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394
12395 if (win != NULL && varname != NULL)
12396 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012397 /* Set curwin to be our win, temporarily. Also set the tabpage,
12398 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012399 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012400 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012401 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012402 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012403 if (get_option_tv(&varname, rettv, 1) == OK)
12404 done = TRUE;
12405 }
12406 else
12407 {
12408 /* Look up the variable. */
12409 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12410 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12411 varname, FALSE);
12412 if (v != NULL)
12413 {
12414 copy_tv(&v->di_tv, rettv);
12415 done = TRUE;
12416 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012419
12420 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012421 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422 }
12423
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012424 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12425 /* use the default return value */
12426 copy_tv(&argvars[off + 2], rettv);
12427
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 --emsg_off;
12429}
12430
12431/*
12432 * "glob()" function
12433 */
12434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012436 typval_T *argvars;
12437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012439 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012441 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012443 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012444 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012445 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012446 if (argvars[1].v_type != VAR_UNKNOWN)
12447 {
12448 if (get_tv_number_chk(&argvars[1], &error))
12449 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012450 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012451 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012452 if (get_tv_number_chk(&argvars[2], &error))
12453 {
12454 rettv->v_type = VAR_LIST;
12455 rettv->vval.v_list = NULL;
12456 }
12457 if (argvars[3].v_type != VAR_UNKNOWN
12458 && get_tv_number_chk(&argvars[3], &error))
12459 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012460 }
12461 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012462 if (!error)
12463 {
12464 ExpandInit(&xpc);
12465 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012466 if (p_wic)
12467 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012468 if (rettv->v_type == VAR_STRING)
12469 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012470 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012471 else if (rettv_list_alloc(rettv) != FAIL)
12472 {
12473 int i;
12474
12475 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12476 NULL, options, WILD_ALL_KEEP);
12477 for (i = 0; i < xpc.xp_numfiles; i++)
12478 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12479
12480 ExpandCleanup(&xpc);
12481 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012482 }
12483 else
12484 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485}
12486
12487/*
12488 * "globpath()" function
12489 */
12490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012492 typval_T *argvars;
12493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012495 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012497 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012498 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012499 garray_T ga;
12500 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012502 /* When the optional second argument is non-zero, don't remove matches
12503 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012505 if (argvars[2].v_type != VAR_UNKNOWN)
12506 {
12507 if (get_tv_number_chk(&argvars[2], &error))
12508 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012509 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012510 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012511 if (get_tv_number_chk(&argvars[3], &error))
12512 {
12513 rettv->v_type = VAR_LIST;
12514 rettv->vval.v_list = NULL;
12515 }
12516 if (argvars[4].v_type != VAR_UNKNOWN
12517 && get_tv_number_chk(&argvars[4], &error))
12518 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012519 }
12520 }
12521 if (file != NULL && !error)
12522 {
12523 ga_init2(&ga, (int)sizeof(char_u *), 10);
12524 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12525 if (rettv->v_type == VAR_STRING)
12526 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12527 else if (rettv_list_alloc(rettv) != FAIL)
12528 for (i = 0; i < ga.ga_len; ++i)
12529 list_append_string(rettv->vval.v_list,
12530 ((char_u **)(ga.ga_data))[i], -1);
12531 ga_clear_strings(&ga);
12532 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012533 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012534 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535}
12536
12537/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012538 * "glob2regpat()" function
12539 */
12540 static void
12541f_glob2regpat(argvars, rettv)
12542 typval_T *argvars;
12543 typval_T *rettv;
12544{
12545 char_u *pat = get_tv_string_chk(&argvars[0]);
12546
12547 rettv->v_type = VAR_STRING;
12548 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12549}
12550
12551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552 * "has()" function
12553 */
12554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012555f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012556 typval_T *argvars;
12557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558{
12559 int i;
12560 char_u *name;
12561 int n = FALSE;
12562 static char *(has_list[]) =
12563 {
12564#ifdef AMIGA
12565 "amiga",
12566# ifdef FEAT_ARP
12567 "arp",
12568# endif
12569#endif
12570#ifdef __BEOS__
12571 "beos",
12572#endif
12573#ifdef MSDOS
12574# ifdef DJGPP
12575 "dos32",
12576# else
12577 "dos16",
12578# endif
12579#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012580#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581 "mac",
12582#endif
12583#if defined(MACOS_X_UNIX)
12584 "macunix",
12585#endif
12586#ifdef OS2
12587 "os2",
12588#endif
12589#ifdef __QNX__
12590 "qnx",
12591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592#ifdef UNIX
12593 "unix",
12594#endif
12595#ifdef VMS
12596 "vms",
12597#endif
12598#ifdef WIN16
12599 "win16",
12600#endif
12601#ifdef WIN32
12602 "win32",
12603#endif
12604#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12605 "win32unix",
12606#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012607#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 "win64",
12609#endif
12610#ifdef EBCDIC
12611 "ebcdic",
12612#endif
12613#ifndef CASE_INSENSITIVE_FILENAME
12614 "fname_case",
12615#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012616#ifdef HAVE_ACL
12617 "acl",
12618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619#ifdef FEAT_ARABIC
12620 "arabic",
12621#endif
12622#ifdef FEAT_AUTOCMD
12623 "autocmd",
12624#endif
12625#ifdef FEAT_BEVAL
12626 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012627# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12628 "balloon_multiline",
12629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630#endif
12631#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12632 "builtin_terms",
12633# ifdef ALL_BUILTIN_TCAPS
12634 "all_builtin_terms",
12635# endif
12636#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012637#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12638 || defined(FEAT_GUI_W32) \
12639 || defined(FEAT_GUI_MOTIF))
12640 "browsefilter",
12641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642#ifdef FEAT_BYTEOFF
12643 "byte_offset",
12644#endif
12645#ifdef FEAT_CINDENT
12646 "cindent",
12647#endif
12648#ifdef FEAT_CLIENTSERVER
12649 "clientserver",
12650#endif
12651#ifdef FEAT_CLIPBOARD
12652 "clipboard",
12653#endif
12654#ifdef FEAT_CMDL_COMPL
12655 "cmdline_compl",
12656#endif
12657#ifdef FEAT_CMDHIST
12658 "cmdline_hist",
12659#endif
12660#ifdef FEAT_COMMENTS
12661 "comments",
12662#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012663#ifdef FEAT_CONCEAL
12664 "conceal",
12665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666#ifdef FEAT_CRYPT
12667 "cryptv",
12668#endif
12669#ifdef FEAT_CSCOPE
12670 "cscope",
12671#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012672#ifdef FEAT_CURSORBIND
12673 "cursorbind",
12674#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012675#ifdef CURSOR_SHAPE
12676 "cursorshape",
12677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678#ifdef DEBUG
12679 "debug",
12680#endif
12681#ifdef FEAT_CON_DIALOG
12682 "dialog_con",
12683#endif
12684#ifdef FEAT_GUI_DIALOG
12685 "dialog_gui",
12686#endif
12687#ifdef FEAT_DIFF
12688 "diff",
12689#endif
12690#ifdef FEAT_DIGRAPHS
12691 "digraphs",
12692#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012693#ifdef FEAT_DIRECTX
12694 "directx",
12695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696#ifdef FEAT_DND
12697 "dnd",
12698#endif
12699#ifdef FEAT_EMACS_TAGS
12700 "emacs_tags",
12701#endif
12702 "eval", /* always present, of course! */
12703#ifdef FEAT_EX_EXTRA
12704 "ex_extra",
12705#endif
12706#ifdef FEAT_SEARCH_EXTRA
12707 "extra_search",
12708#endif
12709#ifdef FEAT_FKMAP
12710 "farsi",
12711#endif
12712#ifdef FEAT_SEARCHPATH
12713 "file_in_path",
12714#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012715#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012716 "filterpipe",
12717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718#ifdef FEAT_FIND_ID
12719 "find_in_path",
12720#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012721#ifdef FEAT_FLOAT
12722 "float",
12723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724#ifdef FEAT_FOLDING
12725 "folding",
12726#endif
12727#ifdef FEAT_FOOTER
12728 "footer",
12729#endif
12730#if !defined(USE_SYSTEM) && defined(UNIX)
12731 "fork",
12732#endif
12733#ifdef FEAT_GETTEXT
12734 "gettext",
12735#endif
12736#ifdef FEAT_GUI
12737 "gui",
12738#endif
12739#ifdef FEAT_GUI_ATHENA
12740# ifdef FEAT_GUI_NEXTAW
12741 "gui_neXtaw",
12742# else
12743 "gui_athena",
12744# endif
12745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746#ifdef FEAT_GUI_GTK
12747 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012750#ifdef FEAT_GUI_GNOME
12751 "gui_gnome",
12752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753#ifdef FEAT_GUI_MAC
12754 "gui_mac",
12755#endif
12756#ifdef FEAT_GUI_MOTIF
12757 "gui_motif",
12758#endif
12759#ifdef FEAT_GUI_PHOTON
12760 "gui_photon",
12761#endif
12762#ifdef FEAT_GUI_W16
12763 "gui_win16",
12764#endif
12765#ifdef FEAT_GUI_W32
12766 "gui_win32",
12767#endif
12768#ifdef FEAT_HANGULIN
12769 "hangul_input",
12770#endif
12771#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12772 "iconv",
12773#endif
12774#ifdef FEAT_INS_EXPAND
12775 "insert_expand",
12776#endif
12777#ifdef FEAT_JUMPLIST
12778 "jumplist",
12779#endif
12780#ifdef FEAT_KEYMAP
12781 "keymap",
12782#endif
12783#ifdef FEAT_LANGMAP
12784 "langmap",
12785#endif
12786#ifdef FEAT_LIBCALL
12787 "libcall",
12788#endif
12789#ifdef FEAT_LINEBREAK
12790 "linebreak",
12791#endif
12792#ifdef FEAT_LISP
12793 "lispindent",
12794#endif
12795#ifdef FEAT_LISTCMDS
12796 "listcmds",
12797#endif
12798#ifdef FEAT_LOCALMAP
12799 "localmap",
12800#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012801#ifdef FEAT_LUA
12802# ifndef DYNAMIC_LUA
12803 "lua",
12804# endif
12805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806#ifdef FEAT_MENU
12807 "menu",
12808#endif
12809#ifdef FEAT_SESSION
12810 "mksession",
12811#endif
12812#ifdef FEAT_MODIFY_FNAME
12813 "modify_fname",
12814#endif
12815#ifdef FEAT_MOUSE
12816 "mouse",
12817#endif
12818#ifdef FEAT_MOUSESHAPE
12819 "mouseshape",
12820#endif
12821#if defined(UNIX) || defined(VMS)
12822# ifdef FEAT_MOUSE_DEC
12823 "mouse_dec",
12824# endif
12825# ifdef FEAT_MOUSE_GPM
12826 "mouse_gpm",
12827# endif
12828# ifdef FEAT_MOUSE_JSB
12829 "mouse_jsbterm",
12830# endif
12831# ifdef FEAT_MOUSE_NET
12832 "mouse_netterm",
12833# endif
12834# ifdef FEAT_MOUSE_PTERM
12835 "mouse_pterm",
12836# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012837# ifdef FEAT_MOUSE_SGR
12838 "mouse_sgr",
12839# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012840# ifdef FEAT_SYSMOUSE
12841 "mouse_sysmouse",
12842# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012843# ifdef FEAT_MOUSE_URXVT
12844 "mouse_urxvt",
12845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846# ifdef FEAT_MOUSE_XTERM
12847 "mouse_xterm",
12848# endif
12849#endif
12850#ifdef FEAT_MBYTE
12851 "multi_byte",
12852#endif
12853#ifdef FEAT_MBYTE_IME
12854 "multi_byte_ime",
12855#endif
12856#ifdef FEAT_MULTI_LANG
12857 "multi_lang",
12858#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012859#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012860#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012861 "mzscheme",
12862#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864#ifdef FEAT_OLE
12865 "ole",
12866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867#ifdef FEAT_PATH_EXTRA
12868 "path_extra",
12869#endif
12870#ifdef FEAT_PERL
12871#ifndef DYNAMIC_PERL
12872 "perl",
12873#endif
12874#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012875#ifdef FEAT_PERSISTENT_UNDO
12876 "persistent_undo",
12877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878#ifdef FEAT_PYTHON
12879#ifndef DYNAMIC_PYTHON
12880 "python",
12881#endif
12882#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012883#ifdef FEAT_PYTHON3
12884#ifndef DYNAMIC_PYTHON3
12885 "python3",
12886#endif
12887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888#ifdef FEAT_POSTSCRIPT
12889 "postscript",
12890#endif
12891#ifdef FEAT_PRINTER
12892 "printer",
12893#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012894#ifdef FEAT_PROFILE
12895 "profile",
12896#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012897#ifdef FEAT_RELTIME
12898 "reltime",
12899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900#ifdef FEAT_QUICKFIX
12901 "quickfix",
12902#endif
12903#ifdef FEAT_RIGHTLEFT
12904 "rightleft",
12905#endif
12906#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12907 "ruby",
12908#endif
12909#ifdef FEAT_SCROLLBIND
12910 "scrollbind",
12911#endif
12912#ifdef FEAT_CMDL_INFO
12913 "showcmd",
12914 "cmdline_info",
12915#endif
12916#ifdef FEAT_SIGNS
12917 "signs",
12918#endif
12919#ifdef FEAT_SMARTINDENT
12920 "smartindent",
12921#endif
12922#ifdef FEAT_SNIFF
12923 "sniff",
12924#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012925#ifdef STARTUPTIME
12926 "startuptime",
12927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928#ifdef FEAT_STL_OPT
12929 "statusline",
12930#endif
12931#ifdef FEAT_SUN_WORKSHOP
12932 "sun_workshop",
12933#endif
12934#ifdef FEAT_NETBEANS_INTG
12935 "netbeans_intg",
12936#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012937#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012938 "spell",
12939#endif
12940#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 "syntax",
12942#endif
12943#if defined(USE_SYSTEM) || !defined(UNIX)
12944 "system",
12945#endif
12946#ifdef FEAT_TAG_BINS
12947 "tag_binary",
12948#endif
12949#ifdef FEAT_TAG_OLDSTATIC
12950 "tag_old_static",
12951#endif
12952#ifdef FEAT_TAG_ANYWHITE
12953 "tag_any_white",
12954#endif
12955#ifdef FEAT_TCL
12956# ifndef DYNAMIC_TCL
12957 "tcl",
12958# endif
12959#endif
12960#ifdef TERMINFO
12961 "terminfo",
12962#endif
12963#ifdef FEAT_TERMRESPONSE
12964 "termresponse",
12965#endif
12966#ifdef FEAT_TEXTOBJ
12967 "textobjects",
12968#endif
12969#ifdef HAVE_TGETENT
12970 "tgetent",
12971#endif
12972#ifdef FEAT_TITLE
12973 "title",
12974#endif
12975#ifdef FEAT_TOOLBAR
12976 "toolbar",
12977#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012978#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12979 "unnamedplus",
12980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981#ifdef FEAT_USR_CMDS
12982 "user-commands", /* was accidentally included in 5.4 */
12983 "user_commands",
12984#endif
12985#ifdef FEAT_VIMINFO
12986 "viminfo",
12987#endif
12988#ifdef FEAT_VERTSPLIT
12989 "vertsplit",
12990#endif
12991#ifdef FEAT_VIRTUALEDIT
12992 "virtualedit",
12993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995#ifdef FEAT_VISUALEXTRA
12996 "visualextra",
12997#endif
12998#ifdef FEAT_VREPLACE
12999 "vreplace",
13000#endif
13001#ifdef FEAT_WILDIGN
13002 "wildignore",
13003#endif
13004#ifdef FEAT_WILDMENU
13005 "wildmenu",
13006#endif
13007#ifdef FEAT_WINDOWS
13008 "windows",
13009#endif
13010#ifdef FEAT_WAK
13011 "winaltkeys",
13012#endif
13013#ifdef FEAT_WRITEBACKUP
13014 "writebackup",
13015#endif
13016#ifdef FEAT_XIM
13017 "xim",
13018#endif
13019#ifdef FEAT_XFONTSET
13020 "xfontset",
13021#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013022#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013023 "xpm",
13024 "xpm_w32", /* for backward compatibility */
13025#else
13026# if defined(HAVE_XPM)
13027 "xpm",
13028# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030#ifdef USE_XSMP
13031 "xsmp",
13032#endif
13033#ifdef USE_XSMP_INTERACT
13034 "xsmp_interact",
13035#endif
13036#ifdef FEAT_XCLIPBOARD
13037 "xterm_clipboard",
13038#endif
13039#ifdef FEAT_XTERM_SAVE
13040 "xterm_save",
13041#endif
13042#if defined(UNIX) && defined(FEAT_X11)
13043 "X11",
13044#endif
13045 NULL
13046 };
13047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013048 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 for (i = 0; has_list[i] != NULL; ++i)
13050 if (STRICMP(name, has_list[i]) == 0)
13051 {
13052 n = TRUE;
13053 break;
13054 }
13055
13056 if (n == FALSE)
13057 {
13058 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013059 {
13060 if (name[5] == '-'
13061 && STRLEN(name) > 11
13062 && vim_isdigit(name[6])
13063 && vim_isdigit(name[8])
13064 && vim_isdigit(name[10]))
13065 {
13066 int major = atoi((char *)name + 6);
13067 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013068
13069 /* Expect "patch-9.9.01234". */
13070 n = (major < VIM_VERSION_MAJOR
13071 || (major == VIM_VERSION_MAJOR
13072 && (minor < VIM_VERSION_MINOR
13073 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013074 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013075 }
13076 else
13077 n = has_patch(atoi((char *)name + 5));
13078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 else if (STRICMP(name, "vim_starting") == 0)
13080 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013081#ifdef FEAT_MBYTE
13082 else if (STRICMP(name, "multi_byte_encoding") == 0)
13083 n = has_mbyte;
13084#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013085#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13086 else if (STRICMP(name, "balloon_multiline") == 0)
13087 n = multiline_balloon_available();
13088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089#ifdef DYNAMIC_TCL
13090 else if (STRICMP(name, "tcl") == 0)
13091 n = tcl_enabled(FALSE);
13092#endif
13093#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13094 else if (STRICMP(name, "iconv") == 0)
13095 n = iconv_enabled(FALSE);
13096#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013097#ifdef DYNAMIC_LUA
13098 else if (STRICMP(name, "lua") == 0)
13099 n = lua_enabled(FALSE);
13100#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013101#ifdef DYNAMIC_MZSCHEME
13102 else if (STRICMP(name, "mzscheme") == 0)
13103 n = mzscheme_enabled(FALSE);
13104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105#ifdef DYNAMIC_RUBY
13106 else if (STRICMP(name, "ruby") == 0)
13107 n = ruby_enabled(FALSE);
13108#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013109#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110#ifdef DYNAMIC_PYTHON
13111 else if (STRICMP(name, "python") == 0)
13112 n = python_enabled(FALSE);
13113#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013114#endif
13115#ifdef FEAT_PYTHON3
13116#ifdef DYNAMIC_PYTHON3
13117 else if (STRICMP(name, "python3") == 0)
13118 n = python3_enabled(FALSE);
13119#endif
13120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121#ifdef DYNAMIC_PERL
13122 else if (STRICMP(name, "perl") == 0)
13123 n = perl_enabled(FALSE);
13124#endif
13125#ifdef FEAT_GUI
13126 else if (STRICMP(name, "gui_running") == 0)
13127 n = (gui.in_use || gui.starting);
13128# ifdef FEAT_GUI_W32
13129 else if (STRICMP(name, "gui_win32s") == 0)
13130 n = gui_is_win32s();
13131# endif
13132# ifdef FEAT_BROWSE
13133 else if (STRICMP(name, "browse") == 0)
13134 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13135# endif
13136#endif
13137#ifdef FEAT_SYN_HL
13138 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013139 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140#endif
13141#if defined(WIN3264)
13142 else if (STRICMP(name, "win95") == 0)
13143 n = mch_windows95();
13144#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013145#ifdef FEAT_NETBEANS_INTG
13146 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013147 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 }
13150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013151 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152}
13153
13154/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013155 * "has_key()" function
13156 */
13157 static void
13158f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013159 typval_T *argvars;
13160 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013161{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013162 if (argvars[0].v_type != VAR_DICT)
13163 {
13164 EMSG(_(e_dictreq));
13165 return;
13166 }
13167 if (argvars[0].vval.v_dict == NULL)
13168 return;
13169
13170 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013171 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013172}
13173
13174/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013175 * "haslocaldir()" function
13176 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013177 static void
13178f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013179 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013180 typval_T *rettv;
13181{
13182 rettv->vval.v_number = (curwin->w_localdir != NULL);
13183}
13184
13185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 * "hasmapto()" function
13187 */
13188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013189f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013190 typval_T *argvars;
13191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192{
13193 char_u *name;
13194 char_u *mode;
13195 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013196 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013199 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200 mode = (char_u *)"nvo";
13201 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013202 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013204 if (argvars[2].v_type != VAR_UNKNOWN)
13205 abbr = get_tv_number(&argvars[2]);
13206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207
Bram Moolenaar2c932302006-03-18 21:42:09 +000013208 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212}
13213
13214/*
13215 * "histadd()" function
13216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013218f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013219 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221{
13222#ifdef FEAT_CMDHIST
13223 int histype;
13224 char_u *str;
13225 char_u buf[NUMBUFLEN];
13226#endif
13227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 if (check_restricted() || check_secure())
13230 return;
13231#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013232 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13233 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 if (histype >= 0)
13235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 if (*str != NUL)
13238 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013239 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 return;
13243 }
13244 }
13245#endif
13246}
13247
13248/*
13249 * "histdel()" function
13250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013253 typval_T *argvars UNUSED;
13254 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255{
13256#ifdef FEAT_CMDHIST
13257 int n;
13258 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013259 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13262 if (str == NULL)
13263 n = 0;
13264 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013266 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013267 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013269 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013270 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 else
13272 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013273 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013274 get_tv_string_buf(&argvars[1], buf));
13275 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276#endif
13277}
13278
13279/*
13280 * "histget()" function
13281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013283f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013284 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286{
13287#ifdef FEAT_CMDHIST
13288 int type;
13289 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013290 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013292 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13293 if (str == NULL)
13294 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013296 {
13297 type = get_histtype(str);
13298 if (argvars[1].v_type == VAR_UNKNOWN)
13299 idx = get_history_idx(type);
13300 else
13301 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13302 /* -1 on type error */
13303 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013306 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013308 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309}
13310
13311/*
13312 * "histnr()" function
13313 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013315f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013316 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318{
13319 int i;
13320
13321#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013322 char_u *history = get_tv_string_chk(&argvars[0]);
13323
13324 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 if (i >= HIST_CMD && i < HIST_COUNT)
13326 i = get_history_idx(i);
13327 else
13328#endif
13329 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013330 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331}
13332
13333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334 * "highlightID(name)" function
13335 */
13336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013337f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013338 typval_T *argvars;
13339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013341 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342}
13343
13344/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013345 * "highlight_exists()" function
13346 */
13347 static void
13348f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013349 typval_T *argvars;
13350 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013351{
13352 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13353}
13354
13355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356 * "hostname()" function
13357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362{
13363 char_u hostname[256];
13364
13365 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013366 rettv->v_type = VAR_STRING;
13367 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368}
13369
13370/*
13371 * iconv() function
13372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013374f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013375 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377{
13378#ifdef FEAT_MBYTE
13379 char_u buf1[NUMBUFLEN];
13380 char_u buf2[NUMBUFLEN];
13381 char_u *from, *to, *str;
13382 vimconv_T vimconv;
13383#endif
13384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013385 rettv->v_type = VAR_STRING;
13386 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387
13388#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013389 str = get_tv_string(&argvars[0]);
13390 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13391 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392 vimconv.vc_type = CONV_NONE;
13393 convert_setup(&vimconv, from, to);
13394
13395 /* If the encodings are equal, no conversion needed. */
13396 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013397 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400
13401 convert_setup(&vimconv, NULL, NULL);
13402 vim_free(from);
13403 vim_free(to);
13404#endif
13405}
13406
13407/*
13408 * "indent()" function
13409 */
13410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013412 typval_T *argvars;
13413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414{
13415 linenr_T lnum;
13416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013417 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013419 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422}
13423
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013424/*
13425 * "index()" function
13426 */
13427 static void
13428f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013429 typval_T *argvars;
13430 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013431{
Bram Moolenaar33570922005-01-25 22:26:29 +000013432 list_T *l;
13433 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013434 long idx = 0;
13435 int ic = FALSE;
13436
13437 rettv->vval.v_number = -1;
13438 if (argvars[0].v_type != VAR_LIST)
13439 {
13440 EMSG(_(e_listreq));
13441 return;
13442 }
13443 l = argvars[0].vval.v_list;
13444 if (l != NULL)
13445 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013446 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013447 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013449 int error = FALSE;
13450
Bram Moolenaar758711c2005-02-02 23:11:38 +000013451 /* Start at specified item. Use the cached index that list_find()
13452 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013453 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013454 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013455 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013456 ic = get_tv_number_chk(&argvars[3], &error);
13457 if (error)
13458 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013459 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013460
Bram Moolenaar758711c2005-02-02 23:11:38 +000013461 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013462 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013463 {
13464 rettv->vval.v_number = idx;
13465 break;
13466 }
13467 }
13468}
13469
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470static int inputsecret_flag = 0;
13471
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013472static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13473
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013475 * This function is used by f_input() and f_inputdialog() functions. The third
13476 * argument to f_input() specifies the type of completion to use at the
13477 * prompt. The third argument to f_inputdialog() specifies the value to return
13478 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 */
13480 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013481get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013482 typval_T *argvars;
13483 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013484 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013486 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487 char_u *p = NULL;
13488 int c;
13489 char_u buf[NUMBUFLEN];
13490 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013492 int xp_type = EXPAND_NOTHING;
13493 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013495 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013496 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497
13498#ifdef NO_CONSOLE_INPUT
13499 /* While starting up, there is no place to enter text. */
13500 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502#endif
13503
13504 cmd_silent = FALSE; /* Want to see the prompt. */
13505 if (prompt != NULL)
13506 {
13507 /* Only the part of the message after the last NL is considered as
13508 * prompt for the command line */
13509 p = vim_strrchr(prompt, '\n');
13510 if (p == NULL)
13511 p = prompt;
13512 else
13513 {
13514 ++p;
13515 c = *p;
13516 *p = NUL;
13517 msg_start();
13518 msg_clr_eos();
13519 msg_puts_attr(prompt, echo_attr);
13520 msg_didout = FALSE;
13521 msg_starthere();
13522 *p = c;
13523 }
13524 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013526 if (argvars[1].v_type != VAR_UNKNOWN)
13527 {
13528 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13529 if (defstr != NULL)
13530 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013532 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013533 {
13534 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013535 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013536 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013537
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013538 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013539 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013540
Bram Moolenaar4463f292005-09-25 22:20:24 +000013541 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13542 if (xp_name == NULL)
13543 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013544
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013545 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013546
Bram Moolenaar4463f292005-09-25 22:20:24 +000013547 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13548 &xp_arg) == FAIL)
13549 return;
13550 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013551 }
13552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013553 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013554 {
13555# ifdef FEAT_EX_EXTRA
13556 int save_ex_normal_busy = ex_normal_busy;
13557 ex_normal_busy = 0;
13558# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013559 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013560 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13561 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013562# ifdef FEAT_EX_EXTRA
13563 ex_normal_busy = save_ex_normal_busy;
13564# endif
13565 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013566 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013567 && argvars[1].v_type != VAR_UNKNOWN
13568 && argvars[2].v_type != VAR_UNKNOWN)
13569 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13570 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013571
13572 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013574 /* since the user typed this, no need to wait for return */
13575 need_wait_return = FALSE;
13576 msg_didout = FALSE;
13577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 cmd_silent = cmd_silent_save;
13579}
13580
13581/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013582 * "input()" function
13583 * Also handles inputsecret() when inputsecret is set.
13584 */
13585 static void
13586f_input(argvars, rettv)
13587 typval_T *argvars;
13588 typval_T *rettv;
13589{
13590 get_user_input(argvars, rettv, FALSE);
13591}
13592
13593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 * "inputdialog()" function
13595 */
13596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013598 typval_T *argvars;
13599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600{
13601#if defined(FEAT_GUI_TEXTDIALOG)
13602 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13603 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13604 {
13605 char_u *message;
13606 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013607 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013609 message = get_tv_string_chk(&argvars[0]);
13610 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013611 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013612 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 else
13614 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013615 if (message != NULL && defstr != NULL
13616 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013617 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 else
13620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 if (message != NULL && defstr != NULL
13622 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013623 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 rettv->vval.v_string = vim_strsave(
13625 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 }
13631 else
13632#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013633 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634}
13635
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013636/*
13637 * "inputlist()" function
13638 */
13639 static void
13640f_inputlist(argvars, rettv)
13641 typval_T *argvars;
13642 typval_T *rettv;
13643{
13644 listitem_T *li;
13645 int selected;
13646 int mouse_used;
13647
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013648#ifdef NO_CONSOLE_INPUT
13649 /* While starting up, there is no place to enter text. */
13650 if (no_console_input())
13651 return;
13652#endif
13653 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13654 {
13655 EMSG2(_(e_listarg), "inputlist()");
13656 return;
13657 }
13658
13659 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013660 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013661 lines_left = Rows; /* avoid more prompt */
13662 msg_scroll = TRUE;
13663 msg_clr_eos();
13664
13665 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13666 {
13667 msg_puts(get_tv_string(&li->li_tv));
13668 msg_putchar('\n');
13669 }
13670
13671 /* Ask for choice. */
13672 selected = prompt_for_number(&mouse_used);
13673 if (mouse_used)
13674 selected -= lines_left;
13675
13676 rettv->vval.v_number = selected;
13677}
13678
13679
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13681
13682/*
13683 * "inputrestore()" function
13684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013686f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013687 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689{
13690 if (ga_userinput.ga_len > 0)
13691 {
13692 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13694 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013695 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 }
13697 else if (p_verbose > 1)
13698 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013699 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013700 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 }
13702}
13703
13704/*
13705 * "inputsave()" function
13706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013708f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013709 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013712 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713 if (ga_grow(&ga_userinput, 1) == OK)
13714 {
13715 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13716 + ga_userinput.ga_len);
13717 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013718 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 }
13720 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013721 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722}
13723
13724/*
13725 * "inputsecret()" function
13726 */
13727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013728f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013729 typval_T *argvars;
13730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731{
13732 ++cmdline_star;
13733 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013734 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 --cmdline_star;
13736 --inputsecret_flag;
13737}
13738
13739/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013740 * "insert()" function
13741 */
13742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013743f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013744 typval_T *argvars;
13745 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013746{
13747 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 listitem_T *item;
13749 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013750 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013751
13752 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013753 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013754 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013755 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013756 {
13757 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013758 before = get_tv_number_chk(&argvars[2], &error);
13759 if (error)
13760 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013761
Bram Moolenaar758711c2005-02-02 23:11:38 +000013762 if (before == l->lv_len)
13763 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013764 else
13765 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013766 item = list_find(l, before);
13767 if (item == NULL)
13768 {
13769 EMSGN(_(e_listidx), before);
13770 l = NULL;
13771 }
13772 }
13773 if (l != NULL)
13774 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013775 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013776 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013777 }
13778 }
13779}
13780
13781/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013782 * "invert(expr)" function
13783 */
13784 static void
13785f_invert(argvars, rettv)
13786 typval_T *argvars;
13787 typval_T *rettv;
13788{
13789 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13790}
13791
13792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793 * "isdirectory()" function
13794 */
13795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 typval_T *argvars;
13798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801}
13802
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013803/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013804 * "islocked()" function
13805 */
13806 static void
13807f_islocked(argvars, rettv)
13808 typval_T *argvars;
13809 typval_T *rettv;
13810{
13811 lval_T lv;
13812 char_u *end;
13813 dictitem_T *di;
13814
13815 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013816 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13817 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013818 if (end != NULL && lv.ll_name != NULL)
13819 {
13820 if (*end != NUL)
13821 EMSG(_(e_trailing));
13822 else
13823 {
13824 if (lv.ll_tv == NULL)
13825 {
13826 if (check_changedtick(lv.ll_name))
13827 rettv->vval.v_number = 1; /* always locked */
13828 else
13829 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013830 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013831 if (di != NULL)
13832 {
13833 /* Consider a variable locked when:
13834 * 1. the variable itself is locked
13835 * 2. the value of the variable is locked.
13836 * 3. the List or Dict value is locked.
13837 */
13838 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13839 || tv_islocked(&di->di_tv));
13840 }
13841 }
13842 }
13843 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013844 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013845 else if (lv.ll_newkey != NULL)
13846 EMSG2(_(e_dictkey), lv.ll_newkey);
13847 else if (lv.ll_list != NULL)
13848 /* List item. */
13849 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13850 else
13851 /* Dictionary item. */
13852 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13853 }
13854 }
13855
13856 clear_lval(&lv);
13857}
13858
Bram Moolenaar33570922005-01-25 22:26:29 +000013859static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013860
13861/*
13862 * Turn a dict into a list:
13863 * "what" == 0: list of keys
13864 * "what" == 1: list of values
13865 * "what" == 2: list of items
13866 */
13867 static void
13868dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013869 typval_T *argvars;
13870 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013871 int what;
13872{
Bram Moolenaar33570922005-01-25 22:26:29 +000013873 list_T *l2;
13874 dictitem_T *di;
13875 hashitem_T *hi;
13876 listitem_T *li;
13877 listitem_T *li2;
13878 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013879 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013880
Bram Moolenaar8c711452005-01-14 21:53:12 +000013881 if (argvars[0].v_type != VAR_DICT)
13882 {
13883 EMSG(_(e_dictreq));
13884 return;
13885 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013886 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013887 return;
13888
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013889 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013890 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013891
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013892 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013893 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013894 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013895 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013896 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013897 --todo;
13898 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013899
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013900 li = listitem_alloc();
13901 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013902 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013903 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013904
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013905 if (what == 0)
13906 {
13907 /* keys() */
13908 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013909 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013910 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13911 }
13912 else if (what == 1)
13913 {
13914 /* values() */
13915 copy_tv(&di->di_tv, &li->li_tv);
13916 }
13917 else
13918 {
13919 /* items() */
13920 l2 = list_alloc();
13921 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013922 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013923 li->li_tv.vval.v_list = l2;
13924 if (l2 == NULL)
13925 break;
13926 ++l2->lv_refcount;
13927
13928 li2 = listitem_alloc();
13929 if (li2 == NULL)
13930 break;
13931 list_append(l2, li2);
13932 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013933 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013934 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13935
13936 li2 = listitem_alloc();
13937 if (li2 == NULL)
13938 break;
13939 list_append(l2, li2);
13940 copy_tv(&di->di_tv, &li2->li_tv);
13941 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013942 }
13943 }
13944}
13945
13946/*
13947 * "items(dict)" function
13948 */
13949 static void
13950f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013951 typval_T *argvars;
13952 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013953{
13954 dict_list(argvars, rettv, 2);
13955}
13956
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013958 * "join()" function
13959 */
13960 static void
13961f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013962 typval_T *argvars;
13963 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013964{
13965 garray_T ga;
13966 char_u *sep;
13967
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013968 if (argvars[0].v_type != VAR_LIST)
13969 {
13970 EMSG(_(e_listreq));
13971 return;
13972 }
13973 if (argvars[0].vval.v_list == NULL)
13974 return;
13975 if (argvars[1].v_type == VAR_UNKNOWN)
13976 sep = (char_u *)" ";
13977 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013978 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013979
13980 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013981
13982 if (sep != NULL)
13983 {
13984 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013985 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986 ga_append(&ga, NUL);
13987 rettv->vval.v_string = (char_u *)ga.ga_data;
13988 }
13989 else
13990 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013991}
13992
13993/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013994 * "keys()" function
13995 */
13996 static void
13997f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013998 typval_T *argvars;
13999 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014000{
14001 dict_list(argvars, rettv, 0);
14002}
14003
14004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 * "last_buffer_nr()" function.
14006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014008f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014009 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011{
14012 int n = 0;
14013 buf_T *buf;
14014
14015 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14016 if (n < buf->b_fnum)
14017 n = buf->b_fnum;
14018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014020}
14021
14022/*
14023 * "len()" function
14024 */
14025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014026f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014027 typval_T *argvars;
14028 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014029{
14030 switch (argvars[0].v_type)
14031 {
14032 case VAR_STRING:
14033 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014034 rettv->vval.v_number = (varnumber_T)STRLEN(
14035 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014036 break;
14037 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014038 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014039 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014040 case VAR_DICT:
14041 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14042 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014043 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014044 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014045 break;
14046 }
14047}
14048
Bram Moolenaar33570922005-01-25 22:26:29 +000014049static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014050
14051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014052libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014053 typval_T *argvars;
14054 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014055 int type;
14056{
14057#ifdef FEAT_LIBCALL
14058 char_u *string_in;
14059 char_u **string_result;
14060 int nr_result;
14061#endif
14062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014063 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014064 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014065 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014066
14067 if (check_restricted() || check_secure())
14068 return;
14069
14070#ifdef FEAT_LIBCALL
14071 /* The first two args must be strings, otherwise its meaningless */
14072 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14073 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014074 string_in = NULL;
14075 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014076 string_in = argvars[2].vval.v_string;
14077 if (type == VAR_NUMBER)
14078 string_result = NULL;
14079 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014080 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014081 if (mch_libcall(argvars[0].vval.v_string,
14082 argvars[1].vval.v_string,
14083 string_in,
14084 argvars[2].vval.v_number,
14085 string_result,
14086 &nr_result) == OK
14087 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014088 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014089 }
14090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091}
14092
14093/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014094 * "libcall()" function
14095 */
14096 static void
14097f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014098 typval_T *argvars;
14099 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014100{
14101 libcall_common(argvars, rettv, VAR_STRING);
14102}
14103
14104/*
14105 * "libcallnr()" function
14106 */
14107 static void
14108f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014109 typval_T *argvars;
14110 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014111{
14112 libcall_common(argvars, rettv, VAR_NUMBER);
14113}
14114
14115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 * "line(string)" function
14117 */
14118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014119f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 typval_T *argvars;
14121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122{
14123 linenr_T lnum = 0;
14124 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014125 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014127 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 if (fp != NULL)
14129 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014130 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131}
14132
14133/*
14134 * "line2byte(lnum)" function
14135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014137f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014138 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140{
14141#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014142 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143#else
14144 linenr_T lnum;
14145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014146 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014148 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014150 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14151 if (rettv->vval.v_number >= 0)
14152 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153#endif
14154}
14155
14156/*
14157 * "lispindent(lnum)" function
14158 */
14159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014160f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014161 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163{
14164#ifdef FEAT_LISP
14165 pos_T pos;
14166 linenr_T lnum;
14167
14168 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014169 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14171 {
14172 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014173 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 curwin->w_cursor = pos;
14175 }
14176 else
14177#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014178 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179}
14180
14181/*
14182 * "localtime()" function
14183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014185f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014186 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014189 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190}
14191
Bram Moolenaar33570922005-01-25 22:26:29 +000014192static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193
14194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014195get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014196 typval_T *argvars;
14197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198 int exact;
14199{
14200 char_u *keys;
14201 char_u *which;
14202 char_u buf[NUMBUFLEN];
14203 char_u *keys_buf = NULL;
14204 char_u *rhs;
14205 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014206 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014207 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014208 mapblock_T *mp;
14209 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210
14211 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014212 rettv->v_type = VAR_STRING;
14213 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014215 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216 if (*keys == NUL)
14217 return;
14218
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014219 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014221 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014222 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014223 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014224 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014225 if (argvars[3].v_type != VAR_UNKNOWN)
14226 get_dict = get_tv_number(&argvars[3]);
14227 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 else
14230 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 if (which == NULL)
14232 return;
14233
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 mode = get_map_mode(&which, 0);
14235
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014236 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014237 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014239
14240 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014242 /* Return a string. */
14243 if (rhs != NULL)
14244 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245
Bram Moolenaarbd743252010-10-20 21:23:33 +020014246 }
14247 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14248 {
14249 /* Return a dictionary. */
14250 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14251 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14252 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253
Bram Moolenaarbd743252010-10-20 21:23:33 +020014254 dict_add_nr_str(dict, "lhs", 0L, lhs);
14255 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14256 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14257 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14258 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14259 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14260 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014261 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014262 dict_add_nr_str(dict, "mode", 0L, mapmode);
14263
14264 vim_free(lhs);
14265 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266 }
14267}
14268
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014269#ifdef FEAT_FLOAT
14270/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014271 * "log()" function
14272 */
14273 static void
14274f_log(argvars, rettv)
14275 typval_T *argvars;
14276 typval_T *rettv;
14277{
14278 float_T f;
14279
14280 rettv->v_type = VAR_FLOAT;
14281 if (get_float_arg(argvars, &f) == OK)
14282 rettv->vval.v_float = log(f);
14283 else
14284 rettv->vval.v_float = 0.0;
14285}
14286
14287/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014288 * "log10()" function
14289 */
14290 static void
14291f_log10(argvars, rettv)
14292 typval_T *argvars;
14293 typval_T *rettv;
14294{
14295 float_T f;
14296
14297 rettv->v_type = VAR_FLOAT;
14298 if (get_float_arg(argvars, &f) == OK)
14299 rettv->vval.v_float = log10(f);
14300 else
14301 rettv->vval.v_float = 0.0;
14302}
14303#endif
14304
Bram Moolenaar1dced572012-04-05 16:54:08 +020014305#ifdef FEAT_LUA
14306/*
14307 * "luaeval()" function
14308 */
14309 static void
14310f_luaeval(argvars, rettv)
14311 typval_T *argvars;
14312 typval_T *rettv;
14313{
14314 char_u *str;
14315 char_u buf[NUMBUFLEN];
14316
14317 str = get_tv_string_buf(&argvars[0], buf);
14318 do_luaeval(str, argvars + 1, rettv);
14319}
14320#endif
14321
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014323 * "map()" function
14324 */
14325 static void
14326f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014327 typval_T *argvars;
14328 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014329{
14330 filter_map(argvars, rettv, TRUE);
14331}
14332
14333/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014334 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335 */
14336 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014337f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014338 typval_T *argvars;
14339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014341 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342}
14343
14344/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014345 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 */
14347 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014348f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014349 typval_T *argvars;
14350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014352 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353}
14354
Bram Moolenaar33570922005-01-25 22:26:29 +000014355static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356
14357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014359 typval_T *argvars;
14360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 int type;
14362{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014363 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014364 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014365 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 char_u *pat;
14367 regmatch_T regmatch;
14368 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014369 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 char_u *save_cpo;
14371 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014372 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014373 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014374 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014375 list_T *l = NULL;
14376 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014377 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014378 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379
14380 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14381 save_cpo = p_cpo;
14382 p_cpo = (char_u *)"";
14383
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014384 rettv->vval.v_number = -1;
14385 if (type == 3)
14386 {
14387 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014388 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014389 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014390 }
14391 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 rettv->v_type = VAR_STRING;
14394 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014397 if (argvars[0].v_type == VAR_LIST)
14398 {
14399 if ((l = argvars[0].vval.v_list) == NULL)
14400 goto theend;
14401 li = l->lv_first;
14402 }
14403 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014404 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014405 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014406 len = (long)STRLEN(str);
14407 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14410 if (pat == NULL)
14411 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014412
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014413 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014415 int error = FALSE;
14416
14417 start = get_tv_number_chk(&argvars[2], &error);
14418 if (error)
14419 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014420 if (l != NULL)
14421 {
14422 li = list_find(l, start);
14423 if (li == NULL)
14424 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014425 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014426 }
14427 else
14428 {
14429 if (start < 0)
14430 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014431 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014432 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014433 /* When "count" argument is there ignore matches before "start",
14434 * otherwise skip part of the string. Differs when pattern is "^"
14435 * or "\<". */
14436 if (argvars[3].v_type != VAR_UNKNOWN)
14437 startcol = start;
14438 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014439 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014440 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014441 len -= start;
14442 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014443 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014444
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014445 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446 nth = get_tv_number_chk(&argvars[3], &error);
14447 if (error)
14448 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 }
14450
14451 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14452 if (regmatch.regprog != NULL)
14453 {
14454 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014455
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014456 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014457 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014458 if (l != NULL)
14459 {
14460 if (li == NULL)
14461 {
14462 match = FALSE;
14463 break;
14464 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014465 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014466 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014467 if (str == NULL)
14468 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014469 }
14470
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014471 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014472
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014473 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014474 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014475 if (l == NULL && !match)
14476 break;
14477
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014478 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014479 if (l != NULL)
14480 {
14481 li = li->li_next;
14482 ++idx;
14483 }
14484 else
14485 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014486#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014487 startcol = (colnr_T)(regmatch.startp[0]
14488 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014489#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014490 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014491#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014492 if (startcol > (colnr_T)len
14493 || str + startcol <= regmatch.startp[0])
14494 {
14495 match = FALSE;
14496 break;
14497 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014498 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014499 }
14500
14501 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014503 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014504 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014505 int i;
14506
14507 /* return list with matched string and submatches */
14508 for (i = 0; i < NSUBEXP; ++i)
14509 {
14510 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014511 {
14512 if (list_append_string(rettv->vval.v_list,
14513 (char_u *)"", 0) == FAIL)
14514 break;
14515 }
14516 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014517 regmatch.startp[i],
14518 (int)(regmatch.endp[i] - regmatch.startp[i]))
14519 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014520 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014521 }
14522 }
14523 else if (type == 2)
14524 {
14525 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014526 if (l != NULL)
14527 copy_tv(&li->li_tv, rettv);
14528 else
14529 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014531 }
14532 else if (l != NULL)
14533 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 else
14535 {
14536 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 (varnumber_T)(regmatch.startp[0] - str);
14539 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014540 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014542 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 }
14544 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014545 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 }
14547
14548theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014549 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 p_cpo = save_cpo;
14551}
14552
14553/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014554 * "match()" function
14555 */
14556 static void
14557f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014558 typval_T *argvars;
14559 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014560{
14561 find_some_match(argvars, rettv, 1);
14562}
14563
14564/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014565 * "matchadd()" function
14566 */
14567 static void
14568f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014569 typval_T *argvars UNUSED;
14570 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014571{
14572#ifdef FEAT_SEARCH_EXTRA
14573 char_u buf[NUMBUFLEN];
14574 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14575 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14576 int prio = 10; /* default priority */
14577 int id = -1;
14578 int error = FALSE;
14579
14580 rettv->vval.v_number = -1;
14581
14582 if (grp == NULL || pat == NULL)
14583 return;
14584 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014585 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014586 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014587 if (argvars[3].v_type != VAR_UNKNOWN)
14588 id = get_tv_number_chk(&argvars[3], &error);
14589 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014590 if (error == TRUE)
14591 return;
14592 if (id >= 1 && id <= 3)
14593 {
14594 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14595 return;
14596 }
14597
Bram Moolenaarb3414592014-06-17 17:48:32 +020014598 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14599#endif
14600}
14601
14602/*
14603 * "matchaddpos()" function
14604 */
14605 static void
14606f_matchaddpos(argvars, rettv)
14607 typval_T *argvars UNUSED;
14608 typval_T *rettv UNUSED;
14609{
14610#ifdef FEAT_SEARCH_EXTRA
14611 char_u buf[NUMBUFLEN];
14612 char_u *group;
14613 int prio = 10;
14614 int id = -1;
14615 int error = FALSE;
14616 list_T *l;
14617
14618 rettv->vval.v_number = -1;
14619
14620 group = get_tv_string_buf_chk(&argvars[0], buf);
14621 if (group == NULL)
14622 return;
14623
14624 if (argvars[1].v_type != VAR_LIST)
14625 {
14626 EMSG2(_(e_listarg), "matchaddpos()");
14627 return;
14628 }
14629 l = argvars[1].vval.v_list;
14630 if (l == NULL)
14631 return;
14632
14633 if (argvars[2].v_type != VAR_UNKNOWN)
14634 {
14635 prio = get_tv_number_chk(&argvars[2], &error);
14636 if (argvars[3].v_type != VAR_UNKNOWN)
14637 id = get_tv_number_chk(&argvars[3], &error);
14638 }
14639 if (error == TRUE)
14640 return;
14641
14642 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14643 if (id == 1 || id == 2)
14644 {
14645 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14646 return;
14647 }
14648
14649 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014650#endif
14651}
14652
14653/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014654 * "matcharg()" function
14655 */
14656 static void
14657f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014658 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014659 typval_T *rettv;
14660{
14661 if (rettv_list_alloc(rettv) == OK)
14662 {
14663#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014664 int id = get_tv_number(&argvars[0]);
14665 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014666
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014667 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014668 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014669 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14670 {
14671 list_append_string(rettv->vval.v_list,
14672 syn_id2name(m->hlg_id), -1);
14673 list_append_string(rettv->vval.v_list, m->pattern, -1);
14674 }
14675 else
14676 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014677 list_append_string(rettv->vval.v_list, NULL, -1);
14678 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014679 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014680 }
14681#endif
14682 }
14683}
14684
14685/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014686 * "matchdelete()" function
14687 */
14688 static void
14689f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014690 typval_T *argvars UNUSED;
14691 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014692{
14693#ifdef FEAT_SEARCH_EXTRA
14694 rettv->vval.v_number = match_delete(curwin,
14695 (int)get_tv_number(&argvars[0]), TRUE);
14696#endif
14697}
14698
14699/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014700 * "matchend()" function
14701 */
14702 static void
14703f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014704 typval_T *argvars;
14705 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014706{
14707 find_some_match(argvars, rettv, 0);
14708}
14709
14710/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014711 * "matchlist()" function
14712 */
14713 static void
14714f_matchlist(argvars, rettv)
14715 typval_T *argvars;
14716 typval_T *rettv;
14717{
14718 find_some_match(argvars, rettv, 3);
14719}
14720
14721/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014722 * "matchstr()" function
14723 */
14724 static void
14725f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014726 typval_T *argvars;
14727 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014728{
14729 find_some_match(argvars, rettv, 2);
14730}
14731
Bram Moolenaar33570922005-01-25 22:26:29 +000014732static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014733
14734 static void
14735max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014736 typval_T *argvars;
14737 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014738 int domax;
14739{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014740 long n = 0;
14741 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014742 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014743
14744 if (argvars[0].v_type == VAR_LIST)
14745 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014746 list_T *l;
14747 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014748
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014749 l = argvars[0].vval.v_list;
14750 if (l != NULL)
14751 {
14752 li = l->lv_first;
14753 if (li != NULL)
14754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014755 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014756 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014757 {
14758 li = li->li_next;
14759 if (li == NULL)
14760 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014761 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014762 if (domax ? i > n : i < n)
14763 n = i;
14764 }
14765 }
14766 }
14767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014768 else if (argvars[0].v_type == VAR_DICT)
14769 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014770 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014771 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014772 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014773 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014774
14775 d = argvars[0].vval.v_dict;
14776 if (d != NULL)
14777 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014778 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014779 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014780 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014781 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014782 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014783 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014784 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014785 if (first)
14786 {
14787 n = i;
14788 first = FALSE;
14789 }
14790 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014791 n = i;
14792 }
14793 }
14794 }
14795 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014796 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014797 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014798 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014799}
14800
14801/*
14802 * "max()" function
14803 */
14804 static void
14805f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014806 typval_T *argvars;
14807 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014808{
14809 max_min(argvars, rettv, TRUE);
14810}
14811
14812/*
14813 * "min()" function
14814 */
14815 static void
14816f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014817 typval_T *argvars;
14818 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014819{
14820 max_min(argvars, rettv, FALSE);
14821}
14822
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014823static int mkdir_recurse __ARGS((char_u *dir, int prot));
14824
14825/*
14826 * Create the directory in which "dir" is located, and higher levels when
14827 * needed.
14828 */
14829 static int
14830mkdir_recurse(dir, prot)
14831 char_u *dir;
14832 int prot;
14833{
14834 char_u *p;
14835 char_u *updir;
14836 int r = FAIL;
14837
14838 /* Get end of directory name in "dir".
14839 * We're done when it's "/" or "c:/". */
14840 p = gettail_sep(dir);
14841 if (p <= get_past_head(dir))
14842 return OK;
14843
14844 /* If the directory exists we're done. Otherwise: create it.*/
14845 updir = vim_strnsave(dir, (int)(p - dir));
14846 if (updir == NULL)
14847 return FAIL;
14848 if (mch_isdir(updir))
14849 r = OK;
14850 else if (mkdir_recurse(updir, prot) == OK)
14851 r = vim_mkdir_emsg(updir, prot);
14852 vim_free(updir);
14853 return r;
14854}
14855
14856#ifdef vim_mkdir
14857/*
14858 * "mkdir()" function
14859 */
14860 static void
14861f_mkdir(argvars, rettv)
14862 typval_T *argvars;
14863 typval_T *rettv;
14864{
14865 char_u *dir;
14866 char_u buf[NUMBUFLEN];
14867 int prot = 0755;
14868
14869 rettv->vval.v_number = FAIL;
14870 if (check_restricted() || check_secure())
14871 return;
14872
14873 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014874 if (*dir == NUL)
14875 rettv->vval.v_number = FAIL;
14876 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014877 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014878 if (*gettail(dir) == NUL)
14879 /* remove trailing slashes */
14880 *gettail_sep(dir) = NUL;
14881
14882 if (argvars[1].v_type != VAR_UNKNOWN)
14883 {
14884 if (argvars[2].v_type != VAR_UNKNOWN)
14885 prot = get_tv_number_chk(&argvars[2], NULL);
14886 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14887 mkdir_recurse(dir, prot);
14888 }
14889 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014890 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014891}
14892#endif
14893
Bram Moolenaar0d660222005-01-07 21:51:51 +000014894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 * "mode()" function
14896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014898f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014899 typval_T *argvars;
14900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014902 char_u buf[3];
14903
14904 buf[1] = NUL;
14905 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907 if (VIsual_active)
14908 {
14909 if (VIsual_select)
14910 buf[0] = VIsual_mode + 's' - 'v';
14911 else
14912 buf[0] = VIsual_mode;
14913 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014914 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014915 || State == CONFIRM)
14916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014918 if (State == ASKMORE)
14919 buf[1] = 'm';
14920 else if (State == CONFIRM)
14921 buf[1] = '?';
14922 }
14923 else if (State == EXTERNCMD)
14924 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925 else if (State & INSERT)
14926 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014927#ifdef FEAT_VREPLACE
14928 if (State & VREPLACE_FLAG)
14929 {
14930 buf[0] = 'R';
14931 buf[1] = 'v';
14932 }
14933 else
14934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 if (State & REPLACE_FLAG)
14936 buf[0] = 'R';
14937 else
14938 buf[0] = 'i';
14939 }
14940 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014943 if (exmode_active)
14944 buf[1] = 'v';
14945 }
14946 else if (exmode_active)
14947 {
14948 buf[0] = 'c';
14949 buf[1] = 'e';
14950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014954 if (finish_op)
14955 buf[1] = 'o';
14956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014958 /* Clear out the minor mode when the argument is not a non-zero number or
14959 * non-empty string. */
14960 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014961 buf[1] = NUL;
14962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014963 rettv->vval.v_string = vim_strsave(buf);
14964 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965}
14966
Bram Moolenaar429fa852013-04-15 12:27:36 +020014967#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014968/*
14969 * "mzeval()" function
14970 */
14971 static void
14972f_mzeval(argvars, rettv)
14973 typval_T *argvars;
14974 typval_T *rettv;
14975{
14976 char_u *str;
14977 char_u buf[NUMBUFLEN];
14978
14979 str = get_tv_string_buf(&argvars[0], buf);
14980 do_mzeval(str, rettv);
14981}
Bram Moolenaar75676462013-01-30 14:55:42 +010014982
14983 void
14984mzscheme_call_vim(name, args, rettv)
14985 char_u *name;
14986 typval_T *args;
14987 typval_T *rettv;
14988{
14989 typval_T argvars[3];
14990
14991 argvars[0].v_type = VAR_STRING;
14992 argvars[0].vval.v_string = name;
14993 copy_tv(args, &argvars[1]);
14994 argvars[2].v_type = VAR_UNKNOWN;
14995 f_call(argvars, rettv);
14996 clear_tv(&argvars[1]);
14997}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014998#endif
14999
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015001 * "nextnonblank()" function
15002 */
15003 static void
15004f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015005 typval_T *argvars;
15006 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015007{
15008 linenr_T lnum;
15009
15010 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15011 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015012 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015013 {
15014 lnum = 0;
15015 break;
15016 }
15017 if (*skipwhite(ml_get(lnum)) != NUL)
15018 break;
15019 }
15020 rettv->vval.v_number = lnum;
15021}
15022
15023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 * "nr2char()" function
15025 */
15026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015027f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015028 typval_T *argvars;
15029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030{
15031 char_u buf[NUMBUFLEN];
15032
15033#ifdef FEAT_MBYTE
15034 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015035 {
15036 int utf8 = 0;
15037
15038 if (argvars[1].v_type != VAR_UNKNOWN)
15039 utf8 = get_tv_number_chk(&argvars[1], NULL);
15040 if (utf8)
15041 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15042 else
15043 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 else
15046#endif
15047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015048 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049 buf[1] = NUL;
15050 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015051 rettv->v_type = VAR_STRING;
15052 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015053}
15054
15055/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015056 * "or(expr, expr)" function
15057 */
15058 static void
15059f_or(argvars, rettv)
15060 typval_T *argvars;
15061 typval_T *rettv;
15062{
15063 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15064 | get_tv_number_chk(&argvars[1], NULL);
15065}
15066
15067/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015068 * "pathshorten()" function
15069 */
15070 static void
15071f_pathshorten(argvars, rettv)
15072 typval_T *argvars;
15073 typval_T *rettv;
15074{
15075 char_u *p;
15076
15077 rettv->v_type = VAR_STRING;
15078 p = get_tv_string_chk(&argvars[0]);
15079 if (p == NULL)
15080 rettv->vval.v_string = NULL;
15081 else
15082 {
15083 p = vim_strsave(p);
15084 rettv->vval.v_string = p;
15085 if (p != NULL)
15086 shorten_dir(p);
15087 }
15088}
15089
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015090#ifdef FEAT_FLOAT
15091/*
15092 * "pow()" function
15093 */
15094 static void
15095f_pow(argvars, rettv)
15096 typval_T *argvars;
15097 typval_T *rettv;
15098{
15099 float_T fx, fy;
15100
15101 rettv->v_type = VAR_FLOAT;
15102 if (get_float_arg(argvars, &fx) == OK
15103 && get_float_arg(&argvars[1], &fy) == OK)
15104 rettv->vval.v_float = pow(fx, fy);
15105 else
15106 rettv->vval.v_float = 0.0;
15107}
15108#endif
15109
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015110/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111 * "prevnonblank()" function
15112 */
15113 static void
15114f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015115 typval_T *argvars;
15116 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015117{
15118 linenr_T lnum;
15119
15120 lnum = get_tv_lnum(argvars);
15121 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15122 lnum = 0;
15123 else
15124 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15125 --lnum;
15126 rettv->vval.v_number = lnum;
15127}
15128
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015129#ifdef HAVE_STDARG_H
15130/* This dummy va_list is here because:
15131 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15132 * - locally in the function results in a "used before set" warning
15133 * - using va_start() to initialize it gives "function with fixed args" error */
15134static va_list ap;
15135#endif
15136
Bram Moolenaar8c711452005-01-14 21:53:12 +000015137/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015138 * "printf()" function
15139 */
15140 static void
15141f_printf(argvars, rettv)
15142 typval_T *argvars;
15143 typval_T *rettv;
15144{
15145 rettv->v_type = VAR_STRING;
15146 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015147#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015148 {
15149 char_u buf[NUMBUFLEN];
15150 int len;
15151 char_u *s;
15152 int saved_did_emsg = did_emsg;
15153 char *fmt;
15154
15155 /* Get the required length, allocate the buffer and do it for real. */
15156 did_emsg = FALSE;
15157 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015158 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015159 if (!did_emsg)
15160 {
15161 s = alloc(len + 1);
15162 if (s != NULL)
15163 {
15164 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015165 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015166 }
15167 }
15168 did_emsg |= saved_did_emsg;
15169 }
15170#endif
15171}
15172
15173/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015174 * "pumvisible()" function
15175 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015176 static void
15177f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015178 typval_T *argvars UNUSED;
15179 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015180{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015181#ifdef FEAT_INS_EXPAND
15182 if (pum_visible())
15183 rettv->vval.v_number = 1;
15184#endif
15185}
15186
Bram Moolenaardb913952012-06-29 12:54:53 +020015187#ifdef FEAT_PYTHON3
15188/*
15189 * "py3eval()" function
15190 */
15191 static void
15192f_py3eval(argvars, rettv)
15193 typval_T *argvars;
15194 typval_T *rettv;
15195{
15196 char_u *str;
15197 char_u buf[NUMBUFLEN];
15198
15199 str = get_tv_string_buf(&argvars[0], buf);
15200 do_py3eval(str, rettv);
15201}
15202#endif
15203
15204#ifdef FEAT_PYTHON
15205/*
15206 * "pyeval()" function
15207 */
15208 static void
15209f_pyeval(argvars, rettv)
15210 typval_T *argvars;
15211 typval_T *rettv;
15212{
15213 char_u *str;
15214 char_u buf[NUMBUFLEN];
15215
15216 str = get_tv_string_buf(&argvars[0], buf);
15217 do_pyeval(str, rettv);
15218}
15219#endif
15220
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015221/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015222 * "range()" function
15223 */
15224 static void
15225f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015226 typval_T *argvars;
15227 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015228{
15229 long start;
15230 long end;
15231 long stride = 1;
15232 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015233 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015235 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015236 if (argvars[1].v_type == VAR_UNKNOWN)
15237 {
15238 end = start - 1;
15239 start = 0;
15240 }
15241 else
15242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015243 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015244 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015245 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015246 }
15247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015248 if (error)
15249 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015250 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015251 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015252 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015253 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015254 else
15255 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015256 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015257 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015258 if (list_append_number(rettv->vval.v_list,
15259 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015260 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015261 }
15262}
15263
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015264/*
15265 * "readfile()" function
15266 */
15267 static void
15268f_readfile(argvars, rettv)
15269 typval_T *argvars;
15270 typval_T *rettv;
15271{
15272 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015273 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015274 char_u *fname;
15275 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015276 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15277 int io_size = sizeof(buf);
15278 int readlen; /* size of last fread() */
15279 char_u *prev = NULL; /* previously read bytes, if any */
15280 long prevlen = 0; /* length of data in prev */
15281 long prevsize = 0; /* size of prev buffer */
15282 long maxline = MAXLNUM;
15283 long cnt = 0;
15284 char_u *p; /* position in buf */
15285 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015286
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015287 if (argvars[1].v_type != VAR_UNKNOWN)
15288 {
15289 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15290 binary = TRUE;
15291 if (argvars[2].v_type != VAR_UNKNOWN)
15292 maxline = get_tv_number(&argvars[2]);
15293 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015294
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015295 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015296 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015297
15298 /* Always open the file in binary mode, library functions have a mind of
15299 * their own about CR-LF conversion. */
15300 fname = get_tv_string(&argvars[0]);
15301 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15302 {
15303 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15304 return;
15305 }
15306
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015307 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015308 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015309 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015310
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015311 /* This for loop processes what was read, but is also entered at end
15312 * of file so that either:
15313 * - an incomplete line gets written
15314 * - a "binary" file gets an empty line at the end if it ends in a
15315 * newline. */
15316 for (p = buf, start = buf;
15317 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15318 ++p)
15319 {
15320 if (*p == '\n' || readlen <= 0)
15321 {
15322 listitem_T *li;
15323 char_u *s = NULL;
15324 long_u len = p - start;
15325
15326 /* Finished a line. Remove CRs before NL. */
15327 if (readlen > 0 && !binary)
15328 {
15329 while (len > 0 && start[len - 1] == '\r')
15330 --len;
15331 /* removal may cross back to the "prev" string */
15332 if (len == 0)
15333 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15334 --prevlen;
15335 }
15336 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015337 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015338 else
15339 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015340 /* Change "prev" buffer to be the right size. This way
15341 * the bytes are only copied once, and very long lines are
15342 * allocated only once. */
15343 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015344 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015345 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015346 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015347 prev = NULL; /* the list will own the string */
15348 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015349 }
15350 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015351 if (s == NULL)
15352 {
15353 do_outofmem_msg((long_u) prevlen + len + 1);
15354 failed = TRUE;
15355 break;
15356 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015357
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015358 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015359 {
15360 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015361 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015362 break;
15363 }
15364 li->li_tv.v_type = VAR_STRING;
15365 li->li_tv.v_lock = 0;
15366 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015367 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015368
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015369 start = p + 1; /* step over newline */
15370 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015371 break;
15372 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015373 else if (*p == NUL)
15374 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015375#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015376 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15377 * when finding the BF and check the previous two bytes. */
15378 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015379 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015380 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15381 * + 1, these may be in the "prev" string. */
15382 char_u back1 = p >= buf + 1 ? p[-1]
15383 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15384 char_u back2 = p >= buf + 2 ? p[-2]
15385 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15386 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015387
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015388 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015389 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015390 char_u *dest = p - 2;
15391
15392 /* Usually a BOM is at the beginning of a file, and so at
15393 * the beginning of a line; then we can just step over it.
15394 */
15395 if (start == dest)
15396 start = p + 1;
15397 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015398 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015399 /* have to shuffle buf to close gap */
15400 int adjust_prevlen = 0;
15401
15402 if (dest < buf)
15403 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015404 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015405 dest = buf;
15406 }
15407 if (readlen > p - buf + 1)
15408 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15409 readlen -= 3 - adjust_prevlen;
15410 prevlen -= adjust_prevlen;
15411 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015412 }
15413 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015414 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015415#endif
15416 } /* for */
15417
15418 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15419 break;
15420 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015421 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015422 /* There's part of a line in buf, store it in "prev". */
15423 if (p - start + prevlen >= prevsize)
15424 {
15425 /* need bigger "prev" buffer */
15426 char_u *newprev;
15427
15428 /* A common use case is ordinary text files and "prev" gets a
15429 * fragment of a line, so the first allocation is made
15430 * small, to avoid repeatedly 'allocing' large and
15431 * 'reallocing' small. */
15432 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015433 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015434 else
15435 {
15436 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015437 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015438 prevsize = grow50pc > growmin ? grow50pc : growmin;
15439 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015440 newprev = prev == NULL ? alloc(prevsize)
15441 : vim_realloc(prev, prevsize);
15442 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015443 {
15444 do_outofmem_msg((long_u)prevsize);
15445 failed = TRUE;
15446 break;
15447 }
15448 prev = newprev;
15449 }
15450 /* Add the line part to end of "prev". */
15451 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015452 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015453 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015454 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015455
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015456 /*
15457 * For a negative line count use only the lines at the end of the file,
15458 * free the rest.
15459 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015460 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015461 while (cnt > -maxline)
15462 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015463 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015464 --cnt;
15465 }
15466
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015467 if (failed)
15468 {
15469 list_free(rettv->vval.v_list, TRUE);
15470 /* readfile doc says an empty list is returned on error */
15471 rettv->vval.v_list = list_alloc();
15472 }
15473
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015474 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015475 fclose(fd);
15476}
15477
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015478#if defined(FEAT_RELTIME)
15479static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15480
15481/*
15482 * Convert a List to proftime_T.
15483 * Return FAIL when there is something wrong.
15484 */
15485 static int
15486list2proftime(arg, tm)
15487 typval_T *arg;
15488 proftime_T *tm;
15489{
15490 long n1, n2;
15491 int error = FALSE;
15492
15493 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15494 || arg->vval.v_list->lv_len != 2)
15495 return FAIL;
15496 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15497 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15498# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015499 tm->HighPart = n1;
15500 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015501# else
15502 tm->tv_sec = n1;
15503 tm->tv_usec = n2;
15504# endif
15505 return error ? FAIL : OK;
15506}
15507#endif /* FEAT_RELTIME */
15508
15509/*
15510 * "reltime()" function
15511 */
15512 static void
15513f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015514 typval_T *argvars UNUSED;
15515 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015516{
15517#ifdef FEAT_RELTIME
15518 proftime_T res;
15519 proftime_T start;
15520
15521 if (argvars[0].v_type == VAR_UNKNOWN)
15522 {
15523 /* No arguments: get current time. */
15524 profile_start(&res);
15525 }
15526 else if (argvars[1].v_type == VAR_UNKNOWN)
15527 {
15528 if (list2proftime(&argvars[0], &res) == FAIL)
15529 return;
15530 profile_end(&res);
15531 }
15532 else
15533 {
15534 /* Two arguments: compute the difference. */
15535 if (list2proftime(&argvars[0], &start) == FAIL
15536 || list2proftime(&argvars[1], &res) == FAIL)
15537 return;
15538 profile_sub(&res, &start);
15539 }
15540
15541 if (rettv_list_alloc(rettv) == OK)
15542 {
15543 long n1, n2;
15544
15545# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015546 n1 = res.HighPart;
15547 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015548# else
15549 n1 = res.tv_sec;
15550 n2 = res.tv_usec;
15551# endif
15552 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15553 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15554 }
15555#endif
15556}
15557
15558/*
15559 * "reltimestr()" function
15560 */
15561 static void
15562f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015563 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015564 typval_T *rettv;
15565{
15566#ifdef FEAT_RELTIME
15567 proftime_T tm;
15568#endif
15569
15570 rettv->v_type = VAR_STRING;
15571 rettv->vval.v_string = NULL;
15572#ifdef FEAT_RELTIME
15573 if (list2proftime(&argvars[0], &tm) == OK)
15574 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15575#endif
15576}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015577
Bram Moolenaar0d660222005-01-07 21:51:51 +000015578#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15579static void make_connection __ARGS((void));
15580static int check_connection __ARGS((void));
15581
15582 static void
15583make_connection()
15584{
15585 if (X_DISPLAY == NULL
15586# ifdef FEAT_GUI
15587 && !gui.in_use
15588# endif
15589 )
15590 {
15591 x_force_connect = TRUE;
15592 setup_term_clip();
15593 x_force_connect = FALSE;
15594 }
15595}
15596
15597 static int
15598check_connection()
15599{
15600 make_connection();
15601 if (X_DISPLAY == NULL)
15602 {
15603 EMSG(_("E240: No connection to Vim server"));
15604 return FAIL;
15605 }
15606 return OK;
15607}
15608#endif
15609
15610#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015611static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015612
15613 static void
15614remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015615 typval_T *argvars;
15616 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015617 int expr;
15618{
15619 char_u *server_name;
15620 char_u *keys;
15621 char_u *r = NULL;
15622 char_u buf[NUMBUFLEN];
15623# ifdef WIN32
15624 HWND w;
15625# else
15626 Window w;
15627# endif
15628
15629 if (check_restricted() || check_secure())
15630 return;
15631
15632# ifdef FEAT_X11
15633 if (check_connection() == FAIL)
15634 return;
15635# endif
15636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 server_name = get_tv_string_chk(&argvars[0]);
15638 if (server_name == NULL)
15639 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640 keys = get_tv_string_buf(&argvars[1], buf);
15641# ifdef WIN32
15642 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15643# else
15644 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15645 < 0)
15646# endif
15647 {
15648 if (r != NULL)
15649 EMSG(r); /* sending worked but evaluation failed */
15650 else
15651 EMSG2(_("E241: Unable to send to %s"), server_name);
15652 return;
15653 }
15654
15655 rettv->vval.v_string = r;
15656
15657 if (argvars[2].v_type != VAR_UNKNOWN)
15658 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015659 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015660 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015661 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015662
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015663 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015664 v.di_tv.v_type = VAR_STRING;
15665 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015666 idvar = get_tv_string_chk(&argvars[2]);
15667 if (idvar != NULL)
15668 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015669 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015670 }
15671}
15672#endif
15673
15674/*
15675 * "remote_expr()" function
15676 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677 static void
15678f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015679 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015680 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015681{
15682 rettv->v_type = VAR_STRING;
15683 rettv->vval.v_string = NULL;
15684#ifdef FEAT_CLIENTSERVER
15685 remote_common(argvars, rettv, TRUE);
15686#endif
15687}
15688
15689/*
15690 * "remote_foreground()" function
15691 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015692 static void
15693f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015694 typval_T *argvars UNUSED;
15695 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015696{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015697#ifdef FEAT_CLIENTSERVER
15698# ifdef WIN32
15699 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015700 {
15701 char_u *server_name = get_tv_string_chk(&argvars[0]);
15702
15703 if (server_name != NULL)
15704 serverForeground(server_name);
15705 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015706# else
15707 /* Send a foreground() expression to the server. */
15708 argvars[1].v_type = VAR_STRING;
15709 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15710 argvars[2].v_type = VAR_UNKNOWN;
15711 remote_common(argvars, rettv, TRUE);
15712 vim_free(argvars[1].vval.v_string);
15713# endif
15714#endif
15715}
15716
Bram Moolenaar0d660222005-01-07 21:51:51 +000015717 static void
15718f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015719 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015720 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015721{
15722#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015723 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015724 char_u *s = NULL;
15725# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015726 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015727# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015728 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015729
15730 if (check_restricted() || check_secure())
15731 {
15732 rettv->vval.v_number = -1;
15733 return;
15734 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015735 serverid = get_tv_string_chk(&argvars[0]);
15736 if (serverid == NULL)
15737 {
15738 rettv->vval.v_number = -1;
15739 return; /* type error; errmsg already given */
15740 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015741# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015742 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743 if (n == 0)
15744 rettv->vval.v_number = -1;
15745 else
15746 {
15747 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15748 rettv->vval.v_number = (s != NULL);
15749 }
15750# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751 if (check_connection() == FAIL)
15752 return;
15753
15754 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015755 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015756# endif
15757
15758 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015760 char_u *retvar;
15761
Bram Moolenaar33570922005-01-25 22:26:29 +000015762 v.di_tv.v_type = VAR_STRING;
15763 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015764 retvar = get_tv_string_chk(&argvars[1]);
15765 if (retvar != NULL)
15766 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015767 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015768 }
15769#else
15770 rettv->vval.v_number = -1;
15771#endif
15772}
15773
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774 static void
15775f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015776 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015777 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015778{
15779 char_u *r = NULL;
15780
15781#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015782 char_u *serverid = get_tv_string_chk(&argvars[0]);
15783
15784 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015785 {
15786# ifdef WIN32
15787 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015788 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015789
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015790 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015791 if (n != 0)
15792 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15793 if (r == NULL)
15794# else
15795 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015796 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015797# endif
15798 EMSG(_("E277: Unable to read a server reply"));
15799 }
15800#endif
15801 rettv->v_type = VAR_STRING;
15802 rettv->vval.v_string = r;
15803}
15804
15805/*
15806 * "remote_send()" function
15807 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015808 static void
15809f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015810 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015811 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015812{
15813 rettv->v_type = VAR_STRING;
15814 rettv->vval.v_string = NULL;
15815#ifdef FEAT_CLIENTSERVER
15816 remote_common(argvars, rettv, FALSE);
15817#endif
15818}
15819
15820/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015821 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015822 */
15823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015824f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015825 typval_T *argvars;
15826 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015827{
Bram Moolenaar33570922005-01-25 22:26:29 +000015828 list_T *l;
15829 listitem_T *item, *item2;
15830 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015831 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015832 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015833 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015834 dict_T *d;
15835 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015836 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015837
Bram Moolenaar8c711452005-01-14 21:53:12 +000015838 if (argvars[0].v_type == VAR_DICT)
15839 {
15840 if (argvars[2].v_type != VAR_UNKNOWN)
15841 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015842 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015843 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015845 key = get_tv_string_chk(&argvars[1]);
15846 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015848 di = dict_find(d, key, -1);
15849 if (di == NULL)
15850 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015851 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15852 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015853 {
15854 *rettv = di->di_tv;
15855 init_tv(&di->di_tv);
15856 dictitem_remove(d, di);
15857 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015858 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015859 }
15860 }
15861 else if (argvars[0].v_type != VAR_LIST)
15862 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015863 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015864 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015866 int error = FALSE;
15867
15868 idx = get_tv_number_chk(&argvars[1], &error);
15869 if (error)
15870 ; /* type error: do nothing, errmsg already given */
15871 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015872 EMSGN(_(e_listidx), idx);
15873 else
15874 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015875 if (argvars[2].v_type == VAR_UNKNOWN)
15876 {
15877 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015878 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015879 *rettv = item->li_tv;
15880 vim_free(item);
15881 }
15882 else
15883 {
15884 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015885 end = get_tv_number_chk(&argvars[2], &error);
15886 if (error)
15887 ; /* type error: do nothing */
15888 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015889 EMSGN(_(e_listidx), end);
15890 else
15891 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015892 int cnt = 0;
15893
15894 for (li = item; li != NULL; li = li->li_next)
15895 {
15896 ++cnt;
15897 if (li == item2)
15898 break;
15899 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015900 if (li == NULL) /* didn't find "item2" after "item" */
15901 EMSG(_(e_invrange));
15902 else
15903 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015904 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015905 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015906 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015907 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015908 l->lv_first = item;
15909 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015910 item->li_prev = NULL;
15911 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015912 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015913 }
15914 }
15915 }
15916 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015917 }
15918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919}
15920
15921/*
15922 * "rename({from}, {to})" function
15923 */
15924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015925f_rename(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 buf[NUMBUFLEN];
15930
15931 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015932 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015934 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15935 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936}
15937
15938/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015939 * "repeat()" function
15940 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015941 static void
15942f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015943 typval_T *argvars;
15944 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015945{
15946 char_u *p;
15947 int n;
15948 int slen;
15949 int len;
15950 char_u *r;
15951 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015952
15953 n = get_tv_number(&argvars[1]);
15954 if (argvars[0].v_type == VAR_LIST)
15955 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015956 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015957 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015958 if (list_extend(rettv->vval.v_list,
15959 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015960 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015961 }
15962 else
15963 {
15964 p = get_tv_string(&argvars[0]);
15965 rettv->v_type = VAR_STRING;
15966 rettv->vval.v_string = NULL;
15967
15968 slen = (int)STRLEN(p);
15969 len = slen * n;
15970 if (len <= 0)
15971 return;
15972
15973 r = alloc(len + 1);
15974 if (r != NULL)
15975 {
15976 for (i = 0; i < n; i++)
15977 mch_memmove(r + i * slen, p, (size_t)slen);
15978 r[len] = NUL;
15979 }
15980
15981 rettv->vval.v_string = r;
15982 }
15983}
15984
15985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986 * "resolve()" function
15987 */
15988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015989f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015990 typval_T *argvars;
15991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992{
15993 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015994#ifdef HAVE_READLINK
15995 char_u *buf = NULL;
15996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015998 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999#ifdef FEAT_SHORTCUT
16000 {
16001 char_u *v = NULL;
16002
16003 v = mch_resolve_shortcut(p);
16004 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016005 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016007 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008 }
16009#else
16010# ifdef HAVE_READLINK
16011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 char_u *cpy;
16013 int len;
16014 char_u *remain = NULL;
16015 char_u *q;
16016 int is_relative_to_current = FALSE;
16017 int has_trailing_pathsep = FALSE;
16018 int limit = 100;
16019
16020 p = vim_strsave(p);
16021
16022 if (p[0] == '.' && (vim_ispathsep(p[1])
16023 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16024 is_relative_to_current = TRUE;
16025
16026 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016027 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016030 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032
16033 q = getnextcomp(p);
16034 if (*q != NUL)
16035 {
16036 /* Separate the first path component in "p", and keep the
16037 * remainder (beginning with the path separator). */
16038 remain = vim_strsave(q - 1);
16039 q[-1] = NUL;
16040 }
16041
Bram Moolenaard9462e32011-04-11 21:35:11 +020016042 buf = alloc(MAXPATHL + 1);
16043 if (buf == NULL)
16044 goto fail;
16045
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046 for (;;)
16047 {
16048 for (;;)
16049 {
16050 len = readlink((char *)p, (char *)buf, MAXPATHL);
16051 if (len <= 0)
16052 break;
16053 buf[len] = NUL;
16054
16055 if (limit-- == 0)
16056 {
16057 vim_free(p);
16058 vim_free(remain);
16059 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016060 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 goto fail;
16062 }
16063
16064 /* Ensure that the result will have a trailing path separator
16065 * if the argument has one. */
16066 if (remain == NULL && has_trailing_pathsep)
16067 add_pathsep(buf);
16068
16069 /* Separate the first path component in the link value and
16070 * concatenate the remainders. */
16071 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16072 if (*q != NUL)
16073 {
16074 if (remain == NULL)
16075 remain = vim_strsave(q - 1);
16076 else
16077 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016078 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079 if (cpy != NULL)
16080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 vim_free(remain);
16082 remain = cpy;
16083 }
16084 }
16085 q[-1] = NUL;
16086 }
16087
16088 q = gettail(p);
16089 if (q > p && *q == NUL)
16090 {
16091 /* Ignore trailing path separator. */
16092 q[-1] = NUL;
16093 q = gettail(p);
16094 }
16095 if (q > p && !mch_isFullName(buf))
16096 {
16097 /* symlink is relative to directory of argument */
16098 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16099 if (cpy != NULL)
16100 {
16101 STRCPY(cpy, p);
16102 STRCPY(gettail(cpy), buf);
16103 vim_free(p);
16104 p = cpy;
16105 }
16106 }
16107 else
16108 {
16109 vim_free(p);
16110 p = vim_strsave(buf);
16111 }
16112 }
16113
16114 if (remain == NULL)
16115 break;
16116
16117 /* Append the first path component of "remain" to "p". */
16118 q = getnextcomp(remain + 1);
16119 len = q - remain - (*q != NUL);
16120 cpy = vim_strnsave(p, STRLEN(p) + len);
16121 if (cpy != NULL)
16122 {
16123 STRNCAT(cpy, remain, len);
16124 vim_free(p);
16125 p = cpy;
16126 }
16127 /* Shorten "remain". */
16128 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016129 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130 else
16131 {
16132 vim_free(remain);
16133 remain = NULL;
16134 }
16135 }
16136
16137 /* If the result is a relative path name, make it explicitly relative to
16138 * the current directory if and only if the argument had this form. */
16139 if (!vim_ispathsep(*p))
16140 {
16141 if (is_relative_to_current
16142 && *p != NUL
16143 && !(p[0] == '.'
16144 && (p[1] == NUL
16145 || vim_ispathsep(p[1])
16146 || (p[1] == '.'
16147 && (p[2] == NUL
16148 || vim_ispathsep(p[2]))))))
16149 {
16150 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016151 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152 if (cpy != NULL)
16153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 vim_free(p);
16155 p = cpy;
16156 }
16157 }
16158 else if (!is_relative_to_current)
16159 {
16160 /* Strip leading "./". */
16161 q = p;
16162 while (q[0] == '.' && vim_ispathsep(q[1]))
16163 q += 2;
16164 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016165 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 }
16167 }
16168
16169 /* Ensure that the result will have no trailing path separator
16170 * if the argument had none. But keep "/" or "//". */
16171 if (!has_trailing_pathsep)
16172 {
16173 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016174 if (after_pathsep(p, q))
16175 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 }
16177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016178 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179 }
16180# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016181 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182# endif
16183#endif
16184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016185 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186
16187#ifdef HAVE_READLINK
16188fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016189 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016191 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192}
16193
16194/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016195 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 */
16197 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016199 typval_T *argvars;
16200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201{
Bram Moolenaar33570922005-01-25 22:26:29 +000016202 list_T *l;
16203 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205 if (argvars[0].v_type != VAR_LIST)
16206 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016207 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016208 && !tv_check_lock(l->lv_lock,
16209 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016210 {
16211 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016212 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016213 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016214 while (li != NULL)
16215 {
16216 ni = li->li_prev;
16217 list_append(l, li);
16218 li = ni;
16219 }
16220 rettv->vval.v_list = l;
16221 rettv->v_type = VAR_LIST;
16222 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016223 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225}
16226
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016227#define SP_NOMOVE 0x01 /* don't move cursor */
16228#define SP_REPEAT 0x02 /* repeat to find outer pair */
16229#define SP_RETCOUNT 0x04 /* return matchcount */
16230#define SP_SETPCMARK 0x08 /* set previous context mark */
16231#define SP_START 0x10 /* accept match at start position */
16232#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16233#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016234
Bram Moolenaar33570922005-01-25 22:26:29 +000016235static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016236
16237/*
16238 * Get flags for a search function.
16239 * Possibly sets "p_ws".
16240 * Returns BACKWARD, FORWARD or zero (for an error).
16241 */
16242 static int
16243get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016244 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016245 int *flagsp;
16246{
16247 int dir = FORWARD;
16248 char_u *flags;
16249 char_u nbuf[NUMBUFLEN];
16250 int mask;
16251
16252 if (varp->v_type != VAR_UNKNOWN)
16253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016254 flags = get_tv_string_buf_chk(varp, nbuf);
16255 if (flags == NULL)
16256 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257 while (*flags != NUL)
16258 {
16259 switch (*flags)
16260 {
16261 case 'b': dir = BACKWARD; break;
16262 case 'w': p_ws = TRUE; break;
16263 case 'W': p_ws = FALSE; break;
16264 default: mask = 0;
16265 if (flagsp != NULL)
16266 switch (*flags)
16267 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016268 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016269 case 'e': mask = SP_END; break;
16270 case 'm': mask = SP_RETCOUNT; break;
16271 case 'n': mask = SP_NOMOVE; break;
16272 case 'p': mask = SP_SUBPAT; break;
16273 case 'r': mask = SP_REPEAT; break;
16274 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016275 }
16276 if (mask == 0)
16277 {
16278 EMSG2(_(e_invarg2), flags);
16279 dir = 0;
16280 }
16281 else
16282 *flagsp |= mask;
16283 }
16284 if (dir == 0)
16285 break;
16286 ++flags;
16287 }
16288 }
16289 return dir;
16290}
16291
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016293 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016295 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016296search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016297 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016298 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016299 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016301 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 char_u *pat;
16303 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016304 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305 int save_p_ws = p_ws;
16306 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016307 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016308 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016309 proftime_T tm;
16310#ifdef FEAT_RELTIME
16311 long time_limit = 0;
16312#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016313 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016314 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016316 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016317 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016318 if (dir == 0)
16319 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016320 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016321 if (flags & SP_START)
16322 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016323 if (flags & SP_END)
16324 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016325
Bram Moolenaar76929292008-01-06 19:07:36 +000016326 /* Optional arguments: line number to stop searching and timeout. */
16327 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016328 {
16329 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16330 if (lnum_stop < 0)
16331 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016332#ifdef FEAT_RELTIME
16333 if (argvars[3].v_type != VAR_UNKNOWN)
16334 {
16335 time_limit = get_tv_number_chk(&argvars[3], NULL);
16336 if (time_limit < 0)
16337 goto theend;
16338 }
16339#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016340 }
16341
Bram Moolenaar76929292008-01-06 19:07:36 +000016342#ifdef FEAT_RELTIME
16343 /* Set the time limit, if there is one. */
16344 profile_setlimit(time_limit, &tm);
16345#endif
16346
Bram Moolenaar231334e2005-07-25 20:46:57 +000016347 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016348 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016349 * Check to make sure only those flags are set.
16350 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16351 * flags cannot be set. Check for that condition also.
16352 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016353 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016354 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016356 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016357 goto theend;
16358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016360 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016361 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016362 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016363 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016365 if (flags & SP_SUBPAT)
16366 retval = subpatnum;
16367 else
16368 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016369 if (flags & SP_SETPCMARK)
16370 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016372 if (match_pos != NULL)
16373 {
16374 /* Store the match cursor position */
16375 match_pos->lnum = pos.lnum;
16376 match_pos->col = pos.col + 1;
16377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 /* "/$" will put the cursor after the end of the line, may need to
16379 * correct that here */
16380 check_cursor();
16381 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016382
16383 /* If 'n' flag is used: restore cursor position. */
16384 if (flags & SP_NOMOVE)
16385 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016386 else
16387 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016388theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016390
16391 return retval;
16392}
16393
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016394#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016395
16396/*
16397 * round() is not in C90, use ceil() or floor() instead.
16398 */
16399 float_T
16400vim_round(f)
16401 float_T f;
16402{
16403 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16404}
16405
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016406/*
16407 * "round({float})" function
16408 */
16409 static void
16410f_round(argvars, rettv)
16411 typval_T *argvars;
16412 typval_T *rettv;
16413{
16414 float_T f;
16415
16416 rettv->v_type = VAR_FLOAT;
16417 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016418 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016419 else
16420 rettv->vval.v_float = 0.0;
16421}
16422#endif
16423
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016424/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016425 * "screenattr()" function
16426 */
16427 static void
16428f_screenattr(argvars, rettv)
16429 typval_T *argvars UNUSED;
16430 typval_T *rettv;
16431{
16432 int row;
16433 int col;
16434 int c;
16435
16436 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16437 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16438 if (row < 0 || row >= screen_Rows
16439 || col < 0 || col >= screen_Columns)
16440 c = -1;
16441 else
16442 c = ScreenAttrs[LineOffset[row] + col];
16443 rettv->vval.v_number = c;
16444}
16445
16446/*
16447 * "screenchar()" function
16448 */
16449 static void
16450f_screenchar(argvars, rettv)
16451 typval_T *argvars UNUSED;
16452 typval_T *rettv;
16453{
16454 int row;
16455 int col;
16456 int off;
16457 int c;
16458
16459 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16460 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16461 if (row < 0 || row >= screen_Rows
16462 || col < 0 || col >= screen_Columns)
16463 c = -1;
16464 else
16465 {
16466 off = LineOffset[row] + col;
16467#ifdef FEAT_MBYTE
16468 if (enc_utf8 && ScreenLinesUC[off] != 0)
16469 c = ScreenLinesUC[off];
16470 else
16471#endif
16472 c = ScreenLines[off];
16473 }
16474 rettv->vval.v_number = c;
16475}
16476
16477/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016478 * "screencol()" function
16479 *
16480 * First column is 1 to be consistent with virtcol().
16481 */
16482 static void
16483f_screencol(argvars, rettv)
16484 typval_T *argvars UNUSED;
16485 typval_T *rettv;
16486{
16487 rettv->vval.v_number = screen_screencol() + 1;
16488}
16489
16490/*
16491 * "screenrow()" function
16492 */
16493 static void
16494f_screenrow(argvars, rettv)
16495 typval_T *argvars UNUSED;
16496 typval_T *rettv;
16497{
16498 rettv->vval.v_number = screen_screenrow() + 1;
16499}
16500
16501/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016502 * "search()" function
16503 */
16504 static void
16505f_search(argvars, rettv)
16506 typval_T *argvars;
16507 typval_T *rettv;
16508{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016509 int flags = 0;
16510
16511 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512}
16513
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016515 * "searchdecl()" function
16516 */
16517 static void
16518f_searchdecl(argvars, rettv)
16519 typval_T *argvars;
16520 typval_T *rettv;
16521{
16522 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016523 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016524 int error = FALSE;
16525 char_u *name;
16526
16527 rettv->vval.v_number = 1; /* default: FAIL */
16528
16529 name = get_tv_string_chk(&argvars[0]);
16530 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016531 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016532 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016533 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16534 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16535 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016536 if (!error && name != NULL)
16537 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016538 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016539}
16540
16541/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016542 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016544 static int
16545searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016546 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016547 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548{
16549 char_u *spat, *mpat, *epat;
16550 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 int dir;
16553 int flags = 0;
16554 char_u nbuf1[NUMBUFLEN];
16555 char_u nbuf2[NUMBUFLEN];
16556 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016557 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016558 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016559 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016562 spat = get_tv_string_chk(&argvars[0]);
16563 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16564 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16565 if (spat == NULL || mpat == NULL || epat == NULL)
16566 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 /* Handle the optional fourth argument: flags */
16569 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016570 if (dir == 0)
16571 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016572
16573 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016574 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16575 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016576 if ((flags & (SP_END | SP_SUBPAT)) != 0
16577 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016578 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016579 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016580 goto theend;
16581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016583 /* Using 'r' implies 'W', otherwise it doesn't work. */
16584 if (flags & SP_REPEAT)
16585 p_ws = FALSE;
16586
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016587 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016588 if (argvars[3].v_type == VAR_UNKNOWN
16589 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590 skip = (char_u *)"";
16591 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016593 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016594 if (argvars[5].v_type != VAR_UNKNOWN)
16595 {
16596 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16597 if (lnum_stop < 0)
16598 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016599#ifdef FEAT_RELTIME
16600 if (argvars[6].v_type != VAR_UNKNOWN)
16601 {
16602 time_limit = get_tv_number_chk(&argvars[6], NULL);
16603 if (time_limit < 0)
16604 goto theend;
16605 }
16606#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016607 }
16608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016609 if (skip == NULL)
16610 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016612 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016613 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016614
16615theend:
16616 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016617
16618 return retval;
16619}
16620
16621/*
16622 * "searchpair()" function
16623 */
16624 static void
16625f_searchpair(argvars, rettv)
16626 typval_T *argvars;
16627 typval_T *rettv;
16628{
16629 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16630}
16631
16632/*
16633 * "searchpairpos()" function
16634 */
16635 static void
16636f_searchpairpos(argvars, rettv)
16637 typval_T *argvars;
16638 typval_T *rettv;
16639{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016640 pos_T match_pos;
16641 int lnum = 0;
16642 int col = 0;
16643
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016644 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016645 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016646
16647 if (searchpair_cmn(argvars, &match_pos) > 0)
16648 {
16649 lnum = match_pos.lnum;
16650 col = match_pos.col;
16651 }
16652
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016653 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16654 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016655}
16656
16657/*
16658 * Search for a start/middle/end thing.
16659 * Used by searchpair(), see its documentation for the details.
16660 * Returns 0 or -1 for no match,
16661 */
16662 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016663do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16664 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016665 char_u *spat; /* start pattern */
16666 char_u *mpat; /* middle pattern */
16667 char_u *epat; /* end pattern */
16668 int dir; /* BACKWARD or FORWARD */
16669 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016670 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016671 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016672 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016673 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016674{
16675 char_u *save_cpo;
16676 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16677 long retval = 0;
16678 pos_T pos;
16679 pos_T firstpos;
16680 pos_T foundpos;
16681 pos_T save_cursor;
16682 pos_T save_pos;
16683 int n;
16684 int r;
16685 int nest = 1;
16686 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016687 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016688 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016689
16690 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16691 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016692 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016693
Bram Moolenaar76929292008-01-06 19:07:36 +000016694#ifdef FEAT_RELTIME
16695 /* Set the time limit, if there is one. */
16696 profile_setlimit(time_limit, &tm);
16697#endif
16698
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016699 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16700 * start/middle/end (pat3, for the top pair). */
16701 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16702 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16703 if (pat2 == NULL || pat3 == NULL)
16704 goto theend;
16705 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16706 if (*mpat == NUL)
16707 STRCPY(pat3, pat2);
16708 else
16709 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16710 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016711 if (flags & SP_START)
16712 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016713
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714 save_cursor = curwin->w_cursor;
16715 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016716 clearpos(&firstpos);
16717 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 pat = pat3;
16719 for (;;)
16720 {
16721 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016722 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16724 /* didn't find it or found the first match again: FAIL */
16725 break;
16726
16727 if (firstpos.lnum == 0)
16728 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016729 if (equalpos(pos, foundpos))
16730 {
16731 /* Found the same position again. Can happen with a pattern that
16732 * has "\zs" at the end and searching backwards. Advance one
16733 * character and try again. */
16734 if (dir == BACKWARD)
16735 decl(&pos);
16736 else
16737 incl(&pos);
16738 }
16739 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016741 /* clear the start flag to avoid getting stuck here */
16742 options &= ~SEARCH_START;
16743
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744 /* If the skip pattern matches, ignore this match. */
16745 if (*skip != NUL)
16746 {
16747 save_pos = curwin->w_cursor;
16748 curwin->w_cursor = pos;
16749 r = eval_to_bool(skip, &err, NULL, FALSE);
16750 curwin->w_cursor = save_pos;
16751 if (err)
16752 {
16753 /* Evaluating {skip} caused an error, break here. */
16754 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016755 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 break;
16757 }
16758 if (r)
16759 continue;
16760 }
16761
16762 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16763 {
16764 /* Found end when searching backwards or start when searching
16765 * forward: nested pair. */
16766 ++nest;
16767 pat = pat2; /* nested, don't search for middle */
16768 }
16769 else
16770 {
16771 /* Found end when searching forward or start when searching
16772 * backward: end of (nested) pair; or found middle in outer pair. */
16773 if (--nest == 1)
16774 pat = pat3; /* outer level, search for middle */
16775 }
16776
16777 if (nest == 0)
16778 {
16779 /* Found the match: return matchcount or line number. */
16780 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016781 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016783 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016784 if (flags & SP_SETPCMARK)
16785 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 curwin->w_cursor = pos;
16787 if (!(flags & SP_REPEAT))
16788 break;
16789 nest = 1; /* search for next unmatched */
16790 }
16791 }
16792
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016793 if (match_pos != NULL)
16794 {
16795 /* Store the match cursor position */
16796 match_pos->lnum = curwin->w_cursor.lnum;
16797 match_pos->col = curwin->w_cursor.col + 1;
16798 }
16799
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016801 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 curwin->w_cursor = save_cursor;
16803
16804theend:
16805 vim_free(pat2);
16806 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016807 if (p_cpo == empty_option)
16808 p_cpo = save_cpo;
16809 else
16810 /* Darn, evaluating the {skip} expression changed the value. */
16811 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016812
16813 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814}
16815
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016816/*
16817 * "searchpos()" function
16818 */
16819 static void
16820f_searchpos(argvars, rettv)
16821 typval_T *argvars;
16822 typval_T *rettv;
16823{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016824 pos_T match_pos;
16825 int lnum = 0;
16826 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016827 int n;
16828 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016829
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016830 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016831 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016832
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016833 n = search_cmn(argvars, &match_pos, &flags);
16834 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016835 {
16836 lnum = match_pos.lnum;
16837 col = match_pos.col;
16838 }
16839
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016840 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16841 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016842 if (flags & SP_SUBPAT)
16843 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016844}
16845
16846
Bram Moolenaar0d660222005-01-07 21:51:51 +000016847 static void
16848f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016849 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016852#ifdef FEAT_CLIENTSERVER
16853 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016854 char_u *server = get_tv_string_chk(&argvars[0]);
16855 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856
Bram Moolenaar0d660222005-01-07 21:51:51 +000016857 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016858 if (server == NULL || reply == NULL)
16859 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016860 if (check_restricted() || check_secure())
16861 return;
16862# ifdef FEAT_X11
16863 if (check_connection() == FAIL)
16864 return;
16865# endif
16866
16867 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016869 EMSG(_("E258: Unable to send to client"));
16870 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016872 rettv->vval.v_number = 0;
16873#else
16874 rettv->vval.v_number = -1;
16875#endif
16876}
16877
Bram Moolenaar0d660222005-01-07 21:51:51 +000016878 static void
16879f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016880 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016881 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016882{
16883 char_u *r = NULL;
16884
16885#ifdef FEAT_CLIENTSERVER
16886# ifdef WIN32
16887 r = serverGetVimNames();
16888# else
16889 make_connection();
16890 if (X_DISPLAY != NULL)
16891 r = serverGetVimNames(X_DISPLAY);
16892# endif
16893#endif
16894 rettv->v_type = VAR_STRING;
16895 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896}
16897
16898/*
16899 * "setbufvar()" function
16900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016902f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016903 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016904 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905{
16906 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016909 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910 char_u nbuf[NUMBUFLEN];
16911
16912 if (check_restricted() || check_secure())
16913 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016914 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16915 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016916 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 varp = &argvars[2];
16918
16919 if (buf != NULL && varname != NULL && varp != NULL)
16920 {
16921 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923
16924 if (*varname == '&')
16925 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016926 long numval;
16927 char_u *strval;
16928 int error = FALSE;
16929
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016931 numval = get_tv_number_chk(varp, &error);
16932 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016933 if (!error && strval != NULL)
16934 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 }
16936 else
16937 {
16938 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16939 if (bufvarname != NULL)
16940 {
16941 STRCPY(bufvarname, "b:");
16942 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016943 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 vim_free(bufvarname);
16945 }
16946 }
16947
16948 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951}
16952
16953/*
16954 * "setcmdpos()" function
16955 */
16956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016957f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016958 typval_T *argvars;
16959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016961 int pos = (int)get_tv_number(&argvars[0]) - 1;
16962
16963 if (pos >= 0)
16964 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965}
16966
16967/*
16968 * "setline()" function
16969 */
16970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016971f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016972 typval_T *argvars;
16973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974{
16975 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016976 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016977 list_T *l = NULL;
16978 listitem_T *li = NULL;
16979 long added = 0;
16980 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016982 lnum = get_tv_lnum(&argvars[0]);
16983 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016985 l = argvars[1].vval.v_list;
16986 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016988 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016989 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016990
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016991 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016992 for (;;)
16993 {
16994 if (l != NULL)
16995 {
16996 /* list argument, get next string */
16997 if (li == NULL)
16998 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016999 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017000 li = li->li_next;
17001 }
17002
17003 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017004 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017005 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017006
17007 /* When coming here from Insert mode, sync undo, so that this can be
17008 * undone separately from what was previously inserted. */
17009 if (u_sync_once == 2)
17010 {
17011 u_sync_once = 1; /* notify that u_sync() was called */
17012 u_sync(TRUE);
17013 }
17014
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017015 if (lnum <= curbuf->b_ml.ml_line_count)
17016 {
17017 /* existing line, replace it */
17018 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17019 {
17020 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017021 if (lnum == curwin->w_cursor.lnum)
17022 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017023 rettv->vval.v_number = 0; /* OK */
17024 }
17025 }
17026 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17027 {
17028 /* lnum is one past the last line, append the line */
17029 ++added;
17030 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17031 rettv->vval.v_number = 0; /* OK */
17032 }
17033
17034 if (l == NULL) /* only one string argument */
17035 break;
17036 ++lnum;
17037 }
17038
17039 if (added > 0)
17040 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041}
17042
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017043static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17044
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017046 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017047 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017048 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017049set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017050 win_T *wp UNUSED;
17051 typval_T *list_arg UNUSED;
17052 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017053 typval_T *rettv;
17054{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017055#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017056 char_u *act;
17057 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017058#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017059
Bram Moolenaar2641f772005-03-25 21:58:17 +000017060 rettv->vval.v_number = -1;
17061
17062#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017063 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017064 EMSG(_(e_listreq));
17065 else
17066 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017067 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017068
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017069 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017070 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017071 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017072 if (act == NULL)
17073 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017074 if (*act == 'a' || *act == 'r')
17075 action = *act;
17076 }
17077
Bram Moolenaar81484f42012-12-05 15:16:47 +010017078 if (l != NULL && set_errorlist(wp, l, action,
17079 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017080 rettv->vval.v_number = 0;
17081 }
17082#endif
17083}
17084
17085/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017086 * "setloclist()" function
17087 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017088 static void
17089f_setloclist(argvars, rettv)
17090 typval_T *argvars;
17091 typval_T *rettv;
17092{
17093 win_T *win;
17094
17095 rettv->vval.v_number = -1;
17096
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017097 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017098 if (win != NULL)
17099 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17100}
17101
17102/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017103 * "setmatches()" function
17104 */
17105 static void
17106f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017107 typval_T *argvars UNUSED;
17108 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017109{
17110#ifdef FEAT_SEARCH_EXTRA
17111 list_T *l;
17112 listitem_T *li;
17113 dict_T *d;
17114
17115 rettv->vval.v_number = -1;
17116 if (argvars[0].v_type != VAR_LIST)
17117 {
17118 EMSG(_(e_listreq));
17119 return;
17120 }
17121 if ((l = argvars[0].vval.v_list) != NULL)
17122 {
17123
17124 /* To some extent make sure that we are dealing with a list from
17125 * "getmatches()". */
17126 li = l->lv_first;
17127 while (li != NULL)
17128 {
17129 if (li->li_tv.v_type != VAR_DICT
17130 || (d = li->li_tv.vval.v_dict) == NULL)
17131 {
17132 EMSG(_(e_invarg));
17133 return;
17134 }
17135 if (!(dict_find(d, (char_u *)"group", -1) != NULL
17136 && dict_find(d, (char_u *)"pattern", -1) != NULL
17137 && dict_find(d, (char_u *)"priority", -1) != NULL
17138 && dict_find(d, (char_u *)"id", -1) != NULL))
17139 {
17140 EMSG(_(e_invarg));
17141 return;
17142 }
17143 li = li->li_next;
17144 }
17145
17146 clear_matches(curwin);
17147 li = l->lv_first;
17148 while (li != NULL)
17149 {
17150 d = li->li_tv.vval.v_dict;
17151 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
17152 get_dict_string(d, (char_u *)"pattern", FALSE),
17153 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020017154 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017155 li = li->li_next;
17156 }
17157 rettv->vval.v_number = 0;
17158 }
17159#endif
17160}
17161
17162/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017163 * "setpos()" function
17164 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017165 static void
17166f_setpos(argvars, rettv)
17167 typval_T *argvars;
17168 typval_T *rettv;
17169{
17170 pos_T pos;
17171 int fnum;
17172 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017173 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017174
Bram Moolenaar08250432008-02-13 11:42:46 +000017175 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017176 name = get_tv_string_chk(argvars);
17177 if (name != NULL)
17178 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017179 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017180 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017181 if (--pos.col < 0)
17182 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017183 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017184 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017185 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017186 if (fnum == curbuf->b_fnum)
17187 {
17188 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017189 if (curswant >= 0)
17190 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017191 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017192 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017193 }
17194 else
17195 EMSG(_(e_invarg));
17196 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017197 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17198 {
17199 /* set mark */
17200 if (setmark_pos(name[1], &pos, fnum) == OK)
17201 rettv->vval.v_number = 0;
17202 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017203 else
17204 EMSG(_(e_invarg));
17205 }
17206 }
17207}
17208
17209/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017210 * "setqflist()" function
17211 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017212 static void
17213f_setqflist(argvars, rettv)
17214 typval_T *argvars;
17215 typval_T *rettv;
17216{
17217 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17218}
17219
17220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 * "setreg()" function
17222 */
17223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017224f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017225 typval_T *argvars;
17226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227{
17228 int regname;
17229 char_u *strregname;
17230 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017231 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 int append;
17233 char_u yank_type;
17234 long block_len;
17235
17236 block_len = -1;
17237 yank_type = MAUTO;
17238 append = FALSE;
17239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017240 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017241 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017243 if (strregname == NULL)
17244 return; /* type error; errmsg already given */
17245 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246 if (regname == 0 || regname == '@')
17247 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017249 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017251 stropt = get_tv_string_chk(&argvars[2]);
17252 if (stropt == NULL)
17253 return; /* type error */
17254 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255 switch (*stropt)
17256 {
17257 case 'a': case 'A': /* append */
17258 append = TRUE;
17259 break;
17260 case 'v': case 'c': /* character-wise selection */
17261 yank_type = MCHAR;
17262 break;
17263 case 'V': case 'l': /* line-wise selection */
17264 yank_type = MLINE;
17265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 case 'b': case Ctrl_V: /* block-wise selection */
17267 yank_type = MBLOCK;
17268 if (VIM_ISDIGIT(stropt[1]))
17269 {
17270 ++stropt;
17271 block_len = getdigits(&stropt) - 1;
17272 --stropt;
17273 }
17274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 }
17276 }
17277
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017278 if (argvars[1].v_type == VAR_LIST)
17279 {
17280 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017281 char_u **allocval;
17282 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017283 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017284 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017285 int len = argvars[1].vval.v_list->lv_len;
17286 listitem_T *li;
17287
Bram Moolenaar7d647822014-04-05 21:28:56 +020017288 /* First half: use for pointers to result lines; second half: use for
17289 * pointers to allocated copies. */
17290 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017291 if (lstval == NULL)
17292 return;
17293 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017294 allocval = lstval + len + 2;
17295 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017296
17297 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17298 li = li->li_next)
17299 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017300 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017301 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017302 goto free_lstval;
17303 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017304 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017305 /* Need to make a copy, next get_tv_string_buf_chk() will
17306 * overwrite the string. */
17307 strval = vim_strsave(buf);
17308 if (strval == NULL)
17309 goto free_lstval;
17310 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017311 }
17312 *curval++ = strval;
17313 }
17314 *curval++ = NULL;
17315
17316 write_reg_contents_lst(regname, lstval, -1,
17317 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017318free_lstval:
17319 while (curallocval > allocval)
17320 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017321 vim_free(lstval);
17322 }
17323 else
17324 {
17325 strval = get_tv_string_chk(&argvars[1]);
17326 if (strval == NULL)
17327 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017328 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017331 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332}
17333
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017334/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017335 * "settabvar()" function
17336 */
17337 static void
17338f_settabvar(argvars, rettv)
17339 typval_T *argvars;
17340 typval_T *rettv;
17341{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017342#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017343 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017344 tabpage_T *tp;
17345#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017346 char_u *varname, *tabvarname;
17347 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017348
17349 rettv->vval.v_number = 0;
17350
17351 if (check_restricted() || check_secure())
17352 return;
17353
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017354#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017355 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017356#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017357 varname = get_tv_string_chk(&argvars[1]);
17358 varp = &argvars[2];
17359
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017360 if (varname != NULL && varp != NULL
17361#ifdef FEAT_WINDOWS
17362 && tp != NULL
17363#endif
17364 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017365 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017366#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017367 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017368 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017369#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017370
17371 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17372 if (tabvarname != NULL)
17373 {
17374 STRCPY(tabvarname, "t:");
17375 STRCPY(tabvarname + 2, varname);
17376 set_var(tabvarname, varp, TRUE);
17377 vim_free(tabvarname);
17378 }
17379
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017380#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017381 /* Restore current tabpage */
17382 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017383 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017384#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017385 }
17386}
17387
17388/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017389 * "settabwinvar()" function
17390 */
17391 static void
17392f_settabwinvar(argvars, rettv)
17393 typval_T *argvars;
17394 typval_T *rettv;
17395{
17396 setwinvar(argvars, rettv, 1);
17397}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398
17399/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017400 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017404 typval_T *argvars;
17405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017407 setwinvar(argvars, rettv, 0);
17408}
17409
17410/*
17411 * "setwinvar()" and "settabwinvar()" functions
17412 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017413
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017414 static void
17415setwinvar(argvars, rettv, off)
17416 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017417 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017418 int off;
17419{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420 win_T *win;
17421#ifdef FEAT_WINDOWS
17422 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017423 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424#endif
17425 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017426 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017428 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429
17430 if (check_restricted() || check_secure())
17431 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017432
17433#ifdef FEAT_WINDOWS
17434 if (off == 1)
17435 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17436 else
17437 tp = curtab;
17438#endif
17439 win = find_win_by_nr(&argvars[off], tp);
17440 varname = get_tv_string_chk(&argvars[off + 1]);
17441 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442
17443 if (win != NULL && varname != NULL && varp != NULL)
17444 {
17445#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017446 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017449 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017451 long numval;
17452 char_u *strval;
17453 int error = FALSE;
17454
17455 ++varname;
17456 numval = get_tv_number_chk(varp, &error);
17457 strval = get_tv_string_buf_chk(varp, nbuf);
17458 if (!error && strval != NULL)
17459 set_option_value(varname, numval, strval, OPT_LOCAL);
17460 }
17461 else
17462 {
17463 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17464 if (winvarname != NULL)
17465 {
17466 STRCPY(winvarname, "w:");
17467 STRCPY(winvarname + 2, varname);
17468 set_var(winvarname, varp, TRUE);
17469 vim_free(winvarname);
17470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 }
17472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017474 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475#endif
17476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477}
17478
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017479#ifdef FEAT_CRYPT
17480/*
17481 * "sha256({string})" function
17482 */
17483 static void
17484f_sha256(argvars, rettv)
17485 typval_T *argvars;
17486 typval_T *rettv;
17487{
17488 char_u *p;
17489
17490 p = get_tv_string(&argvars[0]);
17491 rettv->vval.v_string = vim_strsave(
17492 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17493 rettv->v_type = VAR_STRING;
17494}
17495#endif /* FEAT_CRYPT */
17496
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017498 * "shellescape({string})" function
17499 */
17500 static void
17501f_shellescape(argvars, rettv)
17502 typval_T *argvars;
17503 typval_T *rettv;
17504{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017505 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017506 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017507 rettv->v_type = VAR_STRING;
17508}
17509
17510/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017511 * shiftwidth() function
17512 */
17513 static void
17514f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017515 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017516 typval_T *rettv;
17517{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017518 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017519}
17520
17521/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017522 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 */
17524 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017525f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017526 typval_T *argvars;
17527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017529 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530
Bram Moolenaar0d660222005-01-07 21:51:51 +000017531 p = get_tv_string(&argvars[0]);
17532 rettv->vval.v_string = vim_strsave(p);
17533 simplify_filename(rettv->vval.v_string); /* simplify in place */
17534 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535}
17536
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017537#ifdef FEAT_FLOAT
17538/*
17539 * "sin()" function
17540 */
17541 static void
17542f_sin(argvars, rettv)
17543 typval_T *argvars;
17544 typval_T *rettv;
17545{
17546 float_T f;
17547
17548 rettv->v_type = VAR_FLOAT;
17549 if (get_float_arg(argvars, &f) == OK)
17550 rettv->vval.v_float = sin(f);
17551 else
17552 rettv->vval.v_float = 0.0;
17553}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017554
17555/*
17556 * "sinh()" function
17557 */
17558 static void
17559f_sinh(argvars, rettv)
17560 typval_T *argvars;
17561 typval_T *rettv;
17562{
17563 float_T f;
17564
17565 rettv->v_type = VAR_FLOAT;
17566 if (get_float_arg(argvars, &f) == OK)
17567 rettv->vval.v_float = sinh(f);
17568 else
17569 rettv->vval.v_float = 0.0;
17570}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017571#endif
17572
Bram Moolenaar0d660222005-01-07 21:51:51 +000017573static int
17574#ifdef __BORLANDC__
17575 _RTLENTRYF
17576#endif
17577 item_compare __ARGS((const void *s1, const void *s2));
17578static int
17579#ifdef __BORLANDC__
17580 _RTLENTRYF
17581#endif
17582 item_compare2 __ARGS((const void *s1, const void *s2));
17583
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017584/* struct used in the array that's given to qsort() */
17585typedef struct
17586{
17587 listitem_T *item;
17588 int idx;
17589} sortItem_T;
17590
Bram Moolenaar0d660222005-01-07 21:51:51 +000017591static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017592static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017593static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017594static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017595static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017596static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017597static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017598#define ITEM_COMPARE_FAIL 999
17599
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017601 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017603 static int
17604#ifdef __BORLANDC__
17605_RTLENTRYF
17606#endif
17607item_compare(s1, s2)
17608 const void *s1;
17609 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017611 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017612 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017613 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017614 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017615 int res;
17616 char_u numbuf1[NUMBUFLEN];
17617 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017619 si1 = (sortItem_T *)s1;
17620 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017621 tv1 = &si1->item->li_tv;
17622 tv2 = &si2->item->li_tv;
17623 /* tv2string() puts quotes around a string and allocates memory. Don't do
17624 * that for string variables. Use a single quote when comparing with a
17625 * non-string to do what the docs promise. */
17626 if (tv1->v_type == VAR_STRING)
17627 {
17628 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17629 p1 = (char_u *)"'";
17630 else
17631 p1 = tv1->vval.v_string;
17632 }
17633 else
17634 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17635 if (tv2->v_type == VAR_STRING)
17636 {
17637 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17638 p2 = (char_u *)"'";
17639 else
17640 p2 = tv2->vval.v_string;
17641 }
17642 else
17643 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017644 if (p1 == NULL)
17645 p1 = (char_u *)"";
17646 if (p2 == NULL)
17647 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017648 if (!item_compare_numeric)
17649 {
17650 if (item_compare_ic)
17651 res = STRICMP(p1, p2);
17652 else
17653 res = STRCMP(p1, p2);
17654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017656 {
17657 double n1, n2;
17658 n1 = strtod((char *)p1, (char **)&p1);
17659 n2 = strtod((char *)p2, (char **)&p2);
17660 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17661 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017662
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017663 /* When the result would be zero, compare the item indexes. Makes the
17664 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017665 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017666 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017667
Bram Moolenaar0d660222005-01-07 21:51:51 +000017668 vim_free(tofree1);
17669 vim_free(tofree2);
17670 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671}
17672
17673 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017674#ifdef __BORLANDC__
17675_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017677item_compare2(s1, s2)
17678 const void *s1;
17679 const void *s2;
17680{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017681 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017682 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017683 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017684 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017685 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017687 /* shortcut after failure in previous call; compare all items equal */
17688 if (item_compare_func_err)
17689 return 0;
17690
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017691 si1 = (sortItem_T *)s1;
17692 si2 = (sortItem_T *)s2;
17693
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017694 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017695 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017696 copy_tv(&si1->item->li_tv, &argv[0]);
17697 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017698
17699 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017700 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017701 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17702 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017703 clear_tv(&argv[0]);
17704 clear_tv(&argv[1]);
17705
17706 if (res == FAIL)
17707 res = ITEM_COMPARE_FAIL;
17708 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017709 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17710 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017711 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017712 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017713
17714 /* When the result would be zero, compare the pointers themselves. Makes
17715 * the sort stable. */
17716 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017717 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017718
Bram Moolenaar0d660222005-01-07 21:51:51 +000017719 return res;
17720}
17721
17722/*
17723 * "sort({list})" function
17724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017726do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017727 typval_T *argvars;
17728 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017729 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730{
Bram Moolenaar33570922005-01-25 22:26:29 +000017731 list_T *l;
17732 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017733 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017734 long len;
17735 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736
Bram Moolenaar0d660222005-01-07 21:51:51 +000017737 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017738 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 else
17740 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017741 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017742 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017743 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17744 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017745 return;
17746 rettv->vval.v_list = l;
17747 rettv->v_type = VAR_LIST;
17748 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749
Bram Moolenaar0d660222005-01-07 21:51:51 +000017750 len = list_len(l);
17751 if (len <= 1)
17752 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753
Bram Moolenaar0d660222005-01-07 21:51:51 +000017754 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017755 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017756 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017757 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017758 if (argvars[1].v_type != VAR_UNKNOWN)
17759 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017760 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017761 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017762 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017763 else
17764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017765 int error = FALSE;
17766
17767 i = get_tv_number_chk(&argvars[1], &error);
17768 if (error)
17769 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017770 if (i == 1)
17771 item_compare_ic = TRUE;
17772 else
17773 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017774 if (item_compare_func != NULL)
17775 {
17776 if (STRCMP(item_compare_func, "n") == 0)
17777 {
17778 item_compare_func = NULL;
17779 item_compare_numeric = TRUE;
17780 }
17781 else if (STRCMP(item_compare_func, "i") == 0)
17782 {
17783 item_compare_func = NULL;
17784 item_compare_ic = TRUE;
17785 }
17786 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017787 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017788
17789 if (argvars[2].v_type != VAR_UNKNOWN)
17790 {
17791 /* optional third argument: {dict} */
17792 if (argvars[2].v_type != VAR_DICT)
17793 {
17794 EMSG(_(e_dictreq));
17795 return;
17796 }
17797 item_compare_selfdict = argvars[2].vval.v_dict;
17798 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800
Bram Moolenaar0d660222005-01-07 21:51:51 +000017801 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017802 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017803 if (ptrs == NULL)
17804 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805
Bram Moolenaar327aa022014-03-25 18:24:23 +010017806 i = 0;
17807 if (sort)
17808 {
17809 /* sort(): ptrs will be the list to sort */
17810 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017811 {
17812 ptrs[i].item = li;
17813 ptrs[i].idx = i;
17814 ++i;
17815 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017816
17817 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017818 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017819 /* test the compare function */
17820 if (item_compare_func != NULL
17821 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017822 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017823 EMSG(_("E702: Sort compare function failed"));
17824 else
17825 {
17826 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017827 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017828 item_compare_func == NULL ? item_compare : item_compare2);
17829
17830 if (!item_compare_func_err)
17831 {
17832 /* Clear the List and append the items in sorted order. */
17833 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17834 l->lv_len = 0;
17835 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017836 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017837 }
17838 }
17839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017841 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017842 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17843
17844 /* f_uniq(): ptrs will be a stack of items to remove */
17845 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017846 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017847 item_compare_func_ptr = item_compare_func
17848 ? item_compare2 : item_compare;
17849
17850 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17851 li = li->li_next)
17852 {
17853 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17854 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017855 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017856 if (item_compare_func_err)
17857 {
17858 EMSG(_("E882: Uniq compare function failed"));
17859 break;
17860 }
17861 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017863 if (!item_compare_func_err)
17864 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017865 while (--i >= 0)
17866 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017867 li = ptrs[i].item->li_next;
17868 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017869 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017870 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017871 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017872 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017873 list_fix_watch(l, li);
17874 listitem_free(li);
17875 l->lv_len--;
17876 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017877 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017878 }
17879
17880 vim_free(ptrs);
17881 }
17882}
17883
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017884/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017885 * "sort({list})" function
17886 */
17887 static void
17888f_sort(argvars, rettv)
17889 typval_T *argvars;
17890 typval_T *rettv;
17891{
17892 do_sort_uniq(argvars, rettv, TRUE);
17893}
17894
17895/*
17896 * "uniq({list})" function
17897 */
17898 static void
17899f_uniq(argvars, rettv)
17900 typval_T *argvars;
17901 typval_T *rettv;
17902{
17903 do_sort_uniq(argvars, rettv, FALSE);
17904}
17905
17906/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017907 * "soundfold({word})" function
17908 */
17909 static void
17910f_soundfold(argvars, rettv)
17911 typval_T *argvars;
17912 typval_T *rettv;
17913{
17914 char_u *s;
17915
17916 rettv->v_type = VAR_STRING;
17917 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017918#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017919 rettv->vval.v_string = eval_soundfold(s);
17920#else
17921 rettv->vval.v_string = vim_strsave(s);
17922#endif
17923}
17924
17925/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017926 * "spellbadword()" function
17927 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017928 static void
17929f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017930 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017931 typval_T *rettv;
17932{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017933 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017934 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017935 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017936
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017937 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017938 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017939
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017940#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017941 if (argvars[0].v_type == VAR_UNKNOWN)
17942 {
17943 /* Find the start and length of the badly spelled word. */
17944 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17945 if (len != 0)
17946 word = ml_get_cursor();
17947 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017948 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017949 {
17950 char_u *str = get_tv_string_chk(&argvars[0]);
17951 int capcol = -1;
17952
17953 if (str != NULL)
17954 {
17955 /* Check the argument for spelling. */
17956 while (*str != NUL)
17957 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017958 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017959 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017960 {
17961 word = str;
17962 break;
17963 }
17964 str += len;
17965 }
17966 }
17967 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017968#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017969
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017970 list_append_string(rettv->vval.v_list, word, len);
17971 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017972 attr == HLF_SPB ? "bad" :
17973 attr == HLF_SPR ? "rare" :
17974 attr == HLF_SPL ? "local" :
17975 attr == HLF_SPC ? "caps" :
17976 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017977}
17978
17979/*
17980 * "spellsuggest()" function
17981 */
17982 static void
17983f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017984 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017985 typval_T *rettv;
17986{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017987#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017988 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017989 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017990 int maxcount;
17991 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017992 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017993 listitem_T *li;
17994 int need_capital = FALSE;
17995#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017996
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017997 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017998 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017999
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018000#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018001 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018002 {
18003 str = get_tv_string(&argvars[0]);
18004 if (argvars[1].v_type != VAR_UNKNOWN)
18005 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018006 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018007 if (maxcount <= 0)
18008 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018009 if (argvars[2].v_type != VAR_UNKNOWN)
18010 {
18011 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18012 if (typeerr)
18013 return;
18014 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018015 }
18016 else
18017 maxcount = 25;
18018
Bram Moolenaar4770d092006-01-12 23:22:24 +000018019 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018020
18021 for (i = 0; i < ga.ga_len; ++i)
18022 {
18023 str = ((char_u **)ga.ga_data)[i];
18024
18025 li = listitem_alloc();
18026 if (li == NULL)
18027 vim_free(str);
18028 else
18029 {
18030 li->li_tv.v_type = VAR_STRING;
18031 li->li_tv.v_lock = 0;
18032 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018033 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018034 }
18035 }
18036 ga_clear(&ga);
18037 }
18038#endif
18039}
18040
Bram Moolenaar0d660222005-01-07 21:51:51 +000018041 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018042f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018043 typval_T *argvars;
18044 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018045{
18046 char_u *str;
18047 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018048 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018049 regmatch_T regmatch;
18050 char_u patbuf[NUMBUFLEN];
18051 char_u *save_cpo;
18052 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018053 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018054 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018055 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018056
18057 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18058 save_cpo = p_cpo;
18059 p_cpo = (char_u *)"";
18060
18061 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018062 if (argvars[1].v_type != VAR_UNKNOWN)
18063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018064 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18065 if (pat == NULL)
18066 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018067 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018068 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018069 }
18070 if (pat == NULL || *pat == NUL)
18071 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018072
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018073 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018075 if (typeerr)
18076 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077
Bram Moolenaar0d660222005-01-07 21:51:51 +000018078 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18079 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018081 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018082 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018083 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018084 if (*str == NUL)
18085 match = FALSE; /* empty item at the end */
18086 else
18087 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018088 if (match)
18089 end = regmatch.startp[0];
18090 else
18091 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018092 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18093 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018094 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018095 if (list_append_string(rettv->vval.v_list, str,
18096 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018097 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018098 }
18099 if (!match)
18100 break;
18101 /* Advance to just after the match. */
18102 if (regmatch.endp[0] > str)
18103 col = 0;
18104 else
18105 {
18106 /* Don't get stuck at the same match. */
18107#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018108 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018109#else
18110 col = 1;
18111#endif
18112 }
18113 str = regmatch.endp[0];
18114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115
Bram Moolenaar473de612013-06-08 18:19:48 +020018116 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118
Bram Moolenaar0d660222005-01-07 21:51:51 +000018119 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120}
18121
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018122#ifdef FEAT_FLOAT
18123/*
18124 * "sqrt()" function
18125 */
18126 static void
18127f_sqrt(argvars, rettv)
18128 typval_T *argvars;
18129 typval_T *rettv;
18130{
18131 float_T f;
18132
18133 rettv->v_type = VAR_FLOAT;
18134 if (get_float_arg(argvars, &f) == OK)
18135 rettv->vval.v_float = sqrt(f);
18136 else
18137 rettv->vval.v_float = 0.0;
18138}
18139
18140/*
18141 * "str2float()" function
18142 */
18143 static void
18144f_str2float(argvars, rettv)
18145 typval_T *argvars;
18146 typval_T *rettv;
18147{
18148 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18149
18150 if (*p == '+')
18151 p = skipwhite(p + 1);
18152 (void)string2float(p, &rettv->vval.v_float);
18153 rettv->v_type = VAR_FLOAT;
18154}
18155#endif
18156
Bram Moolenaar2c932302006-03-18 21:42:09 +000018157/*
18158 * "str2nr()" function
18159 */
18160 static void
18161f_str2nr(argvars, rettv)
18162 typval_T *argvars;
18163 typval_T *rettv;
18164{
18165 int base = 10;
18166 char_u *p;
18167 long n;
18168
18169 if (argvars[1].v_type != VAR_UNKNOWN)
18170 {
18171 base = get_tv_number(&argvars[1]);
18172 if (base != 8 && base != 10 && base != 16)
18173 {
18174 EMSG(_(e_invarg));
18175 return;
18176 }
18177 }
18178
18179 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018180 if (*p == '+')
18181 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018182 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
18183 rettv->vval.v_number = n;
18184}
18185
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186#ifdef HAVE_STRFTIME
18187/*
18188 * "strftime({format}[, {time}])" function
18189 */
18190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018191f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018192 typval_T *argvars;
18193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194{
18195 char_u result_buf[256];
18196 struct tm *curtime;
18197 time_t seconds;
18198 char_u *p;
18199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018200 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018202 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018203 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 seconds = time(NULL);
18205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018206 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 curtime = localtime(&seconds);
18208 /* MSVC returns NULL for an invalid value of seconds. */
18209 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018210 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211 else
18212 {
18213# ifdef FEAT_MBYTE
18214 vimconv_T conv;
18215 char_u *enc;
18216
18217 conv.vc_type = CONV_NONE;
18218 enc = enc_locale();
18219 convert_setup(&conv, p_enc, enc);
18220 if (conv.vc_type != CONV_NONE)
18221 p = string_convert(&conv, p, NULL);
18222# endif
18223 if (p != NULL)
18224 (void)strftime((char *)result_buf, sizeof(result_buf),
18225 (char *)p, curtime);
18226 else
18227 result_buf[0] = NUL;
18228
18229# ifdef FEAT_MBYTE
18230 if (conv.vc_type != CONV_NONE)
18231 vim_free(p);
18232 convert_setup(&conv, enc, p_enc);
18233 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018234 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235 else
18236# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018237 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238
18239# ifdef FEAT_MBYTE
18240 /* Release conversion descriptors */
18241 convert_setup(&conv, NULL, NULL);
18242 vim_free(enc);
18243# endif
18244 }
18245}
18246#endif
18247
18248/*
18249 * "stridx()" function
18250 */
18251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018252f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018253 typval_T *argvars;
18254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255{
18256 char_u buf[NUMBUFLEN];
18257 char_u *needle;
18258 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018259 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018261 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018263 needle = get_tv_string_chk(&argvars[1]);
18264 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018265 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018266 if (needle == NULL || haystack == NULL)
18267 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268
Bram Moolenaar33570922005-01-25 22:26:29 +000018269 if (argvars[2].v_type != VAR_UNKNOWN)
18270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018271 int error = FALSE;
18272
18273 start_idx = get_tv_number_chk(&argvars[2], &error);
18274 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018275 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018276 if (start_idx >= 0)
18277 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018278 }
18279
18280 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18281 if (pos != NULL)
18282 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283}
18284
18285/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018286 * "string()" function
18287 */
18288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018289f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018290 typval_T *argvars;
18291 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018292{
18293 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018294 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018296 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018297 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018298 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018299 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018300 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301}
18302
18303/*
18304 * "strlen()" function
18305 */
18306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018307f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018308 typval_T *argvars;
18309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018311 rettv->vval.v_number = (varnumber_T)(STRLEN(
18312 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313}
18314
18315/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018316 * "strchars()" function
18317 */
18318 static void
18319f_strchars(argvars, rettv)
18320 typval_T *argvars;
18321 typval_T *rettv;
18322{
18323 char_u *s = get_tv_string(&argvars[0]);
18324#ifdef FEAT_MBYTE
18325 varnumber_T len = 0;
18326
18327 while (*s != NUL)
18328 {
18329 mb_cptr2char_adv(&s);
18330 ++len;
18331 }
18332 rettv->vval.v_number = len;
18333#else
18334 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18335#endif
18336}
18337
18338/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018339 * "strdisplaywidth()" function
18340 */
18341 static void
18342f_strdisplaywidth(argvars, rettv)
18343 typval_T *argvars;
18344 typval_T *rettv;
18345{
18346 char_u *s = get_tv_string(&argvars[0]);
18347 int col = 0;
18348
18349 if (argvars[1].v_type != VAR_UNKNOWN)
18350 col = get_tv_number(&argvars[1]);
18351
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018352 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018353}
18354
18355/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018356 * "strwidth()" function
18357 */
18358 static void
18359f_strwidth(argvars, rettv)
18360 typval_T *argvars;
18361 typval_T *rettv;
18362{
18363 char_u *s = get_tv_string(&argvars[0]);
18364
18365 rettv->vval.v_number = (varnumber_T)(
18366#ifdef FEAT_MBYTE
18367 mb_string2cells(s, -1)
18368#else
18369 STRLEN(s)
18370#endif
18371 );
18372}
18373
18374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 * "strpart()" function
18376 */
18377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018378f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018379 typval_T *argvars;
18380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381{
18382 char_u *p;
18383 int n;
18384 int len;
18385 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018386 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018388 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389 slen = (int)STRLEN(p);
18390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018391 n = get_tv_number_chk(&argvars[1], &error);
18392 if (error)
18393 len = 0;
18394 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018395 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 else
18397 len = slen - n; /* default len: all bytes that are available. */
18398
18399 /*
18400 * Only return the overlap between the specified part and the actual
18401 * string.
18402 */
18403 if (n < 0)
18404 {
18405 len += n;
18406 n = 0;
18407 }
18408 else if (n > slen)
18409 n = slen;
18410 if (len < 0)
18411 len = 0;
18412 else if (n + len > slen)
18413 len = slen - n;
18414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018415 rettv->v_type = VAR_STRING;
18416 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417}
18418
18419/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018420 * "strridx()" function
18421 */
18422 static void
18423f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018424 typval_T *argvars;
18425 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018426{
18427 char_u buf[NUMBUFLEN];
18428 char_u *needle;
18429 char_u *haystack;
18430 char_u *rest;
18431 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018432 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018434 needle = get_tv_string_chk(&argvars[1]);
18435 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018436
18437 rettv->vval.v_number = -1;
18438 if (needle == NULL || haystack == NULL)
18439 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018440
18441 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018442 if (argvars[2].v_type != VAR_UNKNOWN)
18443 {
18444 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018445 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018446 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018447 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018448 }
18449 else
18450 end_idx = haystack_len;
18451
Bram Moolenaar0d660222005-01-07 21:51:51 +000018452 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018453 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018454 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018455 lastmatch = haystack + end_idx;
18456 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018457 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018458 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018459 for (rest = haystack; *rest != '\0'; ++rest)
18460 {
18461 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018462 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018463 break;
18464 lastmatch = rest;
18465 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018466 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018467
18468 if (lastmatch == NULL)
18469 rettv->vval.v_number = -1;
18470 else
18471 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18472}
18473
18474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 * "strtrans()" function
18476 */
18477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018478f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018479 typval_T *argvars;
18480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018482 rettv->v_type = VAR_STRING;
18483 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484}
18485
18486/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018487 * "submatch()" function
18488 */
18489 static void
18490f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018491 typval_T *argvars;
18492 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018493{
Bram Moolenaar41571762014-04-02 19:00:58 +020018494 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018495 int no;
18496 int retList = 0;
18497
18498 no = (int)get_tv_number_chk(&argvars[0], &error);
18499 if (error)
18500 return;
18501 error = FALSE;
18502 if (argvars[1].v_type != VAR_UNKNOWN)
18503 retList = get_tv_number_chk(&argvars[1], &error);
18504 if (error)
18505 return;
18506
18507 if (retList == 0)
18508 {
18509 rettv->v_type = VAR_STRING;
18510 rettv->vval.v_string = reg_submatch(no);
18511 }
18512 else
18513 {
18514 rettv->v_type = VAR_LIST;
18515 rettv->vval.v_list = reg_submatch_list(no);
18516 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018517}
18518
18519/*
18520 * "substitute()" function
18521 */
18522 static void
18523f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018524 typval_T *argvars;
18525 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018526{
18527 char_u patbuf[NUMBUFLEN];
18528 char_u subbuf[NUMBUFLEN];
18529 char_u flagsbuf[NUMBUFLEN];
18530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018531 char_u *str = get_tv_string_chk(&argvars[0]);
18532 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18533 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18534 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18535
Bram Moolenaar0d660222005-01-07 21:51:51 +000018536 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018537 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18538 rettv->vval.v_string = NULL;
18539 else
18540 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018541}
18542
18543/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018544 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018547f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018548 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550{
18551 int id = 0;
18552#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018553 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554 long col;
18555 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018556 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018558 lnum = get_tv_lnum(argvars); /* -1 on type error */
18559 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18560 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018562 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018563 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018564 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565#endif
18566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018567 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568}
18569
18570/*
18571 * "synIDattr(id, what [, mode])" function
18572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018574f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018575 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577{
18578 char_u *p = NULL;
18579#ifdef FEAT_SYN_HL
18580 int id;
18581 char_u *what;
18582 char_u *mode;
18583 char_u modebuf[NUMBUFLEN];
18584 int modec;
18585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018586 id = get_tv_number(&argvars[0]);
18587 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018588 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018590 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018592 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 modec = 0; /* replace invalid with current */
18594 }
18595 else
18596 {
18597#ifdef FEAT_GUI
18598 if (gui.in_use)
18599 modec = 'g';
18600 else
18601#endif
18602 if (t_colors > 1)
18603 modec = 'c';
18604 else
18605 modec = 't';
18606 }
18607
18608
18609 switch (TOLOWER_ASC(what[0]))
18610 {
18611 case 'b':
18612 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18613 p = highlight_color(id, what, modec);
18614 else /* bold */
18615 p = highlight_has_attr(id, HL_BOLD, modec);
18616 break;
18617
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018618 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 p = highlight_color(id, what, modec);
18620 break;
18621
18622 case 'i':
18623 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18624 p = highlight_has_attr(id, HL_INVERSE, modec);
18625 else /* italic */
18626 p = highlight_has_attr(id, HL_ITALIC, modec);
18627 break;
18628
18629 case 'n': /* name */
18630 p = get_highlight_name(NULL, id - 1);
18631 break;
18632
18633 case 'r': /* reverse */
18634 p = highlight_has_attr(id, HL_INVERSE, modec);
18635 break;
18636
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018637 case 's':
18638 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18639 p = highlight_color(id, what, modec);
18640 else /* standout */
18641 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642 break;
18643
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018644 case 'u':
18645 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18646 /* underline */
18647 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18648 else
18649 /* undercurl */
18650 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 break;
18652 }
18653
18654 if (p != NULL)
18655 p = vim_strsave(p);
18656#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018657 rettv->v_type = VAR_STRING;
18658 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659}
18660
18661/*
18662 * "synIDtrans(id)" function
18663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018665f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018666 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668{
18669 int id;
18670
18671#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018672 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673
18674 if (id > 0)
18675 id = syn_get_final_id(id);
18676 else
18677#endif
18678 id = 0;
18679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018680 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681}
18682
18683/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018684 * "synconcealed(lnum, col)" function
18685 */
18686 static void
18687f_synconcealed(argvars, rettv)
18688 typval_T *argvars UNUSED;
18689 typval_T *rettv;
18690{
18691#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18692 long lnum;
18693 long col;
18694 int syntax_flags = 0;
18695 int cchar;
18696 int matchid = 0;
18697 char_u str[NUMBUFLEN];
18698#endif
18699
18700 rettv->v_type = VAR_LIST;
18701 rettv->vval.v_list = NULL;
18702
18703#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18704 lnum = get_tv_lnum(argvars); /* -1 on type error */
18705 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18706
18707 vim_memset(str, NUL, sizeof(str));
18708
18709 if (rettv_list_alloc(rettv) != FAIL)
18710 {
18711 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18712 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18713 && curwin->w_p_cole > 0)
18714 {
18715 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18716 syntax_flags = get_syntax_info(&matchid);
18717
18718 /* get the conceal character */
18719 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18720 {
18721 cchar = syn_get_sub_char();
18722 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18723 cchar = lcs_conceal;
18724 if (cchar != NUL)
18725 {
18726# ifdef FEAT_MBYTE
18727 if (has_mbyte)
18728 (*mb_char2bytes)(cchar, str);
18729 else
18730# endif
18731 str[0] = cchar;
18732 }
18733 }
18734 }
18735
18736 list_append_number(rettv->vval.v_list,
18737 (syntax_flags & HL_CONCEAL) != 0);
18738 /* -1 to auto-determine strlen */
18739 list_append_string(rettv->vval.v_list, str, -1);
18740 list_append_number(rettv->vval.v_list, matchid);
18741 }
18742#endif
18743}
18744
18745/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018746 * "synstack(lnum, col)" function
18747 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018748 static void
18749f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018750 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018751 typval_T *rettv;
18752{
18753#ifdef FEAT_SYN_HL
18754 long lnum;
18755 long col;
18756 int i;
18757 int id;
18758#endif
18759
18760 rettv->v_type = VAR_LIST;
18761 rettv->vval.v_list = NULL;
18762
18763#ifdef FEAT_SYN_HL
18764 lnum = get_tv_lnum(argvars); /* -1 on type error */
18765 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18766
18767 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018768 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018769 && rettv_list_alloc(rettv) != FAIL)
18770 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018771 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018772 for (i = 0; ; ++i)
18773 {
18774 id = syn_get_stack_item(i);
18775 if (id < 0)
18776 break;
18777 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18778 break;
18779 }
18780 }
18781#endif
18782}
18783
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018785get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018786 typval_T *argvars;
18787 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018788 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018790 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018792 char_u *infile = NULL;
18793 char_u buf[NUMBUFLEN];
18794 int err = FALSE;
18795 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018796 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018797 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018799 rettv->v_type = VAR_STRING;
18800 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018801 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018802 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018803
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018804 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018805 {
18806 /*
18807 * Write the string to a temp file, to be used for input of the shell
18808 * command.
18809 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018810 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018811 {
18812 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018813 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018814 }
18815
18816 fd = mch_fopen((char *)infile, WRITEBIN);
18817 if (fd == NULL)
18818 {
18819 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018820 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018821 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018822 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018823 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018824 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18825 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018826 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018827 else
18828 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018829 size_t len;
18830
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018831 p = get_tv_string_buf_chk(&argvars[1], buf);
18832 if (p == NULL)
18833 {
18834 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018835 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018836 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018837 len = STRLEN(p);
18838 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018839 err = TRUE;
18840 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018841 if (fclose(fd) != 0)
18842 err = TRUE;
18843 if (err)
18844 {
18845 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018846 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018847 }
18848 }
18849
Bram Moolenaar52a72462014-08-29 15:53:52 +020018850 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18851 * echoes typeahead, that messes up the display. */
18852 if (!msg_silent)
18853 flags += SHELL_COOKED;
18854
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018855 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018857 int len;
18858 listitem_T *li;
18859 char_u *s = NULL;
18860 char_u *start;
18861 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018862 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863
Bram Moolenaar52a72462014-08-29 15:53:52 +020018864 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018865 if (res == NULL)
18866 goto errret;
18867
18868 list = list_alloc();
18869 if (list == NULL)
18870 goto errret;
18871
18872 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018874 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018875 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018876 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018877 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018878
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018879 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018880 if (s == NULL)
18881 goto errret;
18882
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018883 for (p = s; start < end; ++p, ++start)
18884 *p = *start == NUL ? NL : *start;
18885 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018886
18887 li = listitem_alloc();
18888 if (li == NULL)
18889 {
18890 vim_free(s);
18891 goto errret;
18892 }
18893 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010018894 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018895 li->li_tv.vval.v_string = s;
18896 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018898
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018899 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018900 rettv->v_type = VAR_LIST;
18901 rettv->vval.v_list = list;
18902 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018904 else
18905 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018906 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018907#ifdef USE_CR
18908 /* translate <CR> into <NL> */
18909 if (res != NULL)
18910 {
18911 char_u *s;
18912
18913 for (s = res; *s; ++s)
18914 {
18915 if (*s == CAR)
18916 *s = NL;
18917 }
18918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919#else
18920# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018921 /* translate <CR><NL> into <NL> */
18922 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018924 char_u *s, *d;
18925
18926 d = res;
18927 for (s = res; *s; ++s)
18928 {
18929 if (s[0] == CAR && s[1] == NL)
18930 ++s;
18931 *d++ = *s;
18932 }
18933 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935# endif
18936#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018937 rettv->vval.v_string = res;
18938 res = NULL;
18939 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018940
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018941errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018942 if (infile != NULL)
18943 {
18944 mch_remove(infile);
18945 vim_free(infile);
18946 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018947 if (res != NULL)
18948 vim_free(res);
18949 if (list != NULL)
18950 list_free(list, TRUE);
18951}
18952
18953/*
18954 * "system()" function
18955 */
18956 static void
18957f_system(argvars, rettv)
18958 typval_T *argvars;
18959 typval_T *rettv;
18960{
18961 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18962}
18963
18964/*
18965 * "systemlist()" function
18966 */
18967 static void
18968f_systemlist(argvars, rettv)
18969 typval_T *argvars;
18970 typval_T *rettv;
18971{
18972 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973}
18974
18975/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018976 * "tabpagebuflist()" function
18977 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018978 static void
18979f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018980 typval_T *argvars UNUSED;
18981 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018982{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018983#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018984 tabpage_T *tp;
18985 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018986
18987 if (argvars[0].v_type == VAR_UNKNOWN)
18988 wp = firstwin;
18989 else
18990 {
18991 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18992 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018993 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018994 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018995 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018996 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018997 for (; wp != NULL; wp = wp->w_next)
18998 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018999 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019000 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019001 }
19002#endif
19003}
19004
19005
19006/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019007 * "tabpagenr()" function
19008 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019009 static void
19010f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019011 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019012 typval_T *rettv;
19013{
19014 int nr = 1;
19015#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019016 char_u *arg;
19017
19018 if (argvars[0].v_type != VAR_UNKNOWN)
19019 {
19020 arg = get_tv_string_chk(&argvars[0]);
19021 nr = 0;
19022 if (arg != NULL)
19023 {
19024 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019025 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019026 else
19027 EMSG2(_(e_invexpr2), arg);
19028 }
19029 }
19030 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019031 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019032#endif
19033 rettv->vval.v_number = nr;
19034}
19035
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019036
19037#ifdef FEAT_WINDOWS
19038static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19039
19040/*
19041 * Common code for tabpagewinnr() and winnr().
19042 */
19043 static int
19044get_winnr(tp, argvar)
19045 tabpage_T *tp;
19046 typval_T *argvar;
19047{
19048 win_T *twin;
19049 int nr = 1;
19050 win_T *wp;
19051 char_u *arg;
19052
19053 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19054 if (argvar->v_type != VAR_UNKNOWN)
19055 {
19056 arg = get_tv_string_chk(argvar);
19057 if (arg == NULL)
19058 nr = 0; /* type error; errmsg already given */
19059 else if (STRCMP(arg, "$") == 0)
19060 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19061 else if (STRCMP(arg, "#") == 0)
19062 {
19063 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19064 if (twin == NULL)
19065 nr = 0;
19066 }
19067 else
19068 {
19069 EMSG2(_(e_invexpr2), arg);
19070 nr = 0;
19071 }
19072 }
19073
19074 if (nr > 0)
19075 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19076 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019077 {
19078 if (wp == NULL)
19079 {
19080 /* didn't find it in this tabpage */
19081 nr = 0;
19082 break;
19083 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019084 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019085 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019086 return nr;
19087}
19088#endif
19089
19090/*
19091 * "tabpagewinnr()" function
19092 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019093 static void
19094f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019095 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019096 typval_T *rettv;
19097{
19098 int nr = 1;
19099#ifdef FEAT_WINDOWS
19100 tabpage_T *tp;
19101
19102 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19103 if (tp == NULL)
19104 nr = 0;
19105 else
19106 nr = get_winnr(tp, &argvars[1]);
19107#endif
19108 rettv->vval.v_number = nr;
19109}
19110
19111
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019112/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019113 * "tagfiles()" function
19114 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019115 static void
19116f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019117 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019118 typval_T *rettv;
19119{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019120 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019121 tagname_T tn;
19122 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019123
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019124 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019125 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019126 fname = alloc(MAXPATHL);
19127 if (fname == NULL)
19128 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019129
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019130 for (first = TRUE; ; first = FALSE)
19131 if (get_tagfname(&tn, first, fname) == FAIL
19132 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019133 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019134 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019135 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019136}
19137
19138/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019139 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019140 */
19141 static void
19142f_taglist(argvars, rettv)
19143 typval_T *argvars;
19144 typval_T *rettv;
19145{
19146 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019147
19148 tag_pattern = get_tv_string(&argvars[0]);
19149
19150 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019151 if (*tag_pattern == NUL)
19152 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019153
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019154 if (rettv_list_alloc(rettv) == OK)
19155 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019156}
19157
19158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 * "tempname()" function
19160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019162f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019163 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165{
19166 static int x = 'A';
19167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019168 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019169 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170
19171 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19172 * names. Skip 'I' and 'O', they are used for shell redirection. */
19173 do
19174 {
19175 if (x == 'Z')
19176 x = '0';
19177 else if (x == '9')
19178 x = 'A';
19179 else
19180 {
19181#ifdef EBCDIC
19182 if (x == 'I')
19183 x = 'J';
19184 else if (x == 'R')
19185 x = 'S';
19186 else
19187#endif
19188 ++x;
19189 }
19190 } while (x == 'I' || x == 'O');
19191}
19192
19193/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019194 * "test(list)" function: Just checking the walls...
19195 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019196 static void
19197f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019198 typval_T *argvars UNUSED;
19199 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019200{
19201 /* Used for unit testing. Change the code below to your liking. */
19202#if 0
19203 listitem_T *li;
19204 list_T *l;
19205 char_u *bad, *good;
19206
19207 if (argvars[0].v_type != VAR_LIST)
19208 return;
19209 l = argvars[0].vval.v_list;
19210 if (l == NULL)
19211 return;
19212 li = l->lv_first;
19213 if (li == NULL)
19214 return;
19215 bad = get_tv_string(&li->li_tv);
19216 li = li->li_next;
19217 if (li == NULL)
19218 return;
19219 good = get_tv_string(&li->li_tv);
19220 rettv->vval.v_number = test_edit_score(bad, good);
19221#endif
19222}
19223
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019224#ifdef FEAT_FLOAT
19225/*
19226 * "tan()" function
19227 */
19228 static void
19229f_tan(argvars, rettv)
19230 typval_T *argvars;
19231 typval_T *rettv;
19232{
19233 float_T f;
19234
19235 rettv->v_type = VAR_FLOAT;
19236 if (get_float_arg(argvars, &f) == OK)
19237 rettv->vval.v_float = tan(f);
19238 else
19239 rettv->vval.v_float = 0.0;
19240}
19241
19242/*
19243 * "tanh()" function
19244 */
19245 static void
19246f_tanh(argvars, rettv)
19247 typval_T *argvars;
19248 typval_T *rettv;
19249{
19250 float_T f;
19251
19252 rettv->v_type = VAR_FLOAT;
19253 if (get_float_arg(argvars, &f) == OK)
19254 rettv->vval.v_float = tanh(f);
19255 else
19256 rettv->vval.v_float = 0.0;
19257}
19258#endif
19259
Bram Moolenaard52d9742005-08-21 22:20:28 +000019260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261 * "tolower(string)" function
19262 */
19263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019264f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019265 typval_T *argvars;
19266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267{
19268 char_u *p;
19269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019270 p = vim_strsave(get_tv_string(&argvars[0]));
19271 rettv->v_type = VAR_STRING;
19272 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273
19274 if (p != NULL)
19275 while (*p != NUL)
19276 {
19277#ifdef FEAT_MBYTE
19278 int l;
19279
19280 if (enc_utf8)
19281 {
19282 int c, lc;
19283
19284 c = utf_ptr2char(p);
19285 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019286 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 /* TODO: reallocate string when byte count changes. */
19288 if (utf_char2len(lc) == l)
19289 utf_char2bytes(lc, p);
19290 p += l;
19291 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019292 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 p += l; /* skip multi-byte character */
19294 else
19295#endif
19296 {
19297 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19298 ++p;
19299 }
19300 }
19301}
19302
19303/*
19304 * "toupper(string)" function
19305 */
19306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019307f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019308 typval_T *argvars;
19309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019311 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019312 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313}
19314
19315/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019316 * "tr(string, fromstr, tostr)" function
19317 */
19318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019319f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019320 typval_T *argvars;
19321 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019322{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019323 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019324 char_u *fromstr;
19325 char_u *tostr;
19326 char_u *p;
19327#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019328 int inlen;
19329 int fromlen;
19330 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019331 int idx;
19332 char_u *cpstr;
19333 int cplen;
19334 int first = TRUE;
19335#endif
19336 char_u buf[NUMBUFLEN];
19337 char_u buf2[NUMBUFLEN];
19338 garray_T ga;
19339
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019340 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019341 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19342 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019343
19344 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019345 rettv->v_type = VAR_STRING;
19346 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019347 if (fromstr == NULL || tostr == NULL)
19348 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019349 ga_init2(&ga, (int)sizeof(char), 80);
19350
19351#ifdef FEAT_MBYTE
19352 if (!has_mbyte)
19353#endif
19354 /* not multi-byte: fromstr and tostr must be the same length */
19355 if (STRLEN(fromstr) != STRLEN(tostr))
19356 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019357#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019358error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019359#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019360 EMSG2(_(e_invarg2), fromstr);
19361 ga_clear(&ga);
19362 return;
19363 }
19364
19365 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019366 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019367 {
19368#ifdef FEAT_MBYTE
19369 if (has_mbyte)
19370 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019371 inlen = (*mb_ptr2len)(in_str);
19372 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019373 cplen = inlen;
19374 idx = 0;
19375 for (p = fromstr; *p != NUL; p += fromlen)
19376 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019377 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019378 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019379 {
19380 for (p = tostr; *p != NUL; p += tolen)
19381 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019382 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019383 if (idx-- == 0)
19384 {
19385 cplen = tolen;
19386 cpstr = p;
19387 break;
19388 }
19389 }
19390 if (*p == NUL) /* tostr is shorter than fromstr */
19391 goto error;
19392 break;
19393 }
19394 ++idx;
19395 }
19396
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019397 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019398 {
19399 /* Check that fromstr and tostr have the same number of
19400 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019401 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019402 first = FALSE;
19403 for (p = tostr; *p != NUL; p += tolen)
19404 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019405 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019406 --idx;
19407 }
19408 if (idx != 0)
19409 goto error;
19410 }
19411
19412 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019413 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019414 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019415
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019416 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019417 }
19418 else
19419#endif
19420 {
19421 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019422 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019423 if (p != NULL)
19424 ga_append(&ga, tostr[p - fromstr]);
19425 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019426 ga_append(&ga, *in_str);
19427 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019428 }
19429 }
19430
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019431 /* add a terminating NUL */
19432 ga_grow(&ga, 1);
19433 ga_append(&ga, NUL);
19434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019435 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019436}
19437
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019438#ifdef FEAT_FLOAT
19439/*
19440 * "trunc({float})" function
19441 */
19442 static void
19443f_trunc(argvars, rettv)
19444 typval_T *argvars;
19445 typval_T *rettv;
19446{
19447 float_T f;
19448
19449 rettv->v_type = VAR_FLOAT;
19450 if (get_float_arg(argvars, &f) == OK)
19451 /* trunc() is not in C90, use floor() or ceil() instead. */
19452 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19453 else
19454 rettv->vval.v_float = 0.0;
19455}
19456#endif
19457
Bram Moolenaar8299df92004-07-10 09:47:34 +000019458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 * "type(expr)" function
19460 */
19461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019462f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019463 typval_T *argvars;
19464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019466 int n;
19467
19468 switch (argvars[0].v_type)
19469 {
19470 case VAR_NUMBER: n = 0; break;
19471 case VAR_STRING: n = 1; break;
19472 case VAR_FUNC: n = 2; break;
19473 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019474 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019475#ifdef FEAT_FLOAT
19476 case VAR_FLOAT: n = 5; break;
19477#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019478 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19479 }
19480 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481}
19482
19483/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019484 * "undofile(name)" function
19485 */
19486 static void
19487f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019488 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019489 typval_T *rettv;
19490{
19491 rettv->v_type = VAR_STRING;
19492#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019493 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019494 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019495
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019496 if (*fname == NUL)
19497 {
19498 /* If there is no file name there will be no undo file. */
19499 rettv->vval.v_string = NULL;
19500 }
19501 else
19502 {
19503 char_u *ffname = FullName_save(fname, FALSE);
19504
19505 if (ffname != NULL)
19506 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19507 vim_free(ffname);
19508 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019509 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019510#else
19511 rettv->vval.v_string = NULL;
19512#endif
19513}
19514
19515/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019516 * "undotree()" function
19517 */
19518 static void
19519f_undotree(argvars, rettv)
19520 typval_T *argvars UNUSED;
19521 typval_T *rettv;
19522{
19523 if (rettv_dict_alloc(rettv) == OK)
19524 {
19525 dict_T *dict = rettv->vval.v_dict;
19526 list_T *list;
19527
Bram Moolenaar730cde92010-06-27 05:18:54 +020019528 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019529 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019530 dict_add_nr_str(dict, "save_last",
19531 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019532 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19533 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019534 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019535
19536 list = list_alloc();
19537 if (list != NULL)
19538 {
19539 u_eval_tree(curbuf->b_u_oldhead, list);
19540 dict_add_list(dict, "entries", list);
19541 }
19542 }
19543}
19544
19545/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019546 * "values(dict)" function
19547 */
19548 static void
19549f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019550 typval_T *argvars;
19551 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019552{
19553 dict_list(argvars, rettv, 1);
19554}
19555
19556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 * "virtcol(string)" function
19558 */
19559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019560f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019561 typval_T *argvars;
19562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563{
19564 colnr_T vcol = 0;
19565 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019566 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019568 fp = var2fpos(&argvars[0], FALSE, &fnum);
19569 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19570 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 {
19572 getvvcol(curwin, fp, NULL, NULL, &vcol);
19573 ++vcol;
19574 }
19575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019576 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577}
19578
19579/*
19580 * "visualmode()" function
19581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019583f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019584 typval_T *argvars UNUSED;
19585 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 char_u str[2];
19588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019589 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 str[0] = curbuf->b_visual_mode_eval;
19591 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019592 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593
19594 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019595 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597}
19598
19599/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019600 * "wildmenumode()" function
19601 */
19602 static void
19603f_wildmenumode(argvars, rettv)
19604 typval_T *argvars UNUSED;
19605 typval_T *rettv UNUSED;
19606{
19607#ifdef FEAT_WILDMENU
19608 if (wild_menu_showing)
19609 rettv->vval.v_number = 1;
19610#endif
19611}
19612
19613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 * "winbufnr(nr)" function
19615 */
19616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019617f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 typval_T *argvars;
19619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620{
19621 win_T *wp;
19622
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019623 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019625 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019627 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628}
19629
19630/*
19631 * "wincol()" function
19632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019634f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637{
19638 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019639 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640}
19641
19642/*
19643 * "winheight(nr)" function
19644 */
19645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019646f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 typval_T *argvars;
19648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649{
19650 win_T *wp;
19651
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019652 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019654 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019656 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657}
19658
19659/*
19660 * "winline()" function
19661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019663f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019664 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666{
19667 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019668 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669}
19670
19671/*
19672 * "winnr()" function
19673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019675f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019676 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678{
19679 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019680
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019682 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019684 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685}
19686
19687/*
19688 * "winrestcmd()" function
19689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019691f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019692 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694{
19695#ifdef FEAT_WINDOWS
19696 win_T *wp;
19697 int winnr = 1;
19698 garray_T ga;
19699 char_u buf[50];
19700
19701 ga_init2(&ga, (int)sizeof(char), 70);
19702 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19703 {
19704 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19705 ga_concat(&ga, buf);
19706# ifdef FEAT_VERTSPLIT
19707 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19708 ga_concat(&ga, buf);
19709# endif
19710 ++winnr;
19711 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019712 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019714 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019716 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019718 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719}
19720
19721/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019722 * "winrestview()" function
19723 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019724 static void
19725f_winrestview(argvars, rettv)
19726 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019727 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019728{
19729 dict_T *dict;
19730
19731 if (argvars[0].v_type != VAR_DICT
19732 || (dict = argvars[0].vval.v_dict) == NULL)
19733 EMSG(_(e_invarg));
19734 else
19735 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019736 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19737 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19738 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19739 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019740#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019741 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19742 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019743#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019744 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19745 {
19746 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19747 curwin->w_set_curswant = FALSE;
19748 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019749
Bram Moolenaar82c25852014-05-28 16:47:16 +020019750 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19751 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019752#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019753 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19754 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019755#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019756 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19757 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19758 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19759 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019760
19761 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019762 win_new_height(curwin, curwin->w_height);
19763# ifdef FEAT_VERTSPLIT
19764 win_new_width(curwin, W_WIDTH(curwin));
19765# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019766 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019767
Bram Moolenaarb851a962014-10-31 15:45:52 +010019768 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019769 curwin->w_topline = 1;
19770 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19771 curwin->w_topline = curbuf->b_ml.ml_line_count;
19772#ifdef FEAT_DIFF
19773 check_topfill(curwin, TRUE);
19774#endif
19775 }
19776}
19777
19778/*
19779 * "winsaveview()" function
19780 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019781 static void
19782f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019783 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019784 typval_T *rettv;
19785{
19786 dict_T *dict;
19787
Bram Moolenaara800b422010-06-27 01:15:55 +020019788 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019789 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019790 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019791
19792 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19793 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19794#ifdef FEAT_VIRTUALEDIT
19795 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19796#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019797 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019798 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19799
19800 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19801#ifdef FEAT_DIFF
19802 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19803#endif
19804 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19805 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19806}
19807
19808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 * "winwidth(nr)" function
19810 */
19811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019812f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019813 typval_T *argvars;
19814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815{
19816 win_T *wp;
19817
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019818 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019820 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 else
19822#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019823 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826#endif
19827}
19828
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019830 * Write list of strings to file
19831 */
19832 static int
19833write_list(fd, list, binary)
19834 FILE *fd;
19835 list_T *list;
19836 int binary;
19837{
19838 listitem_T *li;
19839 int c;
19840 int ret = OK;
19841 char_u *s;
19842
19843 for (li = list->lv_first; li != NULL; li = li->li_next)
19844 {
19845 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19846 {
19847 if (*s == '\n')
19848 c = putc(NUL, fd);
19849 else
19850 c = putc(*s, fd);
19851 if (c == EOF)
19852 {
19853 ret = FAIL;
19854 break;
19855 }
19856 }
19857 if (!binary || li->li_next != NULL)
19858 if (putc('\n', fd) == EOF)
19859 {
19860 ret = FAIL;
19861 break;
19862 }
19863 if (ret == FAIL)
19864 {
19865 EMSG(_(e_write));
19866 break;
19867 }
19868 }
19869 return ret;
19870}
19871
19872/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019873 * "writefile()" function
19874 */
19875 static void
19876f_writefile(argvars, rettv)
19877 typval_T *argvars;
19878 typval_T *rettv;
19879{
19880 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019881 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019882 char_u *fname;
19883 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019884 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019885
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019886 if (check_restricted() || check_secure())
19887 return;
19888
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019889 if (argvars[0].v_type != VAR_LIST)
19890 {
19891 EMSG2(_(e_listarg), "writefile()");
19892 return;
19893 }
19894 if (argvars[0].vval.v_list == NULL)
19895 return;
19896
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019897 if (argvars[2].v_type != VAR_UNKNOWN)
19898 {
19899 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19900 binary = TRUE;
19901 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19902 append = TRUE;
19903 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019904
19905 /* Always open the file in binary mode, library functions have a mind of
19906 * their own about CR-LF conversion. */
19907 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019908 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19909 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019910 {
19911 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19912 ret = -1;
19913 }
19914 else
19915 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019916 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19917 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019918 fclose(fd);
19919 }
19920
19921 rettv->vval.v_number = ret;
19922}
19923
19924/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019925 * "xor(expr, expr)" function
19926 */
19927 static void
19928f_xor(argvars, rettv)
19929 typval_T *argvars;
19930 typval_T *rettv;
19931{
19932 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19933 ^ get_tv_number_chk(&argvars[1], NULL);
19934}
19935
19936
19937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019939 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 */
19941 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019942var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019943 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019944 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019945 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019947 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019949 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950
Bram Moolenaara5525202006-03-02 22:52:09 +000019951 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019952 if (varp->v_type == VAR_LIST)
19953 {
19954 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019955 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019956 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019957 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019958
19959 l = varp->vval.v_list;
19960 if (l == NULL)
19961 return NULL;
19962
19963 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019964 pos.lnum = list_find_nr(l, 0L, &error);
19965 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019966 return NULL; /* invalid line number */
19967
19968 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019969 pos.col = list_find_nr(l, 1L, &error);
19970 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019971 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019972 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019973
19974 /* We accept "$" for the column number: last column. */
19975 li = list_find(l, 1L);
19976 if (li != NULL && li->li_tv.v_type == VAR_STRING
19977 && li->li_tv.vval.v_string != NULL
19978 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19979 pos.col = len + 1;
19980
Bram Moolenaara5525202006-03-02 22:52:09 +000019981 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019982 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019983 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019984 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019985
Bram Moolenaara5525202006-03-02 22:52:09 +000019986#ifdef FEAT_VIRTUALEDIT
19987 /* Get the virtual offset. Defaults to zero. */
19988 pos.coladd = list_find_nr(l, 2L, &error);
19989 if (error)
19990 pos.coladd = 0;
19991#endif
19992
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019993 return &pos;
19994 }
19995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019996 name = get_tv_string_chk(varp);
19997 if (name == NULL)
19998 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019999 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020001 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20002 {
20003 if (VIsual_active)
20004 return &VIsual;
20005 return &curwin->w_cursor;
20006 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020007 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020009 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20011 return NULL;
20012 return pp;
20013 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020014
20015#ifdef FEAT_VIRTUALEDIT
20016 pos.coladd = 0;
20017#endif
20018
Bram Moolenaar477933c2007-07-17 14:32:23 +000020019 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020020 {
20021 pos.col = 0;
20022 if (name[1] == '0') /* "w0": first visible line */
20023 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020024 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020025 pos.lnum = curwin->w_topline;
20026 return &pos;
20027 }
20028 else if (name[1] == '$') /* "w$": last visible line */
20029 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020030 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020031 pos.lnum = curwin->w_botline - 1;
20032 return &pos;
20033 }
20034 }
20035 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020037 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 {
20039 pos.lnum = curbuf->b_ml.ml_line_count;
20040 pos.col = 0;
20041 }
20042 else
20043 {
20044 pos.lnum = curwin->w_cursor.lnum;
20045 pos.col = (colnr_T)STRLEN(ml_get_curline());
20046 }
20047 return &pos;
20048 }
20049 return NULL;
20050}
20051
20052/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020053 * Convert list in "arg" into a position and optional file number.
20054 * When "fnump" is NULL there is no file number, only 3 items.
20055 * Note that the column is passed on as-is, the caller may want to decrement
20056 * it to use 1 for the first column.
20057 * Return FAIL when conversion is not possible, doesn't check the position for
20058 * validity.
20059 */
20060 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020061list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020062 typval_T *arg;
20063 pos_T *posp;
20064 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020065 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020066{
20067 list_T *l = arg->vval.v_list;
20068 long i = 0;
20069 long n;
20070
Bram Moolenaar493c1782014-05-28 14:34:46 +020020071 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20072 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020073 if (arg->v_type != VAR_LIST
20074 || l == NULL
20075 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020076 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020077 return FAIL;
20078
20079 if (fnump != NULL)
20080 {
20081 n = list_find_nr(l, i++, NULL); /* fnum */
20082 if (n < 0)
20083 return FAIL;
20084 if (n == 0)
20085 n = curbuf->b_fnum; /* current buffer */
20086 *fnump = n;
20087 }
20088
20089 n = list_find_nr(l, i++, NULL); /* lnum */
20090 if (n < 0)
20091 return FAIL;
20092 posp->lnum = n;
20093
20094 n = list_find_nr(l, i++, NULL); /* col */
20095 if (n < 0)
20096 return FAIL;
20097 posp->col = n;
20098
20099#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020100 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020101 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020102 posp->coladd = 0;
20103 else
20104 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020105#endif
20106
Bram Moolenaar493c1782014-05-28 14:34:46 +020020107 if (curswantp != NULL)
20108 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20109
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020110 return OK;
20111}
20112
20113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 * Get the length of an environment variable name.
20115 * Advance "arg" to the first character after the name.
20116 * Return 0 for error.
20117 */
20118 static int
20119get_env_len(arg)
20120 char_u **arg;
20121{
20122 char_u *p;
20123 int len;
20124
20125 for (p = *arg; vim_isIDc(*p); ++p)
20126 ;
20127 if (p == *arg) /* no name found */
20128 return 0;
20129
20130 len = (int)(p - *arg);
20131 *arg = p;
20132 return len;
20133}
20134
20135/*
20136 * Get the length of the name of a function or internal variable.
20137 * "arg" is advanced to the first non-white character after the name.
20138 * Return 0 if something is wrong.
20139 */
20140 static int
20141get_id_len(arg)
20142 char_u **arg;
20143{
20144 char_u *p;
20145 int len;
20146
20147 /* Find the end of the name. */
20148 for (p = *arg; eval_isnamec(*p); ++p)
20149 ;
20150 if (p == *arg) /* no name found */
20151 return 0;
20152
20153 len = (int)(p - *arg);
20154 *arg = skipwhite(p);
20155
20156 return len;
20157}
20158
20159/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020160 * Get the length of the name of a variable or function.
20161 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020163 * Return -1 if curly braces expansion failed.
20164 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 * If the name contains 'magic' {}'s, expand them and return the
20166 * expanded name in an allocated string via 'alias' - caller must free.
20167 */
20168 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020169get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 char_u **arg;
20171 char_u **alias;
20172 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020173 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174{
20175 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 char_u *p;
20177 char_u *expr_start;
20178 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179
20180 *alias = NULL; /* default to no alias */
20181
20182 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20183 && (*arg)[2] == (int)KE_SNR)
20184 {
20185 /* hard coded <SNR>, already translated */
20186 *arg += 3;
20187 return get_id_len(arg) + 3;
20188 }
20189 len = eval_fname_script(*arg);
20190 if (len > 0)
20191 {
20192 /* literal "<SID>", "s:" or "<SNR>" */
20193 *arg += len;
20194 }
20195
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020197 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020199 p = find_name_end(*arg, &expr_start, &expr_end,
20200 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 if (expr_start != NULL)
20202 {
20203 char_u *temp_string;
20204
20205 if (!evaluate)
20206 {
20207 len += (int)(p - *arg);
20208 *arg = skipwhite(p);
20209 return len;
20210 }
20211
20212 /*
20213 * Include any <SID> etc in the expanded string:
20214 * Thus the -len here.
20215 */
20216 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20217 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020218 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 *alias = temp_string;
20220 *arg = skipwhite(p);
20221 return (int)STRLEN(temp_string);
20222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223
20224 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020225 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 EMSG2(_(e_invexpr2), *arg);
20227
20228 return len;
20229}
20230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020231/*
20232 * Find the end of a variable or function name, taking care of magic braces.
20233 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20234 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020235 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020236 * Return a pointer to just after the name. Equal to "arg" if there is no
20237 * valid name.
20238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020240find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 char_u *arg;
20242 char_u **expr_start;
20243 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020244 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020246 int mb_nest = 0;
20247 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 char_u *p;
20249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020250 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020252 *expr_start = NULL;
20253 *expr_end = NULL;
20254 }
20255
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020256 /* Quick check for valid starting character. */
20257 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20258 return arg;
20259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020260 for (p = arg; *p != NUL
20261 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020262 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020263 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020264 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020265 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020266 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020267 if (*p == '\'')
20268 {
20269 /* skip over 'string' to avoid counting [ and ] inside it. */
20270 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20271 ;
20272 if (*p == NUL)
20273 break;
20274 }
20275 else if (*p == '"')
20276 {
20277 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20278 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20279 if (*p == '\\' && p[1] != NUL)
20280 ++p;
20281 if (*p == NUL)
20282 break;
20283 }
20284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020285 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020287 if (*p == '[')
20288 ++br_nest;
20289 else if (*p == ']')
20290 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020293 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020295 if (*p == '{')
20296 {
20297 mb_nest++;
20298 if (expr_start != NULL && *expr_start == NULL)
20299 *expr_start = p;
20300 }
20301 else if (*p == '}')
20302 {
20303 mb_nest--;
20304 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20305 *expr_end = p;
20306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 }
20309
20310 return p;
20311}
20312
20313/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020314 * Expands out the 'magic' {}'s in a variable/function name.
20315 * Note that this can call itself recursively, to deal with
20316 * constructs like foo{bar}{baz}{bam}
20317 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20318 * "in_start" ^
20319 * "expr_start" ^
20320 * "expr_end" ^
20321 * "in_end" ^
20322 *
20323 * Returns a new allocated string, which the caller must free.
20324 * Returns NULL for failure.
20325 */
20326 static char_u *
20327make_expanded_name(in_start, expr_start, expr_end, in_end)
20328 char_u *in_start;
20329 char_u *expr_start;
20330 char_u *expr_end;
20331 char_u *in_end;
20332{
20333 char_u c1;
20334 char_u *retval = NULL;
20335 char_u *temp_result;
20336 char_u *nextcmd = NULL;
20337
20338 if (expr_end == NULL || in_end == NULL)
20339 return NULL;
20340 *expr_start = NUL;
20341 *expr_end = NUL;
20342 c1 = *in_end;
20343 *in_end = NUL;
20344
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020345 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020346 if (temp_result != NULL && nextcmd == NULL)
20347 {
20348 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20349 + (in_end - expr_end) + 1));
20350 if (retval != NULL)
20351 {
20352 STRCPY(retval, in_start);
20353 STRCAT(retval, temp_result);
20354 STRCAT(retval, expr_end + 1);
20355 }
20356 }
20357 vim_free(temp_result);
20358
20359 *in_end = c1; /* put char back for error messages */
20360 *expr_start = '{';
20361 *expr_end = '}';
20362
20363 if (retval != NULL)
20364 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020365 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020366 if (expr_start != NULL)
20367 {
20368 /* Further expansion! */
20369 temp_result = make_expanded_name(retval, expr_start,
20370 expr_end, temp_result);
20371 vim_free(retval);
20372 retval = temp_result;
20373 }
20374 }
20375
20376 return retval;
20377}
20378
20379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020381 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 */
20383 static int
20384eval_isnamec(c)
20385 int c;
20386{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020387 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20388}
20389
20390/*
20391 * Return TRUE if character "c" can be used as the first character in a
20392 * variable or function name (excluding '{' and '}').
20393 */
20394 static int
20395eval_isnamec1(c)
20396 int c;
20397{
20398 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399}
20400
20401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402 * Set number v: variable to "val".
20403 */
20404 void
20405set_vim_var_nr(idx, val)
20406 int idx;
20407 long val;
20408{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020409 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410}
20411
20412/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020413 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414 */
20415 long
20416get_vim_var_nr(idx)
20417 int idx;
20418{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020419 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420}
20421
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020422/*
20423 * Get string v: variable value. Uses a static buffer, can only be used once.
20424 */
20425 char_u *
20426get_vim_var_str(idx)
20427 int idx;
20428{
20429 return get_tv_string(&vimvars[idx].vv_tv);
20430}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020431
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020433 * Get List v: variable value. Caller must take care of reference count when
20434 * needed.
20435 */
20436 list_T *
20437get_vim_var_list(idx)
20438 int idx;
20439{
20440 return vimvars[idx].vv_list;
20441}
20442
20443/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020444 * Set v:char to character "c".
20445 */
20446 void
20447set_vim_var_char(c)
20448 int c;
20449{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020450 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020451
20452#ifdef FEAT_MBYTE
20453 if (has_mbyte)
20454 buf[(*mb_char2bytes)(c, buf)] = NUL;
20455 else
20456#endif
20457 {
20458 buf[0] = c;
20459 buf[1] = NUL;
20460 }
20461 set_vim_var_string(VV_CHAR, buf, -1);
20462}
20463
20464/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020465 * Set v:count to "count" and v:count1 to "count1".
20466 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 */
20468 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020469set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 long count;
20471 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020472 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020474 if (set_prevcount)
20475 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020476 vimvars[VV_COUNT].vv_nr = count;
20477 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478}
20479
20480/*
20481 * Set string v: variable to a copy of "val".
20482 */
20483 void
20484set_vim_var_string(idx, val, len)
20485 int idx;
20486 char_u *val;
20487 int len; /* length of "val" to use or -1 (whole string) */
20488{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020489 /* Need to do this (at least) once, since we can't initialize a union.
20490 * Will always be invoked when "v:progname" is set. */
20491 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20492
Bram Moolenaare9a41262005-01-15 22:18:47 +000020493 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020495 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020497 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020499 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500}
20501
20502/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020503 * Set List v: variable to "val".
20504 */
20505 void
20506set_vim_var_list(idx, val)
20507 int idx;
20508 list_T *val;
20509{
20510 list_unref(vimvars[idx].vv_list);
20511 vimvars[idx].vv_list = val;
20512 if (val != NULL)
20513 ++val->lv_refcount;
20514}
20515
20516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 * Set v:register if needed.
20518 */
20519 void
20520set_reg_var(c)
20521 int c;
20522{
20523 char_u regname;
20524
20525 if (c == 0 || c == ' ')
20526 regname = '"';
20527 else
20528 regname = c;
20529 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020530 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 set_vim_var_string(VV_REG, &regname, 1);
20532}
20533
20534/*
20535 * Get or set v:exception. If "oldval" == NULL, return the current value.
20536 * Otherwise, restore the value to "oldval" and return NULL.
20537 * Must always be called in pairs to save and restore v:exception! Does not
20538 * take care of memory allocations.
20539 */
20540 char_u *
20541v_exception(oldval)
20542 char_u *oldval;
20543{
20544 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020545 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546
Bram Moolenaare9a41262005-01-15 22:18:47 +000020547 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548 return NULL;
20549}
20550
20551/*
20552 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20553 * Otherwise, restore the value to "oldval" and return NULL.
20554 * Must always be called in pairs to save and restore v:throwpoint! Does not
20555 * take care of memory allocations.
20556 */
20557 char_u *
20558v_throwpoint(oldval)
20559 char_u *oldval;
20560{
20561 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020562 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563
Bram Moolenaare9a41262005-01-15 22:18:47 +000020564 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 return NULL;
20566}
20567
20568#if defined(FEAT_AUTOCMD) || defined(PROTO)
20569/*
20570 * Set v:cmdarg.
20571 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20572 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20573 * Must always be called in pairs!
20574 */
20575 char_u *
20576set_cmdarg(eap, oldarg)
20577 exarg_T *eap;
20578 char_u *oldarg;
20579{
20580 char_u *oldval;
20581 char_u *newval;
20582 unsigned len;
20583
Bram Moolenaare9a41262005-01-15 22:18:47 +000020584 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020585 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020587 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020588 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020589 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 }
20591
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020592 if (eap->force_bin == FORCE_BIN)
20593 len = 6;
20594 else if (eap->force_bin == FORCE_NOBIN)
20595 len = 8;
20596 else
20597 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020598
20599 if (eap->read_edit)
20600 len += 7;
20601
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020602 if (eap->force_ff != 0)
20603 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20604# ifdef FEAT_MBYTE
20605 if (eap->force_enc != 0)
20606 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020607 if (eap->bad_char != 0)
20608 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020609# endif
20610
20611 newval = alloc(len + 1);
20612 if (newval == NULL)
20613 return NULL;
20614
20615 if (eap->force_bin == FORCE_BIN)
20616 sprintf((char *)newval, " ++bin");
20617 else if (eap->force_bin == FORCE_NOBIN)
20618 sprintf((char *)newval, " ++nobin");
20619 else
20620 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020621
20622 if (eap->read_edit)
20623 STRCAT(newval, " ++edit");
20624
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020625 if (eap->force_ff != 0)
20626 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20627 eap->cmd + eap->force_ff);
20628# ifdef FEAT_MBYTE
20629 if (eap->force_enc != 0)
20630 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20631 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020632 if (eap->bad_char == BAD_KEEP)
20633 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20634 else if (eap->bad_char == BAD_DROP)
20635 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20636 else if (eap->bad_char != 0)
20637 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020638# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020639 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020640 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641}
20642#endif
20643
20644/*
20645 * Get the value of internal variable "name".
20646 * Return OK or FAIL.
20647 */
20648 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020649get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 char_u *name;
20651 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020652 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020653 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020654 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655{
20656 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020657 typval_T *tv = NULL;
20658 typval_T atv;
20659 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661
20662 /* truncate the name, so that we can use strcmp() */
20663 cc = name[len];
20664 name[len] = NUL;
20665
20666 /*
20667 * Check for "b:changedtick".
20668 */
20669 if (STRCMP(name, "b:changedtick") == 0)
20670 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020671 atv.v_type = VAR_NUMBER;
20672 atv.vval.v_number = curbuf->b_changedtick;
20673 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674 }
20675
20676 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677 * Check for user-defined variables.
20678 */
20679 else
20680 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020681 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020683 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 }
20685
Bram Moolenaare9a41262005-01-15 22:18:47 +000020686 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020688 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020689 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690 ret = FAIL;
20691 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020692 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020693 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694
20695 name[len] = cc;
20696
20697 return ret;
20698}
20699
20700/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020701 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20702 * Also handle function call with Funcref variable: func(expr)
20703 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20704 */
20705 static int
20706handle_subscript(arg, rettv, evaluate, verbose)
20707 char_u **arg;
20708 typval_T *rettv;
20709 int evaluate; /* do more than finding the end */
20710 int verbose; /* give error messages */
20711{
20712 int ret = OK;
20713 dict_T *selfdict = NULL;
20714 char_u *s;
20715 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020716 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020717
20718 while (ret == OK
20719 && (**arg == '['
20720 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020721 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020722 && !vim_iswhite(*(*arg - 1)))
20723 {
20724 if (**arg == '(')
20725 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020726 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020727 if (evaluate)
20728 {
20729 functv = *rettv;
20730 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020731
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020732 /* Invoke the function. Recursive! */
20733 s = functv.vval.v_string;
20734 }
20735 else
20736 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020737 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020738 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20739 &len, evaluate, selfdict);
20740
20741 /* Clear the funcref afterwards, so that deleting it while
20742 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020743 if (evaluate)
20744 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020745
20746 /* Stop the expression evaluation when immediately aborting on
20747 * error, or when an interrupt occurred or an exception was thrown
20748 * but not caught. */
20749 if (aborting())
20750 {
20751 if (ret == OK)
20752 clear_tv(rettv);
20753 ret = FAIL;
20754 }
20755 dict_unref(selfdict);
20756 selfdict = NULL;
20757 }
20758 else /* **arg == '[' || **arg == '.' */
20759 {
20760 dict_unref(selfdict);
20761 if (rettv->v_type == VAR_DICT)
20762 {
20763 selfdict = rettv->vval.v_dict;
20764 if (selfdict != NULL)
20765 ++selfdict->dv_refcount;
20766 }
20767 else
20768 selfdict = NULL;
20769 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20770 {
20771 clear_tv(rettv);
20772 ret = FAIL;
20773 }
20774 }
20775 }
20776 dict_unref(selfdict);
20777 return ret;
20778}
20779
20780/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020781 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020782 * value).
20783 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020784 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020785alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020786{
Bram Moolenaar33570922005-01-25 22:26:29 +000020787 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020788}
20789
20790/*
20791 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 * The string "s" must have been allocated, it is consumed.
20793 * Return NULL for out of memory, the variable otherwise.
20794 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020795 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020796alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 char_u *s;
20798{
Bram Moolenaar33570922005-01-25 22:26:29 +000020799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020801 rettv = alloc_tv();
20802 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020804 rettv->v_type = VAR_STRING;
20805 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 }
20807 else
20808 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020809 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810}
20811
20812/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020813 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020815 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020816free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020817 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818{
20819 if (varp != NULL)
20820 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020821 switch (varp->v_type)
20822 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020823 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020824 func_unref(varp->vval.v_string);
20825 /*FALLTHROUGH*/
20826 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020827 vim_free(varp->vval.v_string);
20828 break;
20829 case VAR_LIST:
20830 list_unref(varp->vval.v_list);
20831 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020832 case VAR_DICT:
20833 dict_unref(varp->vval.v_dict);
20834 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020835 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020836#ifdef FEAT_FLOAT
20837 case VAR_FLOAT:
20838#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020839 case VAR_UNKNOWN:
20840 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020841 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020842 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020843 break;
20844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 vim_free(varp);
20846 }
20847}
20848
20849/*
20850 * Free the memory for a variable value and set the value to NULL or 0.
20851 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020852 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020853clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020854 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855{
20856 if (varp != NULL)
20857 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020858 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020860 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020861 func_unref(varp->vval.v_string);
20862 /*FALLTHROUGH*/
20863 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020864 vim_free(varp->vval.v_string);
20865 varp->vval.v_string = NULL;
20866 break;
20867 case VAR_LIST:
20868 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020869 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020870 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020871 case VAR_DICT:
20872 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020873 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020874 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020875 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020876 varp->vval.v_number = 0;
20877 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020878#ifdef FEAT_FLOAT
20879 case VAR_FLOAT:
20880 varp->vval.v_float = 0.0;
20881 break;
20882#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020883 case VAR_UNKNOWN:
20884 break;
20885 default:
20886 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020888 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 }
20890}
20891
20892/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020893 * Set the value of a variable to NULL without freeing items.
20894 */
20895 static void
20896init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020897 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020898{
20899 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020900 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020901}
20902
20903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 * Get the number value of a variable.
20905 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020906 * For incompatible types, return 0.
20907 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20908 * caller of incompatible types: it sets *denote to TRUE if "denote"
20909 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 */
20911 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020912get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020913 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020915 int error = FALSE;
20916
20917 return get_tv_number_chk(varp, &error); /* return 0L on error */
20918}
20919
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020920 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020921get_tv_number_chk(varp, denote)
20922 typval_T *varp;
20923 int *denote;
20924{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020925 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020927 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020929 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020930 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020931#ifdef FEAT_FLOAT
20932 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020933 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020934 break;
20935#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020936 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020937 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020938 break;
20939 case VAR_STRING:
20940 if (varp->vval.v_string != NULL)
20941 vim_str2nr(varp->vval.v_string, NULL, NULL,
20942 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020943 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020944 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020945 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020946 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020947 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020948 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020949 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020950 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020951 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020952 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020954 if (denote == NULL) /* useful for values that must be unsigned */
20955 n = -1;
20956 else
20957 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020958 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959}
20960
20961/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020962 * Get the lnum from the first argument.
20963 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020964 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 */
20966 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020967get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020968 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969{
Bram Moolenaar33570922005-01-25 22:26:29 +000020970 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 linenr_T lnum;
20972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020973 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 if (lnum == 0) /* no valid number, try using line() */
20975 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020976 rettv.v_type = VAR_NUMBER;
20977 f_line(argvars, &rettv);
20978 lnum = rettv.vval.v_number;
20979 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 }
20981 return lnum;
20982}
20983
20984/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020985 * Get the lnum from the first argument.
20986 * Also accepts "$", then "buf" is used.
20987 * Returns 0 on error.
20988 */
20989 static linenr_T
20990get_tv_lnum_buf(argvars, buf)
20991 typval_T *argvars;
20992 buf_T *buf;
20993{
20994 if (argvars[0].v_type == VAR_STRING
20995 && argvars[0].vval.v_string != NULL
20996 && argvars[0].vval.v_string[0] == '$'
20997 && buf != NULL)
20998 return buf->b_ml.ml_line_count;
20999 return get_tv_number_chk(&argvars[0], NULL);
21000}
21001
21002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003 * Get the string value of a variable.
21004 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021005 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21006 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 * If the String variable has never been set, return an empty string.
21008 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021009 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21010 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 */
21012 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021013get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021014 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015{
21016 static char_u mybuf[NUMBUFLEN];
21017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021018 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019}
21020
21021 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021022get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021023 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021024 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021026 char_u *res = get_tv_string_buf_chk(varp, buf);
21027
21028 return res != NULL ? res : (char_u *)"";
21029}
21030
Bram Moolenaar7d647822014-04-05 21:28:56 +020021031/*
21032 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21033 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021034 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021035get_tv_string_chk(varp)
21036 typval_T *varp;
21037{
21038 static char_u mybuf[NUMBUFLEN];
21039
21040 return get_tv_string_buf_chk(varp, mybuf);
21041}
21042
21043 static char_u *
21044get_tv_string_buf_chk(varp, buf)
21045 typval_T *varp;
21046 char_u *buf;
21047{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021048 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021049 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021050 case VAR_NUMBER:
21051 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21052 return buf;
21053 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021054 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021055 break;
21056 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021057 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021058 break;
21059 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021060 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021061 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021062#ifdef FEAT_FLOAT
21063 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021064 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021065 break;
21066#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021067 case VAR_STRING:
21068 if (varp->vval.v_string != NULL)
21069 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021070 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021071 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021072 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021073 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021075 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076}
21077
21078/*
21079 * Find variable "name" in the list of variables.
21080 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021081 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021082 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021083 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021085 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021086find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021088 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021089 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021092 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093
Bram Moolenaara7043832005-01-21 11:56:39 +000021094 ht = find_var_ht(name, &varname);
21095 if (htp != NULL)
21096 *htp = ht;
21097 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021099 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100}
21101
21102/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021103 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021104 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021106 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021107find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021108 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021109 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021110 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021111 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021112{
Bram Moolenaar33570922005-01-25 22:26:29 +000021113 hashitem_T *hi;
21114
21115 if (*varname == NUL)
21116 {
21117 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021118 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021119 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021120 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021121 case 'g': return &globvars_var;
21122 case 'v': return &vimvars_var;
21123 case 'b': return &curbuf->b_bufvar;
21124 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021125#ifdef FEAT_WINDOWS
21126 case 't': return &curtab->tp_winvar;
21127#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021128 case 'l': return current_funccal == NULL
21129 ? NULL : &current_funccal->l_vars_var;
21130 case 'a': return current_funccal == NULL
21131 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021132 }
21133 return NULL;
21134 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021135
21136 hi = hash_find(ht, varname);
21137 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021138 {
21139 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021140 * worked find the variable again. Don't auto-load a script if it was
21141 * loaded already, otherwise it would be loaded every time when
21142 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021143 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021144 {
21145 /* Note: script_autoload() may make "hi" invalid. It must either
21146 * be obtained again or not used. */
21147 if (!script_autoload(varname, FALSE) || aborting())
21148 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021149 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021150 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021151 if (HASHITEM_EMPTY(hi))
21152 return NULL;
21153 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021154 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021155}
21156
21157/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021158 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021159 * Set "varname" to the start of name without ':'.
21160 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021161 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021162find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163 char_u *name;
21164 char_u **varname;
21165{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021166 hashitem_T *hi;
21167
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 if (name[1] != ':')
21169 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021170 /* The name must not start with a colon or #. */
21171 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 return NULL;
21173 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021174
21175 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021176 hi = hash_find(&compat_hashtab, name);
21177 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021178 return &compat_hashtab;
21179
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021181 return &globvarht; /* global variable */
21182 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 }
21184 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021185 if (*name == 'g') /* global variable */
21186 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021187 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21188 */
21189 if (vim_strchr(name + 2, ':') != NULL
21190 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021191 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021193 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021195 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021196#ifdef FEAT_WINDOWS
21197 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021198 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021199#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021200 if (*name == 'v') /* v: variable */
21201 return &vimvarht;
21202 if (*name == 'a' && current_funccal != NULL) /* function argument */
21203 return &current_funccal->l_avars.dv_hashtab;
21204 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21205 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 if (*name == 's' /* script variable */
21207 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21208 return &SCRIPT_VARS(current_SID);
21209 return NULL;
21210}
21211
21212/*
21213 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021214 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215 * Returns NULL when it doesn't exist.
21216 */
21217 char_u *
21218get_var_value(name)
21219 char_u *name;
21220{
Bram Moolenaar33570922005-01-25 22:26:29 +000021221 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021223 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 if (v == NULL)
21225 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021226 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227}
21228
21229/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021230 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 * sourcing this script and when executing functions defined in the script.
21232 */
21233 void
21234new_script_vars(id)
21235 scid_T id;
21236{
Bram Moolenaara7043832005-01-21 11:56:39 +000021237 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021238 hashtab_T *ht;
21239 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021240
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21242 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021243 /* Re-allocating ga_data means that an ht_array pointing to
21244 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021245 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021246 for (i = 1; i <= ga_scripts.ga_len; ++i)
21247 {
21248 ht = &SCRIPT_VARS(i);
21249 if (ht->ht_mask == HT_INIT_SIZE - 1)
21250 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021251 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021252 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021253 }
21254
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 while (ga_scripts.ga_len < id)
21256 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021257 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021258 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021259 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 }
21262 }
21263}
21264
21265/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021266 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21267 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 */
21269 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021270init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021271 dict_T *dict;
21272 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021273 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274{
Bram Moolenaar33570922005-01-25 22:26:29 +000021275 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021276 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021277 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021278 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021279 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021280 dict_var->di_tv.vval.v_dict = dict;
21281 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021282 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021283 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21284 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285}
21286
21287/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021288 * Unreference a dictionary initialized by init_var_dict().
21289 */
21290 void
21291unref_var_dict(dict)
21292 dict_T *dict;
21293{
21294 /* Now the dict needs to be freed if no one else is using it, go back to
21295 * normal reference counting. */
21296 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21297 dict_unref(dict);
21298}
21299
21300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021302 * Frees all allocated variables and the value they contain.
21303 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 */
21305 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021306vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021307 hashtab_T *ht;
21308{
21309 vars_clear_ext(ht, TRUE);
21310}
21311
21312/*
21313 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21314 */
21315 static void
21316vars_clear_ext(ht, free_val)
21317 hashtab_T *ht;
21318 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319{
Bram Moolenaara7043832005-01-21 11:56:39 +000021320 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021321 hashitem_T *hi;
21322 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323
Bram Moolenaar33570922005-01-25 22:26:29 +000021324 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021325 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021326 for (hi = ht->ht_array; todo > 0; ++hi)
21327 {
21328 if (!HASHITEM_EMPTY(hi))
21329 {
21330 --todo;
21331
Bram Moolenaar33570922005-01-25 22:26:29 +000021332 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021333 * ht_array might change then. hash_clear() takes care of it
21334 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021335 v = HI2DI(hi);
21336 if (free_val)
21337 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021338 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021339 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021340 }
21341 }
21342 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021343 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344}
21345
Bram Moolenaara7043832005-01-21 11:56:39 +000021346/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021347 * Delete a variable from hashtab "ht" at item "hi".
21348 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021351delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021352 hashtab_T *ht;
21353 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354{
Bram Moolenaar33570922005-01-25 22:26:29 +000021355 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021356
21357 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021358 clear_tv(&di->di_tv);
21359 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360}
21361
21362/*
21363 * List the value of one internal variable.
21364 */
21365 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021366list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021367 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021369 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021371 char_u *tofree;
21372 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021373 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021374
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021375 current_copyID += COPYID_INC;
21376 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021377 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021378 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021379 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380}
21381
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021383list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 char_u *prefix;
21385 char_u *name;
21386 int type;
21387 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021388 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389{
Bram Moolenaar31859182007-08-14 20:41:13 +000021390 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21391 msg_start();
21392 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 if (name != NULL) /* "a:" vars don't have a name stored */
21394 msg_puts(name);
21395 msg_putchar(' ');
21396 msg_advance(22);
21397 if (type == VAR_NUMBER)
21398 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021399 else if (type == VAR_FUNC)
21400 msg_putchar('*');
21401 else if (type == VAR_LIST)
21402 {
21403 msg_putchar('[');
21404 if (*string == '[')
21405 ++string;
21406 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021407 else if (type == VAR_DICT)
21408 {
21409 msg_putchar('{');
21410 if (*string == '{')
21411 ++string;
21412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 else
21414 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021415
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021417
21418 if (type == VAR_FUNC)
21419 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021420 if (*first)
21421 {
21422 msg_clr_eos();
21423 *first = FALSE;
21424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425}
21426
21427/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021428 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 * If the variable already exists, the value is updated.
21430 * Otherwise the variable is created.
21431 */
21432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021433set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021435 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021436 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437{
Bram Moolenaar33570922005-01-25 22:26:29 +000021438 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021440 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021442 ht = find_var_ht(name, &varname);
21443 if (ht == NULL || *varname == NUL)
21444 {
21445 EMSG2(_(e_illvar), name);
21446 return;
21447 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021448 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021449
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021450 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21451 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021452
Bram Moolenaar33570922005-01-25 22:26:29 +000021453 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021455 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021456 if (var_check_ro(v->di_flags, name, FALSE)
21457 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021458 return;
21459 if (v->di_tv.v_type != tv->v_type
21460 && !((v->di_tv.v_type == VAR_STRING
21461 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021462 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021463 || tv->v_type == VAR_NUMBER))
21464#ifdef FEAT_FLOAT
21465 && !((v->di_tv.v_type == VAR_NUMBER
21466 || v->di_tv.v_type == VAR_FLOAT)
21467 && (tv->v_type == VAR_NUMBER
21468 || tv->v_type == VAR_FLOAT))
21469#endif
21470 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021471 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021472 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021473 return;
21474 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021475
21476 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021477 * Handle setting internal v: variables separately: we don't change
21478 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021479 */
21480 if (ht == &vimvarht)
21481 {
21482 if (v->di_tv.v_type == VAR_STRING)
21483 {
21484 vim_free(v->di_tv.vval.v_string);
21485 if (copy || tv->v_type != VAR_STRING)
21486 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21487 else
21488 {
21489 /* Take over the string to avoid an extra alloc/free. */
21490 v->di_tv.vval.v_string = tv->vval.v_string;
21491 tv->vval.v_string = NULL;
21492 }
21493 }
21494 else if (v->di_tv.v_type != VAR_NUMBER)
21495 EMSG2(_(e_intern2), "set_var()");
21496 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021497 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021498 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021499 if (STRCMP(varname, "searchforward") == 0)
21500 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021501#ifdef FEAT_SEARCH_EXTRA
21502 else if (STRCMP(varname, "hlsearch") == 0)
21503 {
21504 no_hlsearch = !v->di_tv.vval.v_number;
21505 redraw_all_later(SOME_VALID);
21506 }
21507#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021508 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021509 return;
21510 }
21511
21512 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 }
21514 else /* add a new variable */
21515 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021516 /* Can't add "v:" variable. */
21517 if (ht == &vimvarht)
21518 {
21519 EMSG2(_(e_illvar), name);
21520 return;
21521 }
21522
Bram Moolenaar92124a32005-06-17 22:03:40 +000021523 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021524 if (!valid_varname(varname))
21525 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021526
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021527 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21528 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021529 if (v == NULL)
21530 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021531 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021532 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021534 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 return;
21536 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021537 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021539
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021540 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021541 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021542 else
21543 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021544 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021545 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021546 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548}
21549
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021550/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021551 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 * Also give an error message.
21553 */
21554 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021555var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021556 int flags;
21557 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021558 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021559{
21560 if (flags & DI_FLAGS_RO)
21561 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021562 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021563 return TRUE;
21564 }
21565 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21566 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021567 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021568 return TRUE;
21569 }
21570 return FALSE;
21571}
21572
21573/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021574 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21575 * Also give an error message.
21576 */
21577 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021578var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021579 int flags;
21580 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021581 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021582{
21583 if (flags & DI_FLAGS_FIX)
21584 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021585 EMSG2(_("E795: Cannot delete variable %s"),
21586 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021587 return TRUE;
21588 }
21589 return FALSE;
21590}
21591
21592/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021593 * Check if a funcref is assigned to a valid variable name.
21594 * Return TRUE and give an error if not.
21595 */
21596 static int
21597var_check_func_name(name, new_var)
21598 char_u *name; /* points to start of variable name */
21599 int new_var; /* TRUE when creating the variable */
21600{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021601 /* Allow for w: b: s: and t:. */
21602 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021603 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21604 ? name[2] : name[0]))
21605 {
21606 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21607 name);
21608 return TRUE;
21609 }
21610 /* Don't allow hiding a function. When "v" is not NULL we might be
21611 * assigning another function to the same var, the type is checked
21612 * below. */
21613 if (new_var && function_exists(name))
21614 {
21615 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21616 name);
21617 return TRUE;
21618 }
21619 return FALSE;
21620}
21621
21622/*
21623 * Check if a variable name is valid.
21624 * Return FALSE and give an error if not.
21625 */
21626 static int
21627valid_varname(varname)
21628 char_u *varname;
21629{
21630 char_u *p;
21631
21632 for (p = varname; *p != NUL; ++p)
21633 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21634 && *p != AUTOLOAD_CHAR)
21635 {
21636 EMSG2(_(e_illvar), varname);
21637 return FALSE;
21638 }
21639 return TRUE;
21640}
21641
21642/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021643 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021644 * Also give an error message, using "name" or _("name") when use_gettext is
21645 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021646 */
21647 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021648tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021649 int lock;
21650 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021651 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021652{
21653 if (lock & VAR_LOCKED)
21654 {
21655 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021656 name == NULL ? (char_u *)_("Unknown")
21657 : use_gettext ? (char_u *)_(name)
21658 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021659 return TRUE;
21660 }
21661 if (lock & VAR_FIXED)
21662 {
21663 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021664 name == NULL ? (char_u *)_("Unknown")
21665 : use_gettext ? (char_u *)_(name)
21666 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021667 return TRUE;
21668 }
21669 return FALSE;
21670}
21671
21672/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021673 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021674 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021675 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021676 * It is OK for "from" and "to" to point to the same item. This is used to
21677 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021678 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021679 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021680copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021681 typval_T *from;
21682 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021684 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021685 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021686 switch (from->v_type)
21687 {
21688 case VAR_NUMBER:
21689 to->vval.v_number = from->vval.v_number;
21690 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021691#ifdef FEAT_FLOAT
21692 case VAR_FLOAT:
21693 to->vval.v_float = from->vval.v_float;
21694 break;
21695#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021696 case VAR_STRING:
21697 case VAR_FUNC:
21698 if (from->vval.v_string == NULL)
21699 to->vval.v_string = NULL;
21700 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021701 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021702 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021703 if (from->v_type == VAR_FUNC)
21704 func_ref(to->vval.v_string);
21705 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021706 break;
21707 case VAR_LIST:
21708 if (from->vval.v_list == NULL)
21709 to->vval.v_list = NULL;
21710 else
21711 {
21712 to->vval.v_list = from->vval.v_list;
21713 ++to->vval.v_list->lv_refcount;
21714 }
21715 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021716 case VAR_DICT:
21717 if (from->vval.v_dict == NULL)
21718 to->vval.v_dict = NULL;
21719 else
21720 {
21721 to->vval.v_dict = from->vval.v_dict;
21722 ++to->vval.v_dict->dv_refcount;
21723 }
21724 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021725 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021726 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021727 break;
21728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729}
21730
21731/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021732 * Make a copy of an item.
21733 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021734 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21735 * reference to an already copied list/dict can be used.
21736 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021737 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021738 static int
21739item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021740 typval_T *from;
21741 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021742 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021743 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021744{
21745 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021746 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021747
Bram Moolenaar33570922005-01-25 22:26:29 +000021748 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021749 {
21750 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021751 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021752 }
21753 ++recurse;
21754
21755 switch (from->v_type)
21756 {
21757 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021758#ifdef FEAT_FLOAT
21759 case VAR_FLOAT:
21760#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021761 case VAR_STRING:
21762 case VAR_FUNC:
21763 copy_tv(from, to);
21764 break;
21765 case VAR_LIST:
21766 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021767 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021768 if (from->vval.v_list == NULL)
21769 to->vval.v_list = NULL;
21770 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21771 {
21772 /* use the copy made earlier */
21773 to->vval.v_list = from->vval.v_list->lv_copylist;
21774 ++to->vval.v_list->lv_refcount;
21775 }
21776 else
21777 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21778 if (to->vval.v_list == NULL)
21779 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021780 break;
21781 case VAR_DICT:
21782 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021783 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021784 if (from->vval.v_dict == NULL)
21785 to->vval.v_dict = NULL;
21786 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21787 {
21788 /* use the copy made earlier */
21789 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21790 ++to->vval.v_dict->dv_refcount;
21791 }
21792 else
21793 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21794 if (to->vval.v_dict == NULL)
21795 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021796 break;
21797 default:
21798 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021799 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021800 }
21801 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021802 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021803}
21804
21805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806 * ":echo expr1 ..." print each argument separated with a space, add a
21807 * newline at the end.
21808 * ":echon expr1 ..." print each argument plain.
21809 */
21810 void
21811ex_echo(eap)
21812 exarg_T *eap;
21813{
21814 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021815 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021816 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 char_u *p;
21818 int needclr = TRUE;
21819 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021820 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021821
21822 if (eap->skip)
21823 ++emsg_skip;
21824 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21825 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021826 /* If eval1() causes an error message the text from the command may
21827 * still need to be cleared. E.g., "echo 22,44". */
21828 need_clr_eos = needclr;
21829
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021831 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 {
21833 /*
21834 * Report the invalid expression unless the expression evaluation
21835 * has been cancelled due to an aborting error, an interrupt, or an
21836 * exception.
21837 */
21838 if (!aborting())
21839 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021840 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 break;
21842 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021843 need_clr_eos = FALSE;
21844
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 if (!eap->skip)
21846 {
21847 if (atstart)
21848 {
21849 atstart = FALSE;
21850 /* Call msg_start() after eval1(), evaluating the expression
21851 * may cause a message to appear. */
21852 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021853 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021854 /* Mark the saved text as finishing the line, so that what
21855 * follows is displayed on a new line when scrolling back
21856 * at the more prompt. */
21857 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 }
21861 else if (eap->cmdidx == CMD_echo)
21862 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021863 current_copyID += COPYID_INC;
21864 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021865 if (p != NULL)
21866 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021868 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021870 if (*p != TAB && needclr)
21871 {
21872 /* remove any text still there from the command */
21873 msg_clr_eos();
21874 needclr = FALSE;
21875 }
21876 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 }
21878 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021879 {
21880#ifdef FEAT_MBYTE
21881 if (has_mbyte)
21882 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021883 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021884
21885 (void)msg_outtrans_len_attr(p, i, echo_attr);
21886 p += i - 1;
21887 }
21888 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021890 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021893 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896 arg = skipwhite(arg);
21897 }
21898 eap->nextcmd = check_nextcmd(arg);
21899
21900 if (eap->skip)
21901 --emsg_skip;
21902 else
21903 {
21904 /* remove text that may still be there from the command */
21905 if (needclr)
21906 msg_clr_eos();
21907 if (eap->cmdidx == CMD_echo)
21908 msg_end();
21909 }
21910}
21911
21912/*
21913 * ":echohl {name}".
21914 */
21915 void
21916ex_echohl(eap)
21917 exarg_T *eap;
21918{
21919 int id;
21920
21921 id = syn_name2id(eap->arg);
21922 if (id == 0)
21923 echo_attr = 0;
21924 else
21925 echo_attr = syn_id2attr(id);
21926}
21927
21928/*
21929 * ":execute expr1 ..." execute the result of an expression.
21930 * ":echomsg expr1 ..." Print a message
21931 * ":echoerr expr1 ..." Print an error
21932 * Each gets spaces around each argument and a newline at the end for
21933 * echo commands
21934 */
21935 void
21936ex_execute(eap)
21937 exarg_T *eap;
21938{
21939 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021940 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941 int ret = OK;
21942 char_u *p;
21943 garray_T ga;
21944 int len;
21945 int save_did_emsg;
21946
21947 ga_init2(&ga, 1, 80);
21948
21949 if (eap->skip)
21950 ++emsg_skip;
21951 while (*arg != NUL && *arg != '|' && *arg != '\n')
21952 {
21953 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021954 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 {
21956 /*
21957 * Report the invalid expression unless the expression evaluation
21958 * has been cancelled due to an aborting error, an interrupt, or an
21959 * exception.
21960 */
21961 if (!aborting())
21962 EMSG2(_(e_invexpr2), p);
21963 ret = FAIL;
21964 break;
21965 }
21966
21967 if (!eap->skip)
21968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021969 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 len = (int)STRLEN(p);
21971 if (ga_grow(&ga, len + 2) == FAIL)
21972 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021973 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 ret = FAIL;
21975 break;
21976 }
21977 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980 ga.ga_len += len;
21981 }
21982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021983 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 arg = skipwhite(arg);
21985 }
21986
21987 if (ret != FAIL && ga.ga_data != NULL)
21988 {
21989 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021992 out_flush();
21993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 else if (eap->cmdidx == CMD_echoerr)
21995 {
21996 /* We don't want to abort following commands, restore did_emsg. */
21997 save_did_emsg = did_emsg;
21998 EMSG((char_u *)ga.ga_data);
21999 if (!force_abort)
22000 did_emsg = save_did_emsg;
22001 }
22002 else if (eap->cmdidx == CMD_execute)
22003 do_cmdline((char_u *)ga.ga_data,
22004 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22005 }
22006
22007 ga_clear(&ga);
22008
22009 if (eap->skip)
22010 --emsg_skip;
22011
22012 eap->nextcmd = check_nextcmd(arg);
22013}
22014
22015/*
22016 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22017 * "arg" points to the "&" or '+' when called, to "option" when returning.
22018 * Returns NULL when no option name found. Otherwise pointer to the char
22019 * after the option name.
22020 */
22021 static char_u *
22022find_option_end(arg, opt_flags)
22023 char_u **arg;
22024 int *opt_flags;
22025{
22026 char_u *p = *arg;
22027
22028 ++p;
22029 if (*p == 'g' && p[1] == ':')
22030 {
22031 *opt_flags = OPT_GLOBAL;
22032 p += 2;
22033 }
22034 else if (*p == 'l' && p[1] == ':')
22035 {
22036 *opt_flags = OPT_LOCAL;
22037 p += 2;
22038 }
22039 else
22040 *opt_flags = 0;
22041
22042 if (!ASCII_ISALPHA(*p))
22043 return NULL;
22044 *arg = p;
22045
22046 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22047 p += 4; /* termcap option */
22048 else
22049 while (ASCII_ISALPHA(*p))
22050 ++p;
22051 return p;
22052}
22053
22054/*
22055 * ":function"
22056 */
22057 void
22058ex_function(eap)
22059 exarg_T *eap;
22060{
22061 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022062 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 int j;
22064 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022066 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 char_u *name = NULL;
22068 char_u *p;
22069 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022070 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 garray_T newargs;
22072 garray_T newlines;
22073 int varargs = FALSE;
22074 int mustend = FALSE;
22075 int flags = 0;
22076 ufunc_T *fp;
22077 int indent;
22078 int nesting;
22079 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022080 dictitem_T *v;
22081 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022082 static int func_nr = 0; /* number for nameless function */
22083 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022084 hashtab_T *ht;
22085 int todo;
22086 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022087 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088
22089 /*
22090 * ":function" without argument: list functions.
22091 */
22092 if (ends_excmd(*eap->arg))
22093 {
22094 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022095 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022096 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022097 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022098 {
22099 if (!HASHITEM_EMPTY(hi))
22100 {
22101 --todo;
22102 fp = HI2UF(hi);
22103 if (!isdigit(*fp->uf_name))
22104 list_func_head(fp, FALSE);
22105 }
22106 }
22107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 eap->nextcmd = check_nextcmd(eap->arg);
22109 return;
22110 }
22111
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022112 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022113 * ":function /pat": list functions matching pattern.
22114 */
22115 if (*eap->arg == '/')
22116 {
22117 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22118 if (!eap->skip)
22119 {
22120 regmatch_T regmatch;
22121
22122 c = *p;
22123 *p = NUL;
22124 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22125 *p = c;
22126 if (regmatch.regprog != NULL)
22127 {
22128 regmatch.rm_ic = p_ic;
22129
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022130 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022131 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22132 {
22133 if (!HASHITEM_EMPTY(hi))
22134 {
22135 --todo;
22136 fp = HI2UF(hi);
22137 if (!isdigit(*fp->uf_name)
22138 && vim_regexec(&regmatch, fp->uf_name, 0))
22139 list_func_head(fp, FALSE);
22140 }
22141 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022142 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022143 }
22144 }
22145 if (*p == '/')
22146 ++p;
22147 eap->nextcmd = check_nextcmd(p);
22148 return;
22149 }
22150
22151 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022152 * Get the function name. There are these situations:
22153 * func normal function name
22154 * "name" == func, "fudi.fd_dict" == NULL
22155 * dict.func new dictionary entry
22156 * "name" == NULL, "fudi.fd_dict" set,
22157 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22158 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022159 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022160 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22161 * dict.func existing dict entry that's not a Funcref
22162 * "name" == NULL, "fudi.fd_dict" set,
22163 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022164 * s:func script-local function name
22165 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022168 name = trans_function_name(&p, eap->skip, 0, &fudi);
22169 paren = (vim_strchr(p, '(') != NULL);
22170 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 {
22172 /*
22173 * Return on an invalid expression in braces, unless the expression
22174 * evaluation has been cancelled due to an aborting error, an
22175 * interrupt, or an exception.
22176 */
22177 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022178 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022179 if (!eap->skip && fudi.fd_newkey != NULL)
22180 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022181 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 else
22185 eap->skip = TRUE;
22186 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022187
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 /* An error in a function call during evaluation of an expression in magic
22189 * braces should not cause the function not to be defined. */
22190 saved_did_emsg = did_emsg;
22191 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192
22193 /*
22194 * ":function func" with only function name: list function.
22195 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022196 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 {
22198 if (!ends_excmd(*skipwhite(p)))
22199 {
22200 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022201 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 }
22203 eap->nextcmd = check_nextcmd(p);
22204 if (eap->nextcmd != NULL)
22205 *p = NUL;
22206 if (!eap->skip && !got_int)
22207 {
22208 fp = find_func(name);
22209 if (fp != NULL)
22210 {
22211 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022212 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022214 if (FUNCLINE(fp, j) == NULL)
22215 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216 msg_putchar('\n');
22217 msg_outnum((long)(j + 1));
22218 if (j < 9)
22219 msg_putchar(' ');
22220 if (j < 99)
22221 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022222 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223 out_flush(); /* show a line at a time */
22224 ui_breakcheck();
22225 }
22226 if (!got_int)
22227 {
22228 msg_putchar('\n');
22229 msg_puts((char_u *)" endfunction");
22230 }
22231 }
22232 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022233 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022235 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236 }
22237
22238 /*
22239 * ":function name(arg1, arg2)" Define function.
22240 */
22241 p = skipwhite(p);
22242 if (*p != '(')
22243 {
22244 if (!eap->skip)
22245 {
22246 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022247 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 }
22249 /* attempt to continue by skipping some text */
22250 if (vim_strchr(p, '(') != NULL)
22251 p = vim_strchr(p, '(');
22252 }
22253 p = skipwhite(p + 1);
22254
22255 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22256 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22257
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022258 if (!eap->skip)
22259 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022260 /* Check the name of the function. Unless it's a dictionary function
22261 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022262 if (name != NULL)
22263 arg = name;
22264 else
22265 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022266 if (arg != NULL && (fudi.fd_di == NULL
22267 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022268 {
22269 if (*arg == K_SPECIAL)
22270 j = 3;
22271 else
22272 j = 0;
22273 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22274 : eval_isnamec(arg[j])))
22275 ++j;
22276 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022277 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022278 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022279 /* Disallow using the g: dict. */
22280 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22281 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022282 }
22283
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 /*
22285 * Isolate the arguments: "arg1, arg2, ...)"
22286 */
22287 while (*p != ')')
22288 {
22289 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22290 {
22291 varargs = TRUE;
22292 p += 3;
22293 mustend = TRUE;
22294 }
22295 else
22296 {
22297 arg = p;
22298 while (ASCII_ISALNUM(*p) || *p == '_')
22299 ++p;
22300 if (arg == p || isdigit(*arg)
22301 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22302 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22303 {
22304 if (!eap->skip)
22305 EMSG2(_("E125: Illegal argument: %s"), arg);
22306 break;
22307 }
22308 if (ga_grow(&newargs, 1) == FAIL)
22309 goto erret;
22310 c = *p;
22311 *p = NUL;
22312 arg = vim_strsave(arg);
22313 if (arg == NULL)
22314 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022315
22316 /* Check for duplicate argument name. */
22317 for (i = 0; i < newargs.ga_len; ++i)
22318 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22319 {
22320 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022321 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022322 goto erret;
22323 }
22324
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22326 *p = c;
22327 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328 if (*p == ',')
22329 ++p;
22330 else
22331 mustend = TRUE;
22332 }
22333 p = skipwhite(p);
22334 if (mustend && *p != ')')
22335 {
22336 if (!eap->skip)
22337 EMSG2(_(e_invarg2), eap->arg);
22338 break;
22339 }
22340 }
22341 ++p; /* skip the ')' */
22342
Bram Moolenaare9a41262005-01-15 22:18:47 +000022343 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344 for (;;)
22345 {
22346 p = skipwhite(p);
22347 if (STRNCMP(p, "range", 5) == 0)
22348 {
22349 flags |= FC_RANGE;
22350 p += 5;
22351 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022352 else if (STRNCMP(p, "dict", 4) == 0)
22353 {
22354 flags |= FC_DICT;
22355 p += 4;
22356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357 else if (STRNCMP(p, "abort", 5) == 0)
22358 {
22359 flags |= FC_ABORT;
22360 p += 5;
22361 }
22362 else
22363 break;
22364 }
22365
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022366 /* When there is a line break use what follows for the function body.
22367 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22368 if (*p == '\n')
22369 line_arg = p + 1;
22370 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371 EMSG(_(e_trailing));
22372
22373 /*
22374 * Read the body of the function, until ":endfunction" is found.
22375 */
22376 if (KeyTyped)
22377 {
22378 /* Check if the function already exists, don't let the user type the
22379 * whole function before telling him it doesn't work! For a script we
22380 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022381 if (!eap->skip && !eap->forceit)
22382 {
22383 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22384 EMSG(_(e_funcdict));
22385 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022386 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022389 if (!eap->skip && did_emsg)
22390 goto erret;
22391
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 msg_putchar('\n'); /* don't overwrite the function name */
22393 cmdline_row = msg_row;
22394 }
22395
22396 indent = 2;
22397 nesting = 0;
22398 for (;;)
22399 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022400 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022401 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022402 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022403 saved_wait_return = FALSE;
22404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022406 sourcing_lnum_off = sourcing_lnum;
22407
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022408 if (line_arg != NULL)
22409 {
22410 /* Use eap->arg, split up in parts by line breaks. */
22411 theline = line_arg;
22412 p = vim_strchr(theline, '\n');
22413 if (p == NULL)
22414 line_arg += STRLEN(line_arg);
22415 else
22416 {
22417 *p = NUL;
22418 line_arg = p + 1;
22419 }
22420 }
22421 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 theline = getcmdline(':', 0L, indent);
22423 else
22424 theline = eap->getline(':', eap->cookie, indent);
22425 if (KeyTyped)
22426 lines_left = Rows - 1;
22427 if (theline == NULL)
22428 {
22429 EMSG(_("E126: Missing :endfunction"));
22430 goto erret;
22431 }
22432
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022433 /* Detect line continuation: sourcing_lnum increased more than one. */
22434 if (sourcing_lnum > sourcing_lnum_off + 1)
22435 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22436 else
22437 sourcing_lnum_off = 0;
22438
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 if (skip_until != NULL)
22440 {
22441 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22442 * don't check for ":endfunc". */
22443 if (STRCMP(theline, skip_until) == 0)
22444 {
22445 vim_free(skip_until);
22446 skip_until = NULL;
22447 }
22448 }
22449 else
22450 {
22451 /* skip ':' and blanks*/
22452 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22453 ;
22454
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022455 /* Check for "endfunction". */
22456 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022458 if (line_arg == NULL)
22459 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460 break;
22461 }
22462
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022463 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 * at "end". */
22465 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22466 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022467 else if (STRNCMP(p, "if", 2) == 0
22468 || STRNCMP(p, "wh", 2) == 0
22469 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470 || STRNCMP(p, "try", 3) == 0)
22471 indent += 2;
22472
22473 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022474 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022476 if (*p == '!')
22477 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022479 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22480 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022482 ++nesting;
22483 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 }
22485 }
22486
22487 /* Check for ":append" or ":insert". */
22488 p = skip_range(p, NULL);
22489 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22490 || (p[0] == 'i'
22491 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22492 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22493 skip_until = vim_strsave((char_u *)".");
22494
22495 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22496 arg = skipwhite(skiptowhite(p));
22497 if (arg[0] == '<' && arg[1] =='<'
22498 && ((p[0] == 'p' && p[1] == 'y'
22499 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22500 || (p[0] == 'p' && p[1] == 'e'
22501 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22502 || (p[0] == 't' && p[1] == 'c'
22503 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022504 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22505 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22507 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022508 || (p[0] == 'm' && p[1] == 'z'
22509 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 ))
22511 {
22512 /* ":python <<" continues until a dot, like ":append" */
22513 p = skipwhite(arg + 2);
22514 if (*p == NUL)
22515 skip_until = vim_strsave((char_u *)".");
22516 else
22517 skip_until = vim_strsave(p);
22518 }
22519 }
22520
22521 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022522 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022523 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022524 if (line_arg == NULL)
22525 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022527 }
22528
22529 /* Copy the line to newly allocated memory. get_one_sourceline()
22530 * allocates 250 bytes per line, this saves 80% on average. The cost
22531 * is an extra alloc/free. */
22532 p = vim_strsave(theline);
22533 if (p != NULL)
22534 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022535 if (line_arg == NULL)
22536 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022537 theline = p;
22538 }
22539
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022540 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22541
22542 /* Add NULL lines for continuation lines, so that the line count is
22543 * equal to the index in the growarray. */
22544 while (sourcing_lnum_off-- > 0)
22545 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022546
22547 /* Check for end of eap->arg. */
22548 if (line_arg != NULL && *line_arg == NUL)
22549 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022550 }
22551
22552 /* Don't define the function when skipping commands or when an error was
22553 * detected. */
22554 if (eap->skip || did_emsg)
22555 goto erret;
22556
22557 /*
22558 * If there are no errors, add the function
22559 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022560 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022561 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022562 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022563 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022564 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022565 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022566 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022567 goto erret;
22568 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022569
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022570 fp = find_func(name);
22571 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022573 if (!eap->forceit)
22574 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022575 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022576 goto erret;
22577 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022578 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022579 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022580 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022581 name);
22582 goto erret;
22583 }
22584 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022585 ga_clear_strings(&(fp->uf_args));
22586 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022587 vim_free(name);
22588 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 }
22591 else
22592 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022593 char numbuf[20];
22594
22595 fp = NULL;
22596 if (fudi.fd_newkey == NULL && !eap->forceit)
22597 {
22598 EMSG(_(e_funcdict));
22599 goto erret;
22600 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022601 if (fudi.fd_di == NULL)
22602 {
22603 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022604 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022605 goto erret;
22606 }
22607 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022608 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022609 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022610
22611 /* Give the function a sequential number. Can only be used with a
22612 * Funcref! */
22613 vim_free(name);
22614 sprintf(numbuf, "%d", ++func_nr);
22615 name = vim_strsave((char_u *)numbuf);
22616 if (name == NULL)
22617 goto erret;
22618 }
22619
22620 if (fp == NULL)
22621 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022622 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022623 {
22624 int slen, plen;
22625 char_u *scriptname;
22626
22627 /* Check that the autoload name matches the script name. */
22628 j = FAIL;
22629 if (sourcing_name != NULL)
22630 {
22631 scriptname = autoload_name(name);
22632 if (scriptname != NULL)
22633 {
22634 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022635 plen = (int)STRLEN(p);
22636 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022637 if (slen > plen && fnamecmp(p,
22638 sourcing_name + slen - plen) == 0)
22639 j = OK;
22640 vim_free(scriptname);
22641 }
22642 }
22643 if (j == FAIL)
22644 {
22645 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22646 goto erret;
22647 }
22648 }
22649
22650 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 if (fp == NULL)
22652 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022653
22654 if (fudi.fd_dict != NULL)
22655 {
22656 if (fudi.fd_di == NULL)
22657 {
22658 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022659 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022660 if (fudi.fd_di == NULL)
22661 {
22662 vim_free(fp);
22663 goto erret;
22664 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022665 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22666 {
22667 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022668 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022669 goto erret;
22670 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022671 }
22672 else
22673 /* overwrite existing dict entry */
22674 clear_tv(&fudi.fd_di->di_tv);
22675 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022676 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022677 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022678 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022679
22680 /* behave like "dict" was used */
22681 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022682 }
22683
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022685 STRCPY(fp->uf_name, name);
22686 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022688 fp->uf_args = newargs;
22689 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022690#ifdef FEAT_PROFILE
22691 fp->uf_tml_count = NULL;
22692 fp->uf_tml_total = NULL;
22693 fp->uf_tml_self = NULL;
22694 fp->uf_profiling = FALSE;
22695 if (prof_def_func())
22696 func_do_profile(fp);
22697#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022698 fp->uf_varargs = varargs;
22699 fp->uf_flags = flags;
22700 fp->uf_calls = 0;
22701 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022702 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703
22704erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 ga_clear_strings(&newargs);
22706 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022707ret_free:
22708 vim_free(skip_until);
22709 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022712 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713}
22714
22715/*
22716 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022717 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022719 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022720 * TFN_INT: internal function name OK
22721 * TFN_QUIET: be quiet
22722 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 * Advances "pp" to just after the function name (if no error).
22724 */
22725 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022726trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 char_u **pp;
22728 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022729 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022730 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022732 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 char_u *start;
22734 char_u *end;
22735 int lead;
22736 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022739
22740 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022741 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022743
22744 /* Check for hard coded <SNR>: already translated function ID (from a user
22745 * command). */
22746 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22747 && (*pp)[2] == (int)KE_SNR)
22748 {
22749 *pp += 3;
22750 len = get_id_len(pp) + 3;
22751 return vim_strnsave(start, len);
22752 }
22753
22754 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22755 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022757 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022759
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022760 /* Note that TFN_ flags use the same values as GLV_ flags. */
22761 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022762 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022763 if (end == start)
22764 {
22765 if (!skip)
22766 EMSG(_("E129: Function name required"));
22767 goto theend;
22768 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022769 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022770 {
22771 /*
22772 * Report an invalid expression in braces, unless the expression
22773 * evaluation has been cancelled due to an aborting error, an
22774 * interrupt, or an exception.
22775 */
22776 if (!aborting())
22777 {
22778 if (end != NULL)
22779 EMSG2(_(e_invarg2), start);
22780 }
22781 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022782 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022783 goto theend;
22784 }
22785
22786 if (lv.ll_tv != NULL)
22787 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022788 if (fdp != NULL)
22789 {
22790 fdp->fd_dict = lv.ll_dict;
22791 fdp->fd_newkey = lv.ll_newkey;
22792 lv.ll_newkey = NULL;
22793 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022794 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022795 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22796 {
22797 name = vim_strsave(lv.ll_tv->vval.v_string);
22798 *pp = end;
22799 }
22800 else
22801 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022802 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22803 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022804 EMSG(_(e_funcref));
22805 else
22806 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022807 name = NULL;
22808 }
22809 goto theend;
22810 }
22811
22812 if (lv.ll_name == NULL)
22813 {
22814 /* Error found, but continue after the function name. */
22815 *pp = end;
22816 goto theend;
22817 }
22818
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022819 /* Check if the name is a Funcref. If so, use the value. */
22820 if (lv.ll_exp_name != NULL)
22821 {
22822 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022823 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022824 if (name == lv.ll_exp_name)
22825 name = NULL;
22826 }
22827 else
22828 {
22829 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022830 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022831 if (name == *pp)
22832 name = NULL;
22833 }
22834 if (name != NULL)
22835 {
22836 name = vim_strsave(name);
22837 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022838 if (STRNCMP(name, "<SNR>", 5) == 0)
22839 {
22840 /* Change "<SNR>" to the byte sequence. */
22841 name[0] = K_SPECIAL;
22842 name[1] = KS_EXTRA;
22843 name[2] = (int)KE_SNR;
22844 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22845 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022846 goto theend;
22847 }
22848
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022849 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022850 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022851 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022852 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22853 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22854 {
22855 /* When there was "s:" already or the name expanded to get a
22856 * leading "s:" then remove it. */
22857 lv.ll_name += 2;
22858 len -= 2;
22859 lead = 2;
22860 }
22861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022862 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022863 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022864 /* skip over "s:" and "g:" */
22865 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022866 lv.ll_name += 2;
22867 len = (int)(end - lv.ll_name);
22868 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022869
22870 /*
22871 * Copy the function name to allocated memory.
22872 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22873 * Accept <SNR>123_name() outside a script.
22874 */
22875 if (skip)
22876 lead = 0; /* do nothing */
22877 else if (lead > 0)
22878 {
22879 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022880 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22881 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022882 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022883 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022884 if (current_SID <= 0)
22885 {
22886 EMSG(_(e_usingsid));
22887 goto theend;
22888 }
22889 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22890 lead += (int)STRLEN(sid_buf);
22891 }
22892 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022893 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022894 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022895 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022896 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022897 goto theend;
22898 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022899 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022900 {
22901 char_u *cp = vim_strchr(lv.ll_name, ':');
22902
22903 if (cp != NULL && cp < end)
22904 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022905 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022906 goto theend;
22907 }
22908 }
22909
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022910 name = alloc((unsigned)(len + lead + 1));
22911 if (name != NULL)
22912 {
22913 if (lead > 0)
22914 {
22915 name[0] = K_SPECIAL;
22916 name[1] = KS_EXTRA;
22917 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022918 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022919 STRCPY(name + 3, sid_buf);
22920 }
22921 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022922 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022923 }
22924 *pp = end;
22925
22926theend:
22927 clear_lval(&lv);
22928 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929}
22930
22931/*
22932 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22933 * Return 2 if "p" starts with "s:".
22934 * Return 0 otherwise.
22935 */
22936 static int
22937eval_fname_script(p)
22938 char_u *p;
22939{
22940 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22941 || STRNICMP(p + 1, "SNR>", 4) == 0))
22942 return 5;
22943 if (p[0] == 's' && p[1] == ':')
22944 return 2;
22945 return 0;
22946}
22947
22948/*
22949 * Return TRUE if "p" starts with "<SID>" or "s:".
22950 * Only works if eval_fname_script() returned non-zero for "p"!
22951 */
22952 static int
22953eval_fname_sid(p)
22954 char_u *p;
22955{
22956 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22957}
22958
22959/*
22960 * List the head of the function: "name(arg1, arg2)".
22961 */
22962 static void
22963list_func_head(fp, indent)
22964 ufunc_T *fp;
22965 int indent;
22966{
22967 int j;
22968
22969 msg_start();
22970 if (indent)
22971 MSG_PUTS(" ");
22972 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022973 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 {
22975 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022976 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 }
22978 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022979 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022981 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 {
22983 if (j)
22984 MSG_PUTS(", ");
22985 msg_puts(FUNCARG(fp, j));
22986 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022987 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 {
22989 if (j)
22990 MSG_PUTS(", ");
22991 MSG_PUTS("...");
22992 }
22993 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022994 if (fp->uf_flags & FC_ABORT)
22995 MSG_PUTS(" abort");
22996 if (fp->uf_flags & FC_RANGE)
22997 MSG_PUTS(" range");
22998 if (fp->uf_flags & FC_DICT)
22999 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023000 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023001 if (p_verbose > 0)
23002 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003}
23004
23005/*
23006 * Find a function by name, return pointer to it in ufuncs.
23007 * Return NULL for unknown function.
23008 */
23009 static ufunc_T *
23010find_func(name)
23011 char_u *name;
23012{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023013 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023015 hi = hash_find(&func_hashtab, name);
23016 if (!HASHITEM_EMPTY(hi))
23017 return HI2UF(hi);
23018 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019}
23020
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023021#if defined(EXITFREE) || defined(PROTO)
23022 void
23023free_all_functions()
23024{
23025 hashitem_T *hi;
23026
23027 /* Need to start all over every time, because func_free() may change the
23028 * hash table. */
23029 while (func_hashtab.ht_used > 0)
23030 for (hi = func_hashtab.ht_array; ; ++hi)
23031 if (!HASHITEM_EMPTY(hi))
23032 {
23033 func_free(HI2UF(hi));
23034 break;
23035 }
23036}
23037#endif
23038
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023039 int
23040translated_function_exists(name)
23041 char_u *name;
23042{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023043 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023044 return find_internal_func(name) >= 0;
23045 return find_func(name) != NULL;
23046}
23047
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023048/*
23049 * Return TRUE if a function "name" exists.
23050 */
23051 static int
23052function_exists(name)
23053 char_u *name;
23054{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023055 char_u *nm = name;
23056 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023057 int n = FALSE;
23058
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023059 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23060 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023061 nm = skipwhite(nm);
23062
23063 /* Only accept "funcname", "funcname ", "funcname (..." and
23064 * "funcname(...", not "funcname!...". */
23065 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023066 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023067 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023068 return n;
23069}
23070
Bram Moolenaara1544c02013-05-30 12:35:52 +020023071 char_u *
23072get_expanded_name(name, check)
23073 char_u *name;
23074 int check;
23075{
23076 char_u *nm = name;
23077 char_u *p;
23078
23079 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23080
23081 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023082 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023083 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023084
Bram Moolenaara1544c02013-05-30 12:35:52 +020023085 vim_free(p);
23086 return NULL;
23087}
23088
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023089/*
23090 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023091 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23092 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023093 */
23094 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023095builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023096 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023097 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023098{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023099 char_u *p;
23100
23101 if (!ASCII_ISLOWER(name[0]))
23102 return FALSE;
23103 p = vim_strchr(name, AUTOLOAD_CHAR);
23104 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023105}
23106
Bram Moolenaar05159a02005-02-26 23:04:13 +000023107#if defined(FEAT_PROFILE) || defined(PROTO)
23108/*
23109 * Start profiling function "fp".
23110 */
23111 static void
23112func_do_profile(fp)
23113 ufunc_T *fp;
23114{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023115 int len = fp->uf_lines.ga_len;
23116
23117 if (len == 0)
23118 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023119 fp->uf_tm_count = 0;
23120 profile_zero(&fp->uf_tm_self);
23121 profile_zero(&fp->uf_tm_total);
23122 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023123 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023124 if (fp->uf_tml_total == NULL)
23125 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023126 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023127 if (fp->uf_tml_self == NULL)
23128 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023129 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023130 fp->uf_tml_idx = -1;
23131 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23132 || fp->uf_tml_self == NULL)
23133 return; /* out of memory */
23134
23135 fp->uf_profiling = TRUE;
23136}
23137
23138/*
23139 * Dump the profiling results for all functions in file "fd".
23140 */
23141 void
23142func_dump_profile(fd)
23143 FILE *fd;
23144{
23145 hashitem_T *hi;
23146 int todo;
23147 ufunc_T *fp;
23148 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023149 ufunc_T **sorttab;
23150 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023151
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023152 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023153 if (todo == 0)
23154 return; /* nothing to dump */
23155
Bram Moolenaar73830342005-02-28 22:48:19 +000023156 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
23157
Bram Moolenaar05159a02005-02-26 23:04:13 +000023158 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23159 {
23160 if (!HASHITEM_EMPTY(hi))
23161 {
23162 --todo;
23163 fp = HI2UF(hi);
23164 if (fp->uf_profiling)
23165 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023166 if (sorttab != NULL)
23167 sorttab[st_len++] = fp;
23168
Bram Moolenaar05159a02005-02-26 23:04:13 +000023169 if (fp->uf_name[0] == K_SPECIAL)
23170 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23171 else
23172 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23173 if (fp->uf_tm_count == 1)
23174 fprintf(fd, "Called 1 time\n");
23175 else
23176 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23177 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23178 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23179 fprintf(fd, "\n");
23180 fprintf(fd, "count total (s) self (s)\n");
23181
23182 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23183 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023184 if (FUNCLINE(fp, i) == NULL)
23185 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023186 prof_func_line(fd, fp->uf_tml_count[i],
23187 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023188 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23189 }
23190 fprintf(fd, "\n");
23191 }
23192 }
23193 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023194
23195 if (sorttab != NULL && st_len > 0)
23196 {
23197 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23198 prof_total_cmp);
23199 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23200 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23201 prof_self_cmp);
23202 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23203 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023204
23205 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023206}
Bram Moolenaar73830342005-02-28 22:48:19 +000023207
23208 static void
23209prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23210 FILE *fd;
23211 ufunc_T **sorttab;
23212 int st_len;
23213 char *title;
23214 int prefer_self; /* when equal print only self time */
23215{
23216 int i;
23217 ufunc_T *fp;
23218
23219 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23220 fprintf(fd, "count total (s) self (s) function\n");
23221 for (i = 0; i < 20 && i < st_len; ++i)
23222 {
23223 fp = sorttab[i];
23224 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23225 prefer_self);
23226 if (fp->uf_name[0] == K_SPECIAL)
23227 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23228 else
23229 fprintf(fd, " %s()\n", fp->uf_name);
23230 }
23231 fprintf(fd, "\n");
23232}
23233
23234/*
23235 * Print the count and times for one function or function line.
23236 */
23237 static void
23238prof_func_line(fd, count, total, self, prefer_self)
23239 FILE *fd;
23240 int count;
23241 proftime_T *total;
23242 proftime_T *self;
23243 int prefer_self; /* when equal print only self time */
23244{
23245 if (count > 0)
23246 {
23247 fprintf(fd, "%5d ", count);
23248 if (prefer_self && profile_equal(total, self))
23249 fprintf(fd, " ");
23250 else
23251 fprintf(fd, "%s ", profile_msg(total));
23252 if (!prefer_self && profile_equal(total, self))
23253 fprintf(fd, " ");
23254 else
23255 fprintf(fd, "%s ", profile_msg(self));
23256 }
23257 else
23258 fprintf(fd, " ");
23259}
23260
23261/*
23262 * Compare function for total time sorting.
23263 */
23264 static int
23265#ifdef __BORLANDC__
23266_RTLENTRYF
23267#endif
23268prof_total_cmp(s1, s2)
23269 const void *s1;
23270 const void *s2;
23271{
23272 ufunc_T *p1, *p2;
23273
23274 p1 = *(ufunc_T **)s1;
23275 p2 = *(ufunc_T **)s2;
23276 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23277}
23278
23279/*
23280 * Compare function for self time sorting.
23281 */
23282 static int
23283#ifdef __BORLANDC__
23284_RTLENTRYF
23285#endif
23286prof_self_cmp(s1, s2)
23287 const void *s1;
23288 const void *s2;
23289{
23290 ufunc_T *p1, *p2;
23291
23292 p1 = *(ufunc_T **)s1;
23293 p2 = *(ufunc_T **)s2;
23294 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23295}
23296
Bram Moolenaar05159a02005-02-26 23:04:13 +000023297#endif
23298
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023299/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023300 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023301 * Return TRUE if a package was loaded.
23302 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023303 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023304script_autoload(name, reload)
23305 char_u *name;
23306 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023307{
23308 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023309 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023310 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023311 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023312
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023313 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023314 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023315 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023316 return FALSE;
23317
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023318 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023319
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023320 /* Find the name in the list of previously loaded package names. Skip
23321 * "autoload/", it's always the same. */
23322 for (i = 0; i < ga_loaded.ga_len; ++i)
23323 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23324 break;
23325 if (!reload && i < ga_loaded.ga_len)
23326 ret = FALSE; /* was loaded already */
23327 else
23328 {
23329 /* Remember the name if it wasn't loaded already. */
23330 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23331 {
23332 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23333 tofree = NULL;
23334 }
23335
23336 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023337 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023338 ret = TRUE;
23339 }
23340
23341 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023342 return ret;
23343}
23344
23345/*
23346 * Return the autoload script name for a function or variable name.
23347 * Returns NULL when out of memory.
23348 */
23349 static char_u *
23350autoload_name(name)
23351 char_u *name;
23352{
23353 char_u *p;
23354 char_u *scriptname;
23355
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023356 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023357 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23358 if (scriptname == NULL)
23359 return FALSE;
23360 STRCPY(scriptname, "autoload/");
23361 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023362 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023363 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023364 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023365 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023366 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023367}
23368
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23370
23371/*
23372 * Function given to ExpandGeneric() to obtain the list of user defined
23373 * function names.
23374 */
23375 char_u *
23376get_user_func_name(xp, idx)
23377 expand_T *xp;
23378 int idx;
23379{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023380 static long_u done;
23381 static hashitem_T *hi;
23382 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383
23384 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023385 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023386 done = 0;
23387 hi = func_hashtab.ht_array;
23388 }
23389 if (done < func_hashtab.ht_used)
23390 {
23391 if (done++ > 0)
23392 ++hi;
23393 while (HASHITEM_EMPTY(hi))
23394 ++hi;
23395 fp = HI2UF(hi);
23396
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023397 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023398 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023399
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023400 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23401 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402
23403 cat_func_name(IObuff, fp);
23404 if (xp->xp_context != EXPAND_USER_FUNC)
23405 {
23406 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023407 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 STRCAT(IObuff, ")");
23409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410 return IObuff;
23411 }
23412 return NULL;
23413}
23414
23415#endif /* FEAT_CMDL_COMPL */
23416
23417/*
23418 * Copy the function name of "fp" to buffer "buf".
23419 * "buf" must be able to hold the function name plus three bytes.
23420 * Takes care of script-local function names.
23421 */
23422 static void
23423cat_func_name(buf, fp)
23424 char_u *buf;
23425 ufunc_T *fp;
23426{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023427 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428 {
23429 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023430 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023431 }
23432 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023433 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434}
23435
23436/*
23437 * ":delfunction {name}"
23438 */
23439 void
23440ex_delfunction(eap)
23441 exarg_T *eap;
23442{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023443 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023444 char_u *p;
23445 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023446 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023447
23448 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023449 name = trans_function_name(&p, eap->skip, 0, &fudi);
23450 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023452 {
23453 if (fudi.fd_dict != NULL && !eap->skip)
23454 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457 if (!ends_excmd(*skipwhite(p)))
23458 {
23459 vim_free(name);
23460 EMSG(_(e_trailing));
23461 return;
23462 }
23463 eap->nextcmd = check_nextcmd(p);
23464 if (eap->nextcmd != NULL)
23465 *p = NUL;
23466
23467 if (!eap->skip)
23468 fp = find_func(name);
23469 vim_free(name);
23470
23471 if (!eap->skip)
23472 {
23473 if (fp == NULL)
23474 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023475 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 return;
23477 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023478 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 {
23480 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23481 return;
23482 }
23483
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023484 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023486 /* Delete the dict item that refers to the function, it will
23487 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023488 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023490 else
23491 func_free(fp);
23492 }
23493}
23494
23495/*
23496 * Free a function and remove it from the list of functions.
23497 */
23498 static void
23499func_free(fp)
23500 ufunc_T *fp;
23501{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023502 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023503
23504 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023505 ga_clear_strings(&(fp->uf_args));
23506 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023507#ifdef FEAT_PROFILE
23508 vim_free(fp->uf_tml_count);
23509 vim_free(fp->uf_tml_total);
23510 vim_free(fp->uf_tml_self);
23511#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023512
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023513 /* remove the function from the function hashtable */
23514 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23515 if (HASHITEM_EMPTY(hi))
23516 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023517 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023518 hash_remove(&func_hashtab, hi);
23519
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023520 vim_free(fp);
23521}
23522
23523/*
23524 * Unreference a Function: decrement the reference count and free it when it
23525 * becomes zero. Only for numbered functions.
23526 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023527 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023528func_unref(name)
23529 char_u *name;
23530{
23531 ufunc_T *fp;
23532
23533 if (name != NULL && isdigit(*name))
23534 {
23535 fp = find_func(name);
23536 if (fp == NULL)
23537 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023538 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023539 {
23540 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023541 * when "uf_calls" becomes zero. */
23542 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023543 func_free(fp);
23544 }
23545 }
23546}
23547
23548/*
23549 * Count a reference to a Function.
23550 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023551 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023552func_ref(name)
23553 char_u *name;
23554{
23555 ufunc_T *fp;
23556
23557 if (name != NULL && isdigit(*name))
23558 {
23559 fp = find_func(name);
23560 if (fp == NULL)
23561 EMSG2(_(e_intern2), "func_ref()");
23562 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023563 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 }
23565}
23566
23567/*
23568 * Call a user function.
23569 */
23570 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023571call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572 ufunc_T *fp; /* pointer to function */
23573 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023574 typval_T *argvars; /* arguments */
23575 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576 linenr_T firstline; /* first line of range */
23577 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023578 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023579{
Bram Moolenaar33570922005-01-25 22:26:29 +000023580 char_u *save_sourcing_name;
23581 linenr_T save_sourcing_lnum;
23582 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023583 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023584 int save_did_emsg;
23585 static int depth = 0;
23586 dictitem_T *v;
23587 int fixvar_idx = 0; /* index in fixvar[] */
23588 int i;
23589 int ai;
23590 char_u numbuf[NUMBUFLEN];
23591 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023592#ifdef FEAT_PROFILE
23593 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023594 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596
23597 /* If depth of calling is getting too high, don't execute the function */
23598 if (depth >= p_mfd)
23599 {
23600 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023601 rettv->v_type = VAR_NUMBER;
23602 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 return;
23604 }
23605 ++depth;
23606
23607 line_breakcheck(); /* check for CTRL-C hit */
23608
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023609 fc = (funccall_T *)alloc(sizeof(funccall_T));
23610 fc->caller = current_funccal;
23611 current_funccal = fc;
23612 fc->func = fp;
23613 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023614 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023615 fc->linenr = 0;
23616 fc->returned = FALSE;
23617 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023619 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23620 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621
Bram Moolenaar33570922005-01-25 22:26:29 +000023622 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023623 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023624 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23625 * each argument variable and saves a lot of time.
23626 */
23627 /*
23628 * Init l: variables.
23629 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023630 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023631 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023632 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023633 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23634 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023635 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023636 name = v->di_key;
23637 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023638 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023639 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023640 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023641 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023642 v->di_tv.vval.v_dict = selfdict;
23643 ++selfdict->dv_refcount;
23644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023645
Bram Moolenaar33570922005-01-25 22:26:29 +000023646 /*
23647 * Init a: variables.
23648 * Set a:0 to "argcount".
23649 * Set a:000 to a list with room for the "..." arguments.
23650 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023651 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023652 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023653 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023654 /* Use "name" to avoid a warning from some compiler that checks the
23655 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023656 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023657 name = v->di_key;
23658 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023659 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023660 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023661 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023662 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023663 v->di_tv.vval.v_list = &fc->l_varlist;
23664 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23665 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23666 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023667
23668 /*
23669 * Set a:firstline to "firstline" and a:lastline to "lastline".
23670 * Set a:name to named arguments.
23671 * Set a:N to the "..." arguments.
23672 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023673 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023674 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023675 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023676 (varnumber_T)lastline);
23677 for (i = 0; i < argcount; ++i)
23678 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023679 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023680 if (ai < 0)
23681 /* named argument a:name */
23682 name = FUNCARG(fp, i);
23683 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023684 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023685 /* "..." argument a:1, a:2, etc. */
23686 sprintf((char *)numbuf, "%d", ai + 1);
23687 name = numbuf;
23688 }
23689 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23690 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023691 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023692 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23693 }
23694 else
23695 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023696 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23697 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023698 if (v == NULL)
23699 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023700 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023701 }
23702 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023703 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023704
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023705 /* Note: the values are copied directly to avoid alloc/free.
23706 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023707 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023708 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023709
23710 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23711 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023712 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23713 fc->l_listitems[ai].li_tv = argvars[i];
23714 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023715 }
23716 }
23717
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 /* Don't redraw while executing the function. */
23719 ++RedrawingDisabled;
23720 save_sourcing_name = sourcing_name;
23721 save_sourcing_lnum = sourcing_lnum;
23722 sourcing_lnum = 1;
23723 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023724 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023725 if (sourcing_name != NULL)
23726 {
23727 if (save_sourcing_name != NULL
23728 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23729 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23730 else
23731 STRCPY(sourcing_name, "function ");
23732 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23733
23734 if (p_verbose >= 12)
23735 {
23736 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023737 verbose_enter_scroll();
23738
Bram Moolenaar555b2802005-05-19 21:08:39 +000023739 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740 if (p_verbose >= 14)
23741 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023742 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023743 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023744 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023745 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746
23747 msg_puts((char_u *)"(");
23748 for (i = 0; i < argcount; ++i)
23749 {
23750 if (i > 0)
23751 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023752 if (argvars[i].v_type == VAR_NUMBER)
23753 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023754 else
23755 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023756 /* Do not want errors such as E724 here. */
23757 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023758 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023759 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023760 if (s != NULL)
23761 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023762 if (vim_strsize(s) > MSG_BUF_CLEN)
23763 {
23764 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23765 s = buf;
23766 }
23767 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023768 vim_free(tofree);
23769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770 }
23771 }
23772 msg_puts((char_u *)")");
23773 }
23774 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023775
23776 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023777 --no_wait_return;
23778 }
23779 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023780#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023781 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023782 {
23783 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23784 func_do_profile(fp);
23785 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023786 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023787 {
23788 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023789 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023790 profile_zero(&fp->uf_tm_children);
23791 }
23792 script_prof_save(&wait_start);
23793 }
23794#endif
23795
Bram Moolenaar071d4272004-06-13 20:20:40 +000023796 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023797 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 save_did_emsg = did_emsg;
23799 did_emsg = FALSE;
23800
23801 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023802 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23804
23805 --RedrawingDisabled;
23806
23807 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023808 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023810 clear_tv(rettv);
23811 rettv->v_type = VAR_NUMBER;
23812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813 }
23814
Bram Moolenaar05159a02005-02-26 23:04:13 +000023815#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023816 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023817 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023818 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023819 profile_end(&call_start);
23820 profile_sub_wait(&wait_start, &call_start);
23821 profile_add(&fp->uf_tm_total, &call_start);
23822 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023823 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023824 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023825 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23826 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023827 }
23828 }
23829#endif
23830
Bram Moolenaar071d4272004-06-13 20:20:40 +000023831 /* when being verbose, mention the return value */
23832 if (p_verbose >= 12)
23833 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023835 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023836
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023838 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023839 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023840 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023841 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023842 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023844 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023845 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023846 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023847 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023848
Bram Moolenaar555b2802005-05-19 21:08:39 +000023849 /* The value may be very long. Skip the middle part, so that we
23850 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023851 * truncate it at the end. Don't want errors such as E724 here. */
23852 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023853 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023854 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023855 if (s != NULL)
23856 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023857 if (vim_strsize(s) > MSG_BUF_CLEN)
23858 {
23859 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23860 s = buf;
23861 }
23862 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023863 vim_free(tofree);
23864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865 }
23866 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023867
23868 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 --no_wait_return;
23870 }
23871
23872 vim_free(sourcing_name);
23873 sourcing_name = save_sourcing_name;
23874 sourcing_lnum = save_sourcing_lnum;
23875 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023876#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023877 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023878 script_prof_restore(&wait_start);
23879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880
23881 if (p_verbose >= 12 && sourcing_name != NULL)
23882 {
23883 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023884 verbose_enter_scroll();
23885
Bram Moolenaar555b2802005-05-19 21:08:39 +000023886 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023888
23889 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 --no_wait_return;
23891 }
23892
23893 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023894 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023895 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023896
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023897 /* If the a:000 list and the l: and a: dicts are not referenced we can
23898 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023899 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23900 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23901 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23902 {
23903 free_funccal(fc, FALSE);
23904 }
23905 else
23906 {
23907 hashitem_T *hi;
23908 listitem_T *li;
23909 int todo;
23910
23911 /* "fc" is still in use. This can happen when returning "a:000" or
23912 * assigning "l:" to a global variable.
23913 * Link "fc" in the list for garbage collection later. */
23914 fc->caller = previous_funccal;
23915 previous_funccal = fc;
23916
23917 /* Make a copy of the a: variables, since we didn't do that above. */
23918 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23919 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23920 {
23921 if (!HASHITEM_EMPTY(hi))
23922 {
23923 --todo;
23924 v = HI2DI(hi);
23925 copy_tv(&v->di_tv, &v->di_tv);
23926 }
23927 }
23928
23929 /* Make a copy of the a:000 items, since we didn't do that above. */
23930 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23931 copy_tv(&li->li_tv, &li->li_tv);
23932 }
23933}
23934
23935/*
23936 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023937 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023938 */
23939 static int
23940can_free_funccal(fc, copyID)
23941 funccall_T *fc;
23942 int copyID;
23943{
23944 return (fc->l_varlist.lv_copyID != copyID
23945 && fc->l_vars.dv_copyID != copyID
23946 && fc->l_avars.dv_copyID != copyID);
23947}
23948
23949/*
23950 * Free "fc" and what it contains.
23951 */
23952 static void
23953free_funccal(fc, free_val)
23954 funccall_T *fc;
23955 int free_val; /* a: vars were allocated */
23956{
23957 listitem_T *li;
23958
23959 /* The a: variables typevals may not have been allocated, only free the
23960 * allocated variables. */
23961 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23962
23963 /* free all l: variables */
23964 vars_clear(&fc->l_vars.dv_hashtab);
23965
23966 /* Free the a:000 variables if they were allocated. */
23967 if (free_val)
23968 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23969 clear_tv(&li->li_tv);
23970
23971 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972}
23973
23974/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023975 * Add a number variable "name" to dict "dp" with value "nr".
23976 */
23977 static void
23978add_nr_var(dp, v, name, nr)
23979 dict_T *dp;
23980 dictitem_T *v;
23981 char *name;
23982 varnumber_T nr;
23983{
23984 STRCPY(v->di_key, name);
23985 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23986 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23987 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023988 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023989 v->di_tv.vval.v_number = nr;
23990}
23991
23992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993 * ":return [expr]"
23994 */
23995 void
23996ex_return(eap)
23997 exarg_T *eap;
23998{
23999 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024000 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 int returning = FALSE;
24002
24003 if (current_funccal == NULL)
24004 {
24005 EMSG(_("E133: :return not inside a function"));
24006 return;
24007 }
24008
24009 if (eap->skip)
24010 ++emsg_skip;
24011
24012 eap->nextcmd = NULL;
24013 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024014 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024015 {
24016 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024017 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024019 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020 }
24021 /* It's safer to return also on error. */
24022 else if (!eap->skip)
24023 {
24024 /*
24025 * Return unless the expression evaluation has been cancelled due to an
24026 * aborting error, an interrupt, or an exception.
24027 */
24028 if (!aborting())
24029 returning = do_return(eap, FALSE, TRUE, NULL);
24030 }
24031
24032 /* When skipping or the return gets pending, advance to the next command
24033 * in this line (!returning). Otherwise, ignore the rest of the line.
24034 * Following lines will be ignored by get_func_line(). */
24035 if (returning)
24036 eap->nextcmd = NULL;
24037 else if (eap->nextcmd == NULL) /* no argument */
24038 eap->nextcmd = check_nextcmd(arg);
24039
24040 if (eap->skip)
24041 --emsg_skip;
24042}
24043
24044/*
24045 * Return from a function. Possibly makes the return pending. Also called
24046 * for a pending return at the ":endtry" or after returning from an extra
24047 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024048 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024049 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 * FALSE when the return gets pending.
24051 */
24052 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024053do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 exarg_T *eap;
24055 int reanimate;
24056 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024057 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058{
24059 int idx;
24060 struct condstack *cstack = eap->cstack;
24061
24062 if (reanimate)
24063 /* Undo the return. */
24064 current_funccal->returned = FALSE;
24065
24066 /*
24067 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24068 * not in its finally clause (which then is to be executed next) is found.
24069 * In this case, make the ":return" pending for execution at the ":endtry".
24070 * Otherwise, return normally.
24071 */
24072 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24073 if (idx >= 0)
24074 {
24075 cstack->cs_pending[idx] = CSTP_RETURN;
24076
24077 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024078 /* A pending return again gets pending. "rettv" points to an
24079 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024080 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024081 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 else
24083 {
24084 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024085 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024086 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024087 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024089 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 {
24091 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024092 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024093 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 else
24095 EMSG(_(e_outofmem));
24096 }
24097 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024098 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024099
24100 if (reanimate)
24101 {
24102 /* The pending return value could be overwritten by a ":return"
24103 * without argument in a finally clause; reset the default
24104 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024105 current_funccal->rettv->v_type = VAR_NUMBER;
24106 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 }
24108 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024109 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110 }
24111 else
24112 {
24113 current_funccal->returned = TRUE;
24114
24115 /* If the return is carried out now, store the return value. For
24116 * a return immediately after reanimation, the value is already
24117 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024118 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024120 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024121 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024123 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 }
24125 }
24126
24127 return idx < 0;
24128}
24129
24130/*
24131 * Free the variable with a pending return value.
24132 */
24133 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024134discard_pending_return(rettv)
24135 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136{
Bram Moolenaar33570922005-01-25 22:26:29 +000024137 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138}
24139
24140/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024141 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142 * is an allocated string. Used by report_pending() for verbose messages.
24143 */
24144 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024145get_return_cmd(rettv)
24146 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024148 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024149 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024150 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024152 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024153 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024154 if (s == NULL)
24155 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024156
24157 STRCPY(IObuff, ":return ");
24158 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24159 if (STRLEN(s) + 8 >= IOSIZE)
24160 STRCPY(IObuff + IOSIZE - 4, "...");
24161 vim_free(tofree);
24162 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163}
24164
24165/*
24166 * Get next function line.
24167 * Called by do_cmdline() to get the next line.
24168 * Returns allocated string, or NULL for end of function.
24169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170 char_u *
24171get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024172 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024174 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175{
Bram Moolenaar33570922005-01-25 22:26:29 +000024176 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024177 ufunc_T *fp = fcp->func;
24178 char_u *retval;
24179 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024180
24181 /* If breakpoints have been added/deleted need to check for it. */
24182 if (fcp->dbg_tick != debug_tick)
24183 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024184 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 sourcing_lnum);
24186 fcp->dbg_tick = debug_tick;
24187 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024188#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024189 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024190 func_line_end(cookie);
24191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024192
Bram Moolenaar05159a02005-02-26 23:04:13 +000024193 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024194 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24195 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 retval = NULL;
24197 else
24198 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024199 /* Skip NULL lines (continuation lines). */
24200 while (fcp->linenr < gap->ga_len
24201 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24202 ++fcp->linenr;
24203 if (fcp->linenr >= gap->ga_len)
24204 retval = NULL;
24205 else
24206 {
24207 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24208 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024209#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024210 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024211 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024212#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 }
24215
24216 /* Did we encounter a breakpoint? */
24217 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24218 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024219 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024221 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222 sourcing_lnum);
24223 fcp->dbg_tick = debug_tick;
24224 }
24225
24226 return retval;
24227}
24228
Bram Moolenaar05159a02005-02-26 23:04:13 +000024229#if defined(FEAT_PROFILE) || defined(PROTO)
24230/*
24231 * Called when starting to read a function line.
24232 * "sourcing_lnum" must be correct!
24233 * When skipping lines it may not actually be executed, but we won't find out
24234 * until later and we need to store the time now.
24235 */
24236 void
24237func_line_start(cookie)
24238 void *cookie;
24239{
24240 funccall_T *fcp = (funccall_T *)cookie;
24241 ufunc_T *fp = fcp->func;
24242
24243 if (fp->uf_profiling && sourcing_lnum >= 1
24244 && sourcing_lnum <= fp->uf_lines.ga_len)
24245 {
24246 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024247 /* Skip continuation lines. */
24248 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24249 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024250 fp->uf_tml_execed = FALSE;
24251 profile_start(&fp->uf_tml_start);
24252 profile_zero(&fp->uf_tml_children);
24253 profile_get_wait(&fp->uf_tml_wait);
24254 }
24255}
24256
24257/*
24258 * Called when actually executing a function line.
24259 */
24260 void
24261func_line_exec(cookie)
24262 void *cookie;
24263{
24264 funccall_T *fcp = (funccall_T *)cookie;
24265 ufunc_T *fp = fcp->func;
24266
24267 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24268 fp->uf_tml_execed = TRUE;
24269}
24270
24271/*
24272 * Called when done with a function line.
24273 */
24274 void
24275func_line_end(cookie)
24276 void *cookie;
24277{
24278 funccall_T *fcp = (funccall_T *)cookie;
24279 ufunc_T *fp = fcp->func;
24280
24281 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24282 {
24283 if (fp->uf_tml_execed)
24284 {
24285 ++fp->uf_tml_count[fp->uf_tml_idx];
24286 profile_end(&fp->uf_tml_start);
24287 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024288 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024289 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24290 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024291 }
24292 fp->uf_tml_idx = -1;
24293 }
24294}
24295#endif
24296
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297/*
24298 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024299 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300 */
24301 int
24302func_has_ended(cookie)
24303 void *cookie;
24304{
Bram Moolenaar33570922005-01-25 22:26:29 +000024305 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024306
24307 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24308 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024309 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024310 || fcp->returned);
24311}
24312
24313/*
24314 * return TRUE if cookie indicates a function which "abort"s on errors.
24315 */
24316 int
24317func_has_abort(cookie)
24318 void *cookie;
24319{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024320 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321}
24322
24323#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24324typedef enum
24325{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024326 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24327 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24328 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329} var_flavour_T;
24330
24331static var_flavour_T var_flavour __ARGS((char_u *varname));
24332
24333 static var_flavour_T
24334var_flavour(varname)
24335 char_u *varname;
24336{
24337 char_u *p = varname;
24338
24339 if (ASCII_ISUPPER(*p))
24340 {
24341 while (*(++p))
24342 if (ASCII_ISLOWER(*p))
24343 return VAR_FLAVOUR_SESSION;
24344 return VAR_FLAVOUR_VIMINFO;
24345 }
24346 else
24347 return VAR_FLAVOUR_DEFAULT;
24348}
24349#endif
24350
24351#if defined(FEAT_VIMINFO) || defined(PROTO)
24352/*
24353 * Restore global vars that start with a capital from the viminfo file
24354 */
24355 int
24356read_viminfo_varlist(virp, writing)
24357 vir_T *virp;
24358 int writing;
24359{
24360 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024361 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024362 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363
24364 if (!writing && (find_viminfo_parameter('!') != NULL))
24365 {
24366 tab = vim_strchr(virp->vir_line + 1, '\t');
24367 if (tab != NULL)
24368 {
24369 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024370 switch (*tab)
24371 {
24372 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024373#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024374 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024375#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024376 case 'D': type = VAR_DICT; break;
24377 case 'L': type = VAR_LIST; break;
24378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379
24380 tab = vim_strchr(tab, '\t');
24381 if (tab != NULL)
24382 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024383 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024384 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024385 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024387#ifdef FEAT_FLOAT
24388 else if (type == VAR_FLOAT)
24389 (void)string2float(tab + 1, &tv.vval.v_float);
24390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024392 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024393 if (type == VAR_DICT || type == VAR_LIST)
24394 {
24395 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24396
24397 if (etv == NULL)
24398 /* Failed to parse back the dict or list, use it as a
24399 * string. */
24400 tv.v_type = VAR_STRING;
24401 else
24402 {
24403 vim_free(tv.vval.v_string);
24404 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024405 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024406 }
24407 }
24408
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024409 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024410
24411 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024412 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024413 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24414 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024415 }
24416 }
24417 }
24418
24419 return viminfo_readline(virp);
24420}
24421
24422/*
24423 * Write global vars that start with a capital to the viminfo file
24424 */
24425 void
24426write_viminfo_varlist(fp)
24427 FILE *fp;
24428{
Bram Moolenaar33570922005-01-25 22:26:29 +000024429 hashitem_T *hi;
24430 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024431 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024432 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024433 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024434 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024435 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436
24437 if (find_viminfo_parameter('!') == NULL)
24438 return;
24439
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024440 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024441
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024442 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024443 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024444 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024445 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024447 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024448 this_var = HI2DI(hi);
24449 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024450 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024451 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024452 {
24453 case VAR_STRING: s = "STR"; break;
24454 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024455#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024456 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024457#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024458 case VAR_DICT: s = "DIC"; break;
24459 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024460 default: continue;
24461 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024462 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024463 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024464 if (p != NULL)
24465 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024466 vim_free(tofree);
24467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024468 }
24469 }
24470}
24471#endif
24472
24473#if defined(FEAT_SESSION) || defined(PROTO)
24474 int
24475store_session_globals(fd)
24476 FILE *fd;
24477{
Bram Moolenaar33570922005-01-25 22:26:29 +000024478 hashitem_T *hi;
24479 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024480 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 char_u *p, *t;
24482
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024483 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024484 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024485 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024486 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024488 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024489 this_var = HI2DI(hi);
24490 if ((this_var->di_tv.v_type == VAR_NUMBER
24491 || this_var->di_tv.v_type == VAR_STRING)
24492 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024493 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024494 /* Escape special characters with a backslash. Turn a LF and
24495 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024496 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024497 (char_u *)"\\\"\n\r");
24498 if (p == NULL) /* out of memory */
24499 break;
24500 for (t = p; *t != NUL; ++t)
24501 if (*t == '\n')
24502 *t = 'n';
24503 else if (*t == '\r')
24504 *t = 'r';
24505 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024506 this_var->di_key,
24507 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24508 : ' ',
24509 p,
24510 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24511 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024512 || put_eol(fd) == FAIL)
24513 {
24514 vim_free(p);
24515 return FAIL;
24516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024517 vim_free(p);
24518 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024519#ifdef FEAT_FLOAT
24520 else if (this_var->di_tv.v_type == VAR_FLOAT
24521 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24522 {
24523 float_T f = this_var->di_tv.vval.v_float;
24524 int sign = ' ';
24525
24526 if (f < 0)
24527 {
24528 f = -f;
24529 sign = '-';
24530 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024531 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024532 this_var->di_key, sign, f) < 0)
24533 || put_eol(fd) == FAIL)
24534 return FAIL;
24535 }
24536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 }
24538 }
24539 return OK;
24540}
24541#endif
24542
Bram Moolenaar661b1822005-07-28 22:36:45 +000024543/*
24544 * Display script name where an item was last set.
24545 * Should only be invoked when 'verbose' is non-zero.
24546 */
24547 void
24548last_set_msg(scriptID)
24549 scid_T scriptID;
24550{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024551 char_u *p;
24552
Bram Moolenaar661b1822005-07-28 22:36:45 +000024553 if (scriptID != 0)
24554 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024555 p = home_replace_save(NULL, get_scriptname(scriptID));
24556 if (p != NULL)
24557 {
24558 verbose_enter();
24559 MSG_PUTS(_("\n\tLast set from "));
24560 MSG_PUTS(p);
24561 vim_free(p);
24562 verbose_leave();
24563 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024564 }
24565}
24566
Bram Moolenaard812df62008-11-09 12:46:09 +000024567/*
24568 * List v:oldfiles in a nice way.
24569 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024570 void
24571ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024572 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024573{
24574 list_T *l = vimvars[VV_OLDFILES].vv_list;
24575 listitem_T *li;
24576 int nr = 0;
24577
24578 if (l == NULL)
24579 msg((char_u *)_("No old files"));
24580 else
24581 {
24582 msg_start();
24583 msg_scroll = TRUE;
24584 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24585 {
24586 msg_outnum((long)++nr);
24587 MSG_PUTS(": ");
24588 msg_outtrans(get_tv_string(&li->li_tv));
24589 msg_putchar('\n');
24590 out_flush(); /* output one line at a time */
24591 ui_breakcheck();
24592 }
24593 /* Assume "got_int" was set to truncate the listing. */
24594 got_int = FALSE;
24595
24596#ifdef FEAT_BROWSE_CMD
24597 if (cmdmod.browse)
24598 {
24599 quit_more = FALSE;
24600 nr = prompt_for_number(FALSE);
24601 msg_starthere();
24602 if (nr > 0)
24603 {
24604 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24605 (long)nr);
24606
24607 if (p != NULL)
24608 {
24609 p = expand_env_save(p);
24610 eap->arg = p;
24611 eap->cmdidx = CMD_edit;
24612 cmdmod.browse = FALSE;
24613 do_exedit(eap, NULL);
24614 vim_free(p);
24615 }
24616 }
24617 }
24618#endif
24619 }
24620}
24621
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622#endif /* FEAT_EVAL */
24623
Bram Moolenaar071d4272004-06-13 20:20:40 +000024624
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024625#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626
24627#ifdef WIN3264
24628/*
24629 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24630 */
24631static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24632static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24633static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24634
24635/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024636 * Get the short path (8.3) for the filename in "fnamep".
24637 * Only works for a valid file name.
24638 * When the path gets longer "fnamep" is changed and the allocated buffer
24639 * is put in "bufp".
24640 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24641 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642 */
24643 static int
24644get_short_pathname(fnamep, bufp, fnamelen)
24645 char_u **fnamep;
24646 char_u **bufp;
24647 int *fnamelen;
24648{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024649 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 char_u *newbuf;
24651
24652 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 l = GetShortPathName(*fnamep, *fnamep, len);
24654 if (l > len - 1)
24655 {
24656 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024657 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658 newbuf = vim_strnsave(*fnamep, l);
24659 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024660 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661
24662 vim_free(*bufp);
24663 *fnamep = *bufp = newbuf;
24664
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024665 /* Really should always succeed, as the buffer is big enough. */
24666 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 }
24668
24669 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024670 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024671}
24672
24673/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024674 * Get the short path (8.3) for the filename in "fname". The converted
24675 * path is returned in "bufp".
24676 *
24677 * Some of the directories specified in "fname" may not exist. This function
24678 * will shorten the existing directories at the beginning of the path and then
24679 * append the remaining non-existing path.
24680 *
24681 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024682 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024683 * bufp - Pointer to an allocated buffer for the filename.
24684 * fnamelen - Length of the filename pointed to by fname
24685 *
24686 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024687 */
24688 static int
24689shortpath_for_invalid_fname(fname, bufp, fnamelen)
24690 char_u **fname;
24691 char_u **bufp;
24692 int *fnamelen;
24693{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024694 char_u *short_fname, *save_fname, *pbuf_unused;
24695 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024696 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024697 int old_len, len;
24698 int new_len, sfx_len;
24699 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700
24701 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024702 old_len = *fnamelen;
24703 save_fname = vim_strnsave(*fname, old_len);
24704 pbuf_unused = NULL;
24705 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024707 endp = save_fname + old_len - 1; /* Find the end of the copy */
24708 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024709
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024710 /*
24711 * Try shortening the supplied path till it succeeds by removing one
24712 * directory at a time from the tail of the path.
24713 */
24714 len = 0;
24715 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024716 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024717 /* go back one path-separator */
24718 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24719 --endp;
24720 if (endp <= save_fname)
24721 break; /* processed the complete path */
24722
24723 /*
24724 * Replace the path separator with a NUL and try to shorten the
24725 * resulting path.
24726 */
24727 ch = *endp;
24728 *endp = 0;
24729 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024730 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024731 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24732 {
24733 retval = FAIL;
24734 goto theend;
24735 }
24736 *endp = ch; /* preserve the string */
24737
24738 if (len > 0)
24739 break; /* successfully shortened the path */
24740
24741 /* failed to shorten the path. Skip the path separator */
24742 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024743 }
24744
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024745 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024746 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024747 /*
24748 * Succeeded in shortening the path. Now concatenate the shortened
24749 * path with the remaining path at the tail.
24750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024752 /* Compute the length of the new path. */
24753 sfx_len = (int)(save_endp - endp) + 1;
24754 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024755
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024756 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024757 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024758 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024759 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024760 /* There is not enough space in the currently allocated string,
24761 * copy it to a buffer big enough. */
24762 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024764 {
24765 retval = FAIL;
24766 goto theend;
24767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768 }
24769 else
24770 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024771 /* Transfer short_fname to the main buffer (it's big enough),
24772 * unless get_short_pathname() did its work in-place. */
24773 *fname = *bufp = save_fname;
24774 if (short_fname != save_fname)
24775 vim_strncpy(save_fname, short_fname, len);
24776 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024778
24779 /* concat the not-shortened part of the path */
24780 vim_strncpy(*fname + len, endp, sfx_len);
24781 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024782 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024783
24784theend:
24785 vim_free(pbuf_unused);
24786 vim_free(save_fname);
24787
24788 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789}
24790
24791/*
24792 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024793 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024794 */
24795 static int
24796shortpath_for_partial(fnamep, bufp, fnamelen)
24797 char_u **fnamep;
24798 char_u **bufp;
24799 int *fnamelen;
24800{
24801 int sepcount, len, tflen;
24802 char_u *p;
24803 char_u *pbuf, *tfname;
24804 int hasTilde;
24805
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024806 /* Count up the path separators from the RHS.. so we know which part
24807 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024808 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024809 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024810 if (vim_ispathsep(*p))
24811 ++sepcount;
24812
24813 /* Need full path first (use expand_env() to remove a "~/") */
24814 hasTilde = (**fnamep == '~');
24815 if (hasTilde)
24816 pbuf = tfname = expand_env_save(*fnamep);
24817 else
24818 pbuf = tfname = FullName_save(*fnamep, FALSE);
24819
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024820 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024821
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024822 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24823 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824
24825 if (len == 0)
24826 {
24827 /* Don't have a valid filename, so shorten the rest of the
24828 * path if we can. This CAN give us invalid 8.3 filenames, but
24829 * there's not a lot of point in guessing what it might be.
24830 */
24831 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024832 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24833 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024834 }
24835
24836 /* Count the paths backward to find the beginning of the desired string. */
24837 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024838 {
24839#ifdef FEAT_MBYTE
24840 if (has_mbyte)
24841 p -= mb_head_off(tfname, p);
24842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843 if (vim_ispathsep(*p))
24844 {
24845 if (sepcount == 0 || (hasTilde && sepcount == 1))
24846 break;
24847 else
24848 sepcount --;
24849 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 if (hasTilde)
24852 {
24853 --p;
24854 if (p >= tfname)
24855 *p = '~';
24856 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024857 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024858 }
24859 else
24860 ++p;
24861
24862 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24863 vim_free(*bufp);
24864 *fnamelen = (int)STRLEN(p);
24865 *bufp = pbuf;
24866 *fnamep = p;
24867
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024868 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869}
24870#endif /* WIN3264 */
24871
24872/*
24873 * Adjust a filename, according to a string of modifiers.
24874 * *fnamep must be NUL terminated when called. When returning, the length is
24875 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024876 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877 * When there is an error, *fnamep is set to NULL.
24878 */
24879 int
24880modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24881 char_u *src; /* string with modifiers */
24882 int *usedlen; /* characters after src that are used */
24883 char_u **fnamep; /* file name so far */
24884 char_u **bufp; /* buffer for allocated file name or NULL */
24885 int *fnamelen; /* length of fnamep */
24886{
24887 int valid = 0;
24888 char_u *tail;
24889 char_u *s, *p, *pbuf;
24890 char_u dirname[MAXPATHL];
24891 int c;
24892 int has_fullname = 0;
24893#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024894 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895 int has_shortname = 0;
24896#endif
24897
24898repeat:
24899 /* ":p" - full path/file_name */
24900 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24901 {
24902 has_fullname = 1;
24903
24904 valid |= VALID_PATH;
24905 *usedlen += 2;
24906
24907 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24908 if ((*fnamep)[0] == '~'
24909#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24910 && ((*fnamep)[1] == '/'
24911# ifdef BACKSLASH_IN_FILENAME
24912 || (*fnamep)[1] == '\\'
24913# endif
24914 || (*fnamep)[1] == NUL)
24915
24916#endif
24917 )
24918 {
24919 *fnamep = expand_env_save(*fnamep);
24920 vim_free(*bufp); /* free any allocated file name */
24921 *bufp = *fnamep;
24922 if (*fnamep == NULL)
24923 return -1;
24924 }
24925
24926 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024927 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928 {
24929 if (vim_ispathsep(*p)
24930 && p[1] == '.'
24931 && (p[2] == NUL
24932 || vim_ispathsep(p[2])
24933 || (p[2] == '.'
24934 && (p[3] == NUL || vim_ispathsep(p[3])))))
24935 break;
24936 }
24937
24938 /* FullName_save() is slow, don't use it when not needed. */
24939 if (*p != NUL || !vim_isAbsName(*fnamep))
24940 {
24941 *fnamep = FullName_save(*fnamep, *p != NUL);
24942 vim_free(*bufp); /* free any allocated file name */
24943 *bufp = *fnamep;
24944 if (*fnamep == NULL)
24945 return -1;
24946 }
24947
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024948#ifdef WIN3264
24949# if _WIN32_WINNT >= 0x0500
24950 if (vim_strchr(*fnamep, '~') != NULL)
24951 {
24952 /* Expand 8.3 filename to full path. Needed to make sure the same
24953 * file does not have two different names.
24954 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24955 p = alloc(_MAX_PATH + 1);
24956 if (p != NULL)
24957 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010024958 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024959 {
24960 vim_free(*bufp);
24961 *bufp = *fnamep = p;
24962 }
24963 else
24964 vim_free(p);
24965 }
24966 }
24967# endif
24968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024969 /* Append a path separator to a directory. */
24970 if (mch_isdir(*fnamep))
24971 {
24972 /* Make room for one or two extra characters. */
24973 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24974 vim_free(*bufp); /* free any allocated file name */
24975 *bufp = *fnamep;
24976 if (*fnamep == NULL)
24977 return -1;
24978 add_pathsep(*fnamep);
24979 }
24980 }
24981
24982 /* ":." - path relative to the current directory */
24983 /* ":~" - path relative to the home directory */
24984 /* ":8" - shortname path - postponed till after */
24985 while (src[*usedlen] == ':'
24986 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24987 {
24988 *usedlen += 2;
24989 if (c == '8')
24990 {
24991#ifdef WIN3264
24992 has_shortname = 1; /* Postpone this. */
24993#endif
24994 continue;
24995 }
24996 pbuf = NULL;
24997 /* Need full path first (use expand_env() to remove a "~/") */
24998 if (!has_fullname)
24999 {
25000 if (c == '.' && **fnamep == '~')
25001 p = pbuf = expand_env_save(*fnamep);
25002 else
25003 p = pbuf = FullName_save(*fnamep, FALSE);
25004 }
25005 else
25006 p = *fnamep;
25007
25008 has_fullname = 0;
25009
25010 if (p != NULL)
25011 {
25012 if (c == '.')
25013 {
25014 mch_dirname(dirname, MAXPATHL);
25015 s = shorten_fname(p, dirname);
25016 if (s != NULL)
25017 {
25018 *fnamep = s;
25019 if (pbuf != NULL)
25020 {
25021 vim_free(*bufp); /* free any allocated file name */
25022 *bufp = pbuf;
25023 pbuf = NULL;
25024 }
25025 }
25026 }
25027 else
25028 {
25029 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25030 /* Only replace it when it starts with '~' */
25031 if (*dirname == '~')
25032 {
25033 s = vim_strsave(dirname);
25034 if (s != NULL)
25035 {
25036 *fnamep = s;
25037 vim_free(*bufp);
25038 *bufp = s;
25039 }
25040 }
25041 }
25042 vim_free(pbuf);
25043 }
25044 }
25045
25046 tail = gettail(*fnamep);
25047 *fnamelen = (int)STRLEN(*fnamep);
25048
25049 /* ":h" - head, remove "/file_name", can be repeated */
25050 /* Don't remove the first "/" or "c:\" */
25051 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25052 {
25053 valid |= VALID_HEAD;
25054 *usedlen += 2;
25055 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025056 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025057 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058 *fnamelen = (int)(tail - *fnamep);
25059#ifdef VMS
25060 if (*fnamelen > 0)
25061 *fnamelen += 1; /* the path separator is part of the path */
25062#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025063 if (*fnamelen == 0)
25064 {
25065 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25066 p = vim_strsave((char_u *)".");
25067 if (p == NULL)
25068 return -1;
25069 vim_free(*bufp);
25070 *bufp = *fnamep = tail = p;
25071 *fnamelen = 1;
25072 }
25073 else
25074 {
25075 while (tail > s && !after_pathsep(s, tail))
25076 mb_ptr_back(*fnamep, tail);
25077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025078 }
25079
25080 /* ":8" - shortname */
25081 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25082 {
25083 *usedlen += 2;
25084#ifdef WIN3264
25085 has_shortname = 1;
25086#endif
25087 }
25088
25089#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025090 /*
25091 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092 */
25093 if (has_shortname)
25094 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025095 /* Copy the string if it is shortened by :h and when it wasn't copied
25096 * yet, because we are going to change it in place. Avoids changing
25097 * the buffer name for "%:8". */
25098 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025099 {
25100 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025101 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025102 return -1;
25103 vim_free(*bufp);
25104 *bufp = *fnamep = p;
25105 }
25106
25107 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025108 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109 if (!has_fullname && !vim_isAbsName(*fnamep))
25110 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025111 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112 return -1;
25113 }
25114 else
25115 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025116 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117
Bram Moolenaardc935552011-08-17 15:23:23 +020025118 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025119 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025120 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025121 return -1;
25122
25123 if (l == 0)
25124 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025125 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025126 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025127 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128 return -1;
25129 }
25130 *fnamelen = l;
25131 }
25132 }
25133#endif /* WIN3264 */
25134
25135 /* ":t" - tail, just the basename */
25136 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25137 {
25138 *usedlen += 2;
25139 *fnamelen -= (int)(tail - *fnamep);
25140 *fnamep = tail;
25141 }
25142
25143 /* ":e" - extension, can be repeated */
25144 /* ":r" - root, without extension, can be repeated */
25145 while (src[*usedlen] == ':'
25146 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25147 {
25148 /* find a '.' in the tail:
25149 * - for second :e: before the current fname
25150 * - otherwise: The last '.'
25151 */
25152 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25153 s = *fnamep - 2;
25154 else
25155 s = *fnamep + *fnamelen - 1;
25156 for ( ; s > tail; --s)
25157 if (s[0] == '.')
25158 break;
25159 if (src[*usedlen + 1] == 'e') /* :e */
25160 {
25161 if (s > tail)
25162 {
25163 *fnamelen += (int)(*fnamep - (s + 1));
25164 *fnamep = s + 1;
25165#ifdef VMS
25166 /* cut version from the extension */
25167 s = *fnamep + *fnamelen - 1;
25168 for ( ; s > *fnamep; --s)
25169 if (s[0] == ';')
25170 break;
25171 if (s > *fnamep)
25172 *fnamelen = s - *fnamep;
25173#endif
25174 }
25175 else if (*fnamep <= tail)
25176 *fnamelen = 0;
25177 }
25178 else /* :r */
25179 {
25180 if (s > tail) /* remove one extension */
25181 *fnamelen = (int)(s - *fnamep);
25182 }
25183 *usedlen += 2;
25184 }
25185
25186 /* ":s?pat?foo?" - substitute */
25187 /* ":gs?pat?foo?" - global substitute */
25188 if (src[*usedlen] == ':'
25189 && (src[*usedlen + 1] == 's'
25190 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25191 {
25192 char_u *str;
25193 char_u *pat;
25194 char_u *sub;
25195 int sep;
25196 char_u *flags;
25197 int didit = FALSE;
25198
25199 flags = (char_u *)"";
25200 s = src + *usedlen + 2;
25201 if (src[*usedlen + 1] == 'g')
25202 {
25203 flags = (char_u *)"g";
25204 ++s;
25205 }
25206
25207 sep = *s++;
25208 if (sep)
25209 {
25210 /* find end of pattern */
25211 p = vim_strchr(s, sep);
25212 if (p != NULL)
25213 {
25214 pat = vim_strnsave(s, (int)(p - s));
25215 if (pat != NULL)
25216 {
25217 s = p + 1;
25218 /* find end of substitution */
25219 p = vim_strchr(s, sep);
25220 if (p != NULL)
25221 {
25222 sub = vim_strnsave(s, (int)(p - s));
25223 str = vim_strnsave(*fnamep, *fnamelen);
25224 if (sub != NULL && str != NULL)
25225 {
25226 *usedlen = (int)(p + 1 - src);
25227 s = do_string_sub(str, pat, sub, flags);
25228 if (s != NULL)
25229 {
25230 *fnamep = s;
25231 *fnamelen = (int)STRLEN(s);
25232 vim_free(*bufp);
25233 *bufp = s;
25234 didit = TRUE;
25235 }
25236 }
25237 vim_free(sub);
25238 vim_free(str);
25239 }
25240 vim_free(pat);
25241 }
25242 }
25243 /* after using ":s", repeat all the modifiers */
25244 if (didit)
25245 goto repeat;
25246 }
25247 }
25248
Bram Moolenaar26df0922014-02-23 23:39:13 +010025249 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25250 {
25251 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25252 if (p == NULL)
25253 return -1;
25254 vim_free(*bufp);
25255 *bufp = *fnamep = p;
25256 *fnamelen = (int)STRLEN(p);
25257 *usedlen += 2;
25258 }
25259
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260 return valid;
25261}
25262
25263/*
25264 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25265 * "flags" can be "g" to do a global substitute.
25266 * Returns an allocated string, NULL for error.
25267 */
25268 char_u *
25269do_string_sub(str, pat, sub, flags)
25270 char_u *str;
25271 char_u *pat;
25272 char_u *sub;
25273 char_u *flags;
25274{
25275 int sublen;
25276 regmatch_T regmatch;
25277 int i;
25278 int do_all;
25279 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025280 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281 garray_T ga;
25282 char_u *ret;
25283 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025284 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285
25286 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25287 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025288 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289
25290 ga_init2(&ga, 1, 200);
25291
25292 do_all = (flags[0] == 'g');
25293
25294 regmatch.rm_ic = p_ic;
25295 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25296 if (regmatch.regprog != NULL)
25297 {
25298 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025299 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025300 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25301 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025302 /* Skip empty match except for first match. */
25303 if (regmatch.startp[0] == regmatch.endp[0])
25304 {
25305 if (zero_width == regmatch.startp[0])
25306 {
25307 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025308 i = MB_PTR2LEN(tail);
25309 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25310 (size_t)i);
25311 ga.ga_len += i;
25312 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025313 continue;
25314 }
25315 zero_width = regmatch.startp[0];
25316 }
25317
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318 /*
25319 * Get some space for a temporary buffer to do the substitution
25320 * into. It will contain:
25321 * - The text up to where the match is.
25322 * - The substituted text.
25323 * - The text after the match.
25324 */
25325 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025326 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025327 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25328 {
25329 ga_clear(&ga);
25330 break;
25331 }
25332
25333 /* copy the text up to where the match is */
25334 i = (int)(regmatch.startp[0] - tail);
25335 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25336 /* add the substituted text */
25337 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25338 + ga.ga_len + i, TRUE, TRUE, FALSE);
25339 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025340 tail = regmatch.endp[0];
25341 if (*tail == NUL)
25342 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 if (!do_all)
25344 break;
25345 }
25346
25347 if (ga.ga_data != NULL)
25348 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25349
Bram Moolenaar473de612013-06-08 18:19:48 +020025350 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025351 }
25352
25353 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25354 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025355 if (p_cpo == empty_option)
25356 p_cpo = save_cpo;
25357 else
25358 /* Darn, evaluating {sub} expression changed the value. */
25359 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360
25361 return ret;
25362}
25363
25364#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */