blob: 76dd65387a6cbf526b19563ed4e484b7d015551f [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 Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100371 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000372};
373
374/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_type vv_di.di_tv.v_type
376#define vv_nr vv_di.di_tv.vval.v_number
377#define vv_float vv_di.di_tv.vval.v_float
378#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000379#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200380#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000381#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000382
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200383static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000384#define vimvarht vimvardict.dv_hashtab
385
Bram Moolenaara40058a2005-07-11 22:42:07 +0000386static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
387static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000388static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
389static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
390static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
392static void list_glob_vars __ARGS((int *first));
393static void list_buf_vars __ARGS((int *first));
394static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_vim_vars __ARGS((int *first));
399static void list_script_vars __ARGS((int *first));
400static void list_func_vars __ARGS((int *first));
401static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
403static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100404static 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 +0000405static void clear_lval __ARGS((lval_T *lp));
406static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
407static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000434static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
438static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100441static 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 +0000442static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000443static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200444static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
446static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
452static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000454#ifdef FEAT_FLOAT
455static int string2float __ARGS((char_u *text, float_T *value));
456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
458static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100459static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static 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 +0200461static 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 +0000462static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000463static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100470static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200474static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100476static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200480static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200482static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100493static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100495static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
498static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
499#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000500static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000503static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000505#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000506static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
509#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512#ifdef FEAT_FLOAT
513static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200514static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
519static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200529static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200531#ifdef FEAT_FLOAT
532static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
533#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000536static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#ifdef FEAT_FLOAT
543static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200545static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000547static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000556static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000558static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200562static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200566static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000574static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000575static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200576static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000577static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000578static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200581static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000582static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100588static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000591static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000605static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100610static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000612static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200625static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200628#ifdef FEAT_LUA
629static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000635static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200636static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000638static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000640static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000644#ifdef vim_mkdir
645static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100648#ifdef FEAT_MZSCHEME
649static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100653static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000654static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#ifdef FEAT_FLOAT
656static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
657#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000659static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000660static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200661#ifdef FEAT_PYTHON3
662static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
663#endif
664#ifdef FEAT_PYTHON
665static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000668static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000669static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000681#ifdef FEAT_FLOAT
682static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
683#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200684static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100686static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000689static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000691static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200696static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000699static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000700static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000701static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000702static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200704static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000705static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100707#ifdef FEAT_CRYPT
708static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
709#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000710static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200711static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000713#ifdef FEAT_FLOAT
714static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200715static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000718static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000719static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000722#ifdef FEAT_FLOAT
723static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
725#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000726static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728#ifdef HAVE_STRFTIME
729static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
730#endif
731static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200737static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200738static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000744static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200745static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200747static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000748static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000749static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000750static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000751static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000752static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000754static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200755#ifdef FEAT_FLOAT
756static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
758#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
763static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
764#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200766static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200767static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100768static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
770static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100772static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
775static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
777static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000779static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
780static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000782static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaared767a22016-01-03 22:49:16 +0100783static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100784static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785
Bram Moolenaar493c1782014-05-28 14:34:46 +0200786static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000787static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static int get_env_len __ARGS((char_u **arg));
789static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000790static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000791static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
792#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
793#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
794 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000795static 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 +0000796static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000797static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200798static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000799static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static typval_T *alloc_tv __ARGS((void));
801static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static void init_tv __ARGS((typval_T *varp));
803static long get_tv_number __ARGS((typval_T *varp));
804static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000805static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000806static char_u *get_tv_string __ARGS((typval_T *varp));
807static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000808static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100809static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
810static 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 +0000811static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
812static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
813static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000814static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
815static 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 +0000816static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200817static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
818static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200819static int var_check_func_name __ARGS((char_u *name, int new_var));
820static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200821static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000822static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
824static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
825static int eval_fname_script __ARGS((char_u *p));
826static int eval_fname_sid __ARGS((char_u *p));
827static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static ufunc_T *find_func __ARGS((char_u *name));
829static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200830static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000831#ifdef FEAT_PROFILE
832static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000833static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
834static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
835static int
836# ifdef __BORLANDC__
837 _RTLENTRYF
838# endif
839 prof_total_cmp __ARGS((const void *s1, const void *s2));
840static int
841# ifdef __BORLANDC__
842 _RTLENTRYF
843# endif
844 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200846static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000847static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000848static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000850static 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 +0000851static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
852static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000853static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000854static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
855static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000856static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000857static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000858static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200859static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200860static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000861
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200862
863#ifdef EBCDIC
864static int compare_func_name __ARGS((const void *s1, const void *s2));
865static void sortFunctions __ARGS(());
866#endif
867
Bram Moolenaar33570922005-01-25 22:26:29 +0000868/*
869 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000870 */
871 void
872eval_init()
873{
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 int i;
875 struct vimvar *p;
876
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200877 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
878 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200879 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000880 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000881 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000882
883 for (i = 0; i < VV_LEN; ++i)
884 {
885 p = &vimvars[i];
886 STRCPY(p->vv_di.di_key, p->vv_name);
887 if (p->vv_flags & VV_RO)
888 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
889 else if (p->vv_flags & VV_RO_SBX)
890 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
891 else
892 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000893
894 /* add to v: scope dict, unless the value is not always available */
895 if (p->vv_type != VAR_UNKNOWN)
896 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000897 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000898 /* add to compat scope dict */
899 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000900 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000901 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100902 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200903 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100904 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200905 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200906
907#ifdef EBCDIC
908 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100909 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200910 */
911 sortFunctions();
912#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000913}
914
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915#if defined(EXITFREE) || defined(PROTO)
916 void
917eval_clear()
918{
919 int i;
920 struct vimvar *p;
921
922 for (i = 0; i < VV_LEN; ++i)
923 {
924 p = &vimvars[i];
925 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000927 vim_free(p->vv_str);
928 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000929 }
930 else if (p->vv_di.di_tv.v_type == VAR_LIST)
931 {
932 list_unref(p->vv_list);
933 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000934 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000935 }
936 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000937 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000938 hash_clear(&compat_hashtab);
939
Bram Moolenaard9fba312005-06-26 22:34:35 +0000940 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100941# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200942 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100943# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000944
945 /* global variables */
946 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000947
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000948 /* autoloaded script names */
949 ga_clear_strings(&ga_loaded);
950
Bram Moolenaarcca74132013-09-25 21:00:28 +0200951 /* Script-local variables. First clear all the variables and in a second
952 * loop free the scriptvar_T, because a variable in one script might hold
953 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200954 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200955 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200956 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200957 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200958 ga_clear(&ga_scripts);
959
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000960 /* unreferenced lists and dicts */
961 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000962
963 /* functions */
964 free_all_functions();
965 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000966}
967#endif
968
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 * Return the name of the executed function.
971 */
972 char_u *
973func_name(cookie)
974 void *cookie;
975{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000976 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/*
980 * Return the address holding the next breakpoint line for a funccall cookie.
981 */
982 linenr_T *
983func_breakpoint(cookie)
984 void *cookie;
985{
Bram Moolenaar33570922005-01-25 22:26:29 +0000986 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987}
988
989/*
990 * Return the address holding the debug tick for a funccall cookie.
991 */
992 int *
993func_dbg_tick(cookie)
994 void *cookie;
995{
Bram Moolenaar33570922005-01-25 22:26:29 +0000996 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997}
998
999/*
1000 * Return the nesting level for a funccall cookie.
1001 */
1002 int
1003func_level(cookie)
1004 void *cookie;
1005{
Bram Moolenaar33570922005-01-25 22:26:29 +00001006 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007}
1008
1009/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001010funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001012/* pointer to list of previously used funccal, still around because some
1013 * item in it is still being used. */
1014funccall_T *previous_funccal = NULL;
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016/*
1017 * Return TRUE when a function was ended by a ":return" command.
1018 */
1019 int
1020current_func_returned()
1021{
1022 return current_funccal->returned;
1023}
1024
1025
1026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 * Set an internal variable to a string value. Creates the variable if it does
1028 * not already exist.
1029 */
1030 void
1031set_internal_string_var(name, value)
1032 char_u *name;
1033 char_u *value;
1034{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001035 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001036 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
1038 val = vim_strsave(value);
1039 if (val != NULL)
1040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001041 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001042 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001044 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001045 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 }
1047 }
1048}
1049
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001051static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052static char_u *redir_endp = NULL;
1053static char_u *redir_varname = NULL;
1054
1055/*
1056 * Start recording command output to a variable
1057 * Returns OK if successfully completed the setup. FAIL otherwise.
1058 */
1059 int
1060var_redir_start(name, append)
1061 char_u *name;
1062 int append; /* append to an existing variable */
1063{
1064 int save_emsg;
1065 int err;
1066 typval_T tv;
1067
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001068 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001069 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 {
1071 EMSG(_(e_invarg));
1072 return FAIL;
1073 }
1074
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001075 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 redir_varname = vim_strsave(name);
1077 if (redir_varname == NULL)
1078 return FAIL;
1079
1080 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1081 if (redir_lval == NULL)
1082 {
1083 var_redir_stop();
1084 return FAIL;
1085 }
1086
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001087 /* The output is stored in growarray "redir_ga" until redirection ends. */
1088 ga_init2(&redir_ga, (int)sizeof(char), 500);
1089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001091 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001092 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1094 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 if (redir_endp != NULL && *redir_endp != NUL)
1097 /* Trailing characters are present after the variable name */
1098 EMSG(_(e_trailing));
1099 else
1100 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001101 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 var_redir_stop();
1103 return FAIL;
1104 }
1105
1106 /* check if we can write to the variable: set it to or append an empty
1107 * string */
1108 save_emsg = did_emsg;
1109 did_emsg = FALSE;
1110 tv.v_type = VAR_STRING;
1111 tv.vval.v_string = (char_u *)"";
1112 if (append)
1113 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1114 else
1115 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001116 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001118 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 if (err)
1120 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001121 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122 var_redir_stop();
1123 return FAIL;
1124 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125
1126 return OK;
1127}
1128
1129/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 * Append "value[value_len]" to the variable set by var_redir_start().
1131 * The actual appending is postponed until redirection ends, because the value
1132 * appended may in fact be the string we write to, changing it may cause freed
1133 * memory to be used:
1134 * :redir => foo
1135 * :let foo
1136 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 */
1138 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001141 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001143 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144
1145 if (redir_lval == NULL)
1146 return;
1147
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001149 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001151 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001153 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001154 {
1155 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001156 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157 }
1158 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160}
1161
1162/*
1163 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001164 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 */
1166 void
1167var_redir_stop()
1168{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001169 typval_T tv;
1170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 if (redir_lval != NULL)
1172 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001173 /* If there was no error: assign the text to the variable. */
1174 if (redir_endp != NULL)
1175 {
1176 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1177 tv.v_type = VAR_STRING;
1178 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001179 /* Call get_lval() again, if it's inside a Dict or List it may
1180 * have changed. */
1181 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001182 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001183 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1184 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1185 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001186 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001187
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001188 /* free the collected output */
1189 vim_free(redir_ga.ga_data);
1190 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001191
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 vim_free(redir_lval);
1193 redir_lval = NULL;
1194 }
1195 vim_free(redir_varname);
1196 redir_varname = NULL;
1197}
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199# if defined(FEAT_MBYTE) || defined(PROTO)
1200 int
1201eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1202 char_u *enc_from;
1203 char_u *enc_to;
1204 char_u *fname_from;
1205 char_u *fname_to;
1206{
1207 int err = FALSE;
1208
1209 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1210 set_vim_var_string(VV_CC_TO, enc_to, -1);
1211 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1212 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1213 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1214 err = TRUE;
1215 set_vim_var_string(VV_CC_FROM, NULL, -1);
1216 set_vim_var_string(VV_CC_TO, NULL, -1);
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1219
1220 if (err)
1221 return FAIL;
1222 return OK;
1223}
1224# endif
1225
1226# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1227 int
1228eval_printexpr(fname, args)
1229 char_u *fname;
1230 char_u *args;
1231{
1232 int err = FALSE;
1233
1234 set_vim_var_string(VV_FNAME_IN, fname, -1);
1235 set_vim_var_string(VV_CMDARG, args, -1);
1236 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1237 err = TRUE;
1238 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1239 set_vim_var_string(VV_CMDARG, NULL, -1);
1240
1241 if (err)
1242 {
1243 mch_remove(fname);
1244 return FAIL;
1245 }
1246 return OK;
1247}
1248# endif
1249
1250# if defined(FEAT_DIFF) || defined(PROTO)
1251 void
1252eval_diff(origfile, newfile, outfile)
1253 char_u *origfile;
1254 char_u *newfile;
1255 char_u *outfile;
1256{
1257 int err = FALSE;
1258
1259 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1260 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1261 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1262 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1263 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1264 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1265 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1266}
1267
1268 void
1269eval_patch(origfile, difffile, outfile)
1270 char_u *origfile;
1271 char_u *difffile;
1272 char_u *outfile;
1273{
1274 int err;
1275
1276 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1277 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1278 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1279 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1280 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1281 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1282 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1283}
1284# endif
1285
1286/*
1287 * Top level evaluation function, returning a boolean.
1288 * Sets "error" to TRUE if there was an error.
1289 * Return TRUE or FALSE.
1290 */
1291 int
1292eval_to_bool(arg, error, nextcmd, skip)
1293 char_u *arg;
1294 int *error;
1295 char_u **nextcmd;
1296 int skip; /* only parse, don't execute */
1297{
Bram Moolenaar33570922005-01-25 22:26:29 +00001298 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 int retval = FALSE;
1300
1301 if (skip)
1302 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 else
1306 {
1307 *error = FALSE;
1308 if (!skip)
1309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001310 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 }
1314 if (skip)
1315 --emsg_skip;
1316
1317 return retval;
1318}
1319
1320/*
1321 * Top level evaluation function, returning a string. If "skip" is TRUE,
1322 * only parsing to "nextcmd" is done, without reporting errors. Return
1323 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1324 */
1325 char_u *
1326eval_to_string_skip(arg, nextcmd, skip)
1327 char_u *arg;
1328 char_u **nextcmd;
1329 int skip; /* only parse, don't execute */
1330{
Bram Moolenaar33570922005-01-25 22:26:29 +00001331 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 char_u *retval;
1333
1334 if (skip)
1335 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 retval = NULL;
1338 else
1339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 retval = vim_strsave(get_tv_string(&tv));
1341 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343 if (skip)
1344 --emsg_skip;
1345
1346 return retval;
1347}
1348
1349/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001350 * Skip over an expression at "*pp".
1351 * Return FAIL for an error, OK otherwise.
1352 */
1353 int
1354skip_expr(pp)
1355 char_u **pp;
1356{
Bram Moolenaar33570922005-01-25 22:26:29 +00001357 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001358
1359 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001360 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001361}
1362
1363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 * When "convert" is TRUE convert a List into a sequence of lines and convert
1366 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 * Return pointer to allocated memory, or NULL for failure.
1368 */
1369 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001370eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 char_u *arg;
1372 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001373 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
Bram Moolenaar33570922005-01-25 22:26:29 +00001375 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001377 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001378#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001379 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 retval = NULL;
1384 else
1385 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001386 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001387 {
1388 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001389 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001390 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001391 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001392 if (tv.vval.v_list->lv_len > 0)
1393 ga_append(&ga, NL);
1394 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001395 ga_append(&ga, NUL);
1396 retval = (char_u *)ga.ga_data;
1397 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001398#ifdef FEAT_FLOAT
1399 else if (convert && tv.v_type == VAR_FLOAT)
1400 {
1401 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1402 retval = vim_strsave(numbuf);
1403 }
1404#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001405 else
1406 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001407 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409
1410 return retval;
1411}
1412
1413/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001414 * Call eval_to_string() without using current local variables and using
1415 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 */
1417 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001418eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 char_u *arg;
1420 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001421 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
1423 char_u *retval;
1424 void *save_funccalp;
1425
1426 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001427 if (use_sandbox)
1428 ++sandbox;
1429 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001430 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001431 if (use_sandbox)
1432 --sandbox;
1433 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 restore_funccal(save_funccalp);
1435 return retval;
1436}
1437
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438/*
1439 * Top level evaluation function, returning a number.
1440 * Evaluates "expr" silently.
1441 * Returns -1 for an error.
1442 */
1443 int
1444eval_to_number(expr)
1445 char_u *expr;
1446{
Bram Moolenaar33570922005-01-25 22:26:29 +00001447 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001449 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451 ++emsg_off;
1452
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001453 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 retval = -1;
1455 else
1456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001457 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001458 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 }
1460 --emsg_off;
1461
1462 return retval;
1463}
1464
Bram Moolenaara40058a2005-07-11 22:42:07 +00001465/*
1466 * Prepare v: variable "idx" to be used.
1467 * Save the current typeval in "save_tv".
1468 * When not used yet add the variable to the v: hashtable.
1469 */
1470 static void
1471prepare_vimvar(idx, save_tv)
1472 int idx;
1473 typval_T *save_tv;
1474{
1475 *save_tv = vimvars[idx].vv_tv;
1476 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1477 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1478}
1479
1480/*
1481 * Restore v: variable "idx" to typeval "save_tv".
1482 * When no longer defined, remove the variable from the v: hashtable.
1483 */
1484 static void
1485restore_vimvar(idx, save_tv)
1486 int idx;
1487 typval_T *save_tv;
1488{
1489 hashitem_T *hi;
1490
Bram Moolenaara40058a2005-07-11 22:42:07 +00001491 vimvars[idx].vv_tv = *save_tv;
1492 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1493 {
1494 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1495 if (HASHITEM_EMPTY(hi))
1496 EMSG2(_(e_intern2), "restore_vimvar()");
1497 else
1498 hash_remove(&vimvarht, hi);
1499 }
1500}
1501
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001502#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001503/*
1504 * Evaluate an expression to a list with suggestions.
1505 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001506 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001507 */
1508 list_T *
1509eval_spell_expr(badword, expr)
1510 char_u *badword;
1511 char_u *expr;
1512{
1513 typval_T save_val;
1514 typval_T rettv;
1515 list_T *list = NULL;
1516 char_u *p = skipwhite(expr);
1517
1518 /* Set "v:val" to the bad word. */
1519 prepare_vimvar(VV_VAL, &save_val);
1520 vimvars[VV_VAL].vv_type = VAR_STRING;
1521 vimvars[VV_VAL].vv_str = badword;
1522 if (p_verbose == 0)
1523 ++emsg_off;
1524
1525 if (eval1(&p, &rettv, TRUE) == OK)
1526 {
1527 if (rettv.v_type != VAR_LIST)
1528 clear_tv(&rettv);
1529 else
1530 list = rettv.vval.v_list;
1531 }
1532
1533 if (p_verbose == 0)
1534 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001535 restore_vimvar(VV_VAL, &save_val);
1536
1537 return list;
1538}
1539
1540/*
1541 * "list" is supposed to contain two items: a word and a number. Return the
1542 * word in "pp" and the number as the return value.
1543 * Return -1 if anything isn't right.
1544 * Used to get the good word and score from the eval_spell_expr() result.
1545 */
1546 int
1547get_spellword(list, pp)
1548 list_T *list;
1549 char_u **pp;
1550{
1551 listitem_T *li;
1552
1553 li = list->lv_first;
1554 if (li == NULL)
1555 return -1;
1556 *pp = get_tv_string(&li->li_tv);
1557
1558 li = li->li_next;
1559 if (li == NULL)
1560 return -1;
1561 return get_tv_number(&li->li_tv);
1562}
1563#endif
1564
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001565/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001566 * Top level evaluation function.
1567 * Returns an allocated typval_T with the result.
1568 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001569 */
1570 typval_T *
1571eval_expr(arg, nextcmd)
1572 char_u *arg;
1573 char_u **nextcmd;
1574{
1575 typval_T *tv;
1576
1577 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001578 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001579 {
1580 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001581 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001582 }
1583
1584 return tv;
1585}
1586
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001589 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001590 * Uses argv[argc] for the function arguments. Only Number and String
1591 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001594 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001595call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 char_u *func;
1597 int argc;
1598 char_u **argv;
1599 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001600 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
Bram Moolenaar33570922005-01-25 22:26:29 +00001603 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 long n;
1605 int len;
1606 int i;
1607 int doesrange;
1608 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001609 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001611 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 for (i = 0; i < argc; i++)
1616 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001617 /* Pass a NULL or empty argument as an empty string */
1618 if (argv[i] == NULL || *argv[i] == NUL)
1619 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001620 argvars[i].v_type = VAR_STRING;
1621 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001622 continue;
1623 }
1624
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001625 if (str_arg_only)
1626 len = 0;
1627 else
1628 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001629 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (len != 0 && len == (int)STRLEN(argv[i]))
1631 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001632 argvars[i].v_type = VAR_NUMBER;
1633 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635 else
1636 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001637 argvars[i].v_type = VAR_STRING;
1638 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
1640 }
1641
1642 if (safe)
1643 {
1644 save_funccalp = save_funccal();
1645 ++sandbox;
1646 }
1647
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1649 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (safe)
1653 {
1654 --sandbox;
1655 restore_funccal(save_funccalp);
1656 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 vim_free(argvars);
1658
1659 if (ret == FAIL)
1660 clear_tv(rettv);
1661
1662 return ret;
1663}
1664
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001665/*
1666 * Call vimL function "func" and return the result as a number.
1667 * Returns -1 when calling the function fails.
1668 * Uses argv[argc] for the function arguments.
1669 */
1670 long
1671call_func_retnr(func, argc, argv, safe)
1672 char_u *func;
1673 int argc;
1674 char_u **argv;
1675 int safe; /* use the sandbox */
1676{
1677 typval_T rettv;
1678 long retval;
1679
1680 /* All arguments are passed as strings, no conversion to number. */
1681 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1682 return -1;
1683
1684 retval = get_tv_number_chk(&rettv, NULL);
1685 clear_tv(&rettv);
1686 return retval;
1687}
1688
1689#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1690 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1691
Bram Moolenaar4f688582007-07-24 12:34:30 +00001692# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001694 * Call vimL function "func" and return the result as a string.
1695 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 * Uses argv[argc] for the function arguments.
1697 */
1698 void *
1699call_func_retstr(func, argc, argv, safe)
1700 char_u *func;
1701 int argc;
1702 char_u **argv;
1703 int safe; /* use the sandbox */
1704{
1705 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001706 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001708 /* All arguments are passed as strings, no conversion to number. */
1709 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001710 return NULL;
1711
1712 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 return retval;
1715}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001716# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001717
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001718/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001719 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001721 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722 */
1723 void *
1724call_func_retlist(func, argc, argv, safe)
1725 char_u *func;
1726 int argc;
1727 char_u **argv;
1728 int safe; /* use the sandbox */
1729{
1730 typval_T rettv;
1731
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001732 /* All arguments are passed as strings, no conversion to number. */
1733 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734 return NULL;
1735
1736 if (rettv.v_type != VAR_LIST)
1737 {
1738 clear_tv(&rettv);
1739 return NULL;
1740 }
1741
1742 return rettv.vval.v_list;
1743}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#endif
1745
1746/*
1747 * Save the current function call pointer, and set it to NULL.
1748 * Used when executing autocommands and for ":source".
1749 */
1750 void *
1751save_funccal()
1752{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001753 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 current_funccal = NULL;
1756 return (void *)fc;
1757}
1758
1759 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001760restore_funccal(vfc)
1761 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001763 funccall_T *fc = (funccall_T *)vfc;
1764
1765 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766}
1767
Bram Moolenaar05159a02005-02-26 23:04:13 +00001768#if defined(FEAT_PROFILE) || defined(PROTO)
1769/*
1770 * Prepare profiling for entering a child or something else that is not
1771 * counted for the script/function itself.
1772 * Should always be called in pair with prof_child_exit().
1773 */
1774 void
1775prof_child_enter(tm)
1776 proftime_T *tm; /* place to store waittime */
1777{
1778 funccall_T *fc = current_funccal;
1779
1780 if (fc != NULL && fc->func->uf_profiling)
1781 profile_start(&fc->prof_child);
1782 script_prof_save(tm);
1783}
1784
1785/*
1786 * Take care of time spent in a child.
1787 * Should always be called after prof_child_enter().
1788 */
1789 void
1790prof_child_exit(tm)
1791 proftime_T *tm; /* where waittime was stored */
1792{
1793 funccall_T *fc = current_funccal;
1794
1795 if (fc != NULL && fc->func->uf_profiling)
1796 {
1797 profile_end(&fc->prof_child);
1798 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1799 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1800 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1801 }
1802 script_prof_restore(tm);
1803}
1804#endif
1805
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#ifdef FEAT_FOLDING
1808/*
1809 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1810 * it in "*cp". Doesn't give error messages.
1811 */
1812 int
1813eval_foldexpr(arg, cp)
1814 char_u *arg;
1815 int *cp;
1816{
Bram Moolenaar33570922005-01-25 22:26:29 +00001817 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 int retval;
1819 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001820 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1821 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
1823 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001824 if (use_sandbox)
1825 ++sandbox;
1826 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 retval = 0;
1830 else
1831 {
1832 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 if (tv.v_type == VAR_NUMBER)
1834 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001835 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 retval = 0;
1837 else
1838 {
1839 /* If the result is a string, check if there is a non-digit before
1840 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (!VIM_ISDIGIT(*s) && *s != '-')
1843 *cp = *s++;
1844 retval = atol((char *)s);
1845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
1848 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001849 if (use_sandbox)
1850 --sandbox;
1851 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
1853 return retval;
1854}
1855#endif
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 * ":let" list all variable values
1859 * ":let var1 var2" list variable values
1860 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001861 * ":let var += expr" assignment command.
1862 * ":let var -= expr" assignment command.
1863 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 */
1866 void
1867ex_let(eap)
1868 exarg_T *eap;
1869{
1870 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001872 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 int var_count = 0;
1875 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001877 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
Bram Moolenaardb552d602006-03-23 22:59:57 +00001880 argend = skip_var_list(arg, &var_count, &semicolon);
1881 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001883 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1884 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001885 expr = skipwhite(argend);
1886 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1887 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001889 /*
1890 * ":let" without "=": list variables
1891 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 if (*arg == '[')
1893 EMSG(_(e_invarg));
1894 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001896 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001900 list_glob_vars(&first);
1901 list_buf_vars(&first);
1902 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001903#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001904 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001905#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 list_script_vars(&first);
1907 list_func_vars(&first);
1908 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 eap->nextcmd = check_nextcmd(arg);
1911 }
1912 else
1913 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 op[0] = '=';
1915 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001916 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001917 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001918 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1919 op[0] = *expr; /* +=, -= or .= */
1920 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001922 else
1923 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (eap->skip)
1926 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 if (eap->skip)
1929 {
1930 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 --emsg_skip;
1933 }
1934 else if (i != FAIL)
1935 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940 }
1941}
1942
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943/*
1944 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1945 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001946 * When "nextchars" is not NULL it points to a string with characters that
1947 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1948 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 * Returns OK or FAIL;
1950 */
1951 static int
1952ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1953 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001954 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 int copy; /* copy values from "tv", don't move */
1956 int semicolon; /* from skip_var_list() */
1957 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959{
1960 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001961 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001963 listitem_T *item;
1964 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965
1966 if (*arg != '[')
1967 {
1968 /*
1969 * ":let var = expr" or ":for var in list"
1970 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 return FAIL;
1973 return OK;
1974 }
1975
1976 /*
1977 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1978 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001979 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 {
1981 EMSG(_(e_listreq));
1982 return FAIL;
1983 }
1984
1985 i = list_len(l);
1986 if (semicolon == 0 && var_count < i)
1987 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001988 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 return FAIL;
1990 }
1991 if (var_count - semicolon > i)
1992 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001993 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 return FAIL;
1995 }
1996
1997 item = l->lv_first;
1998 while (*arg != ']')
1999 {
2000 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 item = item->li_next;
2003 if (arg == NULL)
2004 return FAIL;
2005
2006 arg = skipwhite(arg);
2007 if (*arg == ';')
2008 {
2009 /* Put the rest of the list (may be empty) in the var after ';'.
2010 * Create a new list for this. */
2011 l = list_alloc();
2012 if (l == NULL)
2013 return FAIL;
2014 while (item != NULL)
2015 {
2016 list_append_tv(l, &item->li_tv);
2017 item = item->li_next;
2018 }
2019
2020 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002021 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 ltv.vval.v_list = l;
2023 l->lv_refcount = 1;
2024
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2026 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002027 clear_tv(&ltv);
2028 if (arg == NULL)
2029 return FAIL;
2030 break;
2031 }
2032 else if (*arg != ',' && *arg != ']')
2033 {
2034 EMSG2(_(e_intern2), "ex_let_vars()");
2035 return FAIL;
2036 }
2037 }
2038
2039 return OK;
2040}
2041
2042/*
2043 * Skip over assignable variable "var" or list of variables "[var, var]".
2044 * Used for ":let varvar = expr" and ":for varvar in expr".
2045 * For "[var, var]" increment "*var_count" for each variable.
2046 * for "[var, var; var]" set "semicolon".
2047 * Return NULL for an error.
2048 */
2049 static char_u *
2050skip_var_list(arg, var_count, semicolon)
2051 char_u *arg;
2052 int *var_count;
2053 int *semicolon;
2054{
2055 char_u *p, *s;
2056
2057 if (*arg == '[')
2058 {
2059 /* "[var, var]": find the matching ']'. */
2060 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002061 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 {
2063 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2064 s = skip_var_one(p);
2065 if (s == p)
2066 {
2067 EMSG2(_(e_invarg2), p);
2068 return NULL;
2069 }
2070 ++*var_count;
2071
2072 p = skipwhite(s);
2073 if (*p == ']')
2074 break;
2075 else if (*p == ';')
2076 {
2077 if (*semicolon == 1)
2078 {
2079 EMSG(_("Double ; in list of variables"));
2080 return NULL;
2081 }
2082 *semicolon = 1;
2083 }
2084 else if (*p != ',')
2085 {
2086 EMSG2(_(e_invarg2), p);
2087 return NULL;
2088 }
2089 }
2090 return p + 1;
2091 }
2092 else
2093 return skip_var_one(arg);
2094}
2095
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002096/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002097 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002098 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002100 static char_u *
2101skip_var_one(arg)
2102 char_u *arg;
2103{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002104 if (*arg == '@' && arg[1] != NUL)
2105 return arg + 2;
2106 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2107 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002108}
2109
Bram Moolenaara7043832005-01-21 11:56:39 +00002110/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002111 * List variables for hashtab "ht" with prefix "prefix".
2112 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002113 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002114 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002116 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002118 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002120{
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 hashitem_T *hi;
2122 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002123 int todo;
2124
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002125 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002126 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2127 {
2128 if (!HASHITEM_EMPTY(hi))
2129 {
2130 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002131 di = HI2DI(hi);
2132 if (empty || di->di_tv.v_type != VAR_STRING
2133 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002135 }
2136 }
2137}
2138
2139/*
2140 * List global variables.
2141 */
2142 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002143list_glob_vars(first)
2144 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002145{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002147}
2148
2149/*
2150 * List buffer variables.
2151 */
2152 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153list_buf_vars(first)
2154 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002155{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002156 char_u numbuf[NUMBUFLEN];
2157
Bram Moolenaar429fa852013-04-15 12:27:36 +02002158 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002160
2161 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2163 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002164}
2165
2166/*
2167 * List window variables.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_win_vars(first)
2171 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002172{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002173 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002175}
2176
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002177#ifdef FEAT_WINDOWS
2178/*
2179 * List tab page variables.
2180 */
2181 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182list_tab_vars(first)
2183 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002184{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002185 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002187}
2188#endif
2189
Bram Moolenaara7043832005-01-21 11:56:39 +00002190/*
2191 * List Vim variables.
2192 */
2193 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194list_vim_vars(first)
2195 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198}
2199
2200/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002201 * List script-local variables, if there is a script.
2202 */
2203 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204list_script_vars(first)
2205 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002206{
2207 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2209 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002210}
2211
2212/*
2213 * List function variables, if there is a function.
2214 */
2215 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216list_func_vars(first)
2217 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002218{
2219 if (current_funccal != NULL)
2220 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002221 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002222}
2223
2224/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 * List variables in "arg".
2226 */
2227 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002228list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 exarg_T *eap;
2230 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232{
2233 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 char_u *name_start;
2237 char_u *arg_subsc;
2238 char_u *tofree;
2239 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240
2241 while (!ends_excmd(*arg) && !got_int)
2242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002245 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2247 {
2248 emsg_severe = TRUE;
2249 EMSG(_(e_trailing));
2250 break;
2251 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 /* get_name_len() takes care of expanding curly braces */
2256 name_start = name = arg;
2257 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2258 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 /* This is mainly to keep test 49 working: when expanding
2261 * curly braces fails overrule the exception error message. */
2262 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 emsg_severe = TRUE;
2265 EMSG2(_(e_invarg2), arg);
2266 break;
2267 }
2268 error = TRUE;
2269 }
2270 else
2271 {
2272 if (tofree != NULL)
2273 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002274 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 else
2277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 /* handle d.key, l[idx], f(expr) */
2279 arg_subsc = arg;
2280 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 case 'g': list_glob_vars(first); break;
2289 case 'b': list_buf_vars(first); break;
2290 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002291#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002292 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002293#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002294 case 'v': list_vim_vars(first); break;
2295 case 's': list_script_vars(first); break;
2296 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 default:
2298 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 }
2301 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 {
2303 char_u numbuf[NUMBUFLEN];
2304 char_u *tf;
2305 int c;
2306 char_u *s;
2307
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002308 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309 c = *arg;
2310 *arg = NUL;
2311 list_one_var_a((char_u *)"",
2312 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002313 tv.v_type,
2314 s == NULL ? (char_u *)"" : s,
2315 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 *arg = c;
2317 vim_free(tf);
2318 }
2319 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 }
2322 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323
2324 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326
2327 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 }
2329
2330 return arg;
2331}
2332
2333/*
2334 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2335 * Returns a pointer to the char just after the var name.
2336 * Returns NULL if there is an error.
2337 */
2338 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002341 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002343 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345{
2346 int c1;
2347 char_u *name;
2348 char_u *p;
2349 char_u *arg_end = NULL;
2350 int len;
2351 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353
2354 /*
2355 * ":let $VAR = expr": Set environment variable.
2356 */
2357 if (*arg == '$')
2358 {
2359 /* Find the end of the name. */
2360 ++arg;
2361 name = arg;
2362 len = get_env_len(&arg);
2363 if (len == 0)
2364 EMSG2(_(e_invarg2), name - 1);
2365 else
2366 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 if (op != NULL && (*op == '+' || *op == '-'))
2368 EMSG2(_(e_letwrong), op);
2369 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002370 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002372 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 {
2374 c1 = name[len];
2375 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 p = get_tv_string_chk(tv);
2377 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 {
2379 int mustfree = FALSE;
2380 char_u *s = vim_getenv(name, &mustfree);
2381
2382 if (s != NULL)
2383 {
2384 p = tofree = concat_str(s, p);
2385 if (mustfree)
2386 vim_free(s);
2387 }
2388 }
2389 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 if (STRICMP(name, "HOME") == 0)
2393 init_homedir();
2394 else if (didset_vim && STRICMP(name, "VIM") == 0)
2395 didset_vim = FALSE;
2396 else if (didset_vimruntime
2397 && STRICMP(name, "VIMRUNTIME") == 0)
2398 didset_vimruntime = FALSE;
2399 arg_end = arg;
2400 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002402 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 }
2404 }
2405 }
2406
2407 /*
2408 * ":let &option = expr": Set option value.
2409 * ":let &l:option = expr": Set local option value.
2410 * ":let &g:option = expr": Set global option value.
2411 */
2412 else if (*arg == '&')
2413 {
2414 /* Find the end of the name. */
2415 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002416 if (p == NULL || (endchars != NULL
2417 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 EMSG(_(e_letunexp));
2419 else
2420 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002421 long n;
2422 int opt_type;
2423 long numval;
2424 char_u *stringval = NULL;
2425 char_u *s;
2426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 c1 = *p;
2428 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429
2430 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002431 s = get_tv_string_chk(tv); /* != NULL if number or string */
2432 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 {
2434 opt_type = get_option_value(arg, &numval,
2435 &stringval, opt_flags);
2436 if ((opt_type == 1 && *op == '.')
2437 || (opt_type == 0 && *op != '.'))
2438 EMSG2(_(e_letwrong), op);
2439 else
2440 {
2441 if (opt_type == 1) /* number */
2442 {
2443 if (*op == '+')
2444 n = numval + n;
2445 else
2446 n = numval - n;
2447 }
2448 else if (opt_type == 0 && stringval != NULL) /* string */
2449 {
2450 s = concat_str(stringval, s);
2451 vim_free(stringval);
2452 stringval = s;
2453 }
2454 }
2455 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 if (s != NULL)
2457 {
2458 set_option_value(arg, n, s, opt_flags);
2459 arg_end = p;
2460 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 }
2464 }
2465
2466 /*
2467 * ":let @r = expr": Set register contents.
2468 */
2469 else if (*arg == '@')
2470 {
2471 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (op != NULL && (*op == '+' || *op == '-'))
2473 EMSG2(_(e_letwrong), op);
2474 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002475 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 EMSG(_(e_letunexp));
2477 else
2478 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002479 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 char_u *s;
2481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 p = get_tv_string_chk(tv);
2483 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002485 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 if (s != NULL)
2487 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002488 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 vim_free(s);
2490 }
2491 }
2492 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002493 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 arg_end = arg + 1;
2496 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002497 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 }
2499 }
2500
2501 /*
2502 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002505 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002507 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002509 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2513 EMSG(_(e_letunexp));
2514 else
2515 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002516 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 arg_end = p;
2518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 }
2522
2523 else
2524 EMSG2(_(e_invarg2), arg);
2525
2526 return arg_end;
2527}
2528
2529/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2531 */
2532 static int
2533check_changedtick(arg)
2534 char_u *arg;
2535{
2536 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2537 {
2538 EMSG2(_(e_readonlyvar), arg);
2539 return TRUE;
2540 }
2541 return FALSE;
2542}
2543
2544/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 * Get an lval: variable, Dict item or List item that can be assigned a value
2546 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2547 * "name.key", "name.key[expr]" etc.
2548 * Indexing only works if "name" is an existing List or Dictionary.
2549 * "name" points to the start of the name.
2550 * If "rettv" is not NULL it points to the value to be assigned.
2551 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2552 * wrong; must end in space or cmd separator.
2553 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002554 * flags:
2555 * GLV_QUIET: do not give error messages
2556 * GLV_NO_AUTOLOAD: do not use script autoloading
2557 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002559 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561 */
2562 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002563get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 typval_T *rettv;
2566 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 int unlet;
2568 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002570 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 char_u *expr_start, *expr_end;
2574 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002575 dictitem_T *v;
2576 typval_T var1;
2577 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002579 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002582 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002583 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587
2588 if (skip)
2589 {
2590 /* When skipping just find the end of the name. */
2591 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002592 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 }
2594
2595 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002596 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (expr_start != NULL)
2598 {
2599 /* Don't expand the name when we already know there is an error. */
2600 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2601 && *p != '[' && *p != '.')
2602 {
2603 EMSG(_(e_trailing));
2604 return NULL;
2605 }
2606
2607 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2608 if (lp->ll_exp_name == NULL)
2609 {
2610 /* Report an invalid expression in braces, unless the
2611 * expression evaluation has been cancelled due to an
2612 * aborting error, an interrupt, or an exception. */
2613 if (!aborting() && !quiet)
2614 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002615 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 EMSG2(_(e_invarg2), name);
2617 return NULL;
2618 }
2619 }
2620 lp->ll_name = lp->ll_exp_name;
2621 }
2622 else
2623 lp->ll_name = name;
2624
2625 /* Without [idx] or .key we are done. */
2626 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2627 return p;
2628
2629 cc = *p;
2630 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002631 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (v == NULL && !quiet)
2633 EMSG2(_(e_undefvar), lp->ll_name);
2634 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 if (v == NULL)
2636 return NULL;
2637
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 /*
2639 * Loop until no more [idx] or .key is following.
2640 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002641 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2645 && !(lp->ll_tv->v_type == VAR_DICT
2646 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (!quiet)
2649 EMSG(_("E689: Can only index a List or Dictionary"));
2650 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!quiet)
2655 EMSG(_("E708: [:] must come last"));
2656 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002657 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 len = -1;
2660 if (*p == '.')
2661 {
2662 key = p + 1;
2663 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2664 ;
2665 if (len == 0)
2666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
2668 EMSG(_(e_emptykey));
2669 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
2671 p = key + len;
2672 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 else
2674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 if (*p == ':')
2678 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 else
2680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 empty1 = FALSE;
2682 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002684 if (get_tv_string_chk(&var1) == NULL)
2685 {
2686 /* not a number or string */
2687 clear_tv(&var1);
2688 return NULL;
2689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
2691
2692 /* Optionally get the second index [ :expr]. */
2693 if (*p == ':')
2694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002698 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002702 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (rettv != NULL && (rettv->v_type != VAR_LIST
2704 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (!quiet)
2707 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (!empty1)
2709 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
2712 p = skipwhite(p + 1);
2713 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 else
2716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2719 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (!empty1)
2721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002724 if (get_tv_string_chk(&var2) == NULL)
2725 {
2726 /* not a number or string */
2727 if (!empty1)
2728 clear_tv(&var1);
2729 clear_tv(&var2);
2730 return NULL;
2731 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 if (!quiet)
2741 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 if (!empty1)
2743 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 }
2748
2749 /* Skip to past ']'. */
2750 ++p;
2751 }
2752
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 {
2755 if (len == -1)
2756 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002758 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 if (*key == NUL)
2760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (!quiet)
2762 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 }
2766 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002767 lp->ll_list = NULL;
2768 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002769 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002770
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002771 /* When assigning to a scope dictionary check that a function and
2772 * variable name is valid (only variable name unless it is l: or
2773 * g: dictionary). Disallow overwriting a builtin function. */
2774 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002775 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002776 int prevval;
2777 int wrong;
2778
2779 if (len != -1)
2780 {
2781 prevval = key[len];
2782 key[len] = NUL;
2783 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002784 else
2785 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002786 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2787 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002788 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002789 || !valid_varname(key);
2790 if (len != -1)
2791 key[len] = prevval;
2792 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 return NULL;
2794 }
2795
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002798 /* Can't add "v:" variable. */
2799 if (lp->ll_dict == &vimvardict)
2800 {
2801 EMSG2(_(e_illvar), name);
2802 return NULL;
2803 }
2804
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002805 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002809 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 if (len == -1)
2811 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 }
2814 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 if (len == -1)
2819 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 p = NULL;
2822 break;
2823 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002824 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002825 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002826 return NULL;
2827
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 if (len == -1)
2829 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 }
2832 else
2833 {
2834 /*
2835 * Get the number and item for the only or first index of the List.
2836 */
2837 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 else
2840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002841 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 clear_tv(&var1);
2843 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002844 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 lp->ll_list = lp->ll_tv->vval.v_list;
2846 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2847 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002849 if (lp->ll_n1 < 0)
2850 {
2851 lp->ll_n1 = 0;
2852 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2853 }
2854 }
2855 if (lp->ll_li == NULL)
2856 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002859 if (!quiet)
2860 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 }
2863
2864 /*
2865 * May need to find the item or absolute index for the second
2866 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 * When no index given: "lp->ll_empty2" is TRUE.
2868 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002872 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002878 {
2879 if (!quiet)
2880 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002882 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 }
2885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2887 if (lp->ll_n1 < 0)
2888 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2889 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002890 {
2891 if (!quiet)
2892 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002894 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002895 }
2896
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002899 }
2900
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 return p;
2902}
2903
2904/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 */
2907 static void
2908clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910{
2911 vim_free(lp->ll_exp_name);
2912 vim_free(lp->ll_newkey);
2913}
2914
2915/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002916 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002918 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 */
2920 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002921set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002922 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927{
2928 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002929 listitem_T *ri;
2930 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931
2932 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 cc = *endp;
2937 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 if (op != NULL && *op != '=')
2939 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002941
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002942 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002943 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002944 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002945 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002947 if ((di == NULL
2948 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2949 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2950 FALSE)))
2951 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 set_var(lp->ll_name, &tv, FALSE);
2953 clear_tv(&tv);
2954 }
2955 }
2956 else
2957 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 else if (tv_check_lock(lp->ll_newkey == NULL
2962 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002963 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002964 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 else if (lp->ll_range)
2966 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002967 listitem_T *ll_li = lp->ll_li;
2968 int ll_n1 = lp->ll_n1;
2969
2970 /*
2971 * Check whether any of the list items is locked
2972 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002973 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002974 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002975 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002976 return;
2977 ri = ri->li_next;
2978 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2979 break;
2980 ll_li = ll_li->li_next;
2981 ++ll_n1;
2982 }
2983
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 /*
2985 * Assign the List values to the list items.
2986 */
2987 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002988 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 if (op != NULL && *op != '=')
2990 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2991 else
2992 {
2993 clear_tv(&lp->ll_li->li_tv);
2994 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2995 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 ri = ri->li_next;
2997 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2998 break;
2999 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003002 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 ri = NULL;
3005 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003006 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003007 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 lp->ll_li = lp->ll_li->li_next;
3009 ++lp->ll_n1;
3010 }
3011 if (ri != NULL)
3012 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 else if (lp->ll_empty2
3014 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 : lp->ll_n1 != lp->ll_n2)
3016 EMSG(_("E711: List value has not enough items"));
3017 }
3018 else
3019 {
3020 /*
3021 * Assign to a List or Dictionary item.
3022 */
3023 if (lp->ll_newkey != NULL)
3024 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003025 if (op != NULL && *op != '=')
3026 {
3027 EMSG2(_(e_letwrong), op);
3028 return;
3029 }
3030
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003032 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003033 if (di == NULL)
3034 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003035 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3036 {
3037 vim_free(di);
3038 return;
3039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003041 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 else if (op != NULL && *op != '=')
3043 {
3044 tv_op(lp->ll_tv, rettv, op);
3045 return;
3046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003047 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003049
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 /*
3051 * Assign the value to the variable or list item.
3052 */
3053 if (copy)
3054 copy_tv(rettv, lp->ll_tv);
3055 else
3056 {
3057 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003058 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003060 }
3061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062}
3063
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003065 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3066 * Returns OK or FAIL.
3067 */
3068 static int
3069tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 typval_T *tv1;
3071 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072 char_u *op;
3073{
3074 long n;
3075 char_u numbuf[NUMBUFLEN];
3076 char_u *s;
3077
3078 /* Can't do anything with a Funcref or a Dict on the right. */
3079 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3080 {
3081 switch (tv1->v_type)
3082 {
3083 case VAR_DICT:
3084 case VAR_FUNC:
3085 break;
3086
3087 case VAR_LIST:
3088 if (*op != '+' || tv2->v_type != VAR_LIST)
3089 break;
3090 /* List += List */
3091 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3092 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3093 return OK;
3094
3095 case VAR_NUMBER:
3096 case VAR_STRING:
3097 if (tv2->v_type == VAR_LIST)
3098 break;
3099 if (*op == '+' || *op == '-')
3100 {
3101 /* nr += nr or nr -= nr*/
3102 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003103#ifdef FEAT_FLOAT
3104 if (tv2->v_type == VAR_FLOAT)
3105 {
3106 float_T f = n;
3107
3108 if (*op == '+')
3109 f += tv2->vval.v_float;
3110 else
3111 f -= tv2->vval.v_float;
3112 clear_tv(tv1);
3113 tv1->v_type = VAR_FLOAT;
3114 tv1->vval.v_float = f;
3115 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117#endif
3118 {
3119 if (*op == '+')
3120 n += get_tv_number(tv2);
3121 else
3122 n -= get_tv_number(tv2);
3123 clear_tv(tv1);
3124 tv1->v_type = VAR_NUMBER;
3125 tv1->vval.v_number = n;
3126 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 }
3128 else
3129 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130 if (tv2->v_type == VAR_FLOAT)
3131 break;
3132
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 /* str .= str */
3134 s = get_tv_string(tv1);
3135 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3136 clear_tv(tv1);
3137 tv1->v_type = VAR_STRING;
3138 tv1->vval.v_string = s;
3139 }
3140 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141
3142#ifdef FEAT_FLOAT
3143 case VAR_FLOAT:
3144 {
3145 float_T f;
3146
3147 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3148 && tv2->v_type != VAR_NUMBER
3149 && tv2->v_type != VAR_STRING))
3150 break;
3151 if (tv2->v_type == VAR_FLOAT)
3152 f = tv2->vval.v_float;
3153 else
3154 f = get_tv_number(tv2);
3155 if (*op == '+')
3156 tv1->vval.v_float += f;
3157 else
3158 tv1->vval.v_float -= f;
3159 }
3160 return OK;
3161#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003162 }
3163 }
3164
3165 EMSG2(_(e_letwrong), op);
3166 return FAIL;
3167}
3168
3169/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170 * Add a watcher to a list.
3171 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003172 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 list_T *l;
3175 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176{
3177 lw->lw_next = l->lv_watch;
3178 l->lv_watch = lw;
3179}
3180
3181/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003182 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183 * No warning when it isn't found...
3184 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003185 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003186list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 list_T *l;
3188 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189{
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191
3192 lwp = &l->lv_watch;
3193 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3194 {
3195 if (lw == lwrem)
3196 {
3197 *lwp = lw->lw_next;
3198 break;
3199 }
3200 lwp = &lw->lw_next;
3201 }
3202}
3203
3204/*
3205 * Just before removing an item from a list: advance watchers to the next
3206 * item.
3207 */
3208 static void
3209list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003210 list_T *l;
3211 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3216 if (lw->lw_item == item)
3217 lw->lw_item = item->li_next;
3218}
3219
3220/*
3221 * Evaluate the expression used in a ":for var in expr" command.
3222 * "arg" points to "var".
3223 * Set "*errp" to TRUE for an error, FALSE otherwise;
3224 * Return a pointer that holds the info. Null when there is an error.
3225 */
3226 void *
3227eval_for_line(arg, errp, nextcmdp, skip)
3228 char_u *arg;
3229 int *errp;
3230 char_u **nextcmdp;
3231 int skip;
3232{
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 typval_T tv;
3236 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237
3238 *errp = TRUE; /* default: there is an error */
3239
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 if (fi == NULL)
3242 return NULL;
3243
3244 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3245 if (expr == NULL)
3246 return fi;
3247
3248 expr = skipwhite(expr);
3249 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3250 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003251 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 return fi;
3253 }
3254
3255 if (skip)
3256 ++emsg_skip;
3257 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3258 {
3259 *errp = FALSE;
3260 if (!skip)
3261 {
3262 l = tv.vval.v_list;
3263 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 clear_tv(&tv);
3267 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 else
3269 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003270 /* No need to increment the refcount, it's already set for the
3271 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 fi->fi_list = l;
3273 list_add_watch(l, &fi->fi_lw);
3274 fi->fi_lw.lw_item = l->lv_first;
3275 }
3276 }
3277 }
3278 if (skip)
3279 --emsg_skip;
3280
3281 return fi;
3282}
3283
3284/*
3285 * Use the first item in a ":for" list. Advance to the next.
3286 * Assign the values to the variable (list). "arg" points to the first one.
3287 * Return TRUE when a valid item was found, FALSE when at end of list or
3288 * something wrong.
3289 */
3290 int
3291next_for_item(fi_void, arg)
3292 void *fi_void;
3293 char_u *arg;
3294{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003295 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298
3299 item = fi->fi_lw.lw_item;
3300 if (item == NULL)
3301 result = FALSE;
3302 else
3303 {
3304 fi->fi_lw.lw_item = item->li_next;
3305 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3306 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3307 }
3308 return result;
3309}
3310
3311/*
3312 * Free the structure used to store info used by ":for".
3313 */
3314 void
3315free_for_info(fi_void)
3316 void *fi_void;
3317{
Bram Moolenaar33570922005-01-25 22:26:29 +00003318 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003320 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003321 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323 list_unref(fi->fi_list);
3324 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 vim_free(fi);
3326}
3327
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3329
3330 void
3331set_context_for_expression(xp, arg, cmdidx)
3332 expand_T *xp;
3333 char_u *arg;
3334 cmdidx_T cmdidx;
3335{
3336 int got_eq = FALSE;
3337 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 if (cmdidx == CMD_let)
3341 {
3342 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003343 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 {
3345 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003346 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347 {
3348 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003349 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 if (vim_iswhite(*p))
3351 break;
3352 }
3353 return;
3354 }
3355 }
3356 else
3357 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3358 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 while ((xp->xp_pattern = vim_strpbrk(arg,
3360 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3361 {
3362 c = *xp->xp_pattern;
3363 if (c == '&')
3364 {
3365 c = xp->xp_pattern[1];
3366 if (c == '&')
3367 {
3368 ++xp->xp_pattern;
3369 xp->xp_context = cmdidx != CMD_let || got_eq
3370 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3371 }
3372 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003375 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3376 xp->xp_pattern += 2;
3377
3378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
3380 else if (c == '$')
3381 {
3382 /* environment variable */
3383 xp->xp_context = EXPAND_ENV_VARS;
3384 }
3385 else if (c == '=')
3386 {
3387 got_eq = TRUE;
3388 xp->xp_context = EXPAND_EXPRESSION;
3389 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003390 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 && xp->xp_context == EXPAND_FUNCTIONS
3392 && vim_strchr(xp->xp_pattern, '(') == NULL)
3393 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003394 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 break;
3396 }
3397 else if (cmdidx != CMD_let || got_eq)
3398 {
3399 if (c == '"') /* string */
3400 {
3401 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3402 if (c == '\\' && xp->xp_pattern[1] != NUL)
3403 ++xp->xp_pattern;
3404 xp->xp_context = EXPAND_NOTHING;
3405 }
3406 else if (c == '\'') /* literal string */
3407 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003408 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3410 /* skip */ ;
3411 xp->xp_context = EXPAND_NOTHING;
3412 }
3413 else if (c == '|')
3414 {
3415 if (xp->xp_pattern[1] == '|')
3416 {
3417 ++xp->xp_pattern;
3418 xp->xp_context = EXPAND_EXPRESSION;
3419 }
3420 else
3421 xp->xp_context = EXPAND_COMMANDS;
3422 }
3423 else
3424 xp->xp_context = EXPAND_EXPRESSION;
3425 }
3426 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003427 /* Doesn't look like something valid, expand as an expression
3428 * anyway. */
3429 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 arg = xp->xp_pattern;
3431 if (*arg != NUL)
3432 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3433 /* skip */ ;
3434 }
3435 xp->xp_pattern = arg;
3436}
3437
3438#endif /* FEAT_CMDL_COMPL */
3439
3440/*
3441 * ":1,25call func(arg1, arg2)" function call.
3442 */
3443 void
3444ex_call(eap)
3445 exarg_T *eap;
3446{
3447 char_u *arg = eap->arg;
3448 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003450 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003452 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 linenr_T lnum;
3454 int doesrange;
3455 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003458 if (eap->skip)
3459 {
3460 /* trans_function_name() doesn't work well when skipping, use eval0()
3461 * instead to skip to any following command, e.g. for:
3462 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003463 ++emsg_skip;
3464 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3465 clear_tv(&rettv);
3466 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003467 return;
3468 }
3469
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003470 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003471 if (fudi.fd_newkey != NULL)
3472 {
3473 /* Still need to give an error message for missing key. */
3474 EMSG2(_(e_dictkey), fudi.fd_newkey);
3475 vim_free(fudi.fd_newkey);
3476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003477 if (tofree == NULL)
3478 return;
3479
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003480 /* Increase refcount on dictionary, it could get deleted when evaluating
3481 * the arguments. */
3482 if (fudi.fd_dict != NULL)
3483 ++fudi.fd_dict->dv_refcount;
3484
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003485 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003486 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003487 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar532c7802005-01-27 14:44:31 +00003489 /* Skip white space to allow ":call func ()". Not good, but required for
3490 * backward compatibility. */
3491 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 if (*startarg != '(')
3495 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003496 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 goto end;
3498 }
3499
3500 /*
3501 * When skipping, evaluate the function once, to find the end of the
3502 * arguments.
3503 * When the function takes a range, this is discovered after the first
3504 * call, and the loop is broken.
3505 */
3506 if (eap->skip)
3507 {
3508 ++emsg_skip;
3509 lnum = eap->line2; /* do it once, also with an invalid range */
3510 }
3511 else
3512 lnum = eap->line1;
3513 for ( ; lnum <= eap->line2; ++lnum)
3514 {
3515 if (!eap->skip && eap->addr_count > 0)
3516 {
3517 curwin->w_cursor.lnum = lnum;
3518 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003519#ifdef FEAT_VIRTUALEDIT
3520 curwin->w_cursor.coladd = 0;
3521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003524 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003525 eap->line1, eap->line2, &doesrange,
3526 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 failed = TRUE;
3529 break;
3530 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003531
3532 /* Handle a function returning a Funcref, Dictionary or List. */
3533 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3534 {
3535 failed = TRUE;
3536 break;
3537 }
3538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (doesrange || eap->skip)
3541 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003546 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (aborting())
3548 break;
3549 }
3550 if (eap->skip)
3551 --emsg_skip;
3552
3553 if (!failed)
3554 {
3555 /* Check for trailing illegal characters and a following command. */
3556 if (!ends_excmd(*arg))
3557 {
3558 emsg_severe = TRUE;
3559 EMSG(_(e_trailing));
3560 }
3561 else
3562 eap->nextcmd = check_nextcmd(arg);
3563 }
3564
3565end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003566 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003567 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568}
3569
3570/*
3571 * ":unlet[!] var1 ... " command.
3572 */
3573 void
3574ex_unlet(eap)
3575 exarg_T *eap;
3576{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 ex_unletlock(eap, eap->arg, 0);
3578}
3579
3580/*
3581 * ":lockvar" and ":unlockvar" commands
3582 */
3583 void
3584ex_lockvar(eap)
3585 exarg_T *eap;
3586{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003588 int deep = 2;
3589
3590 if (eap->forceit)
3591 deep = -1;
3592 else if (vim_isdigit(*arg))
3593 {
3594 deep = getdigits(&arg);
3595 arg = skipwhite(arg);
3596 }
3597
3598 ex_unletlock(eap, arg, deep);
3599}
3600
3601/*
3602 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3603 */
3604 static void
3605ex_unletlock(eap, argstart, deep)
3606 exarg_T *eap;
3607 char_u *argstart;
3608 int deep;
3609{
3610 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
3615 do
3616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003618 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003619 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 if (lv.ll_name == NULL)
3621 error = TRUE; /* error but continue parsing */
3622 if (name_end == NULL || (!vim_iswhite(*name_end)
3623 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 if (name_end != NULL)
3626 {
3627 emsg_severe = TRUE;
3628 EMSG(_(e_trailing));
3629 }
3630 if (!(eap->skip || error))
3631 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 break;
3633 }
3634
3635 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636 {
3637 if (eap->cmdidx == CMD_unlet)
3638 {
3639 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3640 error = TRUE;
3641 }
3642 else
3643 {
3644 if (do_lock_var(&lv, name_end, deep,
3645 eap->cmdidx == CMD_lockvar) == FAIL)
3646 error = TRUE;
3647 }
3648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 if (!eap->skip)
3651 clear_lval(&lv);
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 arg = skipwhite(name_end);
3654 } while (!ends_excmd(*arg));
3655
3656 eap->nextcmd = check_nextcmd(arg);
3657}
3658
Bram Moolenaar8c711452005-01-14 21:53:12 +00003659 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 int forceit;
3664{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 int ret = OK;
3666 int cc;
3667
3668 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 cc = *name_end;
3671 *name_end = NUL;
3672
3673 /* Normal name or expanded name. */
3674 if (check_changedtick(lp->ll_name))
3675 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003676 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003678 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003679 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003680 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003681 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003682 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003684 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 else if (lp->ll_range)
3686 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003687 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003688 listitem_T *ll_li = lp->ll_li;
3689 int ll_n1 = lp->ll_n1;
3690
3691 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3692 {
3693 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003694 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003695 return FAIL;
3696 ll_li = li;
3697 ++ll_n1;
3698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003699
3700 /* Delete a range of List items. */
3701 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3702 {
3703 li = lp->ll_li->li_next;
3704 listitem_remove(lp->ll_list, lp->ll_li);
3705 lp->ll_li = li;
3706 ++lp->ll_n1;
3707 }
3708 }
3709 else
3710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 /* unlet a List item. */
3713 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003716 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 }
3718
3719 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003720}
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722/*
3723 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003724 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 */
3726 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 hashtab_T *ht;
3732 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003733 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003735 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 ht = find_var_ht(name, &varname);
3738 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003740 hi = hash_find(ht, varname);
3741 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003742 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003743 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003744 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003745 || var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003746 return FAIL;
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003747
3748 if (ht == &globvarht)
3749 d = &globvardict;
3750 else if (current_funccal != NULL
3751 && ht == &current_funccal->l_vars.dv_hashtab)
3752 d = &current_funccal->l_vars;
3753 else
3754 {
3755 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3756 d = di->di_tv.vval.v_dict;
3757 }
3758 if (d == NULL || tv_check_lock(d->dv_lock, name, FALSE))
3759 return FAIL;
3760
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003761 delete_var(ht, hi);
3762 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003765 if (forceit)
3766 return OK;
3767 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 return FAIL;
3769}
3770
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003771/*
3772 * Lock or unlock variable indicated by "lp".
3773 * "deep" is the levels to go (-1 for unlimited);
3774 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3775 */
3776 static int
3777do_lock_var(lp, name_end, deep, lock)
3778 lval_T *lp;
3779 char_u *name_end;
3780 int deep;
3781 int lock;
3782{
3783 int ret = OK;
3784 int cc;
3785 dictitem_T *di;
3786
3787 if (deep == 0) /* nothing to do */
3788 return OK;
3789
3790 if (lp->ll_tv == NULL)
3791 {
3792 cc = *name_end;
3793 *name_end = NUL;
3794
3795 /* Normal name or expanded name. */
3796 if (check_changedtick(lp->ll_name))
3797 ret = FAIL;
3798 else
3799 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003800 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003801 if (di == NULL)
3802 ret = FAIL;
3803 else
3804 {
3805 if (lock)
3806 di->di_flags |= DI_FLAGS_LOCK;
3807 else
3808 di->di_flags &= ~DI_FLAGS_LOCK;
3809 item_lock(&di->di_tv, deep, lock);
3810 }
3811 }
3812 *name_end = cc;
3813 }
3814 else if (lp->ll_range)
3815 {
3816 listitem_T *li = lp->ll_li;
3817
3818 /* (un)lock a range of List items. */
3819 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3820 {
3821 item_lock(&li->li_tv, deep, lock);
3822 li = li->li_next;
3823 ++lp->ll_n1;
3824 }
3825 }
3826 else if (lp->ll_list != NULL)
3827 /* (un)lock a List item. */
3828 item_lock(&lp->ll_li->li_tv, deep, lock);
3829 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003830 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003831 item_lock(&lp->ll_di->di_tv, deep, lock);
3832
3833 return ret;
3834}
3835
3836/*
3837 * Lock or unlock an item. "deep" is nr of levels to go.
3838 */
3839 static void
3840item_lock(tv, deep, lock)
3841 typval_T *tv;
3842 int deep;
3843 int lock;
3844{
3845 static int recurse = 0;
3846 list_T *l;
3847 listitem_T *li;
3848 dict_T *d;
3849 hashitem_T *hi;
3850 int todo;
3851
3852 if (recurse >= DICT_MAXNEST)
3853 {
3854 EMSG(_("E743: variable nested too deep for (un)lock"));
3855 return;
3856 }
3857 if (deep == 0)
3858 return;
3859 ++recurse;
3860
3861 /* lock/unlock the item itself */
3862 if (lock)
3863 tv->v_lock |= VAR_LOCKED;
3864 else
3865 tv->v_lock &= ~VAR_LOCKED;
3866
3867 switch (tv->v_type)
3868 {
3869 case VAR_LIST:
3870 if ((l = tv->vval.v_list) != NULL)
3871 {
3872 if (lock)
3873 l->lv_lock |= VAR_LOCKED;
3874 else
3875 l->lv_lock &= ~VAR_LOCKED;
3876 if (deep < 0 || deep > 1)
3877 /* recursive: lock/unlock the items the List contains */
3878 for (li = l->lv_first; li != NULL; li = li->li_next)
3879 item_lock(&li->li_tv, deep - 1, lock);
3880 }
3881 break;
3882 case VAR_DICT:
3883 if ((d = tv->vval.v_dict) != NULL)
3884 {
3885 if (lock)
3886 d->dv_lock |= VAR_LOCKED;
3887 else
3888 d->dv_lock &= ~VAR_LOCKED;
3889 if (deep < 0 || deep > 1)
3890 {
3891 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003892 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003893 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3894 {
3895 if (!HASHITEM_EMPTY(hi))
3896 {
3897 --todo;
3898 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3899 }
3900 }
3901 }
3902 }
3903 }
3904 --recurse;
3905}
3906
Bram Moolenaara40058a2005-07-11 22:42:07 +00003907/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003908 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3909 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003910 */
3911 static int
3912tv_islocked(tv)
3913 typval_T *tv;
3914{
3915 return (tv->v_lock & VAR_LOCKED)
3916 || (tv->v_type == VAR_LIST
3917 && tv->vval.v_list != NULL
3918 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3919 || (tv->v_type == VAR_DICT
3920 && tv->vval.v_dict != NULL
3921 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3922}
3923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3925/*
3926 * Delete all "menutrans_" variables.
3927 */
3928 void
3929del_menutrans_vars()
3930{
Bram Moolenaar33570922005-01-25 22:26:29 +00003931 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003935 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 {
3938 if (!HASHITEM_EMPTY(hi))
3939 {
3940 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3942 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 }
3944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946}
3947#endif
3948
3949#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3950
3951/*
3952 * Local string buffer for the next two functions to store a variable name
3953 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3954 * get_user_var_name().
3955 */
3956
3957static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3958
3959static char_u *varnamebuf = NULL;
3960static int varnamebuflen = 0;
3961
3962/*
3963 * Function to concatenate a prefix and a variable name.
3964 */
3965 static char_u *
3966cat_prefix_varname(prefix, name)
3967 int prefix;
3968 char_u *name;
3969{
3970 int len;
3971
3972 len = (int)STRLEN(name) + 3;
3973 if (len > varnamebuflen)
3974 {
3975 vim_free(varnamebuf);
3976 len += 10; /* some additional space */
3977 varnamebuf = alloc(len);
3978 if (varnamebuf == NULL)
3979 {
3980 varnamebuflen = 0;
3981 return NULL;
3982 }
3983 varnamebuflen = len;
3984 }
3985 *varnamebuf = prefix;
3986 varnamebuf[1] = ':';
3987 STRCPY(varnamebuf + 2, name);
3988 return varnamebuf;
3989}
3990
3991/*
3992 * Function given to ExpandGeneric() to obtain the list of user defined
3993 * (global/buffer/window/built-in) variable names.
3994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 char_u *
3996get_user_var_name(xp, idx)
3997 expand_T *xp;
3998 int idx;
3999{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004000 static long_u gdone;
4001 static long_u bdone;
4002 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004003#ifdef FEAT_WINDOWS
4004 static long_u tdone;
4005#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004006 static int vidx;
4007 static hashitem_T *hi;
4008 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004013#ifdef FEAT_WINDOWS
4014 tdone = 0;
4015#endif
4016 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004017
4018 /* Global variables */
4019 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004021 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004022 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004023 else
4024 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004025 while (HASHITEM_EMPTY(hi))
4026 ++hi;
4027 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4028 return cat_prefix_varname('g', hi->hi_key);
4029 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004031
4032 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004033 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004036 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004037 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004038 else
4039 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004040 while (HASHITEM_EMPTY(hi))
4041 ++hi;
4042 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004046 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 return (char_u *)"b:changedtick";
4048 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004049
4050 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004051 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004054 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004056 else
4057 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004058 while (HASHITEM_EMPTY(hi))
4059 ++hi;
4060 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004062
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004063#ifdef FEAT_WINDOWS
4064 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004065 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004066 if (tdone < ht->ht_used)
4067 {
4068 if (tdone++ == 0)
4069 hi = ht->ht_array;
4070 else
4071 ++hi;
4072 while (HASHITEM_EMPTY(hi))
4073 ++hi;
4074 return cat_prefix_varname('t', hi->hi_key);
4075 }
4076#endif
4077
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 /* v: variables */
4079 if (vidx < VV_LEN)
4080 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081
4082 vim_free(varnamebuf);
4083 varnamebuf = NULL;
4084 varnamebuflen = 0;
4085 return NULL;
4086}
4087
4088#endif /* FEAT_CMDL_COMPL */
4089
4090/*
4091 * types for expressions.
4092 */
4093typedef enum
4094{
4095 TYPE_UNKNOWN = 0
4096 , TYPE_EQUAL /* == */
4097 , TYPE_NEQUAL /* != */
4098 , TYPE_GREATER /* > */
4099 , TYPE_GEQUAL /* >= */
4100 , TYPE_SMALLER /* < */
4101 , TYPE_SEQUAL /* <= */
4102 , TYPE_MATCH /* =~ */
4103 , TYPE_NOMATCH /* !~ */
4104} exptype_T;
4105
4106/*
4107 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4110 */
4111
4112/*
4113 * Handle zero level expression.
4114 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004116 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 * Return OK or FAIL.
4118 */
4119 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 char_u **nextcmd;
4124 int evaluate;
4125{
4126 int ret;
4127 char_u *p;
4128
4129 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 if (ret == FAIL || !ends_excmd(*p))
4132 {
4133 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 /*
4136 * Report the invalid expression unless the expression evaluation has
4137 * been cancelled due to an aborting error, an interrupt, or an
4138 * exception.
4139 */
4140 if (!aborting())
4141 EMSG2(_(e_invexpr2), arg);
4142 ret = FAIL;
4143 }
4144 if (nextcmd != NULL)
4145 *nextcmd = check_nextcmd(p);
4146
4147 return ret;
4148}
4149
4150/*
4151 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004152 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 *
4154 * "arg" must point to the first non-white of the expression.
4155 * "arg" is advanced to the next non-white after the recognized expression.
4156 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004157 * Note: "rettv.v_lock" is not set.
4158 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 * Return OK or FAIL.
4160 */
4161 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 int evaluate;
4166{
4167 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
4170 /*
4171 * Get the first variable.
4172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return FAIL;
4175
4176 if ((*arg)[0] == '?')
4177 {
4178 result = FALSE;
4179 if (evaluate)
4180 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004181 int error = FALSE;
4182
4183 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 if (error)
4187 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189
4190 /*
4191 * Get the second variable.
4192 */
4193 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 return FAIL;
4196
4197 /*
4198 * Check for the ":".
4199 */
4200 if ((*arg)[0] != ':')
4201 {
4202 EMSG(_("E109: Missing ':' after '?'"));
4203 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
4206 }
4207
4208 /*
4209 * Get the third variable.
4210 */
4211 *arg = skipwhite(*arg + 1);
4212 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4213 {
4214 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return FAIL;
4217 }
4218 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221
4222 return OK;
4223}
4224
4225/*
4226 * Handle first level expression:
4227 * expr2 || expr2 || expr2 logical OR
4228 *
4229 * "arg" must point to the first non-white of the expression.
4230 * "arg" is advanced to the next non-white after the recognized expression.
4231 *
4232 * Return OK or FAIL.
4233 */
4234 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004235eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 int evaluate;
4239{
Bram Moolenaar33570922005-01-25 22:26:29 +00004240 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 long result;
4242 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 /*
4246 * Get the first variable.
4247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 return FAIL;
4250
4251 /*
4252 * Repeat until there is no following "||".
4253 */
4254 first = TRUE;
4255 result = FALSE;
4256 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4257 {
4258 if (evaluate && first)
4259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (error)
4264 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 first = FALSE;
4266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 2);
4272 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4273 return FAIL;
4274
4275 /*
4276 * Compute the result.
4277 */
4278 if (evaluate && !result)
4279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (error)
4284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 if (evaluate)
4287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 rettv->v_type = VAR_NUMBER;
4289 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 }
4292
4293 return OK;
4294}
4295
4296/*
4297 * Handle second level expression:
4298 * expr3 && expr3 && expr3 logical AND
4299 *
4300 * "arg" must point to the first non-white of the expression.
4301 * "arg" is advanced to the next non-white after the recognized expression.
4302 *
4303 * Return OK or FAIL.
4304 */
4305 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004306eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 int evaluate;
4310{
Bram Moolenaar33570922005-01-25 22:26:29 +00004311 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 long result;
4313 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004314 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 /*
4317 * Get the first variable.
4318 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 return FAIL;
4321
4322 /*
4323 * Repeat until there is no following "&&".
4324 */
4325 first = TRUE;
4326 result = TRUE;
4327 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4328 {
4329 if (evaluate && first)
4330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004333 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 if (error)
4335 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 first = FALSE;
4337 }
4338
4339 /*
4340 * Get the second variable.
4341 */
4342 *arg = skipwhite(*arg + 2);
4343 if (eval4(arg, &var2, evaluate && result) == FAIL)
4344 return FAIL;
4345
4346 /*
4347 * Compute the result.
4348 */
4349 if (evaluate && result)
4350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004353 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004354 if (error)
4355 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 if (evaluate)
4358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 rettv->v_type = VAR_NUMBER;
4360 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 }
4363
4364 return OK;
4365}
4366
4367/*
4368 * Handle third level expression:
4369 * var1 == var2
4370 * var1 =~ var2
4371 * var1 != var2
4372 * var1 !~ var2
4373 * var1 > var2
4374 * var1 >= var2
4375 * var1 < var2
4376 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004377 * var1 is var2
4378 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 *
4380 * "arg" must point to the first non-white of the expression.
4381 * "arg" is advanced to the next non-white after the recognized expression.
4382 *
4383 * Return OK or FAIL.
4384 */
4385 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004386eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 int evaluate;
4390{
Bram Moolenaar33570922005-01-25 22:26:29 +00004391 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 char_u *p;
4393 int i;
4394 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004395 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 int len = 2;
4397 long n1, n2;
4398 char_u *s1, *s2;
4399 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4400 regmatch_T regmatch;
4401 int ic;
4402 char_u *save_cpo;
4403
4404 /*
4405 * Get the first variable.
4406 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004407 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
4409
4410 p = *arg;
4411 switch (p[0])
4412 {
4413 case '=': if (p[1] == '=')
4414 type = TYPE_EQUAL;
4415 else if (p[1] == '~')
4416 type = TYPE_MATCH;
4417 break;
4418 case '!': if (p[1] == '=')
4419 type = TYPE_NEQUAL;
4420 else if (p[1] == '~')
4421 type = TYPE_NOMATCH;
4422 break;
4423 case '>': if (p[1] != '=')
4424 {
4425 type = TYPE_GREATER;
4426 len = 1;
4427 }
4428 else
4429 type = TYPE_GEQUAL;
4430 break;
4431 case '<': if (p[1] != '=')
4432 {
4433 type = TYPE_SMALLER;
4434 len = 1;
4435 }
4436 else
4437 type = TYPE_SEQUAL;
4438 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 case 'i': if (p[1] == 's')
4440 {
4441 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4442 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004443 i = p[len];
4444 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 {
4446 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4447 type_is = TRUE;
4448 }
4449 }
4450 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
4452
4453 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004454 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 */
4456 if (type != TYPE_UNKNOWN)
4457 {
4458 /* extra question mark appended: ignore case */
4459 if (p[len] == '?')
4460 {
4461 ic = TRUE;
4462 ++len;
4463 }
4464 /* extra '#' appended: match case */
4465 else if (p[len] == '#')
4466 {
4467 ic = FALSE;
4468 ++len;
4469 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004470 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 else
4472 ic = p_ic;
4473
4474 /*
4475 * Get the second variable.
4476 */
4477 *arg = skipwhite(p + len);
4478 if (eval5(arg, &var2, evaluate) == FAIL)
4479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004480 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 return FAIL;
4482 }
4483
4484 if (evaluate)
4485 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 if (type_is && rettv->v_type != var2.v_type)
4487 {
4488 /* For "is" a different type always means FALSE, for "notis"
4489 * it means TRUE. */
4490 n1 = (type == TYPE_NEQUAL);
4491 }
4492 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4493 {
4494 if (type_is)
4495 {
4496 n1 = (rettv->v_type == var2.v_type
4497 && rettv->vval.v_list == var2.vval.v_list);
4498 if (type == TYPE_NEQUAL)
4499 n1 = !n1;
4500 }
4501 else if (rettv->v_type != var2.v_type
4502 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4503 {
4504 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004505 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004507 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 clear_tv(rettv);
4509 clear_tv(&var2);
4510 return FAIL;
4511 }
4512 else
4513 {
4514 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004515 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4516 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004517 if (type == TYPE_NEQUAL)
4518 n1 = !n1;
4519 }
4520 }
4521
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004522 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4523 {
4524 if (type_is)
4525 {
4526 n1 = (rettv->v_type == var2.v_type
4527 && rettv->vval.v_dict == var2.vval.v_dict);
4528 if (type == TYPE_NEQUAL)
4529 n1 = !n1;
4530 }
4531 else if (rettv->v_type != var2.v_type
4532 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4533 {
4534 if (rettv->v_type != var2.v_type)
4535 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4536 else
4537 EMSG(_("E736: Invalid operation for Dictionary"));
4538 clear_tv(rettv);
4539 clear_tv(&var2);
4540 return FAIL;
4541 }
4542 else
4543 {
4544 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004545 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4546 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004547 if (type == TYPE_NEQUAL)
4548 n1 = !n1;
4549 }
4550 }
4551
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004552 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4553 {
4554 if (rettv->v_type != var2.v_type
4555 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4556 {
4557 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004558 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004560 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 clear_tv(rettv);
4562 clear_tv(&var2);
4563 return FAIL;
4564 }
4565 else
4566 {
4567 /* Compare two Funcrefs for being equal or unequal. */
4568 if (rettv->vval.v_string == NULL
4569 || var2.vval.v_string == NULL)
4570 n1 = FALSE;
4571 else
4572 n1 = STRCMP(rettv->vval.v_string,
4573 var2.vval.v_string) == 0;
4574 if (type == TYPE_NEQUAL)
4575 n1 = !n1;
4576 }
4577 }
4578
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004579#ifdef FEAT_FLOAT
4580 /*
4581 * If one of the two variables is a float, compare as a float.
4582 * When using "=~" or "!~", always compare as string.
4583 */
4584 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4585 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4586 {
4587 float_T f1, f2;
4588
4589 if (rettv->v_type == VAR_FLOAT)
4590 f1 = rettv->vval.v_float;
4591 else
4592 f1 = get_tv_number(rettv);
4593 if (var2.v_type == VAR_FLOAT)
4594 f2 = var2.vval.v_float;
4595 else
4596 f2 = get_tv_number(&var2);
4597 n1 = FALSE;
4598 switch (type)
4599 {
4600 case TYPE_EQUAL: n1 = (f1 == f2); break;
4601 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4602 case TYPE_GREATER: n1 = (f1 > f2); break;
4603 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4604 case TYPE_SMALLER: n1 = (f1 < f2); break;
4605 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4606 case TYPE_UNKNOWN:
4607 case TYPE_MATCH:
4608 case TYPE_NOMATCH: break; /* avoid gcc warning */
4609 }
4610 }
4611#endif
4612
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /*
4614 * If one of the two variables is a number, compare as a number.
4615 * When using "=~" or "!~", always compare as string.
4616 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004617 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4619 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620 n1 = get_tv_number(rettv);
4621 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 switch (type)
4623 {
4624 case TYPE_EQUAL: n1 = (n1 == n2); break;
4625 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4626 case TYPE_GREATER: n1 = (n1 > n2); break;
4627 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4628 case TYPE_SMALLER: n1 = (n1 < n2); break;
4629 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4630 case TYPE_UNKNOWN:
4631 case TYPE_MATCH:
4632 case TYPE_NOMATCH: break; /* avoid gcc warning */
4633 }
4634 }
4635 else
4636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 s1 = get_tv_string_buf(rettv, buf1);
4638 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4640 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4641 else
4642 i = 0;
4643 n1 = FALSE;
4644 switch (type)
4645 {
4646 case TYPE_EQUAL: n1 = (i == 0); break;
4647 case TYPE_NEQUAL: n1 = (i != 0); break;
4648 case TYPE_GREATER: n1 = (i > 0); break;
4649 case TYPE_GEQUAL: n1 = (i >= 0); break;
4650 case TYPE_SMALLER: n1 = (i < 0); break;
4651 case TYPE_SEQUAL: n1 = (i <= 0); break;
4652
4653 case TYPE_MATCH:
4654 case TYPE_NOMATCH:
4655 /* avoid 'l' flag in 'cpoptions' */
4656 save_cpo = p_cpo;
4657 p_cpo = (char_u *)"";
4658 regmatch.regprog = vim_regcomp(s2,
4659 RE_MAGIC + RE_STRING);
4660 regmatch.rm_ic = ic;
4661 if (regmatch.regprog != NULL)
4662 {
4663 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004664 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (type == TYPE_NOMATCH)
4666 n1 = !n1;
4667 }
4668 p_cpo = save_cpo;
4669 break;
4670
4671 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4672 }
4673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 clear_tv(rettv);
4675 clear_tv(&var2);
4676 rettv->v_type = VAR_NUMBER;
4677 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679 }
4680
4681 return OK;
4682}
4683
4684/*
4685 * Handle fourth level expression:
4686 * + number addition
4687 * - number subtraction
4688 * . string concatenation
4689 *
4690 * "arg" must point to the first non-white of the expression.
4691 * "arg" is advanced to the next non-white after the recognized expression.
4692 *
4693 * Return OK or FAIL.
4694 */
4695 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 int evaluate;
4700{
Bram Moolenaar33570922005-01-25 22:26:29 +00004701 typval_T var2;
4702 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 int op;
4704 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705#ifdef FEAT_FLOAT
4706 float_T f1 = 0, f2 = 0;
4707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 char_u *s1, *s2;
4709 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4710 char_u *p;
4711
4712 /*
4713 * Get the first variable.
4714 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004715 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 return FAIL;
4717
4718 /*
4719 * Repeat computing, until no '+', '-' or '.' is following.
4720 */
4721 for (;;)
4722 {
4723 op = **arg;
4724 if (op != '+' && op != '-' && op != '.')
4725 break;
4726
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004727 if ((op != '+' || rettv->v_type != VAR_LIST)
4728#ifdef FEAT_FLOAT
4729 && (op == '.' || rettv->v_type != VAR_FLOAT)
4730#endif
4731 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004732 {
4733 /* For "list + ...", an illegal use of the first operand as
4734 * a number cannot be determined before evaluating the 2nd
4735 * operand: if this is also a list, all is ok.
4736 * For "something . ...", "something - ..." or "non-list + ...",
4737 * we know that the first operand needs to be a string or number
4738 * without evaluating the 2nd operand. So check before to avoid
4739 * side effects after an error. */
4740 if (evaluate && get_tv_string_chk(rettv) == NULL)
4741 {
4742 clear_tv(rettv);
4743 return FAIL;
4744 }
4745 }
4746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 /*
4748 * Get the second variable.
4749 */
4750 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004751 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004753 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 return FAIL;
4755 }
4756
4757 if (evaluate)
4758 {
4759 /*
4760 * Compute the result.
4761 */
4762 if (op == '.')
4763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004764 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4765 s2 = get_tv_string_buf_chk(&var2, buf2);
4766 if (s2 == NULL) /* type error ? */
4767 {
4768 clear_tv(rettv);
4769 clear_tv(&var2);
4770 return FAIL;
4771 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004772 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 clear_tv(rettv);
4774 rettv->v_type = VAR_STRING;
4775 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004777 else if (op == '+' && rettv->v_type == VAR_LIST
4778 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004779 {
4780 /* concatenate Lists */
4781 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4782 &var3) == FAIL)
4783 {
4784 clear_tv(rettv);
4785 clear_tv(&var2);
4786 return FAIL;
4787 }
4788 clear_tv(rettv);
4789 *rettv = var3;
4790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 else
4792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 int error = FALSE;
4794
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795#ifdef FEAT_FLOAT
4796 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004798 f1 = rettv->vval.v_float;
4799 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 else
4802#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004803 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004804 n1 = get_tv_number_chk(rettv, &error);
4805 if (error)
4806 {
4807 /* This can only happen for "list + non-list". For
4808 * "non-list + ..." or "something - ...", we returned
4809 * before evaluating the 2nd operand. */
4810 clear_tv(rettv);
4811 return FAIL;
4812 }
4813#ifdef FEAT_FLOAT
4814 if (var2.v_type == VAR_FLOAT)
4815 f1 = n1;
4816#endif
4817 }
4818#ifdef FEAT_FLOAT
4819 if (var2.v_type == VAR_FLOAT)
4820 {
4821 f2 = var2.vval.v_float;
4822 n2 = 0;
4823 }
4824 else
4825#endif
4826 {
4827 n2 = get_tv_number_chk(&var2, &error);
4828 if (error)
4829 {
4830 clear_tv(rettv);
4831 clear_tv(&var2);
4832 return FAIL;
4833 }
4834#ifdef FEAT_FLOAT
4835 if (rettv->v_type == VAR_FLOAT)
4836 f2 = n2;
4837#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840
4841#ifdef FEAT_FLOAT
4842 /* If there is a float on either side the result is a float. */
4843 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4844 {
4845 if (op == '+')
4846 f1 = f1 + f2;
4847 else
4848 f1 = f1 - f2;
4849 rettv->v_type = VAR_FLOAT;
4850 rettv->vval.v_float = f1;
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004853#endif
4854 {
4855 if (op == '+')
4856 n1 = n1 + n2;
4857 else
4858 n1 = n1 - n2;
4859 rettv->v_type = VAR_NUMBER;
4860 rettv->vval.v_number = n1;
4861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
4865 }
4866 return OK;
4867}
4868
4869/*
4870 * Handle fifth level expression:
4871 * * number multiplication
4872 * / number division
4873 * % number modulo
4874 *
4875 * "arg" must point to the first non-white of the expression.
4876 * "arg" is advanced to the next non-white after the recognized expression.
4877 *
4878 * Return OK or FAIL.
4879 */
4880 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004881eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004885 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886{
Bram Moolenaar33570922005-01-25 22:26:29 +00004887 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 int op;
4889 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890#ifdef FEAT_FLOAT
4891 int use_float = FALSE;
4892 float_T f1 = 0, f2;
4893#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004894 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
4896 /*
4897 * Get the first variable.
4898 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004899 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 return FAIL;
4901
4902 /*
4903 * Repeat computing, until no '*', '/' or '%' is following.
4904 */
4905 for (;;)
4906 {
4907 op = **arg;
4908 if (op != '*' && op != '/' && op != '%')
4909 break;
4910
4911 if (evaluate)
4912 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913#ifdef FEAT_FLOAT
4914 if (rettv->v_type == VAR_FLOAT)
4915 {
4916 f1 = rettv->vval.v_float;
4917 use_float = TRUE;
4918 n1 = 0;
4919 }
4920 else
4921#endif
4922 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004923 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004924 if (error)
4925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927 else
4928 n1 = 0;
4929
4930 /*
4931 * Get the second variable.
4932 */
4933 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004934 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 return FAIL;
4936
4937 if (evaluate)
4938 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939#ifdef FEAT_FLOAT
4940 if (var2.v_type == VAR_FLOAT)
4941 {
4942 if (!use_float)
4943 {
4944 f1 = n1;
4945 use_float = TRUE;
4946 }
4947 f2 = var2.vval.v_float;
4948 n2 = 0;
4949 }
4950 else
4951#endif
4952 {
4953 n2 = get_tv_number_chk(&var2, &error);
4954 clear_tv(&var2);
4955 if (error)
4956 return FAIL;
4957#ifdef FEAT_FLOAT
4958 if (use_float)
4959 f2 = n2;
4960#endif
4961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
4963 /*
4964 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004965 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004967#ifdef FEAT_FLOAT
4968 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970 if (op == '*')
4971 f1 = f1 * f2;
4972 else if (op == '/')
4973 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004974# ifdef VMS
4975 /* VMS crashes on divide by zero, work around it */
4976 if (f2 == 0.0)
4977 {
4978 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004979 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004980 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004981 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004983 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004984 }
4985 else
4986 f1 = f1 / f2;
4987# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 /* We rely on the floating point library to handle divide
4989 * by zero to result in "inf" and not a crash. */
4990 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004991# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004995 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 return FAIL;
4997 }
4998 rettv->v_type = VAR_FLOAT;
4999 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 }
5001 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004 if (op == '*')
5005 n1 = n1 * n2;
5006 else if (op == '/')
5007 {
5008 if (n2 == 0) /* give an error message? */
5009 {
5010 if (n1 == 0)
5011 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5012 else if (n1 < 0)
5013 n1 = -0x7fffffffL;
5014 else
5015 n1 = 0x7fffffffL;
5016 }
5017 else
5018 n1 = n1 / n2;
5019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005021 {
5022 if (n2 == 0) /* give an error message? */
5023 n1 = 0;
5024 else
5025 n1 = n1 % n2;
5026 }
5027 rettv->v_type = VAR_NUMBER;
5028 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031 }
5032
5033 return OK;
5034}
5035
5036/*
5037 * Handle sixth level expression:
5038 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005039 * "string" string constant
5040 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 * &option-name option value
5042 * @r register contents
5043 * identifier variable value
5044 * function() function call
5045 * $VAR environment variable
5046 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005047 * [expr, expr] List
5048 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 *
5050 * Also handle:
5051 * ! in front logical NOT
5052 * - in front unary minus
5053 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 * trailing [] subscript in String or List
5055 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 *
5057 * "arg" must point to the first non-white of the expression.
5058 * "arg" is advanced to the next non-white after the recognized expression.
5059 *
5060 * Return OK or FAIL.
5061 */
5062 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005063eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005067 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 long n;
5070 int len;
5071 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 char_u *start_leader, *end_leader;
5073 int ret = OK;
5074 char_u *alias;
5075
5076 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005078 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005080 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
5082 /*
5083 * Skip '!' and '-' characters. They are handled later.
5084 */
5085 start_leader = *arg;
5086 while (**arg == '!' || **arg == '-' || **arg == '+')
5087 *arg = skipwhite(*arg + 1);
5088 end_leader = *arg;
5089
5090 switch (**arg)
5091 {
5092 /*
5093 * Number constant.
5094 */
5095 case '0':
5096 case '1':
5097 case '2':
5098 case '3':
5099 case '4':
5100 case '5':
5101 case '6':
5102 case '7':
5103 case '8':
5104 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 {
5106#ifdef FEAT_FLOAT
5107 char_u *p = skipdigits(*arg + 1);
5108 int get_float = FALSE;
5109
5110 /* We accept a float when the format matches
5111 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005112 * strict to avoid backwards compatibility problems.
5113 * Don't look for a float after the "." operator, so that
5114 * ":let vers = 1.2.3" doesn't fail. */
5115 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005117 get_float = TRUE;
5118 p = skipdigits(p + 2);
5119 if (*p == 'e' || *p == 'E')
5120 {
5121 ++p;
5122 if (*p == '-' || *p == '+')
5123 ++p;
5124 if (!vim_isdigit(*p))
5125 get_float = FALSE;
5126 else
5127 p = skipdigits(p + 1);
5128 }
5129 if (ASCII_ISALPHA(*p) || *p == '.')
5130 get_float = FALSE;
5131 }
5132 if (get_float)
5133 {
5134 float_T f;
5135
5136 *arg += string2float(*arg, &f);
5137 if (evaluate)
5138 {
5139 rettv->v_type = VAR_FLOAT;
5140 rettv->vval.v_float = f;
5141 }
5142 }
5143 else
5144#endif
5145 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005146 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 *arg += len;
5148 if (evaluate)
5149 {
5150 rettv->v_type = VAR_NUMBER;
5151 rettv->vval.v_number = n;
5152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
5157 /*
5158 * String constant: "string".
5159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 break;
5162
5163 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005166 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005167 break;
5168
5169 /*
5170 * List: [expr, expr]
5171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 * Dictionary: {key: val, key: val}
5177 */
5178 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5179 break;
5180
5181 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005182 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005184 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 break;
5186
5187 /*
5188 * Environment variable: $VAR.
5189 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 break;
5192
5193 /*
5194 * Register contents: @r.
5195 */
5196 case '@': ++*arg;
5197 if (evaluate)
5198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005200 rettv->vval.v_string = get_reg_contents(**arg,
5201 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203 if (**arg != NUL)
5204 ++*arg;
5205 break;
5206
5207 /*
5208 * nested expression: (expression).
5209 */
5210 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005211 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (**arg == ')')
5213 ++*arg;
5214 else if (ret == OK)
5215 {
5216 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005217 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 ret = FAIL;
5219 }
5220 break;
5221
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 break;
5224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225
5226 if (ret == NOTDONE)
5227 {
5228 /*
5229 * Must be a variable or function name.
5230 * Can also be a curly-braces kind of name: {expr}.
5231 */
5232 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005233 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 if (alias != NULL)
5235 s = alias;
5236
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005237 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 ret = FAIL;
5239 else
5240 {
5241 if (**arg == '(') /* recursive! */
5242 {
5243 /* If "s" is the name of a variable of type VAR_FUNC
5244 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005245 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246
5247 /* Invoke the function. */
5248 ret = get_func_tv(s, len, rettv, arg,
5249 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005250 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005251
5252 /* If evaluate is FALSE rettv->v_type was not set in
5253 * get_func_tv, but it's needed in handle_subscript() to parse
5254 * what follows. So set it here. */
5255 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5256 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005257 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005258 rettv->v_type = VAR_FUNC;
5259 }
5260
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 /* Stop the expression evaluation when immediately
5262 * aborting on error, or when an interrupt occurred or
5263 * an exception was thrown but not caught. */
5264 if (aborting())
5265 {
5266 if (ret == OK)
5267 clear_tv(rettv);
5268 ret = FAIL;
5269 }
5270 }
5271 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005272 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005273 else
5274 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005276 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005277 }
5278
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 *arg = skipwhite(*arg);
5280
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005281 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5282 * expr(expr). */
5283 if (ret == OK)
5284 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
5286 /*
5287 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5288 */
5289 if (ret == OK && evaluate && end_leader > start_leader)
5290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005292 int val = 0;
5293#ifdef FEAT_FLOAT
5294 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005296 if (rettv->v_type == VAR_FLOAT)
5297 f = rettv->vval.v_float;
5298 else
5299#endif
5300 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005301 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 clear_tv(rettv);
5304 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 else
5307 {
5308 while (end_leader > start_leader)
5309 {
5310 --end_leader;
5311 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 {
5313#ifdef FEAT_FLOAT
5314 if (rettv->v_type == VAR_FLOAT)
5315 f = !f;
5316 else
5317#endif
5318 val = !val;
5319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321 {
5322#ifdef FEAT_FLOAT
5323 if (rettv->v_type == VAR_FLOAT)
5324 f = -f;
5325 else
5326#endif
5327 val = -val;
5328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005330#ifdef FEAT_FLOAT
5331 if (rettv->v_type == VAR_FLOAT)
5332 {
5333 clear_tv(rettv);
5334 rettv->vval.v_float = f;
5335 }
5336 else
5337#endif
5338 {
5339 clear_tv(rettv);
5340 rettv->v_type = VAR_NUMBER;
5341 rettv->vval.v_number = val;
5342 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
5345
5346 return ret;
5347}
5348
5349/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005350 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5351 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5353 */
5354 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005355eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005357 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005359 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360{
5361 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005362 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364 long len = -1;
5365 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005369 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005371 if (verbose)
5372 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 return FAIL;
5374 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005375#ifdef FEAT_FLOAT
5376 else if (rettv->v_type == VAR_FLOAT)
5377 {
5378 if (verbose)
5379 EMSG(_(e_float_as_string));
5380 return FAIL;
5381 }
5382#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005384 init_tv(&var1);
5385 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005388 /*
5389 * dict.name
5390 */
5391 key = *arg + 1;
5392 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5393 ;
5394 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 }
5398 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400 /*
5401 * something[idx]
5402 *
5403 * Get the (first) variable from inside the [].
5404 */
5405 *arg = skipwhite(*arg + 1);
5406 if (**arg == ':')
5407 empty1 = TRUE;
5408 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5409 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005410 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5411 {
5412 /* not a number or string */
5413 clear_tv(&var1);
5414 return FAIL;
5415 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416
5417 /*
5418 * Get the second variable from inside the [:].
5419 */
5420 if (**arg == ':')
5421 {
5422 range = TRUE;
5423 *arg = skipwhite(*arg + 1);
5424 if (**arg == ']')
5425 empty2 = TRUE;
5426 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005428 if (!empty1)
5429 clear_tv(&var1);
5430 return FAIL;
5431 }
5432 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5433 {
5434 /* not a number or string */
5435 if (!empty1)
5436 clear_tv(&var1);
5437 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005438 return FAIL;
5439 }
5440 }
5441
5442 /* Check for the ']'. */
5443 if (**arg != ']')
5444 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005445 if (verbose)
5446 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 clear_tv(&var1);
5448 if (range)
5449 clear_tv(&var2);
5450 return FAIL;
5451 }
5452 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 }
5454
5455 if (evaluate)
5456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 n1 = 0;
5458 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 n1 = get_tv_number(&var1);
5461 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 }
5463 if (range)
5464 {
5465 if (empty2)
5466 n2 = -1;
5467 else
5468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 n2 = get_tv_number(&var2);
5470 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 }
5472 }
5473
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 {
5476 case VAR_NUMBER:
5477 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005478 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 len = (long)STRLEN(s);
5480 if (range)
5481 {
5482 /* The resulting variable is a substring. If the indexes
5483 * are out of range the result is empty. */
5484 if (n1 < 0)
5485 {
5486 n1 = len + n1;
5487 if (n1 < 0)
5488 n1 = 0;
5489 }
5490 if (n2 < 0)
5491 n2 = len + n2;
5492 else if (n2 >= len)
5493 n2 = len;
5494 if (n1 >= len || n2 < 0 || n1 > n2)
5495 s = NULL;
5496 else
5497 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5498 }
5499 else
5500 {
5501 /* The resulting variable is a string of a single
5502 * character. If the index is too big or negative the
5503 * result is empty. */
5504 if (n1 >= len || n1 < 0)
5505 s = NULL;
5506 else
5507 s = vim_strnsave(s + n1, 1);
5508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005509 clear_tv(rettv);
5510 rettv->v_type = VAR_STRING;
5511 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 break;
5513
5514 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005515 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 if (n1 < 0)
5517 n1 = len + n1;
5518 if (!empty1 && (n1 < 0 || n1 >= len))
5519 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005520 /* For a range we allow invalid values and return an empty
5521 * list. A list index out of range is an error. */
5522 if (!range)
5523 {
5524 if (verbose)
5525 EMSGN(_(e_listidx), n1);
5526 return FAIL;
5527 }
5528 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 }
5530 if (range)
5531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005532 list_T *l;
5533 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534
5535 if (n2 < 0)
5536 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005537 else if (n2 >= len)
5538 n2 = len - 1;
5539 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005540 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 l = list_alloc();
5542 if (l == NULL)
5543 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 n1 <= n2; ++n1)
5546 {
5547 if (list_append_tv(l, &item->li_tv) == FAIL)
5548 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005549 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 return FAIL;
5551 }
5552 item = item->li_next;
5553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 clear_tv(rettv);
5555 rettv->v_type = VAR_LIST;
5556 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005557 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 }
5559 else
5560 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005561 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 clear_tv(rettv);
5563 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 }
5565 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566
5567 case VAR_DICT:
5568 if (range)
5569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005570 if (verbose)
5571 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572 if (len == -1)
5573 clear_tv(&var1);
5574 return FAIL;
5575 }
5576 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005577 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578
5579 if (len == -1)
5580 {
5581 key = get_tv_string(&var1);
5582 if (*key == NUL)
5583 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005584 if (verbose)
5585 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 clear_tv(&var1);
5587 return FAIL;
5588 }
5589 }
5590
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005591 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005593 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005594 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 if (len == -1)
5596 clear_tv(&var1);
5597 if (item == NULL)
5598 return FAIL;
5599
5600 copy_tv(&item->di_tv, &var1);
5601 clear_tv(rettv);
5602 *rettv = var1;
5603 }
5604 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 }
5606 }
5607
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 return OK;
5609}
5610
5611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 * Get an option value.
5613 * "arg" points to the '&' or '+' before the option name.
5614 * "arg" is advanced to character after the option name.
5615 * Return OK or FAIL.
5616 */
5617 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005618get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005620 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 int evaluate;
5622{
5623 char_u *option_end;
5624 long numval;
5625 char_u *stringval;
5626 int opt_type;
5627 int c;
5628 int working = (**arg == '+'); /* has("+option") */
5629 int ret = OK;
5630 int opt_flags;
5631
5632 /*
5633 * Isolate the option name and find its value.
5634 */
5635 option_end = find_option_end(arg, &opt_flags);
5636 if (option_end == NULL)
5637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005638 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 EMSG2(_("E112: Option name missing: %s"), *arg);
5640 return FAIL;
5641 }
5642
5643 if (!evaluate)
5644 {
5645 *arg = option_end;
5646 return OK;
5647 }
5648
5649 c = *option_end;
5650 *option_end = NUL;
5651 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
5654 if (opt_type == -3) /* invalid name */
5655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 EMSG2(_("E113: Unknown option: %s"), *arg);
5658 ret = FAIL;
5659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
5662 if (opt_type == -2) /* hidden string option */
5663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005664 rettv->v_type = VAR_STRING;
5665 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 }
5667 else if (opt_type == -1) /* hidden number option */
5668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 rettv->v_type = VAR_NUMBER;
5670 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
5672 else if (opt_type == 1) /* number option */
5673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 rettv->v_type = VAR_NUMBER;
5675 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677 else /* string option */
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 rettv->v_type = VAR_STRING;
5680 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 }
5683 else if (working && (opt_type == -2 || opt_type == -1))
5684 ret = FAIL;
5685
5686 *option_end = c; /* put back for error messages */
5687 *arg = option_end;
5688
5689 return ret;
5690}
5691
5692/*
5693 * Allocate a variable for a string constant.
5694 * Return OK or FAIL.
5695 */
5696 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 int evaluate;
5701{
5702 char_u *p;
5703 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 int extra = 0;
5705
5706 /*
5707 * Find the end of the string, skipping backslashed characters.
5708 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005709 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {
5711 if (*p == '\\' && p[1] != NUL)
5712 {
5713 ++p;
5714 /* A "\<x>" form occupies at least 4 characters, and produces up
5715 * to 6 characters: reserve space for 2 extra */
5716 if (*p == '<')
5717 extra += 2;
5718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720
5721 if (*p != '"')
5722 {
5723 EMSG2(_("E114: Missing quote: %s"), *arg);
5724 return FAIL;
5725 }
5726
5727 /* If only parsing, set *arg and return here */
5728 if (!evaluate)
5729 {
5730 *arg = p + 1;
5731 return OK;
5732 }
5733
5734 /*
5735 * Copy the string into allocated memory, handling backslashed
5736 * characters.
5737 */
5738 name = alloc((unsigned)(p - *arg + extra));
5739 if (name == NULL)
5740 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 rettv->v_type = VAR_STRING;
5742 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
5746 if (*p == '\\')
5747 {
5748 switch (*++p)
5749 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 case 'b': *name++ = BS; ++p; break;
5751 case 'e': *name++ = ESC; ++p; break;
5752 case 'f': *name++ = FF; ++p; break;
5753 case 'n': *name++ = NL; ++p; break;
5754 case 'r': *name++ = CAR; ++p; break;
5755 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756
5757 case 'X': /* hex: "\x1", "\x12" */
5758 case 'x':
5759 case 'u': /* Unicode: "\u0023" */
5760 case 'U':
5761 if (vim_isxdigit(p[1]))
5762 {
5763 int n, nr;
5764 int c = toupper(*p);
5765
5766 if (c == 'X')
5767 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005768 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005770 else
5771 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 nr = 0;
5773 while (--n >= 0 && vim_isxdigit(p[1]))
5774 {
5775 ++p;
5776 nr = (nr << 4) + hex2nr(*p);
5777 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779#ifdef FEAT_MBYTE
5780 /* For "\u" store the number according to
5781 * 'encoding'. */
5782 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 else
5785#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 break;
5789
5790 /* octal: "\1", "\12", "\123" */
5791 case '0':
5792 case '1':
5793 case '2':
5794 case '3':
5795 case '4':
5796 case '5':
5797 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 case '7': *name = *p++ - '0';
5799 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 *name = (*name << 3) + *p++ - '0';
5802 if (*p >= '0' && *p <= '7')
5803 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 break;
5807
5808 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 if (extra != 0)
5811 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
5814 }
5815 /* FALLTHROUGH */
5816
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819 }
5820 }
5821 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 *arg = p + 1;
5827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 return OK;
5829}
5830
5831/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 * Return OK or FAIL.
5834 */
5835 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005836get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 int evaluate;
5840{
5841 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 int reduce = 0;
5844
5845 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 */
5848 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5849 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 break;
5854 ++reduce;
5855 ++p;
5856 }
5857 }
5858
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 return FAIL;
5863 }
5864
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005866 if (!evaluate)
5867 {
5868 *arg = p + 1;
5869 return OK;
5870 }
5871
5872 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 */
5875 str = alloc((unsigned)((p - *arg) - reduce));
5876 if (str == NULL)
5877 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 rettv->v_type = VAR_STRING;
5879 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 break;
5887 ++p;
5888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 *arg = p + 1;
5893
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 return OK;
5895}
5896
5897/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898 * Allocate a variable for a List and fill it from "*arg".
5899 * Return OK or FAIL.
5900 */
5901 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005902get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905 int evaluate;
5906{
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 list_T *l = NULL;
5908 typval_T tv;
5909 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910
5911 if (evaluate)
5912 {
5913 l = list_alloc();
5914 if (l == NULL)
5915 return FAIL;
5916 }
5917
5918 *arg = skipwhite(*arg + 1);
5919 while (**arg != ']' && **arg != NUL)
5920 {
5921 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5922 goto failret;
5923 if (evaluate)
5924 {
5925 item = listitem_alloc();
5926 if (item != NULL)
5927 {
5928 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005929 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 list_append(l, item);
5931 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005932 else
5933 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 }
5935
5936 if (**arg == ']')
5937 break;
5938 if (**arg != ',')
5939 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 goto failret;
5942 }
5943 *arg = skipwhite(*arg + 1);
5944 }
5945
5946 if (**arg != ']')
5947 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005948 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949failret:
5950 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005951 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 return FAIL;
5953 }
5954
5955 *arg = skipwhite(*arg + 1);
5956 if (evaluate)
5957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005958 rettv->v_type = VAR_LIST;
5959 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 ++l->lv_refcount;
5961 }
5962
5963 return OK;
5964}
5965
5966/*
5967 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005968 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005970 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971list_alloc()
5972{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005973 list_T *l;
5974
5975 l = (list_T *)alloc_clear(sizeof(list_T));
5976 if (l != NULL)
5977 {
5978 /* Prepend the list to the list of lists for garbage collection. */
5979 if (first_list != NULL)
5980 first_list->lv_used_prev = l;
5981 l->lv_used_prev = NULL;
5982 l->lv_used_next = first_list;
5983 first_list = l;
5984 }
5985 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986}
5987
5988/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005989 * Allocate an empty list for a return value.
5990 * Returns OK or FAIL.
5991 */
5992 static int
5993rettv_list_alloc(rettv)
5994 typval_T *rettv;
5995{
5996 list_T *l = list_alloc();
5997
5998 if (l == NULL)
5999 return FAIL;
6000
6001 rettv->vval.v_list = l;
6002 rettv->v_type = VAR_LIST;
6003 ++l->lv_refcount;
6004 return OK;
6005}
6006
6007/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008 * Unreference a list: decrement the reference count and free it when it
6009 * becomes zero.
6010 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006011 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006013 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006015 if (l != NULL && --l->lv_refcount <= 0)
6016 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017}
6018
6019/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006020 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 * Ignores the reference count.
6022 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006023 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006024list_free(l, recurse)
6025 list_T *l;
6026 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027{
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006030 /* Remove the list from the list of lists for garbage collection. */
6031 if (l->lv_used_prev == NULL)
6032 first_list = l->lv_used_next;
6033 else
6034 l->lv_used_prev->lv_used_next = l->lv_used_next;
6035 if (l->lv_used_next != NULL)
6036 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6037
Bram Moolenaard9fba312005-06-26 22:34:35 +00006038 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006040 /* Remove the item before deleting it. */
6041 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006042 if (recurse || (item->li_tv.v_type != VAR_LIST
6043 && item->li_tv.v_type != VAR_DICT))
6044 clear_tv(&item->li_tv);
6045 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 }
6047 vim_free(l);
6048}
6049
6050/*
6051 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006052 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006054 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055listitem_alloc()
6056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058}
6059
6060/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006061 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006063 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006065 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006067 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 vim_free(item);
6069}
6070
6071/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006072 * Remove a list item from a List and free it. Also clears the value.
6073 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006074 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 list_T *l;
6077 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006079 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006080 listitem_free(item);
6081}
6082
6083/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 * Get the number of items in a list.
6085 */
6086 static long
6087list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 if (l == NULL)
6091 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006092 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093}
6094
6095/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096 * Return TRUE when two lists have exactly the same values.
6097 */
6098 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006099list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006100 list_T *l1;
6101 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006103 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104{
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006107 if (l1 == NULL || l2 == NULL)
6108 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006109 if (l1 == l2)
6110 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006111 if (list_len(l1) != list_len(l2))
6112 return FALSE;
6113
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 for (item1 = l1->lv_first, item2 = l2->lv_first;
6115 item1 != NULL && item2 != NULL;
6116 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006118 return FALSE;
6119 return item1 == NULL && item2 == NULL;
6120}
6121
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006122#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6123 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006124/*
6125 * Return the dictitem that an entry in a hashtable points to.
6126 */
6127 dictitem_T *
6128dict_lookup(hi)
6129 hashitem_T *hi;
6130{
6131 return HI2DI(hi);
6132}
6133#endif
6134
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136 * Return TRUE when two dictionaries have exactly the same key/values.
6137 */
6138 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006139dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 dict_T *d1;
6141 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006142 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006143 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144{
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 hashitem_T *hi;
6146 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006147 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006149 if (d1 == NULL || d2 == NULL)
6150 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006151 if (d1 == d2)
6152 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006153 if (dict_len(d1) != dict_len(d2))
6154 return FALSE;
6155
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006156 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006157 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006158 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006159 if (!HASHITEM_EMPTY(hi))
6160 {
6161 item2 = dict_find(d2, hi->hi_key, -1);
6162 if (item2 == NULL)
6163 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006164 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006165 return FALSE;
6166 --todo;
6167 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006168 }
6169 return TRUE;
6170}
6171
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006172static int tv_equal_recurse_limit;
6173
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006174/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006176 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006177 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178 */
6179 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006180tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006181 typval_T *tv1;
6182 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 int ic; /* ignore case */
6184 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185{
6186 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006187 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006189 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006190
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006191 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006192 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006193
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006194 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006195 * recursiveness to a limit. We guess they are equal then.
6196 * A fixed limit has the problem of still taking an awful long time.
6197 * Reduce the limit every time running into it. That should work fine for
6198 * deeply linked structures that are not recursively linked and catch
6199 * recursiveness quickly. */
6200 if (!recursive)
6201 tv_equal_recurse_limit = 1000;
6202 if (recursive_cnt >= tv_equal_recurse_limit)
6203 {
6204 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006205 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006207
6208 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006209 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006210 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006211 ++recursive_cnt;
6212 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6213 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006214 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006215
6216 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006217 ++recursive_cnt;
6218 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6219 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006220 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006221
6222 case VAR_FUNC:
6223 return (tv1->vval.v_string != NULL
6224 && tv2->vval.v_string != NULL
6225 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6226
6227 case VAR_NUMBER:
6228 return tv1->vval.v_number == tv2->vval.v_number;
6229
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006230#ifdef FEAT_FLOAT
6231 case VAR_FLOAT:
6232 return tv1->vval.v_float == tv2->vval.v_float;
6233#endif
6234
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006235 case VAR_STRING:
6236 s1 = get_tv_string_buf(tv1, buf1);
6237 s2 = get_tv_string_buf(tv2, buf2);
6238 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006239 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006240
6241 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242 return TRUE;
6243}
6244
6245/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246 * Locate item with index "n" in list "l" and return it.
6247 * A negative index is counted from the end; -1 is the last item.
6248 * Returns NULL when "n" is out of range.
6249 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006250 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006251list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 long n;
6254{
Bram Moolenaar33570922005-01-25 22:26:29 +00006255 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 long idx;
6257
6258 if (l == NULL)
6259 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006260
6261 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006263 n = l->lv_len + n;
6264
6265 /* Check for index out of range. */
6266 if (n < 0 || n >= l->lv_len)
6267 return NULL;
6268
6269 /* When there is a cached index may start search from there. */
6270 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006272 if (n < l->lv_idx / 2)
6273 {
6274 /* closest to the start of the list */
6275 item = l->lv_first;
6276 idx = 0;
6277 }
6278 else if (n > (l->lv_idx + l->lv_len) / 2)
6279 {
6280 /* closest to the end of the list */
6281 item = l->lv_last;
6282 idx = l->lv_len - 1;
6283 }
6284 else
6285 {
6286 /* closest to the cached index */
6287 item = l->lv_idx_item;
6288 idx = l->lv_idx;
6289 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 }
6291 else
6292 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006293 if (n < l->lv_len / 2)
6294 {
6295 /* closest to the start of the list */
6296 item = l->lv_first;
6297 idx = 0;
6298 }
6299 else
6300 {
6301 /* closest to the end of the list */
6302 item = l->lv_last;
6303 idx = l->lv_len - 1;
6304 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006306
6307 while (n > idx)
6308 {
6309 /* search forward */
6310 item = item->li_next;
6311 ++idx;
6312 }
6313 while (n < idx)
6314 {
6315 /* search backward */
6316 item = item->li_prev;
6317 --idx;
6318 }
6319
6320 /* cache the used index */
6321 l->lv_idx = idx;
6322 l->lv_idx_item = item;
6323
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 return item;
6325}
6326
6327/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006328 * Get list item "l[idx]" as a number.
6329 */
6330 static long
6331list_find_nr(l, idx, errorp)
6332 list_T *l;
6333 long idx;
6334 int *errorp; /* set to TRUE when something wrong */
6335{
6336 listitem_T *li;
6337
6338 li = list_find(l, idx);
6339 if (li == NULL)
6340 {
6341 if (errorp != NULL)
6342 *errorp = TRUE;
6343 return -1L;
6344 }
6345 return get_tv_number_chk(&li->li_tv, errorp);
6346}
6347
6348/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006349 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6350 */
6351 char_u *
6352list_find_str(l, idx)
6353 list_T *l;
6354 long idx;
6355{
6356 listitem_T *li;
6357
6358 li = list_find(l, idx - 1);
6359 if (li == NULL)
6360 {
6361 EMSGN(_(e_listidx), idx);
6362 return NULL;
6363 }
6364 return get_tv_string(&li->li_tv);
6365}
6366
6367/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006368 * Locate "item" list "l" and return its index.
6369 * Returns -1 when "item" is not in the list.
6370 */
6371 static long
6372list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 list_T *l;
6374 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375{
6376 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378
6379 if (l == NULL)
6380 return -1;
6381 idx = 0;
6382 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6383 ++idx;
6384 if (li == NULL)
6385 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006386 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006387}
6388
6389/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 * Append item "item" to the end of list "l".
6391 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006392 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 list_T *l;
6395 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396{
6397 if (l->lv_last == NULL)
6398 {
6399 /* empty list */
6400 l->lv_first = item;
6401 l->lv_last = item;
6402 item->li_prev = NULL;
6403 }
6404 else
6405 {
6406 l->lv_last->li_next = item;
6407 item->li_prev = l->lv_last;
6408 l->lv_last = item;
6409 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006410 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 item->li_next = NULL;
6412}
6413
6414/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006416 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006418 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006420 list_T *l;
6421 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424
Bram Moolenaar05159a02005-02-26 23:04:13 +00006425 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006427 copy_tv(tv, &li->li_tv);
6428 list_append(l, li);
6429 return OK;
6430}
6431
6432/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006433 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 * Return FAIL when out of memory.
6435 */
6436 int
6437list_append_dict(list, dict)
6438 list_T *list;
6439 dict_T *dict;
6440{
6441 listitem_T *li = listitem_alloc();
6442
6443 if (li == NULL)
6444 return FAIL;
6445 li->li_tv.v_type = VAR_DICT;
6446 li->li_tv.v_lock = 0;
6447 li->li_tv.vval.v_dict = dict;
6448 list_append(list, li);
6449 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 return OK;
6451}
6452
6453/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006454 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006455 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006456 * Returns FAIL when out of memory.
6457 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006458 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006459list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006460 list_T *l;
6461 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006462 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463{
6464 listitem_T *li = listitem_alloc();
6465
6466 if (li == NULL)
6467 return FAIL;
6468 list_append(l, li);
6469 li->li_tv.v_type = VAR_STRING;
6470 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006471 if (str == NULL)
6472 li->li_tv.vval.v_string = NULL;
6473 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006474 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006475 return FAIL;
6476 return OK;
6477}
6478
6479/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006480 * Append "n" to list "l".
6481 * Returns FAIL when out of memory.
6482 */
6483 static int
6484list_append_number(l, n)
6485 list_T *l;
6486 varnumber_T n;
6487{
6488 listitem_T *li;
6489
6490 li = listitem_alloc();
6491 if (li == NULL)
6492 return FAIL;
6493 li->li_tv.v_type = VAR_NUMBER;
6494 li->li_tv.v_lock = 0;
6495 li->li_tv.vval.v_number = n;
6496 list_append(l, li);
6497 return OK;
6498}
6499
6500/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502 * If "item" is NULL append at the end.
6503 * Return FAIL when out of memory.
6504 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006505 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 list_T *l;
6508 typval_T *tv;
6509 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510{
Bram Moolenaar33570922005-01-25 22:26:29 +00006511 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512
6513 if (ni == NULL)
6514 return FAIL;
6515 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006516 list_insert(l, ni, item);
6517 return OK;
6518}
6519
6520 void
6521list_insert(l, ni, item)
6522 list_T *l;
6523 listitem_T *ni;
6524 listitem_T *item;
6525{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 if (item == NULL)
6527 /* Append new item at end of list. */
6528 list_append(l, ni);
6529 else
6530 {
6531 /* Insert new item before existing item. */
6532 ni->li_prev = item->li_prev;
6533 ni->li_next = item;
6534 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006535 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006537 ++l->lv_idx;
6538 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006540 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006542 l->lv_idx_item = NULL;
6543 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006545 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547}
6548
6549/*
6550 * Extend "l1" with "l2".
6551 * If "bef" is NULL append at the end, otherwise insert before this item.
6552 * Returns FAIL when out of memory.
6553 */
6554 static int
6555list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 list_T *l1;
6557 list_T *l2;
6558 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559{
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006561 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006563 /* We also quit the loop when we have inserted the original item count of
6564 * the list, avoid a hang when we extend a list with itself. */
6565 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6567 return FAIL;
6568 return OK;
6569}
6570
6571/*
6572 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6573 * Return FAIL when out of memory.
6574 */
6575 static int
6576list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 list_T *l1;
6578 list_T *l2;
6579 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580{
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006583 if (l1 == NULL || l2 == NULL)
6584 return FAIL;
6585
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006587 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006588 if (l == NULL)
6589 return FAIL;
6590 tv->v_type = VAR_LIST;
6591 tv->vval.v_list = l;
6592
6593 /* append all items from the second list */
6594 return list_extend(l, l2, NULL);
6595}
6596
6597/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006598 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006600 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601 * Returns NULL when out of memory.
6602 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006603 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608{
Bram Moolenaar33570922005-01-25 22:26:29 +00006609 list_T *copy;
6610 listitem_T *item;
6611 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612
6613 if (orig == NULL)
6614 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615
6616 copy = list_alloc();
6617 if (copy != NULL)
6618 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006619 if (copyID != 0)
6620 {
6621 /* Do this before adding the items, because one of the items may
6622 * refer back to this list. */
6623 orig->lv_copyID = copyID;
6624 orig->lv_copylist = copy;
6625 }
6626 for (item = orig->lv_first; item != NULL && !got_int;
6627 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 {
6629 ni = listitem_alloc();
6630 if (ni == NULL)
6631 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006632 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006633 {
6634 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6635 {
6636 vim_free(ni);
6637 break;
6638 }
6639 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006641 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642 list_append(copy, ni);
6643 }
6644 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006645 if (item != NULL)
6646 {
6647 list_unref(copy);
6648 copy = NULL;
6649 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 }
6651
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006652 return copy;
6653}
6654
6655/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006656 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006657 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006658 * This used to be called list_remove, but that conflicts with a Sun header
6659 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006661 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006662vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006663 list_T *l;
6664 listitem_T *item;
6665 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006666{
Bram Moolenaar33570922005-01-25 22:26:29 +00006667 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669 /* notify watchers */
6670 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006672 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673 list_fix_watch(l, ip);
6674 if (ip == item2)
6675 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006677
6678 if (item2->li_next == NULL)
6679 l->lv_last = item->li_prev;
6680 else
6681 item2->li_next->li_prev = item->li_prev;
6682 if (item->li_prev == NULL)
6683 l->lv_first = item2->li_next;
6684 else
6685 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006686 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687}
6688
6689/*
6690 * Return an allocated string with the string representation of a list.
6691 * May return NULL.
6692 */
6693 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006694list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006695 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006696 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697{
6698 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699
6700 if (tv->vval.v_list == NULL)
6701 return NULL;
6702 ga_init2(&ga, (int)sizeof(char), 80);
6703 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006704 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006705 {
6706 vim_free(ga.ga_data);
6707 return NULL;
6708 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006709 ga_append(&ga, ']');
6710 ga_append(&ga, NUL);
6711 return (char_u *)ga.ga_data;
6712}
6713
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006714typedef struct join_S {
6715 char_u *s;
6716 char_u *tofree;
6717} join_T;
6718
6719 static int
6720list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6721 garray_T *gap; /* to store the result in */
6722 list_T *l;
6723 char_u *sep;
6724 int echo_style;
6725 int copyID;
6726 garray_T *join_gap; /* to keep each list item string */
6727{
6728 int i;
6729 join_T *p;
6730 int len;
6731 int sumlen = 0;
6732 int first = TRUE;
6733 char_u *tofree;
6734 char_u numbuf[NUMBUFLEN];
6735 listitem_T *item;
6736 char_u *s;
6737
6738 /* Stringify each item in the list. */
6739 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6740 {
6741 if (echo_style)
6742 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6743 else
6744 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6745 if (s == NULL)
6746 return FAIL;
6747
6748 len = (int)STRLEN(s);
6749 sumlen += len;
6750
Bram Moolenaarcde88542015-08-11 19:14:00 +02006751 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006752 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6753 if (tofree != NULL || s != numbuf)
6754 {
6755 p->s = s;
6756 p->tofree = tofree;
6757 }
6758 else
6759 {
6760 p->s = vim_strnsave(s, len);
6761 p->tofree = p->s;
6762 }
6763
6764 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006765 if (did_echo_string_emsg) /* recursion error, bail out */
6766 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006767 }
6768
6769 /* Allocate result buffer with its total size, avoid re-allocation and
6770 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6771 if (join_gap->ga_len >= 2)
6772 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6773 if (ga_grow(gap, sumlen + 2) == FAIL)
6774 return FAIL;
6775
6776 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6777 {
6778 if (first)
6779 first = FALSE;
6780 else
6781 ga_concat(gap, sep);
6782 p = ((join_T *)join_gap->ga_data) + i;
6783
6784 if (p->s != NULL)
6785 ga_concat(gap, p->s);
6786 line_breakcheck();
6787 }
6788
6789 return OK;
6790}
6791
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006793 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006794 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006795 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006796 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006797 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006798list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006799 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006800 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006802 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006803 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006804{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006805 garray_T join_ga;
6806 int retval;
6807 join_T *p;
6808 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006809
Bram Moolenaard39a7512015-04-16 22:51:22 +02006810 if (l->lv_len < 1)
6811 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006812 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6813 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6814
6815 /* Dispose each item in join_ga. */
6816 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006817 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006818 p = (join_T *)join_ga.ga_data;
6819 for (i = 0; i < join_ga.ga_len; ++i)
6820 {
6821 vim_free(p->tofree);
6822 ++p;
6823 }
6824 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006825 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006826
6827 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006828}
6829
6830/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006831 * Garbage collection for lists and dictionaries.
6832 *
6833 * We use reference counts to be able to free most items right away when they
6834 * are no longer used. But for composite items it's possible that it becomes
6835 * unused while the reference count is > 0: When there is a recursive
6836 * reference. Example:
6837 * :let l = [1, 2, 3]
6838 * :let d = {9: l}
6839 * :let l[1] = d
6840 *
6841 * Since this is quite unusual we handle this with garbage collection: every
6842 * once in a while find out which lists and dicts are not referenced from any
6843 * variable.
6844 *
6845 * Here is a good reference text about garbage collection (refers to Python
6846 * but it applies to all reference-counting mechanisms):
6847 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006848 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849
6850/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851 * Do garbage collection for lists and dicts.
6852 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006853 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 int
6855garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006858 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 buf_T *buf;
6860 win_T *wp;
6861 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006862 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006863 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006865#ifdef FEAT_WINDOWS
6866 tabpage_T *tp;
6867#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006868
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006869 /* Only do this once. */
6870 want_garbage_collect = FALSE;
6871 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006872 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006873
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 /* We advance by two because we add one for items referenced through
6875 * previous_funccal. */
6876 current_copyID += COPYID_INC;
6877 copyID = current_copyID;
6878
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 /*
6880 * 1. Go through all accessible variables and mark all lists and dicts
6881 * with copyID.
6882 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883
6884 /* Don't free variables in the previous_funccal list unless they are only
6885 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006886 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006887 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6888 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6890 NULL);
6891 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6892 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006893 }
6894
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 /* script-local variables */
6896 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898
6899 /* buffer-local variables */
6900 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6902 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903
6904 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006905 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006906 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6907 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006908#ifdef FEAT_AUTOCMD
6909 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6911 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006912#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006914#ifdef FEAT_WINDOWS
6915 /* tabpage-local variables */
6916 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6918 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006919#endif
6920
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923
6924 /* function-local variables */
6925 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6926 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6928 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 }
6930
Bram Moolenaard812df62008-11-09 12:46:09 +00006931 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006933
Bram Moolenaar1dced572012-04-05 16:54:08 +02006934#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006936#endif
6937
Bram Moolenaardb913952012-06-29 12:54:53 +02006938#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006940#endif
6941
6942#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006943 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006944#endif
6945
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006947 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 /*
6949 * 2. Free lists and dictionaries that are not referenced.
6950 */
6951 did_free = free_unref_items(copyID);
6952
6953 /*
6954 * 3. Check if any funccal can be freed now.
6955 */
6956 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 if (can_free_funccal(*pfc, copyID))
6959 {
6960 fc = *pfc;
6961 *pfc = fc->caller;
6962 free_funccal(fc, TRUE);
6963 did_free = TRUE;
6964 did_free_funccal = TRUE;
6965 }
6966 else
6967 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006969 if (did_free_funccal)
6970 /* When a funccal was freed some more items might be garbage
6971 * collected, so run again. */
6972 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006974 else if (p_verbose > 0)
6975 {
6976 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6977 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978
6979 return did_free;
6980}
6981
6982/*
6983 * Free lists and dictionaries that are no longer referenced.
6984 */
6985 static int
6986free_unref_items(copyID)
6987 int copyID;
6988{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006989 dict_T *dd, *dd_next;
6990 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991 int did_free = FALSE;
6992
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 */
6996 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006997 {
6998 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006999 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007001 /* Free the Dictionary and ordinary items it contains, but don't
7002 * recurse into Lists and Dictionaries, they will be in the list
7003 * of dicts or list of lists. */
7004 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 dd = dd_next;
7008 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009
7010 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007011 * Go through the list of lists and free items without the copyID.
7012 * But don't free a list that has a watcher (used in a for loop), these
7013 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 */
7015 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007016 {
7017 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007018 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7019 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007021 /* Free the List and ordinary items it contains, but don't recurse
7022 * into Lists and Dictionaries, they will be in the list of dicts
7023 * or list of lists. */
7024 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007027 ll = ll_next;
7028 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 return did_free;
7030}
7031
7032/*
7033 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 * "list_stack" is used to add lists to be marked. Can be NULL.
7035 *
7036 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 int
7039set_ref_in_ht(ht, copyID, list_stack)
7040 hashtab_T *ht;
7041 int copyID;
7042 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043{
7044 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 hashtab_T *cur_ht;
7048 ht_stack_T *ht_stack = NULL;
7049 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051 cur_ht = ht;
7052 for (;;)
7053 {
7054 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007056 /* Mark each item in the hashtab. If the item contains a hashtab
7057 * it is added to ht_stack, if it contains a list it is added to
7058 * list_stack. */
7059 todo = (int)cur_ht->ht_used;
7060 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7061 if (!HASHITEM_EMPTY(hi))
7062 {
7063 --todo;
7064 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7065 &ht_stack, list_stack);
7066 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068
7069 if (ht_stack == NULL)
7070 break;
7071
7072 /* take an item from the stack */
7073 cur_ht = ht_stack->ht;
7074 tempitem = ht_stack;
7075 ht_stack = ht_stack->prev;
7076 free(tempitem);
7077 }
7078
7079 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080}
7081
7082/*
7083 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7085 *
7086 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 int
7089set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090 list_T *l;
7091 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007092 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007094 listitem_T *li;
7095 int abort = FALSE;
7096 list_T *cur_l;
7097 list_stack_T *list_stack = NULL;
7098 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007099
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007100 cur_l = l;
7101 for (;;)
7102 {
7103 if (!abort)
7104 /* Mark each item in the list. If the item contains a hashtab
7105 * it is added to ht_stack, if it contains a list it is added to
7106 * list_stack. */
7107 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7108 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7109 ht_stack, &list_stack);
7110 if (list_stack == NULL)
7111 break;
7112
7113 /* take an item from the stack */
7114 cur_l = list_stack->list;
7115 tempitem = list_stack;
7116 list_stack = list_stack->prev;
7117 free(tempitem);
7118 }
7119
7120 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007121}
7122
7123/*
7124 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007125 * "list_stack" is used to add lists to be marked. Can be NULL.
7126 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7127 *
7128 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007129 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 int
7131set_ref_in_item(tv, copyID, ht_stack, list_stack)
7132 typval_T *tv;
7133 int copyID;
7134 ht_stack_T **ht_stack;
7135 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007136{
7137 dict_T *dd;
7138 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007139 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007140
7141 switch (tv->v_type)
7142 {
7143 case VAR_DICT:
7144 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007145 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007146 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007147 /* Didn't see this dict yet. */
7148 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007149 if (ht_stack == NULL)
7150 {
7151 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7152 }
7153 else
7154 {
7155 ht_stack_T *newitem = (ht_stack_T*)malloc(
7156 sizeof(ht_stack_T));
7157 if (newitem == NULL)
7158 abort = TRUE;
7159 else
7160 {
7161 newitem->ht = &dd->dv_hashtab;
7162 newitem->prev = *ht_stack;
7163 *ht_stack = newitem;
7164 }
7165 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007167 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168
7169 case VAR_LIST:
7170 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007171 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007172 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007173 /* Didn't see this list yet. */
7174 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007175 if (list_stack == NULL)
7176 {
7177 abort = set_ref_in_list(ll, copyID, ht_stack);
7178 }
7179 else
7180 {
7181 list_stack_T *newitem = (list_stack_T*)malloc(
7182 sizeof(list_stack_T));
7183 if (newitem == NULL)
7184 abort = TRUE;
7185 else
7186 {
7187 newitem->list = ll;
7188 newitem->prev = *list_stack;
7189 *list_stack = newitem;
7190 }
7191 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007192 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007194 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007195 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007196}
7197
7198/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199 * Allocate an empty header for a dictionary.
7200 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007201 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202dict_alloc()
7203{
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205
Bram Moolenaar33570922005-01-25 22:26:29 +00007206 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007208 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007209 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007210 if (first_dict != NULL)
7211 first_dict->dv_used_prev = d;
7212 d->dv_used_next = first_dict;
7213 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007214 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007215
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007217 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007218 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007219 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007220 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007221 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007222 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223}
7224
7225/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007226 * Allocate an empty dict for a return value.
7227 * Returns OK or FAIL.
7228 */
7229 static int
7230rettv_dict_alloc(rettv)
7231 typval_T *rettv;
7232{
7233 dict_T *d = dict_alloc();
7234
7235 if (d == NULL)
7236 return FAIL;
7237
7238 rettv->vval.v_dict = d;
7239 rettv->v_type = VAR_DICT;
7240 ++d->dv_refcount;
7241 return OK;
7242}
7243
7244
7245/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 * Unreference a Dictionary: decrement the reference count and free it when it
7247 * becomes zero.
7248 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007249 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007253 if (d != NULL && --d->dv_refcount <= 0)
7254 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255}
7256
7257/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007258 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 * Ignores the reference count.
7260 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007261 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007262dict_free(d, recurse)
7263 dict_T *d;
7264 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007266 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007268 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007270 /* Remove the dict from the list of dicts for garbage collection. */
7271 if (d->dv_used_prev == NULL)
7272 first_dict = d->dv_used_next;
7273 else
7274 d->dv_used_prev->dv_used_next = d->dv_used_next;
7275 if (d->dv_used_next != NULL)
7276 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7277
7278 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007279 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007280 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 if (!HASHITEM_EMPTY(hi))
7284 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007285 /* Remove the item before deleting it, just in case there is
7286 * something recursive causing trouble. */
7287 di = HI2DI(hi);
7288 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007289 if (recurse || (di->di_tv.v_type != VAR_LIST
7290 && di->di_tv.v_type != VAR_DICT))
7291 clear_tv(&di->di_tv);
7292 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 --todo;
7294 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297 vim_free(d);
7298}
7299
7300/*
7301 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 * The "key" is copied to the new item.
7303 * Note that the value of the item "di_tv" still needs to be initialized!
7304 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007306 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307dictitem_alloc(key)
7308 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309{
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007312 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007314 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007316 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007317 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319}
7320
7321/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322 * Make a copy of a Dictionary item.
7323 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327{
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007330 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7331 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 if (di != NULL)
7333 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007334 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007335 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336 copy_tv(&org->di_tv, &di->di_tv);
7337 }
7338 return di;
7339}
7340
7341/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342 * Remove item "item" from Dictionary "dict" and free it.
7343 */
7344 static void
7345dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 dict_T *dict;
7347 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348{
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007350
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 if (HASHITEM_EMPTY(hi))
7353 EMSG2(_(e_intern2), "dictitem_remove()");
7354 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007356 dictitem_free(item);
7357}
7358
7359/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360 * Free a dict item. Also clears the value.
7361 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007362 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007363dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007367 if (item->di_flags & DI_FLAGS_ALLOC)
7368 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369}
7370
7371/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7373 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375 * Returns NULL when out of memory.
7376 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007381 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382{
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 dict_T *copy;
7384 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007385 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387
7388 if (orig == NULL)
7389 return NULL;
7390
7391 copy = dict_alloc();
7392 if (copy != NULL)
7393 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007394 if (copyID != 0)
7395 {
7396 orig->dv_copyID = copyID;
7397 orig->dv_copydict = copy;
7398 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007399 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007400 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007403 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404 --todo;
7405
7406 di = dictitem_alloc(hi->hi_key);
7407 if (di == NULL)
7408 break;
7409 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007410 {
7411 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7412 copyID) == FAIL)
7413 {
7414 vim_free(di);
7415 break;
7416 }
7417 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007418 else
7419 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7420 if (dict_add(copy, di) == FAIL)
7421 {
7422 dictitem_free(di);
7423 break;
7424 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007426 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007429 if (todo > 0)
7430 {
7431 dict_unref(copy);
7432 copy = NULL;
7433 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434 }
7435
7436 return copy;
7437}
7438
7439/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007441 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007443 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 dict_T *d;
7446 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447{
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449}
7450
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007452 * Add a number or string entry to dictionary "d".
7453 * When "str" is NULL use number "nr", otherwise use "str".
7454 * Returns FAIL when out of memory and when key already exists.
7455 */
7456 int
7457dict_add_nr_str(d, key, nr, str)
7458 dict_T *d;
7459 char *key;
7460 long nr;
7461 char_u *str;
7462{
7463 dictitem_T *item;
7464
7465 item = dictitem_alloc((char_u *)key);
7466 if (item == NULL)
7467 return FAIL;
7468 item->di_tv.v_lock = 0;
7469 if (str == NULL)
7470 {
7471 item->di_tv.v_type = VAR_NUMBER;
7472 item->di_tv.vval.v_number = nr;
7473 }
7474 else
7475 {
7476 item->di_tv.v_type = VAR_STRING;
7477 item->di_tv.vval.v_string = vim_strsave(str);
7478 }
7479 if (dict_add(d, item) == FAIL)
7480 {
7481 dictitem_free(item);
7482 return FAIL;
7483 }
7484 return OK;
7485}
7486
7487/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007488 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007489 * Returns FAIL when out of memory and when key already exists.
7490 */
7491 int
7492dict_add_list(d, key, list)
7493 dict_T *d;
7494 char *key;
7495 list_T *list;
7496{
7497 dictitem_T *item;
7498
7499 item = dictitem_alloc((char_u *)key);
7500 if (item == NULL)
7501 return FAIL;
7502 item->di_tv.v_lock = 0;
7503 item->di_tv.v_type = VAR_LIST;
7504 item->di_tv.vval.v_list = list;
7505 if (dict_add(d, item) == FAIL)
7506 {
7507 dictitem_free(item);
7508 return FAIL;
7509 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007510 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007511 return OK;
7512}
7513
7514/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007515 * Get the number of items in a Dictionary.
7516 */
7517 static long
7518dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007520{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007521 if (d == NULL)
7522 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007523 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007524}
7525
7526/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 * Find item "key[len]" in Dictionary "d".
7528 * If "len" is negative use strlen(key).
7529 * Returns NULL when not found.
7530 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007531 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007533 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 char_u *key;
7535 int len;
7536{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007537#define AKEYLEN 200
7538 char_u buf[AKEYLEN];
7539 char_u *akey;
7540 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 if (len < 0)
7544 akey = key;
7545 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007546 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 tofree = akey = vim_strnsave(key, len);
7548 if (akey == NULL)
7549 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007550 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 else
7552 {
7553 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007554 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007555 akey = buf;
7556 }
7557
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 vim_free(tofree);
7560 if (HASHITEM_EMPTY(hi))
7561 return NULL;
7562 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563}
7564
7565/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007566 * Get a string item from a dictionary.
7567 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007568 * Returns NULL if the entry doesn't exist or out of memory.
7569 */
7570 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007571get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007572 dict_T *d;
7573 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007574 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007575{
7576 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007577 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007578
7579 di = dict_find(d, key, -1);
7580 if (di == NULL)
7581 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007582 s = get_tv_string(&di->di_tv);
7583 if (save && s != NULL)
7584 s = vim_strsave(s);
7585 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007586}
7587
7588/*
7589 * Get a number item from a dictionary.
7590 * Returns 0 if the entry doesn't exist or out of memory.
7591 */
7592 long
7593get_dict_number(d, key)
7594 dict_T *d;
7595 char_u *key;
7596{
7597 dictitem_T *di;
7598
7599 di = dict_find(d, key, -1);
7600 if (di == NULL)
7601 return 0;
7602 return get_tv_number(&di->di_tv);
7603}
7604
7605/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 * Return an allocated string with the string representation of a Dictionary.
7607 * May return NULL.
7608 */
7609 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007610dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007611 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007612 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613{
7614 garray_T ga;
7615 int first = TRUE;
7616 char_u *tofree;
7617 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007620 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007621 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 return NULL;
7625 ga_init2(&ga, (int)sizeof(char), 80);
7626 ga_append(&ga, '{');
7627
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007628 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007629 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007631 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633 --todo;
7634
7635 if (first)
7636 first = FALSE;
7637 else
7638 ga_concat(&ga, (char_u *)", ");
7639
7640 tofree = string_quote(hi->hi_key, FALSE);
7641 if (tofree != NULL)
7642 {
7643 ga_concat(&ga, tofree);
7644 vim_free(tofree);
7645 }
7646 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007647 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007648 if (s != NULL)
7649 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007651 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007652 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007653 line_breakcheck();
7654
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007657 if (todo > 0)
7658 {
7659 vim_free(ga.ga_data);
7660 return NULL;
7661 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662
7663 ga_append(&ga, '}');
7664 ga_append(&ga, NUL);
7665 return (char_u *)ga.ga_data;
7666}
7667
7668/*
7669 * Allocate a variable for a Dictionary and fill it from "*arg".
7670 * Return OK or FAIL. Returns NOTDONE for {expr}.
7671 */
7672 static int
7673get_dict_tv(arg, rettv, evaluate)
7674 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 int evaluate;
7677{
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 dict_T *d = NULL;
7679 typval_T tvkey;
7680 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007681 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007684 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685
7686 /*
7687 * First check if it's not a curly-braces thing: {expr}.
7688 * Must do this without evaluating, otherwise a function may be called
7689 * twice. Unfortunately this means we need to call eval1() twice for the
7690 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007691 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007693 if (*start != '}')
7694 {
7695 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7696 return FAIL;
7697 if (*start == '}')
7698 return NOTDONE;
7699 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700
7701 if (evaluate)
7702 {
7703 d = dict_alloc();
7704 if (d == NULL)
7705 return FAIL;
7706 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007707 tvkey.v_type = VAR_UNKNOWN;
7708 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007709
7710 *arg = skipwhite(*arg + 1);
7711 while (**arg != '}' && **arg != NUL)
7712 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007713 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 goto failret;
7715 if (**arg != ':')
7716 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007717 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007718 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719 goto failret;
7720 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007721 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007723 key = get_tv_string_buf_chk(&tvkey, buf);
7724 if (key == NULL || *key == NUL)
7725 {
7726 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7727 if (key != NULL)
7728 EMSG(_(e_emptykey));
7729 clear_tv(&tvkey);
7730 goto failret;
7731 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733
7734 *arg = skipwhite(*arg + 1);
7735 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7736 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007737 if (evaluate)
7738 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 goto failret;
7740 }
7741 if (evaluate)
7742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007743 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 if (item != NULL)
7745 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007746 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007747 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007748 clear_tv(&tv);
7749 goto failret;
7750 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007751 item = dictitem_alloc(key);
7752 clear_tv(&tvkey);
7753 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007756 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007757 if (dict_add(d, item) == FAIL)
7758 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759 }
7760 }
7761
7762 if (**arg == '}')
7763 break;
7764 if (**arg != ',')
7765 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007766 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007767 goto failret;
7768 }
7769 *arg = skipwhite(*arg + 1);
7770 }
7771
7772 if (**arg != '}')
7773 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007774 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775failret:
7776 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007777 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007778 return FAIL;
7779 }
7780
7781 *arg = skipwhite(*arg + 1);
7782 if (evaluate)
7783 {
7784 rettv->v_type = VAR_DICT;
7785 rettv->vval.v_dict = d;
7786 ++d->dv_refcount;
7787 }
7788
7789 return OK;
7790}
7791
Bram Moolenaar8c711452005-01-14 21:53:12 +00007792/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 * Return a string with the string representation of a variable.
7794 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007795 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007796 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007797 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007798 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 */
7800 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007801echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007804 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007805 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007807 static int recurse = 0;
7808 char_u *r = NULL;
7809
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007812 if (!did_echo_string_emsg)
7813 {
7814 /* Only give this message once for a recursive call to avoid
7815 * flooding the user with errors. And stop iterating over lists
7816 * and dicts. */
7817 did_echo_string_emsg = TRUE;
7818 EMSG(_("E724: variable nested too deep for displaying"));
7819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007821 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 }
7823 ++recurse;
7824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007825 switch (tv->v_type)
7826 {
7827 case VAR_FUNC:
7828 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829 r = tv->vval.v_string;
7830 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007832 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007833 if (tv->vval.v_list == NULL)
7834 {
7835 *tofree = NULL;
7836 r = NULL;
7837 }
7838 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7839 {
7840 *tofree = NULL;
7841 r = (char_u *)"[...]";
7842 }
7843 else
7844 {
7845 tv->vval.v_list->lv_copyID = copyID;
7846 *tofree = list2string(tv, copyID);
7847 r = *tofree;
7848 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007850
Bram Moolenaar8c711452005-01-14 21:53:12 +00007851 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007852 if (tv->vval.v_dict == NULL)
7853 {
7854 *tofree = NULL;
7855 r = NULL;
7856 }
7857 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7858 {
7859 *tofree = NULL;
7860 r = (char_u *)"{...}";
7861 }
7862 else
7863 {
7864 tv->vval.v_dict->dv_copyID = copyID;
7865 *tofree = dict2string(tv, copyID);
7866 r = *tofree;
7867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007870 case VAR_STRING:
7871 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007872 *tofree = NULL;
7873 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007874 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007875
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876#ifdef FEAT_FLOAT
7877 case VAR_FLOAT:
7878 *tofree = NULL;
7879 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7880 r = numbuf;
7881 break;
7882#endif
7883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007885 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007886 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007887 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888
Bram Moolenaar8502c702014-06-17 12:51:16 +02007889 if (--recurse == 0)
7890 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007891 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892}
7893
7894/*
7895 * Return a string with the string representation of a variable.
7896 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7897 * "numbuf" is used for a number.
7898 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007899 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007900 */
7901 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007902tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007904 char_u **tofree;
7905 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007906 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907{
7908 switch (tv->v_type)
7909 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007910 case VAR_FUNC:
7911 *tofree = string_quote(tv->vval.v_string, TRUE);
7912 return *tofree;
7913 case VAR_STRING:
7914 *tofree = string_quote(tv->vval.v_string, FALSE);
7915 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007916#ifdef FEAT_FLOAT
7917 case VAR_FLOAT:
7918 *tofree = NULL;
7919 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7920 return numbuf;
7921#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007922 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007923 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007924 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007925 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007928 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007929 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007930}
7931
7932/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007933 * Return string "str" in ' quotes, doubling ' characters.
7934 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007936 */
7937 static char_u *
7938string_quote(str, function)
7939 char_u *str;
7940 int function;
7941{
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007943 char_u *p, *r, *s;
7944
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 len = (function ? 13 : 3);
7946 if (str != NULL)
7947 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007948 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 for (p = str; *p != NUL; mb_ptr_adv(p))
7950 if (*p == '\'')
7951 ++len;
7952 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007953 s = r = alloc(len);
7954 if (r != NULL)
7955 {
7956 if (function)
7957 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007958 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007959 r += 10;
7960 }
7961 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007962 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007963 if (str != NULL)
7964 for (p = str; *p != NUL; )
7965 {
7966 if (*p == '\'')
7967 *r++ = '\'';
7968 MB_COPY_CHAR(p, r);
7969 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007971 if (function)
7972 *r++ = ')';
7973 *r++ = NUL;
7974 }
7975 return s;
7976}
7977
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007978#ifdef FEAT_FLOAT
7979/*
7980 * Convert the string "text" to a floating point number.
7981 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7982 * this always uses a decimal point.
7983 * Returns the length of the text that was consumed.
7984 */
7985 static int
7986string2float(text, value)
7987 char_u *text;
7988 float_T *value; /* result stored here */
7989{
7990 char *s = (char *)text;
7991 float_T f;
7992
7993 f = strtod(s, &s);
7994 *value = f;
7995 return (int)((char_u *)s - text);
7996}
7997#endif
7998
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 * Get the value of an environment variable.
8001 * "arg" is pointing to the '$'. It is advanced to after the name.
8002 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008003 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 */
8005 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 int evaluate;
8010{
8011 char_u *string = NULL;
8012 int len;
8013 int cc;
8014 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008015 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
8017 ++*arg;
8018 name = *arg;
8019 len = get_env_len(arg);
8020 if (evaluate)
8021 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008022 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008023 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008024
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008025 cc = name[len];
8026 name[len] = NUL;
8027 /* first try vim_getenv(), fast for normal environment vars */
8028 string = vim_getenv(name, &mustfree);
8029 if (string != NULL && *string != NUL)
8030 {
8031 if (!mustfree)
8032 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008034 else
8035 {
8036 if (mustfree)
8037 vim_free(string);
8038
8039 /* next try expanding things like $VIM and ${HOME} */
8040 string = expand_env_save(name - 1);
8041 if (string != NULL && *string == '$')
8042 {
8043 vim_free(string);
8044 string = NULL;
8045 }
8046 }
8047 name[len] = cc;
8048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049 rettv->v_type = VAR_STRING;
8050 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 }
8052
8053 return OK;
8054}
8055
8056/*
8057 * Array with names and number of arguments of all internal functions
8058 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8059 */
8060static struct fst
8061{
8062 char *f_name; /* function name */
8063 char f_min_argc; /* minimal number of arguments */
8064 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008065 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008066 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067} functions[] =
8068{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008069#ifdef FEAT_FLOAT
8070 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008071 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008073 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008074 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"append", 2, 2, f_append},
8076 {"argc", 0, 0, f_argc},
8077 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008078 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008079 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008080#ifdef FEAT_FLOAT
8081 {"asin", 1, 1, f_asin}, /* WJMc */
8082#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008083 {"assert_equal", 2, 3, f_assert_equal},
8084 {"assert_false", 1, 2, f_assert_false},
8085 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#ifdef FEAT_FLOAT
8087 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008088 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008091 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"bufexists", 1, 1, f_bufexists},
8093 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8094 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8095 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8096 {"buflisted", 1, 1, f_buflisted},
8097 {"bufloaded", 1, 1, f_bufloaded},
8098 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008099 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"bufwinnr", 1, 1, f_bufwinnr},
8101 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008102 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008103 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008104 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008105#ifdef FEAT_FLOAT
8106 {"ceil", 1, 1, f_ceil},
8107#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008108 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008109 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008111 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008113#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008114 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008115 {"complete_add", 1, 1, f_complete_add},
8116 {"complete_check", 0, 0, f_complete_check},
8117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008119 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008120#ifdef FEAT_FLOAT
8121 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008122 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008123#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008124 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008126 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008127 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"delete", 1, 1, f_delete},
8129 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008130 {"diff_filler", 1, 1, f_diff_filler},
8131 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008132 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008134 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"eventhandler", 0, 0, f_eventhandler},
8136 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008137 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008139#ifdef FEAT_FLOAT
8140 {"exp", 1, 1, f_exp},
8141#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008142 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008143 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008144 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8146 {"filereadable", 1, 1, f_filereadable},
8147 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008148 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008149 {"finddir", 1, 3, f_finddir},
8150 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008151#ifdef FEAT_FLOAT
8152 {"float2nr", 1, 1, f_float2nr},
8153 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008154 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008155#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008156 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"fnamemodify", 2, 2, f_fnamemodify},
8158 {"foldclosed", 1, 1, f_foldclosed},
8159 {"foldclosedend", 1, 1, f_foldclosedend},
8160 {"foldlevel", 1, 1, f_foldlevel},
8161 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008162 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008164 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008165 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008166 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008167 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008168 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {"getchar", 0, 1, f_getchar},
8170 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008171 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"getcmdline", 0, 0, f_getcmdline},
8173 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008174 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008175 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008176 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008178 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008179 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"getfsize", 1, 1, f_getfsize},
8181 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008182 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008183 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008184 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008185 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008186 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008187 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008188 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008189 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008191 {"gettabvar", 2, 3, f_gettabvar},
8192 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"getwinposx", 0, 0, f_getwinposx},
8194 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008195 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008196 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008197 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008198 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008200 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008201 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008202 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8204 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8205 {"histadd", 2, 2, f_histadd},
8206 {"histdel", 1, 2, f_histdel},
8207 {"histget", 1, 2, f_histget},
8208 {"histnr", 1, 1, f_histnr},
8209 {"hlID", 1, 1, f_hlID},
8210 {"hlexists", 1, 1, f_hlexists},
8211 {"hostname", 0, 0, f_hostname},
8212 {"iconv", 3, 3, f_iconv},
8213 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008214 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008215 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008217 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {"inputrestore", 0, 0, f_inputrestore},
8219 {"inputsave", 0, 0, f_inputsave},
8220 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008221 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008222 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008224 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008225 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008226 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008227 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008229 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"libcall", 3, 3, f_libcall},
8231 {"libcallnr", 3, 3, f_libcallnr},
8232 {"line", 1, 1, f_line},
8233 {"line2byte", 1, 1, f_line2byte},
8234 {"lispindent", 1, 1, f_lispindent},
8235 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008236#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008237 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008238 {"log10", 1, 1, f_log10},
8239#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008240#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008241 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008242#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008243 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008244 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008245 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008246 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008247 {"matchadd", 2, 5, f_matchadd},
8248 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008249 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008250 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008251 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008252 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008253 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008254 {"max", 1, 1, f_max},
8255 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008256#ifdef vim_mkdir
8257 {"mkdir", 1, 3, f_mkdir},
8258#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008259 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008260#ifdef FEAT_MZSCHEME
8261 {"mzeval", 1, 1, f_mzeval},
8262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008264 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008265 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008266 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008267#ifdef FEAT_FLOAT
8268 {"pow", 2, 2, f_pow},
8269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008271 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008272 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008273#ifdef FEAT_PYTHON3
8274 {"py3eval", 1, 1, f_py3eval},
8275#endif
8276#ifdef FEAT_PYTHON
8277 {"pyeval", 1, 1, f_pyeval},
8278#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008279 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008280 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008281 {"reltime", 0, 2, f_reltime},
8282 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {"remote_expr", 2, 3, f_remote_expr},
8284 {"remote_foreground", 1, 1, f_remote_foreground},
8285 {"remote_peek", 1, 2, f_remote_peek},
8286 {"remote_read", 1, 1, f_remote_read},
8287 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008288 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008290 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008292 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008293#ifdef FEAT_FLOAT
8294 {"round", 1, 1, f_round},
8295#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008296 {"screenattr", 2, 2, f_screenattr},
8297 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008298 {"screencol", 0, 0, f_screencol},
8299 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008300 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008301 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008302 {"searchpair", 3, 7, f_searchpair},
8303 {"searchpairpos", 3, 7, f_searchpairpos},
8304 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"server2client", 2, 2, f_server2client},
8306 {"serverlist", 0, 0, f_serverlist},
8307 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008308 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {"setcmdpos", 1, 1, f_setcmdpos},
8310 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008311 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008312 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008313 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008314 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008316 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008317 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008319#ifdef FEAT_CRYPT
8320 {"sha256", 1, 1, f_sha256},
8321#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008322 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008323 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008325#ifdef FEAT_FLOAT
8326 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008327 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008328#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008329 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008330 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008331 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008332 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008333 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008334#ifdef FEAT_FLOAT
8335 {"sqrt", 1, 1, f_sqrt},
8336 {"str2float", 1, 1, f_str2float},
8337#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008338 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008339 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008340 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341#ifdef HAVE_STRFTIME
8342 {"strftime", 1, 2, f_strftime},
8343#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008344 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008345 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {"strlen", 1, 1, f_strlen},
8347 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008348 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008350 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008351 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"substitute", 4, 4, f_substitute},
8353 {"synID", 3, 3, f_synID},
8354 {"synIDattr", 2, 3, f_synIDattr},
8355 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008356 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008357 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008358 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008359 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008360 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008361 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008362 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008363 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008364 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008365#ifdef FEAT_FLOAT
8366 {"tan", 1, 1, f_tan},
8367 {"tanh", 1, 1, f_tanh},
8368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008370 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {"tolower", 1, 1, f_tolower},
8372 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008373 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008374#ifdef FEAT_FLOAT
8375 {"trunc", 1, 1, f_trunc},
8376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008378 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008379 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008380 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008381 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {"virtcol", 1, 1, f_virtcol},
8383 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008384 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {"winbufnr", 1, 1, f_winbufnr},
8386 {"wincol", 0, 0, f_wincol},
8387 {"winheight", 1, 1, f_winheight},
8388 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008389 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008391 {"winrestview", 1, 1, f_winrestview},
8392 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008394 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008395 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008396 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397};
8398
8399#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8400
8401/*
8402 * Function given to ExpandGeneric() to obtain the list of internal
8403 * or user defined function names.
8404 */
8405 char_u *
8406get_function_name(xp, idx)
8407 expand_T *xp;
8408 int idx;
8409{
8410 static int intidx = -1;
8411 char_u *name;
8412
8413 if (idx == 0)
8414 intidx = -1;
8415 if (intidx < 0)
8416 {
8417 name = get_user_func_name(xp, idx);
8418 if (name != NULL)
8419 return name;
8420 }
8421 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8422 {
8423 STRCPY(IObuff, functions[intidx].f_name);
8424 STRCAT(IObuff, "(");
8425 if (functions[intidx].f_max_argc == 0)
8426 STRCAT(IObuff, ")");
8427 return IObuff;
8428 }
8429
8430 return NULL;
8431}
8432
8433/*
8434 * Function given to ExpandGeneric() to obtain the list of internal or
8435 * user defined variable or function names.
8436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 char_u *
8438get_expr_name(xp, idx)
8439 expand_T *xp;
8440 int idx;
8441{
8442 static int intidx = -1;
8443 char_u *name;
8444
8445 if (idx == 0)
8446 intidx = -1;
8447 if (intidx < 0)
8448 {
8449 name = get_function_name(xp, idx);
8450 if (name != NULL)
8451 return name;
8452 }
8453 return get_user_var_name(xp, ++intidx);
8454}
8455
8456#endif /* FEAT_CMDL_COMPL */
8457
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008458#if defined(EBCDIC) || defined(PROTO)
8459/*
8460 * Compare struct fst by function name.
8461 */
8462 static int
8463compare_func_name(s1, s2)
8464 const void *s1;
8465 const void *s2;
8466{
8467 struct fst *p1 = (struct fst *)s1;
8468 struct fst *p2 = (struct fst *)s2;
8469
8470 return STRCMP(p1->f_name, p2->f_name);
8471}
8472
8473/*
8474 * Sort the function table by function name.
8475 * The sorting of the table above is ASCII dependant.
8476 * On machines using EBCDIC we have to sort it.
8477 */
8478 static void
8479sortFunctions()
8480{
8481 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8482
8483 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8484}
8485#endif
8486
8487
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488/*
8489 * Find internal function in table above.
8490 * Return index, or -1 if not found
8491 */
8492 static int
8493find_internal_func(name)
8494 char_u *name; /* name of the function */
8495{
8496 int first = 0;
8497 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8498 int cmp;
8499 int x;
8500
8501 /*
8502 * Find the function name in the table. Binary search.
8503 */
8504 while (first <= last)
8505 {
8506 x = first + ((unsigned)(last - first) >> 1);
8507 cmp = STRCMP(name, functions[x].f_name);
8508 if (cmp < 0)
8509 last = x - 1;
8510 else if (cmp > 0)
8511 first = x + 1;
8512 else
8513 return x;
8514 }
8515 return -1;
8516}
8517
8518/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008519 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8520 * name it contains, otherwise return "name".
8521 */
8522 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008523deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 char_u *name;
8525 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008526 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527{
Bram Moolenaar33570922005-01-25 22:26:29 +00008528 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 int cc;
8530
8531 cc = name[*lenp];
8532 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008533 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008534 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008535 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008536 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008537 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008538 {
8539 *lenp = 0;
8540 return (char_u *)""; /* just in case */
8541 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008542 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008544 }
8545
8546 return name;
8547}
8548
8549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 * Allocate a variable for the result of a function.
8551 * Return OK or FAIL.
8552 */
8553 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008554get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8555 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 char_u *name; /* name of the function */
8557 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 char_u **arg; /* argument, pointing to the '(' */
8560 linenr_T firstline; /* first line of range */
8561 linenr_T lastline; /* last line of range */
8562 int *doesrange; /* return: function handled range */
8563 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008564 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
8566 char_u *argp;
8567 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008568 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 int argcount = 0; /* number of arguments found */
8570
8571 /*
8572 * Get the arguments.
8573 */
8574 argp = *arg;
8575 while (argcount < MAX_FUNC_ARGS)
8576 {
8577 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8578 if (*argp == ')' || *argp == ',' || *argp == NUL)
8579 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8581 {
8582 ret = FAIL;
8583 break;
8584 }
8585 ++argcount;
8586 if (*argp != ',')
8587 break;
8588 }
8589 if (*argp == ')')
8590 ++argp;
8591 else
8592 ret = FAIL;
8593
8594 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008595 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008596 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 {
8599 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008600 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008602 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
8605 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008606 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
8608 *arg = skipwhite(argp);
8609 return ret;
8610}
8611
8612
8613/*
8614 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008615 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008616 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 */
8618 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008619call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008620 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008621 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008625 typval_T *argvars; /* vars for arguments, must have "argcount"
8626 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 linenr_T firstline; /* first line of range */
8628 linenr_T lastline; /* last line of range */
8629 int *doesrange; /* return: function handled range */
8630 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008631 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632{
8633 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634#define ERROR_UNKNOWN 0
8635#define ERROR_TOOMANY 1
8636#define ERROR_TOOFEW 2
8637#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008638#define ERROR_DICT 4
8639#define ERROR_NONE 5
8640#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 int error = ERROR_NONE;
8642 int i;
8643 int llen;
8644 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645#define FLEN_FIXED 40
8646 char_u fname_buf[FLEN_FIXED + 1];
8647 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008648 char_u *name;
8649
8650 /* Make a copy of the name, if it comes from a funcref variable it could
8651 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008652 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008653 if (name == NULL)
8654 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655
8656 /*
8657 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8658 * Change <SNR>123_name() to K_SNR 123_name().
8659 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 llen = eval_fname_script(name);
8662 if (llen > 0)
8663 {
8664 fname_buf[0] = K_SPECIAL;
8665 fname_buf[1] = KS_EXTRA;
8666 fname_buf[2] = (int)KE_SNR;
8667 i = 3;
8668 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8669 {
8670 if (current_SID <= 0)
8671 error = ERROR_SCRIPT;
8672 else
8673 {
8674 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8675 i = (int)STRLEN(fname_buf);
8676 }
8677 }
8678 if (i + STRLEN(name + llen) < FLEN_FIXED)
8679 {
8680 STRCPY(fname_buf + i, name + llen);
8681 fname = fname_buf;
8682 }
8683 else
8684 {
8685 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8686 if (fname == NULL)
8687 error = ERROR_OTHER;
8688 else
8689 {
8690 mch_memmove(fname, fname_buf, (size_t)i);
8691 STRCPY(fname + i, name + llen);
8692 }
8693 }
8694 }
8695 else
8696 fname = name;
8697
8698 *doesrange = FALSE;
8699
8700
8701 /* execute the function if no errors detected and executing */
8702 if (evaluate && error == ERROR_NONE)
8703 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008704 char_u *rfname = fname;
8705
8706 /* Ignore "g:" before a function name. */
8707 if (fname[0] == 'g' && fname[1] == ':')
8708 rfname = fname + 2;
8709
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008710 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8711 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 error = ERROR_UNKNOWN;
8713
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008714 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 {
8716 /*
8717 * User defined function.
8718 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008719 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008720
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008722 /* Trigger FuncUndefined event, may load the function. */
8723 if (fp == NULL
8724 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008725 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008726 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008728 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008729 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 }
8731#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008732 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008733 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008734 {
8735 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008736 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008737 }
8738
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 if (fp != NULL)
8740 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008741 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008743 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008745 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008747 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008748 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 else
8750 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008751 int did_save_redo = FALSE;
8752
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 /*
8754 * Call the user function.
8755 * Save and restore search patterns, script variables and
8756 * redo buffer.
8757 */
8758 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008759#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008760 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008761#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008762 {
8763 saveRedobuff();
8764 did_save_redo = TRUE;
8765 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008766 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008767 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008768 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008769 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8770 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8771 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008772 /* Function was unreferenced while being used, free it
8773 * now. */
8774 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008775 if (did_save_redo)
8776 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 restore_search_patterns();
8778 error = ERROR_NONE;
8779 }
8780 }
8781 }
8782 else
8783 {
8784 /*
8785 * Find the function name in the table, call its implementation.
8786 */
8787 i = find_internal_func(fname);
8788 if (i >= 0)
8789 {
8790 if (argcount < functions[i].f_min_argc)
8791 error = ERROR_TOOFEW;
8792 else if (argcount > functions[i].f_max_argc)
8793 error = ERROR_TOOMANY;
8794 else
8795 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008796 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 error = ERROR_NONE;
8799 }
8800 }
8801 }
8802 /*
8803 * The function call (or "FuncUndefined" autocommand sequence) might
8804 * have been aborted by an error, an interrupt, or an explicitly thrown
8805 * exception that has not been caught so far. This situation can be
8806 * tested for by calling aborting(). For an error in an internal
8807 * function or for the "E132" error in call_user_func(), however, the
8808 * throw point at which the "force_abort" flag (temporarily reset by
8809 * emsg()) is normally updated has not been reached yet. We need to
8810 * update that flag first to make aborting() reliable.
8811 */
8812 update_force_abort();
8813 }
8814 if (error == ERROR_NONE)
8815 ret = OK;
8816
8817 /*
8818 * Report an error unless the argument evaluation or function call has been
8819 * cancelled due to an aborting error, an interrupt, or an exception.
8820 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008821 if (!aborting())
8822 {
8823 switch (error)
8824 {
8825 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008826 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008827 break;
8828 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008829 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008830 break;
8831 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008832 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008833 name);
8834 break;
8835 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008836 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008837 name);
8838 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008839 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008840 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008841 name);
8842 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008843 }
8844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 if (fname != name && fname != fname_buf)
8847 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008848 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849
8850 return ret;
8851}
8852
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008853/*
8854 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008855 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008856 */
8857 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008858emsg_funcname(ermsg, name)
8859 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008860 char_u *name;
8861{
8862 char_u *p;
8863
8864 if (*name == K_SPECIAL)
8865 p = concat_str((char_u *)"<SNR>", name + 3);
8866 else
8867 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008868 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008869 if (p != name)
8870 vim_free(p);
8871}
8872
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008873/*
8874 * Return TRUE for a non-zero Number and a non-empty String.
8875 */
8876 static int
8877non_zero_arg(argvars)
8878 typval_T *argvars;
8879{
8880 return ((argvars[0].v_type == VAR_NUMBER
8881 && argvars[0].vval.v_number != 0)
8882 || (argvars[0].v_type == VAR_STRING
8883 && argvars[0].vval.v_string != NULL
8884 && *argvars[0].vval.v_string != NUL));
8885}
8886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887/*********************************************
8888 * Implementation of the built-in functions
8889 */
8890
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008891#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008892static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8893
8894/*
8895 * Get the float value of "argvars[0]" into "f".
8896 * Returns FAIL when the argument is not a Number or Float.
8897 */
8898 static int
8899get_float_arg(argvars, f)
8900 typval_T *argvars;
8901 float_T *f;
8902{
8903 if (argvars[0].v_type == VAR_FLOAT)
8904 {
8905 *f = argvars[0].vval.v_float;
8906 return OK;
8907 }
8908 if (argvars[0].v_type == VAR_NUMBER)
8909 {
8910 *f = (float_T)argvars[0].vval.v_number;
8911 return OK;
8912 }
8913 EMSG(_("E808: Number or Float required"));
8914 return FAIL;
8915}
8916
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008917/*
8918 * "abs(expr)" function
8919 */
8920 static void
8921f_abs(argvars, rettv)
8922 typval_T *argvars;
8923 typval_T *rettv;
8924{
8925 if (argvars[0].v_type == VAR_FLOAT)
8926 {
8927 rettv->v_type = VAR_FLOAT;
8928 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8929 }
8930 else
8931 {
8932 varnumber_T n;
8933 int error = FALSE;
8934
8935 n = get_tv_number_chk(&argvars[0], &error);
8936 if (error)
8937 rettv->vval.v_number = -1;
8938 else if (n > 0)
8939 rettv->vval.v_number = n;
8940 else
8941 rettv->vval.v_number = -n;
8942 }
8943}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008944
8945/*
8946 * "acos()" function
8947 */
8948 static void
8949f_acos(argvars, rettv)
8950 typval_T *argvars;
8951 typval_T *rettv;
8952{
8953 float_T f;
8954
8955 rettv->v_type = VAR_FLOAT;
8956 if (get_float_arg(argvars, &f) == OK)
8957 rettv->vval.v_float = acos(f);
8958 else
8959 rettv->vval.v_float = 0.0;
8960}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008961#endif
8962
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008964 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 */
8966 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008967f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 typval_T *argvars;
8969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970{
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008974 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008976 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008977 && !tv_check_lock(l->lv_lock,
8978 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008979 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008981 }
8982 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008983 EMSG(_(e_listreq));
8984}
8985
8986/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008987 * "and(expr, expr)" function
8988 */
8989 static void
8990f_and(argvars, rettv)
8991 typval_T *argvars;
8992 typval_T *rettv;
8993{
8994 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8995 & get_tv_number_chk(&argvars[1], NULL);
8996}
8997
8998/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008999 * "append(lnum, string/list)" function
9000 */
9001 static void
9002f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 typval_T *argvars;
9004 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009005{
9006 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009007 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009008 list_T *l = NULL;
9009 listitem_T *li = NULL;
9010 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009011 long added = 0;
9012
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009013 /* When coming here from Insert mode, sync undo, so that this can be
9014 * undone separately from what was previously inserted. */
9015 if (u_sync_once == 2)
9016 {
9017 u_sync_once = 1; /* notify that u_sync() was called */
9018 u_sync(TRUE);
9019 }
9020
Bram Moolenaar0d660222005-01-07 21:51:51 +00009021 lnum = get_tv_lnum(argvars);
9022 if (lnum >= 0
9023 && lnum <= curbuf->b_ml.ml_line_count
9024 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009025 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009026 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009027 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009028 l = argvars[1].vval.v_list;
9029 if (l == NULL)
9030 return;
9031 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009032 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009033 for (;;)
9034 {
9035 if (l == NULL)
9036 tv = &argvars[1]; /* append a string */
9037 else if (li == NULL)
9038 break; /* end of list */
9039 else
9040 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009041 line = get_tv_string_chk(tv);
9042 if (line == NULL) /* type error */
9043 {
9044 rettv->vval.v_number = 1; /* Failed */
9045 break;
9046 }
9047 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009048 ++added;
9049 if (l == NULL)
9050 break;
9051 li = li->li_next;
9052 }
9053
9054 appended_lines_mark(lnum, added);
9055 if (curwin->w_cursor.lnum > lnum)
9056 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009058 else
9059 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060}
9061
9062/*
9063 * "argc()" function
9064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009067 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071}
9072
9073/*
9074 * "argidx()" function
9075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009078 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082}
9083
9084/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009085 * "arglistid()" function
9086 */
9087 static void
9088f_arglistid(argvars, rettv)
9089 typval_T *argvars UNUSED;
9090 typval_T *rettv;
9091{
9092 win_T *wp;
9093 tabpage_T *tp = NULL;
9094 long n;
9095
9096 rettv->vval.v_number = -1;
9097 if (argvars[0].v_type != VAR_UNKNOWN)
9098 {
9099 if (argvars[1].v_type != VAR_UNKNOWN)
9100 {
9101 n = get_tv_number(&argvars[1]);
9102 if (n >= 0)
9103 tp = find_tabpage(n);
9104 }
9105 else
9106 tp = curtab;
9107
9108 if (tp != NULL)
9109 {
9110 wp = find_win_by_nr(&argvars[0], tp);
9111 if (wp != NULL)
9112 rettv->vval.v_number = wp->w_alist->id;
9113 }
9114 }
9115 else
9116 rettv->vval.v_number = curwin->w_alist->id;
9117}
9118
9119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 * "argv(nr)" function
9121 */
9122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 typval_T *argvars;
9125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126{
9127 int idx;
9128
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009129 if (argvars[0].v_type != VAR_UNKNOWN)
9130 {
9131 idx = get_tv_number_chk(&argvars[0], NULL);
9132 if (idx >= 0 && idx < ARGCOUNT)
9133 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9134 else
9135 rettv->vval.v_string = NULL;
9136 rettv->v_type = VAR_STRING;
9137 }
9138 else if (rettv_list_alloc(rettv) == OK)
9139 for (idx = 0; idx < ARGCOUNT; ++idx)
9140 list_append_string(rettv->vval.v_list,
9141 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142}
9143
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009144static void prepare_assert_error __ARGS((garray_T*gap));
9145static void fill_assert_error __ARGS((garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv));
9146static void assert_error __ARGS((garray_T *gap));
9147static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9148
9149/*
9150 * Prepare "gap" for an assert error and add the sourcing position.
9151 */
9152 static void
9153prepare_assert_error(gap)
9154 garray_T *gap;
9155{
9156 char buf[NUMBUFLEN];
9157
9158 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009159 if (sourcing_name != NULL)
9160 {
9161 ga_concat(gap, sourcing_name);
9162 if (sourcing_lnum > 0)
9163 ga_concat(gap, (char_u *)" ");
9164 }
9165 if (sourcing_lnum > 0)
9166 {
9167 sprintf(buf, "line %ld", (long)sourcing_lnum);
9168 ga_concat(gap, (char_u *)buf);
9169 }
9170 if (sourcing_name != NULL || sourcing_lnum > 0)
9171 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009172}
9173
9174/*
9175 * Fill "gap" with information about an assert error.
9176 */
9177 static void
9178fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9179 garray_T *gap;
9180 typval_T *opt_msg_tv;
9181 char_u *exp_str;
9182 typval_T *exp_tv;
9183 typval_T *got_tv;
9184{
9185 char_u numbuf[NUMBUFLEN];
9186 char_u *tofree;
9187
9188 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9189 {
9190 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9191 vim_free(tofree);
9192 }
9193 else
9194 {
9195 ga_concat(gap, (char_u *)"Expected ");
9196 if (exp_str == NULL)
9197 {
9198 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9199 vim_free(tofree);
9200 }
9201 else
9202 ga_concat(gap, exp_str);
9203 ga_concat(gap, (char_u *)" but got ");
9204 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9205 vim_free(tofree);
9206 }
9207}
Bram Moolenaar43345542015-11-29 17:35:35 +01009208
9209/*
9210 * Add an assert error to v:errors.
9211 */
9212 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009213assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009214 garray_T *gap;
9215{
9216 struct vimvar *vp = &vimvars[VV_ERRORS];
9217
9218 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9219 /* Make sure v:errors is a list. */
9220 set_vim_var_list(VV_ERRORS, list_alloc());
9221 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9222}
9223
Bram Moolenaar43345542015-11-29 17:35:35 +01009224/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009225 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009226 */
9227 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009228f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009229 typval_T *argvars;
9230 typval_T *rettv UNUSED;
9231{
9232 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009233
9234 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9235 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009236 prepare_assert_error(&ga);
9237 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9238 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009239 ga_clear(&ga);
9240 }
9241}
9242
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009243/*
9244 * Common for assert_true() and assert_false().
9245 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009246 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009247assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009248 typval_T *argvars;
9249 int isTrue;
9250{
9251 int error = FALSE;
9252 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009253
9254 if (argvars[0].v_type != VAR_NUMBER
9255 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9256 || error)
9257 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009258 prepare_assert_error(&ga);
9259 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009260 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009261 NULL, &argvars[0]);
9262 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009263 ga_clear(&ga);
9264 }
9265}
9266
9267/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009268 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009269 */
9270 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009271f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009272 typval_T *argvars;
9273 typval_T *rettv UNUSED;
9274{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009275 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009276}
9277
9278/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009279 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009280 */
9281 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009282f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009283 typval_T *argvars;
9284 typval_T *rettv UNUSED;
9285{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009286 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009287}
9288
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009289#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009290/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009291 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009292 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009293 static void
9294f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009295 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009296 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009297{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009298 float_T f;
9299
9300 rettv->v_type = VAR_FLOAT;
9301 if (get_float_arg(argvars, &f) == OK)
9302 rettv->vval.v_float = asin(f);
9303 else
9304 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009305}
9306
9307/*
9308 * "atan()" function
9309 */
9310 static void
9311f_atan(argvars, rettv)
9312 typval_T *argvars;
9313 typval_T *rettv;
9314{
9315 float_T f;
9316
9317 rettv->v_type = VAR_FLOAT;
9318 if (get_float_arg(argvars, &f) == OK)
9319 rettv->vval.v_float = atan(f);
9320 else
9321 rettv->vval.v_float = 0.0;
9322}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009323
9324/*
9325 * "atan2()" function
9326 */
9327 static void
9328f_atan2(argvars, rettv)
9329 typval_T *argvars;
9330 typval_T *rettv;
9331{
9332 float_T fx, fy;
9333
9334 rettv->v_type = VAR_FLOAT;
9335 if (get_float_arg(argvars, &fx) == OK
9336 && get_float_arg(&argvars[1], &fy) == OK)
9337 rettv->vval.v_float = atan2(fx, fy);
9338 else
9339 rettv->vval.v_float = 0.0;
9340}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009341#endif
9342
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343/*
9344 * "browse(save, title, initdir, default)" function
9345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009347f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009348 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350{
9351#ifdef FEAT_BROWSE
9352 int save;
9353 char_u *title;
9354 char_u *initdir;
9355 char_u *defname;
9356 char_u buf[NUMBUFLEN];
9357 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009358 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 save = get_tv_number_chk(&argvars[0], &error);
9361 title = get_tv_string_chk(&argvars[1]);
9362 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9363 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009365 if (error || title == NULL || initdir == NULL || defname == NULL)
9366 rettv->vval.v_string = NULL;
9367 else
9368 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009369 do_browse(save ? BROWSE_SAVE : 0,
9370 title, defname, NULL, initdir, NULL, curbuf);
9371#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009373#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009375}
9376
9377/*
9378 * "browsedir(title, initdir)" function
9379 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009382 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009384{
9385#ifdef FEAT_BROWSE
9386 char_u *title;
9387 char_u *initdir;
9388 char_u buf[NUMBUFLEN];
9389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009390 title = get_tv_string_chk(&argvars[0]);
9391 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009392
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009393 if (title == NULL || initdir == NULL)
9394 rettv->vval.v_string = NULL;
9395 else
9396 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009397 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009401 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402}
9403
Bram Moolenaar33570922005-01-25 22:26:29 +00009404static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009405
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406/*
9407 * Find a buffer by number or exact name.
9408 */
9409 static buf_T *
9410find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009411 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412{
9413 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009415 if (avar->v_type == VAR_NUMBER)
9416 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009417 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009419 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009420 if (buf == NULL)
9421 {
9422 /* No full path name match, try a match with a URL or a "nofile"
9423 * buffer, these don't use the full path. */
9424 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9425 if (buf->b_fname != NULL
9426 && (path_with_url(buf->b_fname)
9427#ifdef FEAT_QUICKFIX
9428 || bt_nofile(buf)
9429#endif
9430 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009431 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009432 break;
9433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 }
9435 return buf;
9436}
9437
9438/*
9439 * "bufexists(expr)" function
9440 */
9441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009442f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009443 typval_T *argvars;
9444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009446 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447}
9448
9449/*
9450 * "buflisted(expr)" function
9451 */
9452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009453f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009454 typval_T *argvars;
9455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456{
9457 buf_T *buf;
9458
9459 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461}
9462
9463/*
9464 * "bufloaded(expr)" function
9465 */
9466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009468 typval_T *argvars;
9469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 buf_T *buf;
9472
9473 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475}
9476
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009477static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009478
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479/*
9480 * Get buffer by number or pattern.
9481 */
9482 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009483get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009484 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009485 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 int save_magic;
9489 char_u *save_cpo;
9490 buf_T *buf;
9491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492 if (tv->v_type == VAR_NUMBER)
9493 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009494 if (tv->v_type != VAR_STRING)
9495 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 if (name == NULL || *name == NUL)
9497 return curbuf;
9498 if (name[0] == '$' && name[1] == NUL)
9499 return lastbuf;
9500
9501 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9502 save_magic = p_magic;
9503 p_magic = TRUE;
9504 save_cpo = p_cpo;
9505 p_cpo = (char_u *)"";
9506
9507 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009508 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509
9510 p_magic = save_magic;
9511 p_cpo = save_cpo;
9512
9513 /* If not found, try expanding the name, like done for bufexists(). */
9514 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516
9517 return buf;
9518}
9519
9520/*
9521 * "bufname(expr)" function
9522 */
9523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009524f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009525 typval_T *argvars;
9526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527{
9528 buf_T *buf;
9529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009532 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 --emsg_off;
9539}
9540
9541/*
9542 * "bufnr(expr)" function
9543 */
9544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009546 typval_T *argvars;
9547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
9549 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009550 int error = FALSE;
9551 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009553 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009555 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009556 --emsg_off;
9557
9558 /* If the buffer isn't found and the second argument is not zero create a
9559 * new buffer. */
9560 if (buf == NULL
9561 && argvars[1].v_type != VAR_UNKNOWN
9562 && get_tv_number_chk(&argvars[1], &error) != 0
9563 && !error
9564 && (name = get_tv_string_chk(&argvars[0])) != NULL
9565 && !error)
9566 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9567
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572}
9573
9574/*
9575 * "bufwinnr(nr)" function
9576 */
9577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009579 typval_T *argvars;
9580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
9582#ifdef FEAT_WINDOWS
9583 win_T *wp;
9584 int winnr = 0;
9585#endif
9586 buf_T *buf;
9587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009590 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591#ifdef FEAT_WINDOWS
9592 for (wp = firstwin; wp; wp = wp->w_next)
9593 {
9594 ++winnr;
9595 if (wp->w_buffer == buf)
9596 break;
9597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601#endif
9602 --emsg_off;
9603}
9604
9605/*
9606 * "byte2line(byte)" function
9607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009610 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612{
9613#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615#else
9616 long boff = 0;
9617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009618 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 (linenr_T)0, &boff);
9624#endif
9625}
9626
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009627 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009628byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009629 typval_T *argvars;
9630 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009631 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009632{
9633#ifdef FEAT_MBYTE
9634 char_u *t;
9635#endif
9636 char_u *str;
9637 long idx;
9638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 str = get_tv_string_chk(&argvars[0]);
9640 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009642 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009643 return;
9644
9645#ifdef FEAT_MBYTE
9646 t = str;
9647 for ( ; idx > 0; idx--)
9648 {
9649 if (*t == NUL) /* EOL reached */
9650 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009651 if (enc_utf8 && comp)
9652 t += utf_ptr2len(t);
9653 else
9654 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009655 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009656 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009657#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009658 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009660#endif
9661}
9662
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009663/*
9664 * "byteidx()" function
9665 */
9666 static void
9667f_byteidx(argvars, rettv)
9668 typval_T *argvars;
9669 typval_T *rettv;
9670{
9671 byteidx(argvars, rettv, FALSE);
9672}
9673
9674/*
9675 * "byteidxcomp()" function
9676 */
9677 static void
9678f_byteidxcomp(argvars, rettv)
9679 typval_T *argvars;
9680 typval_T *rettv;
9681{
9682 byteidx(argvars, rettv, TRUE);
9683}
9684
Bram Moolenaardb913952012-06-29 12:54:53 +02009685 int
9686func_call(name, args, selfdict, rettv)
9687 char_u *name;
9688 typval_T *args;
9689 dict_T *selfdict;
9690 typval_T *rettv;
9691{
9692 listitem_T *item;
9693 typval_T argv[MAX_FUNC_ARGS + 1];
9694 int argc = 0;
9695 int dummy;
9696 int r = 0;
9697
9698 for (item = args->vval.v_list->lv_first; item != NULL;
9699 item = item->li_next)
9700 {
9701 if (argc == MAX_FUNC_ARGS)
9702 {
9703 EMSG(_("E699: Too many arguments"));
9704 break;
9705 }
9706 /* Make a copy of each argument. This is needed to be able to set
9707 * v_lock to VAR_FIXED in the copy without changing the original list.
9708 */
9709 copy_tv(&item->li_tv, &argv[argc++]);
9710 }
9711
9712 if (item == NULL)
9713 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9714 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9715 &dummy, TRUE, selfdict);
9716
9717 /* Free the arguments. */
9718 while (argc > 0)
9719 clear_tv(&argv[--argc]);
9720
9721 return r;
9722}
9723
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009724/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009725 * "call(func, arglist)" function
9726 */
9727 static void
9728f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009729 typval_T *argvars;
9730 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009731{
9732 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009733 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009734
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009735 if (argvars[1].v_type != VAR_LIST)
9736 {
9737 EMSG(_(e_listreq));
9738 return;
9739 }
9740 if (argvars[1].vval.v_list == NULL)
9741 return;
9742
9743 if (argvars[0].v_type == VAR_FUNC)
9744 func = argvars[0].vval.v_string;
9745 else
9746 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009747 if (*func == NUL)
9748 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009749
Bram Moolenaare9a41262005-01-15 22:18:47 +00009750 if (argvars[2].v_type != VAR_UNKNOWN)
9751 {
9752 if (argvars[2].v_type != VAR_DICT)
9753 {
9754 EMSG(_(e_dictreq));
9755 return;
9756 }
9757 selfdict = argvars[2].vval.v_dict;
9758 }
9759
Bram Moolenaardb913952012-06-29 12:54:53 +02009760 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009761}
9762
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009763#ifdef FEAT_FLOAT
9764/*
9765 * "ceil({float})" function
9766 */
9767 static void
9768f_ceil(argvars, rettv)
9769 typval_T *argvars;
9770 typval_T *rettv;
9771{
9772 float_T f;
9773
9774 rettv->v_type = VAR_FLOAT;
9775 if (get_float_arg(argvars, &f) == OK)
9776 rettv->vval.v_float = ceil(f);
9777 else
9778 rettv->vval.v_float = 0.0;
9779}
9780#endif
9781
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009782/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009783 * "changenr()" function
9784 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009785 static void
9786f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009787 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009788 typval_T *rettv;
9789{
9790 rettv->vval.v_number = curbuf->b_u_seq_cur;
9791}
9792
9793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 * "char2nr(string)" function
9795 */
9796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009798 typval_T *argvars;
9799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
9801#ifdef FEAT_MBYTE
9802 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009803 {
9804 int utf8 = 0;
9805
9806 if (argvars[1].v_type != VAR_UNKNOWN)
9807 utf8 = get_tv_number_chk(&argvars[1], NULL);
9808
9809 if (utf8)
9810 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9811 else
9812 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 else
9815#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817}
9818
9819/*
9820 * "cindent(lnum)" function
9821 */
9822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009824 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826{
9827#ifdef FEAT_CINDENT
9828 pos_T pos;
9829 linenr_T lnum;
9830
9831 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9834 {
9835 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 curwin->w_cursor = pos;
9838 }
9839 else
9840#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842}
9843
9844/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009845 * "clearmatches()" function
9846 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009847 static void
9848f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009849 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009850 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009851{
9852#ifdef FEAT_SEARCH_EXTRA
9853 clear_matches(curwin);
9854#endif
9855}
9856
9857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 * "col(string)" function
9859 */
9860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009861f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009862 typval_T *argvars;
9863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864{
9865 colnr_T col = 0;
9866 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009867 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009869 fp = var2fpos(&argvars[0], FALSE, &fnum);
9870 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 {
9872 if (fp->col == MAXCOL)
9873 {
9874 /* '> can be MAXCOL, get the length of the line then */
9875 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009876 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 else
9878 col = MAXCOL;
9879 }
9880 else
9881 {
9882 col = fp->col + 1;
9883#ifdef FEAT_VIRTUALEDIT
9884 /* col(".") when the cursor is on the NUL at the end of the line
9885 * because of "coladd" can be seen as an extra column. */
9886 if (virtual_active() && fp == &curwin->w_cursor)
9887 {
9888 char_u *p = ml_get_cursor();
9889
9890 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9891 curwin->w_virtcol - curwin->w_cursor.coladd))
9892 {
9893# ifdef FEAT_MBYTE
9894 int l;
9895
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009896 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 col += l;
9898# else
9899 if (*p != NUL && p[1] == NUL)
9900 ++col;
9901# endif
9902 }
9903 }
9904#endif
9905 }
9906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908}
9909
Bram Moolenaar572cb562005-08-05 21:35:02 +00009910#if defined(FEAT_INS_EXPAND)
9911/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009912 * "complete()" function
9913 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009914 static void
9915f_complete(argvars, rettv)
9916 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009917 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009918{
9919 int startcol;
9920
9921 if ((State & INSERT) == 0)
9922 {
9923 EMSG(_("E785: complete() can only be used in Insert mode"));
9924 return;
9925 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009926
9927 /* Check for undo allowed here, because if something was already inserted
9928 * the line was already saved for undo and this check isn't done. */
9929 if (!undo_allowed())
9930 return;
9931
Bram Moolenaarade00832006-03-10 21:46:58 +00009932 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9933 {
9934 EMSG(_(e_invarg));
9935 return;
9936 }
9937
9938 startcol = get_tv_number_chk(&argvars[0], NULL);
9939 if (startcol <= 0)
9940 return;
9941
9942 set_completion(startcol - 1, argvars[1].vval.v_list);
9943}
9944
9945/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009946 * "complete_add()" function
9947 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009948 static void
9949f_complete_add(argvars, rettv)
9950 typval_T *argvars;
9951 typval_T *rettv;
9952{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009953 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009954}
9955
9956/*
9957 * "complete_check()" function
9958 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009959 static void
9960f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009961 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009962 typval_T *rettv;
9963{
9964 int saved = RedrawingDisabled;
9965
9966 RedrawingDisabled = 0;
9967 ins_compl_check_keys(0);
9968 rettv->vval.v_number = compl_interrupted;
9969 RedrawingDisabled = saved;
9970}
9971#endif
9972
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973/*
9974 * "confirm(message, buttons[, default [, type]])" function
9975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009978 typval_T *argvars UNUSED;
9979 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980{
9981#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9982 char_u *message;
9983 char_u *buttons = NULL;
9984 char_u buf[NUMBUFLEN];
9985 char_u buf2[NUMBUFLEN];
9986 int def = 1;
9987 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009988 char_u *typestr;
9989 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009991 message = get_tv_string_chk(&argvars[0]);
9992 if (message == NULL)
9993 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009994 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009996 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9997 if (buttons == NULL)
9998 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009999 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010001 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010002 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010004 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10005 if (typestr == NULL)
10006 error = TRUE;
10007 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010009 switch (TOUPPER_ASC(*typestr))
10010 {
10011 case 'E': type = VIM_ERROR; break;
10012 case 'Q': type = VIM_QUESTION; break;
10013 case 'I': type = VIM_INFO; break;
10014 case 'W': type = VIM_WARNING; break;
10015 case 'G': type = VIM_GENERIC; break;
10016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 }
10018 }
10019 }
10020 }
10021
10022 if (buttons == NULL || *buttons == NUL)
10023 buttons = (char_u *)_("&Ok");
10024
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010025 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010026 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010027 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028#endif
10029}
10030
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010031/*
10032 * "copy()" function
10033 */
10034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010035f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010036 typval_T *argvars;
10037 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010038{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010039 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010040}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010042#ifdef FEAT_FLOAT
10043/*
10044 * "cos()" function
10045 */
10046 static void
10047f_cos(argvars, rettv)
10048 typval_T *argvars;
10049 typval_T *rettv;
10050{
10051 float_T f;
10052
10053 rettv->v_type = VAR_FLOAT;
10054 if (get_float_arg(argvars, &f) == OK)
10055 rettv->vval.v_float = cos(f);
10056 else
10057 rettv->vval.v_float = 0.0;
10058}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010059
10060/*
10061 * "cosh()" function
10062 */
10063 static void
10064f_cosh(argvars, rettv)
10065 typval_T *argvars;
10066 typval_T *rettv;
10067{
10068 float_T f;
10069
10070 rettv->v_type = VAR_FLOAT;
10071 if (get_float_arg(argvars, &f) == OK)
10072 rettv->vval.v_float = cosh(f);
10073 else
10074 rettv->vval.v_float = 0.0;
10075}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010076#endif
10077
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010079 * "count()" function
10080 */
10081 static void
10082f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010083 typval_T *argvars;
10084 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010085{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010086 long n = 0;
10087 int ic = FALSE;
10088
Bram Moolenaare9a41262005-01-15 22:18:47 +000010089 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010090 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010091 listitem_T *li;
10092 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010093 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010094
Bram Moolenaare9a41262005-01-15 22:18:47 +000010095 if ((l = argvars[0].vval.v_list) != NULL)
10096 {
10097 li = l->lv_first;
10098 if (argvars[2].v_type != VAR_UNKNOWN)
10099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100 int error = FALSE;
10101
10102 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010103 if (argvars[3].v_type != VAR_UNKNOWN)
10104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010105 idx = get_tv_number_chk(&argvars[3], &error);
10106 if (!error)
10107 {
10108 li = list_find(l, idx);
10109 if (li == NULL)
10110 EMSGN(_(e_listidx), idx);
10111 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010112 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 if (error)
10114 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010115 }
10116
10117 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010118 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010119 ++n;
10120 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010121 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010122 else if (argvars[0].v_type == VAR_DICT)
10123 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010124 int todo;
10125 dict_T *d;
10126 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010127
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010128 if ((d = argvars[0].vval.v_dict) != NULL)
10129 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010130 int error = FALSE;
10131
Bram Moolenaare9a41262005-01-15 22:18:47 +000010132 if (argvars[2].v_type != VAR_UNKNOWN)
10133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010134 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010135 if (argvars[3].v_type != VAR_UNKNOWN)
10136 EMSG(_(e_invarg));
10137 }
10138
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010139 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010140 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010141 {
10142 if (!HASHITEM_EMPTY(hi))
10143 {
10144 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010145 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010146 ++n;
10147 }
10148 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010149 }
10150 }
10151 else
10152 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010153 rettv->vval.v_number = n;
10154}
10155
10156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10158 *
10159 * Checks the existence of a cscope connection.
10160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010163 typval_T *argvars UNUSED;
10164 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165{
10166#ifdef FEAT_CSCOPE
10167 int num = 0;
10168 char_u *dbpath = NULL;
10169 char_u *prepend = NULL;
10170 char_u buf[NUMBUFLEN];
10171
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010172 if (argvars[0].v_type != VAR_UNKNOWN
10173 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010175 num = (int)get_tv_number(&argvars[0]);
10176 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010177 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 }
10180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182#endif
10183}
10184
10185/*
10186 * "cursor(lnum, col)" function
10187 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010188 * Moves the cursor to the specified line and column.
10189 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010192f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010193 typval_T *argvars;
10194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195{
10196 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010197#ifdef FEAT_VIRTUALEDIT
10198 long coladd = 0;
10199#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010200 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010202 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010203 if (argvars[1].v_type == VAR_UNKNOWN)
10204 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010205 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010206 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010207
Bram Moolenaar493c1782014-05-28 14:34:46 +020010208 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010209 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010210 line = pos.lnum;
10211 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010212#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010213 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010214#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010215 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010216 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010217 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010218 set_curswant = FALSE;
10219 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010220 }
10221 else
10222 {
10223 line = get_tv_lnum(argvars);
10224 col = get_tv_number_chk(&argvars[1], NULL);
10225#ifdef FEAT_VIRTUALEDIT
10226 if (argvars[2].v_type != VAR_UNKNOWN)
10227 coladd = get_tv_number_chk(&argvars[2], NULL);
10228#endif
10229 }
10230 if (line < 0 || col < 0
10231#ifdef FEAT_VIRTUALEDIT
10232 || coladd < 0
10233#endif
10234 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 if (line > 0)
10237 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 if (col > 0)
10239 curwin->w_cursor.col = col - 1;
10240#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010241 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242#endif
10243
10244 /* Make sure the cursor is in a valid position. */
10245 check_cursor();
10246#ifdef FEAT_MBYTE
10247 /* Correct cursor for multi-byte character. */
10248 if (has_mbyte)
10249 mb_adjust_cursor();
10250#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010251
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010252 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010253 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254}
10255
10256/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010257 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 */
10259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010260f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010261 typval_T *argvars;
10262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010264 int noref = 0;
10265
10266 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010268 if (noref < 0 || noref > 1)
10269 EMSG(_(e_invarg));
10270 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010271 {
10272 current_copyID += COPYID_INC;
10273 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275}
10276
10277/*
10278 * "delete()" function
10279 */
10280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010281f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010282 typval_T *argvars;
10283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284{
10285 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010286 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010288 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289}
10290
10291/*
10292 * "did_filetype()" function
10293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010296 typval_T *argvars UNUSED;
10297 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
10299#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010300 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301#endif
10302}
10303
10304/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010305 * "diff_filler()" function
10306 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010309 typval_T *argvars UNUSED;
10310 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010311{
10312#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010314#endif
10315}
10316
10317/*
10318 * "diff_hlID()" function
10319 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010321f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010322 typval_T *argvars UNUSED;
10323 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010324{
10325#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010327 static linenr_T prev_lnum = 0;
10328 static int changedtick = 0;
10329 static int fnum = 0;
10330 static int change_start = 0;
10331 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010332 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010333 int filler_lines;
10334 int col;
10335
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010336 if (lnum < 0) /* ignore type error in {lnum} arg */
10337 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010338 if (lnum != prev_lnum
10339 || changedtick != curbuf->b_changedtick
10340 || fnum != curbuf->b_fnum)
10341 {
10342 /* New line, buffer, change: need to get the values. */
10343 filler_lines = diff_check(curwin, lnum);
10344 if (filler_lines < 0)
10345 {
10346 if (filler_lines == -1)
10347 {
10348 change_start = MAXCOL;
10349 change_end = -1;
10350 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10351 hlID = HLF_ADD; /* added line */
10352 else
10353 hlID = HLF_CHD; /* changed line */
10354 }
10355 else
10356 hlID = HLF_ADD; /* added line */
10357 }
10358 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010359 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010360 prev_lnum = lnum;
10361 changedtick = curbuf->b_changedtick;
10362 fnum = curbuf->b_fnum;
10363 }
10364
10365 if (hlID == HLF_CHD || hlID == HLF_TXD)
10366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010367 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010368 if (col >= change_start && col <= change_end)
10369 hlID = HLF_TXD; /* changed text */
10370 else
10371 hlID = HLF_CHD; /* changed line */
10372 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010373 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010374#endif
10375}
10376
10377/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010378 * "empty({expr})" function
10379 */
10380 static void
10381f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010382 typval_T *argvars;
10383 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010384{
10385 int n;
10386
10387 switch (argvars[0].v_type)
10388 {
10389 case VAR_STRING:
10390 case VAR_FUNC:
10391 n = argvars[0].vval.v_string == NULL
10392 || *argvars[0].vval.v_string == NUL;
10393 break;
10394 case VAR_NUMBER:
10395 n = argvars[0].vval.v_number == 0;
10396 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010397#ifdef FEAT_FLOAT
10398 case VAR_FLOAT:
10399 n = argvars[0].vval.v_float == 0.0;
10400 break;
10401#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010402 case VAR_LIST:
10403 n = argvars[0].vval.v_list == NULL
10404 || argvars[0].vval.v_list->lv_first == NULL;
10405 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010406 case VAR_DICT:
10407 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010408 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010409 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010410 default:
10411 EMSG2(_(e_intern2), "f_empty()");
10412 n = 0;
10413 }
10414
10415 rettv->vval.v_number = n;
10416}
10417
10418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 * "escape({string}, {chars})" function
10420 */
10421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010422f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010423 typval_T *argvars;
10424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425{
10426 char_u buf[NUMBUFLEN];
10427
Bram Moolenaar758711c2005-02-02 23:11:38 +000010428 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10429 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431}
10432
10433/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010434 * "eval()" function
10435 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010436 static void
10437f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010438 typval_T *argvars;
10439 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010440{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010441 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010443 s = get_tv_string_chk(&argvars[0]);
10444 if (s != NULL)
10445 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010446
Bram Moolenaar615b9972015-01-14 17:15:05 +010010447 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10449 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010450 if (p != NULL && !aborting())
10451 EMSG2(_(e_invexpr2), p);
10452 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010454 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010455 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010456 else if (*s != NUL)
10457 EMSG(_(e_trailing));
10458}
10459
10460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 * "eventhandler()" function
10462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010465 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010468 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469}
10470
10471/*
10472 * "executable()" function
10473 */
10474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010476 typval_T *argvars;
10477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010479 char_u *name = get_tv_string(&argvars[0]);
10480
10481 /* Check in $PATH and also check directly if there is a directory name. */
10482 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10483 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010484}
10485
10486/*
10487 * "exepath()" function
10488 */
10489 static void
10490f_exepath(argvars, rettv)
10491 typval_T *argvars;
10492 typval_T *rettv;
10493{
10494 char_u *p = NULL;
10495
Bram Moolenaarb5971142015-03-21 17:32:19 +010010496 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010497 rettv->v_type = VAR_STRING;
10498 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499}
10500
10501/*
10502 * "exists()" function
10503 */
10504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010505f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010506 typval_T *argvars;
10507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 char_u *p;
10510 char_u *name;
10511 int n = FALSE;
10512 int len = 0;
10513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 if (*p == '$') /* environment variable */
10516 {
10517 /* first try "normal" environment variables (fast) */
10518 if (mch_getenv(p + 1) != NULL)
10519 n = TRUE;
10520 else
10521 {
10522 /* try expanding things like $VIM and ${HOME} */
10523 p = expand_env_save(p);
10524 if (p != NULL && *p != '$')
10525 n = TRUE;
10526 vim_free(p);
10527 }
10528 }
10529 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010532 if (*skipwhite(p) != NUL)
10533 n = FALSE; /* trailing garbage */
10534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 else if (*p == '*') /* internal or user defined function */
10536 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010537 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 }
10539 else if (*p == ':')
10540 {
10541 n = cmd_exists(p + 1);
10542 }
10543 else if (*p == '#')
10544 {
10545#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010546 if (p[1] == '#')
10547 n = autocmd_supported(p + 2);
10548 else
10549 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550#endif
10551 }
10552 else /* internal variable */
10553 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010554 char_u *tofree;
10555 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010557 /* get_name_len() takes care of expanding curly braces */
10558 name = p;
10559 len = get_name_len(&p, &tofree, TRUE, FALSE);
10560 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010562 if (tofree != NULL)
10563 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010564 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010565 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010567 /* handle d.key, l[idx], f(expr) */
10568 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10569 if (n)
10570 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 }
10572 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010573 if (*p != NUL)
10574 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010576 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 }
10578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010579 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580}
10581
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010582#ifdef FEAT_FLOAT
10583/*
10584 * "exp()" function
10585 */
10586 static void
10587f_exp(argvars, rettv)
10588 typval_T *argvars;
10589 typval_T *rettv;
10590{
10591 float_T f;
10592
10593 rettv->v_type = VAR_FLOAT;
10594 if (get_float_arg(argvars, &f) == OK)
10595 rettv->vval.v_float = exp(f);
10596 else
10597 rettv->vval.v_float = 0.0;
10598}
10599#endif
10600
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601/*
10602 * "expand()" function
10603 */
10604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010605f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010606 typval_T *argvars;
10607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608{
10609 char_u *s;
10610 int len;
10611 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010612 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010615 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010617 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010618 if (argvars[1].v_type != VAR_UNKNOWN
10619 && argvars[2].v_type != VAR_UNKNOWN
10620 && get_tv_number_chk(&argvars[2], &error)
10621 && !error)
10622 {
10623 rettv->v_type = VAR_LIST;
10624 rettv->vval.v_list = NULL;
10625 }
10626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010627 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 if (*s == '%' || *s == '#' || *s == '<')
10629 {
10630 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010631 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010633 if (rettv->v_type == VAR_LIST)
10634 {
10635 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10636 list_append_string(rettv->vval.v_list, result, -1);
10637 else
10638 vim_free(result);
10639 }
10640 else
10641 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 }
10643 else
10644 {
10645 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010646 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010647 if (argvars[1].v_type != VAR_UNKNOWN
10648 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010649 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010650 if (!error)
10651 {
10652 ExpandInit(&xpc);
10653 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010654 if (p_wic)
10655 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010656 if (rettv->v_type == VAR_STRING)
10657 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10658 options, WILD_ALL);
10659 else if (rettv_list_alloc(rettv) != FAIL)
10660 {
10661 int i;
10662
10663 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10664 for (i = 0; i < xpc.xp_numfiles; i++)
10665 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10666 ExpandCleanup(&xpc);
10667 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010668 }
10669 else
10670 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 }
10672}
10673
10674/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010675 * Go over all entries in "d2" and add them to "d1".
10676 * When "action" is "error" then a duplicate key is an error.
10677 * When "action" is "force" then a duplicate key is overwritten.
10678 * Otherwise duplicate keys are ignored ("action" is "keep").
10679 */
10680 void
10681dict_extend(d1, d2, action)
10682 dict_T *d1;
10683 dict_T *d2;
10684 char_u *action;
10685{
10686 dictitem_T *di1;
10687 hashitem_T *hi2;
10688 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010689 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010690
10691 todo = (int)d2->dv_hashtab.ht_used;
10692 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10693 {
10694 if (!HASHITEM_EMPTY(hi2))
10695 {
10696 --todo;
10697 di1 = dict_find(d1, hi2->hi_key, -1);
10698 if (d1->dv_scope != 0)
10699 {
10700 /* Disallow replacing a builtin function in l: and g:.
10701 * Check the key to be valid when adding to any
10702 * scope. */
10703 if (d1->dv_scope == VAR_DEF_SCOPE
10704 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10705 && var_check_func_name(hi2->hi_key,
10706 di1 == NULL))
10707 break;
10708 if (!valid_varname(hi2->hi_key))
10709 break;
10710 }
10711 if (di1 == NULL)
10712 {
10713 di1 = dictitem_copy(HI2DI(hi2));
10714 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10715 dictitem_free(di1);
10716 }
10717 else if (*action == 'e')
10718 {
10719 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10720 break;
10721 }
10722 else if (*action == 'f' && HI2DI(hi2) != di1)
10723 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010724 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10725 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010726 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010727 clear_tv(&di1->di_tv);
10728 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10729 }
10730 }
10731 }
10732}
10733
10734/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010735 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010736 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010737 */
10738 static void
10739f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010740 typval_T *argvars;
10741 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010742{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010743 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010744
Bram Moolenaare9a41262005-01-15 22:18:47 +000010745 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010746 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010747 list_T *l1, *l2;
10748 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010749 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010750 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010751
Bram Moolenaare9a41262005-01-15 22:18:47 +000010752 l1 = argvars[0].vval.v_list;
10753 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010754 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010755 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010756 {
10757 if (argvars[2].v_type != VAR_UNKNOWN)
10758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010759 before = get_tv_number_chk(&argvars[2], &error);
10760 if (error)
10761 return; /* type error; errmsg already given */
10762
Bram Moolenaar758711c2005-02-02 23:11:38 +000010763 if (before == l1->lv_len)
10764 item = NULL;
10765 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010766 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010767 item = list_find(l1, before);
10768 if (item == NULL)
10769 {
10770 EMSGN(_(e_listidx), before);
10771 return;
10772 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010773 }
10774 }
10775 else
10776 item = NULL;
10777 list_extend(l1, l2, item);
10778
Bram Moolenaare9a41262005-01-15 22:18:47 +000010779 copy_tv(&argvars[0], rettv);
10780 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010781 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010782 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10783 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010784 dict_T *d1, *d2;
10785 char_u *action;
10786 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787
10788 d1 = argvars[0].vval.v_dict;
10789 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010790 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010791 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010792 {
10793 /* Check the third argument. */
10794 if (argvars[2].v_type != VAR_UNKNOWN)
10795 {
10796 static char *(av[]) = {"keep", "force", "error"};
10797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 action = get_tv_string_chk(&argvars[2]);
10799 if (action == NULL)
10800 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010801 for (i = 0; i < 3; ++i)
10802 if (STRCMP(action, av[i]) == 0)
10803 break;
10804 if (i == 3)
10805 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010806 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010807 return;
10808 }
10809 }
10810 else
10811 action = (char_u *)"force";
10812
Bram Moolenaara9922d62013-05-30 13:01:18 +020010813 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010814
Bram Moolenaare9a41262005-01-15 22:18:47 +000010815 copy_tv(&argvars[0], rettv);
10816 }
10817 }
10818 else
10819 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010820}
10821
10822/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010823 * "feedkeys()" function
10824 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010825 static void
10826f_feedkeys(argvars, rettv)
10827 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010828 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010829{
10830 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010831 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010832 char_u *keys, *flags;
10833 char_u nbuf[NUMBUFLEN];
10834 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010835 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010836
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010837 /* This is not allowed in the sandbox. If the commands would still be
10838 * executed in the sandbox it would be OK, but it probably happens later,
10839 * when "sandbox" is no longer set. */
10840 if (check_secure())
10841 return;
10842
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010843 keys = get_tv_string(&argvars[0]);
10844 if (*keys != NUL)
10845 {
10846 if (argvars[1].v_type != VAR_UNKNOWN)
10847 {
10848 flags = get_tv_string_buf(&argvars[1], nbuf);
10849 for ( ; *flags != NUL; ++flags)
10850 {
10851 switch (*flags)
10852 {
10853 case 'n': remap = FALSE; break;
10854 case 'm': remap = TRUE; break;
10855 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010856 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010857 }
10858 }
10859 }
10860
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010861 /* Need to escape K_SPECIAL and CSI before putting the string in the
10862 * typeahead buffer. */
10863 keys_esc = vim_strsave_escape_csi(keys);
10864 if (keys_esc != NULL)
10865 {
10866 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010867 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010868 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010869 if (vgetc_busy)
10870 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010871 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010872 }
10873}
10874
10875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 * "filereadable()" function
10877 */
10878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010880 typval_T *argvars;
10881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010883 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 char_u *p;
10885 int n;
10886
Bram Moolenaarc236c162008-07-13 17:41:49 +000010887#ifndef O_NONBLOCK
10888# define O_NONBLOCK 0
10889#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010890 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010891 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10892 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 {
10894 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010895 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 }
10897 else
10898 n = FALSE;
10899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010900 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901}
10902
10903/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010904 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 * rights to write into.
10906 */
10907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010909 typval_T *argvars;
10910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010912 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913}
10914
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010915static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010916
10917 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010918findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010919 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010920 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010921 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010922{
10923#ifdef FEAT_SEARCHPATH
10924 char_u *fname;
10925 char_u *fresult = NULL;
10926 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10927 char_u *p;
10928 char_u pathbuf[NUMBUFLEN];
10929 int count = 1;
10930 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010931 int error = FALSE;
10932#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010933
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010934 rettv->vval.v_string = NULL;
10935 rettv->v_type = VAR_STRING;
10936
10937#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010939
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010940 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10943 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010944 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010945 else
10946 {
10947 if (*p != NUL)
10948 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010951 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010952 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010953 }
10954
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010955 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10956 error = TRUE;
10957
10958 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010959 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010960 do
10961 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010962 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010963 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010964 fresult = find_file_in_path_option(first ? fname : NULL,
10965 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010966 0, first, path,
10967 find_what,
10968 curbuf->b_ffname,
10969 find_what == FINDFILE_DIR
10970 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010971 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010972
10973 if (fresult != NULL && rettv->v_type == VAR_LIST)
10974 list_append_string(rettv->vval.v_list, fresult, -1);
10975
10976 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010977 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010978
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010979 if (rettv->v_type == VAR_STRING)
10980 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010981#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010982}
10983
Bram Moolenaar33570922005-01-25 22:26:29 +000010984static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10985static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010986
10987/*
10988 * Implementation of map() and filter().
10989 */
10990 static void
10991filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010992 typval_T *argvars;
10993 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010994 int map;
10995{
10996 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010997 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010998 listitem_T *li, *nli;
10999 list_T *l = NULL;
11000 dictitem_T *di;
11001 hashtab_T *ht;
11002 hashitem_T *hi;
11003 dict_T *d = NULL;
11004 typval_T save_val;
11005 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011007 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011008 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011009 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011010 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011011 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011012 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011013
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011015 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011016 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011017 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011018 return;
11019 }
11020 else if (argvars[0].v_type == VAR_DICT)
11021 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011022 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011023 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011024 return;
11025 }
11026 else
11027 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011028 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011029 return;
11030 }
11031
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032 expr = get_tv_string_buf_chk(&argvars[1], buf);
11033 /* On type errors, the preceding call has already displayed an error
11034 * message. Avoid a misleading error message for an empty string that
11035 * was not passed as argument. */
11036 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011038 prepare_vimvar(VV_VAL, &save_val);
11039 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011040
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011041 /* We reset "did_emsg" to be able to detect whether an error
11042 * occurred during evaluation of the expression. */
11043 save_did_emsg = did_emsg;
11044 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011045
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011046 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011049 vimvars[VV_KEY].vv_type = VAR_STRING;
11050
11051 ht = &d->dv_hashtab;
11052 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011053 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011054 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 if (!HASHITEM_EMPTY(hi))
11057 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011058 int r;
11059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011060 --todo;
11061 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011062 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011063 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11064 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065 break;
11066 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011067 r = filter_map_one(&di->di_tv, expr, map, &rem);
11068 clear_tv(&vimvars[VV_KEY].vv_tv);
11069 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 break;
11071 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011072 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011073 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11074 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011075 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011077 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 }
11079 }
11080 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 }
11082 else
11083 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011084 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011086 for (li = l->lv_first; li != NULL; li = nli)
11087 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011088 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011089 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011090 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011091 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011092 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011093 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011094 break;
11095 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011097 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011098 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011099 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011100
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011101 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011103
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011104 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011105 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011106
11107 copy_tv(&argvars[0], rettv);
11108}
11109
11110 static int
11111filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011112 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011113 char_u *expr;
11114 int map;
11115 int *remp;
11116{
Bram Moolenaar33570922005-01-25 22:26:29 +000011117 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011118 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011119 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011120
Bram Moolenaar33570922005-01-25 22:26:29 +000011121 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011122 s = expr;
11123 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011124 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125 if (*s != NUL) /* check for trailing chars after expr */
11126 {
11127 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011128 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011129 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 }
11131 if (map)
11132 {
11133 /* map(): replace the list item value */
11134 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011135 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011136 *tv = rettv;
11137 }
11138 else
11139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140 int error = FALSE;
11141
Bram Moolenaare9a41262005-01-15 22:18:47 +000011142 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011143 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011144 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 /* On type error, nothing has been removed; return FAIL to stop the
11146 * loop. The error message was given by get_tv_number_chk(). */
11147 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011148 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011149 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011150 retval = OK;
11151theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011152 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011153 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011154}
11155
11156/*
11157 * "filter()" function
11158 */
11159 static void
11160f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011161 typval_T *argvars;
11162 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011163{
11164 filter_map(argvars, rettv, FALSE);
11165}
11166
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011167/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011168 * "finddir({fname}[, {path}[, {count}]])" function
11169 */
11170 static void
11171f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011172 typval_T *argvars;
11173 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011174{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011175 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011176}
11177
11178/*
11179 * "findfile({fname}[, {path}[, {count}]])" function
11180 */
11181 static void
11182f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011183 typval_T *argvars;
11184 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011185{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011186 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011187}
11188
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011189#ifdef FEAT_FLOAT
11190/*
11191 * "float2nr({float})" function
11192 */
11193 static void
11194f_float2nr(argvars, rettv)
11195 typval_T *argvars;
11196 typval_T *rettv;
11197{
11198 float_T f;
11199
11200 if (get_float_arg(argvars, &f) == OK)
11201 {
11202 if (f < -0x7fffffff)
11203 rettv->vval.v_number = -0x7fffffff;
11204 else if (f > 0x7fffffff)
11205 rettv->vval.v_number = 0x7fffffff;
11206 else
11207 rettv->vval.v_number = (varnumber_T)f;
11208 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011209}
11210
11211/*
11212 * "floor({float})" function
11213 */
11214 static void
11215f_floor(argvars, rettv)
11216 typval_T *argvars;
11217 typval_T *rettv;
11218{
11219 float_T f;
11220
11221 rettv->v_type = VAR_FLOAT;
11222 if (get_float_arg(argvars, &f) == OK)
11223 rettv->vval.v_float = floor(f);
11224 else
11225 rettv->vval.v_float = 0.0;
11226}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011227
11228/*
11229 * "fmod()" function
11230 */
11231 static void
11232f_fmod(argvars, rettv)
11233 typval_T *argvars;
11234 typval_T *rettv;
11235{
11236 float_T fx, fy;
11237
11238 rettv->v_type = VAR_FLOAT;
11239 if (get_float_arg(argvars, &fx) == OK
11240 && get_float_arg(&argvars[1], &fy) == OK)
11241 rettv->vval.v_float = fmod(fx, fy);
11242 else
11243 rettv->vval.v_float = 0.0;
11244}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011245#endif
11246
Bram Moolenaar0d660222005-01-07 21:51:51 +000011247/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011248 * "fnameescape({string})" function
11249 */
11250 static void
11251f_fnameescape(argvars, rettv)
11252 typval_T *argvars;
11253 typval_T *rettv;
11254{
11255 rettv->vval.v_string = vim_strsave_fnameescape(
11256 get_tv_string(&argvars[0]), FALSE);
11257 rettv->v_type = VAR_STRING;
11258}
11259
11260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 * "fnamemodify({fname}, {mods})" function
11262 */
11263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011265 typval_T *argvars;
11266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267{
11268 char_u *fname;
11269 char_u *mods;
11270 int usedlen = 0;
11271 int len;
11272 char_u *fbuf = NULL;
11273 char_u buf[NUMBUFLEN];
11274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011275 fname = get_tv_string_chk(&argvars[0]);
11276 mods = get_tv_string_buf_chk(&argvars[1], buf);
11277 if (fname == NULL || mods == NULL)
11278 fname = NULL;
11279 else
11280 {
11281 len = (int)STRLEN(fname);
11282 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011289 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 vim_free(fbuf);
11291}
11292
Bram Moolenaar33570922005-01-25 22:26:29 +000011293static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294
11295/*
11296 * "foldclosed()" function
11297 */
11298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011299foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011300 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011301 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011302 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303{
11304#ifdef FEAT_FOLDING
11305 linenr_T lnum;
11306 linenr_T first, last;
11307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11310 {
11311 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11312 {
11313 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 return;
11318 }
11319 }
11320#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322}
11323
11324/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011325 * "foldclosed()" function
11326 */
11327 static void
11328f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011329 typval_T *argvars;
11330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011331{
11332 foldclosed_both(argvars, rettv, FALSE);
11333}
11334
11335/*
11336 * "foldclosedend()" function
11337 */
11338 static void
11339f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011340 typval_T *argvars;
11341 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011342{
11343 foldclosed_both(argvars, rettv, TRUE);
11344}
11345
11346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 * "foldlevel()" function
11348 */
11349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011351 typval_T *argvars UNUSED;
11352 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353{
11354#ifdef FEAT_FOLDING
11355 linenr_T lnum;
11356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361}
11362
11363/*
11364 * "foldtext()" function
11365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011368 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370{
11371#ifdef FEAT_FOLDING
11372 linenr_T lnum;
11373 char_u *s;
11374 char_u *r;
11375 int len;
11376 char *txt;
11377#endif
11378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379 rettv->v_type = VAR_STRING;
11380 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011382 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11383 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11384 <= curbuf->b_ml.ml_line_count
11385 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 {
11387 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011388 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11389 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 {
11391 if (!linewhite(lnum))
11392 break;
11393 ++lnum;
11394 }
11395
11396 /* Find interesting text in this line. */
11397 s = skipwhite(ml_get(lnum));
11398 /* skip C comment-start */
11399 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011402 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011403 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011404 {
11405 s = skipwhite(ml_get(lnum + 1));
11406 if (*s == '*')
11407 s = skipwhite(s + 1);
11408 }
11409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 txt = _("+-%s%3ld lines: ");
11411 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011412 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 + 20 /* for %3ld */
11414 + STRLEN(s))); /* concatenated */
11415 if (r != NULL)
11416 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011417 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11418 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11419 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 len = (int)STRLEN(r);
11421 STRCAT(r, s);
11422 /* remove 'foldmarker' and 'commentstring' */
11423 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 }
11426 }
11427#endif
11428}
11429
11430/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011431 * "foldtextresult(lnum)" function
11432 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011435 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011436 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011437{
11438#ifdef FEAT_FOLDING
11439 linenr_T lnum;
11440 char_u *text;
11441 char_u buf[51];
11442 foldinfo_T foldinfo;
11443 int fold_count;
11444#endif
11445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->v_type = VAR_STRING;
11447 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011448#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011450 /* treat illegal types and illegal string values for {lnum} the same */
11451 if (lnum < 0)
11452 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011453 fold_count = foldedCount(curwin, lnum, &foldinfo);
11454 if (fold_count > 0)
11455 {
11456 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11457 &foldinfo, buf);
11458 if (text == buf)
11459 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011461 }
11462#endif
11463}
11464
11465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 * "foreground()" function
11467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011470 typval_T *argvars UNUSED;
11471 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473#ifdef FEAT_GUI
11474 if (gui.in_use)
11475 gui_mch_set_foreground();
11476#else
11477# ifdef WIN32
11478 win32_set_foreground();
11479# endif
11480#endif
11481}
11482
11483/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011484 * "function()" function
11485 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011488 typval_T *argvars;
11489 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011490{
11491 char_u *s;
11492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011494 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011495 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011496 /* Don't check an autoload name for existence here. */
11497 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011498 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011499 else
11500 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011501 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011502 {
11503 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011504 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011505
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011506 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11507 * also be called from another script. Using trans_function_name()
11508 * would also work, but some plugins depend on the name being
11509 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011510 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011511 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011512 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011513 if (rettv->vval.v_string != NULL)
11514 {
11515 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011516 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011517 }
11518 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011519 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011520 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011522 }
11523}
11524
11525/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011526 * "garbagecollect()" function
11527 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011528 static void
11529f_garbagecollect(argvars, rettv)
11530 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011531 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011532{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011533 /* This is postponed until we are back at the toplevel, because we may be
11534 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11535 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011536
11537 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11538 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011539}
11540
11541/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011542 * "get()" function
11543 */
11544 static void
11545f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011546 typval_T *argvars;
11547 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011548{
Bram Moolenaar33570922005-01-25 22:26:29 +000011549 listitem_T *li;
11550 list_T *l;
11551 dictitem_T *di;
11552 dict_T *d;
11553 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011554
Bram Moolenaare9a41262005-01-15 22:18:47 +000011555 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011556 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011557 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 int error = FALSE;
11560
11561 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11562 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011564 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011565 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011566 else if (argvars[0].v_type == VAR_DICT)
11567 {
11568 if ((d = argvars[0].vval.v_dict) != NULL)
11569 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011570 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011571 if (di != NULL)
11572 tv = &di->di_tv;
11573 }
11574 }
11575 else
11576 EMSG2(_(e_listdictarg), "get()");
11577
11578 if (tv == NULL)
11579 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011580 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011581 copy_tv(&argvars[2], rettv);
11582 }
11583 else
11584 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011585}
11586
Bram Moolenaar342337a2005-07-21 21:11:17 +000011587static 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 +000011588
11589/*
11590 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011591 * Return a range (from start to end) of lines in rettv from the specified
11592 * buffer.
11593 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011594 */
11595 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011596get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011597 buf_T *buf;
11598 linenr_T start;
11599 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011600 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011601 typval_T *rettv;
11602{
11603 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011604
Bram Moolenaar959a1432013-12-14 12:17:38 +010011605 rettv->v_type = VAR_STRING;
11606 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011607 if (retlist && rettv_list_alloc(rettv) == FAIL)
11608 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011609
11610 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11611 return;
11612
11613 if (!retlist)
11614 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011615 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11616 p = ml_get_buf(buf, start, FALSE);
11617 else
11618 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011619 rettv->vval.v_string = vim_strsave(p);
11620 }
11621 else
11622 {
11623 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011624 return;
11625
11626 if (start < 1)
11627 start = 1;
11628 if (end > buf->b_ml.ml_line_count)
11629 end = buf->b_ml.ml_line_count;
11630 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011631 if (list_append_string(rettv->vval.v_list,
11632 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011633 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011634 }
11635}
11636
11637/*
11638 * "getbufline()" function
11639 */
11640 static void
11641f_getbufline(argvars, rettv)
11642 typval_T *argvars;
11643 typval_T *rettv;
11644{
11645 linenr_T lnum;
11646 linenr_T end;
11647 buf_T *buf;
11648
11649 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11650 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011651 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011652 --emsg_off;
11653
Bram Moolenaar661b1822005-07-28 22:36:45 +000011654 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011655 if (argvars[2].v_type == VAR_UNKNOWN)
11656 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011657 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011658 end = get_tv_lnum_buf(&argvars[2], buf);
11659
Bram Moolenaar342337a2005-07-21 21:11:17 +000011660 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011661}
11662
Bram Moolenaar0d660222005-01-07 21:51:51 +000011663/*
11664 * "getbufvar()" function
11665 */
11666 static void
11667f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011668 typval_T *argvars;
11669 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011670{
11671 buf_T *buf;
11672 buf_T *save_curbuf;
11673 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011674 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011675 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011677 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11678 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011679 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011680 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011681
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011682 rettv->v_type = VAR_STRING;
11683 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011684
11685 if (buf != NULL && varname != NULL)
11686 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011687 /* set curbuf to be our buf, temporarily */
11688 save_curbuf = curbuf;
11689 curbuf = buf;
11690
Bram Moolenaar0d660222005-01-07 21:51:51 +000011691 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011692 {
11693 if (get_option_tv(&varname, rettv, TRUE) == OK)
11694 done = TRUE;
11695 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011696 else if (STRCMP(varname, "changedtick") == 0)
11697 {
11698 rettv->v_type = VAR_NUMBER;
11699 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011700 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011701 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011702 else
11703 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011704 /* Look up the variable. */
11705 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11706 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11707 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011708 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011709 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011710 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011711 done = TRUE;
11712 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011713 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011714
11715 /* restore previous notion of curbuf */
11716 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011717 }
11718
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011719 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11720 /* use the default value */
11721 copy_tv(&argvars[2], rettv);
11722
Bram Moolenaar0d660222005-01-07 21:51:51 +000011723 --emsg_off;
11724}
11725
11726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 * "getchar()" function
11728 */
11729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011731 typval_T *argvars;
11732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733{
11734 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011735 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011737 /* Position the cursor. Needed after a message that ends in a space. */
11738 windgoto(msg_row, msg_col);
11739
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 ++no_mapping;
11741 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011742 for (;;)
11743 {
11744 if (argvars[0].v_type == VAR_UNKNOWN)
11745 /* getchar(): blocking wait. */
11746 n = safe_vgetc();
11747 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11748 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011749 n = vpeekc_any();
11750 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011751 /* illegal argument or getchar(0) and no char avail: return zero */
11752 n = 0;
11753 else
11754 /* getchar(0) and char avail: return char */
11755 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011756
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011757 if (n == K_IGNORE)
11758 continue;
11759 break;
11760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 --no_mapping;
11762 --allow_keys;
11763
Bram Moolenaar219b8702006-11-01 14:32:36 +000011764 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11765 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11766 vimvars[VV_MOUSE_COL].vv_nr = 0;
11767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011768 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 if (IS_SPECIAL(n) || mod_mask != 0)
11770 {
11771 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11772 int i = 0;
11773
11774 /* Turn a special key into three bytes, plus modifier. */
11775 if (mod_mask != 0)
11776 {
11777 temp[i++] = K_SPECIAL;
11778 temp[i++] = KS_MODIFIER;
11779 temp[i++] = mod_mask;
11780 }
11781 if (IS_SPECIAL(n))
11782 {
11783 temp[i++] = K_SPECIAL;
11784 temp[i++] = K_SECOND(n);
11785 temp[i++] = K_THIRD(n);
11786 }
11787#ifdef FEAT_MBYTE
11788 else if (has_mbyte)
11789 i += (*mb_char2bytes)(n, temp + i);
11790#endif
11791 else
11792 temp[i++] = n;
11793 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794 rettv->v_type = VAR_STRING;
11795 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011796
11797#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011798 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011799 {
11800 int row = mouse_row;
11801 int col = mouse_col;
11802 win_T *win;
11803 linenr_T lnum;
11804# ifdef FEAT_WINDOWS
11805 win_T *wp;
11806# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011807 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011808
11809 if (row >= 0 && col >= 0)
11810 {
11811 /* Find the window at the mouse coordinates and compute the
11812 * text position. */
11813 win = mouse_find_win(&row, &col);
11814 (void)mouse_comp_pos(win, &row, &col, &lnum);
11815# ifdef FEAT_WINDOWS
11816 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011817 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011818# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011819 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011820 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11821 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11822 }
11823 }
11824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 }
11826}
11827
11828/*
11829 * "getcharmod()" function
11830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011833 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837}
11838
11839/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011840 * "getcharsearch()" function
11841 */
11842 static void
11843f_getcharsearch(argvars, rettv)
11844 typval_T *argvars UNUSED;
11845 typval_T *rettv;
11846{
11847 if (rettv_dict_alloc(rettv) != FAIL)
11848 {
11849 dict_T *dict = rettv->vval.v_dict;
11850
11851 dict_add_nr_str(dict, "char", 0L, last_csearch());
11852 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11853 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11854 }
11855}
11856
11857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 * "getcmdline()" function
11859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011862 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->v_type = VAR_STRING;
11866 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867}
11868
11869/*
11870 * "getcmdpos()" function
11871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011874 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878}
11879
11880/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011881 * "getcmdtype()" function
11882 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011883 static void
11884f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011885 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011886 typval_T *rettv;
11887{
11888 rettv->v_type = VAR_STRING;
11889 rettv->vval.v_string = alloc(2);
11890 if (rettv->vval.v_string != NULL)
11891 {
11892 rettv->vval.v_string[0] = get_cmdline_type();
11893 rettv->vval.v_string[1] = NUL;
11894 }
11895}
11896
11897/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011898 * "getcmdwintype()" function
11899 */
11900 static void
11901f_getcmdwintype(argvars, rettv)
11902 typval_T *argvars UNUSED;
11903 typval_T *rettv;
11904{
11905 rettv->v_type = VAR_STRING;
11906 rettv->vval.v_string = NULL;
11907#ifdef FEAT_CMDWIN
11908 rettv->vval.v_string = alloc(2);
11909 if (rettv->vval.v_string != NULL)
11910 {
11911 rettv->vval.v_string[0] = cmdwin_type;
11912 rettv->vval.v_string[1] = NUL;
11913 }
11914#endif
11915}
11916
11917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 * "getcwd()" function
11919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011921f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011922 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011925 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011928 rettv->vval.v_string = NULL;
11929 cwd = alloc(MAXPATHL);
11930 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011932 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11933 {
11934 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011936 if (rettv->vval.v_string != NULL)
11937 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011939 }
11940 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941 }
11942}
11943
11944/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011945 * "getfontname()" function
11946 */
11947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011948f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011949 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011951{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011952 rettv->v_type = VAR_STRING;
11953 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011954#ifdef FEAT_GUI
11955 if (gui.in_use)
11956 {
11957 GuiFont font;
11958 char_u *name = NULL;
11959
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011960 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011961 {
11962 /* Get the "Normal" font. Either the name saved by
11963 * hl_set_font_name() or from the font ID. */
11964 font = gui.norm_font;
11965 name = hl_get_font_name();
11966 }
11967 else
11968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011969 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011970 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11971 return;
11972 font = gui_mch_get_font(name, FALSE);
11973 if (font == NOFONT)
11974 return; /* Invalid font name, return empty string. */
11975 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011977 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011978 gui_mch_free_font(font);
11979 }
11980#endif
11981}
11982
11983/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011984 * "getfperm({fname})" function
11985 */
11986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011988 typval_T *argvars;
11989 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011990{
11991 char_u *fname;
11992 struct stat st;
11993 char_u *perm = NULL;
11994 char_u flags[] = "rwx";
11995 int i;
11996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011999 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012000 if (mch_stat((char *)fname, &st) >= 0)
12001 {
12002 perm = vim_strsave((char_u *)"---------");
12003 if (perm != NULL)
12004 {
12005 for (i = 0; i < 9; i++)
12006 {
12007 if (st.st_mode & (1 << (8 - i)))
12008 perm[i] = flags[i % 3];
12009 }
12010 }
12011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012012 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012013}
12014
12015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 * "getfsize({fname})" function
12017 */
12018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012020 typval_T *argvars;
12021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022{
12023 char_u *fname;
12024 struct stat st;
12025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029
12030 if (mch_stat((char *)fname, &st) >= 0)
12031 {
12032 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012035 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012037
12038 /* non-perfect check for overflow */
12039 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12040 rettv->vval.v_number = -2;
12041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 }
12043 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012044 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045}
12046
12047/*
12048 * "getftime({fname})" function
12049 */
12050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 typval_T *argvars;
12053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054{
12055 char_u *fname;
12056 struct stat st;
12057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059
12060 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012061 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064}
12065
12066/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012067 * "getftype({fname})" function
12068 */
12069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012070f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012071 typval_T *argvars;
12072 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012073{
12074 char_u *fname;
12075 struct stat st;
12076 char_u *type = NULL;
12077 char *t;
12078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012082 if (mch_lstat((char *)fname, &st) >= 0)
12083 {
12084#ifdef S_ISREG
12085 if (S_ISREG(st.st_mode))
12086 t = "file";
12087 else if (S_ISDIR(st.st_mode))
12088 t = "dir";
12089# ifdef S_ISLNK
12090 else if (S_ISLNK(st.st_mode))
12091 t = "link";
12092# endif
12093# ifdef S_ISBLK
12094 else if (S_ISBLK(st.st_mode))
12095 t = "bdev";
12096# endif
12097# ifdef S_ISCHR
12098 else if (S_ISCHR(st.st_mode))
12099 t = "cdev";
12100# endif
12101# ifdef S_ISFIFO
12102 else if (S_ISFIFO(st.st_mode))
12103 t = "fifo";
12104# endif
12105# ifdef S_ISSOCK
12106 else if (S_ISSOCK(st.st_mode))
12107 t = "fifo";
12108# endif
12109 else
12110 t = "other";
12111#else
12112# ifdef S_IFMT
12113 switch (st.st_mode & S_IFMT)
12114 {
12115 case S_IFREG: t = "file"; break;
12116 case S_IFDIR: t = "dir"; break;
12117# ifdef S_IFLNK
12118 case S_IFLNK: t = "link"; break;
12119# endif
12120# ifdef S_IFBLK
12121 case S_IFBLK: t = "bdev"; break;
12122# endif
12123# ifdef S_IFCHR
12124 case S_IFCHR: t = "cdev"; break;
12125# endif
12126# ifdef S_IFIFO
12127 case S_IFIFO: t = "fifo"; break;
12128# endif
12129# ifdef S_IFSOCK
12130 case S_IFSOCK: t = "socket"; break;
12131# endif
12132 default: t = "other";
12133 }
12134# else
12135 if (mch_isdir(fname))
12136 t = "dir";
12137 else
12138 t = "file";
12139# endif
12140#endif
12141 type = vim_strsave((char_u *)t);
12142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012144}
12145
12146/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012147 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012148 */
12149 static void
12150f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012151 typval_T *argvars;
12152 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012153{
12154 linenr_T lnum;
12155 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012156 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012157
12158 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012159 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012160 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012161 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012162 retlist = FALSE;
12163 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012165 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012166 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012167 retlist = TRUE;
12168 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012169
Bram Moolenaar342337a2005-07-21 21:11:17 +000012170 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012171}
12172
12173/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012174 * "getmatches()" function
12175 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012176 static void
12177f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012178 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012179 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012180{
12181#ifdef FEAT_SEARCH_EXTRA
12182 dict_T *dict;
12183 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012184 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012185
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012186 if (rettv_list_alloc(rettv) == OK)
12187 {
12188 while (cur != NULL)
12189 {
12190 dict = dict_alloc();
12191 if (dict == NULL)
12192 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012193 if (cur->match.regprog == NULL)
12194 {
12195 /* match added with matchaddpos() */
12196 for (i = 0; i < MAXPOSMATCH; ++i)
12197 {
12198 llpos_T *llpos;
12199 char buf[6];
12200 list_T *l;
12201
12202 llpos = &cur->pos.pos[i];
12203 if (llpos->lnum == 0)
12204 break;
12205 l = list_alloc();
12206 if (l == NULL)
12207 break;
12208 list_append_number(l, (varnumber_T)llpos->lnum);
12209 if (llpos->col > 0)
12210 {
12211 list_append_number(l, (varnumber_T)llpos->col);
12212 list_append_number(l, (varnumber_T)llpos->len);
12213 }
12214 sprintf(buf, "pos%d", i + 1);
12215 dict_add_list(dict, buf, l);
12216 }
12217 }
12218 else
12219 {
12220 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12221 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012222 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012223 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12224 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012225# ifdef FEAT_CONCEAL
12226 if (cur->conceal_char)
12227 {
12228 char_u buf[MB_MAXBYTES + 1];
12229
12230 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12231 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12232 }
12233# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012234 list_append_dict(rettv->vval.v_list, dict);
12235 cur = cur->next;
12236 }
12237 }
12238#endif
12239}
12240
12241/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012242 * "getpid()" function
12243 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012244 static void
12245f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012246 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012247 typval_T *rettv;
12248{
12249 rettv->vval.v_number = mch_get_pid();
12250}
12251
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012252static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12253
12254/*
12255 * "getcurpos()" function
12256 */
12257 static void
12258f_getcurpos(argvars, rettv)
12259 typval_T *argvars;
12260 typval_T *rettv;
12261{
12262 getpos_both(argvars, rettv, TRUE);
12263}
12264
Bram Moolenaar18081e32008-02-20 19:11:07 +000012265/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012266 * "getpos(string)" function
12267 */
12268 static void
12269f_getpos(argvars, rettv)
12270 typval_T *argvars;
12271 typval_T *rettv;
12272{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012273 getpos_both(argvars, rettv, FALSE);
12274}
12275
12276 static void
12277getpos_both(argvars, rettv, getcurpos)
12278 typval_T *argvars;
12279 typval_T *rettv;
12280 int getcurpos;
12281{
Bram Moolenaara5525202006-03-02 22:52:09 +000012282 pos_T *fp;
12283 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012284 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012285
12286 if (rettv_list_alloc(rettv) == OK)
12287 {
12288 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012289 if (getcurpos)
12290 fp = &curwin->w_cursor;
12291 else
12292 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012293 if (fnum != -1)
12294 list_append_number(l, (varnumber_T)fnum);
12295 else
12296 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012297 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12298 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012299 list_append_number(l, (fp != NULL)
12300 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012301 : (varnumber_T)0);
12302 list_append_number(l,
12303#ifdef FEAT_VIRTUALEDIT
12304 (fp != NULL) ? (varnumber_T)fp->coladd :
12305#endif
12306 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012307 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012308 list_append_number(l, curwin->w_curswant == MAXCOL ?
12309 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012310 }
12311 else
12312 rettv->vval.v_number = FALSE;
12313}
12314
12315/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012316 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012317 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012318 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012319f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012320 typval_T *argvars UNUSED;
12321 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012322{
12323#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012324 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012325#endif
12326
Bram Moolenaar2641f772005-03-25 21:58:17 +000012327#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012328 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012329 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012330 wp = NULL;
12331 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12332 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012333 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012334 if (wp == NULL)
12335 return;
12336 }
12337
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012338 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012339 }
12340#endif
12341}
12342
12343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 * "getreg()" function
12345 */
12346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012348 typval_T *argvars;
12349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350{
12351 char_u *strregname;
12352 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012353 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012354 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012355 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012357 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012359 strregname = get_tv_string_chk(&argvars[0]);
12360 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012361 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012363 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012364 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12365 return_list = get_tv_number_chk(&argvars[2], &error);
12366 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012369 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012370
12371 if (error)
12372 return;
12373
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 regname = (strregname == NULL ? '"' : *strregname);
12375 if (regname == 0)
12376 regname = '"';
12377
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012378 if (return_list)
12379 {
12380 rettv->v_type = VAR_LIST;
12381 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12382 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012383 if (rettv->vval.v_list != NULL)
12384 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012385 }
12386 else
12387 {
12388 rettv->v_type = VAR_STRING;
12389 rettv->vval.v_string = get_reg_contents(regname,
12390 arg2 ? GREG_EXPR_SRC : 0);
12391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392}
12393
12394/*
12395 * "getregtype()" function
12396 */
12397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012399 typval_T *argvars;
12400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401{
12402 char_u *strregname;
12403 int regname;
12404 char_u buf[NUMBUFLEN + 2];
12405 long reglen = 0;
12406
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012407 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012408 {
12409 strregname = get_tv_string_chk(&argvars[0]);
12410 if (strregname == NULL) /* type error; errmsg already given */
12411 {
12412 rettv->v_type = VAR_STRING;
12413 rettv->vval.v_string = NULL;
12414 return;
12415 }
12416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417 else
12418 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012419 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420
12421 regname = (strregname == NULL ? '"' : *strregname);
12422 if (regname == 0)
12423 regname = '"';
12424
12425 buf[0] = NUL;
12426 buf[1] = NUL;
12427 switch (get_reg_type(regname, &reglen))
12428 {
12429 case MLINE: buf[0] = 'V'; break;
12430 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 case MBLOCK:
12432 buf[0] = Ctrl_V;
12433 sprintf((char *)buf + 1, "%ld", reglen + 1);
12434 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012436 rettv->v_type = VAR_STRING;
12437 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438}
12439
12440/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012441 * "gettabvar()" function
12442 */
12443 static void
12444f_gettabvar(argvars, rettv)
12445 typval_T *argvars;
12446 typval_T *rettv;
12447{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012448 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012449 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012450 dictitem_T *v;
12451 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012452 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012453
12454 rettv->v_type = VAR_STRING;
12455 rettv->vval.v_string = NULL;
12456
12457 varname = get_tv_string_chk(&argvars[1]);
12458 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12459 if (tp != NULL && varname != NULL)
12460 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012461 /* Set tp to be our tabpage, temporarily. Also set the window to the
12462 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012463 if (switch_win(&oldcurwin, &oldtabpage,
12464 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012465 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012466 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012467 /* look up the variable */
12468 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12469 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12470 if (v != NULL)
12471 {
12472 copy_tv(&v->di_tv, rettv);
12473 done = TRUE;
12474 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012475 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012476
12477 /* restore previous notion of curwin */
12478 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012479 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012480
12481 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012482 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012483}
12484
12485/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012486 * "gettabwinvar()" function
12487 */
12488 static void
12489f_gettabwinvar(argvars, rettv)
12490 typval_T *argvars;
12491 typval_T *rettv;
12492{
12493 getwinvar(argvars, rettv, 1);
12494}
12495
12496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497 * "getwinposx()" function
12498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012501 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505#ifdef FEAT_GUI
12506 if (gui.in_use)
12507 {
12508 int x, y;
12509
12510 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012511 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 }
12513#endif
12514}
12515
12516/*
12517 * "getwinposy()" function
12518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012520f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012521 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012525#ifdef FEAT_GUI
12526 if (gui.in_use)
12527 {
12528 int x, y;
12529
12530 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 }
12533#endif
12534}
12535
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012536/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012537 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012538 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012539 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012540find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012541 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012542 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012543{
12544#ifdef FEAT_WINDOWS
12545 win_T *wp;
12546#endif
12547 int nr;
12548
12549 nr = get_tv_number_chk(vp, NULL);
12550
12551#ifdef FEAT_WINDOWS
12552 if (nr < 0)
12553 return NULL;
12554 if (nr == 0)
12555 return curwin;
12556
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012557 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12558 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012559 if (--nr <= 0)
12560 break;
12561 return wp;
12562#else
12563 if (nr == 0 || nr == 1)
12564 return curwin;
12565 return NULL;
12566#endif
12567}
12568
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569/*
12570 * "getwinvar()" function
12571 */
12572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012573f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012574 typval_T *argvars;
12575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012577 getwinvar(argvars, rettv, 0);
12578}
12579
12580/*
12581 * getwinvar() and gettabwinvar()
12582 */
12583 static void
12584getwinvar(argvars, rettv, off)
12585 typval_T *argvars;
12586 typval_T *rettv;
12587 int off; /* 1 for gettabwinvar() */
12588{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012589 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012591 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012592 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012593 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012594#ifdef FEAT_WINDOWS
12595 win_T *oldcurwin;
12596 tabpage_T *oldtabpage;
12597 int need_switch_win;
12598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012600#ifdef FEAT_WINDOWS
12601 if (off == 1)
12602 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12603 else
12604 tp = curtab;
12605#endif
12606 win = find_win_by_nr(&argvars[off], tp);
12607 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012608 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012610 rettv->v_type = VAR_STRING;
12611 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612
12613 if (win != NULL && varname != NULL)
12614 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012615#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012616 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012617 * otherwise the window is not valid. Only do this when needed,
12618 * autocommands get blocked. */
12619 need_switch_win = !(tp == curtab && win == curwin);
12620 if (!need_switch_win
12621 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12622#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012623 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012624 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012625 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012626 if (get_option_tv(&varname, rettv, 1) == OK)
12627 done = TRUE;
12628 }
12629 else
12630 {
12631 /* Look up the variable. */
12632 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12633 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12634 varname, FALSE);
12635 if (v != NULL)
12636 {
12637 copy_tv(&v->di_tv, rettv);
12638 done = TRUE;
12639 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012642
Bram Moolenaarba117c22015-09-29 16:53:22 +020012643#ifdef FEAT_WINDOWS
12644 if (need_switch_win)
12645 /* restore previous notion of curwin */
12646 restore_win(oldcurwin, oldtabpage, TRUE);
12647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 }
12649
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012650 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12651 /* use the default return value */
12652 copy_tv(&argvars[off + 2], rettv);
12653
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 --emsg_off;
12655}
12656
12657/*
12658 * "glob()" function
12659 */
12660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012661f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012662 typval_T *argvars;
12663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012665 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012667 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012669 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012670 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012672 if (argvars[1].v_type != VAR_UNKNOWN)
12673 {
12674 if (get_tv_number_chk(&argvars[1], &error))
12675 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012676 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012677 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012678 if (get_tv_number_chk(&argvars[2], &error))
12679 {
12680 rettv->v_type = VAR_LIST;
12681 rettv->vval.v_list = NULL;
12682 }
12683 if (argvars[3].v_type != VAR_UNKNOWN
12684 && get_tv_number_chk(&argvars[3], &error))
12685 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012686 }
12687 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012688 if (!error)
12689 {
12690 ExpandInit(&xpc);
12691 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012692 if (p_wic)
12693 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012694 if (rettv->v_type == VAR_STRING)
12695 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012696 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012697 else if (rettv_list_alloc(rettv) != FAIL)
12698 {
12699 int i;
12700
12701 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12702 NULL, options, WILD_ALL_KEEP);
12703 for (i = 0; i < xpc.xp_numfiles; i++)
12704 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12705
12706 ExpandCleanup(&xpc);
12707 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012708 }
12709 else
12710 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711}
12712
12713/*
12714 * "globpath()" function
12715 */
12716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012718 typval_T *argvars;
12719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012721 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012723 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012724 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012725 garray_T ga;
12726 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012728 /* When the optional second argument is non-zero, don't remove matches
12729 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012731 if (argvars[2].v_type != VAR_UNKNOWN)
12732 {
12733 if (get_tv_number_chk(&argvars[2], &error))
12734 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012735 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012736 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012737 if (get_tv_number_chk(&argvars[3], &error))
12738 {
12739 rettv->v_type = VAR_LIST;
12740 rettv->vval.v_list = NULL;
12741 }
12742 if (argvars[4].v_type != VAR_UNKNOWN
12743 && get_tv_number_chk(&argvars[4], &error))
12744 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012745 }
12746 }
12747 if (file != NULL && !error)
12748 {
12749 ga_init2(&ga, (int)sizeof(char_u *), 10);
12750 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12751 if (rettv->v_type == VAR_STRING)
12752 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12753 else if (rettv_list_alloc(rettv) != FAIL)
12754 for (i = 0; i < ga.ga_len; ++i)
12755 list_append_string(rettv->vval.v_list,
12756 ((char_u **)(ga.ga_data))[i], -1);
12757 ga_clear_strings(&ga);
12758 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012759 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012760 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761}
12762
12763/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012764 * "glob2regpat()" function
12765 */
12766 static void
12767f_glob2regpat(argvars, rettv)
12768 typval_T *argvars;
12769 typval_T *rettv;
12770{
12771 char_u *pat = get_tv_string_chk(&argvars[0]);
12772
12773 rettv->v_type = VAR_STRING;
12774 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12775}
12776
12777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 * "has()" function
12779 */
12780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012781f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012782 typval_T *argvars;
12783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784{
12785 int i;
12786 char_u *name;
12787 int n = FALSE;
12788 static char *(has_list[]) =
12789 {
12790#ifdef AMIGA
12791 "amiga",
12792# ifdef FEAT_ARP
12793 "arp",
12794# endif
12795#endif
12796#ifdef __BEOS__
12797 "beos",
12798#endif
12799#ifdef MSDOS
12800# ifdef DJGPP
12801 "dos32",
12802# else
12803 "dos16",
12804# endif
12805#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012806#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 "mac",
12808#endif
12809#if defined(MACOS_X_UNIX)
12810 "macunix",
12811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812#ifdef __QNX__
12813 "qnx",
12814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815#ifdef UNIX
12816 "unix",
12817#endif
12818#ifdef VMS
12819 "vms",
12820#endif
12821#ifdef WIN16
12822 "win16",
12823#endif
12824#ifdef WIN32
12825 "win32",
12826#endif
12827#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12828 "win32unix",
12829#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012830#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 "win64",
12832#endif
12833#ifdef EBCDIC
12834 "ebcdic",
12835#endif
12836#ifndef CASE_INSENSITIVE_FILENAME
12837 "fname_case",
12838#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012839#ifdef HAVE_ACL
12840 "acl",
12841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842#ifdef FEAT_ARABIC
12843 "arabic",
12844#endif
12845#ifdef FEAT_AUTOCMD
12846 "autocmd",
12847#endif
12848#ifdef FEAT_BEVAL
12849 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012850# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12851 "balloon_multiline",
12852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853#endif
12854#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12855 "builtin_terms",
12856# ifdef ALL_BUILTIN_TCAPS
12857 "all_builtin_terms",
12858# endif
12859#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012860#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12861 || defined(FEAT_GUI_W32) \
12862 || defined(FEAT_GUI_MOTIF))
12863 "browsefilter",
12864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865#ifdef FEAT_BYTEOFF
12866 "byte_offset",
12867#endif
12868#ifdef FEAT_CINDENT
12869 "cindent",
12870#endif
12871#ifdef FEAT_CLIENTSERVER
12872 "clientserver",
12873#endif
12874#ifdef FEAT_CLIPBOARD
12875 "clipboard",
12876#endif
12877#ifdef FEAT_CMDL_COMPL
12878 "cmdline_compl",
12879#endif
12880#ifdef FEAT_CMDHIST
12881 "cmdline_hist",
12882#endif
12883#ifdef FEAT_COMMENTS
12884 "comments",
12885#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012886#ifdef FEAT_CONCEAL
12887 "conceal",
12888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889#ifdef FEAT_CRYPT
12890 "cryptv",
12891#endif
12892#ifdef FEAT_CSCOPE
12893 "cscope",
12894#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012895#ifdef FEAT_CURSORBIND
12896 "cursorbind",
12897#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012898#ifdef CURSOR_SHAPE
12899 "cursorshape",
12900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901#ifdef DEBUG
12902 "debug",
12903#endif
12904#ifdef FEAT_CON_DIALOG
12905 "dialog_con",
12906#endif
12907#ifdef FEAT_GUI_DIALOG
12908 "dialog_gui",
12909#endif
12910#ifdef FEAT_DIFF
12911 "diff",
12912#endif
12913#ifdef FEAT_DIGRAPHS
12914 "digraphs",
12915#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012916#ifdef FEAT_DIRECTX
12917 "directx",
12918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919#ifdef FEAT_DND
12920 "dnd",
12921#endif
12922#ifdef FEAT_EMACS_TAGS
12923 "emacs_tags",
12924#endif
12925 "eval", /* always present, of course! */
12926#ifdef FEAT_EX_EXTRA
12927 "ex_extra",
12928#endif
12929#ifdef FEAT_SEARCH_EXTRA
12930 "extra_search",
12931#endif
12932#ifdef FEAT_FKMAP
12933 "farsi",
12934#endif
12935#ifdef FEAT_SEARCHPATH
12936 "file_in_path",
12937#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012938#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012939 "filterpipe",
12940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941#ifdef FEAT_FIND_ID
12942 "find_in_path",
12943#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012944#ifdef FEAT_FLOAT
12945 "float",
12946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947#ifdef FEAT_FOLDING
12948 "folding",
12949#endif
12950#ifdef FEAT_FOOTER
12951 "footer",
12952#endif
12953#if !defined(USE_SYSTEM) && defined(UNIX)
12954 "fork",
12955#endif
12956#ifdef FEAT_GETTEXT
12957 "gettext",
12958#endif
12959#ifdef FEAT_GUI
12960 "gui",
12961#endif
12962#ifdef FEAT_GUI_ATHENA
12963# ifdef FEAT_GUI_NEXTAW
12964 "gui_neXtaw",
12965# else
12966 "gui_athena",
12967# endif
12968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969#ifdef FEAT_GUI_GTK
12970 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012973#ifdef FEAT_GUI_GNOME
12974 "gui_gnome",
12975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976#ifdef FEAT_GUI_MAC
12977 "gui_mac",
12978#endif
12979#ifdef FEAT_GUI_MOTIF
12980 "gui_motif",
12981#endif
12982#ifdef FEAT_GUI_PHOTON
12983 "gui_photon",
12984#endif
12985#ifdef FEAT_GUI_W16
12986 "gui_win16",
12987#endif
12988#ifdef FEAT_GUI_W32
12989 "gui_win32",
12990#endif
12991#ifdef FEAT_HANGULIN
12992 "hangul_input",
12993#endif
12994#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12995 "iconv",
12996#endif
12997#ifdef FEAT_INS_EXPAND
12998 "insert_expand",
12999#endif
13000#ifdef FEAT_JUMPLIST
13001 "jumplist",
13002#endif
13003#ifdef FEAT_KEYMAP
13004 "keymap",
13005#endif
13006#ifdef FEAT_LANGMAP
13007 "langmap",
13008#endif
13009#ifdef FEAT_LIBCALL
13010 "libcall",
13011#endif
13012#ifdef FEAT_LINEBREAK
13013 "linebreak",
13014#endif
13015#ifdef FEAT_LISP
13016 "lispindent",
13017#endif
13018#ifdef FEAT_LISTCMDS
13019 "listcmds",
13020#endif
13021#ifdef FEAT_LOCALMAP
13022 "localmap",
13023#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013024#ifdef FEAT_LUA
13025# ifndef DYNAMIC_LUA
13026 "lua",
13027# endif
13028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029#ifdef FEAT_MENU
13030 "menu",
13031#endif
13032#ifdef FEAT_SESSION
13033 "mksession",
13034#endif
13035#ifdef FEAT_MODIFY_FNAME
13036 "modify_fname",
13037#endif
13038#ifdef FEAT_MOUSE
13039 "mouse",
13040#endif
13041#ifdef FEAT_MOUSESHAPE
13042 "mouseshape",
13043#endif
13044#if defined(UNIX) || defined(VMS)
13045# ifdef FEAT_MOUSE_DEC
13046 "mouse_dec",
13047# endif
13048# ifdef FEAT_MOUSE_GPM
13049 "mouse_gpm",
13050# endif
13051# ifdef FEAT_MOUSE_JSB
13052 "mouse_jsbterm",
13053# endif
13054# ifdef FEAT_MOUSE_NET
13055 "mouse_netterm",
13056# endif
13057# ifdef FEAT_MOUSE_PTERM
13058 "mouse_pterm",
13059# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013060# ifdef FEAT_MOUSE_SGR
13061 "mouse_sgr",
13062# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013063# ifdef FEAT_SYSMOUSE
13064 "mouse_sysmouse",
13065# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013066# ifdef FEAT_MOUSE_URXVT
13067 "mouse_urxvt",
13068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069# ifdef FEAT_MOUSE_XTERM
13070 "mouse_xterm",
13071# endif
13072#endif
13073#ifdef FEAT_MBYTE
13074 "multi_byte",
13075#endif
13076#ifdef FEAT_MBYTE_IME
13077 "multi_byte_ime",
13078#endif
13079#ifdef FEAT_MULTI_LANG
13080 "multi_lang",
13081#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013082#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013083#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013084 "mzscheme",
13085#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087#ifdef FEAT_OLE
13088 "ole",
13089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090#ifdef FEAT_PATH_EXTRA
13091 "path_extra",
13092#endif
13093#ifdef FEAT_PERL
13094#ifndef DYNAMIC_PERL
13095 "perl",
13096#endif
13097#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013098#ifdef FEAT_PERSISTENT_UNDO
13099 "persistent_undo",
13100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101#ifdef FEAT_PYTHON
13102#ifndef DYNAMIC_PYTHON
13103 "python",
13104#endif
13105#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013106#ifdef FEAT_PYTHON3
13107#ifndef DYNAMIC_PYTHON3
13108 "python3",
13109#endif
13110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111#ifdef FEAT_POSTSCRIPT
13112 "postscript",
13113#endif
13114#ifdef FEAT_PRINTER
13115 "printer",
13116#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013117#ifdef FEAT_PROFILE
13118 "profile",
13119#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013120#ifdef FEAT_RELTIME
13121 "reltime",
13122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123#ifdef FEAT_QUICKFIX
13124 "quickfix",
13125#endif
13126#ifdef FEAT_RIGHTLEFT
13127 "rightleft",
13128#endif
13129#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13130 "ruby",
13131#endif
13132#ifdef FEAT_SCROLLBIND
13133 "scrollbind",
13134#endif
13135#ifdef FEAT_CMDL_INFO
13136 "showcmd",
13137 "cmdline_info",
13138#endif
13139#ifdef FEAT_SIGNS
13140 "signs",
13141#endif
13142#ifdef FEAT_SMARTINDENT
13143 "smartindent",
13144#endif
13145#ifdef FEAT_SNIFF
13146 "sniff",
13147#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013148#ifdef STARTUPTIME
13149 "startuptime",
13150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151#ifdef FEAT_STL_OPT
13152 "statusline",
13153#endif
13154#ifdef FEAT_SUN_WORKSHOP
13155 "sun_workshop",
13156#endif
13157#ifdef FEAT_NETBEANS_INTG
13158 "netbeans_intg",
13159#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013160#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013161 "spell",
13162#endif
13163#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164 "syntax",
13165#endif
13166#if defined(USE_SYSTEM) || !defined(UNIX)
13167 "system",
13168#endif
13169#ifdef FEAT_TAG_BINS
13170 "tag_binary",
13171#endif
13172#ifdef FEAT_TAG_OLDSTATIC
13173 "tag_old_static",
13174#endif
13175#ifdef FEAT_TAG_ANYWHITE
13176 "tag_any_white",
13177#endif
13178#ifdef FEAT_TCL
13179# ifndef DYNAMIC_TCL
13180 "tcl",
13181# endif
13182#endif
13183#ifdef TERMINFO
13184 "terminfo",
13185#endif
13186#ifdef FEAT_TERMRESPONSE
13187 "termresponse",
13188#endif
13189#ifdef FEAT_TEXTOBJ
13190 "textobjects",
13191#endif
13192#ifdef HAVE_TGETENT
13193 "tgetent",
13194#endif
13195#ifdef FEAT_TITLE
13196 "title",
13197#endif
13198#ifdef FEAT_TOOLBAR
13199 "toolbar",
13200#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013201#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13202 "unnamedplus",
13203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204#ifdef FEAT_USR_CMDS
13205 "user-commands", /* was accidentally included in 5.4 */
13206 "user_commands",
13207#endif
13208#ifdef FEAT_VIMINFO
13209 "viminfo",
13210#endif
13211#ifdef FEAT_VERTSPLIT
13212 "vertsplit",
13213#endif
13214#ifdef FEAT_VIRTUALEDIT
13215 "virtualedit",
13216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218#ifdef FEAT_VISUALEXTRA
13219 "visualextra",
13220#endif
13221#ifdef FEAT_VREPLACE
13222 "vreplace",
13223#endif
13224#ifdef FEAT_WILDIGN
13225 "wildignore",
13226#endif
13227#ifdef FEAT_WILDMENU
13228 "wildmenu",
13229#endif
13230#ifdef FEAT_WINDOWS
13231 "windows",
13232#endif
13233#ifdef FEAT_WAK
13234 "winaltkeys",
13235#endif
13236#ifdef FEAT_WRITEBACKUP
13237 "writebackup",
13238#endif
13239#ifdef FEAT_XIM
13240 "xim",
13241#endif
13242#ifdef FEAT_XFONTSET
13243 "xfontset",
13244#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013245#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013246 "xpm",
13247 "xpm_w32", /* for backward compatibility */
13248#else
13249# if defined(HAVE_XPM)
13250 "xpm",
13251# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253#ifdef USE_XSMP
13254 "xsmp",
13255#endif
13256#ifdef USE_XSMP_INTERACT
13257 "xsmp_interact",
13258#endif
13259#ifdef FEAT_XCLIPBOARD
13260 "xterm_clipboard",
13261#endif
13262#ifdef FEAT_XTERM_SAVE
13263 "xterm_save",
13264#endif
13265#if defined(UNIX) && defined(FEAT_X11)
13266 "X11",
13267#endif
13268 NULL
13269 };
13270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013271 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 for (i = 0; has_list[i] != NULL; ++i)
13273 if (STRICMP(name, has_list[i]) == 0)
13274 {
13275 n = TRUE;
13276 break;
13277 }
13278
13279 if (n == FALSE)
13280 {
13281 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013282 {
13283 if (name[5] == '-'
13284 && STRLEN(name) > 11
13285 && vim_isdigit(name[6])
13286 && vim_isdigit(name[8])
13287 && vim_isdigit(name[10]))
13288 {
13289 int major = atoi((char *)name + 6);
13290 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013291
13292 /* Expect "patch-9.9.01234". */
13293 n = (major < VIM_VERSION_MAJOR
13294 || (major == VIM_VERSION_MAJOR
13295 && (minor < VIM_VERSION_MINOR
13296 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013297 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013298 }
13299 else
13300 n = has_patch(atoi((char *)name + 5));
13301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 else if (STRICMP(name, "vim_starting") == 0)
13303 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013304#ifdef FEAT_MBYTE
13305 else if (STRICMP(name, "multi_byte_encoding") == 0)
13306 n = has_mbyte;
13307#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013308#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13309 else if (STRICMP(name, "balloon_multiline") == 0)
13310 n = multiline_balloon_available();
13311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312#ifdef DYNAMIC_TCL
13313 else if (STRICMP(name, "tcl") == 0)
13314 n = tcl_enabled(FALSE);
13315#endif
13316#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13317 else if (STRICMP(name, "iconv") == 0)
13318 n = iconv_enabled(FALSE);
13319#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013320#ifdef DYNAMIC_LUA
13321 else if (STRICMP(name, "lua") == 0)
13322 n = lua_enabled(FALSE);
13323#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013324#ifdef DYNAMIC_MZSCHEME
13325 else if (STRICMP(name, "mzscheme") == 0)
13326 n = mzscheme_enabled(FALSE);
13327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328#ifdef DYNAMIC_RUBY
13329 else if (STRICMP(name, "ruby") == 0)
13330 n = ruby_enabled(FALSE);
13331#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013332#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333#ifdef DYNAMIC_PYTHON
13334 else if (STRICMP(name, "python") == 0)
13335 n = python_enabled(FALSE);
13336#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013337#endif
13338#ifdef FEAT_PYTHON3
13339#ifdef DYNAMIC_PYTHON3
13340 else if (STRICMP(name, "python3") == 0)
13341 n = python3_enabled(FALSE);
13342#endif
13343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344#ifdef DYNAMIC_PERL
13345 else if (STRICMP(name, "perl") == 0)
13346 n = perl_enabled(FALSE);
13347#endif
13348#ifdef FEAT_GUI
13349 else if (STRICMP(name, "gui_running") == 0)
13350 n = (gui.in_use || gui.starting);
13351# ifdef FEAT_GUI_W32
13352 else if (STRICMP(name, "gui_win32s") == 0)
13353 n = gui_is_win32s();
13354# endif
13355# ifdef FEAT_BROWSE
13356 else if (STRICMP(name, "browse") == 0)
13357 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13358# endif
13359#endif
13360#ifdef FEAT_SYN_HL
13361 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013362 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363#endif
13364#if defined(WIN3264)
13365 else if (STRICMP(name, "win95") == 0)
13366 n = mch_windows95();
13367#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013368#ifdef FEAT_NETBEANS_INTG
13369 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013370 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 }
13373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013374 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375}
13376
13377/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013378 * "has_key()" function
13379 */
13380 static void
13381f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 typval_T *argvars;
13383 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013384{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013385 if (argvars[0].v_type != VAR_DICT)
13386 {
13387 EMSG(_(e_dictreq));
13388 return;
13389 }
13390 if (argvars[0].vval.v_dict == NULL)
13391 return;
13392
13393 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013394 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013395}
13396
13397/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013398 * "haslocaldir()" function
13399 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013400 static void
13401f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013402 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013403 typval_T *rettv;
13404{
13405 rettv->vval.v_number = (curwin->w_localdir != NULL);
13406}
13407
13408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 * "hasmapto()" function
13410 */
13411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013412f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013413 typval_T *argvars;
13414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415{
13416 char_u *name;
13417 char_u *mode;
13418 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013419 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013422 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 mode = (char_u *)"nvo";
13424 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013426 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013427 if (argvars[2].v_type != VAR_UNKNOWN)
13428 abbr = get_tv_number(&argvars[2]);
13429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430
Bram Moolenaar2c932302006-03-18 21:42:09 +000013431 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013432 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435}
13436
13437/*
13438 * "histadd()" function
13439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013442 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444{
13445#ifdef FEAT_CMDHIST
13446 int histype;
13447 char_u *str;
13448 char_u buf[NUMBUFLEN];
13449#endif
13450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013451 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 if (check_restricted() || check_secure())
13453 return;
13454#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13456 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457 if (histype >= 0)
13458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 if (*str != NUL)
13461 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013462 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013464 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465 return;
13466 }
13467 }
13468#endif
13469}
13470
13471/*
13472 * "histdel()" function
13473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013475f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013476 typval_T *argvars UNUSED;
13477 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478{
13479#ifdef FEAT_CMDHIST
13480 int n;
13481 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013482 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013484 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13485 if (str == NULL)
13486 n = 0;
13487 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013489 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013490 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013492 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 else
13495 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013497 get_tv_string_buf(&argvars[1], buf));
13498 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499#endif
13500}
13501
13502/*
13503 * "histget()" function
13504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013506f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013507 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509{
13510#ifdef FEAT_CMDHIST
13511 int type;
13512 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013515 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13516 if (str == NULL)
13517 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013519 {
13520 type = get_histtype(str);
13521 if (argvars[1].v_type == VAR_UNKNOWN)
13522 idx = get_history_idx(type);
13523 else
13524 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13525 /* -1 on type error */
13526 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013529 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013531 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532}
13533
13534/*
13535 * "histnr()" function
13536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013538f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013539 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541{
13542 int i;
13543
13544#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013545 char_u *history = get_tv_string_chk(&argvars[0]);
13546
13547 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 if (i >= HIST_CMD && i < HIST_COUNT)
13549 i = get_history_idx(i);
13550 else
13551#endif
13552 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554}
13555
13556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557 * "highlightID(name)" function
13558 */
13559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013560f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013561 typval_T *argvars;
13562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013564 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565}
13566
13567/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013568 * "highlight_exists()" function
13569 */
13570 static void
13571f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013572 typval_T *argvars;
13573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013574{
13575 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13576}
13577
13578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 * "hostname()" function
13580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013583 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585{
13586 char_u hostname[256];
13587
13588 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->v_type = VAR_STRING;
13590 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591}
13592
13593/*
13594 * iconv() function
13595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013598 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600{
13601#ifdef FEAT_MBYTE
13602 char_u buf1[NUMBUFLEN];
13603 char_u buf2[NUMBUFLEN];
13604 char_u *from, *to, *str;
13605 vimconv_T vimconv;
13606#endif
13607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608 rettv->v_type = VAR_STRING;
13609 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610
13611#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612 str = get_tv_string(&argvars[0]);
13613 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13614 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 vimconv.vc_type = CONV_NONE;
13616 convert_setup(&vimconv, from, to);
13617
13618 /* If the encodings are equal, no conversion needed. */
13619 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623
13624 convert_setup(&vimconv, NULL, NULL);
13625 vim_free(from);
13626 vim_free(to);
13627#endif
13628}
13629
13630/*
13631 * "indent()" function
13632 */
13633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 typval_T *argvars;
13636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637{
13638 linenr_T lnum;
13639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645}
13646
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013647/*
13648 * "index()" function
13649 */
13650 static void
13651f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013652 typval_T *argvars;
13653 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013654{
Bram Moolenaar33570922005-01-25 22:26:29 +000013655 list_T *l;
13656 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013657 long idx = 0;
13658 int ic = FALSE;
13659
13660 rettv->vval.v_number = -1;
13661 if (argvars[0].v_type != VAR_LIST)
13662 {
13663 EMSG(_(e_listreq));
13664 return;
13665 }
13666 l = argvars[0].vval.v_list;
13667 if (l != NULL)
13668 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013669 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013670 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013672 int error = FALSE;
13673
Bram Moolenaar758711c2005-02-02 23:11:38 +000013674 /* Start at specified item. Use the cached index that list_find()
13675 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013676 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013677 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013678 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013679 ic = get_tv_number_chk(&argvars[3], &error);
13680 if (error)
13681 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013682 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013683
Bram Moolenaar758711c2005-02-02 23:11:38 +000013684 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013685 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013686 {
13687 rettv->vval.v_number = idx;
13688 break;
13689 }
13690 }
13691}
13692
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693static int inputsecret_flag = 0;
13694
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013695static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13696
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013698 * This function is used by f_input() and f_inputdialog() functions. The third
13699 * argument to f_input() specifies the type of completion to use at the
13700 * prompt. The third argument to f_inputdialog() specifies the value to return
13701 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 */
13703 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013704get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013705 typval_T *argvars;
13706 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013707 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013709 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 char_u *p = NULL;
13711 int c;
13712 char_u buf[NUMBUFLEN];
13713 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013714 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013715 int xp_type = EXPAND_NOTHING;
13716 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013719 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720
13721#ifdef NO_CONSOLE_INPUT
13722 /* While starting up, there is no place to enter text. */
13723 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725#endif
13726
13727 cmd_silent = FALSE; /* Want to see the prompt. */
13728 if (prompt != NULL)
13729 {
13730 /* Only the part of the message after the last NL is considered as
13731 * prompt for the command line */
13732 p = vim_strrchr(prompt, '\n');
13733 if (p == NULL)
13734 p = prompt;
13735 else
13736 {
13737 ++p;
13738 c = *p;
13739 *p = NUL;
13740 msg_start();
13741 msg_clr_eos();
13742 msg_puts_attr(prompt, echo_attr);
13743 msg_didout = FALSE;
13744 msg_starthere();
13745 *p = c;
13746 }
13747 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013749 if (argvars[1].v_type != VAR_UNKNOWN)
13750 {
13751 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13752 if (defstr != NULL)
13753 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013755 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013756 {
13757 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013758 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013759 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013760
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013761 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013762 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013763
Bram Moolenaar4463f292005-09-25 22:20:24 +000013764 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13765 if (xp_name == NULL)
13766 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013767
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013768 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013769
Bram Moolenaar4463f292005-09-25 22:20:24 +000013770 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13771 &xp_arg) == FAIL)
13772 return;
13773 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013774 }
13775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013776 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013777 {
13778# ifdef FEAT_EX_EXTRA
13779 int save_ex_normal_busy = ex_normal_busy;
13780 ex_normal_busy = 0;
13781# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013782 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013783 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13784 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013785# ifdef FEAT_EX_EXTRA
13786 ex_normal_busy = save_ex_normal_busy;
13787# endif
13788 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013789 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013790 && argvars[1].v_type != VAR_UNKNOWN
13791 && argvars[2].v_type != VAR_UNKNOWN)
13792 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13793 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013794
13795 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013797 /* since the user typed this, no need to wait for return */
13798 need_wait_return = FALSE;
13799 msg_didout = FALSE;
13800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801 cmd_silent = cmd_silent_save;
13802}
13803
13804/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013805 * "input()" function
13806 * Also handles inputsecret() when inputsecret is set.
13807 */
13808 static void
13809f_input(argvars, rettv)
13810 typval_T *argvars;
13811 typval_T *rettv;
13812{
13813 get_user_input(argvars, rettv, FALSE);
13814}
13815
13816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 * "inputdialog()" function
13818 */
13819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013821 typval_T *argvars;
13822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823{
13824#if defined(FEAT_GUI_TEXTDIALOG)
13825 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13826 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13827 {
13828 char_u *message;
13829 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013830 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013832 message = get_tv_string_chk(&argvars[0]);
13833 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013834 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013835 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836 else
13837 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013838 if (message != NULL && defstr != NULL
13839 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013840 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 else
13843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013844 if (message != NULL && defstr != NULL
13845 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013846 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013847 rettv->vval.v_string = vim_strsave(
13848 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013850 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013852 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 }
13854 else
13855#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013856 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857}
13858
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013859/*
13860 * "inputlist()" function
13861 */
13862 static void
13863f_inputlist(argvars, rettv)
13864 typval_T *argvars;
13865 typval_T *rettv;
13866{
13867 listitem_T *li;
13868 int selected;
13869 int mouse_used;
13870
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013871#ifdef NO_CONSOLE_INPUT
13872 /* While starting up, there is no place to enter text. */
13873 if (no_console_input())
13874 return;
13875#endif
13876 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13877 {
13878 EMSG2(_(e_listarg), "inputlist()");
13879 return;
13880 }
13881
13882 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013883 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013884 lines_left = Rows; /* avoid more prompt */
13885 msg_scroll = TRUE;
13886 msg_clr_eos();
13887
13888 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13889 {
13890 msg_puts(get_tv_string(&li->li_tv));
13891 msg_putchar('\n');
13892 }
13893
13894 /* Ask for choice. */
13895 selected = prompt_for_number(&mouse_used);
13896 if (mouse_used)
13897 selected -= lines_left;
13898
13899 rettv->vval.v_number = selected;
13900}
13901
13902
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13904
13905/*
13906 * "inputrestore()" function
13907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013910 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912{
13913 if (ga_userinput.ga_len > 0)
13914 {
13915 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13917 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013918 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 }
13920 else if (p_verbose > 1)
13921 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013922 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013923 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924 }
13925}
13926
13927/*
13928 * "inputsave()" function
13929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013931f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013932 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013935 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 if (ga_grow(&ga_userinput, 1) == OK)
13937 {
13938 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13939 + ga_userinput.ga_len);
13940 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013941 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942 }
13943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013944 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945}
13946
13947/*
13948 * "inputsecret()" function
13949 */
13950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013951f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013952 typval_T *argvars;
13953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954{
13955 ++cmdline_star;
13956 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013957 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 --cmdline_star;
13959 --inputsecret_flag;
13960}
13961
13962/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013963 * "insert()" function
13964 */
13965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013966f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013967 typval_T *argvars;
13968 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013969{
13970 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013971 listitem_T *item;
13972 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013973 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013974
13975 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013976 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013977 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013978 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013979 {
13980 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013981 before = get_tv_number_chk(&argvars[2], &error);
13982 if (error)
13983 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013984
Bram Moolenaar758711c2005-02-02 23:11:38 +000013985 if (before == l->lv_len)
13986 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013987 else
13988 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013989 item = list_find(l, before);
13990 if (item == NULL)
13991 {
13992 EMSGN(_(e_listidx), before);
13993 l = NULL;
13994 }
13995 }
13996 if (l != NULL)
13997 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013998 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013999 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014000 }
14001 }
14002}
14003
14004/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014005 * "invert(expr)" function
14006 */
14007 static void
14008f_invert(argvars, rettv)
14009 typval_T *argvars;
14010 typval_T *rettv;
14011{
14012 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14013}
14014
14015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 * "isdirectory()" function
14017 */
14018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014020 typval_T *argvars;
14021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024}
14025
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014026/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014027 * "islocked()" function
14028 */
14029 static void
14030f_islocked(argvars, rettv)
14031 typval_T *argvars;
14032 typval_T *rettv;
14033{
14034 lval_T lv;
14035 char_u *end;
14036 dictitem_T *di;
14037
14038 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014039 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14040 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014041 if (end != NULL && lv.ll_name != NULL)
14042 {
14043 if (*end != NUL)
14044 EMSG(_(e_trailing));
14045 else
14046 {
14047 if (lv.ll_tv == NULL)
14048 {
14049 if (check_changedtick(lv.ll_name))
14050 rettv->vval.v_number = 1; /* always locked */
14051 else
14052 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014053 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014054 if (di != NULL)
14055 {
14056 /* Consider a variable locked when:
14057 * 1. the variable itself is locked
14058 * 2. the value of the variable is locked.
14059 * 3. the List or Dict value is locked.
14060 */
14061 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14062 || tv_islocked(&di->di_tv));
14063 }
14064 }
14065 }
14066 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014067 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014068 else if (lv.ll_newkey != NULL)
14069 EMSG2(_(e_dictkey), lv.ll_newkey);
14070 else if (lv.ll_list != NULL)
14071 /* List item. */
14072 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14073 else
14074 /* Dictionary item. */
14075 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14076 }
14077 }
14078
14079 clear_lval(&lv);
14080}
14081
Bram Moolenaar33570922005-01-25 22:26:29 +000014082static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014083
14084/*
14085 * Turn a dict into a list:
14086 * "what" == 0: list of keys
14087 * "what" == 1: list of values
14088 * "what" == 2: list of items
14089 */
14090 static void
14091dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014092 typval_T *argvars;
14093 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014094 int what;
14095{
Bram Moolenaar33570922005-01-25 22:26:29 +000014096 list_T *l2;
14097 dictitem_T *di;
14098 hashitem_T *hi;
14099 listitem_T *li;
14100 listitem_T *li2;
14101 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014102 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014103
Bram Moolenaar8c711452005-01-14 21:53:12 +000014104 if (argvars[0].v_type != VAR_DICT)
14105 {
14106 EMSG(_(e_dictreq));
14107 return;
14108 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014109 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014110 return;
14111
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014112 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014113 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014114
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014115 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014116 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014117 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014118 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014119 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014120 --todo;
14121 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014122
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014123 li = listitem_alloc();
14124 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014125 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014126 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014127
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014128 if (what == 0)
14129 {
14130 /* keys() */
14131 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014132 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014133 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14134 }
14135 else if (what == 1)
14136 {
14137 /* values() */
14138 copy_tv(&di->di_tv, &li->li_tv);
14139 }
14140 else
14141 {
14142 /* items() */
14143 l2 = list_alloc();
14144 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014145 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014146 li->li_tv.vval.v_list = l2;
14147 if (l2 == NULL)
14148 break;
14149 ++l2->lv_refcount;
14150
14151 li2 = listitem_alloc();
14152 if (li2 == NULL)
14153 break;
14154 list_append(l2, li2);
14155 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014156 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014157 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14158
14159 li2 = listitem_alloc();
14160 if (li2 == NULL)
14161 break;
14162 list_append(l2, li2);
14163 copy_tv(&di->di_tv, &li2->li_tv);
14164 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014165 }
14166 }
14167}
14168
14169/*
14170 * "items(dict)" function
14171 */
14172 static void
14173f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014174 typval_T *argvars;
14175 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014176{
14177 dict_list(argvars, rettv, 2);
14178}
14179
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014181 * "join()" function
14182 */
14183 static void
14184f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014185 typval_T *argvars;
14186 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014187{
14188 garray_T ga;
14189 char_u *sep;
14190
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014191 if (argvars[0].v_type != VAR_LIST)
14192 {
14193 EMSG(_(e_listreq));
14194 return;
14195 }
14196 if (argvars[0].vval.v_list == NULL)
14197 return;
14198 if (argvars[1].v_type == VAR_UNKNOWN)
14199 sep = (char_u *)" ";
14200 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014201 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014202
14203 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204
14205 if (sep != NULL)
14206 {
14207 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014208 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209 ga_append(&ga, NUL);
14210 rettv->vval.v_string = (char_u *)ga.ga_data;
14211 }
14212 else
14213 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014214}
14215
14216/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014217 * "keys()" function
14218 */
14219 static void
14220f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014221 typval_T *argvars;
14222 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014223{
14224 dict_list(argvars, rettv, 0);
14225}
14226
14227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 * "last_buffer_nr()" function.
14229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014231f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014232 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234{
14235 int n = 0;
14236 buf_T *buf;
14237
14238 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14239 if (n < buf->b_fnum)
14240 n = buf->b_fnum;
14241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014243}
14244
14245/*
14246 * "len()" function
14247 */
14248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014249f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014250 typval_T *argvars;
14251 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014252{
14253 switch (argvars[0].v_type)
14254 {
14255 case VAR_STRING:
14256 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014257 rettv->vval.v_number = (varnumber_T)STRLEN(
14258 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014259 break;
14260 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014261 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014262 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014263 case VAR_DICT:
14264 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14265 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014266 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014267 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014268 break;
14269 }
14270}
14271
Bram Moolenaar33570922005-01-25 22:26:29 +000014272static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014273
14274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014276 typval_T *argvars;
14277 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014278 int type;
14279{
14280#ifdef FEAT_LIBCALL
14281 char_u *string_in;
14282 char_u **string_result;
14283 int nr_result;
14284#endif
14285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014287 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014289
14290 if (check_restricted() || check_secure())
14291 return;
14292
14293#ifdef FEAT_LIBCALL
14294 /* The first two args must be strings, otherwise its meaningless */
14295 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14296 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014297 string_in = NULL;
14298 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014299 string_in = argvars[2].vval.v_string;
14300 if (type == VAR_NUMBER)
14301 string_result = NULL;
14302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014303 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014304 if (mch_libcall(argvars[0].vval.v_string,
14305 argvars[1].vval.v_string,
14306 string_in,
14307 argvars[2].vval.v_number,
14308 string_result,
14309 &nr_result) == OK
14310 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014312 }
14313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314}
14315
14316/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014317 * "libcall()" function
14318 */
14319 static void
14320f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014321 typval_T *argvars;
14322 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014323{
14324 libcall_common(argvars, rettv, VAR_STRING);
14325}
14326
14327/*
14328 * "libcallnr()" function
14329 */
14330 static void
14331f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014332 typval_T *argvars;
14333 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014334{
14335 libcall_common(argvars, rettv, VAR_NUMBER);
14336}
14337
14338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 * "line(string)" function
14340 */
14341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014342f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014343 typval_T *argvars;
14344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345{
14346 linenr_T lnum = 0;
14347 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014348 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014350 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 if (fp != NULL)
14352 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354}
14355
14356/*
14357 * "line2byte(lnum)" function
14358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014360f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014361 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363{
14364#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366#else
14367 linenr_T lnum;
14368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014369 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014371 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014373 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14374 if (rettv->vval.v_number >= 0)
14375 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376#endif
14377}
14378
14379/*
14380 * "lispindent(lnum)" function
14381 */
14382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014383f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014384 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386{
14387#ifdef FEAT_LISP
14388 pos_T pos;
14389 linenr_T lnum;
14390
14391 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014392 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14394 {
14395 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014396 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 curwin->w_cursor = pos;
14398 }
14399 else
14400#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014401 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402}
14403
14404/*
14405 * "localtime()" function
14406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014408f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014409 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014412 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413}
14414
Bram Moolenaar33570922005-01-25 22:26:29 +000014415static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416
14417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014419 typval_T *argvars;
14420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 int exact;
14422{
14423 char_u *keys;
14424 char_u *which;
14425 char_u buf[NUMBUFLEN];
14426 char_u *keys_buf = NULL;
14427 char_u *rhs;
14428 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014429 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014430 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014431 mapblock_T *mp;
14432 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433
14434 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014435 rettv->v_type = VAR_STRING;
14436 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014438 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439 if (*keys == NUL)
14440 return;
14441
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014442 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014444 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014445 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014446 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014447 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014448 if (argvars[3].v_type != VAR_UNKNOWN)
14449 get_dict = get_tv_number(&argvars[3]);
14450 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 else
14453 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014454 if (which == NULL)
14455 return;
14456
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 mode = get_map_mode(&which, 0);
14458
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014459 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014460 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014462
14463 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014465 /* Return a string. */
14466 if (rhs != NULL)
14467 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468
Bram Moolenaarbd743252010-10-20 21:23:33 +020014469 }
14470 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14471 {
14472 /* Return a dictionary. */
14473 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14474 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14475 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476
Bram Moolenaarbd743252010-10-20 21:23:33 +020014477 dict_add_nr_str(dict, "lhs", 0L, lhs);
14478 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14479 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14480 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14481 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14482 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14483 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014484 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014485 dict_add_nr_str(dict, "mode", 0L, mapmode);
14486
14487 vim_free(lhs);
14488 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 }
14490}
14491
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014492#ifdef FEAT_FLOAT
14493/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014494 * "log()" function
14495 */
14496 static void
14497f_log(argvars, rettv)
14498 typval_T *argvars;
14499 typval_T *rettv;
14500{
14501 float_T f;
14502
14503 rettv->v_type = VAR_FLOAT;
14504 if (get_float_arg(argvars, &f) == OK)
14505 rettv->vval.v_float = log(f);
14506 else
14507 rettv->vval.v_float = 0.0;
14508}
14509
14510/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014511 * "log10()" function
14512 */
14513 static void
14514f_log10(argvars, rettv)
14515 typval_T *argvars;
14516 typval_T *rettv;
14517{
14518 float_T f;
14519
14520 rettv->v_type = VAR_FLOAT;
14521 if (get_float_arg(argvars, &f) == OK)
14522 rettv->vval.v_float = log10(f);
14523 else
14524 rettv->vval.v_float = 0.0;
14525}
14526#endif
14527
Bram Moolenaar1dced572012-04-05 16:54:08 +020014528#ifdef FEAT_LUA
14529/*
14530 * "luaeval()" function
14531 */
14532 static void
14533f_luaeval(argvars, rettv)
14534 typval_T *argvars;
14535 typval_T *rettv;
14536{
14537 char_u *str;
14538 char_u buf[NUMBUFLEN];
14539
14540 str = get_tv_string_buf(&argvars[0], buf);
14541 do_luaeval(str, argvars + 1, rettv);
14542}
14543#endif
14544
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014546 * "map()" function
14547 */
14548 static void
14549f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014550 typval_T *argvars;
14551 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014552{
14553 filter_map(argvars, rettv, TRUE);
14554}
14555
14556/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014557 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 */
14559 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014560f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014561 typval_T *argvars;
14562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014564 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565}
14566
14567/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014568 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 */
14570 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014571f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014572 typval_T *argvars;
14573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014575 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576}
14577
Bram Moolenaar33570922005-01-25 22:26:29 +000014578static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579
14580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014581find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014582 typval_T *argvars;
14583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 int type;
14585{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014586 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014587 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014588 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 char_u *pat;
14590 regmatch_T regmatch;
14591 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014592 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 char_u *save_cpo;
14594 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014595 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014596 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014597 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014598 list_T *l = NULL;
14599 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014600 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014601 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602
14603 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14604 save_cpo = p_cpo;
14605 p_cpo = (char_u *)"";
14606
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014607 rettv->vval.v_number = -1;
14608 if (type == 3)
14609 {
14610 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014611 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014612 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014613 }
14614 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014616 rettv->v_type = VAR_STRING;
14617 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014620 if (argvars[0].v_type == VAR_LIST)
14621 {
14622 if ((l = argvars[0].vval.v_list) == NULL)
14623 goto theend;
14624 li = l->lv_first;
14625 }
14626 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014627 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014628 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014629 len = (long)STRLEN(str);
14630 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014632 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14633 if (pat == NULL)
14634 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014635
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014636 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014638 int error = FALSE;
14639
14640 start = get_tv_number_chk(&argvars[2], &error);
14641 if (error)
14642 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014643 if (l != NULL)
14644 {
14645 li = list_find(l, start);
14646 if (li == NULL)
14647 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014648 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014649 }
14650 else
14651 {
14652 if (start < 0)
14653 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014654 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014655 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014656 /* When "count" argument is there ignore matches before "start",
14657 * otherwise skip part of the string. Differs when pattern is "^"
14658 * or "\<". */
14659 if (argvars[3].v_type != VAR_UNKNOWN)
14660 startcol = start;
14661 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014662 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014663 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014664 len -= start;
14665 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014666 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014667
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014668 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014669 nth = get_tv_number_chk(&argvars[3], &error);
14670 if (error)
14671 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 }
14673
14674 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14675 if (regmatch.regprog != NULL)
14676 {
14677 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014678
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014679 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014680 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014681 if (l != NULL)
14682 {
14683 if (li == NULL)
14684 {
14685 match = FALSE;
14686 break;
14687 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014688 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014689 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014690 if (str == NULL)
14691 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014692 }
14693
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014694 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014695
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014696 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014697 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014698 if (l == NULL && !match)
14699 break;
14700
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014701 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014702 if (l != NULL)
14703 {
14704 li = li->li_next;
14705 ++idx;
14706 }
14707 else
14708 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014709#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014710 startcol = (colnr_T)(regmatch.startp[0]
14711 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014712#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014713 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014714#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014715 if (startcol > (colnr_T)len
14716 || str + startcol <= regmatch.startp[0])
14717 {
14718 match = FALSE;
14719 break;
14720 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014721 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014722 }
14723
14724 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014726 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014727 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014728 int i;
14729
14730 /* return list with matched string and submatches */
14731 for (i = 0; i < NSUBEXP; ++i)
14732 {
14733 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014734 {
14735 if (list_append_string(rettv->vval.v_list,
14736 (char_u *)"", 0) == FAIL)
14737 break;
14738 }
14739 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014740 regmatch.startp[i],
14741 (int)(regmatch.endp[i] - regmatch.startp[i]))
14742 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014743 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014744 }
14745 }
14746 else if (type == 2)
14747 {
14748 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014749 if (l != NULL)
14750 copy_tv(&li->li_tv, rettv);
14751 else
14752 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014754 }
14755 else if (l != NULL)
14756 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 else
14758 {
14759 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014760 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 (varnumber_T)(regmatch.startp[0] - str);
14762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014763 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014765 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766 }
14767 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014768 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 }
14770
14771theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014772 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 p_cpo = save_cpo;
14774}
14775
14776/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014777 * "match()" function
14778 */
14779 static void
14780f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014781 typval_T *argvars;
14782 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014783{
14784 find_some_match(argvars, rettv, 1);
14785}
14786
14787/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014788 * "matchadd()" function
14789 */
14790 static void
14791f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014792 typval_T *argvars UNUSED;
14793 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014794{
14795#ifdef FEAT_SEARCH_EXTRA
14796 char_u buf[NUMBUFLEN];
14797 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14798 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14799 int prio = 10; /* default priority */
14800 int id = -1;
14801 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014802 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014803
14804 rettv->vval.v_number = -1;
14805
14806 if (grp == NULL || pat == NULL)
14807 return;
14808 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014809 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014810 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014811 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014812 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014813 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014814 if (argvars[4].v_type != VAR_UNKNOWN)
14815 {
14816 if (argvars[4].v_type != VAR_DICT)
14817 {
14818 EMSG(_(e_dictreq));
14819 return;
14820 }
14821 if (dict_find(argvars[4].vval.v_dict,
14822 (char_u *)"conceal", -1) != NULL)
14823 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14824 (char_u *)"conceal", FALSE);
14825 }
14826 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014827 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014828 if (error == TRUE)
14829 return;
14830 if (id >= 1 && id <= 3)
14831 {
14832 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14833 return;
14834 }
14835
Bram Moolenaar6561d522015-07-21 15:48:27 +020014836 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14837 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014838#endif
14839}
14840
14841/*
14842 * "matchaddpos()" function
14843 */
14844 static void
14845f_matchaddpos(argvars, rettv)
14846 typval_T *argvars UNUSED;
14847 typval_T *rettv UNUSED;
14848{
14849#ifdef FEAT_SEARCH_EXTRA
14850 char_u buf[NUMBUFLEN];
14851 char_u *group;
14852 int prio = 10;
14853 int id = -1;
14854 int error = FALSE;
14855 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014856 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014857
14858 rettv->vval.v_number = -1;
14859
14860 group = get_tv_string_buf_chk(&argvars[0], buf);
14861 if (group == NULL)
14862 return;
14863
14864 if (argvars[1].v_type != VAR_LIST)
14865 {
14866 EMSG2(_(e_listarg), "matchaddpos()");
14867 return;
14868 }
14869 l = argvars[1].vval.v_list;
14870 if (l == NULL)
14871 return;
14872
14873 if (argvars[2].v_type != VAR_UNKNOWN)
14874 {
14875 prio = get_tv_number_chk(&argvars[2], &error);
14876 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014877 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014878 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014879 if (argvars[4].v_type != VAR_UNKNOWN)
14880 {
14881 if (argvars[4].v_type != VAR_DICT)
14882 {
14883 EMSG(_(e_dictreq));
14884 return;
14885 }
14886 if (dict_find(argvars[4].vval.v_dict,
14887 (char_u *)"conceal", -1) != NULL)
14888 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14889 (char_u *)"conceal", FALSE);
14890 }
14891 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014892 }
14893 if (error == TRUE)
14894 return;
14895
14896 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14897 if (id == 1 || id == 2)
14898 {
14899 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14900 return;
14901 }
14902
Bram Moolenaar6561d522015-07-21 15:48:27 +020014903 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14904 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014905#endif
14906}
14907
14908/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014909 * "matcharg()" function
14910 */
14911 static void
14912f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014913 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014914 typval_T *rettv;
14915{
14916 if (rettv_list_alloc(rettv) == OK)
14917 {
14918#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014919 int id = get_tv_number(&argvars[0]);
14920 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014921
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014922 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014923 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014924 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14925 {
14926 list_append_string(rettv->vval.v_list,
14927 syn_id2name(m->hlg_id), -1);
14928 list_append_string(rettv->vval.v_list, m->pattern, -1);
14929 }
14930 else
14931 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014932 list_append_string(rettv->vval.v_list, NULL, -1);
14933 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014934 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014935 }
14936#endif
14937 }
14938}
14939
14940/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014941 * "matchdelete()" function
14942 */
14943 static void
14944f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014945 typval_T *argvars UNUSED;
14946 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014947{
14948#ifdef FEAT_SEARCH_EXTRA
14949 rettv->vval.v_number = match_delete(curwin,
14950 (int)get_tv_number(&argvars[0]), TRUE);
14951#endif
14952}
14953
14954/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955 * "matchend()" function
14956 */
14957 static void
14958f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014959 typval_T *argvars;
14960 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014961{
14962 find_some_match(argvars, rettv, 0);
14963}
14964
14965/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014966 * "matchlist()" function
14967 */
14968 static void
14969f_matchlist(argvars, rettv)
14970 typval_T *argvars;
14971 typval_T *rettv;
14972{
14973 find_some_match(argvars, rettv, 3);
14974}
14975
14976/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977 * "matchstr()" function
14978 */
14979 static void
14980f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014981 typval_T *argvars;
14982 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014983{
14984 find_some_match(argvars, rettv, 2);
14985}
14986
Bram Moolenaar33570922005-01-25 22:26:29 +000014987static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014988
14989 static void
14990max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014991 typval_T *argvars;
14992 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014993 int domax;
14994{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014995 long n = 0;
14996 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014997 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014998
14999 if (argvars[0].v_type == VAR_LIST)
15000 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015001 list_T *l;
15002 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015003
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015004 l = argvars[0].vval.v_list;
15005 if (l != NULL)
15006 {
15007 li = l->lv_first;
15008 if (li != NULL)
15009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015010 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015011 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015012 {
15013 li = li->li_next;
15014 if (li == NULL)
15015 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015016 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015017 if (domax ? i > n : i < n)
15018 n = i;
15019 }
15020 }
15021 }
15022 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015023 else if (argvars[0].v_type == VAR_DICT)
15024 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015025 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015026 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015027 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015028 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015029
15030 d = argvars[0].vval.v_dict;
15031 if (d != NULL)
15032 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015033 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015034 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015035 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015036 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015037 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015038 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015039 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015040 if (first)
15041 {
15042 n = i;
15043 first = FALSE;
15044 }
15045 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015046 n = i;
15047 }
15048 }
15049 }
15050 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015051 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015052 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015053 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015054}
15055
15056/*
15057 * "max()" function
15058 */
15059 static void
15060f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015061 typval_T *argvars;
15062 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015063{
15064 max_min(argvars, rettv, TRUE);
15065}
15066
15067/*
15068 * "min()" function
15069 */
15070 static void
15071f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015072 typval_T *argvars;
15073 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015074{
15075 max_min(argvars, rettv, FALSE);
15076}
15077
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015078static int mkdir_recurse __ARGS((char_u *dir, int prot));
15079
15080/*
15081 * Create the directory in which "dir" is located, and higher levels when
15082 * needed.
15083 */
15084 static int
15085mkdir_recurse(dir, prot)
15086 char_u *dir;
15087 int prot;
15088{
15089 char_u *p;
15090 char_u *updir;
15091 int r = FAIL;
15092
15093 /* Get end of directory name in "dir".
15094 * We're done when it's "/" or "c:/". */
15095 p = gettail_sep(dir);
15096 if (p <= get_past_head(dir))
15097 return OK;
15098
15099 /* If the directory exists we're done. Otherwise: create it.*/
15100 updir = vim_strnsave(dir, (int)(p - dir));
15101 if (updir == NULL)
15102 return FAIL;
15103 if (mch_isdir(updir))
15104 r = OK;
15105 else if (mkdir_recurse(updir, prot) == OK)
15106 r = vim_mkdir_emsg(updir, prot);
15107 vim_free(updir);
15108 return r;
15109}
15110
15111#ifdef vim_mkdir
15112/*
15113 * "mkdir()" function
15114 */
15115 static void
15116f_mkdir(argvars, rettv)
15117 typval_T *argvars;
15118 typval_T *rettv;
15119{
15120 char_u *dir;
15121 char_u buf[NUMBUFLEN];
15122 int prot = 0755;
15123
15124 rettv->vval.v_number = FAIL;
15125 if (check_restricted() || check_secure())
15126 return;
15127
15128 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015129 if (*dir == NUL)
15130 rettv->vval.v_number = FAIL;
15131 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015132 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015133 if (*gettail(dir) == NUL)
15134 /* remove trailing slashes */
15135 *gettail_sep(dir) = NUL;
15136
15137 if (argvars[1].v_type != VAR_UNKNOWN)
15138 {
15139 if (argvars[2].v_type != VAR_UNKNOWN)
15140 prot = get_tv_number_chk(&argvars[2], NULL);
15141 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15142 mkdir_recurse(dir, prot);
15143 }
15144 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015145 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015146}
15147#endif
15148
Bram Moolenaar0d660222005-01-07 21:51:51 +000015149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 * "mode()" function
15151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015153f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015154 typval_T *argvars;
15155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015157 char_u buf[3];
15158
15159 buf[1] = NUL;
15160 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162 if (VIsual_active)
15163 {
15164 if (VIsual_select)
15165 buf[0] = VIsual_mode + 's' - 'v';
15166 else
15167 buf[0] = VIsual_mode;
15168 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015169 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015170 || State == CONFIRM)
15171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015173 if (State == ASKMORE)
15174 buf[1] = 'm';
15175 else if (State == CONFIRM)
15176 buf[1] = '?';
15177 }
15178 else if (State == EXTERNCMD)
15179 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 else if (State & INSERT)
15181 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015182#ifdef FEAT_VREPLACE
15183 if (State & VREPLACE_FLAG)
15184 {
15185 buf[0] = 'R';
15186 buf[1] = 'v';
15187 }
15188 else
15189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190 if (State & REPLACE_FLAG)
15191 buf[0] = 'R';
15192 else
15193 buf[0] = 'i';
15194 }
15195 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015196 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015198 if (exmode_active)
15199 buf[1] = 'v';
15200 }
15201 else if (exmode_active)
15202 {
15203 buf[0] = 'c';
15204 buf[1] = 'e';
15205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015209 if (finish_op)
15210 buf[1] = 'o';
15211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015213 /* Clear out the minor mode when the argument is not a non-zero number or
15214 * non-empty string. */
15215 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015216 buf[1] = NUL;
15217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015218 rettv->vval.v_string = vim_strsave(buf);
15219 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220}
15221
Bram Moolenaar429fa852013-04-15 12:27:36 +020015222#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015223/*
15224 * "mzeval()" function
15225 */
15226 static void
15227f_mzeval(argvars, rettv)
15228 typval_T *argvars;
15229 typval_T *rettv;
15230{
15231 char_u *str;
15232 char_u buf[NUMBUFLEN];
15233
15234 str = get_tv_string_buf(&argvars[0], buf);
15235 do_mzeval(str, rettv);
15236}
Bram Moolenaar75676462013-01-30 14:55:42 +010015237
15238 void
15239mzscheme_call_vim(name, args, rettv)
15240 char_u *name;
15241 typval_T *args;
15242 typval_T *rettv;
15243{
15244 typval_T argvars[3];
15245
15246 argvars[0].v_type = VAR_STRING;
15247 argvars[0].vval.v_string = name;
15248 copy_tv(args, &argvars[1]);
15249 argvars[2].v_type = VAR_UNKNOWN;
15250 f_call(argvars, rettv);
15251 clear_tv(&argvars[1]);
15252}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015253#endif
15254
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015256 * "nextnonblank()" function
15257 */
15258 static void
15259f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015260 typval_T *argvars;
15261 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015262{
15263 linenr_T lnum;
15264
15265 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015267 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015268 {
15269 lnum = 0;
15270 break;
15271 }
15272 if (*skipwhite(ml_get(lnum)) != NUL)
15273 break;
15274 }
15275 rettv->vval.v_number = lnum;
15276}
15277
15278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 * "nr2char()" function
15280 */
15281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015282f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015283 typval_T *argvars;
15284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285{
15286 char_u buf[NUMBUFLEN];
15287
15288#ifdef FEAT_MBYTE
15289 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015290 {
15291 int utf8 = 0;
15292
15293 if (argvars[1].v_type != VAR_UNKNOWN)
15294 utf8 = get_tv_number_chk(&argvars[1], NULL);
15295 if (utf8)
15296 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15297 else
15298 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300 else
15301#endif
15302 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015303 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304 buf[1] = NUL;
15305 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015306 rettv->v_type = VAR_STRING;
15307 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015308}
15309
15310/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015311 * "or(expr, expr)" function
15312 */
15313 static void
15314f_or(argvars, rettv)
15315 typval_T *argvars;
15316 typval_T *rettv;
15317{
15318 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15319 | get_tv_number_chk(&argvars[1], NULL);
15320}
15321
15322/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015323 * "pathshorten()" function
15324 */
15325 static void
15326f_pathshorten(argvars, rettv)
15327 typval_T *argvars;
15328 typval_T *rettv;
15329{
15330 char_u *p;
15331
15332 rettv->v_type = VAR_STRING;
15333 p = get_tv_string_chk(&argvars[0]);
15334 if (p == NULL)
15335 rettv->vval.v_string = NULL;
15336 else
15337 {
15338 p = vim_strsave(p);
15339 rettv->vval.v_string = p;
15340 if (p != NULL)
15341 shorten_dir(p);
15342 }
15343}
15344
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015345#ifdef FEAT_FLOAT
15346/*
15347 * "pow()" function
15348 */
15349 static void
15350f_pow(argvars, rettv)
15351 typval_T *argvars;
15352 typval_T *rettv;
15353{
15354 float_T fx, fy;
15355
15356 rettv->v_type = VAR_FLOAT;
15357 if (get_float_arg(argvars, &fx) == OK
15358 && get_float_arg(&argvars[1], &fy) == OK)
15359 rettv->vval.v_float = pow(fx, fy);
15360 else
15361 rettv->vval.v_float = 0.0;
15362}
15363#endif
15364
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015365/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015366 * "prevnonblank()" function
15367 */
15368 static void
15369f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015370 typval_T *argvars;
15371 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015372{
15373 linenr_T lnum;
15374
15375 lnum = get_tv_lnum(argvars);
15376 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15377 lnum = 0;
15378 else
15379 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15380 --lnum;
15381 rettv->vval.v_number = lnum;
15382}
15383
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015384#ifdef HAVE_STDARG_H
15385/* This dummy va_list is here because:
15386 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15387 * - locally in the function results in a "used before set" warning
15388 * - using va_start() to initialize it gives "function with fixed args" error */
15389static va_list ap;
15390#endif
15391
Bram Moolenaar8c711452005-01-14 21:53:12 +000015392/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015393 * "printf()" function
15394 */
15395 static void
15396f_printf(argvars, rettv)
15397 typval_T *argvars;
15398 typval_T *rettv;
15399{
15400 rettv->v_type = VAR_STRING;
15401 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015402#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015403 {
15404 char_u buf[NUMBUFLEN];
15405 int len;
15406 char_u *s;
15407 int saved_did_emsg = did_emsg;
15408 char *fmt;
15409
15410 /* Get the required length, allocate the buffer and do it for real. */
15411 did_emsg = FALSE;
15412 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015413 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015414 if (!did_emsg)
15415 {
15416 s = alloc(len + 1);
15417 if (s != NULL)
15418 {
15419 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015420 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015421 }
15422 }
15423 did_emsg |= saved_did_emsg;
15424 }
15425#endif
15426}
15427
15428/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015429 * "pumvisible()" function
15430 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015431 static void
15432f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015433 typval_T *argvars UNUSED;
15434 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015435{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015436#ifdef FEAT_INS_EXPAND
15437 if (pum_visible())
15438 rettv->vval.v_number = 1;
15439#endif
15440}
15441
Bram Moolenaardb913952012-06-29 12:54:53 +020015442#ifdef FEAT_PYTHON3
15443/*
15444 * "py3eval()" function
15445 */
15446 static void
15447f_py3eval(argvars, rettv)
15448 typval_T *argvars;
15449 typval_T *rettv;
15450{
15451 char_u *str;
15452 char_u buf[NUMBUFLEN];
15453
15454 str = get_tv_string_buf(&argvars[0], buf);
15455 do_py3eval(str, rettv);
15456}
15457#endif
15458
15459#ifdef FEAT_PYTHON
15460/*
15461 * "pyeval()" function
15462 */
15463 static void
15464f_pyeval(argvars, rettv)
15465 typval_T *argvars;
15466 typval_T *rettv;
15467{
15468 char_u *str;
15469 char_u buf[NUMBUFLEN];
15470
15471 str = get_tv_string_buf(&argvars[0], buf);
15472 do_pyeval(str, rettv);
15473}
15474#endif
15475
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015476/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015477 * "range()" function
15478 */
15479 static void
15480f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015481 typval_T *argvars;
15482 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015483{
15484 long start;
15485 long end;
15486 long stride = 1;
15487 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015488 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015489
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015490 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015491 if (argvars[1].v_type == VAR_UNKNOWN)
15492 {
15493 end = start - 1;
15494 start = 0;
15495 }
15496 else
15497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015498 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015499 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015500 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015501 }
15502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015503 if (error)
15504 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015505 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015506 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015507 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015508 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015509 else
15510 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015511 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015512 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015513 if (list_append_number(rettv->vval.v_list,
15514 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015515 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015516 }
15517}
15518
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015519/*
15520 * "readfile()" function
15521 */
15522 static void
15523f_readfile(argvars, rettv)
15524 typval_T *argvars;
15525 typval_T *rettv;
15526{
15527 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015528 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015529 char_u *fname;
15530 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015531 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15532 int io_size = sizeof(buf);
15533 int readlen; /* size of last fread() */
15534 char_u *prev = NULL; /* previously read bytes, if any */
15535 long prevlen = 0; /* length of data in prev */
15536 long prevsize = 0; /* size of prev buffer */
15537 long maxline = MAXLNUM;
15538 long cnt = 0;
15539 char_u *p; /* position in buf */
15540 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015541
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015542 if (argvars[1].v_type != VAR_UNKNOWN)
15543 {
15544 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15545 binary = TRUE;
15546 if (argvars[2].v_type != VAR_UNKNOWN)
15547 maxline = get_tv_number(&argvars[2]);
15548 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015549
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015550 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015551 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015552
15553 /* Always open the file in binary mode, library functions have a mind of
15554 * their own about CR-LF conversion. */
15555 fname = get_tv_string(&argvars[0]);
15556 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15557 {
15558 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15559 return;
15560 }
15561
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015562 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015563 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015564 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015565
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015566 /* This for loop processes what was read, but is also entered at end
15567 * of file so that either:
15568 * - an incomplete line gets written
15569 * - a "binary" file gets an empty line at the end if it ends in a
15570 * newline. */
15571 for (p = buf, start = buf;
15572 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15573 ++p)
15574 {
15575 if (*p == '\n' || readlen <= 0)
15576 {
15577 listitem_T *li;
15578 char_u *s = NULL;
15579 long_u len = p - start;
15580
15581 /* Finished a line. Remove CRs before NL. */
15582 if (readlen > 0 && !binary)
15583 {
15584 while (len > 0 && start[len - 1] == '\r')
15585 --len;
15586 /* removal may cross back to the "prev" string */
15587 if (len == 0)
15588 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15589 --prevlen;
15590 }
15591 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015592 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015593 else
15594 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015595 /* Change "prev" buffer to be the right size. This way
15596 * the bytes are only copied once, and very long lines are
15597 * allocated only once. */
15598 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015599 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015600 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015601 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015602 prev = NULL; /* the list will own the string */
15603 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015604 }
15605 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015606 if (s == NULL)
15607 {
15608 do_outofmem_msg((long_u) prevlen + len + 1);
15609 failed = TRUE;
15610 break;
15611 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015612
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015613 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015614 {
15615 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015616 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015617 break;
15618 }
15619 li->li_tv.v_type = VAR_STRING;
15620 li->li_tv.v_lock = 0;
15621 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015622 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015623
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015624 start = p + 1; /* step over newline */
15625 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015626 break;
15627 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015628 else if (*p == NUL)
15629 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015630#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015631 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15632 * when finding the BF and check the previous two bytes. */
15633 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015634 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015635 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15636 * + 1, these may be in the "prev" string. */
15637 char_u back1 = p >= buf + 1 ? p[-1]
15638 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15639 char_u back2 = p >= buf + 2 ? p[-2]
15640 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15641 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015642
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015643 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015644 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015645 char_u *dest = p - 2;
15646
15647 /* Usually a BOM is at the beginning of a file, and so at
15648 * the beginning of a line; then we can just step over it.
15649 */
15650 if (start == dest)
15651 start = p + 1;
15652 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015653 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015654 /* have to shuffle buf to close gap */
15655 int adjust_prevlen = 0;
15656
15657 if (dest < buf)
15658 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015659 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015660 dest = buf;
15661 }
15662 if (readlen > p - buf + 1)
15663 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15664 readlen -= 3 - adjust_prevlen;
15665 prevlen -= adjust_prevlen;
15666 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015667 }
15668 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015669 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015670#endif
15671 } /* for */
15672
15673 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15674 break;
15675 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015676 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015677 /* There's part of a line in buf, store it in "prev". */
15678 if (p - start + prevlen >= prevsize)
15679 {
15680 /* need bigger "prev" buffer */
15681 char_u *newprev;
15682
15683 /* A common use case is ordinary text files and "prev" gets a
15684 * fragment of a line, so the first allocation is made
15685 * small, to avoid repeatedly 'allocing' large and
15686 * 'reallocing' small. */
15687 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015688 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015689 else
15690 {
15691 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015692 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015693 prevsize = grow50pc > growmin ? grow50pc : growmin;
15694 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015695 newprev = prev == NULL ? alloc(prevsize)
15696 : vim_realloc(prev, prevsize);
15697 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015698 {
15699 do_outofmem_msg((long_u)prevsize);
15700 failed = TRUE;
15701 break;
15702 }
15703 prev = newprev;
15704 }
15705 /* Add the line part to end of "prev". */
15706 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015707 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015708 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015709 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015710
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015711 /*
15712 * For a negative line count use only the lines at the end of the file,
15713 * free the rest.
15714 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015715 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015716 while (cnt > -maxline)
15717 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015718 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015719 --cnt;
15720 }
15721
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015722 if (failed)
15723 {
15724 list_free(rettv->vval.v_list, TRUE);
15725 /* readfile doc says an empty list is returned on error */
15726 rettv->vval.v_list = list_alloc();
15727 }
15728
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015729 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015730 fclose(fd);
15731}
15732
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015733#if defined(FEAT_RELTIME)
15734static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15735
15736/*
15737 * Convert a List to proftime_T.
15738 * Return FAIL when there is something wrong.
15739 */
15740 static int
15741list2proftime(arg, tm)
15742 typval_T *arg;
15743 proftime_T *tm;
15744{
15745 long n1, n2;
15746 int error = FALSE;
15747
15748 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15749 || arg->vval.v_list->lv_len != 2)
15750 return FAIL;
15751 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15752 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15753# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015754 tm->HighPart = n1;
15755 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015756# else
15757 tm->tv_sec = n1;
15758 tm->tv_usec = n2;
15759# endif
15760 return error ? FAIL : OK;
15761}
15762#endif /* FEAT_RELTIME */
15763
15764/*
15765 * "reltime()" function
15766 */
15767 static void
15768f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015769 typval_T *argvars UNUSED;
15770 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015771{
15772#ifdef FEAT_RELTIME
15773 proftime_T res;
15774 proftime_T start;
15775
15776 if (argvars[0].v_type == VAR_UNKNOWN)
15777 {
15778 /* No arguments: get current time. */
15779 profile_start(&res);
15780 }
15781 else if (argvars[1].v_type == VAR_UNKNOWN)
15782 {
15783 if (list2proftime(&argvars[0], &res) == FAIL)
15784 return;
15785 profile_end(&res);
15786 }
15787 else
15788 {
15789 /* Two arguments: compute the difference. */
15790 if (list2proftime(&argvars[0], &start) == FAIL
15791 || list2proftime(&argvars[1], &res) == FAIL)
15792 return;
15793 profile_sub(&res, &start);
15794 }
15795
15796 if (rettv_list_alloc(rettv) == OK)
15797 {
15798 long n1, n2;
15799
15800# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015801 n1 = res.HighPart;
15802 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015803# else
15804 n1 = res.tv_sec;
15805 n2 = res.tv_usec;
15806# endif
15807 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15808 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15809 }
15810#endif
15811}
15812
15813/*
15814 * "reltimestr()" function
15815 */
15816 static void
15817f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015818 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015819 typval_T *rettv;
15820{
15821#ifdef FEAT_RELTIME
15822 proftime_T tm;
15823#endif
15824
15825 rettv->v_type = VAR_STRING;
15826 rettv->vval.v_string = NULL;
15827#ifdef FEAT_RELTIME
15828 if (list2proftime(&argvars[0], &tm) == OK)
15829 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15830#endif
15831}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015832
Bram Moolenaar0d660222005-01-07 21:51:51 +000015833#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15834static void make_connection __ARGS((void));
15835static int check_connection __ARGS((void));
15836
15837 static void
15838make_connection()
15839{
15840 if (X_DISPLAY == NULL
15841# ifdef FEAT_GUI
15842 && !gui.in_use
15843# endif
15844 )
15845 {
15846 x_force_connect = TRUE;
15847 setup_term_clip();
15848 x_force_connect = FALSE;
15849 }
15850}
15851
15852 static int
15853check_connection()
15854{
15855 make_connection();
15856 if (X_DISPLAY == NULL)
15857 {
15858 EMSG(_("E240: No connection to Vim server"));
15859 return FAIL;
15860 }
15861 return OK;
15862}
15863#endif
15864
15865#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015866static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015867
15868 static void
15869remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015870 typval_T *argvars;
15871 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015872 int expr;
15873{
15874 char_u *server_name;
15875 char_u *keys;
15876 char_u *r = NULL;
15877 char_u buf[NUMBUFLEN];
15878# ifdef WIN32
15879 HWND w;
15880# else
15881 Window w;
15882# endif
15883
15884 if (check_restricted() || check_secure())
15885 return;
15886
15887# ifdef FEAT_X11
15888 if (check_connection() == FAIL)
15889 return;
15890# endif
15891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015892 server_name = get_tv_string_chk(&argvars[0]);
15893 if (server_name == NULL)
15894 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015895 keys = get_tv_string_buf(&argvars[1], buf);
15896# ifdef WIN32
15897 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15898# else
15899 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15900 < 0)
15901# endif
15902 {
15903 if (r != NULL)
15904 EMSG(r); /* sending worked but evaluation failed */
15905 else
15906 EMSG2(_("E241: Unable to send to %s"), server_name);
15907 return;
15908 }
15909
15910 rettv->vval.v_string = r;
15911
15912 if (argvars[2].v_type != VAR_UNKNOWN)
15913 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015914 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015915 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015916 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015917
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015918 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015919 v.di_tv.v_type = VAR_STRING;
15920 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015921 idvar = get_tv_string_chk(&argvars[2]);
15922 if (idvar != NULL)
15923 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015924 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015925 }
15926}
15927#endif
15928
15929/*
15930 * "remote_expr()" function
15931 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015932 static void
15933f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015934 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015935 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015936{
15937 rettv->v_type = VAR_STRING;
15938 rettv->vval.v_string = NULL;
15939#ifdef FEAT_CLIENTSERVER
15940 remote_common(argvars, rettv, TRUE);
15941#endif
15942}
15943
15944/*
15945 * "remote_foreground()" function
15946 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015947 static void
15948f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015949 typval_T *argvars UNUSED;
15950 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015951{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015952#ifdef FEAT_CLIENTSERVER
15953# ifdef WIN32
15954 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015955 {
15956 char_u *server_name = get_tv_string_chk(&argvars[0]);
15957
15958 if (server_name != NULL)
15959 serverForeground(server_name);
15960 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015961# else
15962 /* Send a foreground() expression to the server. */
15963 argvars[1].v_type = VAR_STRING;
15964 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15965 argvars[2].v_type = VAR_UNKNOWN;
15966 remote_common(argvars, rettv, TRUE);
15967 vim_free(argvars[1].vval.v_string);
15968# endif
15969#endif
15970}
15971
Bram Moolenaar0d660222005-01-07 21:51:51 +000015972 static void
15973f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015974 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015975 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015976{
15977#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015978 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015979 char_u *s = NULL;
15980# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015981 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015982# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015983 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015984
15985 if (check_restricted() || check_secure())
15986 {
15987 rettv->vval.v_number = -1;
15988 return;
15989 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015990 serverid = get_tv_string_chk(&argvars[0]);
15991 if (serverid == NULL)
15992 {
15993 rettv->vval.v_number = -1;
15994 return; /* type error; errmsg already given */
15995 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015996# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015997 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015998 if (n == 0)
15999 rettv->vval.v_number = -1;
16000 else
16001 {
16002 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16003 rettv->vval.v_number = (s != NULL);
16004 }
16005# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016006 if (check_connection() == FAIL)
16007 return;
16008
16009 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016010 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016011# endif
16012
16013 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016015 char_u *retvar;
16016
Bram Moolenaar33570922005-01-25 22:26:29 +000016017 v.di_tv.v_type = VAR_STRING;
16018 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016019 retvar = get_tv_string_chk(&argvars[1]);
16020 if (retvar != NULL)
16021 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016022 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016023 }
16024#else
16025 rettv->vval.v_number = -1;
16026#endif
16027}
16028
Bram Moolenaar0d660222005-01-07 21:51:51 +000016029 static void
16030f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016031 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016032 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016033{
16034 char_u *r = NULL;
16035
16036#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016037 char_u *serverid = get_tv_string_chk(&argvars[0]);
16038
16039 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016040 {
16041# ifdef WIN32
16042 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016043 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016044
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016045 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016046 if (n != 0)
16047 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16048 if (r == NULL)
16049# else
16050 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016051 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016052# endif
16053 EMSG(_("E277: Unable to read a server reply"));
16054 }
16055#endif
16056 rettv->v_type = VAR_STRING;
16057 rettv->vval.v_string = r;
16058}
16059
16060/*
16061 * "remote_send()" function
16062 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016063 static void
16064f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016065 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016066 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016067{
16068 rettv->v_type = VAR_STRING;
16069 rettv->vval.v_string = NULL;
16070#ifdef FEAT_CLIENTSERVER
16071 remote_common(argvars, rettv, FALSE);
16072#endif
16073}
16074
16075/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016076 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016077 */
16078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016079f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016080 typval_T *argvars;
16081 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016082{
Bram Moolenaar33570922005-01-25 22:26:29 +000016083 list_T *l;
16084 listitem_T *item, *item2;
16085 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016086 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016087 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016088 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016089 dict_T *d;
16090 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016091 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016092
Bram Moolenaar8c711452005-01-14 21:53:12 +000016093 if (argvars[0].v_type == VAR_DICT)
16094 {
16095 if (argvars[2].v_type != VAR_UNKNOWN)
16096 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016097 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016098 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016100 key = get_tv_string_chk(&argvars[1]);
16101 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016103 di = dict_find(d, key, -1);
16104 if (di == NULL)
16105 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016106 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16107 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016108 {
16109 *rettv = di->di_tv;
16110 init_tv(&di->di_tv);
16111 dictitem_remove(d, di);
16112 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016113 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016114 }
16115 }
16116 else if (argvars[0].v_type != VAR_LIST)
16117 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016118 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016119 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016121 int error = FALSE;
16122
16123 idx = get_tv_number_chk(&argvars[1], &error);
16124 if (error)
16125 ; /* type error: do nothing, errmsg already given */
16126 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016127 EMSGN(_(e_listidx), idx);
16128 else
16129 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016130 if (argvars[2].v_type == VAR_UNKNOWN)
16131 {
16132 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016133 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016134 *rettv = item->li_tv;
16135 vim_free(item);
16136 }
16137 else
16138 {
16139 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016140 end = get_tv_number_chk(&argvars[2], &error);
16141 if (error)
16142 ; /* type error: do nothing */
16143 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016144 EMSGN(_(e_listidx), end);
16145 else
16146 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016147 int cnt = 0;
16148
16149 for (li = item; li != NULL; li = li->li_next)
16150 {
16151 ++cnt;
16152 if (li == item2)
16153 break;
16154 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016155 if (li == NULL) /* didn't find "item2" after "item" */
16156 EMSG(_(e_invrange));
16157 else
16158 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016159 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016160 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016161 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016162 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016163 l->lv_first = item;
16164 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016165 item->li_prev = NULL;
16166 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016167 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016168 }
16169 }
16170 }
16171 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016172 }
16173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174}
16175
16176/*
16177 * "rename({from}, {to})" function
16178 */
16179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016180f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 typval_T *argvars;
16182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183{
16184 char_u buf[NUMBUFLEN];
16185
16186 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016187 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016189 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16190 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191}
16192
16193/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016194 * "repeat()" function
16195 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016196 static void
16197f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016198 typval_T *argvars;
16199 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016200{
16201 char_u *p;
16202 int n;
16203 int slen;
16204 int len;
16205 char_u *r;
16206 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016207
16208 n = get_tv_number(&argvars[1]);
16209 if (argvars[0].v_type == VAR_LIST)
16210 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016211 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016212 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016213 if (list_extend(rettv->vval.v_list,
16214 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016215 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016216 }
16217 else
16218 {
16219 p = get_tv_string(&argvars[0]);
16220 rettv->v_type = VAR_STRING;
16221 rettv->vval.v_string = NULL;
16222
16223 slen = (int)STRLEN(p);
16224 len = slen * n;
16225 if (len <= 0)
16226 return;
16227
16228 r = alloc(len + 1);
16229 if (r != NULL)
16230 {
16231 for (i = 0; i < n; i++)
16232 mch_memmove(r + i * slen, p, (size_t)slen);
16233 r[len] = NUL;
16234 }
16235
16236 rettv->vval.v_string = r;
16237 }
16238}
16239
16240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 * "resolve()" function
16242 */
16243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016244f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016245 typval_T *argvars;
16246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247{
16248 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016249#ifdef HAVE_READLINK
16250 char_u *buf = NULL;
16251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016253 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254#ifdef FEAT_SHORTCUT
16255 {
16256 char_u *v = NULL;
16257
16258 v = mch_resolve_shortcut(p);
16259 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016262 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 }
16264#else
16265# ifdef HAVE_READLINK
16266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 char_u *cpy;
16268 int len;
16269 char_u *remain = NULL;
16270 char_u *q;
16271 int is_relative_to_current = FALSE;
16272 int has_trailing_pathsep = FALSE;
16273 int limit = 100;
16274
16275 p = vim_strsave(p);
16276
16277 if (p[0] == '.' && (vim_ispathsep(p[1])
16278 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16279 is_relative_to_current = TRUE;
16280
16281 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016282 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016285 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287
16288 q = getnextcomp(p);
16289 if (*q != NUL)
16290 {
16291 /* Separate the first path component in "p", and keep the
16292 * remainder (beginning with the path separator). */
16293 remain = vim_strsave(q - 1);
16294 q[-1] = NUL;
16295 }
16296
Bram Moolenaard9462e32011-04-11 21:35:11 +020016297 buf = alloc(MAXPATHL + 1);
16298 if (buf == NULL)
16299 goto fail;
16300
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 for (;;)
16302 {
16303 for (;;)
16304 {
16305 len = readlink((char *)p, (char *)buf, MAXPATHL);
16306 if (len <= 0)
16307 break;
16308 buf[len] = NUL;
16309
16310 if (limit-- == 0)
16311 {
16312 vim_free(p);
16313 vim_free(remain);
16314 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016315 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316 goto fail;
16317 }
16318
16319 /* Ensure that the result will have a trailing path separator
16320 * if the argument has one. */
16321 if (remain == NULL && has_trailing_pathsep)
16322 add_pathsep(buf);
16323
16324 /* Separate the first path component in the link value and
16325 * concatenate the remainders. */
16326 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16327 if (*q != NUL)
16328 {
16329 if (remain == NULL)
16330 remain = vim_strsave(q - 1);
16331 else
16332 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016333 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 if (cpy != NULL)
16335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 vim_free(remain);
16337 remain = cpy;
16338 }
16339 }
16340 q[-1] = NUL;
16341 }
16342
16343 q = gettail(p);
16344 if (q > p && *q == NUL)
16345 {
16346 /* Ignore trailing path separator. */
16347 q[-1] = NUL;
16348 q = gettail(p);
16349 }
16350 if (q > p && !mch_isFullName(buf))
16351 {
16352 /* symlink is relative to directory of argument */
16353 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16354 if (cpy != NULL)
16355 {
16356 STRCPY(cpy, p);
16357 STRCPY(gettail(cpy), buf);
16358 vim_free(p);
16359 p = cpy;
16360 }
16361 }
16362 else
16363 {
16364 vim_free(p);
16365 p = vim_strsave(buf);
16366 }
16367 }
16368
16369 if (remain == NULL)
16370 break;
16371
16372 /* Append the first path component of "remain" to "p". */
16373 q = getnextcomp(remain + 1);
16374 len = q - remain - (*q != NUL);
16375 cpy = vim_strnsave(p, STRLEN(p) + len);
16376 if (cpy != NULL)
16377 {
16378 STRNCAT(cpy, remain, len);
16379 vim_free(p);
16380 p = cpy;
16381 }
16382 /* Shorten "remain". */
16383 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016384 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 else
16386 {
16387 vim_free(remain);
16388 remain = NULL;
16389 }
16390 }
16391
16392 /* If the result is a relative path name, make it explicitly relative to
16393 * the current directory if and only if the argument had this form. */
16394 if (!vim_ispathsep(*p))
16395 {
16396 if (is_relative_to_current
16397 && *p != NUL
16398 && !(p[0] == '.'
16399 && (p[1] == NUL
16400 || vim_ispathsep(p[1])
16401 || (p[1] == '.'
16402 && (p[2] == NUL
16403 || vim_ispathsep(p[2]))))))
16404 {
16405 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016406 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407 if (cpy != NULL)
16408 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 vim_free(p);
16410 p = cpy;
16411 }
16412 }
16413 else if (!is_relative_to_current)
16414 {
16415 /* Strip leading "./". */
16416 q = p;
16417 while (q[0] == '.' && vim_ispathsep(q[1]))
16418 q += 2;
16419 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016420 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421 }
16422 }
16423
16424 /* Ensure that the result will have no trailing path separator
16425 * if the argument had none. But keep "/" or "//". */
16426 if (!has_trailing_pathsep)
16427 {
16428 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016429 if (after_pathsep(p, q))
16430 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 }
16432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016433 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434 }
16435# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016436 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437# endif
16438#endif
16439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016440 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441
16442#ifdef HAVE_READLINK
16443fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016444 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016446 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447}
16448
16449/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 */
16452 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016453f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016454 typval_T *argvars;
16455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456{
Bram Moolenaar33570922005-01-25 22:26:29 +000016457 list_T *l;
16458 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459
Bram Moolenaar0d660222005-01-07 21:51:51 +000016460 if (argvars[0].v_type != VAR_LIST)
16461 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016462 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016463 && !tv_check_lock(l->lv_lock,
16464 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016465 {
16466 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016467 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016468 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016469 while (li != NULL)
16470 {
16471 ni = li->li_prev;
16472 list_append(l, li);
16473 li = ni;
16474 }
16475 rettv->vval.v_list = l;
16476 rettv->v_type = VAR_LIST;
16477 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016478 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480}
16481
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016482#define SP_NOMOVE 0x01 /* don't move cursor */
16483#define SP_REPEAT 0x02 /* repeat to find outer pair */
16484#define SP_RETCOUNT 0x04 /* return matchcount */
16485#define SP_SETPCMARK 0x08 /* set previous context mark */
16486#define SP_START 0x10 /* accept match at start position */
16487#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16488#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016489#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016490
Bram Moolenaar33570922005-01-25 22:26:29 +000016491static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492
16493/*
16494 * Get flags for a search function.
16495 * Possibly sets "p_ws".
16496 * Returns BACKWARD, FORWARD or zero (for an error).
16497 */
16498 static int
16499get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016500 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016501 int *flagsp;
16502{
16503 int dir = FORWARD;
16504 char_u *flags;
16505 char_u nbuf[NUMBUFLEN];
16506 int mask;
16507
16508 if (varp->v_type != VAR_UNKNOWN)
16509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016510 flags = get_tv_string_buf_chk(varp, nbuf);
16511 if (flags == NULL)
16512 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016513 while (*flags != NUL)
16514 {
16515 switch (*flags)
16516 {
16517 case 'b': dir = BACKWARD; break;
16518 case 'w': p_ws = TRUE; break;
16519 case 'W': p_ws = FALSE; break;
16520 default: mask = 0;
16521 if (flagsp != NULL)
16522 switch (*flags)
16523 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016524 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016525 case 'e': mask = SP_END; break;
16526 case 'm': mask = SP_RETCOUNT; break;
16527 case 'n': mask = SP_NOMOVE; break;
16528 case 'p': mask = SP_SUBPAT; break;
16529 case 'r': mask = SP_REPEAT; break;
16530 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016531 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016532 }
16533 if (mask == 0)
16534 {
16535 EMSG2(_(e_invarg2), flags);
16536 dir = 0;
16537 }
16538 else
16539 *flagsp |= mask;
16540 }
16541 if (dir == 0)
16542 break;
16543 ++flags;
16544 }
16545 }
16546 return dir;
16547}
16548
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016550 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016552 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016553search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016554 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016555 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016556 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016558 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 char_u *pat;
16560 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016561 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 int save_p_ws = p_ws;
16563 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016564 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016565 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016566 proftime_T tm;
16567#ifdef FEAT_RELTIME
16568 long time_limit = 0;
16569#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016570 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016571 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016573 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016574 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016575 if (dir == 0)
16576 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016577 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016578 if (flags & SP_START)
16579 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016580 if (flags & SP_END)
16581 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016582 if (flags & SP_COLUMN)
16583 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016584
Bram Moolenaar76929292008-01-06 19:07:36 +000016585 /* Optional arguments: line number to stop searching and timeout. */
16586 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016587 {
16588 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16589 if (lnum_stop < 0)
16590 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016591#ifdef FEAT_RELTIME
16592 if (argvars[3].v_type != VAR_UNKNOWN)
16593 {
16594 time_limit = get_tv_number_chk(&argvars[3], NULL);
16595 if (time_limit < 0)
16596 goto theend;
16597 }
16598#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016599 }
16600
Bram Moolenaar76929292008-01-06 19:07:36 +000016601#ifdef FEAT_RELTIME
16602 /* Set the time limit, if there is one. */
16603 profile_setlimit(time_limit, &tm);
16604#endif
16605
Bram Moolenaar231334e2005-07-25 20:46:57 +000016606 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016607 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016608 * Check to make sure only those flags are set.
16609 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16610 * flags cannot be set. Check for that condition also.
16611 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016612 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016613 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016615 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016616 goto theend;
16617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016619 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016620 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016621 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016622 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016624 if (flags & SP_SUBPAT)
16625 retval = subpatnum;
16626 else
16627 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016628 if (flags & SP_SETPCMARK)
16629 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016631 if (match_pos != NULL)
16632 {
16633 /* Store the match cursor position */
16634 match_pos->lnum = pos.lnum;
16635 match_pos->col = pos.col + 1;
16636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637 /* "/$" will put the cursor after the end of the line, may need to
16638 * correct that here */
16639 check_cursor();
16640 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016641
16642 /* If 'n' flag is used: restore cursor position. */
16643 if (flags & SP_NOMOVE)
16644 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016645 else
16646 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016647theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016649
16650 return retval;
16651}
16652
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016653#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016654
16655/*
16656 * round() is not in C90, use ceil() or floor() instead.
16657 */
16658 float_T
16659vim_round(f)
16660 float_T f;
16661{
16662 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16663}
16664
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016665/*
16666 * "round({float})" function
16667 */
16668 static void
16669f_round(argvars, rettv)
16670 typval_T *argvars;
16671 typval_T *rettv;
16672{
16673 float_T f;
16674
16675 rettv->v_type = VAR_FLOAT;
16676 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016677 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016678 else
16679 rettv->vval.v_float = 0.0;
16680}
16681#endif
16682
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016683/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016684 * "screenattr()" function
16685 */
16686 static void
16687f_screenattr(argvars, rettv)
16688 typval_T *argvars UNUSED;
16689 typval_T *rettv;
16690{
16691 int row;
16692 int col;
16693 int c;
16694
16695 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16696 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16697 if (row < 0 || row >= screen_Rows
16698 || col < 0 || col >= screen_Columns)
16699 c = -1;
16700 else
16701 c = ScreenAttrs[LineOffset[row] + col];
16702 rettv->vval.v_number = c;
16703}
16704
16705/*
16706 * "screenchar()" function
16707 */
16708 static void
16709f_screenchar(argvars, rettv)
16710 typval_T *argvars UNUSED;
16711 typval_T *rettv;
16712{
16713 int row;
16714 int col;
16715 int off;
16716 int c;
16717
16718 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16719 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16720 if (row < 0 || row >= screen_Rows
16721 || col < 0 || col >= screen_Columns)
16722 c = -1;
16723 else
16724 {
16725 off = LineOffset[row] + col;
16726#ifdef FEAT_MBYTE
16727 if (enc_utf8 && ScreenLinesUC[off] != 0)
16728 c = ScreenLinesUC[off];
16729 else
16730#endif
16731 c = ScreenLines[off];
16732 }
16733 rettv->vval.v_number = c;
16734}
16735
16736/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016737 * "screencol()" function
16738 *
16739 * First column is 1 to be consistent with virtcol().
16740 */
16741 static void
16742f_screencol(argvars, rettv)
16743 typval_T *argvars UNUSED;
16744 typval_T *rettv;
16745{
16746 rettv->vval.v_number = screen_screencol() + 1;
16747}
16748
16749/*
16750 * "screenrow()" function
16751 */
16752 static void
16753f_screenrow(argvars, rettv)
16754 typval_T *argvars UNUSED;
16755 typval_T *rettv;
16756{
16757 rettv->vval.v_number = screen_screenrow() + 1;
16758}
16759
16760/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016761 * "search()" function
16762 */
16763 static void
16764f_search(argvars, rettv)
16765 typval_T *argvars;
16766 typval_T *rettv;
16767{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016768 int flags = 0;
16769
16770 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771}
16772
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016774 * "searchdecl()" function
16775 */
16776 static void
16777f_searchdecl(argvars, rettv)
16778 typval_T *argvars;
16779 typval_T *rettv;
16780{
16781 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016782 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016783 int error = FALSE;
16784 char_u *name;
16785
16786 rettv->vval.v_number = 1; /* default: FAIL */
16787
16788 name = get_tv_string_chk(&argvars[0]);
16789 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016790 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016791 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016792 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16793 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16794 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016795 if (!error && name != NULL)
16796 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016797 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016798}
16799
16800/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016801 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016803 static int
16804searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016805 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016806 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807{
16808 char_u *spat, *mpat, *epat;
16809 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811 int dir;
16812 int flags = 0;
16813 char_u nbuf1[NUMBUFLEN];
16814 char_u nbuf2[NUMBUFLEN];
16815 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016816 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016817 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016818 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016821 spat = get_tv_string_chk(&argvars[0]);
16822 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16823 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16824 if (spat == NULL || mpat == NULL || epat == NULL)
16825 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 /* Handle the optional fourth argument: flags */
16828 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016829 if (dir == 0)
16830 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016831
16832 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016833 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16834 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016835 if ((flags & (SP_END | SP_SUBPAT)) != 0
16836 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016837 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016838 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016839 goto theend;
16840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016842 /* Using 'r' implies 'W', otherwise it doesn't work. */
16843 if (flags & SP_REPEAT)
16844 p_ws = FALSE;
16845
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016846 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016847 if (argvars[3].v_type == VAR_UNKNOWN
16848 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 skip = (char_u *)"";
16850 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016852 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016853 if (argvars[5].v_type != VAR_UNKNOWN)
16854 {
16855 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16856 if (lnum_stop < 0)
16857 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016858#ifdef FEAT_RELTIME
16859 if (argvars[6].v_type != VAR_UNKNOWN)
16860 {
16861 time_limit = get_tv_number_chk(&argvars[6], NULL);
16862 if (time_limit < 0)
16863 goto theend;
16864 }
16865#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016866 }
16867 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016868 if (skip == NULL)
16869 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016871 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016872 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016873
16874theend:
16875 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016876
16877 return retval;
16878}
16879
16880/*
16881 * "searchpair()" function
16882 */
16883 static void
16884f_searchpair(argvars, rettv)
16885 typval_T *argvars;
16886 typval_T *rettv;
16887{
16888 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16889}
16890
16891/*
16892 * "searchpairpos()" function
16893 */
16894 static void
16895f_searchpairpos(argvars, rettv)
16896 typval_T *argvars;
16897 typval_T *rettv;
16898{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016899 pos_T match_pos;
16900 int lnum = 0;
16901 int col = 0;
16902
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016903 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016904 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016905
16906 if (searchpair_cmn(argvars, &match_pos) > 0)
16907 {
16908 lnum = match_pos.lnum;
16909 col = match_pos.col;
16910 }
16911
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016912 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16913 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016914}
16915
16916/*
16917 * Search for a start/middle/end thing.
16918 * Used by searchpair(), see its documentation for the details.
16919 * Returns 0 or -1 for no match,
16920 */
16921 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016922do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16923 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016924 char_u *spat; /* start pattern */
16925 char_u *mpat; /* middle pattern */
16926 char_u *epat; /* end pattern */
16927 int dir; /* BACKWARD or FORWARD */
16928 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016929 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016930 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016931 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016932 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016933{
16934 char_u *save_cpo;
16935 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16936 long retval = 0;
16937 pos_T pos;
16938 pos_T firstpos;
16939 pos_T foundpos;
16940 pos_T save_cursor;
16941 pos_T save_pos;
16942 int n;
16943 int r;
16944 int nest = 1;
16945 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016946 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016947 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016948
16949 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16950 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016951 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016952
Bram Moolenaar76929292008-01-06 19:07:36 +000016953#ifdef FEAT_RELTIME
16954 /* Set the time limit, if there is one. */
16955 profile_setlimit(time_limit, &tm);
16956#endif
16957
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016958 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16959 * start/middle/end (pat3, for the top pair). */
16960 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16961 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16962 if (pat2 == NULL || pat3 == NULL)
16963 goto theend;
16964 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16965 if (*mpat == NUL)
16966 STRCPY(pat3, pat2);
16967 else
16968 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16969 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016970 if (flags & SP_START)
16971 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016972
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973 save_cursor = curwin->w_cursor;
16974 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016975 clearpos(&firstpos);
16976 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977 pat = pat3;
16978 for (;;)
16979 {
16980 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016981 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16983 /* didn't find it or found the first match again: FAIL */
16984 break;
16985
16986 if (firstpos.lnum == 0)
16987 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016988 if (equalpos(pos, foundpos))
16989 {
16990 /* Found the same position again. Can happen with a pattern that
16991 * has "\zs" at the end and searching backwards. Advance one
16992 * character and try again. */
16993 if (dir == BACKWARD)
16994 decl(&pos);
16995 else
16996 incl(&pos);
16997 }
16998 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017000 /* clear the start flag to avoid getting stuck here */
17001 options &= ~SEARCH_START;
17002
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 /* If the skip pattern matches, ignore this match. */
17004 if (*skip != NUL)
17005 {
17006 save_pos = curwin->w_cursor;
17007 curwin->w_cursor = pos;
17008 r = eval_to_bool(skip, &err, NULL, FALSE);
17009 curwin->w_cursor = save_pos;
17010 if (err)
17011 {
17012 /* Evaluating {skip} caused an error, break here. */
17013 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017014 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 break;
17016 }
17017 if (r)
17018 continue;
17019 }
17020
17021 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17022 {
17023 /* Found end when searching backwards or start when searching
17024 * forward: nested pair. */
17025 ++nest;
17026 pat = pat2; /* nested, don't search for middle */
17027 }
17028 else
17029 {
17030 /* Found end when searching forward or start when searching
17031 * backward: end of (nested) pair; or found middle in outer pair. */
17032 if (--nest == 1)
17033 pat = pat3; /* outer level, search for middle */
17034 }
17035
17036 if (nest == 0)
17037 {
17038 /* Found the match: return matchcount or line number. */
17039 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017040 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017042 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017043 if (flags & SP_SETPCMARK)
17044 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 curwin->w_cursor = pos;
17046 if (!(flags & SP_REPEAT))
17047 break;
17048 nest = 1; /* search for next unmatched */
17049 }
17050 }
17051
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017052 if (match_pos != NULL)
17053 {
17054 /* Store the match cursor position */
17055 match_pos->lnum = curwin->w_cursor.lnum;
17056 match_pos->col = curwin->w_cursor.col + 1;
17057 }
17058
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017060 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061 curwin->w_cursor = save_cursor;
17062
17063theend:
17064 vim_free(pat2);
17065 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017066 if (p_cpo == empty_option)
17067 p_cpo = save_cpo;
17068 else
17069 /* Darn, evaluating the {skip} expression changed the value. */
17070 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017071
17072 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073}
17074
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017075/*
17076 * "searchpos()" function
17077 */
17078 static void
17079f_searchpos(argvars, rettv)
17080 typval_T *argvars;
17081 typval_T *rettv;
17082{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017083 pos_T match_pos;
17084 int lnum = 0;
17085 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017086 int n;
17087 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017088
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017089 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017090 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017091
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017092 n = search_cmn(argvars, &match_pos, &flags);
17093 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017094 {
17095 lnum = match_pos.lnum;
17096 col = match_pos.col;
17097 }
17098
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017099 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17100 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017101 if (flags & SP_SUBPAT)
17102 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017103}
17104
17105
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106 static void
17107f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017108 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017111#ifdef FEAT_CLIENTSERVER
17112 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017113 char_u *server = get_tv_string_chk(&argvars[0]);
17114 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017117 if (server == NULL || reply == NULL)
17118 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017119 if (check_restricted() || check_secure())
17120 return;
17121# ifdef FEAT_X11
17122 if (check_connection() == FAIL)
17123 return;
17124# endif
17125
17126 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128 EMSG(_("E258: Unable to send to client"));
17129 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017131 rettv->vval.v_number = 0;
17132#else
17133 rettv->vval.v_number = -1;
17134#endif
17135}
17136
Bram Moolenaar0d660222005-01-07 21:51:51 +000017137 static void
17138f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017139 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017140 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141{
17142 char_u *r = NULL;
17143
17144#ifdef FEAT_CLIENTSERVER
17145# ifdef WIN32
17146 r = serverGetVimNames();
17147# else
17148 make_connection();
17149 if (X_DISPLAY != NULL)
17150 r = serverGetVimNames(X_DISPLAY);
17151# endif
17152#endif
17153 rettv->v_type = VAR_STRING;
17154 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155}
17156
17157/*
17158 * "setbufvar()" function
17159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017161f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017162 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017163 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164{
17165 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017168 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 char_u nbuf[NUMBUFLEN];
17170
17171 if (check_restricted() || check_secure())
17172 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017173 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17174 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017175 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 varp = &argvars[2];
17177
17178 if (buf != NULL && varname != NULL && varp != NULL)
17179 {
17180 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182
17183 if (*varname == '&')
17184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017185 long numval;
17186 char_u *strval;
17187 int error = FALSE;
17188
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017190 numval = get_tv_number_chk(varp, &error);
17191 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017192 if (!error && strval != NULL)
17193 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 }
17195 else
17196 {
17197 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17198 if (bufvarname != NULL)
17199 {
17200 STRCPY(bufvarname, "b:");
17201 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017202 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203 vim_free(bufvarname);
17204 }
17205 }
17206
17207 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210}
17211
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017212 static void
17213f_setcharsearch(argvars, rettv)
17214 typval_T *argvars;
17215 typval_T *rettv UNUSED;
17216{
17217 dict_T *d;
17218 dictitem_T *di;
17219 char_u *csearch;
17220
17221 if (argvars[0].v_type != VAR_DICT)
17222 {
17223 EMSG(_(e_dictreq));
17224 return;
17225 }
17226
17227 if ((d = argvars[0].vval.v_dict) != NULL)
17228 {
17229 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17230 if (csearch != NULL)
17231 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017232#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017233 if (enc_utf8)
17234 {
17235 int pcc[MAX_MCO];
17236 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017237
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017238 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17239 }
17240 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017241#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017242 set_last_csearch(PTR2CHAR(csearch),
17243 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017244 }
17245
17246 di = dict_find(d, (char_u *)"forward", -1);
17247 if (di != NULL)
17248 set_csearch_direction(get_tv_number(&di->di_tv)
17249 ? FORWARD : BACKWARD);
17250
17251 di = dict_find(d, (char_u *)"until", -1);
17252 if (di != NULL)
17253 set_csearch_until(!!get_tv_number(&di->di_tv));
17254 }
17255}
17256
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257/*
17258 * "setcmdpos()" function
17259 */
17260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017261f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017262 typval_T *argvars;
17263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017265 int pos = (int)get_tv_number(&argvars[0]) - 1;
17266
17267 if (pos >= 0)
17268 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269}
17270
17271/*
17272 * "setline()" function
17273 */
17274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017275f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017276 typval_T *argvars;
17277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278{
17279 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017280 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017281 list_T *l = NULL;
17282 listitem_T *li = NULL;
17283 long added = 0;
17284 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017286 lnum = get_tv_lnum(&argvars[0]);
17287 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017289 l = argvars[1].vval.v_list;
17290 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017292 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017293 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017294
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017295 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017296 for (;;)
17297 {
17298 if (l != NULL)
17299 {
17300 /* list argument, get next string */
17301 if (li == NULL)
17302 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017303 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017304 li = li->li_next;
17305 }
17306
17307 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017308 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017309 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017310
17311 /* When coming here from Insert mode, sync undo, so that this can be
17312 * undone separately from what was previously inserted. */
17313 if (u_sync_once == 2)
17314 {
17315 u_sync_once = 1; /* notify that u_sync() was called */
17316 u_sync(TRUE);
17317 }
17318
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017319 if (lnum <= curbuf->b_ml.ml_line_count)
17320 {
17321 /* existing line, replace it */
17322 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17323 {
17324 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017325 if (lnum == curwin->w_cursor.lnum)
17326 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017327 rettv->vval.v_number = 0; /* OK */
17328 }
17329 }
17330 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17331 {
17332 /* lnum is one past the last line, append the line */
17333 ++added;
17334 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17335 rettv->vval.v_number = 0; /* OK */
17336 }
17337
17338 if (l == NULL) /* only one string argument */
17339 break;
17340 ++lnum;
17341 }
17342
17343 if (added > 0)
17344 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345}
17346
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017347static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17348
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017350 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017351 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017352 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017353set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017354 win_T *wp UNUSED;
17355 typval_T *list_arg UNUSED;
17356 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017357 typval_T *rettv;
17358{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017359#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017360 char_u *act;
17361 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017362#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017363
Bram Moolenaar2641f772005-03-25 21:58:17 +000017364 rettv->vval.v_number = -1;
17365
17366#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017367 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017368 EMSG(_(e_listreq));
17369 else
17370 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017371 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017372
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017373 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017374 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017375 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017376 if (act == NULL)
17377 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017378 if (*act == 'a' || *act == 'r')
17379 action = *act;
17380 }
17381
Bram Moolenaar81484f42012-12-05 15:16:47 +010017382 if (l != NULL && set_errorlist(wp, l, action,
17383 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017384 rettv->vval.v_number = 0;
17385 }
17386#endif
17387}
17388
17389/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017390 * "setloclist()" function
17391 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017392 static void
17393f_setloclist(argvars, rettv)
17394 typval_T *argvars;
17395 typval_T *rettv;
17396{
17397 win_T *win;
17398
17399 rettv->vval.v_number = -1;
17400
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017401 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017402 if (win != NULL)
17403 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17404}
17405
17406/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017407 * "setmatches()" function
17408 */
17409 static void
17410f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017411 typval_T *argvars UNUSED;
17412 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017413{
17414#ifdef FEAT_SEARCH_EXTRA
17415 list_T *l;
17416 listitem_T *li;
17417 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017418 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017419
17420 rettv->vval.v_number = -1;
17421 if (argvars[0].v_type != VAR_LIST)
17422 {
17423 EMSG(_(e_listreq));
17424 return;
17425 }
17426 if ((l = argvars[0].vval.v_list) != NULL)
17427 {
17428
17429 /* To some extent make sure that we are dealing with a list from
17430 * "getmatches()". */
17431 li = l->lv_first;
17432 while (li != NULL)
17433 {
17434 if (li->li_tv.v_type != VAR_DICT
17435 || (d = li->li_tv.vval.v_dict) == NULL)
17436 {
17437 EMSG(_(e_invarg));
17438 return;
17439 }
17440 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017441 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17442 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017443 && dict_find(d, (char_u *)"priority", -1) != NULL
17444 && dict_find(d, (char_u *)"id", -1) != NULL))
17445 {
17446 EMSG(_(e_invarg));
17447 return;
17448 }
17449 li = li->li_next;
17450 }
17451
17452 clear_matches(curwin);
17453 li = l->lv_first;
17454 while (li != NULL)
17455 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017456 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017457 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017458 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017459 char_u *group;
17460 int priority;
17461 int id;
17462 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017463
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017464 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017465 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17466 {
17467 if (s == NULL)
17468 {
17469 s = list_alloc();
17470 if (s == NULL)
17471 return;
17472 }
17473
17474 /* match from matchaddpos() */
17475 for (i = 1; i < 9; i++)
17476 {
17477 sprintf((char *)buf, (char *)"pos%d", i);
17478 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17479 {
17480 if (di->di_tv.v_type != VAR_LIST)
17481 return;
17482
17483 list_append_tv(s, &di->di_tv);
17484 s->lv_refcount++;
17485 }
17486 else
17487 break;
17488 }
17489 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017490
17491 group = get_dict_string(d, (char_u *)"group", FALSE);
17492 priority = (int)get_dict_number(d, (char_u *)"priority");
17493 id = (int)get_dict_number(d, (char_u *)"id");
17494 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17495 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17496 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017497 if (i == 0)
17498 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017499 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017500 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017501 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017502 }
17503 else
17504 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017505 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017506 list_unref(s);
17507 s = NULL;
17508 }
17509
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017510 li = li->li_next;
17511 }
17512 rettv->vval.v_number = 0;
17513 }
17514#endif
17515}
17516
17517/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017518 * "setpos()" function
17519 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017520 static void
17521f_setpos(argvars, rettv)
17522 typval_T *argvars;
17523 typval_T *rettv;
17524{
17525 pos_T pos;
17526 int fnum;
17527 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017528 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017529
Bram Moolenaar08250432008-02-13 11:42:46 +000017530 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017531 name = get_tv_string_chk(argvars);
17532 if (name != NULL)
17533 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017534 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017535 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017536 if (--pos.col < 0)
17537 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017538 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017539 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017540 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017541 if (fnum == curbuf->b_fnum)
17542 {
17543 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017544 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017545 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017546 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017547 curwin->w_set_curswant = FALSE;
17548 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017549 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017550 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017551 }
17552 else
17553 EMSG(_(e_invarg));
17554 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017555 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17556 {
17557 /* set mark */
17558 if (setmark_pos(name[1], &pos, fnum) == OK)
17559 rettv->vval.v_number = 0;
17560 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017561 else
17562 EMSG(_(e_invarg));
17563 }
17564 }
17565}
17566
17567/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017568 * "setqflist()" function
17569 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017570 static void
17571f_setqflist(argvars, rettv)
17572 typval_T *argvars;
17573 typval_T *rettv;
17574{
17575 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17576}
17577
17578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579 * "setreg()" function
17580 */
17581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017582f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017583 typval_T *argvars;
17584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585{
17586 int regname;
17587 char_u *strregname;
17588 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017589 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 int append;
17591 char_u yank_type;
17592 long block_len;
17593
17594 block_len = -1;
17595 yank_type = MAUTO;
17596 append = FALSE;
17597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017598 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017599 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017601 if (strregname == NULL)
17602 return; /* type error; errmsg already given */
17603 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 if (regname == 0 || regname == '@')
17605 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017607 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017609 stropt = get_tv_string_chk(&argvars[2]);
17610 if (stropt == NULL)
17611 return; /* type error */
17612 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 switch (*stropt)
17614 {
17615 case 'a': case 'A': /* append */
17616 append = TRUE;
17617 break;
17618 case 'v': case 'c': /* character-wise selection */
17619 yank_type = MCHAR;
17620 break;
17621 case 'V': case 'l': /* line-wise selection */
17622 yank_type = MLINE;
17623 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 case 'b': case Ctrl_V: /* block-wise selection */
17625 yank_type = MBLOCK;
17626 if (VIM_ISDIGIT(stropt[1]))
17627 {
17628 ++stropt;
17629 block_len = getdigits(&stropt) - 1;
17630 --stropt;
17631 }
17632 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 }
17634 }
17635
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017636 if (argvars[1].v_type == VAR_LIST)
17637 {
17638 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017639 char_u **allocval;
17640 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017641 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017642 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017643 int len = argvars[1].vval.v_list->lv_len;
17644 listitem_T *li;
17645
Bram Moolenaar7d647822014-04-05 21:28:56 +020017646 /* First half: use for pointers to result lines; second half: use for
17647 * pointers to allocated copies. */
17648 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017649 if (lstval == NULL)
17650 return;
17651 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017652 allocval = lstval + len + 2;
17653 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017654
17655 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17656 li = li->li_next)
17657 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017658 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017659 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017660 goto free_lstval;
17661 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017662 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017663 /* Need to make a copy, next get_tv_string_buf_chk() will
17664 * overwrite the string. */
17665 strval = vim_strsave(buf);
17666 if (strval == NULL)
17667 goto free_lstval;
17668 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017669 }
17670 *curval++ = strval;
17671 }
17672 *curval++ = NULL;
17673
17674 write_reg_contents_lst(regname, lstval, -1,
17675 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017676free_lstval:
17677 while (curallocval > allocval)
17678 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017679 vim_free(lstval);
17680 }
17681 else
17682 {
17683 strval = get_tv_string_chk(&argvars[1]);
17684 if (strval == NULL)
17685 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017686 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017688 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017689 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690}
17691
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017692/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017693 * "settabvar()" function
17694 */
17695 static void
17696f_settabvar(argvars, rettv)
17697 typval_T *argvars;
17698 typval_T *rettv;
17699{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017700#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017701 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017702 tabpage_T *tp;
17703#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017704 char_u *varname, *tabvarname;
17705 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017706
17707 rettv->vval.v_number = 0;
17708
17709 if (check_restricted() || check_secure())
17710 return;
17711
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017712#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017713 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017714#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017715 varname = get_tv_string_chk(&argvars[1]);
17716 varp = &argvars[2];
17717
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017718 if (varname != NULL && varp != NULL
17719#ifdef FEAT_WINDOWS
17720 && tp != NULL
17721#endif
17722 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017723 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017724#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017725 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017726 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017727#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017728
17729 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17730 if (tabvarname != NULL)
17731 {
17732 STRCPY(tabvarname, "t:");
17733 STRCPY(tabvarname + 2, varname);
17734 set_var(tabvarname, varp, TRUE);
17735 vim_free(tabvarname);
17736 }
17737
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017738#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017739 /* Restore current tabpage */
17740 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017741 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017742#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017743 }
17744}
17745
17746/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017747 * "settabwinvar()" function
17748 */
17749 static void
17750f_settabwinvar(argvars, rettv)
17751 typval_T *argvars;
17752 typval_T *rettv;
17753{
17754 setwinvar(argvars, rettv, 1);
17755}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756
17757/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017758 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017761f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017762 typval_T *argvars;
17763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017765 setwinvar(argvars, rettv, 0);
17766}
17767
17768/*
17769 * "setwinvar()" and "settabwinvar()" functions
17770 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017771
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017772 static void
17773setwinvar(argvars, rettv, off)
17774 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017775 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017776 int off;
17777{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 win_T *win;
17779#ifdef FEAT_WINDOWS
17780 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017781 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017782 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783#endif
17784 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017785 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017787 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788
17789 if (check_restricted() || check_secure())
17790 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017791
17792#ifdef FEAT_WINDOWS
17793 if (off == 1)
17794 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17795 else
17796 tp = curtab;
17797#endif
17798 win = find_win_by_nr(&argvars[off], tp);
17799 varname = get_tv_string_chk(&argvars[off + 1]);
17800 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801
17802 if (win != NULL && varname != NULL && varp != NULL)
17803 {
17804#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017805 need_switch_win = !(tp == curtab && win == curwin);
17806 if (!need_switch_win
17807 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017810 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017812 long numval;
17813 char_u *strval;
17814 int error = FALSE;
17815
17816 ++varname;
17817 numval = get_tv_number_chk(varp, &error);
17818 strval = get_tv_string_buf_chk(varp, nbuf);
17819 if (!error && strval != NULL)
17820 set_option_value(varname, numval, strval, OPT_LOCAL);
17821 }
17822 else
17823 {
17824 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17825 if (winvarname != NULL)
17826 {
17827 STRCPY(winvarname, "w:");
17828 STRCPY(winvarname + 2, varname);
17829 set_var(winvarname, varp, TRUE);
17830 vim_free(winvarname);
17831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 }
17833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017835 if (need_switch_win)
17836 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837#endif
17838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839}
17840
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017841#ifdef FEAT_CRYPT
17842/*
17843 * "sha256({string})" function
17844 */
17845 static void
17846f_sha256(argvars, rettv)
17847 typval_T *argvars;
17848 typval_T *rettv;
17849{
17850 char_u *p;
17851
17852 p = get_tv_string(&argvars[0]);
17853 rettv->vval.v_string = vim_strsave(
17854 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17855 rettv->v_type = VAR_STRING;
17856}
17857#endif /* FEAT_CRYPT */
17858
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017860 * "shellescape({string})" function
17861 */
17862 static void
17863f_shellescape(argvars, rettv)
17864 typval_T *argvars;
17865 typval_T *rettv;
17866{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017867 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017868 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017869 rettv->v_type = VAR_STRING;
17870}
17871
17872/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017873 * shiftwidth() function
17874 */
17875 static void
17876f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017877 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017878 typval_T *rettv;
17879{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017880 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017881}
17882
17883/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017884 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 */
17886 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017887f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017888 typval_T *argvars;
17889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017891 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892
Bram Moolenaar0d660222005-01-07 21:51:51 +000017893 p = get_tv_string(&argvars[0]);
17894 rettv->vval.v_string = vim_strsave(p);
17895 simplify_filename(rettv->vval.v_string); /* simplify in place */
17896 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897}
17898
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017899#ifdef FEAT_FLOAT
17900/*
17901 * "sin()" function
17902 */
17903 static void
17904f_sin(argvars, rettv)
17905 typval_T *argvars;
17906 typval_T *rettv;
17907{
17908 float_T f;
17909
17910 rettv->v_type = VAR_FLOAT;
17911 if (get_float_arg(argvars, &f) == OK)
17912 rettv->vval.v_float = sin(f);
17913 else
17914 rettv->vval.v_float = 0.0;
17915}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017916
17917/*
17918 * "sinh()" function
17919 */
17920 static void
17921f_sinh(argvars, rettv)
17922 typval_T *argvars;
17923 typval_T *rettv;
17924{
17925 float_T f;
17926
17927 rettv->v_type = VAR_FLOAT;
17928 if (get_float_arg(argvars, &f) == OK)
17929 rettv->vval.v_float = sinh(f);
17930 else
17931 rettv->vval.v_float = 0.0;
17932}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017933#endif
17934
Bram Moolenaar0d660222005-01-07 21:51:51 +000017935static int
17936#ifdef __BORLANDC__
17937 _RTLENTRYF
17938#endif
17939 item_compare __ARGS((const void *s1, const void *s2));
17940static int
17941#ifdef __BORLANDC__
17942 _RTLENTRYF
17943#endif
17944 item_compare2 __ARGS((const void *s1, const void *s2));
17945
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017946/* struct used in the array that's given to qsort() */
17947typedef struct
17948{
17949 listitem_T *item;
17950 int idx;
17951} sortItem_T;
17952
Bram Moolenaar0d660222005-01-07 21:51:51 +000017953static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017954static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017955static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017956static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017957static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017958static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017959static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017960static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017961#define ITEM_COMPARE_FAIL 999
17962
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017964 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017965 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017966 static int
17967#ifdef __BORLANDC__
17968_RTLENTRYF
17969#endif
17970item_compare(s1, s2)
17971 const void *s1;
17972 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017974 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017975 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017976 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017977 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017978 int res;
17979 char_u numbuf1[NUMBUFLEN];
17980 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017982 si1 = (sortItem_T *)s1;
17983 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017984 tv1 = &si1->item->li_tv;
17985 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017986
17987 if (item_compare_numbers)
17988 {
17989 long v1 = get_tv_number(tv1);
17990 long v2 = get_tv_number(tv2);
17991
17992 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17993 }
17994
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017995 /* tv2string() puts quotes around a string and allocates memory. Don't do
17996 * that for string variables. Use a single quote when comparing with a
17997 * non-string to do what the docs promise. */
17998 if (tv1->v_type == VAR_STRING)
17999 {
18000 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18001 p1 = (char_u *)"'";
18002 else
18003 p1 = tv1->vval.v_string;
18004 }
18005 else
18006 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18007 if (tv2->v_type == VAR_STRING)
18008 {
18009 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18010 p2 = (char_u *)"'";
18011 else
18012 p2 = tv2->vval.v_string;
18013 }
18014 else
18015 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018016 if (p1 == NULL)
18017 p1 = (char_u *)"";
18018 if (p2 == NULL)
18019 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018020 if (!item_compare_numeric)
18021 {
18022 if (item_compare_ic)
18023 res = STRICMP(p1, p2);
18024 else
18025 res = STRCMP(p1, p2);
18026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018028 {
18029 double n1, n2;
18030 n1 = strtod((char *)p1, (char **)&p1);
18031 n2 = strtod((char *)p2, (char **)&p2);
18032 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18033 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018034
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018035 /* When the result would be zero, compare the item indexes. Makes the
18036 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018037 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018038 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018039
Bram Moolenaar0d660222005-01-07 21:51:51 +000018040 vim_free(tofree1);
18041 vim_free(tofree2);
18042 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043}
18044
18045 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018046#ifdef __BORLANDC__
18047_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018049item_compare2(s1, s2)
18050 const void *s1;
18051 const void *s2;
18052{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018053 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018054 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018055 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018056 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018057 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018059 /* shortcut after failure in previous call; compare all items equal */
18060 if (item_compare_func_err)
18061 return 0;
18062
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018063 si1 = (sortItem_T *)s1;
18064 si2 = (sortItem_T *)s2;
18065
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018066 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018067 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018068 copy_tv(&si1->item->li_tv, &argv[0]);
18069 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018070
18071 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018072 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018073 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18074 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018075 clear_tv(&argv[0]);
18076 clear_tv(&argv[1]);
18077
18078 if (res == FAIL)
18079 res = ITEM_COMPARE_FAIL;
18080 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018081 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18082 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018083 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018084 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018085
18086 /* When the result would be zero, compare the pointers themselves. Makes
18087 * the sort stable. */
18088 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018089 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018090
Bram Moolenaar0d660222005-01-07 21:51:51 +000018091 return res;
18092}
18093
18094/*
18095 * "sort({list})" function
18096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018098do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018099 typval_T *argvars;
18100 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018101 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102{
Bram Moolenaar33570922005-01-25 22:26:29 +000018103 list_T *l;
18104 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018105 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018106 long len;
18107 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108
Bram Moolenaar0d660222005-01-07 21:51:51 +000018109 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018110 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111 else
18112 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018113 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018114 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018115 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18116 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018117 return;
18118 rettv->vval.v_list = l;
18119 rettv->v_type = VAR_LIST;
18120 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121
Bram Moolenaar0d660222005-01-07 21:51:51 +000018122 len = list_len(l);
18123 if (len <= 1)
18124 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125
Bram Moolenaar0d660222005-01-07 21:51:51 +000018126 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018127 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018128 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018129 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018130 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018131 if (argvars[1].v_type != VAR_UNKNOWN)
18132 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018133 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018134 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018135 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018136 else
18137 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018138 int error = FALSE;
18139
18140 i = get_tv_number_chk(&argvars[1], &error);
18141 if (error)
18142 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018143 if (i == 1)
18144 item_compare_ic = TRUE;
18145 else
18146 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018147 if (item_compare_func != NULL)
18148 {
18149 if (STRCMP(item_compare_func, "n") == 0)
18150 {
18151 item_compare_func = NULL;
18152 item_compare_numeric = TRUE;
18153 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018154 else if (STRCMP(item_compare_func, "N") == 0)
18155 {
18156 item_compare_func = NULL;
18157 item_compare_numbers = TRUE;
18158 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018159 else if (STRCMP(item_compare_func, "i") == 0)
18160 {
18161 item_compare_func = NULL;
18162 item_compare_ic = TRUE;
18163 }
18164 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018165 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018166
18167 if (argvars[2].v_type != VAR_UNKNOWN)
18168 {
18169 /* optional third argument: {dict} */
18170 if (argvars[2].v_type != VAR_DICT)
18171 {
18172 EMSG(_(e_dictreq));
18173 return;
18174 }
18175 item_compare_selfdict = argvars[2].vval.v_dict;
18176 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178
Bram Moolenaar0d660222005-01-07 21:51:51 +000018179 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018180 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018181 if (ptrs == NULL)
18182 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183
Bram Moolenaar327aa022014-03-25 18:24:23 +010018184 i = 0;
18185 if (sort)
18186 {
18187 /* sort(): ptrs will be the list to sort */
18188 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018189 {
18190 ptrs[i].item = li;
18191 ptrs[i].idx = i;
18192 ++i;
18193 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018194
18195 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018196 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018197 /* test the compare function */
18198 if (item_compare_func != NULL
18199 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018200 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018201 EMSG(_("E702: Sort compare function failed"));
18202 else
18203 {
18204 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018205 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018206 item_compare_func == NULL ? item_compare : item_compare2);
18207
18208 if (!item_compare_func_err)
18209 {
18210 /* Clear the List and append the items in sorted order. */
18211 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18212 l->lv_len = 0;
18213 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018214 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018215 }
18216 }
18217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018219 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018220 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18221
18222 /* f_uniq(): ptrs will be a stack of items to remove */
18223 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018224 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018225 item_compare_func_ptr = item_compare_func
18226 ? item_compare2 : item_compare;
18227
18228 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18229 li = li->li_next)
18230 {
18231 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18232 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018233 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018234 if (item_compare_func_err)
18235 {
18236 EMSG(_("E882: Uniq compare function failed"));
18237 break;
18238 }
18239 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018241 if (!item_compare_func_err)
18242 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018243 while (--i >= 0)
18244 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018245 li = ptrs[i].item->li_next;
18246 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018247 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018248 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018249 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018250 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018251 list_fix_watch(l, li);
18252 listitem_free(li);
18253 l->lv_len--;
18254 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018255 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018256 }
18257
18258 vim_free(ptrs);
18259 }
18260}
18261
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018262/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018263 * "sort({list})" function
18264 */
18265 static void
18266f_sort(argvars, rettv)
18267 typval_T *argvars;
18268 typval_T *rettv;
18269{
18270 do_sort_uniq(argvars, rettv, TRUE);
18271}
18272
18273/*
18274 * "uniq({list})" function
18275 */
18276 static void
18277f_uniq(argvars, rettv)
18278 typval_T *argvars;
18279 typval_T *rettv;
18280{
18281 do_sort_uniq(argvars, rettv, FALSE);
18282}
18283
18284/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018285 * "soundfold({word})" function
18286 */
18287 static void
18288f_soundfold(argvars, rettv)
18289 typval_T *argvars;
18290 typval_T *rettv;
18291{
18292 char_u *s;
18293
18294 rettv->v_type = VAR_STRING;
18295 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018296#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018297 rettv->vval.v_string = eval_soundfold(s);
18298#else
18299 rettv->vval.v_string = vim_strsave(s);
18300#endif
18301}
18302
18303/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018304 * "spellbadword()" function
18305 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018306 static void
18307f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018308 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018309 typval_T *rettv;
18310{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018311 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018312 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018313 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018314
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018315 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018316 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018317
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018318#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018319 if (argvars[0].v_type == VAR_UNKNOWN)
18320 {
18321 /* Find the start and length of the badly spelled word. */
18322 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18323 if (len != 0)
18324 word = ml_get_cursor();
18325 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018326 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018327 {
18328 char_u *str = get_tv_string_chk(&argvars[0]);
18329 int capcol = -1;
18330
18331 if (str != NULL)
18332 {
18333 /* Check the argument for spelling. */
18334 while (*str != NUL)
18335 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018336 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018337 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018338 {
18339 word = str;
18340 break;
18341 }
18342 str += len;
18343 }
18344 }
18345 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018346#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018347
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018348 list_append_string(rettv->vval.v_list, word, len);
18349 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018350 attr == HLF_SPB ? "bad" :
18351 attr == HLF_SPR ? "rare" :
18352 attr == HLF_SPL ? "local" :
18353 attr == HLF_SPC ? "caps" :
18354 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018355}
18356
18357/*
18358 * "spellsuggest()" function
18359 */
18360 static void
18361f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018362 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018363 typval_T *rettv;
18364{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018365#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018366 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018367 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018368 int maxcount;
18369 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018370 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018371 listitem_T *li;
18372 int need_capital = FALSE;
18373#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018374
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018375 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018376 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018377
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018378#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018379 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018380 {
18381 str = get_tv_string(&argvars[0]);
18382 if (argvars[1].v_type != VAR_UNKNOWN)
18383 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018384 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018385 if (maxcount <= 0)
18386 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018387 if (argvars[2].v_type != VAR_UNKNOWN)
18388 {
18389 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18390 if (typeerr)
18391 return;
18392 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018393 }
18394 else
18395 maxcount = 25;
18396
Bram Moolenaar4770d092006-01-12 23:22:24 +000018397 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018398
18399 for (i = 0; i < ga.ga_len; ++i)
18400 {
18401 str = ((char_u **)ga.ga_data)[i];
18402
18403 li = listitem_alloc();
18404 if (li == NULL)
18405 vim_free(str);
18406 else
18407 {
18408 li->li_tv.v_type = VAR_STRING;
18409 li->li_tv.v_lock = 0;
18410 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018411 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018412 }
18413 }
18414 ga_clear(&ga);
18415 }
18416#endif
18417}
18418
Bram Moolenaar0d660222005-01-07 21:51:51 +000018419 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018420f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018421 typval_T *argvars;
18422 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018423{
18424 char_u *str;
18425 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018426 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018427 regmatch_T regmatch;
18428 char_u patbuf[NUMBUFLEN];
18429 char_u *save_cpo;
18430 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018431 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018432 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018433 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018434
18435 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18436 save_cpo = p_cpo;
18437 p_cpo = (char_u *)"";
18438
18439 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018440 if (argvars[1].v_type != VAR_UNKNOWN)
18441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018442 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18443 if (pat == NULL)
18444 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018445 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018446 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018447 }
18448 if (pat == NULL || *pat == NUL)
18449 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018450
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018451 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018453 if (typeerr)
18454 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455
Bram Moolenaar0d660222005-01-07 21:51:51 +000018456 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18457 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018459 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018460 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018461 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018462 if (*str == NUL)
18463 match = FALSE; /* empty item at the end */
18464 else
18465 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018466 if (match)
18467 end = regmatch.startp[0];
18468 else
18469 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018470 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18471 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018472 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018473 if (list_append_string(rettv->vval.v_list, str,
18474 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018475 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018476 }
18477 if (!match)
18478 break;
18479 /* Advance to just after the match. */
18480 if (regmatch.endp[0] > str)
18481 col = 0;
18482 else
18483 {
18484 /* Don't get stuck at the same match. */
18485#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018486 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018487#else
18488 col = 1;
18489#endif
18490 }
18491 str = regmatch.endp[0];
18492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493
Bram Moolenaar473de612013-06-08 18:19:48 +020018494 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496
Bram Moolenaar0d660222005-01-07 21:51:51 +000018497 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498}
18499
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018500#ifdef FEAT_FLOAT
18501/*
18502 * "sqrt()" function
18503 */
18504 static void
18505f_sqrt(argvars, rettv)
18506 typval_T *argvars;
18507 typval_T *rettv;
18508{
18509 float_T f;
18510
18511 rettv->v_type = VAR_FLOAT;
18512 if (get_float_arg(argvars, &f) == OK)
18513 rettv->vval.v_float = sqrt(f);
18514 else
18515 rettv->vval.v_float = 0.0;
18516}
18517
18518/*
18519 * "str2float()" function
18520 */
18521 static void
18522f_str2float(argvars, rettv)
18523 typval_T *argvars;
18524 typval_T *rettv;
18525{
18526 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18527
18528 if (*p == '+')
18529 p = skipwhite(p + 1);
18530 (void)string2float(p, &rettv->vval.v_float);
18531 rettv->v_type = VAR_FLOAT;
18532}
18533#endif
18534
Bram Moolenaar2c932302006-03-18 21:42:09 +000018535/*
18536 * "str2nr()" function
18537 */
18538 static void
18539f_str2nr(argvars, rettv)
18540 typval_T *argvars;
18541 typval_T *rettv;
18542{
18543 int base = 10;
18544 char_u *p;
18545 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018546 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018547
18548 if (argvars[1].v_type != VAR_UNKNOWN)
18549 {
18550 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018551 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018552 {
18553 EMSG(_(e_invarg));
18554 return;
18555 }
18556 }
18557
18558 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018559 if (*p == '+')
18560 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018561 switch (base)
18562 {
18563 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18564 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18565 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18566 default: what = 0;
18567 }
18568 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018569 rettv->vval.v_number = n;
18570}
18571
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572#ifdef HAVE_STRFTIME
18573/*
18574 * "strftime({format}[, {time}])" function
18575 */
18576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018577f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018578 typval_T *argvars;
18579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580{
18581 char_u result_buf[256];
18582 struct tm *curtime;
18583 time_t seconds;
18584 char_u *p;
18585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018586 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018588 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018589 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 seconds = time(NULL);
18591 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018592 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 curtime = localtime(&seconds);
18594 /* MSVC returns NULL for an invalid value of seconds. */
18595 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018596 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 else
18598 {
18599# ifdef FEAT_MBYTE
18600 vimconv_T conv;
18601 char_u *enc;
18602
18603 conv.vc_type = CONV_NONE;
18604 enc = enc_locale();
18605 convert_setup(&conv, p_enc, enc);
18606 if (conv.vc_type != CONV_NONE)
18607 p = string_convert(&conv, p, NULL);
18608# endif
18609 if (p != NULL)
18610 (void)strftime((char *)result_buf, sizeof(result_buf),
18611 (char *)p, curtime);
18612 else
18613 result_buf[0] = NUL;
18614
18615# ifdef FEAT_MBYTE
18616 if (conv.vc_type != CONV_NONE)
18617 vim_free(p);
18618 convert_setup(&conv, enc, p_enc);
18619 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018620 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 else
18622# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018623 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624
18625# ifdef FEAT_MBYTE
18626 /* Release conversion descriptors */
18627 convert_setup(&conv, NULL, NULL);
18628 vim_free(enc);
18629# endif
18630 }
18631}
18632#endif
18633
18634/*
18635 * "stridx()" function
18636 */
18637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018638f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018639 typval_T *argvars;
18640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641{
18642 char_u buf[NUMBUFLEN];
18643 char_u *needle;
18644 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018645 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018647 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018649 needle = get_tv_string_chk(&argvars[1]);
18650 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018651 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018652 if (needle == NULL || haystack == NULL)
18653 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654
Bram Moolenaar33570922005-01-25 22:26:29 +000018655 if (argvars[2].v_type != VAR_UNKNOWN)
18656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018657 int error = FALSE;
18658
18659 start_idx = get_tv_number_chk(&argvars[2], &error);
18660 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018661 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018662 if (start_idx >= 0)
18663 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018664 }
18665
18666 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18667 if (pos != NULL)
18668 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669}
18670
18671/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018672 * "string()" function
18673 */
18674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018675f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018676 typval_T *argvars;
18677 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018678{
18679 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018680 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018682 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018683 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018684 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018685 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018686 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687}
18688
18689/*
18690 * "strlen()" function
18691 */
18692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018693f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018694 typval_T *argvars;
18695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697 rettv->vval.v_number = (varnumber_T)(STRLEN(
18698 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699}
18700
18701/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018702 * "strchars()" function
18703 */
18704 static void
18705f_strchars(argvars, rettv)
18706 typval_T *argvars;
18707 typval_T *rettv;
18708{
18709 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018710 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018711#ifdef FEAT_MBYTE
18712 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018713 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018714#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018715
18716 if (argvars[1].v_type != VAR_UNKNOWN)
18717 skipcc = get_tv_number_chk(&argvars[1], NULL);
18718 if (skipcc < 0 || skipcc > 1)
18719 EMSG(_(e_invarg));
18720 else
18721 {
18722#ifdef FEAT_MBYTE
18723 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18724 while (*s != NUL)
18725 {
18726 func_mb_ptr2char_adv(&s);
18727 ++len;
18728 }
18729 rettv->vval.v_number = len;
18730#else
18731 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18732#endif
18733 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018734}
18735
18736/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018737 * "strdisplaywidth()" function
18738 */
18739 static void
18740f_strdisplaywidth(argvars, rettv)
18741 typval_T *argvars;
18742 typval_T *rettv;
18743{
18744 char_u *s = get_tv_string(&argvars[0]);
18745 int col = 0;
18746
18747 if (argvars[1].v_type != VAR_UNKNOWN)
18748 col = get_tv_number(&argvars[1]);
18749
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018750 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018751}
18752
18753/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018754 * "strwidth()" function
18755 */
18756 static void
18757f_strwidth(argvars, rettv)
18758 typval_T *argvars;
18759 typval_T *rettv;
18760{
18761 char_u *s = get_tv_string(&argvars[0]);
18762
18763 rettv->vval.v_number = (varnumber_T)(
18764#ifdef FEAT_MBYTE
18765 mb_string2cells(s, -1)
18766#else
18767 STRLEN(s)
18768#endif
18769 );
18770}
18771
18772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773 * "strpart()" function
18774 */
18775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018776f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018777 typval_T *argvars;
18778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779{
18780 char_u *p;
18781 int n;
18782 int len;
18783 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018784 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 slen = (int)STRLEN(p);
18788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018789 n = get_tv_number_chk(&argvars[1], &error);
18790 if (error)
18791 len = 0;
18792 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018793 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 else
18795 len = slen - n; /* default len: all bytes that are available. */
18796
18797 /*
18798 * Only return the overlap between the specified part and the actual
18799 * string.
18800 */
18801 if (n < 0)
18802 {
18803 len += n;
18804 n = 0;
18805 }
18806 else if (n > slen)
18807 n = slen;
18808 if (len < 0)
18809 len = 0;
18810 else if (n + len > slen)
18811 len = slen - n;
18812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018813 rettv->v_type = VAR_STRING;
18814 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815}
18816
18817/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018818 * "strridx()" function
18819 */
18820 static void
18821f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018822 typval_T *argvars;
18823 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018824{
18825 char_u buf[NUMBUFLEN];
18826 char_u *needle;
18827 char_u *haystack;
18828 char_u *rest;
18829 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018830 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018832 needle = get_tv_string_chk(&argvars[1]);
18833 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018834
18835 rettv->vval.v_number = -1;
18836 if (needle == NULL || haystack == NULL)
18837 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018838
18839 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018840 if (argvars[2].v_type != VAR_UNKNOWN)
18841 {
18842 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018843 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018844 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018845 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018846 }
18847 else
18848 end_idx = haystack_len;
18849
Bram Moolenaar0d660222005-01-07 21:51:51 +000018850 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018851 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018852 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018853 lastmatch = haystack + end_idx;
18854 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018855 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018856 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018857 for (rest = haystack; *rest != '\0'; ++rest)
18858 {
18859 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018860 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018861 break;
18862 lastmatch = rest;
18863 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018864 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018865
18866 if (lastmatch == NULL)
18867 rettv->vval.v_number = -1;
18868 else
18869 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18870}
18871
18872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873 * "strtrans()" function
18874 */
18875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018876f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018877 typval_T *argvars;
18878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018880 rettv->v_type = VAR_STRING;
18881 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882}
18883
18884/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018885 * "submatch()" function
18886 */
18887 static void
18888f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018889 typval_T *argvars;
18890 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018891{
Bram Moolenaar41571762014-04-02 19:00:58 +020018892 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018893 int no;
18894 int retList = 0;
18895
18896 no = (int)get_tv_number_chk(&argvars[0], &error);
18897 if (error)
18898 return;
18899 error = FALSE;
18900 if (argvars[1].v_type != VAR_UNKNOWN)
18901 retList = get_tv_number_chk(&argvars[1], &error);
18902 if (error)
18903 return;
18904
18905 if (retList == 0)
18906 {
18907 rettv->v_type = VAR_STRING;
18908 rettv->vval.v_string = reg_submatch(no);
18909 }
18910 else
18911 {
18912 rettv->v_type = VAR_LIST;
18913 rettv->vval.v_list = reg_submatch_list(no);
18914 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018915}
18916
18917/*
18918 * "substitute()" function
18919 */
18920 static void
18921f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018922 typval_T *argvars;
18923 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018924{
18925 char_u patbuf[NUMBUFLEN];
18926 char_u subbuf[NUMBUFLEN];
18927 char_u flagsbuf[NUMBUFLEN];
18928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018929 char_u *str = get_tv_string_chk(&argvars[0]);
18930 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18931 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18932 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18933
Bram Moolenaar0d660222005-01-07 21:51:51 +000018934 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018935 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18936 rettv->vval.v_string = NULL;
18937 else
18938 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018939}
18940
18941/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018942 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018945f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018946 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948{
18949 int id = 0;
18950#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018951 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952 long col;
18953 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018954 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018956 lnum = get_tv_lnum(argvars); /* -1 on type error */
18957 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18958 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018960 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018961 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018962 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963#endif
18964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018965 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966}
18967
18968/*
18969 * "synIDattr(id, what [, mode])" function
18970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018972f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018973 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975{
18976 char_u *p = NULL;
18977#ifdef FEAT_SYN_HL
18978 int id;
18979 char_u *what;
18980 char_u *mode;
18981 char_u modebuf[NUMBUFLEN];
18982 int modec;
18983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018984 id = get_tv_number(&argvars[0]);
18985 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018986 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018988 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018990 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 modec = 0; /* replace invalid with current */
18992 }
18993 else
18994 {
18995#ifdef FEAT_GUI
18996 if (gui.in_use)
18997 modec = 'g';
18998 else
18999#endif
19000 if (t_colors > 1)
19001 modec = 'c';
19002 else
19003 modec = 't';
19004 }
19005
19006
19007 switch (TOLOWER_ASC(what[0]))
19008 {
19009 case 'b':
19010 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19011 p = highlight_color(id, what, modec);
19012 else /* bold */
19013 p = highlight_has_attr(id, HL_BOLD, modec);
19014 break;
19015
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019016 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 p = highlight_color(id, what, modec);
19018 break;
19019
19020 case 'i':
19021 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19022 p = highlight_has_attr(id, HL_INVERSE, modec);
19023 else /* italic */
19024 p = highlight_has_attr(id, HL_ITALIC, modec);
19025 break;
19026
19027 case 'n': /* name */
19028 p = get_highlight_name(NULL, id - 1);
19029 break;
19030
19031 case 'r': /* reverse */
19032 p = highlight_has_attr(id, HL_INVERSE, modec);
19033 break;
19034
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019035 case 's':
19036 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19037 p = highlight_color(id, what, modec);
19038 else /* standout */
19039 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 break;
19041
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019042 case 'u':
19043 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19044 /* underline */
19045 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19046 else
19047 /* undercurl */
19048 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049 break;
19050 }
19051
19052 if (p != NULL)
19053 p = vim_strsave(p);
19054#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019055 rettv->v_type = VAR_STRING;
19056 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057}
19058
19059/*
19060 * "synIDtrans(id)" function
19061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019063f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019064 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066{
19067 int id;
19068
19069#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019070 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071
19072 if (id > 0)
19073 id = syn_get_final_id(id);
19074 else
19075#endif
19076 id = 0;
19077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019078 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079}
19080
19081/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019082 * "synconcealed(lnum, col)" function
19083 */
19084 static void
19085f_synconcealed(argvars, rettv)
19086 typval_T *argvars UNUSED;
19087 typval_T *rettv;
19088{
19089#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19090 long lnum;
19091 long col;
19092 int syntax_flags = 0;
19093 int cchar;
19094 int matchid = 0;
19095 char_u str[NUMBUFLEN];
19096#endif
19097
19098 rettv->v_type = VAR_LIST;
19099 rettv->vval.v_list = NULL;
19100
19101#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19102 lnum = get_tv_lnum(argvars); /* -1 on type error */
19103 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19104
19105 vim_memset(str, NUL, sizeof(str));
19106
19107 if (rettv_list_alloc(rettv) != FAIL)
19108 {
19109 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19110 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19111 && curwin->w_p_cole > 0)
19112 {
19113 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19114 syntax_flags = get_syntax_info(&matchid);
19115
19116 /* get the conceal character */
19117 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19118 {
19119 cchar = syn_get_sub_char();
19120 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19121 cchar = lcs_conceal;
19122 if (cchar != NUL)
19123 {
19124# ifdef FEAT_MBYTE
19125 if (has_mbyte)
19126 (*mb_char2bytes)(cchar, str);
19127 else
19128# endif
19129 str[0] = cchar;
19130 }
19131 }
19132 }
19133
19134 list_append_number(rettv->vval.v_list,
19135 (syntax_flags & HL_CONCEAL) != 0);
19136 /* -1 to auto-determine strlen */
19137 list_append_string(rettv->vval.v_list, str, -1);
19138 list_append_number(rettv->vval.v_list, matchid);
19139 }
19140#endif
19141}
19142
19143/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019144 * "synstack(lnum, col)" function
19145 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019146 static void
19147f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019148 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019149 typval_T *rettv;
19150{
19151#ifdef FEAT_SYN_HL
19152 long lnum;
19153 long col;
19154 int i;
19155 int id;
19156#endif
19157
19158 rettv->v_type = VAR_LIST;
19159 rettv->vval.v_list = NULL;
19160
19161#ifdef FEAT_SYN_HL
19162 lnum = get_tv_lnum(argvars); /* -1 on type error */
19163 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19164
19165 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019166 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019167 && rettv_list_alloc(rettv) != FAIL)
19168 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019169 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019170 for (i = 0; ; ++i)
19171 {
19172 id = syn_get_stack_item(i);
19173 if (id < 0)
19174 break;
19175 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19176 break;
19177 }
19178 }
19179#endif
19180}
19181
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019183get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019184 typval_T *argvars;
19185 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019186 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019188 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019190 char_u *infile = NULL;
19191 char_u buf[NUMBUFLEN];
19192 int err = FALSE;
19193 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019194 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019195 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019197 rettv->v_type = VAR_STRING;
19198 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019199 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019200 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019201
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019202 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019203 {
19204 /*
19205 * Write the string to a temp file, to be used for input of the shell
19206 * command.
19207 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019208 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019209 {
19210 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019211 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019212 }
19213
19214 fd = mch_fopen((char *)infile, WRITEBIN);
19215 if (fd == NULL)
19216 {
19217 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019218 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019219 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019220 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019221 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019222 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19223 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019224 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019225 else
19226 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019227 size_t len;
19228
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019229 p = get_tv_string_buf_chk(&argvars[1], buf);
19230 if (p == NULL)
19231 {
19232 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019233 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019234 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019235 len = STRLEN(p);
19236 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019237 err = TRUE;
19238 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019239 if (fclose(fd) != 0)
19240 err = TRUE;
19241 if (err)
19242 {
19243 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019244 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019245 }
19246 }
19247
Bram Moolenaar52a72462014-08-29 15:53:52 +020019248 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19249 * echoes typeahead, that messes up the display. */
19250 if (!msg_silent)
19251 flags += SHELL_COOKED;
19252
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019253 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019255 int len;
19256 listitem_T *li;
19257 char_u *s = NULL;
19258 char_u *start;
19259 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019260 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261
Bram Moolenaar52a72462014-08-29 15:53:52 +020019262 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019263 if (res == NULL)
19264 goto errret;
19265
19266 list = list_alloc();
19267 if (list == NULL)
19268 goto errret;
19269
19270 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019272 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019273 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019274 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019275 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019276
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019277 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019278 if (s == NULL)
19279 goto errret;
19280
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019281 for (p = s; start < end; ++p, ++start)
19282 *p = *start == NUL ? NL : *start;
19283 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019284
19285 li = listitem_alloc();
19286 if (li == NULL)
19287 {
19288 vim_free(s);
19289 goto errret;
19290 }
19291 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019292 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019293 li->li_tv.vval.v_string = s;
19294 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019296
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019297 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019298 rettv->v_type = VAR_LIST;
19299 rettv->vval.v_list = list;
19300 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019302 else
19303 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019304 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019305#ifdef USE_CR
19306 /* translate <CR> into <NL> */
19307 if (res != NULL)
19308 {
19309 char_u *s;
19310
19311 for (s = res; *s; ++s)
19312 {
19313 if (*s == CAR)
19314 *s = NL;
19315 }
19316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317#else
19318# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019319 /* translate <CR><NL> into <NL> */
19320 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019322 char_u *s, *d;
19323
19324 d = res;
19325 for (s = res; *s; ++s)
19326 {
19327 if (s[0] == CAR && s[1] == NL)
19328 ++s;
19329 *d++ = *s;
19330 }
19331 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333# endif
19334#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019335 rettv->vval.v_string = res;
19336 res = NULL;
19337 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019338
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019339errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019340 if (infile != NULL)
19341 {
19342 mch_remove(infile);
19343 vim_free(infile);
19344 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019345 if (res != NULL)
19346 vim_free(res);
19347 if (list != NULL)
19348 list_free(list, TRUE);
19349}
19350
19351/*
19352 * "system()" function
19353 */
19354 static void
19355f_system(argvars, rettv)
19356 typval_T *argvars;
19357 typval_T *rettv;
19358{
19359 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19360}
19361
19362/*
19363 * "systemlist()" function
19364 */
19365 static void
19366f_systemlist(argvars, rettv)
19367 typval_T *argvars;
19368 typval_T *rettv;
19369{
19370 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371}
19372
19373/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019374 * "tabpagebuflist()" function
19375 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019376 static void
19377f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019378 typval_T *argvars UNUSED;
19379 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019380{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019381#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019382 tabpage_T *tp;
19383 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019384
19385 if (argvars[0].v_type == VAR_UNKNOWN)
19386 wp = firstwin;
19387 else
19388 {
19389 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19390 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019391 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019392 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019393 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019394 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019395 for (; wp != NULL; wp = wp->w_next)
19396 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019397 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019398 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019399 }
19400#endif
19401}
19402
19403
19404/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019405 * "tabpagenr()" function
19406 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019407 static void
19408f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019409 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019410 typval_T *rettv;
19411{
19412 int nr = 1;
19413#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019414 char_u *arg;
19415
19416 if (argvars[0].v_type != VAR_UNKNOWN)
19417 {
19418 arg = get_tv_string_chk(&argvars[0]);
19419 nr = 0;
19420 if (arg != NULL)
19421 {
19422 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019423 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019424 else
19425 EMSG2(_(e_invexpr2), arg);
19426 }
19427 }
19428 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019429 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019430#endif
19431 rettv->vval.v_number = nr;
19432}
19433
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019434
19435#ifdef FEAT_WINDOWS
19436static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19437
19438/*
19439 * Common code for tabpagewinnr() and winnr().
19440 */
19441 static int
19442get_winnr(tp, argvar)
19443 tabpage_T *tp;
19444 typval_T *argvar;
19445{
19446 win_T *twin;
19447 int nr = 1;
19448 win_T *wp;
19449 char_u *arg;
19450
19451 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19452 if (argvar->v_type != VAR_UNKNOWN)
19453 {
19454 arg = get_tv_string_chk(argvar);
19455 if (arg == NULL)
19456 nr = 0; /* type error; errmsg already given */
19457 else if (STRCMP(arg, "$") == 0)
19458 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19459 else if (STRCMP(arg, "#") == 0)
19460 {
19461 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19462 if (twin == NULL)
19463 nr = 0;
19464 }
19465 else
19466 {
19467 EMSG2(_(e_invexpr2), arg);
19468 nr = 0;
19469 }
19470 }
19471
19472 if (nr > 0)
19473 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19474 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019475 {
19476 if (wp == NULL)
19477 {
19478 /* didn't find it in this tabpage */
19479 nr = 0;
19480 break;
19481 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019482 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019483 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019484 return nr;
19485}
19486#endif
19487
19488/*
19489 * "tabpagewinnr()" function
19490 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019491 static void
19492f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019493 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019494 typval_T *rettv;
19495{
19496 int nr = 1;
19497#ifdef FEAT_WINDOWS
19498 tabpage_T *tp;
19499
19500 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19501 if (tp == NULL)
19502 nr = 0;
19503 else
19504 nr = get_winnr(tp, &argvars[1]);
19505#endif
19506 rettv->vval.v_number = nr;
19507}
19508
19509
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019510/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019511 * "tagfiles()" function
19512 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019513 static void
19514f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019515 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019516 typval_T *rettv;
19517{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019518 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019519 tagname_T tn;
19520 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019521
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019522 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019523 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019524 fname = alloc(MAXPATHL);
19525 if (fname == NULL)
19526 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019527
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019528 for (first = TRUE; ; first = FALSE)
19529 if (get_tagfname(&tn, first, fname) == FAIL
19530 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019531 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019532 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019533 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019534}
19535
19536/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019537 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019538 */
19539 static void
19540f_taglist(argvars, rettv)
19541 typval_T *argvars;
19542 typval_T *rettv;
19543{
19544 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019545
19546 tag_pattern = get_tv_string(&argvars[0]);
19547
19548 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019549 if (*tag_pattern == NUL)
19550 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019551
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019552 if (rettv_list_alloc(rettv) == OK)
19553 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019554}
19555
19556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 * "tempname()" function
19558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019560f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019561 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563{
19564 static int x = 'A';
19565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019566 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019567 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568
19569 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19570 * names. Skip 'I' and 'O', they are used for shell redirection. */
19571 do
19572 {
19573 if (x == 'Z')
19574 x = '0';
19575 else if (x == '9')
19576 x = 'A';
19577 else
19578 {
19579#ifdef EBCDIC
19580 if (x == 'I')
19581 x = 'J';
19582 else if (x == 'R')
19583 x = 'S';
19584 else
19585#endif
19586 ++x;
19587 }
19588 } while (x == 'I' || x == 'O');
19589}
19590
19591/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019592 * "test(list)" function: Just checking the walls...
19593 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019594 static void
19595f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019596 typval_T *argvars UNUSED;
19597 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019598{
19599 /* Used for unit testing. Change the code below to your liking. */
19600#if 0
19601 listitem_T *li;
19602 list_T *l;
19603 char_u *bad, *good;
19604
19605 if (argvars[0].v_type != VAR_LIST)
19606 return;
19607 l = argvars[0].vval.v_list;
19608 if (l == NULL)
19609 return;
19610 li = l->lv_first;
19611 if (li == NULL)
19612 return;
19613 bad = get_tv_string(&li->li_tv);
19614 li = li->li_next;
19615 if (li == NULL)
19616 return;
19617 good = get_tv_string(&li->li_tv);
19618 rettv->vval.v_number = test_edit_score(bad, good);
19619#endif
19620}
19621
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019622#ifdef FEAT_FLOAT
19623/*
19624 * "tan()" function
19625 */
19626 static void
19627f_tan(argvars, rettv)
19628 typval_T *argvars;
19629 typval_T *rettv;
19630{
19631 float_T f;
19632
19633 rettv->v_type = VAR_FLOAT;
19634 if (get_float_arg(argvars, &f) == OK)
19635 rettv->vval.v_float = tan(f);
19636 else
19637 rettv->vval.v_float = 0.0;
19638}
19639
19640/*
19641 * "tanh()" function
19642 */
19643 static void
19644f_tanh(argvars, rettv)
19645 typval_T *argvars;
19646 typval_T *rettv;
19647{
19648 float_T f;
19649
19650 rettv->v_type = VAR_FLOAT;
19651 if (get_float_arg(argvars, &f) == OK)
19652 rettv->vval.v_float = tanh(f);
19653 else
19654 rettv->vval.v_float = 0.0;
19655}
19656#endif
19657
Bram Moolenaard52d9742005-08-21 22:20:28 +000019658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 * "tolower(string)" function
19660 */
19661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019662f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019663 typval_T *argvars;
19664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665{
19666 char_u *p;
19667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019668 p = vim_strsave(get_tv_string(&argvars[0]));
19669 rettv->v_type = VAR_STRING;
19670 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671
19672 if (p != NULL)
19673 while (*p != NUL)
19674 {
19675#ifdef FEAT_MBYTE
19676 int l;
19677
19678 if (enc_utf8)
19679 {
19680 int c, lc;
19681
19682 c = utf_ptr2char(p);
19683 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019684 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 /* TODO: reallocate string when byte count changes. */
19686 if (utf_char2len(lc) == l)
19687 utf_char2bytes(lc, p);
19688 p += l;
19689 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019690 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 p += l; /* skip multi-byte character */
19692 else
19693#endif
19694 {
19695 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19696 ++p;
19697 }
19698 }
19699}
19700
19701/*
19702 * "toupper(string)" function
19703 */
19704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019705f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019706 typval_T *argvars;
19707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019709 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019710 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711}
19712
19713/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019714 * "tr(string, fromstr, tostr)" function
19715 */
19716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019717f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019718 typval_T *argvars;
19719 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019720{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019721 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019722 char_u *fromstr;
19723 char_u *tostr;
19724 char_u *p;
19725#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019726 int inlen;
19727 int fromlen;
19728 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019729 int idx;
19730 char_u *cpstr;
19731 int cplen;
19732 int first = TRUE;
19733#endif
19734 char_u buf[NUMBUFLEN];
19735 char_u buf2[NUMBUFLEN];
19736 garray_T ga;
19737
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019738 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019739 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19740 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019741
19742 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019743 rettv->v_type = VAR_STRING;
19744 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019745 if (fromstr == NULL || tostr == NULL)
19746 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019747 ga_init2(&ga, (int)sizeof(char), 80);
19748
19749#ifdef FEAT_MBYTE
19750 if (!has_mbyte)
19751#endif
19752 /* not multi-byte: fromstr and tostr must be the same length */
19753 if (STRLEN(fromstr) != STRLEN(tostr))
19754 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019755#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019756error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019757#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019758 EMSG2(_(e_invarg2), fromstr);
19759 ga_clear(&ga);
19760 return;
19761 }
19762
19763 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019764 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019765 {
19766#ifdef FEAT_MBYTE
19767 if (has_mbyte)
19768 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019769 inlen = (*mb_ptr2len)(in_str);
19770 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019771 cplen = inlen;
19772 idx = 0;
19773 for (p = fromstr; *p != NUL; p += fromlen)
19774 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019775 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019776 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019777 {
19778 for (p = tostr; *p != NUL; p += tolen)
19779 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019780 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019781 if (idx-- == 0)
19782 {
19783 cplen = tolen;
19784 cpstr = p;
19785 break;
19786 }
19787 }
19788 if (*p == NUL) /* tostr is shorter than fromstr */
19789 goto error;
19790 break;
19791 }
19792 ++idx;
19793 }
19794
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019795 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019796 {
19797 /* Check that fromstr and tostr have the same number of
19798 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019799 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019800 first = FALSE;
19801 for (p = tostr; *p != NUL; p += tolen)
19802 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019803 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019804 --idx;
19805 }
19806 if (idx != 0)
19807 goto error;
19808 }
19809
Bram Moolenaarcde88542015-08-11 19:14:00 +020019810 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019811 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019812 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019813
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019814 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019815 }
19816 else
19817#endif
19818 {
19819 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019820 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019821 if (p != NULL)
19822 ga_append(&ga, tostr[p - fromstr]);
19823 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019824 ga_append(&ga, *in_str);
19825 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019826 }
19827 }
19828
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019829 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019830 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019831 ga_append(&ga, NUL);
19832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019833 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019834}
19835
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019836#ifdef FEAT_FLOAT
19837/*
19838 * "trunc({float})" function
19839 */
19840 static void
19841f_trunc(argvars, rettv)
19842 typval_T *argvars;
19843 typval_T *rettv;
19844{
19845 float_T f;
19846
19847 rettv->v_type = VAR_FLOAT;
19848 if (get_float_arg(argvars, &f) == OK)
19849 /* trunc() is not in C90, use floor() or ceil() instead. */
19850 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19851 else
19852 rettv->vval.v_float = 0.0;
19853}
19854#endif
19855
Bram Moolenaar8299df92004-07-10 09:47:34 +000019856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857 * "type(expr)" function
19858 */
19859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019860f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019861 typval_T *argvars;
19862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019864 int n;
19865
19866 switch (argvars[0].v_type)
19867 {
19868 case VAR_NUMBER: n = 0; break;
19869 case VAR_STRING: n = 1; break;
19870 case VAR_FUNC: n = 2; break;
19871 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019872 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019873#ifdef FEAT_FLOAT
19874 case VAR_FLOAT: n = 5; break;
19875#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019876 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19877 }
19878 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879}
19880
19881/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019882 * "undofile(name)" function
19883 */
19884 static void
19885f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019886 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019887 typval_T *rettv;
19888{
19889 rettv->v_type = VAR_STRING;
19890#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019891 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019892 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019893
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019894 if (*fname == NUL)
19895 {
19896 /* If there is no file name there will be no undo file. */
19897 rettv->vval.v_string = NULL;
19898 }
19899 else
19900 {
19901 char_u *ffname = FullName_save(fname, FALSE);
19902
19903 if (ffname != NULL)
19904 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19905 vim_free(ffname);
19906 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019907 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019908#else
19909 rettv->vval.v_string = NULL;
19910#endif
19911}
19912
19913/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019914 * "undotree()" function
19915 */
19916 static void
19917f_undotree(argvars, rettv)
19918 typval_T *argvars UNUSED;
19919 typval_T *rettv;
19920{
19921 if (rettv_dict_alloc(rettv) == OK)
19922 {
19923 dict_T *dict = rettv->vval.v_dict;
19924 list_T *list;
19925
Bram Moolenaar730cde92010-06-27 05:18:54 +020019926 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019927 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019928 dict_add_nr_str(dict, "save_last",
19929 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019930 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19931 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019932 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019933
19934 list = list_alloc();
19935 if (list != NULL)
19936 {
19937 u_eval_tree(curbuf->b_u_oldhead, list);
19938 dict_add_list(dict, "entries", list);
19939 }
19940 }
19941}
19942
19943/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019944 * "values(dict)" function
19945 */
19946 static void
19947f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019948 typval_T *argvars;
19949 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019950{
19951 dict_list(argvars, rettv, 1);
19952}
19953
19954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 * "virtcol(string)" function
19956 */
19957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019958f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019959 typval_T *argvars;
19960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961{
19962 colnr_T vcol = 0;
19963 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019964 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019966 fp = var2fpos(&argvars[0], FALSE, &fnum);
19967 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19968 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 {
19970 getvvcol(curwin, fp, NULL, NULL, &vcol);
19971 ++vcol;
19972 }
19973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975}
19976
19977/*
19978 * "visualmode()" function
19979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019981f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019982 typval_T *argvars UNUSED;
19983 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 char_u str[2];
19986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019987 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 str[0] = curbuf->b_visual_mode_eval;
19989 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019990 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991
19992 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019993 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995}
19996
19997/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019998 * "wildmenumode()" function
19999 */
20000 static void
20001f_wildmenumode(argvars, rettv)
20002 typval_T *argvars UNUSED;
20003 typval_T *rettv UNUSED;
20004{
20005#ifdef FEAT_WILDMENU
20006 if (wild_menu_showing)
20007 rettv->vval.v_number = 1;
20008#endif
20009}
20010
20011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 * "winbufnr(nr)" function
20013 */
20014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020015f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020016 typval_T *argvars;
20017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018{
20019 win_T *wp;
20020
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020021 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020023 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020025 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026}
20027
20028/*
20029 * "wincol()" function
20030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020032f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020033 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035{
20036 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020037 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038}
20039
20040/*
20041 * "winheight(nr)" function
20042 */
20043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020044f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020045 typval_T *argvars;
20046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047{
20048 win_T *wp;
20049
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020050 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020052 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020054 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055}
20056
20057/*
20058 * "winline()" function
20059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020062 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064{
20065 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020066 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067}
20068
20069/*
20070 * "winnr()" function
20071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020073f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020074 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076{
20077 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020078
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020080 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020082 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083}
20084
20085/*
20086 * "winrestcmd()" function
20087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020089f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020090 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092{
20093#ifdef FEAT_WINDOWS
20094 win_T *wp;
20095 int winnr = 1;
20096 garray_T ga;
20097 char_u buf[50];
20098
20099 ga_init2(&ga, (int)sizeof(char), 70);
20100 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20101 {
20102 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20103 ga_concat(&ga, buf);
20104# ifdef FEAT_VERTSPLIT
20105 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20106 ga_concat(&ga, buf);
20107# endif
20108 ++winnr;
20109 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020110 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020112 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020114 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020116 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117}
20118
20119/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020120 * "winrestview()" function
20121 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020122 static void
20123f_winrestview(argvars, rettv)
20124 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020125 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020126{
20127 dict_T *dict;
20128
20129 if (argvars[0].v_type != VAR_DICT
20130 || (dict = argvars[0].vval.v_dict) == NULL)
20131 EMSG(_(e_invarg));
20132 else
20133 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020134 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20135 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20136 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20137 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020138#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020139 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20140 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020141#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020142 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20143 {
20144 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20145 curwin->w_set_curswant = FALSE;
20146 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020147
Bram Moolenaar82c25852014-05-28 16:47:16 +020020148 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20149 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020150#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020151 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20152 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020153#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020154 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20155 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20156 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20157 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020158
20159 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020160 win_new_height(curwin, curwin->w_height);
20161# ifdef FEAT_VERTSPLIT
20162 win_new_width(curwin, W_WIDTH(curwin));
20163# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020164 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020165
Bram Moolenaarb851a962014-10-31 15:45:52 +010020166 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020167 curwin->w_topline = 1;
20168 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20169 curwin->w_topline = curbuf->b_ml.ml_line_count;
20170#ifdef FEAT_DIFF
20171 check_topfill(curwin, TRUE);
20172#endif
20173 }
20174}
20175
20176/*
20177 * "winsaveview()" function
20178 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020179 static void
20180f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020181 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020182 typval_T *rettv;
20183{
20184 dict_T *dict;
20185
Bram Moolenaara800b422010-06-27 01:15:55 +020020186 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020187 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020188 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020189
20190 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20191 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20192#ifdef FEAT_VIRTUALEDIT
20193 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20194#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020195 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020196 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20197
20198 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20199#ifdef FEAT_DIFF
20200 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20201#endif
20202 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20203 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20204}
20205
20206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 * "winwidth(nr)" function
20208 */
20209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020210f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020211 typval_T *argvars;
20212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213{
20214 win_T *wp;
20215
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020216 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 else
20220#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020221 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020223 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224#endif
20225}
20226
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020228 * "wordcount()" function
20229 */
20230 static void
20231f_wordcount(argvars, rettv)
20232 typval_T *argvars UNUSED;
20233 typval_T *rettv;
20234{
20235 if (rettv_dict_alloc(rettv) == FAIL)
20236 return;
20237 cursor_pos_info(rettv->vval.v_dict);
20238}
20239
20240/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020241 * Write list of strings to file
20242 */
20243 static int
20244write_list(fd, list, binary)
20245 FILE *fd;
20246 list_T *list;
20247 int binary;
20248{
20249 listitem_T *li;
20250 int c;
20251 int ret = OK;
20252 char_u *s;
20253
20254 for (li = list->lv_first; li != NULL; li = li->li_next)
20255 {
20256 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20257 {
20258 if (*s == '\n')
20259 c = putc(NUL, fd);
20260 else
20261 c = putc(*s, fd);
20262 if (c == EOF)
20263 {
20264 ret = FAIL;
20265 break;
20266 }
20267 }
20268 if (!binary || li->li_next != NULL)
20269 if (putc('\n', fd) == EOF)
20270 {
20271 ret = FAIL;
20272 break;
20273 }
20274 if (ret == FAIL)
20275 {
20276 EMSG(_(e_write));
20277 break;
20278 }
20279 }
20280 return ret;
20281}
20282
20283/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020284 * "writefile()" function
20285 */
20286 static void
20287f_writefile(argvars, rettv)
20288 typval_T *argvars;
20289 typval_T *rettv;
20290{
20291 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020292 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020293 char_u *fname;
20294 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020295 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020296
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020297 if (check_restricted() || check_secure())
20298 return;
20299
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020300 if (argvars[0].v_type != VAR_LIST)
20301 {
20302 EMSG2(_(e_listarg), "writefile()");
20303 return;
20304 }
20305 if (argvars[0].vval.v_list == NULL)
20306 return;
20307
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020308 if (argvars[2].v_type != VAR_UNKNOWN)
20309 {
20310 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20311 binary = TRUE;
20312 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20313 append = TRUE;
20314 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020315
20316 /* Always open the file in binary mode, library functions have a mind of
20317 * their own about CR-LF conversion. */
20318 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020319 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20320 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020321 {
20322 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20323 ret = -1;
20324 }
20325 else
20326 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020327 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20328 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020329 fclose(fd);
20330 }
20331
20332 rettv->vval.v_number = ret;
20333}
20334
20335/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020336 * "xor(expr, expr)" function
20337 */
20338 static void
20339f_xor(argvars, rettv)
20340 typval_T *argvars;
20341 typval_T *rettv;
20342{
20343 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20344 ^ get_tv_number_chk(&argvars[1], NULL);
20345}
20346
20347
20348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020350 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 */
20352 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020353var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020354 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020355 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020356 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020358 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020360 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361
Bram Moolenaara5525202006-03-02 22:52:09 +000020362 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020363 if (varp->v_type == VAR_LIST)
20364 {
20365 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020366 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020367 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020368 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020369
20370 l = varp->vval.v_list;
20371 if (l == NULL)
20372 return NULL;
20373
20374 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020375 pos.lnum = list_find_nr(l, 0L, &error);
20376 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020377 return NULL; /* invalid line number */
20378
20379 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020380 pos.col = list_find_nr(l, 1L, &error);
20381 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020382 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020383 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020384
20385 /* We accept "$" for the column number: last column. */
20386 li = list_find(l, 1L);
20387 if (li != NULL && li->li_tv.v_type == VAR_STRING
20388 && li->li_tv.vval.v_string != NULL
20389 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20390 pos.col = len + 1;
20391
Bram Moolenaara5525202006-03-02 22:52:09 +000020392 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020393 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020394 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020395 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020396
Bram Moolenaara5525202006-03-02 22:52:09 +000020397#ifdef FEAT_VIRTUALEDIT
20398 /* Get the virtual offset. Defaults to zero. */
20399 pos.coladd = list_find_nr(l, 2L, &error);
20400 if (error)
20401 pos.coladd = 0;
20402#endif
20403
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020404 return &pos;
20405 }
20406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020407 name = get_tv_string_chk(varp);
20408 if (name == NULL)
20409 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020410 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020412 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20413 {
20414 if (VIsual_active)
20415 return &VIsual;
20416 return &curwin->w_cursor;
20417 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020418 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020420 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20422 return NULL;
20423 return pp;
20424 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020425
20426#ifdef FEAT_VIRTUALEDIT
20427 pos.coladd = 0;
20428#endif
20429
Bram Moolenaar477933c2007-07-17 14:32:23 +000020430 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020431 {
20432 pos.col = 0;
20433 if (name[1] == '0') /* "w0": first visible line */
20434 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020435 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020436 pos.lnum = curwin->w_topline;
20437 return &pos;
20438 }
20439 else if (name[1] == '$') /* "w$": last visible line */
20440 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020441 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020442 pos.lnum = curwin->w_botline - 1;
20443 return &pos;
20444 }
20445 }
20446 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020448 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449 {
20450 pos.lnum = curbuf->b_ml.ml_line_count;
20451 pos.col = 0;
20452 }
20453 else
20454 {
20455 pos.lnum = curwin->w_cursor.lnum;
20456 pos.col = (colnr_T)STRLEN(ml_get_curline());
20457 }
20458 return &pos;
20459 }
20460 return NULL;
20461}
20462
20463/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020464 * Convert list in "arg" into a position and optional file number.
20465 * When "fnump" is NULL there is no file number, only 3 items.
20466 * Note that the column is passed on as-is, the caller may want to decrement
20467 * it to use 1 for the first column.
20468 * Return FAIL when conversion is not possible, doesn't check the position for
20469 * validity.
20470 */
20471 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020472list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020473 typval_T *arg;
20474 pos_T *posp;
20475 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020476 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020477{
20478 list_T *l = arg->vval.v_list;
20479 long i = 0;
20480 long n;
20481
Bram Moolenaar493c1782014-05-28 14:34:46 +020020482 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20483 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020484 if (arg->v_type != VAR_LIST
20485 || l == NULL
20486 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020487 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020488 return FAIL;
20489
20490 if (fnump != NULL)
20491 {
20492 n = list_find_nr(l, i++, NULL); /* fnum */
20493 if (n < 0)
20494 return FAIL;
20495 if (n == 0)
20496 n = curbuf->b_fnum; /* current buffer */
20497 *fnump = n;
20498 }
20499
20500 n = list_find_nr(l, i++, NULL); /* lnum */
20501 if (n < 0)
20502 return FAIL;
20503 posp->lnum = n;
20504
20505 n = list_find_nr(l, i++, NULL); /* col */
20506 if (n < 0)
20507 return FAIL;
20508 posp->col = n;
20509
20510#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020511 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020512 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020513 posp->coladd = 0;
20514 else
20515 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020516#endif
20517
Bram Moolenaar493c1782014-05-28 14:34:46 +020020518 if (curswantp != NULL)
20519 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20520
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020521 return OK;
20522}
20523
20524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 * Get the length of an environment variable name.
20526 * Advance "arg" to the first character after the name.
20527 * Return 0 for error.
20528 */
20529 static int
20530get_env_len(arg)
20531 char_u **arg;
20532{
20533 char_u *p;
20534 int len;
20535
20536 for (p = *arg; vim_isIDc(*p); ++p)
20537 ;
20538 if (p == *arg) /* no name found */
20539 return 0;
20540
20541 len = (int)(p - *arg);
20542 *arg = p;
20543 return len;
20544}
20545
20546/*
20547 * Get the length of the name of a function or internal variable.
20548 * "arg" is advanced to the first non-white character after the name.
20549 * Return 0 if something is wrong.
20550 */
20551 static int
20552get_id_len(arg)
20553 char_u **arg;
20554{
20555 char_u *p;
20556 int len;
20557
20558 /* Find the end of the name. */
20559 for (p = *arg; eval_isnamec(*p); ++p)
20560 ;
20561 if (p == *arg) /* no name found */
20562 return 0;
20563
20564 len = (int)(p - *arg);
20565 *arg = skipwhite(p);
20566
20567 return len;
20568}
20569
20570/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020571 * Get the length of the name of a variable or function.
20572 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020574 * Return -1 if curly braces expansion failed.
20575 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 * If the name contains 'magic' {}'s, expand them and return the
20577 * expanded name in an allocated string via 'alias' - caller must free.
20578 */
20579 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020580get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581 char_u **arg;
20582 char_u **alias;
20583 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020584 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585{
20586 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 char_u *p;
20588 char_u *expr_start;
20589 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590
20591 *alias = NULL; /* default to no alias */
20592
20593 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20594 && (*arg)[2] == (int)KE_SNR)
20595 {
20596 /* hard coded <SNR>, already translated */
20597 *arg += 3;
20598 return get_id_len(arg) + 3;
20599 }
20600 len = eval_fname_script(*arg);
20601 if (len > 0)
20602 {
20603 /* literal "<SID>", "s:" or "<SNR>" */
20604 *arg += len;
20605 }
20606
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020608 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020610 p = find_name_end(*arg, &expr_start, &expr_end,
20611 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 if (expr_start != NULL)
20613 {
20614 char_u *temp_string;
20615
20616 if (!evaluate)
20617 {
20618 len += (int)(p - *arg);
20619 *arg = skipwhite(p);
20620 return len;
20621 }
20622
20623 /*
20624 * Include any <SID> etc in the expanded string:
20625 * Thus the -len here.
20626 */
20627 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20628 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020629 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 *alias = temp_string;
20631 *arg = skipwhite(p);
20632 return (int)STRLEN(temp_string);
20633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020634
20635 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020636 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 EMSG2(_(e_invexpr2), *arg);
20638
20639 return len;
20640}
20641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020642/*
20643 * Find the end of a variable or function name, taking care of magic braces.
20644 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20645 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020646 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020647 * Return a pointer to just after the name. Equal to "arg" if there is no
20648 * valid name.
20649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020651find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 char_u *arg;
20653 char_u **expr_start;
20654 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020655 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020657 int mb_nest = 0;
20658 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659 char_u *p;
20660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020661 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020663 *expr_start = NULL;
20664 *expr_end = NULL;
20665 }
20666
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020667 /* Quick check for valid starting character. */
20668 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20669 return arg;
20670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020671 for (p = arg; *p != NUL
20672 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020673 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020674 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020675 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020676 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020677 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020678 if (*p == '\'')
20679 {
20680 /* skip over 'string' to avoid counting [ and ] inside it. */
20681 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20682 ;
20683 if (*p == NUL)
20684 break;
20685 }
20686 else if (*p == '"')
20687 {
20688 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20689 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20690 if (*p == '\\' && p[1] != NUL)
20691 ++p;
20692 if (*p == NUL)
20693 break;
20694 }
20695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020696 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020698 if (*p == '[')
20699 ++br_nest;
20700 else if (*p == ']')
20701 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020704 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020706 if (*p == '{')
20707 {
20708 mb_nest++;
20709 if (expr_start != NULL && *expr_start == NULL)
20710 *expr_start = p;
20711 }
20712 else if (*p == '}')
20713 {
20714 mb_nest--;
20715 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20716 *expr_end = p;
20717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719 }
20720
20721 return p;
20722}
20723
20724/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020725 * Expands out the 'magic' {}'s in a variable/function name.
20726 * Note that this can call itself recursively, to deal with
20727 * constructs like foo{bar}{baz}{bam}
20728 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20729 * "in_start" ^
20730 * "expr_start" ^
20731 * "expr_end" ^
20732 * "in_end" ^
20733 *
20734 * Returns a new allocated string, which the caller must free.
20735 * Returns NULL for failure.
20736 */
20737 static char_u *
20738make_expanded_name(in_start, expr_start, expr_end, in_end)
20739 char_u *in_start;
20740 char_u *expr_start;
20741 char_u *expr_end;
20742 char_u *in_end;
20743{
20744 char_u c1;
20745 char_u *retval = NULL;
20746 char_u *temp_result;
20747 char_u *nextcmd = NULL;
20748
20749 if (expr_end == NULL || in_end == NULL)
20750 return NULL;
20751 *expr_start = NUL;
20752 *expr_end = NUL;
20753 c1 = *in_end;
20754 *in_end = NUL;
20755
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020756 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020757 if (temp_result != NULL && nextcmd == NULL)
20758 {
20759 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20760 + (in_end - expr_end) + 1));
20761 if (retval != NULL)
20762 {
20763 STRCPY(retval, in_start);
20764 STRCAT(retval, temp_result);
20765 STRCAT(retval, expr_end + 1);
20766 }
20767 }
20768 vim_free(temp_result);
20769
20770 *in_end = c1; /* put char back for error messages */
20771 *expr_start = '{';
20772 *expr_end = '}';
20773
20774 if (retval != NULL)
20775 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020776 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020777 if (expr_start != NULL)
20778 {
20779 /* Further expansion! */
20780 temp_result = make_expanded_name(retval, expr_start,
20781 expr_end, temp_result);
20782 vim_free(retval);
20783 retval = temp_result;
20784 }
20785 }
20786
20787 return retval;
20788}
20789
20790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020792 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793 */
20794 static int
20795eval_isnamec(c)
20796 int c;
20797{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020798 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20799}
20800
20801/*
20802 * Return TRUE if character "c" can be used as the first character in a
20803 * variable or function name (excluding '{' and '}').
20804 */
20805 static int
20806eval_isnamec1(c)
20807 int c;
20808{
20809 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810}
20811
20812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 * Set number v: variable to "val".
20814 */
20815 void
20816set_vim_var_nr(idx, val)
20817 int idx;
20818 long val;
20819{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020820 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821}
20822
20823/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020824 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 */
20826 long
20827get_vim_var_nr(idx)
20828 int idx;
20829{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020830 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831}
20832
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020833/*
20834 * Get string v: variable value. Uses a static buffer, can only be used once.
20835 */
20836 char_u *
20837get_vim_var_str(idx)
20838 int idx;
20839{
20840 return get_tv_string(&vimvars[idx].vv_tv);
20841}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020842
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020844 * Get List v: variable value. Caller must take care of reference count when
20845 * needed.
20846 */
20847 list_T *
20848get_vim_var_list(idx)
20849 int idx;
20850{
20851 return vimvars[idx].vv_list;
20852}
20853
20854/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020855 * Set v:char to character "c".
20856 */
20857 void
20858set_vim_var_char(c)
20859 int c;
20860{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020861 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020862
20863#ifdef FEAT_MBYTE
20864 if (has_mbyte)
20865 buf[(*mb_char2bytes)(c, buf)] = NUL;
20866 else
20867#endif
20868 {
20869 buf[0] = c;
20870 buf[1] = NUL;
20871 }
20872 set_vim_var_string(VV_CHAR, buf, -1);
20873}
20874
20875/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020876 * Set v:count to "count" and v:count1 to "count1".
20877 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 */
20879 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020880set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 long count;
20882 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020883 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020885 if (set_prevcount)
20886 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020887 vimvars[VV_COUNT].vv_nr = count;
20888 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889}
20890
20891/*
20892 * Set string v: variable to a copy of "val".
20893 */
20894 void
20895set_vim_var_string(idx, val, len)
20896 int idx;
20897 char_u *val;
20898 int len; /* length of "val" to use or -1 (whole string) */
20899{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020900 /* Need to do this (at least) once, since we can't initialize a union.
20901 * Will always be invoked when "v:progname" is set. */
20902 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20903
Bram Moolenaare9a41262005-01-15 22:18:47 +000020904 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020906 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020908 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020910 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911}
20912
20913/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020914 * Set List v: variable to "val".
20915 */
20916 void
20917set_vim_var_list(idx, val)
20918 int idx;
20919 list_T *val;
20920{
20921 list_unref(vimvars[idx].vv_list);
20922 vimvars[idx].vv_list = val;
20923 if (val != NULL)
20924 ++val->lv_refcount;
20925}
20926
20927/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020928 * Set Dictionary v: variable to "val".
20929 */
20930 void
20931set_vim_var_dict(idx, val)
20932 int idx;
20933 dict_T *val;
20934{
20935 int todo;
20936 hashitem_T *hi;
20937
20938 dict_unref(vimvars[idx].vv_dict);
20939 vimvars[idx].vv_dict = val;
20940 if (val != NULL)
20941 {
20942 ++val->dv_refcount;
20943
20944 /* Set readonly */
20945 todo = (int)val->dv_hashtab.ht_used;
20946 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20947 {
20948 if (HASHITEM_EMPTY(hi))
20949 continue;
20950 --todo;
20951 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20952 }
20953 }
20954}
20955
20956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 * Set v:register if needed.
20958 */
20959 void
20960set_reg_var(c)
20961 int c;
20962{
20963 char_u regname;
20964
20965 if (c == 0 || c == ' ')
20966 regname = '"';
20967 else
20968 regname = c;
20969 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020970 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 set_vim_var_string(VV_REG, &regname, 1);
20972}
20973
20974/*
20975 * Get or set v:exception. If "oldval" == NULL, return the current value.
20976 * Otherwise, restore the value to "oldval" and return NULL.
20977 * Must always be called in pairs to save and restore v:exception! Does not
20978 * take care of memory allocations.
20979 */
20980 char_u *
20981v_exception(oldval)
20982 char_u *oldval;
20983{
20984 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020985 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986
Bram Moolenaare9a41262005-01-15 22:18:47 +000020987 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 return NULL;
20989}
20990
20991/*
20992 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20993 * Otherwise, restore the value to "oldval" and return NULL.
20994 * Must always be called in pairs to save and restore v:throwpoint! Does not
20995 * take care of memory allocations.
20996 */
20997 char_u *
20998v_throwpoint(oldval)
20999 char_u *oldval;
21000{
21001 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021002 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003
Bram Moolenaare9a41262005-01-15 22:18:47 +000021004 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 return NULL;
21006}
21007
21008#if defined(FEAT_AUTOCMD) || defined(PROTO)
21009/*
21010 * Set v:cmdarg.
21011 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21012 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21013 * Must always be called in pairs!
21014 */
21015 char_u *
21016set_cmdarg(eap, oldarg)
21017 exarg_T *eap;
21018 char_u *oldarg;
21019{
21020 char_u *oldval;
21021 char_u *newval;
21022 unsigned len;
21023
Bram Moolenaare9a41262005-01-15 22:18:47 +000021024 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021025 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021027 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021028 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021029 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 }
21031
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021032 if (eap->force_bin == FORCE_BIN)
21033 len = 6;
21034 else if (eap->force_bin == FORCE_NOBIN)
21035 len = 8;
21036 else
21037 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021038
21039 if (eap->read_edit)
21040 len += 7;
21041
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021042 if (eap->force_ff != 0)
21043 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21044# ifdef FEAT_MBYTE
21045 if (eap->force_enc != 0)
21046 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021047 if (eap->bad_char != 0)
21048 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021049# endif
21050
21051 newval = alloc(len + 1);
21052 if (newval == NULL)
21053 return NULL;
21054
21055 if (eap->force_bin == FORCE_BIN)
21056 sprintf((char *)newval, " ++bin");
21057 else if (eap->force_bin == FORCE_NOBIN)
21058 sprintf((char *)newval, " ++nobin");
21059 else
21060 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021061
21062 if (eap->read_edit)
21063 STRCAT(newval, " ++edit");
21064
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021065 if (eap->force_ff != 0)
21066 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21067 eap->cmd + eap->force_ff);
21068# ifdef FEAT_MBYTE
21069 if (eap->force_enc != 0)
21070 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21071 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021072 if (eap->bad_char == BAD_KEEP)
21073 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21074 else if (eap->bad_char == BAD_DROP)
21075 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21076 else if (eap->bad_char != 0)
21077 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021078# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021079 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021080 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081}
21082#endif
21083
21084/*
21085 * Get the value of internal variable "name".
21086 * Return OK or FAIL.
21087 */
21088 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021089get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 char_u *name;
21091 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021092 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021093 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021094 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021095 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096{
21097 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021098 typval_T *tv = NULL;
21099 typval_T atv;
21100 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102
21103 /* truncate the name, so that we can use strcmp() */
21104 cc = name[len];
21105 name[len] = NUL;
21106
21107 /*
21108 * Check for "b:changedtick".
21109 */
21110 if (STRCMP(name, "b:changedtick") == 0)
21111 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021112 atv.v_type = VAR_NUMBER;
21113 atv.vval.v_number = curbuf->b_changedtick;
21114 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 }
21116
21117 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 * Check for user-defined variables.
21119 */
21120 else
21121 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021122 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021124 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021125 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021126 if (dip != NULL)
21127 *dip = v;
21128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 }
21130
Bram Moolenaare9a41262005-01-15 22:18:47 +000021131 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021133 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021134 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 ret = FAIL;
21136 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021137 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021138 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139
21140 name[len] = cc;
21141
21142 return ret;
21143}
21144
21145/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021146 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21147 * Also handle function call with Funcref variable: func(expr)
21148 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21149 */
21150 static int
21151handle_subscript(arg, rettv, evaluate, verbose)
21152 char_u **arg;
21153 typval_T *rettv;
21154 int evaluate; /* do more than finding the end */
21155 int verbose; /* give error messages */
21156{
21157 int ret = OK;
21158 dict_T *selfdict = NULL;
21159 char_u *s;
21160 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021161 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021162
21163 while (ret == OK
21164 && (**arg == '['
21165 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021166 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021167 && !vim_iswhite(*(*arg - 1)))
21168 {
21169 if (**arg == '(')
21170 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021171 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021172 if (evaluate)
21173 {
21174 functv = *rettv;
21175 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021176
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021177 /* Invoke the function. Recursive! */
21178 s = functv.vval.v_string;
21179 }
21180 else
21181 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021182 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021183 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21184 &len, evaluate, selfdict);
21185
21186 /* Clear the funcref afterwards, so that deleting it while
21187 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021188 if (evaluate)
21189 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021190
21191 /* Stop the expression evaluation when immediately aborting on
21192 * error, or when an interrupt occurred or an exception was thrown
21193 * but not caught. */
21194 if (aborting())
21195 {
21196 if (ret == OK)
21197 clear_tv(rettv);
21198 ret = FAIL;
21199 }
21200 dict_unref(selfdict);
21201 selfdict = NULL;
21202 }
21203 else /* **arg == '[' || **arg == '.' */
21204 {
21205 dict_unref(selfdict);
21206 if (rettv->v_type == VAR_DICT)
21207 {
21208 selfdict = rettv->vval.v_dict;
21209 if (selfdict != NULL)
21210 ++selfdict->dv_refcount;
21211 }
21212 else
21213 selfdict = NULL;
21214 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21215 {
21216 clear_tv(rettv);
21217 ret = FAIL;
21218 }
21219 }
21220 }
21221 dict_unref(selfdict);
21222 return ret;
21223}
21224
21225/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021226 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021227 * value).
21228 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021229 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021230alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021231{
Bram Moolenaar33570922005-01-25 22:26:29 +000021232 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021233}
21234
21235/*
21236 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 * The string "s" must have been allocated, it is consumed.
21238 * Return NULL for out of memory, the variable otherwise.
21239 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021240 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021241alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 char_u *s;
21243{
Bram Moolenaar33570922005-01-25 22:26:29 +000021244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021246 rettv = alloc_tv();
21247 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021249 rettv->v_type = VAR_STRING;
21250 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 }
21252 else
21253 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021254 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255}
21256
21257/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021258 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021260 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021261free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021262 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263{
21264 if (varp != NULL)
21265 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021266 switch (varp->v_type)
21267 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021268 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021269 func_unref(varp->vval.v_string);
21270 /*FALLTHROUGH*/
21271 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021272 vim_free(varp->vval.v_string);
21273 break;
21274 case VAR_LIST:
21275 list_unref(varp->vval.v_list);
21276 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021277 case VAR_DICT:
21278 dict_unref(varp->vval.v_dict);
21279 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021280 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021281#ifdef FEAT_FLOAT
21282 case VAR_FLOAT:
21283#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021284 case VAR_UNKNOWN:
21285 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021286 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021287 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021288 break;
21289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 vim_free(varp);
21291 }
21292}
21293
21294/*
21295 * Free the memory for a variable value and set the value to NULL or 0.
21296 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021297 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021298clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021299 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300{
21301 if (varp != NULL)
21302 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021303 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021305 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021306 func_unref(varp->vval.v_string);
21307 /*FALLTHROUGH*/
21308 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021309 vim_free(varp->vval.v_string);
21310 varp->vval.v_string = NULL;
21311 break;
21312 case VAR_LIST:
21313 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021314 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021315 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021316 case VAR_DICT:
21317 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021318 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021319 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021320 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021321 varp->vval.v_number = 0;
21322 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021323#ifdef FEAT_FLOAT
21324 case VAR_FLOAT:
21325 varp->vval.v_float = 0.0;
21326 break;
21327#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021328 case VAR_UNKNOWN:
21329 break;
21330 default:
21331 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021333 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 }
21335}
21336
21337/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021338 * Set the value of a variable to NULL without freeing items.
21339 */
21340 static void
21341init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021342 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021343{
21344 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021345 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021346}
21347
21348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 * Get the number value of a variable.
21350 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021351 * For incompatible types, return 0.
21352 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21353 * caller of incompatible types: it sets *denote to TRUE if "denote"
21354 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 */
21356 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021357get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021358 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021360 int error = FALSE;
21361
21362 return get_tv_number_chk(varp, &error); /* return 0L on error */
21363}
21364
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021365 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021366get_tv_number_chk(varp, denote)
21367 typval_T *varp;
21368 int *denote;
21369{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021370 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021372 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021374 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021375 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021376#ifdef FEAT_FLOAT
21377 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021378 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021379 break;
21380#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021381 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021382 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021383 break;
21384 case VAR_STRING:
21385 if (varp->vval.v_string != NULL)
21386 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021387 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021388 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021389 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021390 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021391 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021392 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021393 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021394 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021395 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021396 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021397 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021399 if (denote == NULL) /* useful for values that must be unsigned */
21400 n = -1;
21401 else
21402 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021403 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404}
21405
21406/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021407 * Get the lnum from the first argument.
21408 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021409 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 */
21411 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021412get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021413 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414{
Bram Moolenaar33570922005-01-25 22:26:29 +000021415 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 linenr_T lnum;
21417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021418 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 if (lnum == 0) /* no valid number, try using line() */
21420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021421 rettv.v_type = VAR_NUMBER;
21422 f_line(argvars, &rettv);
21423 lnum = rettv.vval.v_number;
21424 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 }
21426 return lnum;
21427}
21428
21429/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021430 * Get the lnum from the first argument.
21431 * Also accepts "$", then "buf" is used.
21432 * Returns 0 on error.
21433 */
21434 static linenr_T
21435get_tv_lnum_buf(argvars, buf)
21436 typval_T *argvars;
21437 buf_T *buf;
21438{
21439 if (argvars[0].v_type == VAR_STRING
21440 && argvars[0].vval.v_string != NULL
21441 && argvars[0].vval.v_string[0] == '$'
21442 && buf != NULL)
21443 return buf->b_ml.ml_line_count;
21444 return get_tv_number_chk(&argvars[0], NULL);
21445}
21446
21447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 * Get the string value of a variable.
21449 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021450 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21451 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 * If the String variable has never been set, return an empty string.
21453 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021454 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21455 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 */
21457 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021458get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021459 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460{
21461 static char_u mybuf[NUMBUFLEN];
21462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021463 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464}
21465
21466 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021467get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021468 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021469 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021471 char_u *res = get_tv_string_buf_chk(varp, buf);
21472
21473 return res != NULL ? res : (char_u *)"";
21474}
21475
Bram Moolenaar7d647822014-04-05 21:28:56 +020021476/*
21477 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21478 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021479 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021480get_tv_string_chk(varp)
21481 typval_T *varp;
21482{
21483 static char_u mybuf[NUMBUFLEN];
21484
21485 return get_tv_string_buf_chk(varp, mybuf);
21486}
21487
21488 static char_u *
21489get_tv_string_buf_chk(varp, buf)
21490 typval_T *varp;
21491 char_u *buf;
21492{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021493 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021495 case VAR_NUMBER:
21496 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21497 return buf;
21498 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021499 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021500 break;
21501 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021502 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021503 break;
21504 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021505 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021506 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021507#ifdef FEAT_FLOAT
21508 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021509 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021510 break;
21511#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021512 case VAR_STRING:
21513 if (varp->vval.v_string != NULL)
21514 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021515 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021516 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021517 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021520 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521}
21522
21523/*
21524 * Find variable "name" in the list of variables.
21525 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021526 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021527 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021528 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021530 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021531find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021533 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021534 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021537 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538
Bram Moolenaara7043832005-01-21 11:56:39 +000021539 ht = find_var_ht(name, &varname);
21540 if (htp != NULL)
21541 *htp = ht;
21542 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021544 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545}
21546
21547/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021548 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021549 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021551 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021552find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021553 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021554 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021555 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021556 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021557{
Bram Moolenaar33570922005-01-25 22:26:29 +000021558 hashitem_T *hi;
21559
21560 if (*varname == NUL)
21561 {
21562 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021563 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021564 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021565 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021566 case 'g': return &globvars_var;
21567 case 'v': return &vimvars_var;
21568 case 'b': return &curbuf->b_bufvar;
21569 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021570#ifdef FEAT_WINDOWS
21571 case 't': return &curtab->tp_winvar;
21572#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021573 case 'l': return current_funccal == NULL
21574 ? NULL : &current_funccal->l_vars_var;
21575 case 'a': return current_funccal == NULL
21576 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021577 }
21578 return NULL;
21579 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021580
21581 hi = hash_find(ht, varname);
21582 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021583 {
21584 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021585 * worked find the variable again. Don't auto-load a script if it was
21586 * loaded already, otherwise it would be loaded every time when
21587 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021588 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021589 {
21590 /* Note: script_autoload() may make "hi" invalid. It must either
21591 * be obtained again or not used. */
21592 if (!script_autoload(varname, FALSE) || aborting())
21593 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021594 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021595 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021596 if (HASHITEM_EMPTY(hi))
21597 return NULL;
21598 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021599 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021600}
21601
21602/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021603 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021604 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021605 * Set "varname" to the start of name without ':'.
21606 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021607 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021608find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 char_u *name;
21610 char_u **varname;
21611{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021612 hashitem_T *hi;
21613
Bram Moolenaar73627d02015-08-11 15:46:09 +020021614 if (name[0] == NUL)
21615 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 if (name[1] != ':')
21617 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021618 /* The name must not start with a colon or #. */
21619 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 return NULL;
21621 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021622
21623 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021624 hi = hash_find(&compat_hashtab, name);
21625 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021626 return &compat_hashtab;
21627
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021629 return &globvarht; /* global variable */
21630 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 }
21632 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021633 if (*name == 'g') /* global variable */
21634 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021635 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21636 */
21637 if (vim_strchr(name + 2, ':') != NULL
21638 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021639 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021641 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021643 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021644#ifdef FEAT_WINDOWS
21645 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021646 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021648 if (*name == 'v') /* v: variable */
21649 return &vimvarht;
21650 if (*name == 'a' && current_funccal != NULL) /* function argument */
21651 return &current_funccal->l_avars.dv_hashtab;
21652 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21653 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 if (*name == 's' /* script variable */
21655 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21656 return &SCRIPT_VARS(current_SID);
21657 return NULL;
21658}
21659
21660/*
21661 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021662 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 * Returns NULL when it doesn't exist.
21664 */
21665 char_u *
21666get_var_value(name)
21667 char_u *name;
21668{
Bram Moolenaar33570922005-01-25 22:26:29 +000021669 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021671 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 if (v == NULL)
21673 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021674 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675}
21676
21677/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021678 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 * sourcing this script and when executing functions defined in the script.
21680 */
21681 void
21682new_script_vars(id)
21683 scid_T id;
21684{
Bram Moolenaara7043832005-01-21 11:56:39 +000021685 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021686 hashtab_T *ht;
21687 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021688
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21690 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021691 /* Re-allocating ga_data means that an ht_array pointing to
21692 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021693 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021694 for (i = 1; i <= ga_scripts.ga_len; ++i)
21695 {
21696 ht = &SCRIPT_VARS(i);
21697 if (ht->ht_mask == HT_INIT_SIZE - 1)
21698 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021699 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021700 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021701 }
21702
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 while (ga_scripts.ga_len < id)
21704 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021705 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021706 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021707 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709 }
21710 }
21711}
21712
21713/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021714 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21715 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 */
21717 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021718init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021719 dict_T *dict;
21720 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021721 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722{
Bram Moolenaar33570922005-01-25 22:26:29 +000021723 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021724 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021725 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021726 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021727 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021728 dict_var->di_tv.vval.v_dict = dict;
21729 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021730 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021731 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21732 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733}
21734
21735/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021736 * Unreference a dictionary initialized by init_var_dict().
21737 */
21738 void
21739unref_var_dict(dict)
21740 dict_T *dict;
21741{
21742 /* Now the dict needs to be freed if no one else is using it, go back to
21743 * normal reference counting. */
21744 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21745 dict_unref(dict);
21746}
21747
21748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021750 * Frees all allocated variables and the value they contain.
21751 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 */
21753 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021754vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021755 hashtab_T *ht;
21756{
21757 vars_clear_ext(ht, TRUE);
21758}
21759
21760/*
21761 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21762 */
21763 static void
21764vars_clear_ext(ht, free_val)
21765 hashtab_T *ht;
21766 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767{
Bram Moolenaara7043832005-01-21 11:56:39 +000021768 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021769 hashitem_T *hi;
21770 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771
Bram Moolenaar33570922005-01-25 22:26:29 +000021772 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021773 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021774 for (hi = ht->ht_array; todo > 0; ++hi)
21775 {
21776 if (!HASHITEM_EMPTY(hi))
21777 {
21778 --todo;
21779
Bram Moolenaar33570922005-01-25 22:26:29 +000021780 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021781 * ht_array might change then. hash_clear() takes care of it
21782 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021783 v = HI2DI(hi);
21784 if (free_val)
21785 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021786 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021787 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021788 }
21789 }
21790 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021791 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792}
21793
Bram Moolenaara7043832005-01-21 11:56:39 +000021794/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021795 * Delete a variable from hashtab "ht" at item "hi".
21796 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021799delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021800 hashtab_T *ht;
21801 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802{
Bram Moolenaar33570922005-01-25 22:26:29 +000021803 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021804
21805 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021806 clear_tv(&di->di_tv);
21807 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808}
21809
21810/*
21811 * List the value of one internal variable.
21812 */
21813 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021814list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021815 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021817 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021819 char_u *tofree;
21820 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021821 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021822
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021823 current_copyID += COPYID_INC;
21824 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021825 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021826 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021827 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828}
21829
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021831list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 char_u *prefix;
21833 char_u *name;
21834 int type;
21835 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021836 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837{
Bram Moolenaar31859182007-08-14 20:41:13 +000021838 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21839 msg_start();
21840 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 if (name != NULL) /* "a:" vars don't have a name stored */
21842 msg_puts(name);
21843 msg_putchar(' ');
21844 msg_advance(22);
21845 if (type == VAR_NUMBER)
21846 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021847 else if (type == VAR_FUNC)
21848 msg_putchar('*');
21849 else if (type == VAR_LIST)
21850 {
21851 msg_putchar('[');
21852 if (*string == '[')
21853 ++string;
21854 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021855 else if (type == VAR_DICT)
21856 {
21857 msg_putchar('{');
21858 if (*string == '{')
21859 ++string;
21860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 else
21862 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021863
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021865
21866 if (type == VAR_FUNC)
21867 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021868 if (*first)
21869 {
21870 msg_clr_eos();
21871 *first = FALSE;
21872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873}
21874
21875/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021876 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 * If the variable already exists, the value is updated.
21878 * Otherwise the variable is created.
21879 */
21880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021881set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021883 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021884 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885{
Bram Moolenaar33570922005-01-25 22:26:29 +000021886 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021888 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021890 ht = find_var_ht(name, &varname);
21891 if (ht == NULL || *varname == NUL)
21892 {
21893 EMSG2(_(e_illvar), name);
21894 return;
21895 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021896 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021897
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021898 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21899 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021900
Bram Moolenaar33570922005-01-25 22:26:29 +000021901 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021904 if (var_check_ro(v->di_flags, name, FALSE)
21905 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021906 return;
21907 if (v->di_tv.v_type != tv->v_type
21908 && !((v->di_tv.v_type == VAR_STRING
21909 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021910 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021911 || tv->v_type == VAR_NUMBER))
21912#ifdef FEAT_FLOAT
21913 && !((v->di_tv.v_type == VAR_NUMBER
21914 || v->di_tv.v_type == VAR_FLOAT)
21915 && (tv->v_type == VAR_NUMBER
21916 || tv->v_type == VAR_FLOAT))
21917#endif
21918 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021919 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021920 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021921 return;
21922 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021923
21924 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021925 * Handle setting internal v: variables separately where needed to
21926 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021927 */
21928 if (ht == &vimvarht)
21929 {
21930 if (v->di_tv.v_type == VAR_STRING)
21931 {
21932 vim_free(v->di_tv.vval.v_string);
21933 if (copy || tv->v_type != VAR_STRING)
21934 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21935 else
21936 {
21937 /* Take over the string to avoid an extra alloc/free. */
21938 v->di_tv.vval.v_string = tv->vval.v_string;
21939 tv->vval.v_string = NULL;
21940 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021941 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021942 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021943 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021944 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021945 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021946 if (STRCMP(varname, "searchforward") == 0)
21947 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021948#ifdef FEAT_SEARCH_EXTRA
21949 else if (STRCMP(varname, "hlsearch") == 0)
21950 {
21951 no_hlsearch = !v->di_tv.vval.v_number;
21952 redraw_all_later(SOME_VALID);
21953 }
21954#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021955 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021956 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021957 else if (v->di_tv.v_type != tv->v_type)
21958 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021959 }
21960
21961 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 }
21963 else /* add a new variable */
21964 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021965 /* Can't add "v:" variable. */
21966 if (ht == &vimvarht)
21967 {
21968 EMSG2(_(e_illvar), name);
21969 return;
21970 }
21971
Bram Moolenaar92124a32005-06-17 22:03:40 +000021972 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021973 if (!valid_varname(varname))
21974 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021975
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021976 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21977 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021978 if (v == NULL)
21979 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021980 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021981 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021983 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 return;
21985 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021986 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021988
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021989 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021991 else
21992 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021993 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021994 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021995 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997}
21998
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021999/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022000 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022001 * Also give an error message.
22002 */
22003 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022004var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 int flags;
22006 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022007 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022008{
22009 if (flags & DI_FLAGS_RO)
22010 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022011 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022012 return TRUE;
22013 }
22014 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22015 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022016 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022017 return TRUE;
22018 }
22019 return FALSE;
22020}
22021
22022/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022023 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22024 * Also give an error message.
22025 */
22026 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022027var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022028 int flags;
22029 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022030 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022031{
22032 if (flags & DI_FLAGS_FIX)
22033 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022034 EMSG2(_("E795: Cannot delete variable %s"),
22035 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022036 return TRUE;
22037 }
22038 return FALSE;
22039}
22040
22041/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022042 * Check if a funcref is assigned to a valid variable name.
22043 * Return TRUE and give an error if not.
22044 */
22045 static int
22046var_check_func_name(name, new_var)
22047 char_u *name; /* points to start of variable name */
22048 int new_var; /* TRUE when creating the variable */
22049{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022050 /* Allow for w: b: s: and t:. */
22051 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022052 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22053 ? name[2] : name[0]))
22054 {
22055 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22056 name);
22057 return TRUE;
22058 }
22059 /* Don't allow hiding a function. When "v" is not NULL we might be
22060 * assigning another function to the same var, the type is checked
22061 * below. */
22062 if (new_var && function_exists(name))
22063 {
22064 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22065 name);
22066 return TRUE;
22067 }
22068 return FALSE;
22069}
22070
22071/*
22072 * Check if a variable name is valid.
22073 * Return FALSE and give an error if not.
22074 */
22075 static int
22076valid_varname(varname)
22077 char_u *varname;
22078{
22079 char_u *p;
22080
22081 for (p = varname; *p != NUL; ++p)
22082 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22083 && *p != AUTOLOAD_CHAR)
22084 {
22085 EMSG2(_(e_illvar), varname);
22086 return FALSE;
22087 }
22088 return TRUE;
22089}
22090
22091/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022092 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022093 * Also give an error message, using "name" or _("name") when use_gettext is
22094 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022095 */
22096 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022097tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022098 int lock;
22099 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022100 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022101{
22102 if (lock & VAR_LOCKED)
22103 {
22104 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022105 name == NULL ? (char_u *)_("Unknown")
22106 : use_gettext ? (char_u *)_(name)
22107 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022108 return TRUE;
22109 }
22110 if (lock & VAR_FIXED)
22111 {
22112 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022113 name == NULL ? (char_u *)_("Unknown")
22114 : use_gettext ? (char_u *)_(name)
22115 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022116 return TRUE;
22117 }
22118 return FALSE;
22119}
22120
22121/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022122 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022123 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022124 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022125 * It is OK for "from" and "to" to point to the same item. This is used to
22126 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022127 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022128 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022129copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022130 typval_T *from;
22131 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022133 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022134 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022135 switch (from->v_type)
22136 {
22137 case VAR_NUMBER:
22138 to->vval.v_number = from->vval.v_number;
22139 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022140#ifdef FEAT_FLOAT
22141 case VAR_FLOAT:
22142 to->vval.v_float = from->vval.v_float;
22143 break;
22144#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022145 case VAR_STRING:
22146 case VAR_FUNC:
22147 if (from->vval.v_string == NULL)
22148 to->vval.v_string = NULL;
22149 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022150 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022151 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022152 if (from->v_type == VAR_FUNC)
22153 func_ref(to->vval.v_string);
22154 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022155 break;
22156 case VAR_LIST:
22157 if (from->vval.v_list == NULL)
22158 to->vval.v_list = NULL;
22159 else
22160 {
22161 to->vval.v_list = from->vval.v_list;
22162 ++to->vval.v_list->lv_refcount;
22163 }
22164 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022165 case VAR_DICT:
22166 if (from->vval.v_dict == NULL)
22167 to->vval.v_dict = NULL;
22168 else
22169 {
22170 to->vval.v_dict = from->vval.v_dict;
22171 ++to->vval.v_dict->dv_refcount;
22172 }
22173 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022174 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022175 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022176 break;
22177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178}
22179
22180/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022181 * Make a copy of an item.
22182 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022183 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22184 * reference to an already copied list/dict can be used.
22185 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022186 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022187 static int
22188item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022189 typval_T *from;
22190 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022191 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022192 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022193{
22194 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022195 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022196
Bram Moolenaar33570922005-01-25 22:26:29 +000022197 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022198 {
22199 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022200 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022201 }
22202 ++recurse;
22203
22204 switch (from->v_type)
22205 {
22206 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022207#ifdef FEAT_FLOAT
22208 case VAR_FLOAT:
22209#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022210 case VAR_STRING:
22211 case VAR_FUNC:
22212 copy_tv(from, to);
22213 break;
22214 case VAR_LIST:
22215 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022216 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022217 if (from->vval.v_list == NULL)
22218 to->vval.v_list = NULL;
22219 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22220 {
22221 /* use the copy made earlier */
22222 to->vval.v_list = from->vval.v_list->lv_copylist;
22223 ++to->vval.v_list->lv_refcount;
22224 }
22225 else
22226 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22227 if (to->vval.v_list == NULL)
22228 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022229 break;
22230 case VAR_DICT:
22231 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022232 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022233 if (from->vval.v_dict == NULL)
22234 to->vval.v_dict = NULL;
22235 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22236 {
22237 /* use the copy made earlier */
22238 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22239 ++to->vval.v_dict->dv_refcount;
22240 }
22241 else
22242 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22243 if (to->vval.v_dict == NULL)
22244 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022245 break;
22246 default:
22247 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022248 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022249 }
22250 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022251 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022252}
22253
22254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 * ":echo expr1 ..." print each argument separated with a space, add a
22256 * newline at the end.
22257 * ":echon expr1 ..." print each argument plain.
22258 */
22259 void
22260ex_echo(eap)
22261 exarg_T *eap;
22262{
22263 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022264 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022265 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266 char_u *p;
22267 int needclr = TRUE;
22268 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022269 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270
22271 if (eap->skip)
22272 ++emsg_skip;
22273 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22274 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022275 /* If eval1() causes an error message the text from the command may
22276 * still need to be cleared. E.g., "echo 22,44". */
22277 need_clr_eos = needclr;
22278
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022280 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281 {
22282 /*
22283 * Report the invalid expression unless the expression evaluation
22284 * has been cancelled due to an aborting error, an interrupt, or an
22285 * exception.
22286 */
22287 if (!aborting())
22288 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022289 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 break;
22291 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022292 need_clr_eos = FALSE;
22293
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 if (!eap->skip)
22295 {
22296 if (atstart)
22297 {
22298 atstart = FALSE;
22299 /* Call msg_start() after eval1(), evaluating the expression
22300 * may cause a message to appear. */
22301 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022302 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022303 /* Mark the saved text as finishing the line, so that what
22304 * follows is displayed on a new line when scrolling back
22305 * at the more prompt. */
22306 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 }
22310 else if (eap->cmdidx == CMD_echo)
22311 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022312 current_copyID += COPYID_INC;
22313 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022314 if (p != NULL)
22315 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022317 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022319 if (*p != TAB && needclr)
22320 {
22321 /* remove any text still there from the command */
22322 msg_clr_eos();
22323 needclr = FALSE;
22324 }
22325 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 }
22327 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022328 {
22329#ifdef FEAT_MBYTE
22330 if (has_mbyte)
22331 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022332 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022333
22334 (void)msg_outtrans_len_attr(p, i, echo_attr);
22335 p += i - 1;
22336 }
22337 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022339 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022342 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022344 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 arg = skipwhite(arg);
22346 }
22347 eap->nextcmd = check_nextcmd(arg);
22348
22349 if (eap->skip)
22350 --emsg_skip;
22351 else
22352 {
22353 /* remove text that may still be there from the command */
22354 if (needclr)
22355 msg_clr_eos();
22356 if (eap->cmdidx == CMD_echo)
22357 msg_end();
22358 }
22359}
22360
22361/*
22362 * ":echohl {name}".
22363 */
22364 void
22365ex_echohl(eap)
22366 exarg_T *eap;
22367{
22368 int id;
22369
22370 id = syn_name2id(eap->arg);
22371 if (id == 0)
22372 echo_attr = 0;
22373 else
22374 echo_attr = syn_id2attr(id);
22375}
22376
22377/*
22378 * ":execute expr1 ..." execute the result of an expression.
22379 * ":echomsg expr1 ..." Print a message
22380 * ":echoerr expr1 ..." Print an error
22381 * Each gets spaces around each argument and a newline at the end for
22382 * echo commands
22383 */
22384 void
22385ex_execute(eap)
22386 exarg_T *eap;
22387{
22388 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022389 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 int ret = OK;
22391 char_u *p;
22392 garray_T ga;
22393 int len;
22394 int save_did_emsg;
22395
22396 ga_init2(&ga, 1, 80);
22397
22398 if (eap->skip)
22399 ++emsg_skip;
22400 while (*arg != NUL && *arg != '|' && *arg != '\n')
22401 {
22402 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022403 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022404 {
22405 /*
22406 * Report the invalid expression unless the expression evaluation
22407 * has been cancelled due to an aborting error, an interrupt, or an
22408 * exception.
22409 */
22410 if (!aborting())
22411 EMSG2(_(e_invexpr2), p);
22412 ret = FAIL;
22413 break;
22414 }
22415
22416 if (!eap->skip)
22417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022418 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 len = (int)STRLEN(p);
22420 if (ga_grow(&ga, len + 2) == FAIL)
22421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022422 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 ret = FAIL;
22424 break;
22425 }
22426 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 ga.ga_len += len;
22430 }
22431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022432 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433 arg = skipwhite(arg);
22434 }
22435
22436 if (ret != FAIL && ga.ga_data != NULL)
22437 {
22438 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022441 out_flush();
22442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 else if (eap->cmdidx == CMD_echoerr)
22444 {
22445 /* We don't want to abort following commands, restore did_emsg. */
22446 save_did_emsg = did_emsg;
22447 EMSG((char_u *)ga.ga_data);
22448 if (!force_abort)
22449 did_emsg = save_did_emsg;
22450 }
22451 else if (eap->cmdidx == CMD_execute)
22452 do_cmdline((char_u *)ga.ga_data,
22453 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22454 }
22455
22456 ga_clear(&ga);
22457
22458 if (eap->skip)
22459 --emsg_skip;
22460
22461 eap->nextcmd = check_nextcmd(arg);
22462}
22463
22464/*
22465 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22466 * "arg" points to the "&" or '+' when called, to "option" when returning.
22467 * Returns NULL when no option name found. Otherwise pointer to the char
22468 * after the option name.
22469 */
22470 static char_u *
22471find_option_end(arg, opt_flags)
22472 char_u **arg;
22473 int *opt_flags;
22474{
22475 char_u *p = *arg;
22476
22477 ++p;
22478 if (*p == 'g' && p[1] == ':')
22479 {
22480 *opt_flags = OPT_GLOBAL;
22481 p += 2;
22482 }
22483 else if (*p == 'l' && p[1] == ':')
22484 {
22485 *opt_flags = OPT_LOCAL;
22486 p += 2;
22487 }
22488 else
22489 *opt_flags = 0;
22490
22491 if (!ASCII_ISALPHA(*p))
22492 return NULL;
22493 *arg = p;
22494
22495 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22496 p += 4; /* termcap option */
22497 else
22498 while (ASCII_ISALPHA(*p))
22499 ++p;
22500 return p;
22501}
22502
22503/*
22504 * ":function"
22505 */
22506 void
22507ex_function(eap)
22508 exarg_T *eap;
22509{
22510 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022511 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 int j;
22513 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022514 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022515 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516 char_u *name = NULL;
22517 char_u *p;
22518 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022519 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 garray_T newargs;
22521 garray_T newlines;
22522 int varargs = FALSE;
22523 int mustend = FALSE;
22524 int flags = 0;
22525 ufunc_T *fp;
22526 int indent;
22527 int nesting;
22528 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022529 dictitem_T *v;
22530 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022531 static int func_nr = 0; /* number for nameless function */
22532 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022533 hashtab_T *ht;
22534 int todo;
22535 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022536 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537
22538 /*
22539 * ":function" without argument: list functions.
22540 */
22541 if (ends_excmd(*eap->arg))
22542 {
22543 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022544 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022545 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022546 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022547 {
22548 if (!HASHITEM_EMPTY(hi))
22549 {
22550 --todo;
22551 fp = HI2UF(hi);
22552 if (!isdigit(*fp->uf_name))
22553 list_func_head(fp, FALSE);
22554 }
22555 }
22556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 eap->nextcmd = check_nextcmd(eap->arg);
22558 return;
22559 }
22560
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022561 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022562 * ":function /pat": list functions matching pattern.
22563 */
22564 if (*eap->arg == '/')
22565 {
22566 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22567 if (!eap->skip)
22568 {
22569 regmatch_T regmatch;
22570
22571 c = *p;
22572 *p = NUL;
22573 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22574 *p = c;
22575 if (regmatch.regprog != NULL)
22576 {
22577 regmatch.rm_ic = p_ic;
22578
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022579 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022580 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22581 {
22582 if (!HASHITEM_EMPTY(hi))
22583 {
22584 --todo;
22585 fp = HI2UF(hi);
22586 if (!isdigit(*fp->uf_name)
22587 && vim_regexec(&regmatch, fp->uf_name, 0))
22588 list_func_head(fp, FALSE);
22589 }
22590 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022591 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022592 }
22593 }
22594 if (*p == '/')
22595 ++p;
22596 eap->nextcmd = check_nextcmd(p);
22597 return;
22598 }
22599
22600 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022601 * Get the function name. There are these situations:
22602 * func normal function name
22603 * "name" == func, "fudi.fd_dict" == NULL
22604 * dict.func new dictionary entry
22605 * "name" == NULL, "fudi.fd_dict" set,
22606 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22607 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022608 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022609 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22610 * dict.func existing dict entry that's not a Funcref
22611 * "name" == NULL, "fudi.fd_dict" set,
22612 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022613 * s:func script-local function name
22614 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022617 name = trans_function_name(&p, eap->skip, 0, &fudi);
22618 paren = (vim_strchr(p, '(') != NULL);
22619 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 {
22621 /*
22622 * Return on an invalid expression in braces, unless the expression
22623 * evaluation has been cancelled due to an aborting error, an
22624 * interrupt, or an exception.
22625 */
22626 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022627 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022628 if (!eap->skip && fudi.fd_newkey != NULL)
22629 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022630 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 else
22634 eap->skip = TRUE;
22635 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022636
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 /* An error in a function call during evaluation of an expression in magic
22638 * braces should not cause the function not to be defined. */
22639 saved_did_emsg = did_emsg;
22640 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641
22642 /*
22643 * ":function func" with only function name: list function.
22644 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022645 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646 {
22647 if (!ends_excmd(*skipwhite(p)))
22648 {
22649 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022650 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 }
22652 eap->nextcmd = check_nextcmd(p);
22653 if (eap->nextcmd != NULL)
22654 *p = NUL;
22655 if (!eap->skip && !got_int)
22656 {
22657 fp = find_func(name);
22658 if (fp != NULL)
22659 {
22660 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022661 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022663 if (FUNCLINE(fp, j) == NULL)
22664 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 msg_putchar('\n');
22666 msg_outnum((long)(j + 1));
22667 if (j < 9)
22668 msg_putchar(' ');
22669 if (j < 99)
22670 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022671 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672 out_flush(); /* show a line at a time */
22673 ui_breakcheck();
22674 }
22675 if (!got_int)
22676 {
22677 msg_putchar('\n');
22678 msg_puts((char_u *)" endfunction");
22679 }
22680 }
22681 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022682 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022684 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 }
22686
22687 /*
22688 * ":function name(arg1, arg2)" Define function.
22689 */
22690 p = skipwhite(p);
22691 if (*p != '(')
22692 {
22693 if (!eap->skip)
22694 {
22695 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022696 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 }
22698 /* attempt to continue by skipping some text */
22699 if (vim_strchr(p, '(') != NULL)
22700 p = vim_strchr(p, '(');
22701 }
22702 p = skipwhite(p + 1);
22703
22704 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22705 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22706
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022707 if (!eap->skip)
22708 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022709 /* Check the name of the function. Unless it's a dictionary function
22710 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022711 if (name != NULL)
22712 arg = name;
22713 else
22714 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022715 if (arg != NULL && (fudi.fd_di == NULL
22716 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022717 {
22718 if (*arg == K_SPECIAL)
22719 j = 3;
22720 else
22721 j = 0;
22722 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22723 : eval_isnamec(arg[j])))
22724 ++j;
22725 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022726 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022727 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022728 /* Disallow using the g: dict. */
22729 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22730 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022731 }
22732
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 /*
22734 * Isolate the arguments: "arg1, arg2, ...)"
22735 */
22736 while (*p != ')')
22737 {
22738 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22739 {
22740 varargs = TRUE;
22741 p += 3;
22742 mustend = TRUE;
22743 }
22744 else
22745 {
22746 arg = p;
22747 while (ASCII_ISALNUM(*p) || *p == '_')
22748 ++p;
22749 if (arg == p || isdigit(*arg)
22750 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22751 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22752 {
22753 if (!eap->skip)
22754 EMSG2(_("E125: Illegal argument: %s"), arg);
22755 break;
22756 }
22757 if (ga_grow(&newargs, 1) == FAIL)
22758 goto erret;
22759 c = *p;
22760 *p = NUL;
22761 arg = vim_strsave(arg);
22762 if (arg == NULL)
22763 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022764
22765 /* Check for duplicate argument name. */
22766 for (i = 0; i < newargs.ga_len; ++i)
22767 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22768 {
22769 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022770 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022771 goto erret;
22772 }
22773
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22775 *p = c;
22776 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 if (*p == ',')
22778 ++p;
22779 else
22780 mustend = TRUE;
22781 }
22782 p = skipwhite(p);
22783 if (mustend && *p != ')')
22784 {
22785 if (!eap->skip)
22786 EMSG2(_(e_invarg2), eap->arg);
22787 break;
22788 }
22789 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022790 if (*p != ')')
22791 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 ++p; /* skip the ')' */
22793
Bram Moolenaare9a41262005-01-15 22:18:47 +000022794 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 for (;;)
22796 {
22797 p = skipwhite(p);
22798 if (STRNCMP(p, "range", 5) == 0)
22799 {
22800 flags |= FC_RANGE;
22801 p += 5;
22802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022803 else if (STRNCMP(p, "dict", 4) == 0)
22804 {
22805 flags |= FC_DICT;
22806 p += 4;
22807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 else if (STRNCMP(p, "abort", 5) == 0)
22809 {
22810 flags |= FC_ABORT;
22811 p += 5;
22812 }
22813 else
22814 break;
22815 }
22816
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022817 /* When there is a line break use what follows for the function body.
22818 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22819 if (*p == '\n')
22820 line_arg = p + 1;
22821 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022822 EMSG(_(e_trailing));
22823
22824 /*
22825 * Read the body of the function, until ":endfunction" is found.
22826 */
22827 if (KeyTyped)
22828 {
22829 /* Check if the function already exists, don't let the user type the
22830 * whole function before telling him it doesn't work! For a script we
22831 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022832 if (!eap->skip && !eap->forceit)
22833 {
22834 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22835 EMSG(_(e_funcdict));
22836 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022837 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022840 if (!eap->skip && did_emsg)
22841 goto erret;
22842
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 msg_putchar('\n'); /* don't overwrite the function name */
22844 cmdline_row = msg_row;
22845 }
22846
22847 indent = 2;
22848 nesting = 0;
22849 for (;;)
22850 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022851 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022852 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022853 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022854 saved_wait_return = FALSE;
22855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022857 sourcing_lnum_off = sourcing_lnum;
22858
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022859 if (line_arg != NULL)
22860 {
22861 /* Use eap->arg, split up in parts by line breaks. */
22862 theline = line_arg;
22863 p = vim_strchr(theline, '\n');
22864 if (p == NULL)
22865 line_arg += STRLEN(line_arg);
22866 else
22867 {
22868 *p = NUL;
22869 line_arg = p + 1;
22870 }
22871 }
22872 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022873 theline = getcmdline(':', 0L, indent);
22874 else
22875 theline = eap->getline(':', eap->cookie, indent);
22876 if (KeyTyped)
22877 lines_left = Rows - 1;
22878 if (theline == NULL)
22879 {
22880 EMSG(_("E126: Missing :endfunction"));
22881 goto erret;
22882 }
22883
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022884 /* Detect line continuation: sourcing_lnum increased more than one. */
22885 if (sourcing_lnum > sourcing_lnum_off + 1)
22886 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22887 else
22888 sourcing_lnum_off = 0;
22889
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 if (skip_until != NULL)
22891 {
22892 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22893 * don't check for ":endfunc". */
22894 if (STRCMP(theline, skip_until) == 0)
22895 {
22896 vim_free(skip_until);
22897 skip_until = NULL;
22898 }
22899 }
22900 else
22901 {
22902 /* skip ':' and blanks*/
22903 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22904 ;
22905
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022906 /* Check for "endfunction". */
22907 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022909 if (line_arg == NULL)
22910 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 break;
22912 }
22913
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022914 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 * at "end". */
22916 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22917 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022918 else if (STRNCMP(p, "if", 2) == 0
22919 || STRNCMP(p, "wh", 2) == 0
22920 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 || STRNCMP(p, "try", 3) == 0)
22922 indent += 2;
22923
22924 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022925 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022927 if (*p == '!')
22928 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022930 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22931 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022933 ++nesting;
22934 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 }
22936 }
22937
22938 /* Check for ":append" or ":insert". */
22939 p = skip_range(p, NULL);
22940 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22941 || (p[0] == 'i'
22942 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22943 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22944 skip_until = vim_strsave((char_u *)".");
22945
22946 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22947 arg = skipwhite(skiptowhite(p));
22948 if (arg[0] == '<' && arg[1] =='<'
22949 && ((p[0] == 'p' && p[1] == 'y'
22950 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22951 || (p[0] == 'p' && p[1] == 'e'
22952 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22953 || (p[0] == 't' && p[1] == 'c'
22954 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022955 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22956 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22958 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022959 || (p[0] == 'm' && p[1] == 'z'
22960 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 ))
22962 {
22963 /* ":python <<" continues until a dot, like ":append" */
22964 p = skipwhite(arg + 2);
22965 if (*p == NUL)
22966 skip_until = vim_strsave((char_u *)".");
22967 else
22968 skip_until = vim_strsave(p);
22969 }
22970 }
22971
22972 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022973 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022974 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022975 if (line_arg == NULL)
22976 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022978 }
22979
22980 /* Copy the line to newly allocated memory. get_one_sourceline()
22981 * allocates 250 bytes per line, this saves 80% on average. The cost
22982 * is an extra alloc/free. */
22983 p = vim_strsave(theline);
22984 if (p != NULL)
22985 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022986 if (line_arg == NULL)
22987 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022988 theline = p;
22989 }
22990
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022991 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22992
22993 /* Add NULL lines for continuation lines, so that the line count is
22994 * equal to the index in the growarray. */
22995 while (sourcing_lnum_off-- > 0)
22996 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022997
22998 /* Check for end of eap->arg. */
22999 if (line_arg != NULL && *line_arg == NUL)
23000 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 }
23002
23003 /* Don't define the function when skipping commands or when an error was
23004 * detected. */
23005 if (eap->skip || did_emsg)
23006 goto erret;
23007
23008 /*
23009 * If there are no errors, add the function
23010 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023011 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023012 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023013 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023014 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023015 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023016 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023017 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023018 goto erret;
23019 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023020
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023021 fp = find_func(name);
23022 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023024 if (!eap->forceit)
23025 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023026 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023027 goto erret;
23028 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023029 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023030 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023031 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023032 name);
23033 goto erret;
23034 }
23035 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023036 ga_clear_strings(&(fp->uf_args));
23037 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023038 vim_free(name);
23039 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 }
23042 else
23043 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023044 char numbuf[20];
23045
23046 fp = NULL;
23047 if (fudi.fd_newkey == NULL && !eap->forceit)
23048 {
23049 EMSG(_(e_funcdict));
23050 goto erret;
23051 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023052 if (fudi.fd_di == NULL)
23053 {
23054 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023055 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023056 goto erret;
23057 }
23058 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023059 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023060 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023061
23062 /* Give the function a sequential number. Can only be used with a
23063 * Funcref! */
23064 vim_free(name);
23065 sprintf(numbuf, "%d", ++func_nr);
23066 name = vim_strsave((char_u *)numbuf);
23067 if (name == NULL)
23068 goto erret;
23069 }
23070
23071 if (fp == NULL)
23072 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023073 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023074 {
23075 int slen, plen;
23076 char_u *scriptname;
23077
23078 /* Check that the autoload name matches the script name. */
23079 j = FAIL;
23080 if (sourcing_name != NULL)
23081 {
23082 scriptname = autoload_name(name);
23083 if (scriptname != NULL)
23084 {
23085 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023086 plen = (int)STRLEN(p);
23087 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023088 if (slen > plen && fnamecmp(p,
23089 sourcing_name + slen - plen) == 0)
23090 j = OK;
23091 vim_free(scriptname);
23092 }
23093 }
23094 if (j == FAIL)
23095 {
23096 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23097 goto erret;
23098 }
23099 }
23100
23101 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 if (fp == NULL)
23103 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023104
23105 if (fudi.fd_dict != NULL)
23106 {
23107 if (fudi.fd_di == NULL)
23108 {
23109 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023110 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023111 if (fudi.fd_di == NULL)
23112 {
23113 vim_free(fp);
23114 goto erret;
23115 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023116 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23117 {
23118 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023119 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023120 goto erret;
23121 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023122 }
23123 else
23124 /* overwrite existing dict entry */
23125 clear_tv(&fudi.fd_di->di_tv);
23126 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023127 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023128 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023129 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023130
23131 /* behave like "dict" was used */
23132 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023133 }
23134
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023136 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023137 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23138 {
23139 vim_free(fp);
23140 goto erret;
23141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023143 fp->uf_args = newargs;
23144 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023145#ifdef FEAT_PROFILE
23146 fp->uf_tml_count = NULL;
23147 fp->uf_tml_total = NULL;
23148 fp->uf_tml_self = NULL;
23149 fp->uf_profiling = FALSE;
23150 if (prof_def_func())
23151 func_do_profile(fp);
23152#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023153 fp->uf_varargs = varargs;
23154 fp->uf_flags = flags;
23155 fp->uf_calls = 0;
23156 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023157 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158
23159erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160 ga_clear_strings(&newargs);
23161 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023162ret_free:
23163 vim_free(skip_until);
23164 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023167 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168}
23169
23170/*
23171 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023172 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023174 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023175 * TFN_INT: internal function name OK
23176 * TFN_QUIET: be quiet
23177 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178 * Advances "pp" to just after the function name (if no error).
23179 */
23180 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023181trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182 char_u **pp;
23183 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023184 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023185 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023187 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 char_u *start;
23189 char_u *end;
23190 int lead;
23191 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023193 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023194
23195 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023196 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023198
23199 /* Check for hard coded <SNR>: already translated function ID (from a user
23200 * command). */
23201 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23202 && (*pp)[2] == (int)KE_SNR)
23203 {
23204 *pp += 3;
23205 len = get_id_len(pp) + 3;
23206 return vim_strnsave(start, len);
23207 }
23208
23209 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23210 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023212 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023214
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023215 /* Note that TFN_ flags use the same values as GLV_ flags. */
23216 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023217 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023218 if (end == start)
23219 {
23220 if (!skip)
23221 EMSG(_("E129: Function name required"));
23222 goto theend;
23223 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023224 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023225 {
23226 /*
23227 * Report an invalid expression in braces, unless the expression
23228 * evaluation has been cancelled due to an aborting error, an
23229 * interrupt, or an exception.
23230 */
23231 if (!aborting())
23232 {
23233 if (end != NULL)
23234 EMSG2(_(e_invarg2), start);
23235 }
23236 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023237 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023238 goto theend;
23239 }
23240
23241 if (lv.ll_tv != NULL)
23242 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023243 if (fdp != NULL)
23244 {
23245 fdp->fd_dict = lv.ll_dict;
23246 fdp->fd_newkey = lv.ll_newkey;
23247 lv.ll_newkey = NULL;
23248 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023249 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023250 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23251 {
23252 name = vim_strsave(lv.ll_tv->vval.v_string);
23253 *pp = end;
23254 }
23255 else
23256 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023257 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23258 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023259 EMSG(_(e_funcref));
23260 else
23261 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023262 name = NULL;
23263 }
23264 goto theend;
23265 }
23266
23267 if (lv.ll_name == NULL)
23268 {
23269 /* Error found, but continue after the function name. */
23270 *pp = end;
23271 goto theend;
23272 }
23273
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023274 /* Check if the name is a Funcref. If so, use the value. */
23275 if (lv.ll_exp_name != NULL)
23276 {
23277 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023278 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023279 if (name == lv.ll_exp_name)
23280 name = NULL;
23281 }
23282 else
23283 {
23284 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023285 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023286 if (name == *pp)
23287 name = NULL;
23288 }
23289 if (name != NULL)
23290 {
23291 name = vim_strsave(name);
23292 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023293 if (STRNCMP(name, "<SNR>", 5) == 0)
23294 {
23295 /* Change "<SNR>" to the byte sequence. */
23296 name[0] = K_SPECIAL;
23297 name[1] = KS_EXTRA;
23298 name[2] = (int)KE_SNR;
23299 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23300 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023301 goto theend;
23302 }
23303
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023304 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023305 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023306 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023307 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23308 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23309 {
23310 /* When there was "s:" already or the name expanded to get a
23311 * leading "s:" then remove it. */
23312 lv.ll_name += 2;
23313 len -= 2;
23314 lead = 2;
23315 }
23316 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023317 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023318 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023319 /* skip over "s:" and "g:" */
23320 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023321 lv.ll_name += 2;
23322 len = (int)(end - lv.ll_name);
23323 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023324
23325 /*
23326 * Copy the function name to allocated memory.
23327 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23328 * Accept <SNR>123_name() outside a script.
23329 */
23330 if (skip)
23331 lead = 0; /* do nothing */
23332 else if (lead > 0)
23333 {
23334 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023335 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23336 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023337 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023338 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023339 if (current_SID <= 0)
23340 {
23341 EMSG(_(e_usingsid));
23342 goto theend;
23343 }
23344 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23345 lead += (int)STRLEN(sid_buf);
23346 }
23347 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023348 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023349 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023350 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023351 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023352 goto theend;
23353 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023354 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023355 {
23356 char_u *cp = vim_strchr(lv.ll_name, ':');
23357
23358 if (cp != NULL && cp < end)
23359 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023360 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023361 goto theend;
23362 }
23363 }
23364
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023365 name = alloc((unsigned)(len + lead + 1));
23366 if (name != NULL)
23367 {
23368 if (lead > 0)
23369 {
23370 name[0] = K_SPECIAL;
23371 name[1] = KS_EXTRA;
23372 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023373 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023374 STRCPY(name + 3, sid_buf);
23375 }
23376 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023377 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023378 }
23379 *pp = end;
23380
23381theend:
23382 clear_lval(&lv);
23383 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384}
23385
23386/*
23387 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23388 * Return 2 if "p" starts with "s:".
23389 * Return 0 otherwise.
23390 */
23391 static int
23392eval_fname_script(p)
23393 char_u *p;
23394{
23395 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23396 || STRNICMP(p + 1, "SNR>", 4) == 0))
23397 return 5;
23398 if (p[0] == 's' && p[1] == ':')
23399 return 2;
23400 return 0;
23401}
23402
23403/*
23404 * Return TRUE if "p" starts with "<SID>" or "s:".
23405 * Only works if eval_fname_script() returned non-zero for "p"!
23406 */
23407 static int
23408eval_fname_sid(p)
23409 char_u *p;
23410{
23411 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23412}
23413
23414/*
23415 * List the head of the function: "name(arg1, arg2)".
23416 */
23417 static void
23418list_func_head(fp, indent)
23419 ufunc_T *fp;
23420 int indent;
23421{
23422 int j;
23423
23424 msg_start();
23425 if (indent)
23426 MSG_PUTS(" ");
23427 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023428 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023429 {
23430 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023431 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432 }
23433 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023434 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023435 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023436 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437 {
23438 if (j)
23439 MSG_PUTS(", ");
23440 msg_puts(FUNCARG(fp, j));
23441 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023442 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023443 {
23444 if (j)
23445 MSG_PUTS(", ");
23446 MSG_PUTS("...");
23447 }
23448 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023449 if (fp->uf_flags & FC_ABORT)
23450 MSG_PUTS(" abort");
23451 if (fp->uf_flags & FC_RANGE)
23452 MSG_PUTS(" range");
23453 if (fp->uf_flags & FC_DICT)
23454 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023455 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023456 if (p_verbose > 0)
23457 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458}
23459
23460/*
23461 * Find a function by name, return pointer to it in ufuncs.
23462 * Return NULL for unknown function.
23463 */
23464 static ufunc_T *
23465find_func(name)
23466 char_u *name;
23467{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023468 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023470 hi = hash_find(&func_hashtab, name);
23471 if (!HASHITEM_EMPTY(hi))
23472 return HI2UF(hi);
23473 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474}
23475
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023476#if defined(EXITFREE) || defined(PROTO)
23477 void
23478free_all_functions()
23479{
23480 hashitem_T *hi;
23481
23482 /* Need to start all over every time, because func_free() may change the
23483 * hash table. */
23484 while (func_hashtab.ht_used > 0)
23485 for (hi = func_hashtab.ht_array; ; ++hi)
23486 if (!HASHITEM_EMPTY(hi))
23487 {
23488 func_free(HI2UF(hi));
23489 break;
23490 }
23491}
23492#endif
23493
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023494 int
23495translated_function_exists(name)
23496 char_u *name;
23497{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023498 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023499 return find_internal_func(name) >= 0;
23500 return find_func(name) != NULL;
23501}
23502
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023503/*
23504 * Return TRUE if a function "name" exists.
23505 */
23506 static int
23507function_exists(name)
23508 char_u *name;
23509{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023510 char_u *nm = name;
23511 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023512 int n = FALSE;
23513
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023514 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23515 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023516 nm = skipwhite(nm);
23517
23518 /* Only accept "funcname", "funcname ", "funcname (..." and
23519 * "funcname(...", not "funcname!...". */
23520 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023521 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023522 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023523 return n;
23524}
23525
Bram Moolenaara1544c02013-05-30 12:35:52 +020023526 char_u *
23527get_expanded_name(name, check)
23528 char_u *name;
23529 int check;
23530{
23531 char_u *nm = name;
23532 char_u *p;
23533
23534 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23535
23536 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023537 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023538 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023539
Bram Moolenaara1544c02013-05-30 12:35:52 +020023540 vim_free(p);
23541 return NULL;
23542}
23543
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023544/*
23545 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023546 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23547 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023548 */
23549 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023550builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023551 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023552 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023553{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023554 char_u *p;
23555
23556 if (!ASCII_ISLOWER(name[0]))
23557 return FALSE;
23558 p = vim_strchr(name, AUTOLOAD_CHAR);
23559 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023560}
23561
Bram Moolenaar05159a02005-02-26 23:04:13 +000023562#if defined(FEAT_PROFILE) || defined(PROTO)
23563/*
23564 * Start profiling function "fp".
23565 */
23566 static void
23567func_do_profile(fp)
23568 ufunc_T *fp;
23569{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023570 int len = fp->uf_lines.ga_len;
23571
23572 if (len == 0)
23573 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023574 fp->uf_tm_count = 0;
23575 profile_zero(&fp->uf_tm_self);
23576 profile_zero(&fp->uf_tm_total);
23577 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023578 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023579 if (fp->uf_tml_total == NULL)
23580 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023581 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023582 if (fp->uf_tml_self == NULL)
23583 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023584 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023585 fp->uf_tml_idx = -1;
23586 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23587 || fp->uf_tml_self == NULL)
23588 return; /* out of memory */
23589
23590 fp->uf_profiling = TRUE;
23591}
23592
23593/*
23594 * Dump the profiling results for all functions in file "fd".
23595 */
23596 void
23597func_dump_profile(fd)
23598 FILE *fd;
23599{
23600 hashitem_T *hi;
23601 int todo;
23602 ufunc_T *fp;
23603 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023604 ufunc_T **sorttab;
23605 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023606
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023607 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023608 if (todo == 0)
23609 return; /* nothing to dump */
23610
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023611 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023612
Bram Moolenaar05159a02005-02-26 23:04:13 +000023613 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23614 {
23615 if (!HASHITEM_EMPTY(hi))
23616 {
23617 --todo;
23618 fp = HI2UF(hi);
23619 if (fp->uf_profiling)
23620 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023621 if (sorttab != NULL)
23622 sorttab[st_len++] = fp;
23623
Bram Moolenaar05159a02005-02-26 23:04:13 +000023624 if (fp->uf_name[0] == K_SPECIAL)
23625 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23626 else
23627 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23628 if (fp->uf_tm_count == 1)
23629 fprintf(fd, "Called 1 time\n");
23630 else
23631 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23632 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23633 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23634 fprintf(fd, "\n");
23635 fprintf(fd, "count total (s) self (s)\n");
23636
23637 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23638 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023639 if (FUNCLINE(fp, i) == NULL)
23640 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023641 prof_func_line(fd, fp->uf_tml_count[i],
23642 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023643 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23644 }
23645 fprintf(fd, "\n");
23646 }
23647 }
23648 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023649
23650 if (sorttab != NULL && st_len > 0)
23651 {
23652 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23653 prof_total_cmp);
23654 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23655 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23656 prof_self_cmp);
23657 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23658 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023659
23660 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023661}
Bram Moolenaar73830342005-02-28 22:48:19 +000023662
23663 static void
23664prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23665 FILE *fd;
23666 ufunc_T **sorttab;
23667 int st_len;
23668 char *title;
23669 int prefer_self; /* when equal print only self time */
23670{
23671 int i;
23672 ufunc_T *fp;
23673
23674 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23675 fprintf(fd, "count total (s) self (s) function\n");
23676 for (i = 0; i < 20 && i < st_len; ++i)
23677 {
23678 fp = sorttab[i];
23679 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23680 prefer_self);
23681 if (fp->uf_name[0] == K_SPECIAL)
23682 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23683 else
23684 fprintf(fd, " %s()\n", fp->uf_name);
23685 }
23686 fprintf(fd, "\n");
23687}
23688
23689/*
23690 * Print the count and times for one function or function line.
23691 */
23692 static void
23693prof_func_line(fd, count, total, self, prefer_self)
23694 FILE *fd;
23695 int count;
23696 proftime_T *total;
23697 proftime_T *self;
23698 int prefer_self; /* when equal print only self time */
23699{
23700 if (count > 0)
23701 {
23702 fprintf(fd, "%5d ", count);
23703 if (prefer_self && profile_equal(total, self))
23704 fprintf(fd, " ");
23705 else
23706 fprintf(fd, "%s ", profile_msg(total));
23707 if (!prefer_self && profile_equal(total, self))
23708 fprintf(fd, " ");
23709 else
23710 fprintf(fd, "%s ", profile_msg(self));
23711 }
23712 else
23713 fprintf(fd, " ");
23714}
23715
23716/*
23717 * Compare function for total time sorting.
23718 */
23719 static int
23720#ifdef __BORLANDC__
23721_RTLENTRYF
23722#endif
23723prof_total_cmp(s1, s2)
23724 const void *s1;
23725 const void *s2;
23726{
23727 ufunc_T *p1, *p2;
23728
23729 p1 = *(ufunc_T **)s1;
23730 p2 = *(ufunc_T **)s2;
23731 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23732}
23733
23734/*
23735 * Compare function for self time sorting.
23736 */
23737 static int
23738#ifdef __BORLANDC__
23739_RTLENTRYF
23740#endif
23741prof_self_cmp(s1, s2)
23742 const void *s1;
23743 const void *s2;
23744{
23745 ufunc_T *p1, *p2;
23746
23747 p1 = *(ufunc_T **)s1;
23748 p2 = *(ufunc_T **)s2;
23749 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23750}
23751
Bram Moolenaar05159a02005-02-26 23:04:13 +000023752#endif
23753
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023754/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023755 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023756 * Return TRUE if a package was loaded.
23757 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023758 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023759script_autoload(name, reload)
23760 char_u *name;
23761 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023762{
23763 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023764 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023765 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023766 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023767
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023768 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023769 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023770 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023771 return FALSE;
23772
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023773 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023774
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023775 /* Find the name in the list of previously loaded package names. Skip
23776 * "autoload/", it's always the same. */
23777 for (i = 0; i < ga_loaded.ga_len; ++i)
23778 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23779 break;
23780 if (!reload && i < ga_loaded.ga_len)
23781 ret = FALSE; /* was loaded already */
23782 else
23783 {
23784 /* Remember the name if it wasn't loaded already. */
23785 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23786 {
23787 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23788 tofree = NULL;
23789 }
23790
23791 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023792 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023793 ret = TRUE;
23794 }
23795
23796 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023797 return ret;
23798}
23799
23800/*
23801 * Return the autoload script name for a function or variable name.
23802 * Returns NULL when out of memory.
23803 */
23804 static char_u *
23805autoload_name(name)
23806 char_u *name;
23807{
23808 char_u *p;
23809 char_u *scriptname;
23810
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023811 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023812 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23813 if (scriptname == NULL)
23814 return FALSE;
23815 STRCPY(scriptname, "autoload/");
23816 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023817 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023818 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023819 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023820 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023821 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023822}
23823
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23825
23826/*
23827 * Function given to ExpandGeneric() to obtain the list of user defined
23828 * function names.
23829 */
23830 char_u *
23831get_user_func_name(xp, idx)
23832 expand_T *xp;
23833 int idx;
23834{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023835 static long_u done;
23836 static hashitem_T *hi;
23837 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838
23839 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023841 done = 0;
23842 hi = func_hashtab.ht_array;
23843 }
23844 if (done < func_hashtab.ht_used)
23845 {
23846 if (done++ > 0)
23847 ++hi;
23848 while (HASHITEM_EMPTY(hi))
23849 ++hi;
23850 fp = HI2UF(hi);
23851
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023852 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023853 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023854
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023855 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23856 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857
23858 cat_func_name(IObuff, fp);
23859 if (xp->xp_context != EXPAND_USER_FUNC)
23860 {
23861 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023862 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 STRCAT(IObuff, ")");
23864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865 return IObuff;
23866 }
23867 return NULL;
23868}
23869
23870#endif /* FEAT_CMDL_COMPL */
23871
23872/*
23873 * Copy the function name of "fp" to buffer "buf".
23874 * "buf" must be able to hold the function name plus three bytes.
23875 * Takes care of script-local function names.
23876 */
23877 static void
23878cat_func_name(buf, fp)
23879 char_u *buf;
23880 ufunc_T *fp;
23881{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023882 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 {
23884 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023885 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 }
23887 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023888 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889}
23890
23891/*
23892 * ":delfunction {name}"
23893 */
23894 void
23895ex_delfunction(eap)
23896 exarg_T *eap;
23897{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023898 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899 char_u *p;
23900 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023901 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902
23903 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023904 name = trans_function_name(&p, eap->skip, 0, &fudi);
23905 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023907 {
23908 if (fudi.fd_dict != NULL && !eap->skip)
23909 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912 if (!ends_excmd(*skipwhite(p)))
23913 {
23914 vim_free(name);
23915 EMSG(_(e_trailing));
23916 return;
23917 }
23918 eap->nextcmd = check_nextcmd(p);
23919 if (eap->nextcmd != NULL)
23920 *p = NUL;
23921
23922 if (!eap->skip)
23923 fp = find_func(name);
23924 vim_free(name);
23925
23926 if (!eap->skip)
23927 {
23928 if (fp == NULL)
23929 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023930 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931 return;
23932 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023933 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023934 {
23935 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23936 return;
23937 }
23938
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023939 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023941 /* Delete the dict item that refers to the function, it will
23942 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023943 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023945 else
23946 func_free(fp);
23947 }
23948}
23949
23950/*
23951 * Free a function and remove it from the list of functions.
23952 */
23953 static void
23954func_free(fp)
23955 ufunc_T *fp;
23956{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023957 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023958
23959 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023960 ga_clear_strings(&(fp->uf_args));
23961 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023962#ifdef FEAT_PROFILE
23963 vim_free(fp->uf_tml_count);
23964 vim_free(fp->uf_tml_total);
23965 vim_free(fp->uf_tml_self);
23966#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023967
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023968 /* remove the function from the function hashtable */
23969 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23970 if (HASHITEM_EMPTY(hi))
23971 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023972 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023973 hash_remove(&func_hashtab, hi);
23974
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023975 vim_free(fp);
23976}
23977
23978/*
23979 * Unreference a Function: decrement the reference count and free it when it
23980 * becomes zero. Only for numbered functions.
23981 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023982 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023983func_unref(name)
23984 char_u *name;
23985{
23986 ufunc_T *fp;
23987
23988 if (name != NULL && isdigit(*name))
23989 {
23990 fp = find_func(name);
23991 if (fp == NULL)
23992 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023993 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023994 {
23995 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023996 * when "uf_calls" becomes zero. */
23997 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023998 func_free(fp);
23999 }
24000 }
24001}
24002
24003/*
24004 * Count a reference to a Function.
24005 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024006 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024007func_ref(name)
24008 char_u *name;
24009{
24010 ufunc_T *fp;
24011
24012 if (name != NULL && isdigit(*name))
24013 {
24014 fp = find_func(name);
24015 if (fp == NULL)
24016 EMSG2(_(e_intern2), "func_ref()");
24017 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024018 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 }
24020}
24021
24022/*
24023 * Call a user function.
24024 */
24025 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024026call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 ufunc_T *fp; /* pointer to function */
24028 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024029 typval_T *argvars; /* arguments */
24030 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031 linenr_T firstline; /* first line of range */
24032 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024033 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034{
Bram Moolenaar33570922005-01-25 22:26:29 +000024035 char_u *save_sourcing_name;
24036 linenr_T save_sourcing_lnum;
24037 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024038 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024039 int save_did_emsg;
24040 static int depth = 0;
24041 dictitem_T *v;
24042 int fixvar_idx = 0; /* index in fixvar[] */
24043 int i;
24044 int ai;
24045 char_u numbuf[NUMBUFLEN];
24046 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024047 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024048#ifdef FEAT_PROFILE
24049 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024050 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052
24053 /* If depth of calling is getting too high, don't execute the function */
24054 if (depth >= p_mfd)
24055 {
24056 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024057 rettv->v_type = VAR_NUMBER;
24058 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059 return;
24060 }
24061 ++depth;
24062
24063 line_breakcheck(); /* check for CTRL-C hit */
24064
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024065 fc = (funccall_T *)alloc(sizeof(funccall_T));
24066 fc->caller = current_funccal;
24067 current_funccal = fc;
24068 fc->func = fp;
24069 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024070 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024071 fc->linenr = 0;
24072 fc->returned = FALSE;
24073 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024074 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024075 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24076 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024077
Bram Moolenaar33570922005-01-25 22:26:29 +000024078 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024079 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024080 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24081 * each argument variable and saves a lot of time.
24082 */
24083 /*
24084 * Init l: variables.
24085 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024086 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024087 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024088 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024089 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24090 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024091 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024092 name = v->di_key;
24093 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024094 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024095 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024096 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024097 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024098 v->di_tv.vval.v_dict = selfdict;
24099 ++selfdict->dv_refcount;
24100 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024101
Bram Moolenaar33570922005-01-25 22:26:29 +000024102 /*
24103 * Init a: variables.
24104 * Set a:0 to "argcount".
24105 * Set a:000 to a list with room for the "..." arguments.
24106 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024107 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024108 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024109 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024110 /* Use "name" to avoid a warning from some compiler that checks the
24111 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024112 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024113 name = v->di_key;
24114 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024115 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024116 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024117 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024118 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024119 v->di_tv.vval.v_list = &fc->l_varlist;
24120 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24121 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24122 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024123
24124 /*
24125 * Set a:firstline to "firstline" and a:lastline to "lastline".
24126 * Set a:name to named arguments.
24127 * Set a:N to the "..." arguments.
24128 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024129 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024130 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024131 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024132 (varnumber_T)lastline);
24133 for (i = 0; i < argcount; ++i)
24134 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024135 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024136 if (ai < 0)
24137 /* named argument a:name */
24138 name = FUNCARG(fp, i);
24139 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024140 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024141 /* "..." argument a:1, a:2, etc. */
24142 sprintf((char *)numbuf, "%d", ai + 1);
24143 name = numbuf;
24144 }
24145 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24146 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024147 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024148 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24149 }
24150 else
24151 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024152 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24153 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024154 if (v == NULL)
24155 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024156 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024157 }
24158 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024159 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024160
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024161 /* Note: the values are copied directly to avoid alloc/free.
24162 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024163 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024164 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024165
24166 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24167 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024168 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24169 fc->l_listitems[ai].li_tv = argvars[i];
24170 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024171 }
24172 }
24173
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174 /* Don't redraw while executing the function. */
24175 ++RedrawingDisabled;
24176 save_sourcing_name = sourcing_name;
24177 save_sourcing_lnum = sourcing_lnum;
24178 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024179 /* need space for function name + ("function " + 3) or "[number]" */
24180 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24181 + STRLEN(fp->uf_name) + 20;
24182 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 if (sourcing_name != NULL)
24184 {
24185 if (save_sourcing_name != NULL
24186 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024187 sprintf((char *)sourcing_name, "%s[%d]..",
24188 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 else
24190 STRCPY(sourcing_name, "function ");
24191 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24192
24193 if (p_verbose >= 12)
24194 {
24195 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024196 verbose_enter_scroll();
24197
Bram Moolenaar555b2802005-05-19 21:08:39 +000024198 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199 if (p_verbose >= 14)
24200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024201 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024202 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024203 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024204 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205
24206 msg_puts((char_u *)"(");
24207 for (i = 0; i < argcount; ++i)
24208 {
24209 if (i > 0)
24210 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024211 if (argvars[i].v_type == VAR_NUMBER)
24212 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213 else
24214 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024215 /* Do not want errors such as E724 here. */
24216 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024217 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024218 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024219 if (s != NULL)
24220 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024221 if (vim_strsize(s) > MSG_BUF_CLEN)
24222 {
24223 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24224 s = buf;
24225 }
24226 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024227 vim_free(tofree);
24228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 }
24230 }
24231 msg_puts((char_u *)")");
24232 }
24233 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024234
24235 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 --no_wait_return;
24237 }
24238 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024239#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024240 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024241 {
24242 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24243 func_do_profile(fp);
24244 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024245 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024246 {
24247 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024248 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024249 profile_zero(&fp->uf_tm_children);
24250 }
24251 script_prof_save(&wait_start);
24252 }
24253#endif
24254
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024256 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257 save_did_emsg = did_emsg;
24258 did_emsg = FALSE;
24259
24260 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024261 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24263
24264 --RedrawingDisabled;
24265
24266 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024267 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024269 clear_tv(rettv);
24270 rettv->v_type = VAR_NUMBER;
24271 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272 }
24273
Bram Moolenaar05159a02005-02-26 23:04:13 +000024274#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024275 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024276 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024277 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024278 profile_end(&call_start);
24279 profile_sub_wait(&wait_start, &call_start);
24280 profile_add(&fp->uf_tm_total, &call_start);
24281 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024282 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024283 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024284 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24285 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024286 }
24287 }
24288#endif
24289
Bram Moolenaar071d4272004-06-13 20:20:40 +000024290 /* when being verbose, mention the return value */
24291 if (p_verbose >= 12)
24292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024294 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024297 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024298 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024299 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024300 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024303 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024304 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024305 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024306 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024307
Bram Moolenaar555b2802005-05-19 21:08:39 +000024308 /* The value may be very long. Skip the middle part, so that we
24309 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024310 * truncate it at the end. Don't want errors such as E724 here. */
24311 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024312 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024313 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024314 if (s != NULL)
24315 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024316 if (vim_strsize(s) > MSG_BUF_CLEN)
24317 {
24318 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24319 s = buf;
24320 }
24321 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024322 vim_free(tofree);
24323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324 }
24325 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024326
24327 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 --no_wait_return;
24329 }
24330
24331 vim_free(sourcing_name);
24332 sourcing_name = save_sourcing_name;
24333 sourcing_lnum = save_sourcing_lnum;
24334 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024335#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024336 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024337 script_prof_restore(&wait_start);
24338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339
24340 if (p_verbose >= 12 && sourcing_name != NULL)
24341 {
24342 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024343 verbose_enter_scroll();
24344
Bram Moolenaar555b2802005-05-19 21:08:39 +000024345 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024347
24348 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349 --no_wait_return;
24350 }
24351
24352 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024353 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024355
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024356 /* If the a:000 list and the l: and a: dicts are not referenced we can
24357 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024358 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24359 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24360 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24361 {
24362 free_funccal(fc, FALSE);
24363 }
24364 else
24365 {
24366 hashitem_T *hi;
24367 listitem_T *li;
24368 int todo;
24369
24370 /* "fc" is still in use. This can happen when returning "a:000" or
24371 * assigning "l:" to a global variable.
24372 * Link "fc" in the list for garbage collection later. */
24373 fc->caller = previous_funccal;
24374 previous_funccal = fc;
24375
24376 /* Make a copy of the a: variables, since we didn't do that above. */
24377 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24378 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24379 {
24380 if (!HASHITEM_EMPTY(hi))
24381 {
24382 --todo;
24383 v = HI2DI(hi);
24384 copy_tv(&v->di_tv, &v->di_tv);
24385 }
24386 }
24387
24388 /* Make a copy of the a:000 items, since we didn't do that above. */
24389 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24390 copy_tv(&li->li_tv, &li->li_tv);
24391 }
24392}
24393
24394/*
24395 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024396 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024397 */
24398 static int
24399can_free_funccal(fc, copyID)
24400 funccall_T *fc;
24401 int copyID;
24402{
24403 return (fc->l_varlist.lv_copyID != copyID
24404 && fc->l_vars.dv_copyID != copyID
24405 && fc->l_avars.dv_copyID != copyID);
24406}
24407
24408/*
24409 * Free "fc" and what it contains.
24410 */
24411 static void
24412free_funccal(fc, free_val)
24413 funccall_T *fc;
24414 int free_val; /* a: vars were allocated */
24415{
24416 listitem_T *li;
24417
24418 /* The a: variables typevals may not have been allocated, only free the
24419 * allocated variables. */
24420 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24421
24422 /* free all l: variables */
24423 vars_clear(&fc->l_vars.dv_hashtab);
24424
24425 /* Free the a:000 variables if they were allocated. */
24426 if (free_val)
24427 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24428 clear_tv(&li->li_tv);
24429
24430 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024431}
24432
24433/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024434 * Add a number variable "name" to dict "dp" with value "nr".
24435 */
24436 static void
24437add_nr_var(dp, v, name, nr)
24438 dict_T *dp;
24439 dictitem_T *v;
24440 char *name;
24441 varnumber_T nr;
24442{
24443 STRCPY(v->di_key, name);
24444 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24445 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24446 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024447 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024448 v->di_tv.vval.v_number = nr;
24449}
24450
24451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024452 * ":return [expr]"
24453 */
24454 void
24455ex_return(eap)
24456 exarg_T *eap;
24457{
24458 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024459 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 int returning = FALSE;
24461
24462 if (current_funccal == NULL)
24463 {
24464 EMSG(_("E133: :return not inside a function"));
24465 return;
24466 }
24467
24468 if (eap->skip)
24469 ++emsg_skip;
24470
24471 eap->nextcmd = NULL;
24472 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024473 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474 {
24475 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024476 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024477 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024478 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479 }
24480 /* It's safer to return also on error. */
24481 else if (!eap->skip)
24482 {
24483 /*
24484 * Return unless the expression evaluation has been cancelled due to an
24485 * aborting error, an interrupt, or an exception.
24486 */
24487 if (!aborting())
24488 returning = do_return(eap, FALSE, TRUE, NULL);
24489 }
24490
24491 /* When skipping or the return gets pending, advance to the next command
24492 * in this line (!returning). Otherwise, ignore the rest of the line.
24493 * Following lines will be ignored by get_func_line(). */
24494 if (returning)
24495 eap->nextcmd = NULL;
24496 else if (eap->nextcmd == NULL) /* no argument */
24497 eap->nextcmd = check_nextcmd(arg);
24498
24499 if (eap->skip)
24500 --emsg_skip;
24501}
24502
24503/*
24504 * Return from a function. Possibly makes the return pending. Also called
24505 * for a pending return at the ":endtry" or after returning from an extra
24506 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024507 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024508 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024509 * FALSE when the return gets pending.
24510 */
24511 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024512do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513 exarg_T *eap;
24514 int reanimate;
24515 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024516 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024517{
24518 int idx;
24519 struct condstack *cstack = eap->cstack;
24520
24521 if (reanimate)
24522 /* Undo the return. */
24523 current_funccal->returned = FALSE;
24524
24525 /*
24526 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24527 * not in its finally clause (which then is to be executed next) is found.
24528 * In this case, make the ":return" pending for execution at the ":endtry".
24529 * Otherwise, return normally.
24530 */
24531 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24532 if (idx >= 0)
24533 {
24534 cstack->cs_pending[idx] = CSTP_RETURN;
24535
24536 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024537 /* A pending return again gets pending. "rettv" points to an
24538 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024540 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 else
24542 {
24543 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024544 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024546 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024548 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024549 {
24550 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024551 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024552 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024553 else
24554 EMSG(_(e_outofmem));
24555 }
24556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024557 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558
24559 if (reanimate)
24560 {
24561 /* The pending return value could be overwritten by a ":return"
24562 * without argument in a finally clause; reset the default
24563 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024564 current_funccal->rettv->v_type = VAR_NUMBER;
24565 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 }
24567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024568 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024569 }
24570 else
24571 {
24572 current_funccal->returned = TRUE;
24573
24574 /* If the return is carried out now, store the return value. For
24575 * a return immediately after reanimation, the value is already
24576 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024577 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024579 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024580 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024581 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024582 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583 }
24584 }
24585
24586 return idx < 0;
24587}
24588
24589/*
24590 * Free the variable with a pending return value.
24591 */
24592 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024593discard_pending_return(rettv)
24594 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595{
Bram Moolenaar33570922005-01-25 22:26:29 +000024596 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597}
24598
24599/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024600 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601 * is an allocated string. Used by report_pending() for verbose messages.
24602 */
24603 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024604get_return_cmd(rettv)
24605 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024606{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024607 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024608 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024609 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024610
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024611 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024612 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024613 if (s == NULL)
24614 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024615
24616 STRCPY(IObuff, ":return ");
24617 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24618 if (STRLEN(s) + 8 >= IOSIZE)
24619 STRCPY(IObuff + IOSIZE - 4, "...");
24620 vim_free(tofree);
24621 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622}
24623
24624/*
24625 * Get next function line.
24626 * Called by do_cmdline() to get the next line.
24627 * Returns allocated string, or NULL for end of function.
24628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024629 char_u *
24630get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024631 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024633 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634{
Bram Moolenaar33570922005-01-25 22:26:29 +000024635 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024636 ufunc_T *fp = fcp->func;
24637 char_u *retval;
24638 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639
24640 /* If breakpoints have been added/deleted need to check for it. */
24641 if (fcp->dbg_tick != debug_tick)
24642 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024643 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024644 sourcing_lnum);
24645 fcp->dbg_tick = debug_tick;
24646 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024647#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024648 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024649 func_line_end(cookie);
24650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651
Bram Moolenaar05159a02005-02-26 23:04:13 +000024652 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024653 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24654 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 retval = NULL;
24656 else
24657 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024658 /* Skip NULL lines (continuation lines). */
24659 while (fcp->linenr < gap->ga_len
24660 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24661 ++fcp->linenr;
24662 if (fcp->linenr >= gap->ga_len)
24663 retval = NULL;
24664 else
24665 {
24666 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24667 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024668#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024669 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024670 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024671#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 }
24674
24675 /* Did we encounter a breakpoint? */
24676 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24677 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024678 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024680 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024681 sourcing_lnum);
24682 fcp->dbg_tick = debug_tick;
24683 }
24684
24685 return retval;
24686}
24687
Bram Moolenaar05159a02005-02-26 23:04:13 +000024688#if defined(FEAT_PROFILE) || defined(PROTO)
24689/*
24690 * Called when starting to read a function line.
24691 * "sourcing_lnum" must be correct!
24692 * When skipping lines it may not actually be executed, but we won't find out
24693 * until later and we need to store the time now.
24694 */
24695 void
24696func_line_start(cookie)
24697 void *cookie;
24698{
24699 funccall_T *fcp = (funccall_T *)cookie;
24700 ufunc_T *fp = fcp->func;
24701
24702 if (fp->uf_profiling && sourcing_lnum >= 1
24703 && sourcing_lnum <= fp->uf_lines.ga_len)
24704 {
24705 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024706 /* Skip continuation lines. */
24707 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24708 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024709 fp->uf_tml_execed = FALSE;
24710 profile_start(&fp->uf_tml_start);
24711 profile_zero(&fp->uf_tml_children);
24712 profile_get_wait(&fp->uf_tml_wait);
24713 }
24714}
24715
24716/*
24717 * Called when actually executing a function line.
24718 */
24719 void
24720func_line_exec(cookie)
24721 void *cookie;
24722{
24723 funccall_T *fcp = (funccall_T *)cookie;
24724 ufunc_T *fp = fcp->func;
24725
24726 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24727 fp->uf_tml_execed = TRUE;
24728}
24729
24730/*
24731 * Called when done with a function line.
24732 */
24733 void
24734func_line_end(cookie)
24735 void *cookie;
24736{
24737 funccall_T *fcp = (funccall_T *)cookie;
24738 ufunc_T *fp = fcp->func;
24739
24740 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24741 {
24742 if (fp->uf_tml_execed)
24743 {
24744 ++fp->uf_tml_count[fp->uf_tml_idx];
24745 profile_end(&fp->uf_tml_start);
24746 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024747 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024748 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24749 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024750 }
24751 fp->uf_tml_idx = -1;
24752 }
24753}
24754#endif
24755
Bram Moolenaar071d4272004-06-13 20:20:40 +000024756/*
24757 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024758 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024759 */
24760 int
24761func_has_ended(cookie)
24762 void *cookie;
24763{
Bram Moolenaar33570922005-01-25 22:26:29 +000024764 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765
24766 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24767 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024768 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769 || fcp->returned);
24770}
24771
24772/*
24773 * return TRUE if cookie indicates a function which "abort"s on errors.
24774 */
24775 int
24776func_has_abort(cookie)
24777 void *cookie;
24778{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024779 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780}
24781
24782#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24783typedef enum
24784{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024785 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24786 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24787 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788} var_flavour_T;
24789
24790static var_flavour_T var_flavour __ARGS((char_u *varname));
24791
24792 static var_flavour_T
24793var_flavour(varname)
24794 char_u *varname;
24795{
24796 char_u *p = varname;
24797
24798 if (ASCII_ISUPPER(*p))
24799 {
24800 while (*(++p))
24801 if (ASCII_ISLOWER(*p))
24802 return VAR_FLAVOUR_SESSION;
24803 return VAR_FLAVOUR_VIMINFO;
24804 }
24805 else
24806 return VAR_FLAVOUR_DEFAULT;
24807}
24808#endif
24809
24810#if defined(FEAT_VIMINFO) || defined(PROTO)
24811/*
24812 * Restore global vars that start with a capital from the viminfo file
24813 */
24814 int
24815read_viminfo_varlist(virp, writing)
24816 vir_T *virp;
24817 int writing;
24818{
24819 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024820 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024821 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024822
24823 if (!writing && (find_viminfo_parameter('!') != NULL))
24824 {
24825 tab = vim_strchr(virp->vir_line + 1, '\t');
24826 if (tab != NULL)
24827 {
24828 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024829 switch (*tab)
24830 {
24831 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024832#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024833 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024834#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024835 case 'D': type = VAR_DICT; break;
24836 case 'L': type = VAR_LIST; break;
24837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838
24839 tab = vim_strchr(tab, '\t');
24840 if (tab != NULL)
24841 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024842 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024843 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024844 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024845 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024846#ifdef FEAT_FLOAT
24847 else if (type == VAR_FLOAT)
24848 (void)string2float(tab + 1, &tv.vval.v_float);
24849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024851 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024852 if (type == VAR_DICT || type == VAR_LIST)
24853 {
24854 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24855
24856 if (etv == NULL)
24857 /* Failed to parse back the dict or list, use it as a
24858 * string. */
24859 tv.v_type = VAR_STRING;
24860 else
24861 {
24862 vim_free(tv.vval.v_string);
24863 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024864 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024865 }
24866 }
24867
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024868 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024869
24870 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024871 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024872 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24873 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024874 }
24875 }
24876 }
24877
24878 return viminfo_readline(virp);
24879}
24880
24881/*
24882 * Write global vars that start with a capital to the viminfo file
24883 */
24884 void
24885write_viminfo_varlist(fp)
24886 FILE *fp;
24887{
Bram Moolenaar33570922005-01-25 22:26:29 +000024888 hashitem_T *hi;
24889 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024890 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024891 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024892 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024893 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024894 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895
24896 if (find_viminfo_parameter('!') == NULL)
24897 return;
24898
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024899 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024900
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024901 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024902 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024904 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024906 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024907 this_var = HI2DI(hi);
24908 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024909 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024910 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024911 {
24912 case VAR_STRING: s = "STR"; break;
24913 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024914#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024915 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024916#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024917 case VAR_DICT: s = "DIC"; break;
24918 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024919 default: continue;
24920 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024921 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024922 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024923 if (p != NULL)
24924 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024925 vim_free(tofree);
24926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927 }
24928 }
24929}
24930#endif
24931
24932#if defined(FEAT_SESSION) || defined(PROTO)
24933 int
24934store_session_globals(fd)
24935 FILE *fd;
24936{
Bram Moolenaar33570922005-01-25 22:26:29 +000024937 hashitem_T *hi;
24938 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024939 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940 char_u *p, *t;
24941
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024942 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024943 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024945 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024946 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024947 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024948 this_var = HI2DI(hi);
24949 if ((this_var->di_tv.v_type == VAR_NUMBER
24950 || this_var->di_tv.v_type == VAR_STRING)
24951 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024952 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024953 /* Escape special characters with a backslash. Turn a LF and
24954 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024955 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024956 (char_u *)"\\\"\n\r");
24957 if (p == NULL) /* out of memory */
24958 break;
24959 for (t = p; *t != NUL; ++t)
24960 if (*t == '\n')
24961 *t = 'n';
24962 else if (*t == '\r')
24963 *t = 'r';
24964 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024965 this_var->di_key,
24966 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24967 : ' ',
24968 p,
24969 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24970 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024971 || put_eol(fd) == FAIL)
24972 {
24973 vim_free(p);
24974 return FAIL;
24975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024976 vim_free(p);
24977 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024978#ifdef FEAT_FLOAT
24979 else if (this_var->di_tv.v_type == VAR_FLOAT
24980 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24981 {
24982 float_T f = this_var->di_tv.vval.v_float;
24983 int sign = ' ';
24984
24985 if (f < 0)
24986 {
24987 f = -f;
24988 sign = '-';
24989 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024990 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024991 this_var->di_key, sign, f) < 0)
24992 || put_eol(fd) == FAIL)
24993 return FAIL;
24994 }
24995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024996 }
24997 }
24998 return OK;
24999}
25000#endif
25001
Bram Moolenaar661b1822005-07-28 22:36:45 +000025002/*
25003 * Display script name where an item was last set.
25004 * Should only be invoked when 'verbose' is non-zero.
25005 */
25006 void
25007last_set_msg(scriptID)
25008 scid_T scriptID;
25009{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025010 char_u *p;
25011
Bram Moolenaar661b1822005-07-28 22:36:45 +000025012 if (scriptID != 0)
25013 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025014 p = home_replace_save(NULL, get_scriptname(scriptID));
25015 if (p != NULL)
25016 {
25017 verbose_enter();
25018 MSG_PUTS(_("\n\tLast set from "));
25019 MSG_PUTS(p);
25020 vim_free(p);
25021 verbose_leave();
25022 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025023 }
25024}
25025
Bram Moolenaard812df62008-11-09 12:46:09 +000025026/*
25027 * List v:oldfiles in a nice way.
25028 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025029 void
25030ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025031 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025032{
25033 list_T *l = vimvars[VV_OLDFILES].vv_list;
25034 listitem_T *li;
25035 int nr = 0;
25036
25037 if (l == NULL)
25038 msg((char_u *)_("No old files"));
25039 else
25040 {
25041 msg_start();
25042 msg_scroll = TRUE;
25043 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25044 {
25045 msg_outnum((long)++nr);
25046 MSG_PUTS(": ");
25047 msg_outtrans(get_tv_string(&li->li_tv));
25048 msg_putchar('\n');
25049 out_flush(); /* output one line at a time */
25050 ui_breakcheck();
25051 }
25052 /* Assume "got_int" was set to truncate the listing. */
25053 got_int = FALSE;
25054
25055#ifdef FEAT_BROWSE_CMD
25056 if (cmdmod.browse)
25057 {
25058 quit_more = FALSE;
25059 nr = prompt_for_number(FALSE);
25060 msg_starthere();
25061 if (nr > 0)
25062 {
25063 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25064 (long)nr);
25065
25066 if (p != NULL)
25067 {
25068 p = expand_env_save(p);
25069 eap->arg = p;
25070 eap->cmdidx = CMD_edit;
25071 cmdmod.browse = FALSE;
25072 do_exedit(eap, NULL);
25073 vim_free(p);
25074 }
25075 }
25076 }
25077#endif
25078 }
25079}
25080
Bram Moolenaar53744302015-07-17 17:38:22 +020025081/* reset v:option_new, v:option_old and v:option_type */
25082 void
25083reset_v_option_vars()
25084{
25085 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25086 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25087 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25088}
25089
25090
Bram Moolenaar071d4272004-06-13 20:20:40 +000025091#endif /* FEAT_EVAL */
25092
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025094#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025095
25096#ifdef WIN3264
25097/*
25098 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25099 */
25100static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25101static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25102static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25103
25104/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025105 * Get the short path (8.3) for the filename in "fnamep".
25106 * Only works for a valid file name.
25107 * When the path gets longer "fnamep" is changed and the allocated buffer
25108 * is put in "bufp".
25109 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25110 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111 */
25112 static int
25113get_short_pathname(fnamep, bufp, fnamelen)
25114 char_u **fnamep;
25115 char_u **bufp;
25116 int *fnamelen;
25117{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025118 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025119 char_u *newbuf;
25120
25121 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122 l = GetShortPathName(*fnamep, *fnamep, len);
25123 if (l > len - 1)
25124 {
25125 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025126 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025127 newbuf = vim_strnsave(*fnamep, l);
25128 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130
25131 vim_free(*bufp);
25132 *fnamep = *bufp = newbuf;
25133
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025134 /* Really should always succeed, as the buffer is big enough. */
25135 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025136 }
25137
25138 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025139 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025140}
25141
25142/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025143 * Get the short path (8.3) for the filename in "fname". The converted
25144 * path is returned in "bufp".
25145 *
25146 * Some of the directories specified in "fname" may not exist. This function
25147 * will shorten the existing directories at the beginning of the path and then
25148 * append the remaining non-existing path.
25149 *
25150 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025151 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025152 * bufp - Pointer to an allocated buffer for the filename.
25153 * fnamelen - Length of the filename pointed to by fname
25154 *
25155 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156 */
25157 static int
25158shortpath_for_invalid_fname(fname, bufp, fnamelen)
25159 char_u **fname;
25160 char_u **bufp;
25161 int *fnamelen;
25162{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025163 char_u *short_fname, *save_fname, *pbuf_unused;
25164 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025165 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025166 int old_len, len;
25167 int new_len, sfx_len;
25168 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025169
25170 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025171 old_len = *fnamelen;
25172 save_fname = vim_strnsave(*fname, old_len);
25173 pbuf_unused = NULL;
25174 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025176 endp = save_fname + old_len - 1; /* Find the end of the copy */
25177 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025179 /*
25180 * Try shortening the supplied path till it succeeds by removing one
25181 * directory at a time from the tail of the path.
25182 */
25183 len = 0;
25184 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025186 /* go back one path-separator */
25187 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25188 --endp;
25189 if (endp <= save_fname)
25190 break; /* processed the complete path */
25191
25192 /*
25193 * Replace the path separator with a NUL and try to shorten the
25194 * resulting path.
25195 */
25196 ch = *endp;
25197 *endp = 0;
25198 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025199 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025200 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25201 {
25202 retval = FAIL;
25203 goto theend;
25204 }
25205 *endp = ch; /* preserve the string */
25206
25207 if (len > 0)
25208 break; /* successfully shortened the path */
25209
25210 /* failed to shorten the path. Skip the path separator */
25211 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212 }
25213
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025214 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025216 /*
25217 * Succeeded in shortening the path. Now concatenate the shortened
25218 * path with the remaining path at the tail.
25219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025221 /* Compute the length of the new path. */
25222 sfx_len = (int)(save_endp - endp) + 1;
25223 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025224
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025225 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025226 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025227 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025229 /* There is not enough space in the currently allocated string,
25230 * copy it to a buffer big enough. */
25231 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025232 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025233 {
25234 retval = FAIL;
25235 goto theend;
25236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025237 }
25238 else
25239 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025240 /* Transfer short_fname to the main buffer (it's big enough),
25241 * unless get_short_pathname() did its work in-place. */
25242 *fname = *bufp = save_fname;
25243 if (short_fname != save_fname)
25244 vim_strncpy(save_fname, short_fname, len);
25245 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025247
25248 /* concat the not-shortened part of the path */
25249 vim_strncpy(*fname + len, endp, sfx_len);
25250 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025251 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025252
25253theend:
25254 vim_free(pbuf_unused);
25255 vim_free(save_fname);
25256
25257 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258}
25259
25260/*
25261 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025262 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025263 */
25264 static int
25265shortpath_for_partial(fnamep, bufp, fnamelen)
25266 char_u **fnamep;
25267 char_u **bufp;
25268 int *fnamelen;
25269{
25270 int sepcount, len, tflen;
25271 char_u *p;
25272 char_u *pbuf, *tfname;
25273 int hasTilde;
25274
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025275 /* Count up the path separators from the RHS.. so we know which part
25276 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025278 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 if (vim_ispathsep(*p))
25280 ++sepcount;
25281
25282 /* Need full path first (use expand_env() to remove a "~/") */
25283 hasTilde = (**fnamep == '~');
25284 if (hasTilde)
25285 pbuf = tfname = expand_env_save(*fnamep);
25286 else
25287 pbuf = tfname = FullName_save(*fnamep, FALSE);
25288
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025289 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025291 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293
25294 if (len == 0)
25295 {
25296 /* Don't have a valid filename, so shorten the rest of the
25297 * path if we can. This CAN give us invalid 8.3 filenames, but
25298 * there's not a lot of point in guessing what it might be.
25299 */
25300 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025301 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25302 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025303 }
25304
25305 /* Count the paths backward to find the beginning of the desired string. */
25306 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025307 {
25308#ifdef FEAT_MBYTE
25309 if (has_mbyte)
25310 p -= mb_head_off(tfname, p);
25311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312 if (vim_ispathsep(*p))
25313 {
25314 if (sepcount == 0 || (hasTilde && sepcount == 1))
25315 break;
25316 else
25317 sepcount --;
25318 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320 if (hasTilde)
25321 {
25322 --p;
25323 if (p >= tfname)
25324 *p = '~';
25325 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025327 }
25328 else
25329 ++p;
25330
25331 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25332 vim_free(*bufp);
25333 *fnamelen = (int)STRLEN(p);
25334 *bufp = pbuf;
25335 *fnamep = p;
25336
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025337 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338}
25339#endif /* WIN3264 */
25340
25341/*
25342 * Adjust a filename, according to a string of modifiers.
25343 * *fnamep must be NUL terminated when called. When returning, the length is
25344 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025345 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346 * When there is an error, *fnamep is set to NULL.
25347 */
25348 int
25349modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25350 char_u *src; /* string with modifiers */
25351 int *usedlen; /* characters after src that are used */
25352 char_u **fnamep; /* file name so far */
25353 char_u **bufp; /* buffer for allocated file name or NULL */
25354 int *fnamelen; /* length of fnamep */
25355{
25356 int valid = 0;
25357 char_u *tail;
25358 char_u *s, *p, *pbuf;
25359 char_u dirname[MAXPATHL];
25360 int c;
25361 int has_fullname = 0;
25362#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025363 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364 int has_shortname = 0;
25365#endif
25366
25367repeat:
25368 /* ":p" - full path/file_name */
25369 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25370 {
25371 has_fullname = 1;
25372
25373 valid |= VALID_PATH;
25374 *usedlen += 2;
25375
25376 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25377 if ((*fnamep)[0] == '~'
25378#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25379 && ((*fnamep)[1] == '/'
25380# ifdef BACKSLASH_IN_FILENAME
25381 || (*fnamep)[1] == '\\'
25382# endif
25383 || (*fnamep)[1] == NUL)
25384
25385#endif
25386 )
25387 {
25388 *fnamep = expand_env_save(*fnamep);
25389 vim_free(*bufp); /* free any allocated file name */
25390 *bufp = *fnamep;
25391 if (*fnamep == NULL)
25392 return -1;
25393 }
25394
25395 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025396 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397 {
25398 if (vim_ispathsep(*p)
25399 && p[1] == '.'
25400 && (p[2] == NUL
25401 || vim_ispathsep(p[2])
25402 || (p[2] == '.'
25403 && (p[3] == NUL || vim_ispathsep(p[3])))))
25404 break;
25405 }
25406
25407 /* FullName_save() is slow, don't use it when not needed. */
25408 if (*p != NUL || !vim_isAbsName(*fnamep))
25409 {
25410 *fnamep = FullName_save(*fnamep, *p != NUL);
25411 vim_free(*bufp); /* free any allocated file name */
25412 *bufp = *fnamep;
25413 if (*fnamep == NULL)
25414 return -1;
25415 }
25416
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025417#ifdef WIN3264
25418# if _WIN32_WINNT >= 0x0500
25419 if (vim_strchr(*fnamep, '~') != NULL)
25420 {
25421 /* Expand 8.3 filename to full path. Needed to make sure the same
25422 * file does not have two different names.
25423 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25424 p = alloc(_MAX_PATH + 1);
25425 if (p != NULL)
25426 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025427 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025428 {
25429 vim_free(*bufp);
25430 *bufp = *fnamep = p;
25431 }
25432 else
25433 vim_free(p);
25434 }
25435 }
25436# endif
25437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025438 /* Append a path separator to a directory. */
25439 if (mch_isdir(*fnamep))
25440 {
25441 /* Make room for one or two extra characters. */
25442 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25443 vim_free(*bufp); /* free any allocated file name */
25444 *bufp = *fnamep;
25445 if (*fnamep == NULL)
25446 return -1;
25447 add_pathsep(*fnamep);
25448 }
25449 }
25450
25451 /* ":." - path relative to the current directory */
25452 /* ":~" - path relative to the home directory */
25453 /* ":8" - shortname path - postponed till after */
25454 while (src[*usedlen] == ':'
25455 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25456 {
25457 *usedlen += 2;
25458 if (c == '8')
25459 {
25460#ifdef WIN3264
25461 has_shortname = 1; /* Postpone this. */
25462#endif
25463 continue;
25464 }
25465 pbuf = NULL;
25466 /* Need full path first (use expand_env() to remove a "~/") */
25467 if (!has_fullname)
25468 {
25469 if (c == '.' && **fnamep == '~')
25470 p = pbuf = expand_env_save(*fnamep);
25471 else
25472 p = pbuf = FullName_save(*fnamep, FALSE);
25473 }
25474 else
25475 p = *fnamep;
25476
25477 has_fullname = 0;
25478
25479 if (p != NULL)
25480 {
25481 if (c == '.')
25482 {
25483 mch_dirname(dirname, MAXPATHL);
25484 s = shorten_fname(p, dirname);
25485 if (s != NULL)
25486 {
25487 *fnamep = s;
25488 if (pbuf != NULL)
25489 {
25490 vim_free(*bufp); /* free any allocated file name */
25491 *bufp = pbuf;
25492 pbuf = NULL;
25493 }
25494 }
25495 }
25496 else
25497 {
25498 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25499 /* Only replace it when it starts with '~' */
25500 if (*dirname == '~')
25501 {
25502 s = vim_strsave(dirname);
25503 if (s != NULL)
25504 {
25505 *fnamep = s;
25506 vim_free(*bufp);
25507 *bufp = s;
25508 }
25509 }
25510 }
25511 vim_free(pbuf);
25512 }
25513 }
25514
25515 tail = gettail(*fnamep);
25516 *fnamelen = (int)STRLEN(*fnamep);
25517
25518 /* ":h" - head, remove "/file_name", can be repeated */
25519 /* Don't remove the first "/" or "c:\" */
25520 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25521 {
25522 valid |= VALID_HEAD;
25523 *usedlen += 2;
25524 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025525 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025526 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527 *fnamelen = (int)(tail - *fnamep);
25528#ifdef VMS
25529 if (*fnamelen > 0)
25530 *fnamelen += 1; /* the path separator is part of the path */
25531#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025532 if (*fnamelen == 0)
25533 {
25534 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25535 p = vim_strsave((char_u *)".");
25536 if (p == NULL)
25537 return -1;
25538 vim_free(*bufp);
25539 *bufp = *fnamep = tail = p;
25540 *fnamelen = 1;
25541 }
25542 else
25543 {
25544 while (tail > s && !after_pathsep(s, tail))
25545 mb_ptr_back(*fnamep, tail);
25546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547 }
25548
25549 /* ":8" - shortname */
25550 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25551 {
25552 *usedlen += 2;
25553#ifdef WIN3264
25554 has_shortname = 1;
25555#endif
25556 }
25557
25558#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025559 /*
25560 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561 */
25562 if (has_shortname)
25563 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025564 /* Copy the string if it is shortened by :h and when it wasn't copied
25565 * yet, because we are going to change it in place. Avoids changing
25566 * the buffer name for "%:8". */
25567 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568 {
25569 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025570 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025571 return -1;
25572 vim_free(*bufp);
25573 *bufp = *fnamep = p;
25574 }
25575
25576 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025577 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025578 if (!has_fullname && !vim_isAbsName(*fnamep))
25579 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025580 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025581 return -1;
25582 }
25583 else
25584 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025585 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586
Bram Moolenaardc935552011-08-17 15:23:23 +020025587 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025588 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025589 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590 return -1;
25591
25592 if (l == 0)
25593 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025594 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025595 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025596 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025597 return -1;
25598 }
25599 *fnamelen = l;
25600 }
25601 }
25602#endif /* WIN3264 */
25603
25604 /* ":t" - tail, just the basename */
25605 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25606 {
25607 *usedlen += 2;
25608 *fnamelen -= (int)(tail - *fnamep);
25609 *fnamep = tail;
25610 }
25611
25612 /* ":e" - extension, can be repeated */
25613 /* ":r" - root, without extension, can be repeated */
25614 while (src[*usedlen] == ':'
25615 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25616 {
25617 /* find a '.' in the tail:
25618 * - for second :e: before the current fname
25619 * - otherwise: The last '.'
25620 */
25621 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25622 s = *fnamep - 2;
25623 else
25624 s = *fnamep + *fnamelen - 1;
25625 for ( ; s > tail; --s)
25626 if (s[0] == '.')
25627 break;
25628 if (src[*usedlen + 1] == 'e') /* :e */
25629 {
25630 if (s > tail)
25631 {
25632 *fnamelen += (int)(*fnamep - (s + 1));
25633 *fnamep = s + 1;
25634#ifdef VMS
25635 /* cut version from the extension */
25636 s = *fnamep + *fnamelen - 1;
25637 for ( ; s > *fnamep; --s)
25638 if (s[0] == ';')
25639 break;
25640 if (s > *fnamep)
25641 *fnamelen = s - *fnamep;
25642#endif
25643 }
25644 else if (*fnamep <= tail)
25645 *fnamelen = 0;
25646 }
25647 else /* :r */
25648 {
25649 if (s > tail) /* remove one extension */
25650 *fnamelen = (int)(s - *fnamep);
25651 }
25652 *usedlen += 2;
25653 }
25654
25655 /* ":s?pat?foo?" - substitute */
25656 /* ":gs?pat?foo?" - global substitute */
25657 if (src[*usedlen] == ':'
25658 && (src[*usedlen + 1] == 's'
25659 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25660 {
25661 char_u *str;
25662 char_u *pat;
25663 char_u *sub;
25664 int sep;
25665 char_u *flags;
25666 int didit = FALSE;
25667
25668 flags = (char_u *)"";
25669 s = src + *usedlen + 2;
25670 if (src[*usedlen + 1] == 'g')
25671 {
25672 flags = (char_u *)"g";
25673 ++s;
25674 }
25675
25676 sep = *s++;
25677 if (sep)
25678 {
25679 /* find end of pattern */
25680 p = vim_strchr(s, sep);
25681 if (p != NULL)
25682 {
25683 pat = vim_strnsave(s, (int)(p - s));
25684 if (pat != NULL)
25685 {
25686 s = p + 1;
25687 /* find end of substitution */
25688 p = vim_strchr(s, sep);
25689 if (p != NULL)
25690 {
25691 sub = vim_strnsave(s, (int)(p - s));
25692 str = vim_strnsave(*fnamep, *fnamelen);
25693 if (sub != NULL && str != NULL)
25694 {
25695 *usedlen = (int)(p + 1 - src);
25696 s = do_string_sub(str, pat, sub, flags);
25697 if (s != NULL)
25698 {
25699 *fnamep = s;
25700 *fnamelen = (int)STRLEN(s);
25701 vim_free(*bufp);
25702 *bufp = s;
25703 didit = TRUE;
25704 }
25705 }
25706 vim_free(sub);
25707 vim_free(str);
25708 }
25709 vim_free(pat);
25710 }
25711 }
25712 /* after using ":s", repeat all the modifiers */
25713 if (didit)
25714 goto repeat;
25715 }
25716 }
25717
Bram Moolenaar26df0922014-02-23 23:39:13 +010025718 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25719 {
25720 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25721 if (p == NULL)
25722 return -1;
25723 vim_free(*bufp);
25724 *bufp = *fnamep = p;
25725 *fnamelen = (int)STRLEN(p);
25726 *usedlen += 2;
25727 }
25728
Bram Moolenaar071d4272004-06-13 20:20:40 +000025729 return valid;
25730}
25731
25732/*
25733 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25734 * "flags" can be "g" to do a global substitute.
25735 * Returns an allocated string, NULL for error.
25736 */
25737 char_u *
25738do_string_sub(str, pat, sub, flags)
25739 char_u *str;
25740 char_u *pat;
25741 char_u *sub;
25742 char_u *flags;
25743{
25744 int sublen;
25745 regmatch_T regmatch;
25746 int i;
25747 int do_all;
25748 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025749 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025750 garray_T ga;
25751 char_u *ret;
25752 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025753 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754
25755 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25756 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025757 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758
25759 ga_init2(&ga, 1, 200);
25760
25761 do_all = (flags[0] == 'g');
25762
25763 regmatch.rm_ic = p_ic;
25764 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25765 if (regmatch.regprog != NULL)
25766 {
25767 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025768 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25770 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025771 /* Skip empty match except for first match. */
25772 if (regmatch.startp[0] == regmatch.endp[0])
25773 {
25774 if (zero_width == regmatch.startp[0])
25775 {
25776 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025777 i = MB_PTR2LEN(tail);
25778 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25779 (size_t)i);
25780 ga.ga_len += i;
25781 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025782 continue;
25783 }
25784 zero_width = regmatch.startp[0];
25785 }
25786
Bram Moolenaar071d4272004-06-13 20:20:40 +000025787 /*
25788 * Get some space for a temporary buffer to do the substitution
25789 * into. It will contain:
25790 * - The text up to where the match is.
25791 * - The substituted text.
25792 * - The text after the match.
25793 */
25794 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025795 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25797 {
25798 ga_clear(&ga);
25799 break;
25800 }
25801
25802 /* copy the text up to where the match is */
25803 i = (int)(regmatch.startp[0] - tail);
25804 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25805 /* add the substituted text */
25806 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25807 + ga.ga_len + i, TRUE, TRUE, FALSE);
25808 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025809 tail = regmatch.endp[0];
25810 if (*tail == NUL)
25811 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812 if (!do_all)
25813 break;
25814 }
25815
25816 if (ga.ga_data != NULL)
25817 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25818
Bram Moolenaar473de612013-06-08 18:19:48 +020025819 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025820 }
25821
25822 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25823 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025824 if (p_cpo == empty_option)
25825 p_cpo = save_cpo;
25826 else
25827 /* Darn, evaluating {sub} expression changed the value. */
25828 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829
25830 return ret;
25831}
25832
25833#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */