blob: 9de2764154a1594974c0486de0ef749ef63b530b [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 Moolenaar33570922005-01-25 22:26:29 +0000371};
372
373/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000374#define vv_type vv_di.di_tv.v_type
375#define vv_nr vv_di.di_tv.vval.v_number
376#define vv_float vv_di.di_tv.vval.v_float
377#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000378#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200379#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000381
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200382static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000383#define vimvarht vimvardict.dv_hashtab
384
Bram Moolenaara40058a2005-07-11 22:42:07 +0000385static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
386static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000387static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
388static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
389static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
391static void list_glob_vars __ARGS((int *first));
392static void list_buf_vars __ARGS((int *first));
393static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000394#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000395static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000396#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000397static void list_vim_vars __ARGS((int *first));
398static void list_script_vars __ARGS((int *first));
399static void list_func_vars __ARGS((int *first));
400static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000401static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
402static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100403static 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 +0000404static void clear_lval __ARGS((lval_T *lp));
405static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
406static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
408static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
409static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
410static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
411static void item_lock __ARGS((typval_T *tv, int deep, int lock));
412static int tv_islocked __ARGS((typval_T *tv));
413
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
415static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000420static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
421static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000423static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000428static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100430static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
431static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
432static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000433static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000435static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
437static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100440static 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 +0000441static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000442static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200443static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100458static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static 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 +0200460static 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 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200473static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200476static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200478static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100489static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100491static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000493#ifdef FEAT_FLOAT
494static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
495#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000496static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000499static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000501#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000502static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000503static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
509static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200510static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
515static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200525static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200527#ifdef FEAT_FLOAT
528static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
529#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000532static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#ifdef FEAT_FLOAT
539static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000543static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000552static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000554static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200558static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000561static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200562static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000570static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000571static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200572static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000573static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000574static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200577static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000578static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100584static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000587static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000601static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100606static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000608static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000620#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200621static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200624#ifdef FEAT_LUA
625static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
626#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000631static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200632static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000633static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000634static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000640#ifdef vim_mkdir
641static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100644#ifdef FEAT_MZSCHEME
645static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100649static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000650static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#ifdef FEAT_FLOAT
652static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000655static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000656static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200657#ifdef FEAT_PYTHON3
658static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
659#endif
660#ifdef FEAT_PYTHON
661static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
662#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000664static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000665static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000677#ifdef FEAT_FLOAT
678static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
679#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200680static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100682static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000685static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000687static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200692static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000695static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000696static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000697static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000698static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200700static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000701static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100703#ifdef FEAT_CRYPT
704static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
705#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000706static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200707static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#ifdef FEAT_FLOAT
710static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200711static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000712#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000714static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000715static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#ifdef FEAT_FLOAT
719static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
721#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000722static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200723static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724#ifdef HAVE_STRFTIME
725static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
726#endif
727static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200733static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200734static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000740static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200741static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200743static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000744static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000745static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000746static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000747static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000748static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000750static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200751#ifdef FEAT_FLOAT
752static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
754#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000758#ifdef FEAT_FLOAT
759static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
760#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200762static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200763static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100764static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100768static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
770static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
772static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
773static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000775static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100779static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780
Bram Moolenaar493c1782014-05-28 14:34:46 +0200781static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000782static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static int get_env_len __ARGS((char_u **arg));
784static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000785static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000786static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
787#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
788#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
789 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000790static 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 +0000791static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000792static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200793static 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 +0000794static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static typval_T *alloc_tv __ARGS((void));
796static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static void init_tv __ARGS((typval_T *varp));
798static long get_tv_number __ARGS((typval_T *varp));
799static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000800static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static char_u *get_tv_string __ARGS((typval_T *varp));
802static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000803static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100804static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
805static 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 +0000806static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
807static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
808static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000809static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
810static 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 +0000811static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200812static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
813static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200814static int var_check_func_name __ARGS((char_u *name, int new_var));
815static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200816static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000817static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
819static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
820static int eval_fname_script __ARGS((char_u *p));
821static int eval_fname_sid __ARGS((char_u *p));
822static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static ufunc_T *find_func __ARGS((char_u *name));
824static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200825static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#ifdef FEAT_PROFILE
827static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000828static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
829static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
830static int
831# ifdef __BORLANDC__
832 _RTLENTRYF
833# endif
834 prof_total_cmp __ARGS((const void *s1, const void *s2));
835static int
836# ifdef __BORLANDC__
837 _RTLENTRYF
838# endif
839 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200841static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000842static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000843static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000844static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000845static 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 +0000846static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
847static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000848static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000849static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
850static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000851static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000852static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000853static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200854static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200855static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000856
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200857
858#ifdef EBCDIC
859static int compare_func_name __ARGS((const void *s1, const void *s2));
860static void sortFunctions __ARGS(());
861#endif
862
Bram Moolenaar33570922005-01-25 22:26:29 +0000863/*
864 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000865 */
866 void
867eval_init()
868{
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 int i;
870 struct vimvar *p;
871
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200872 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
873 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200874 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000876 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877
878 for (i = 0; i < VV_LEN; ++i)
879 {
880 p = &vimvars[i];
881 STRCPY(p->vv_di.di_key, p->vv_name);
882 if (p->vv_flags & VV_RO)
883 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
884 else if (p->vv_flags & VV_RO_SBX)
885 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
886 else
887 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000888
889 /* add to v: scope dict, unless the value is not always available */
890 if (p->vv_type != VAR_UNKNOWN)
891 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000893 /* add to compat scope dict */
894 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000895 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000896 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100897 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200898 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200899 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200900
901#ifdef EBCDIC
902 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100903 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200904 */
905 sortFunctions();
906#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000907}
908
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909#if defined(EXITFREE) || defined(PROTO)
910 void
911eval_clear()
912{
913 int i;
914 struct vimvar *p;
915
916 for (i = 0; i < VV_LEN; ++i)
917 {
918 p = &vimvars[i];
919 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000921 vim_free(p->vv_str);
922 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000923 }
924 else if (p->vv_di.di_tv.v_type == VAR_LIST)
925 {
926 list_unref(p->vv_list);
927 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000928 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000929 }
930 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000931 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932 hash_clear(&compat_hashtab);
933
Bram Moolenaard9fba312005-06-26 22:34:35 +0000934 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100935# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200936 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100937# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000938
939 /* global variables */
940 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000941
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000942 /* autoloaded script names */
943 ga_clear_strings(&ga_loaded);
944
Bram Moolenaarcca74132013-09-25 21:00:28 +0200945 /* Script-local variables. First clear all the variables and in a second
946 * loop free the scriptvar_T, because a variable in one script might hold
947 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200948 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200949 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200950 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200951 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200952 ga_clear(&ga_scripts);
953
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000954 /* unreferenced lists and dicts */
955 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000956
957 /* functions */
958 free_all_functions();
959 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000960}
961#endif
962
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 * Return the name of the executed function.
965 */
966 char_u *
967func_name(cookie)
968 void *cookie;
969{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000970 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/*
974 * Return the address holding the next breakpoint line for a funccall cookie.
975 */
976 linenr_T *
977func_breakpoint(cookie)
978 void *cookie;
979{
Bram Moolenaar33570922005-01-25 22:26:29 +0000980 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981}
982
983/*
984 * Return the address holding the debug tick for a funccall cookie.
985 */
986 int *
987func_dbg_tick(cookie)
988 void *cookie;
989{
Bram Moolenaar33570922005-01-25 22:26:29 +0000990 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991}
992
993/*
994 * Return the nesting level for a funccall cookie.
995 */
996 int
997func_level(cookie)
998 void *cookie;
999{
Bram Moolenaar33570922005-01-25 22:26:29 +00001000 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001}
1002
1003/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001004funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001006/* pointer to list of previously used funccal, still around because some
1007 * item in it is still being used. */
1008funccall_T *previous_funccal = NULL;
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010/*
1011 * Return TRUE when a function was ended by a ":return" command.
1012 */
1013 int
1014current_func_returned()
1015{
1016 return current_funccal->returned;
1017}
1018
1019
1020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 * Set an internal variable to a string value. Creates the variable if it does
1022 * not already exist.
1023 */
1024 void
1025set_internal_string_var(name, value)
1026 char_u *name;
1027 char_u *value;
1028{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001029 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001030 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
1032 val = vim_strsave(value);
1033 if (val != NULL)
1034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001035 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001036 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001038 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041 }
1042}
1043
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001045static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046static char_u *redir_endp = NULL;
1047static char_u *redir_varname = NULL;
1048
1049/*
1050 * Start recording command output to a variable
1051 * Returns OK if successfully completed the setup. FAIL otherwise.
1052 */
1053 int
1054var_redir_start(name, append)
1055 char_u *name;
1056 int append; /* append to an existing variable */
1057{
1058 int save_emsg;
1059 int err;
1060 typval_T tv;
1061
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001062 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001063 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 {
1065 EMSG(_(e_invarg));
1066 return FAIL;
1067 }
1068
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001069 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 redir_varname = vim_strsave(name);
1071 if (redir_varname == NULL)
1072 return FAIL;
1073
1074 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1075 if (redir_lval == NULL)
1076 {
1077 var_redir_stop();
1078 return FAIL;
1079 }
1080
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001081 /* The output is stored in growarray "redir_ga" until redirection ends. */
1082 ga_init2(&redir_ga, (int)sizeof(char), 500);
1083
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001085 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001086 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1088 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001089 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 if (redir_endp != NULL && *redir_endp != NUL)
1091 /* Trailing characters are present after the variable name */
1092 EMSG(_(e_trailing));
1093 else
1094 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001095 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 var_redir_stop();
1097 return FAIL;
1098 }
1099
1100 /* check if we can write to the variable: set it to or append an empty
1101 * string */
1102 save_emsg = did_emsg;
1103 did_emsg = FALSE;
1104 tv.v_type = VAR_STRING;
1105 tv.vval.v_string = (char_u *)"";
1106 if (append)
1107 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1108 else
1109 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001110 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001112 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 if (err)
1114 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001115 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 var_redir_stop();
1117 return FAIL;
1118 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119
1120 return OK;
1121}
1122
1123/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 * Append "value[value_len]" to the variable set by var_redir_start().
1125 * The actual appending is postponed until redirection ends, because the value
1126 * appended may in fact be the string we write to, changing it may cause freed
1127 * memory to be used:
1128 * :redir => foo
1129 * :let foo
1130 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
1139 if (redir_lval == NULL)
1140 return;
1141
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001143 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001145 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001147 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 {
1149 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001150 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151 }
1152 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154}
1155
1156/*
1157 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 */
1160 void
1161var_redir_stop()
1162{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163 typval_T tv;
1164
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 if (redir_lval != NULL)
1166 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* If there was no error: assign the text to the variable. */
1168 if (redir_endp != NULL)
1169 {
1170 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1171 tv.v_type = VAR_STRING;
1172 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001173 /* Call get_lval() again, if it's inside a Dict or List it may
1174 * have changed. */
1175 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001176 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001177 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1178 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1179 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001181
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001182 /* free the collected output */
1183 vim_free(redir_ga.ga_data);
1184 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001185
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 vim_free(redir_lval);
1187 redir_lval = NULL;
1188 }
1189 vim_free(redir_varname);
1190 redir_varname = NULL;
1191}
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193# if defined(FEAT_MBYTE) || defined(PROTO)
1194 int
1195eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1196 char_u *enc_from;
1197 char_u *enc_to;
1198 char_u *fname_from;
1199 char_u *fname_to;
1200{
1201 int err = FALSE;
1202
1203 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1204 set_vim_var_string(VV_CC_TO, enc_to, -1);
1205 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1206 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1207 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1208 err = TRUE;
1209 set_vim_var_string(VV_CC_FROM, NULL, -1);
1210 set_vim_var_string(VV_CC_TO, NULL, -1);
1211 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1212 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1213
1214 if (err)
1215 return FAIL;
1216 return OK;
1217}
1218# endif
1219
1220# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1221 int
1222eval_printexpr(fname, args)
1223 char_u *fname;
1224 char_u *args;
1225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_FNAME_IN, fname, -1);
1229 set_vim_var_string(VV_CMDARG, args, -1);
1230 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1231 err = TRUE;
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_CMDARG, NULL, -1);
1234
1235 if (err)
1236 {
1237 mch_remove(fname);
1238 return FAIL;
1239 }
1240 return OK;
1241}
1242# endif
1243
1244# if defined(FEAT_DIFF) || defined(PROTO)
1245 void
1246eval_diff(origfile, newfile, outfile)
1247 char_u *origfile;
1248 char_u *newfile;
1249 char_u *outfile;
1250{
1251 int err = FALSE;
1252
1253 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1254 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1255 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1256 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1257 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1258 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1259 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1260}
1261
1262 void
1263eval_patch(origfile, difffile, outfile)
1264 char_u *origfile;
1265 char_u *difffile;
1266 char_u *outfile;
1267{
1268 int err;
1269
1270 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1271 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1272 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1273 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1274 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1275 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1276 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1277}
1278# endif
1279
1280/*
1281 * Top level evaluation function, returning a boolean.
1282 * Sets "error" to TRUE if there was an error.
1283 * Return TRUE or FALSE.
1284 */
1285 int
1286eval_to_bool(arg, error, nextcmd, skip)
1287 char_u *arg;
1288 int *error;
1289 char_u **nextcmd;
1290 int skip; /* only parse, don't execute */
1291{
Bram Moolenaar33570922005-01-25 22:26:29 +00001292 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 int retval = FALSE;
1294
1295 if (skip)
1296 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 else
1300 {
1301 *error = FALSE;
1302 if (!skip)
1303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001304 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 }
1308 if (skip)
1309 --emsg_skip;
1310
1311 return retval;
1312}
1313
1314/*
1315 * Top level evaluation function, returning a string. If "skip" is TRUE,
1316 * only parsing to "nextcmd" is done, without reporting errors. Return
1317 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1318 */
1319 char_u *
1320eval_to_string_skip(arg, nextcmd, skip)
1321 char_u *arg;
1322 char_u **nextcmd;
1323 int skip; /* only parse, don't execute */
1324{
Bram Moolenaar33570922005-01-25 22:26:29 +00001325 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 char_u *retval;
1327
1328 if (skip)
1329 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 retval = NULL;
1332 else
1333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 retval = vim_strsave(get_tv_string(&tv));
1335 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337 if (skip)
1338 --emsg_skip;
1339
1340 return retval;
1341}
1342
1343/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344 * Skip over an expression at "*pp".
1345 * Return FAIL for an error, OK otherwise.
1346 */
1347 int
1348skip_expr(pp)
1349 char_u **pp;
1350{
Bram Moolenaar33570922005-01-25 22:26:29 +00001351 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001352
1353 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001355}
1356
1357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 * When "convert" is TRUE convert a List into a sequence of lines and convert
1360 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 * Return pointer to allocated memory, or NULL for failure.
1362 */
1363 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 char_u *arg;
1366 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001367 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001372#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001373 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 retval = NULL;
1378 else
1379 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001380 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 {
1382 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001383 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001384 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001385 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001386 if (tv.vval.v_list->lv_len > 0)
1387 ga_append(&ga, NL);
1388 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 ga_append(&ga, NUL);
1390 retval = (char_u *)ga.ga_data;
1391 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001392#ifdef FEAT_FLOAT
1393 else if (convert && tv.v_type == VAR_FLOAT)
1394 {
1395 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1396 retval = vim_strsave(numbuf);
1397 }
1398#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001399 else
1400 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403
1404 return retval;
1405}
1406
1407/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001408 * Call eval_to_string() without using current local variables and using
1409 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 */
1411 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001412eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 char_u *arg;
1414 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001415 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
1417 char_u *retval;
1418 void *save_funccalp;
1419
1420 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001421 if (use_sandbox)
1422 ++sandbox;
1423 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001424 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001425 if (use_sandbox)
1426 --sandbox;
1427 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 restore_funccal(save_funccalp);
1429 return retval;
1430}
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432/*
1433 * Top level evaluation function, returning a number.
1434 * Evaluates "expr" silently.
1435 * Returns -1 for an error.
1436 */
1437 int
1438eval_to_number(expr)
1439 char_u *expr;
1440{
Bram Moolenaar33570922005-01-25 22:26:29 +00001441 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001443 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
1445 ++emsg_off;
1446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 retval = -1;
1449 else
1450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001451 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001452 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
1454 --emsg_off;
1455
1456 return retval;
1457}
1458
Bram Moolenaara40058a2005-07-11 22:42:07 +00001459/*
1460 * Prepare v: variable "idx" to be used.
1461 * Save the current typeval in "save_tv".
1462 * When not used yet add the variable to the v: hashtable.
1463 */
1464 static void
1465prepare_vimvar(idx, save_tv)
1466 int idx;
1467 typval_T *save_tv;
1468{
1469 *save_tv = vimvars[idx].vv_tv;
1470 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1471 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1472}
1473
1474/*
1475 * Restore v: variable "idx" to typeval "save_tv".
1476 * When no longer defined, remove the variable from the v: hashtable.
1477 */
1478 static void
1479restore_vimvar(idx, save_tv)
1480 int idx;
1481 typval_T *save_tv;
1482{
1483 hashitem_T *hi;
1484
Bram Moolenaara40058a2005-07-11 22:42:07 +00001485 vimvars[idx].vv_tv = *save_tv;
1486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1487 {
1488 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1489 if (HASHITEM_EMPTY(hi))
1490 EMSG2(_(e_intern2), "restore_vimvar()");
1491 else
1492 hash_remove(&vimvarht, hi);
1493 }
1494}
1495
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001496#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001497/*
1498 * Evaluate an expression to a list with suggestions.
1499 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001500 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501 */
1502 list_T *
1503eval_spell_expr(badword, expr)
1504 char_u *badword;
1505 char_u *expr;
1506{
1507 typval_T save_val;
1508 typval_T rettv;
1509 list_T *list = NULL;
1510 char_u *p = skipwhite(expr);
1511
1512 /* Set "v:val" to the bad word. */
1513 prepare_vimvar(VV_VAL, &save_val);
1514 vimvars[VV_VAL].vv_type = VAR_STRING;
1515 vimvars[VV_VAL].vv_str = badword;
1516 if (p_verbose == 0)
1517 ++emsg_off;
1518
1519 if (eval1(&p, &rettv, TRUE) == OK)
1520 {
1521 if (rettv.v_type != VAR_LIST)
1522 clear_tv(&rettv);
1523 else
1524 list = rettv.vval.v_list;
1525 }
1526
1527 if (p_verbose == 0)
1528 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001529 restore_vimvar(VV_VAL, &save_val);
1530
1531 return list;
1532}
1533
1534/*
1535 * "list" is supposed to contain two items: a word and a number. Return the
1536 * word in "pp" and the number as the return value.
1537 * Return -1 if anything isn't right.
1538 * Used to get the good word and score from the eval_spell_expr() result.
1539 */
1540 int
1541get_spellword(list, pp)
1542 list_T *list;
1543 char_u **pp;
1544{
1545 listitem_T *li;
1546
1547 li = list->lv_first;
1548 if (li == NULL)
1549 return -1;
1550 *pp = get_tv_string(&li->li_tv);
1551
1552 li = li->li_next;
1553 if (li == NULL)
1554 return -1;
1555 return get_tv_number(&li->li_tv);
1556}
1557#endif
1558
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001559/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 * Top level evaluation function.
1561 * Returns an allocated typval_T with the result.
1562 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001563 */
1564 typval_T *
1565eval_expr(arg, nextcmd)
1566 char_u *arg;
1567 char_u **nextcmd;
1568{
1569 typval_T *tv;
1570
1571 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001572 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001573 {
1574 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001575 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001576 }
1577
1578 return tv;
1579}
1580
1581
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001583 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584 * Uses argv[argc] for the function arguments. Only Number and String
1585 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001588 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001589call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 char_u *func;
1591 int argc;
1592 char_u **argv;
1593 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001594 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
Bram Moolenaar33570922005-01-25 22:26:29 +00001597 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 long n;
1599 int len;
1600 int i;
1601 int doesrange;
1602 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001605 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001607 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609 for (i = 0; i < argc; i++)
1610 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001611 /* Pass a NULL or empty argument as an empty string */
1612 if (argv[i] == NULL || *argv[i] == NUL)
1613 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001614 argvars[i].v_type = VAR_STRING;
1615 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001616 continue;
1617 }
1618
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001619 if (str_arg_only)
1620 len = 0;
1621 else
1622 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001623 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 if (len != 0 && len == (int)STRLEN(argv[i]))
1625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001626 argvars[i].v_type = VAR_NUMBER;
1627 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
1629 else
1630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001631 argvars[i].v_type = VAR_STRING;
1632 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
1634 }
1635
1636 if (safe)
1637 {
1638 save_funccalp = save_funccal();
1639 ++sandbox;
1640 }
1641
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1643 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001645 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (safe)
1647 {
1648 --sandbox;
1649 restore_funccal(save_funccalp);
1650 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651 vim_free(argvars);
1652
1653 if (ret == FAIL)
1654 clear_tv(rettv);
1655
1656 return ret;
1657}
1658
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001659/*
1660 * Call vimL function "func" and return the result as a number.
1661 * Returns -1 when calling the function fails.
1662 * Uses argv[argc] for the function arguments.
1663 */
1664 long
1665call_func_retnr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
1672 long retval;
1673
1674 /* All arguments are passed as strings, no conversion to number. */
1675 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1676 return -1;
1677
1678 retval = get_tv_number_chk(&rettv, NULL);
1679 clear_tv(&rettv);
1680 return retval;
1681}
1682
1683#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1684 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1685
Bram Moolenaar4f688582007-07-24 12:34:30 +00001686# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001688 * Call vimL function "func" and return the result as a string.
1689 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690 * Uses argv[argc] for the function arguments.
1691 */
1692 void *
1693call_func_retstr(func, argc, argv, safe)
1694 char_u *func;
1695 int argc;
1696 char_u **argv;
1697 int safe; /* use the sandbox */
1698{
1699 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001700 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001702 /* All arguments are passed as strings, no conversion to number. */
1703 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001704 return NULL;
1705
1706 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 return retval;
1709}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001710# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001712/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001713 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001715 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716 */
1717 void *
1718call_func_retlist(func, argc, argv, safe)
1719 char_u *func;
1720 int argc;
1721 char_u **argv;
1722 int safe; /* use the sandbox */
1723{
1724 typval_T rettv;
1725
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001726 /* All arguments are passed as strings, no conversion to number. */
1727 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 return NULL;
1729
1730 if (rettv.v_type != VAR_LIST)
1731 {
1732 clear_tv(&rettv);
1733 return NULL;
1734 }
1735
1736 return rettv.vval.v_list;
1737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
1739
1740/*
1741 * Save the current function call pointer, and set it to NULL.
1742 * Used when executing autocommands and for ":source".
1743 */
1744 void *
1745save_funccal()
1746{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 current_funccal = NULL;
1750 return (void *)fc;
1751}
1752
1753 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754restore_funccal(vfc)
1755 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001757 funccall_T *fc = (funccall_T *)vfc;
1758
1759 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760}
1761
Bram Moolenaar05159a02005-02-26 23:04:13 +00001762#if defined(FEAT_PROFILE) || defined(PROTO)
1763/*
1764 * Prepare profiling for entering a child or something else that is not
1765 * counted for the script/function itself.
1766 * Should always be called in pair with prof_child_exit().
1767 */
1768 void
1769prof_child_enter(tm)
1770 proftime_T *tm; /* place to store waittime */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 profile_start(&fc->prof_child);
1776 script_prof_save(tm);
1777}
1778
1779/*
1780 * Take care of time spent in a child.
1781 * Should always be called after prof_child_enter().
1782 */
1783 void
1784prof_child_exit(tm)
1785 proftime_T *tm; /* where waittime was stored */
1786{
1787 funccall_T *fc = current_funccal;
1788
1789 if (fc != NULL && fc->func->uf_profiling)
1790 {
1791 profile_end(&fc->prof_child);
1792 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1793 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1794 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1795 }
1796 script_prof_restore(tm);
1797}
1798#endif
1799
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801#ifdef FEAT_FOLDING
1802/*
1803 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1804 * it in "*cp". Doesn't give error messages.
1805 */
1806 int
1807eval_foldexpr(arg, cp)
1808 char_u *arg;
1809 int *cp;
1810{
Bram Moolenaar33570922005-01-25 22:26:29 +00001811 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 int retval;
1813 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001814 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1815 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001818 if (use_sandbox)
1819 ++sandbox;
1820 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 retval = 0;
1824 else
1825 {
1826 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 if (tv.v_type == VAR_NUMBER)
1828 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001829 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 retval = 0;
1831 else
1832 {
1833 /* If the result is a string, check if there is a non-digit before
1834 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if (!VIM_ISDIGIT(*s) && *s != '-')
1837 *cp = *s++;
1838 retval = atol((char *)s);
1839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001843 if (use_sandbox)
1844 --sandbox;
1845 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 return retval;
1848}
1849#endif
1850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 * ":let" list all variable values
1853 * ":let var1 var2" list variable values
1854 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 * ":let var += expr" assignment command.
1856 * ":let var -= expr" assignment command.
1857 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 */
1860 void
1861ex_let(eap)
1862 exarg_T *eap;
1863{
1864 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001866 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 int var_count = 0;
1869 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001871 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaardb552d602006-03-23 22:59:57 +00001874 argend = skip_var_list(arg, &var_count, &semicolon);
1875 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001876 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001877 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1878 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001879 expr = skipwhite(argend);
1880 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1881 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001883 /*
1884 * ":let" without "=": list variables
1885 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 if (*arg == '[')
1887 EMSG(_(e_invarg));
1888 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001894 list_glob_vars(&first);
1895 list_buf_vars(&first);
1896 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001897#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001899#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001900 list_script_vars(&first);
1901 list_func_vars(&first);
1902 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 eap->nextcmd = check_nextcmd(arg);
1905 }
1906 else
1907 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 op[0] = '=';
1909 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001910 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001911 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001912 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1913 op[0] = *expr; /* +=, -= or .= */
1914 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001916 else
1917 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (eap->skip)
1920 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (eap->skip)
1923 {
1924 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 --emsg_skip;
1927 }
1928 else if (i != FAIL)
1929 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 }
1934 }
1935}
1936
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937/*
1938 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1939 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 * When "nextchars" is not NULL it points to a string with characters that
1941 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1942 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 * Returns OK or FAIL;
1944 */
1945 static int
1946ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1947 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 int copy; /* copy values from "tv", don't move */
1950 int semicolon; /* from skip_var_list() */
1951 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953{
1954 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001957 listitem_T *item;
1958 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959
1960 if (*arg != '[')
1961 {
1962 /*
1963 * ":let var = expr" or ":for var in list"
1964 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 return FAIL;
1967 return OK;
1968 }
1969
1970 /*
1971 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1972 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001973 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 {
1975 EMSG(_(e_listreq));
1976 return FAIL;
1977 }
1978
1979 i = list_len(l);
1980 if (semicolon == 0 && var_count < i)
1981 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001982 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 return FAIL;
1984 }
1985 if (var_count - semicolon > i)
1986 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001987 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 return FAIL;
1989 }
1990
1991 item = l->lv_first;
1992 while (*arg != ']')
1993 {
1994 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 item = item->li_next;
1997 if (arg == NULL)
1998 return FAIL;
1999
2000 arg = skipwhite(arg);
2001 if (*arg == ';')
2002 {
2003 /* Put the rest of the list (may be empty) in the var after ';'.
2004 * Create a new list for this. */
2005 l = list_alloc();
2006 if (l == NULL)
2007 return FAIL;
2008 while (item != NULL)
2009 {
2010 list_append_tv(l, &item->li_tv);
2011 item = item->li_next;
2012 }
2013
2014 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002015 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 ltv.vval.v_list = l;
2017 l->lv_refcount = 1;
2018
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2020 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 clear_tv(&ltv);
2022 if (arg == NULL)
2023 return FAIL;
2024 break;
2025 }
2026 else if (*arg != ',' && *arg != ']')
2027 {
2028 EMSG2(_(e_intern2), "ex_let_vars()");
2029 return FAIL;
2030 }
2031 }
2032
2033 return OK;
2034}
2035
2036/*
2037 * Skip over assignable variable "var" or list of variables "[var, var]".
2038 * Used for ":let varvar = expr" and ":for varvar in expr".
2039 * For "[var, var]" increment "*var_count" for each variable.
2040 * for "[var, var; var]" set "semicolon".
2041 * Return NULL for an error.
2042 */
2043 static char_u *
2044skip_var_list(arg, var_count, semicolon)
2045 char_u *arg;
2046 int *var_count;
2047 int *semicolon;
2048{
2049 char_u *p, *s;
2050
2051 if (*arg == '[')
2052 {
2053 /* "[var, var]": find the matching ']'. */
2054 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002055 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 {
2057 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2058 s = skip_var_one(p);
2059 if (s == p)
2060 {
2061 EMSG2(_(e_invarg2), p);
2062 return NULL;
2063 }
2064 ++*var_count;
2065
2066 p = skipwhite(s);
2067 if (*p == ']')
2068 break;
2069 else if (*p == ';')
2070 {
2071 if (*semicolon == 1)
2072 {
2073 EMSG(_("Double ; in list of variables"));
2074 return NULL;
2075 }
2076 *semicolon = 1;
2077 }
2078 else if (*p != ',')
2079 {
2080 EMSG2(_(e_invarg2), p);
2081 return NULL;
2082 }
2083 }
2084 return p + 1;
2085 }
2086 else
2087 return skip_var_one(arg);
2088}
2089
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002090/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002091 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002092 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002093 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094 static char_u *
2095skip_var_one(arg)
2096 char_u *arg;
2097{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002098 if (*arg == '@' && arg[1] != NUL)
2099 return arg + 2;
2100 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2101 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102}
2103
Bram Moolenaara7043832005-01-21 11:56:39 +00002104/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002105 * List variables for hashtab "ht" with prefix "prefix".
2106 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002108 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002112 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002114{
Bram Moolenaar33570922005-01-25 22:26:29 +00002115 hashitem_T *hi;
2116 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117 int todo;
2118
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002119 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2121 {
2122 if (!HASHITEM_EMPTY(hi))
2123 {
2124 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 di = HI2DI(hi);
2126 if (empty || di->di_tv.v_type != VAR_STRING
2127 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002129 }
2130 }
2131}
2132
2133/*
2134 * List global variables.
2135 */
2136 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137list_glob_vars(first)
2138 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002139{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141}
2142
2143/*
2144 * List buffer variables.
2145 */
2146 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147list_buf_vars(first)
2148 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 char_u numbuf[NUMBUFLEN];
2151
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154
2155 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2157 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158}
2159
2160/*
2161 * List window variables.
2162 */
2163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164list_win_vars(first)
2165 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002166{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002167 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002169}
2170
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002171#ifdef FEAT_WINDOWS
2172/*
2173 * List tab page variables.
2174 */
2175 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176list_tab_vars(first)
2177 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002178{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002179 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002181}
2182#endif
2183
Bram Moolenaara7043832005-01-21 11:56:39 +00002184/*
2185 * List Vim variables.
2186 */
2187 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188list_vim_vars(first)
2189 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192}
2193
2194/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002195 * List script-local variables, if there is a script.
2196 */
2197 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198list_script_vars(first)
2199 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200{
2201 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2203 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204}
2205
2206/*
2207 * List function variables, if there is a function.
2208 */
2209 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210list_func_vars(first)
2211 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212{
2213 if (current_funccal != NULL)
2214 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216}
2217
2218/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 * List variables in "arg".
2220 */
2221 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 exarg_T *eap;
2224 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002225 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226{
2227 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 char_u *name_start;
2231 char_u *arg_subsc;
2232 char_u *tofree;
2233 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234
2235 while (!ends_excmd(*arg) && !got_int)
2236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002239 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2241 {
2242 emsg_severe = TRUE;
2243 EMSG(_(e_trailing));
2244 break;
2245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 /* get_name_len() takes care of expanding curly braces */
2250 name_start = name = arg;
2251 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2252 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* This is mainly to keep test 49 working: when expanding
2255 * curly braces fails overrule the exception error message. */
2256 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 emsg_severe = TRUE;
2259 EMSG2(_(e_invarg2), arg);
2260 break;
2261 }
2262 error = TRUE;
2263 }
2264 else
2265 {
2266 if (tofree != NULL)
2267 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002268 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 else
2271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 /* handle d.key, l[idx], f(expr) */
2273 arg_subsc = arg;
2274 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002282 case 'g': list_glob_vars(first); break;
2283 case 'b': list_buf_vars(first); break;
2284 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002285#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002286 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002287#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 case 'v': list_vim_vars(first); break;
2289 case 's': list_script_vars(first); break;
2290 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 default:
2292 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 }
2295 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 {
2297 char_u numbuf[NUMBUFLEN];
2298 char_u *tf;
2299 int c;
2300 char_u *s;
2301
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002302 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 c = *arg;
2304 *arg = NUL;
2305 list_one_var_a((char_u *)"",
2306 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 tv.v_type,
2308 s == NULL ? (char_u *)"" : s,
2309 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 *arg = c;
2311 vim_free(tf);
2312 }
2313 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
2316 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317
2318 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320
2321 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 }
2323
2324 return arg;
2325}
2326
2327/*
2328 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2329 * Returns a pointer to the char just after the var name.
2330 * Returns NULL if there is an error.
2331 */
2332 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002335 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002337 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339{
2340 int c1;
2341 char_u *name;
2342 char_u *p;
2343 char_u *arg_end = NULL;
2344 int len;
2345 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347
2348 /*
2349 * ":let $VAR = expr": Set environment variable.
2350 */
2351 if (*arg == '$')
2352 {
2353 /* Find the end of the name. */
2354 ++arg;
2355 name = arg;
2356 len = get_env_len(&arg);
2357 if (len == 0)
2358 EMSG2(_(e_invarg2), name - 1);
2359 else
2360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 if (op != NULL && (*op == '+' || *op == '-'))
2362 EMSG2(_(e_letwrong), op);
2363 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002364 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002366 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 {
2368 c1 = name[len];
2369 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 p = get_tv_string_chk(tv);
2371 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 {
2373 int mustfree = FALSE;
2374 char_u *s = vim_getenv(name, &mustfree);
2375
2376 if (s != NULL)
2377 {
2378 p = tofree = concat_str(s, p);
2379 if (mustfree)
2380 vim_free(s);
2381 }
2382 }
2383 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 if (STRICMP(name, "HOME") == 0)
2387 init_homedir();
2388 else if (didset_vim && STRICMP(name, "VIM") == 0)
2389 didset_vim = FALSE;
2390 else if (didset_vimruntime
2391 && STRICMP(name, "VIMRUNTIME") == 0)
2392 didset_vimruntime = FALSE;
2393 arg_end = arg;
2394 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 }
2398 }
2399 }
2400
2401 /*
2402 * ":let &option = expr": Set option value.
2403 * ":let &l:option = expr": Set local option value.
2404 * ":let &g:option = expr": Set global option value.
2405 */
2406 else if (*arg == '&')
2407 {
2408 /* Find the end of the name. */
2409 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002410 if (p == NULL || (endchars != NULL
2411 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 EMSG(_(e_letunexp));
2413 else
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 long n;
2416 int opt_type;
2417 long numval;
2418 char_u *stringval = NULL;
2419 char_u *s;
2420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 c1 = *p;
2422 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423
2424 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 s = get_tv_string_chk(tv); /* != NULL if number or string */
2426 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 {
2428 opt_type = get_option_value(arg, &numval,
2429 &stringval, opt_flags);
2430 if ((opt_type == 1 && *op == '.')
2431 || (opt_type == 0 && *op != '.'))
2432 EMSG2(_(e_letwrong), op);
2433 else
2434 {
2435 if (opt_type == 1) /* number */
2436 {
2437 if (*op == '+')
2438 n = numval + n;
2439 else
2440 n = numval - n;
2441 }
2442 else if (opt_type == 0 && stringval != NULL) /* string */
2443 {
2444 s = concat_str(stringval, s);
2445 vim_free(stringval);
2446 stringval = s;
2447 }
2448 }
2449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 if (s != NULL)
2451 {
2452 set_option_value(arg, n, s, opt_flags);
2453 arg_end = p;
2454 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 }
2458 }
2459
2460 /*
2461 * ":let @r = expr": Set register contents.
2462 */
2463 else if (*arg == '@')
2464 {
2465 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 if (op != NULL && (*op == '+' || *op == '-'))
2467 EMSG2(_(e_letwrong), op);
2468 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002469 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 char_u *s;
2475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002476 p = get_tv_string_chk(tv);
2477 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002479 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 if (s != NULL)
2481 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002482 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 vim_free(s);
2484 }
2485 }
2486 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002487 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002489 arg_end = arg + 1;
2490 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002491 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 }
2493 }
2494
2495 /*
2496 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002499 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002501 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002503 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2507 EMSG(_(e_letunexp));
2508 else
2509 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 arg_end = p;
2512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 }
2516
2517 else
2518 EMSG2(_(e_invarg2), arg);
2519
2520 return arg_end;
2521}
2522
2523/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2525 */
2526 static int
2527check_changedtick(arg)
2528 char_u *arg;
2529{
2530 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2531 {
2532 EMSG2(_(e_readonlyvar), arg);
2533 return TRUE;
2534 }
2535 return FALSE;
2536}
2537
2538/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 * Get an lval: variable, Dict item or List item that can be assigned a value
2540 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2541 * "name.key", "name.key[expr]" etc.
2542 * Indexing only works if "name" is an existing List or Dictionary.
2543 * "name" points to the start of the name.
2544 * If "rettv" is not NULL it points to the value to be assigned.
2545 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2546 * wrong; must end in space or cmd separator.
2547 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 * flags:
2549 * GLV_QUIET: do not give error messages
2550 * GLV_NO_AUTOLOAD: do not use script autoloading
2551 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002553 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 */
2556 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002557get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002559 typval_T *rettv;
2560 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 int unlet;
2562 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002563 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002564 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 char_u *expr_start, *expr_end;
2568 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002569 dictitem_T *v;
2570 typval_T var1;
2571 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002576 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002577 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581
2582 if (skip)
2583 {
2584 /* When skipping just find the end of the name. */
2585 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002586 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 }
2588
2589 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002590 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (expr_start != NULL)
2592 {
2593 /* Don't expand the name when we already know there is an error. */
2594 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2595 && *p != '[' && *p != '.')
2596 {
2597 EMSG(_(e_trailing));
2598 return NULL;
2599 }
2600
2601 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2602 if (lp->ll_exp_name == NULL)
2603 {
2604 /* Report an invalid expression in braces, unless the
2605 * expression evaluation has been cancelled due to an
2606 * aborting error, an interrupt, or an exception. */
2607 if (!aborting() && !quiet)
2608 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002609 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 EMSG2(_(e_invarg2), name);
2611 return NULL;
2612 }
2613 }
2614 lp->ll_name = lp->ll_exp_name;
2615 }
2616 else
2617 lp->ll_name = name;
2618
2619 /* Without [idx] or .key we are done. */
2620 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2621 return p;
2622
2623 cc = *p;
2624 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002625 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (v == NULL && !quiet)
2627 EMSG2(_(e_undefvar), lp->ll_name);
2628 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 if (v == NULL)
2630 return NULL;
2631
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 /*
2633 * Loop until no more [idx] or .key is following.
2634 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2639 && !(lp->ll_tv->v_type == VAR_DICT
2640 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!quiet)
2643 EMSG(_("E689: Can only index a List or Dictionary"));
2644 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (!quiet)
2649 EMSG(_("E708: [:] must come last"));
2650 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 len = -1;
2654 if (*p == '.')
2655 {
2656 key = p + 1;
2657 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2658 ;
2659 if (len == 0)
2660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_(e_emptykey));
2663 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 }
2665 p = key + len;
2666 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667 else
2668 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (*p == ':')
2672 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 else
2674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 empty1 = FALSE;
2676 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002678 if (get_tv_string_chk(&var1) == NULL)
2679 {
2680 /* not a number or string */
2681 clear_tv(&var1);
2682 return NULL;
2683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
2685
2686 /* Optionally get the second index [ :expr]. */
2687 if (*p == ':')
2688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002692 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (rettv != NULL && (rettv->v_type != VAR_LIST
2698 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (!quiet)
2701 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 if (!empty1)
2703 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706 p = skipwhite(p + 1);
2707 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 else
2710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 if (!empty1)
2715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002718 if (get_tv_string_chk(&var2) == NULL)
2719 {
2720 /* not a number or string */
2721 if (!empty1)
2722 clear_tv(&var1);
2723 clear_tv(&var2);
2724 return NULL;
2725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002731
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 if (!quiet)
2735 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (!empty1)
2737 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
2742
2743 /* Skip to past ']'. */
2744 ++p;
2745 }
2746
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 {
2749 if (len == -1)
2750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002752 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (*key == NUL)
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (!quiet)
2756 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002761 lp->ll_list = NULL;
2762 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002763 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002764
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 /* When assigning to a scope dictionary check that a function and
2766 * variable name is valid (only variable name unless it is l: or
2767 * g: dictionary). Disallow overwriting a builtin function. */
2768 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002769 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002770 int prevval;
2771 int wrong;
2772
2773 if (len != -1)
2774 {
2775 prevval = key[len];
2776 key[len] = NUL;
2777 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002778 else
2779 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002780 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2781 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 || !valid_varname(key);
2784 if (len != -1)
2785 key[len] = prevval;
2786 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 return NULL;
2788 }
2789
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002792 /* Can't add "v:" variable. */
2793 if (lp->ll_dict == &vimvardict)
2794 {
2795 EMSG2(_(e_illvar), name);
2796 return NULL;
2797 }
2798
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002799 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 }
2808 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 if (len == -1)
2813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 p = NULL;
2816 break;
2817 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002819 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820 return NULL;
2821
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 if (len == -1)
2823 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
2826 else
2827 {
2828 /*
2829 * Get the number and item for the only or first index of the List.
2830 */
2831 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 else
2834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002835 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 clear_tv(&var1);
2837 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002838 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 lp->ll_list = lp->ll_tv->vval.v_list;
2840 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2841 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002843 if (lp->ll_n1 < 0)
2844 {
2845 lp->ll_n1 = 0;
2846 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2847 }
2848 }
2849 if (lp->ll_li == NULL)
2850 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002853 if (!quiet)
2854 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 }
2857
2858 /*
2859 * May need to find the item or absolute index for the second
2860 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 * When no index given: "lp->ll_empty2" is TRUE.
2862 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002866 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002872 {
2873 if (!quiet)
2874 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 }
2879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2881 if (lp->ll_n1 < 0)
2882 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2883 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 {
2885 if (!quiet)
2886 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002889 }
2890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 return p;
2896}
2897
2898/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 */
2901 static void
2902clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904{
2905 vim_free(lp->ll_exp_name);
2906 vim_free(lp->ll_newkey);
2907}
2908
2909/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002910 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 */
2914 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921{
2922 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 listitem_T *ri;
2924 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925
2926 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 cc = *endp;
2931 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 if (op != NULL && *op != '=')
2933 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002937 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002938 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002939 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002941 if ((di == NULL
2942 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2943 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2944 FALSE)))
2945 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 set_var(lp->ll_name, &tv, FALSE);
2947 clear_tv(&tv);
2948 }
2949 }
2950 else
2951 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002953 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002955 else if (tv_check_lock(lp->ll_newkey == NULL
2956 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002957 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002958 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 else if (lp->ll_range)
2960 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002961 listitem_T *ll_li = lp->ll_li;
2962 int ll_n1 = lp->ll_n1;
2963
2964 /*
2965 * Check whether any of the list items is locked
2966 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002967 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002968 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002969 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002970 return;
2971 ri = ri->li_next;
2972 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2973 break;
2974 ll_li = ll_li->li_next;
2975 ++ll_n1;
2976 }
2977
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 /*
2979 * Assign the List values to the list items.
2980 */
2981 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002982 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 if (op != NULL && *op != '=')
2984 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2985 else
2986 {
2987 clear_tv(&lp->ll_li->li_tv);
2988 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2989 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 ri = ri->li_next;
2991 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2992 break;
2993 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002996 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002997 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 ri = NULL;
2999 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 lp->ll_li = lp->ll_li->li_next;
3003 ++lp->ll_n1;
3004 }
3005 if (ri != NULL)
3006 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003007 else if (lp->ll_empty2
3008 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 : lp->ll_n1 != lp->ll_n2)
3010 EMSG(_("E711: List value has not enough items"));
3011 }
3012 else
3013 {
3014 /*
3015 * Assign to a List or Dictionary item.
3016 */
3017 if (lp->ll_newkey != NULL)
3018 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 if (op != NULL && *op != '=')
3020 {
3021 EMSG2(_(e_letwrong), op);
3022 return;
3023 }
3024
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003026 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 if (di == NULL)
3028 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003029 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3030 {
3031 vim_free(di);
3032 return;
3033 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003035 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 else if (op != NULL && *op != '=')
3037 {
3038 tv_op(lp->ll_tv, rettv, op);
3039 return;
3040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 /*
3045 * Assign the value to the variable or list item.
3046 */
3047 if (copy)
3048 copy_tv(rettv, lp->ll_tv);
3049 else
3050 {
3051 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003052 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 }
3055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003056}
3057
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3060 * Returns OK or FAIL.
3061 */
3062 static int
3063tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 typval_T *tv1;
3065 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 char_u *op;
3067{
3068 long n;
3069 char_u numbuf[NUMBUFLEN];
3070 char_u *s;
3071
3072 /* Can't do anything with a Funcref or a Dict on the right. */
3073 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3074 {
3075 switch (tv1->v_type)
3076 {
3077 case VAR_DICT:
3078 case VAR_FUNC:
3079 break;
3080
3081 case VAR_LIST:
3082 if (*op != '+' || tv2->v_type != VAR_LIST)
3083 break;
3084 /* List += List */
3085 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3086 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3087 return OK;
3088
3089 case VAR_NUMBER:
3090 case VAR_STRING:
3091 if (tv2->v_type == VAR_LIST)
3092 break;
3093 if (*op == '+' || *op == '-')
3094 {
3095 /* nr += nr or nr -= nr*/
3096 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003097#ifdef FEAT_FLOAT
3098 if (tv2->v_type == VAR_FLOAT)
3099 {
3100 float_T f = n;
3101
3102 if (*op == '+')
3103 f += tv2->vval.v_float;
3104 else
3105 f -= tv2->vval.v_float;
3106 clear_tv(tv1);
3107 tv1->v_type = VAR_FLOAT;
3108 tv1->vval.v_float = f;
3109 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111#endif
3112 {
3113 if (*op == '+')
3114 n += get_tv_number(tv2);
3115 else
3116 n -= get_tv_number(tv2);
3117 clear_tv(tv1);
3118 tv1->v_type = VAR_NUMBER;
3119 tv1->vval.v_number = n;
3120 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003121 }
3122 else
3123 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124 if (tv2->v_type == VAR_FLOAT)
3125 break;
3126
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 /* str .= str */
3128 s = get_tv_string(tv1);
3129 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3130 clear_tv(tv1);
3131 tv1->v_type = VAR_STRING;
3132 tv1->vval.v_string = s;
3133 }
3134 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135
3136#ifdef FEAT_FLOAT
3137 case VAR_FLOAT:
3138 {
3139 float_T f;
3140
3141 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3142 && tv2->v_type != VAR_NUMBER
3143 && tv2->v_type != VAR_STRING))
3144 break;
3145 if (tv2->v_type == VAR_FLOAT)
3146 f = tv2->vval.v_float;
3147 else
3148 f = get_tv_number(tv2);
3149 if (*op == '+')
3150 tv1->vval.v_float += f;
3151 else
3152 tv1->vval.v_float -= f;
3153 }
3154 return OK;
3155#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003156 }
3157 }
3158
3159 EMSG2(_(e_letwrong), op);
3160 return FAIL;
3161}
3162
3163/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 * Add a watcher to a list.
3165 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003166 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 list_T *l;
3169 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170{
3171 lw->lw_next = l->lv_watch;
3172 l->lv_watch = lw;
3173}
3174
3175/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003176 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 * No warning when it isn't found...
3178 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003179 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 list_T *l;
3182 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183{
Bram Moolenaar33570922005-01-25 22:26:29 +00003184 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185
3186 lwp = &l->lv_watch;
3187 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3188 {
3189 if (lw == lwrem)
3190 {
3191 *lwp = lw->lw_next;
3192 break;
3193 }
3194 lwp = &lw->lw_next;
3195 }
3196}
3197
3198/*
3199 * Just before removing an item from a list: advance watchers to the next
3200 * item.
3201 */
3202 static void
3203list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 list_T *l;
3205 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206{
Bram Moolenaar33570922005-01-25 22:26:29 +00003207 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208
3209 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3210 if (lw->lw_item == item)
3211 lw->lw_item = item->li_next;
3212}
3213
3214/*
3215 * Evaluate the expression used in a ":for var in expr" command.
3216 * "arg" points to "var".
3217 * Set "*errp" to TRUE for an error, FALSE otherwise;
3218 * Return a pointer that holds the info. Null when there is an error.
3219 */
3220 void *
3221eval_for_line(arg, errp, nextcmdp, skip)
3222 char_u *arg;
3223 int *errp;
3224 char_u **nextcmdp;
3225 int skip;
3226{
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 typval_T tv;
3230 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003231
3232 *errp = TRUE; /* default: there is an error */
3233
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235 if (fi == NULL)
3236 return NULL;
3237
3238 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3239 if (expr == NULL)
3240 return fi;
3241
3242 expr = skipwhite(expr);
3243 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3244 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003245 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 return fi;
3247 }
3248
3249 if (skip)
3250 ++emsg_skip;
3251 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3252 {
3253 *errp = FALSE;
3254 if (!skip)
3255 {
3256 l = tv.vval.v_list;
3257 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 clear_tv(&tv);
3261 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 else
3263 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003264 /* No need to increment the refcount, it's already set for the
3265 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 fi->fi_list = l;
3267 list_add_watch(l, &fi->fi_lw);
3268 fi->fi_lw.lw_item = l->lv_first;
3269 }
3270 }
3271 }
3272 if (skip)
3273 --emsg_skip;
3274
3275 return fi;
3276}
3277
3278/*
3279 * Use the first item in a ":for" list. Advance to the next.
3280 * Assign the values to the variable (list). "arg" points to the first one.
3281 * Return TRUE when a valid item was found, FALSE when at end of list or
3282 * something wrong.
3283 */
3284 int
3285next_for_item(fi_void, arg)
3286 void *fi_void;
3287 char_u *arg;
3288{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003289 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003291 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292
3293 item = fi->fi_lw.lw_item;
3294 if (item == NULL)
3295 result = FALSE;
3296 else
3297 {
3298 fi->fi_lw.lw_item = item->li_next;
3299 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3300 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3301 }
3302 return result;
3303}
3304
3305/*
3306 * Free the structure used to store info used by ":for".
3307 */
3308 void
3309free_for_info(fi_void)
3310 void *fi_void;
3311{
Bram Moolenaar33570922005-01-25 22:26:29 +00003312 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003313
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003314 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003315 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003317 list_unref(fi->fi_list);
3318 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 vim_free(fi);
3320}
3321
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3323
3324 void
3325set_context_for_expression(xp, arg, cmdidx)
3326 expand_T *xp;
3327 char_u *arg;
3328 cmdidx_T cmdidx;
3329{
3330 int got_eq = FALSE;
3331 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003332 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 if (cmdidx == CMD_let)
3335 {
3336 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003337 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338 {
3339 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003340 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 {
3342 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003343 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 if (vim_iswhite(*p))
3345 break;
3346 }
3347 return;
3348 }
3349 }
3350 else
3351 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3352 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 while ((xp->xp_pattern = vim_strpbrk(arg,
3354 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3355 {
3356 c = *xp->xp_pattern;
3357 if (c == '&')
3358 {
3359 c = xp->xp_pattern[1];
3360 if (c == '&')
3361 {
3362 ++xp->xp_pattern;
3363 xp->xp_context = cmdidx != CMD_let || got_eq
3364 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3365 }
3366 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003367 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003369 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3370 xp->xp_pattern += 2;
3371
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 else if (c == '$')
3375 {
3376 /* environment variable */
3377 xp->xp_context = EXPAND_ENV_VARS;
3378 }
3379 else if (c == '=')
3380 {
3381 got_eq = TRUE;
3382 xp->xp_context = EXPAND_EXPRESSION;
3383 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003384 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 && xp->xp_context == EXPAND_FUNCTIONS
3386 && vim_strchr(xp->xp_pattern, '(') == NULL)
3387 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003388 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 break;
3390 }
3391 else if (cmdidx != CMD_let || got_eq)
3392 {
3393 if (c == '"') /* string */
3394 {
3395 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3396 if (c == '\\' && xp->xp_pattern[1] != NUL)
3397 ++xp->xp_pattern;
3398 xp->xp_context = EXPAND_NOTHING;
3399 }
3400 else if (c == '\'') /* literal string */
3401 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003402 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3404 /* skip */ ;
3405 xp->xp_context = EXPAND_NOTHING;
3406 }
3407 else if (c == '|')
3408 {
3409 if (xp->xp_pattern[1] == '|')
3410 {
3411 ++xp->xp_pattern;
3412 xp->xp_context = EXPAND_EXPRESSION;
3413 }
3414 else
3415 xp->xp_context = EXPAND_COMMANDS;
3416 }
3417 else
3418 xp->xp_context = EXPAND_EXPRESSION;
3419 }
3420 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003421 /* Doesn't look like something valid, expand as an expression
3422 * anyway. */
3423 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 arg = xp->xp_pattern;
3425 if (*arg != NUL)
3426 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3427 /* skip */ ;
3428 }
3429 xp->xp_pattern = arg;
3430}
3431
3432#endif /* FEAT_CMDL_COMPL */
3433
3434/*
3435 * ":1,25call func(arg1, arg2)" function call.
3436 */
3437 void
3438ex_call(eap)
3439 exarg_T *eap;
3440{
3441 char_u *arg = eap->arg;
3442 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003444 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003446 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 linenr_T lnum;
3448 int doesrange;
3449 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003452 if (eap->skip)
3453 {
3454 /* trans_function_name() doesn't work well when skipping, use eval0()
3455 * instead to skip to any following command, e.g. for:
3456 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003457 ++emsg_skip;
3458 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3459 clear_tv(&rettv);
3460 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003461 return;
3462 }
3463
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003464 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003465 if (fudi.fd_newkey != NULL)
3466 {
3467 /* Still need to give an error message for missing key. */
3468 EMSG2(_(e_dictkey), fudi.fd_newkey);
3469 vim_free(fudi.fd_newkey);
3470 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003471 if (tofree == NULL)
3472 return;
3473
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003474 /* Increase refcount on dictionary, it could get deleted when evaluating
3475 * the arguments. */
3476 if (fudi.fd_dict != NULL)
3477 ++fudi.fd_dict->dv_refcount;
3478
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003479 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003480 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003481 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaar532c7802005-01-27 14:44:31 +00003483 /* Skip white space to allow ":call func ()". Not good, but required for
3484 * backward compatibility. */
3485 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 if (*startarg != '(')
3489 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003490 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 goto end;
3492 }
3493
3494 /*
3495 * When skipping, evaluate the function once, to find the end of the
3496 * arguments.
3497 * When the function takes a range, this is discovered after the first
3498 * call, and the loop is broken.
3499 */
3500 if (eap->skip)
3501 {
3502 ++emsg_skip;
3503 lnum = eap->line2; /* do it once, also with an invalid range */
3504 }
3505 else
3506 lnum = eap->line1;
3507 for ( ; lnum <= eap->line2; ++lnum)
3508 {
3509 if (!eap->skip && eap->addr_count > 0)
3510 {
3511 curwin->w_cursor.lnum = lnum;
3512 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003513#ifdef FEAT_VIRTUALEDIT
3514 curwin->w_cursor.coladd = 0;
3515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003518 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003519 eap->line1, eap->line2, &doesrange,
3520 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 failed = TRUE;
3523 break;
3524 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003525
3526 /* Handle a function returning a Funcref, Dictionary or List. */
3527 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3528 {
3529 failed = TRUE;
3530 break;
3531 }
3532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (doesrange || eap->skip)
3535 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003538 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003540 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (aborting())
3542 break;
3543 }
3544 if (eap->skip)
3545 --emsg_skip;
3546
3547 if (!failed)
3548 {
3549 /* Check for trailing illegal characters and a following command. */
3550 if (!ends_excmd(*arg))
3551 {
3552 emsg_severe = TRUE;
3553 EMSG(_(e_trailing));
3554 }
3555 else
3556 eap->nextcmd = check_nextcmd(arg);
3557 }
3558
3559end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003560 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003561 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562}
3563
3564/*
3565 * ":unlet[!] var1 ... " command.
3566 */
3567 void
3568ex_unlet(eap)
3569 exarg_T *eap;
3570{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003571 ex_unletlock(eap, eap->arg, 0);
3572}
3573
3574/*
3575 * ":lockvar" and ":unlockvar" commands
3576 */
3577 void
3578ex_lockvar(eap)
3579 exarg_T *eap;
3580{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582 int deep = 2;
3583
3584 if (eap->forceit)
3585 deep = -1;
3586 else if (vim_isdigit(*arg))
3587 {
3588 deep = getdigits(&arg);
3589 arg = skipwhite(arg);
3590 }
3591
3592 ex_unletlock(eap, arg, deep);
3593}
3594
3595/*
3596 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3597 */
3598 static void
3599ex_unletlock(eap, argstart, deep)
3600 exarg_T *eap;
3601 char_u *argstart;
3602 int deep;
3603{
3604 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 do
3610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003612 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003613 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 if (lv.ll_name == NULL)
3615 error = TRUE; /* error but continue parsing */
3616 if (name_end == NULL || (!vim_iswhite(*name_end)
3617 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (name_end != NULL)
3620 {
3621 emsg_severe = TRUE;
3622 EMSG(_(e_trailing));
3623 }
3624 if (!(eap->skip || error))
3625 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 break;
3627 }
3628
3629 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 {
3631 if (eap->cmdidx == CMD_unlet)
3632 {
3633 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3634 error = TRUE;
3635 }
3636 else
3637 {
3638 if (do_lock_var(&lv, name_end, deep,
3639 eap->cmdidx == CMD_lockvar) == FAIL)
3640 error = TRUE;
3641 }
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (!eap->skip)
3645 clear_lval(&lv);
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 arg = skipwhite(name_end);
3648 } while (!ends_excmd(*arg));
3649
3650 eap->nextcmd = check_nextcmd(arg);
3651}
3652
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003655 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 int forceit;
3658{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 int ret = OK;
3660 int cc;
3661
3662 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 cc = *name_end;
3665 *name_end = NUL;
3666
3667 /* Normal name or expanded name. */
3668 if (check_changedtick(lp->ll_name))
3669 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003674 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003676 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003677 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 else if (lp->ll_range)
3680 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003682 listitem_T *ll_li = lp->ll_li;
3683 int ll_n1 = lp->ll_n1;
3684
3685 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3686 {
3687 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003688 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 return FAIL;
3690 ll_li = li;
3691 ++ll_n1;
3692 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693
3694 /* Delete a range of List items. */
3695 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3696 {
3697 li = lp->ll_li->li_next;
3698 listitem_remove(lp->ll_list, lp->ll_li);
3699 lp->ll_li = li;
3700 ++lp->ll_n1;
3701 }
3702 }
3703 else
3704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003705 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 /* unlet a List item. */
3707 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003710 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 }
3712
3713 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714}
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716/*
3717 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003718 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
3720 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003721do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003723 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 hashtab_T *ht;
3726 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003727 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003728 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003729 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 ht = find_var_ht(name, &varname);
3732 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 if (ht == &globvarht)
3735 d = &globvardict;
3736 else if (current_funccal != NULL
3737 && ht == &current_funccal->l_vars.dv_hashtab)
3738 d = &current_funccal->l_vars;
3739 else
3740 {
3741 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3742 d = di->di_tv.vval.v_dict;
3743 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 hi = hash_find(ht, varname);
3745 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003746 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003747 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003748 if (var_check_fixed(di->di_flags, name, FALSE)
3749 || var_check_ro(di->di_flags, name, FALSE)
3750 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003751 return FAIL;
3752 delete_var(ht, hi);
3753 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003756 if (forceit)
3757 return OK;
3758 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 return FAIL;
3760}
3761
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762/*
3763 * Lock or unlock variable indicated by "lp".
3764 * "deep" is the levels to go (-1 for unlimited);
3765 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3766 */
3767 static int
3768do_lock_var(lp, name_end, deep, lock)
3769 lval_T *lp;
3770 char_u *name_end;
3771 int deep;
3772 int lock;
3773{
3774 int ret = OK;
3775 int cc;
3776 dictitem_T *di;
3777
3778 if (deep == 0) /* nothing to do */
3779 return OK;
3780
3781 if (lp->ll_tv == NULL)
3782 {
3783 cc = *name_end;
3784 *name_end = NUL;
3785
3786 /* Normal name or expanded name. */
3787 if (check_changedtick(lp->ll_name))
3788 ret = FAIL;
3789 else
3790 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003791 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003792 if (di == NULL)
3793 ret = FAIL;
3794 else
3795 {
3796 if (lock)
3797 di->di_flags |= DI_FLAGS_LOCK;
3798 else
3799 di->di_flags &= ~DI_FLAGS_LOCK;
3800 item_lock(&di->di_tv, deep, lock);
3801 }
3802 }
3803 *name_end = cc;
3804 }
3805 else if (lp->ll_range)
3806 {
3807 listitem_T *li = lp->ll_li;
3808
3809 /* (un)lock a range of List items. */
3810 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3811 {
3812 item_lock(&li->li_tv, deep, lock);
3813 li = li->li_next;
3814 ++lp->ll_n1;
3815 }
3816 }
3817 else if (lp->ll_list != NULL)
3818 /* (un)lock a List item. */
3819 item_lock(&lp->ll_li->li_tv, deep, lock);
3820 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003821 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003822 item_lock(&lp->ll_di->di_tv, deep, lock);
3823
3824 return ret;
3825}
3826
3827/*
3828 * Lock or unlock an item. "deep" is nr of levels to go.
3829 */
3830 static void
3831item_lock(tv, deep, lock)
3832 typval_T *tv;
3833 int deep;
3834 int lock;
3835{
3836 static int recurse = 0;
3837 list_T *l;
3838 listitem_T *li;
3839 dict_T *d;
3840 hashitem_T *hi;
3841 int todo;
3842
3843 if (recurse >= DICT_MAXNEST)
3844 {
3845 EMSG(_("E743: variable nested too deep for (un)lock"));
3846 return;
3847 }
3848 if (deep == 0)
3849 return;
3850 ++recurse;
3851
3852 /* lock/unlock the item itself */
3853 if (lock)
3854 tv->v_lock |= VAR_LOCKED;
3855 else
3856 tv->v_lock &= ~VAR_LOCKED;
3857
3858 switch (tv->v_type)
3859 {
3860 case VAR_LIST:
3861 if ((l = tv->vval.v_list) != NULL)
3862 {
3863 if (lock)
3864 l->lv_lock |= VAR_LOCKED;
3865 else
3866 l->lv_lock &= ~VAR_LOCKED;
3867 if (deep < 0 || deep > 1)
3868 /* recursive: lock/unlock the items the List contains */
3869 for (li = l->lv_first; li != NULL; li = li->li_next)
3870 item_lock(&li->li_tv, deep - 1, lock);
3871 }
3872 break;
3873 case VAR_DICT:
3874 if ((d = tv->vval.v_dict) != NULL)
3875 {
3876 if (lock)
3877 d->dv_lock |= VAR_LOCKED;
3878 else
3879 d->dv_lock &= ~VAR_LOCKED;
3880 if (deep < 0 || deep > 1)
3881 {
3882 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003883 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003884 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3885 {
3886 if (!HASHITEM_EMPTY(hi))
3887 {
3888 --todo;
3889 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3890 }
3891 }
3892 }
3893 }
3894 }
3895 --recurse;
3896}
3897
Bram Moolenaara40058a2005-07-11 22:42:07 +00003898/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003899 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3900 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003901 */
3902 static int
3903tv_islocked(tv)
3904 typval_T *tv;
3905{
3906 return (tv->v_lock & VAR_LOCKED)
3907 || (tv->v_type == VAR_LIST
3908 && tv->vval.v_list != NULL
3909 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3910 || (tv->v_type == VAR_DICT
3911 && tv->vval.v_dict != NULL
3912 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3913}
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3916/*
3917 * Delete all "menutrans_" variables.
3918 */
3919 void
3920del_menutrans_vars()
3921{
Bram Moolenaar33570922005-01-25 22:26:29 +00003922 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003923 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003926 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 {
3929 if (!HASHITEM_EMPTY(hi))
3930 {
3931 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3933 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003934 }
3935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937}
3938#endif
3939
3940#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3941
3942/*
3943 * Local string buffer for the next two functions to store a variable name
3944 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3945 * get_user_var_name().
3946 */
3947
3948static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3949
3950static char_u *varnamebuf = NULL;
3951static int varnamebuflen = 0;
3952
3953/*
3954 * Function to concatenate a prefix and a variable name.
3955 */
3956 static char_u *
3957cat_prefix_varname(prefix, name)
3958 int prefix;
3959 char_u *name;
3960{
3961 int len;
3962
3963 len = (int)STRLEN(name) + 3;
3964 if (len > varnamebuflen)
3965 {
3966 vim_free(varnamebuf);
3967 len += 10; /* some additional space */
3968 varnamebuf = alloc(len);
3969 if (varnamebuf == NULL)
3970 {
3971 varnamebuflen = 0;
3972 return NULL;
3973 }
3974 varnamebuflen = len;
3975 }
3976 *varnamebuf = prefix;
3977 varnamebuf[1] = ':';
3978 STRCPY(varnamebuf + 2, name);
3979 return varnamebuf;
3980}
3981
3982/*
3983 * Function given to ExpandGeneric() to obtain the list of user defined
3984 * (global/buffer/window/built-in) variable names.
3985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 char_u *
3987get_user_var_name(xp, idx)
3988 expand_T *xp;
3989 int idx;
3990{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003991 static long_u gdone;
3992 static long_u bdone;
3993 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994#ifdef FEAT_WINDOWS
3995 static long_u tdone;
3996#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003997 static int vidx;
3998 static hashitem_T *hi;
3999 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004004#ifdef FEAT_WINDOWS
4005 tdone = 0;
4006#endif
4007 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004008
4009 /* Global variables */
4010 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004014 else
4015 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 while (HASHITEM_EMPTY(hi))
4017 ++hi;
4018 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4019 return cat_prefix_varname('g', hi->hi_key);
4020 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004022
4023 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004024 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004029 else
4030 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 while (HASHITEM_EMPTY(hi))
4032 ++hi;
4033 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 return (char_u *)"b:changedtick";
4039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004040
4041 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004042 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004045 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004046 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004047 else
4048 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004049 while (HASHITEM_EMPTY(hi))
4050 ++hi;
4051 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004053
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004054#ifdef FEAT_WINDOWS
4055 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004056 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004057 if (tdone < ht->ht_used)
4058 {
4059 if (tdone++ == 0)
4060 hi = ht->ht_array;
4061 else
4062 ++hi;
4063 while (HASHITEM_EMPTY(hi))
4064 ++hi;
4065 return cat_prefix_varname('t', hi->hi_key);
4066 }
4067#endif
4068
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 /* v: variables */
4070 if (vidx < VV_LEN)
4071 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 vim_free(varnamebuf);
4074 varnamebuf = NULL;
4075 varnamebuflen = 0;
4076 return NULL;
4077}
4078
4079#endif /* FEAT_CMDL_COMPL */
4080
4081/*
4082 * types for expressions.
4083 */
4084typedef enum
4085{
4086 TYPE_UNKNOWN = 0
4087 , TYPE_EQUAL /* == */
4088 , TYPE_NEQUAL /* != */
4089 , TYPE_GREATER /* > */
4090 , TYPE_GEQUAL /* >= */
4091 , TYPE_SMALLER /* < */
4092 , TYPE_SEQUAL /* <= */
4093 , TYPE_MATCH /* =~ */
4094 , TYPE_NOMATCH /* !~ */
4095} exptype_T;
4096
4097/*
4098 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4101 */
4102
4103/*
4104 * Handle zero level expression.
4105 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004107 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * Return OK or FAIL.
4109 */
4110 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 char_u **nextcmd;
4115 int evaluate;
4116{
4117 int ret;
4118 char_u *p;
4119
4120 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (ret == FAIL || !ends_excmd(*p))
4123 {
4124 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 /*
4127 * Report the invalid expression unless the expression evaluation has
4128 * been cancelled due to an aborting error, an interrupt, or an
4129 * exception.
4130 */
4131 if (!aborting())
4132 EMSG2(_(e_invexpr2), arg);
4133 ret = FAIL;
4134 }
4135 if (nextcmd != NULL)
4136 *nextcmd = check_nextcmd(p);
4137
4138 return ret;
4139}
4140
4141/*
4142 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004143 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 *
4145 * "arg" must point to the first non-white of the expression.
4146 * "arg" is advanced to the next non-white after the recognized expression.
4147 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004148 * Note: "rettv.v_lock" is not set.
4149 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 * Return OK or FAIL.
4151 */
4152 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 int evaluate;
4157{
4158 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 /*
4162 * Get the first variable.
4163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return FAIL;
4166
4167 if ((*arg)[0] == '?')
4168 {
4169 result = FALSE;
4170 if (evaluate)
4171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 int error = FALSE;
4173
4174 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 if (error)
4178 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180
4181 /*
4182 * Get the second variable.
4183 */
4184 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 return FAIL;
4187
4188 /*
4189 * Check for the ":".
4190 */
4191 if ((*arg)[0] != ':')
4192 {
4193 EMSG(_("E109: Missing ':' after '?'"));
4194 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return FAIL;
4197 }
4198
4199 /*
4200 * Get the third variable.
4201 */
4202 *arg = skipwhite(*arg + 1);
4203 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4204 {
4205 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 return FAIL;
4208 }
4209 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
4212
4213 return OK;
4214}
4215
4216/*
4217 * Handle first level expression:
4218 * expr2 || expr2 || expr2 logical OR
4219 *
4220 * "arg" must point to the first non-white of the expression.
4221 * "arg" is advanced to the next non-white after the recognized expression.
4222 *
4223 * Return OK or FAIL.
4224 */
4225 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 int evaluate;
4230{
Bram Moolenaar33570922005-01-25 22:26:29 +00004231 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 long result;
4233 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
4236 /*
4237 * Get the first variable.
4238 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 return FAIL;
4241
4242 /*
4243 * Repeat until there is no following "||".
4244 */
4245 first = TRUE;
4246 result = FALSE;
4247 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4248 {
4249 if (evaluate && first)
4250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004254 if (error)
4255 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 first = FALSE;
4257 }
4258
4259 /*
4260 * Get the second variable.
4261 */
4262 *arg = skipwhite(*arg + 2);
4263 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4264 return FAIL;
4265
4266 /*
4267 * Compute the result.
4268 */
4269 if (evaluate && !result)
4270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 if (error)
4275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 if (evaluate)
4278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 rettv->v_type = VAR_NUMBER;
4280 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 }
4283
4284 return OK;
4285}
4286
4287/*
4288 * Handle second level expression:
4289 * expr3 && expr3 && expr3 logical AND
4290 *
4291 * "arg" must point to the first non-white of the expression.
4292 * "arg" is advanced to the next non-white after the recognized expression.
4293 *
4294 * Return OK or FAIL.
4295 */
4296 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 int evaluate;
4301{
Bram Moolenaar33570922005-01-25 22:26:29 +00004302 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 long result;
4304 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004305 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
4307 /*
4308 * Get the first variable.
4309 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 return FAIL;
4312
4313 /*
4314 * Repeat until there is no following "&&".
4315 */
4316 first = TRUE;
4317 result = TRUE;
4318 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4319 {
4320 if (evaluate && first)
4321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 if (error)
4326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 first = FALSE;
4328 }
4329
4330 /*
4331 * Get the second variable.
4332 */
4333 *arg = skipwhite(*arg + 2);
4334 if (eval4(arg, &var2, evaluate && result) == FAIL)
4335 return FAIL;
4336
4337 /*
4338 * Compute the result.
4339 */
4340 if (evaluate && result)
4341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004344 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345 if (error)
4346 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 if (evaluate)
4349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 rettv->v_type = VAR_NUMBER;
4351 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 }
4354
4355 return OK;
4356}
4357
4358/*
4359 * Handle third level expression:
4360 * var1 == var2
4361 * var1 =~ var2
4362 * var1 != var2
4363 * var1 !~ var2
4364 * var1 > var2
4365 * var1 >= var2
4366 * var1 < var2
4367 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004368 * var1 is var2
4369 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *
4371 * "arg" must point to the first non-white of the expression.
4372 * "arg" is advanced to the next non-white after the recognized expression.
4373 *
4374 * Return OK or FAIL.
4375 */
4376 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 int evaluate;
4381{
Bram Moolenaar33570922005-01-25 22:26:29 +00004382 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 char_u *p;
4384 int i;
4385 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 int len = 2;
4388 long n1, n2;
4389 char_u *s1, *s2;
4390 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4391 regmatch_T regmatch;
4392 int ic;
4393 char_u *save_cpo;
4394
4395 /*
4396 * Get the first variable.
4397 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400
4401 p = *arg;
4402 switch (p[0])
4403 {
4404 case '=': if (p[1] == '=')
4405 type = TYPE_EQUAL;
4406 else if (p[1] == '~')
4407 type = TYPE_MATCH;
4408 break;
4409 case '!': if (p[1] == '=')
4410 type = TYPE_NEQUAL;
4411 else if (p[1] == '~')
4412 type = TYPE_NOMATCH;
4413 break;
4414 case '>': if (p[1] != '=')
4415 {
4416 type = TYPE_GREATER;
4417 len = 1;
4418 }
4419 else
4420 type = TYPE_GEQUAL;
4421 break;
4422 case '<': if (p[1] != '=')
4423 {
4424 type = TYPE_SMALLER;
4425 len = 1;
4426 }
4427 else
4428 type = TYPE_SEQUAL;
4429 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 case 'i': if (p[1] == 's')
4431 {
4432 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4433 len = 5;
4434 if (!vim_isIDc(p[len]))
4435 {
4436 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4437 type_is = TRUE;
4438 }
4439 }
4440 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442
4443 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004444 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 */
4446 if (type != TYPE_UNKNOWN)
4447 {
4448 /* extra question mark appended: ignore case */
4449 if (p[len] == '?')
4450 {
4451 ic = TRUE;
4452 ++len;
4453 }
4454 /* extra '#' appended: match case */
4455 else if (p[len] == '#')
4456 {
4457 ic = FALSE;
4458 ++len;
4459 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004460 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 else
4462 ic = p_ic;
4463
4464 /*
4465 * Get the second variable.
4466 */
4467 *arg = skipwhite(p + len);
4468 if (eval5(arg, &var2, evaluate) == FAIL)
4469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 return FAIL;
4472 }
4473
4474 if (evaluate)
4475 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004476 if (type_is && rettv->v_type != var2.v_type)
4477 {
4478 /* For "is" a different type always means FALSE, for "notis"
4479 * it means TRUE. */
4480 n1 = (type == TYPE_NEQUAL);
4481 }
4482 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4483 {
4484 if (type_is)
4485 {
4486 n1 = (rettv->v_type == var2.v_type
4487 && rettv->vval.v_list == var2.vval.v_list);
4488 if (type == TYPE_NEQUAL)
4489 n1 = !n1;
4490 }
4491 else if (rettv->v_type != var2.v_type
4492 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4493 {
4494 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004495 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004496 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004497 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004498 clear_tv(rettv);
4499 clear_tv(&var2);
4500 return FAIL;
4501 }
4502 else
4503 {
4504 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004505 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4506 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004507 if (type == TYPE_NEQUAL)
4508 n1 = !n1;
4509 }
4510 }
4511
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004512 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4513 {
4514 if (type_is)
4515 {
4516 n1 = (rettv->v_type == var2.v_type
4517 && rettv->vval.v_dict == var2.vval.v_dict);
4518 if (type == TYPE_NEQUAL)
4519 n1 = !n1;
4520 }
4521 else if (rettv->v_type != var2.v_type
4522 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4523 {
4524 if (rettv->v_type != var2.v_type)
4525 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4526 else
4527 EMSG(_("E736: Invalid operation for Dictionary"));
4528 clear_tv(rettv);
4529 clear_tv(&var2);
4530 return FAIL;
4531 }
4532 else
4533 {
4534 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004535 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4536 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004537 if (type == TYPE_NEQUAL)
4538 n1 = !n1;
4539 }
4540 }
4541
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004542 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4543 {
4544 if (rettv->v_type != var2.v_type
4545 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4546 {
4547 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004548 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004549 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004550 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004551 clear_tv(rettv);
4552 clear_tv(&var2);
4553 return FAIL;
4554 }
4555 else
4556 {
4557 /* Compare two Funcrefs for being equal or unequal. */
4558 if (rettv->vval.v_string == NULL
4559 || var2.vval.v_string == NULL)
4560 n1 = FALSE;
4561 else
4562 n1 = STRCMP(rettv->vval.v_string,
4563 var2.vval.v_string) == 0;
4564 if (type == TYPE_NEQUAL)
4565 n1 = !n1;
4566 }
4567 }
4568
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004569#ifdef FEAT_FLOAT
4570 /*
4571 * If one of the two variables is a float, compare as a float.
4572 * When using "=~" or "!~", always compare as string.
4573 */
4574 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4575 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4576 {
4577 float_T f1, f2;
4578
4579 if (rettv->v_type == VAR_FLOAT)
4580 f1 = rettv->vval.v_float;
4581 else
4582 f1 = get_tv_number(rettv);
4583 if (var2.v_type == VAR_FLOAT)
4584 f2 = var2.vval.v_float;
4585 else
4586 f2 = get_tv_number(&var2);
4587 n1 = FALSE;
4588 switch (type)
4589 {
4590 case TYPE_EQUAL: n1 = (f1 == f2); break;
4591 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4592 case TYPE_GREATER: n1 = (f1 > f2); break;
4593 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4594 case TYPE_SMALLER: n1 = (f1 < f2); break;
4595 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4596 case TYPE_UNKNOWN:
4597 case TYPE_MATCH:
4598 case TYPE_NOMATCH: break; /* avoid gcc warning */
4599 }
4600 }
4601#endif
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 /*
4604 * If one of the two variables is a number, compare as a number.
4605 * When using "=~" or "!~", always compare as string.
4606 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004607 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004610 n1 = get_tv_number(rettv);
4611 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 switch (type)
4613 {
4614 case TYPE_EQUAL: n1 = (n1 == n2); break;
4615 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4616 case TYPE_GREATER: n1 = (n1 > n2); break;
4617 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4618 case TYPE_SMALLER: n1 = (n1 < n2); break;
4619 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4620 case TYPE_UNKNOWN:
4621 case TYPE_MATCH:
4622 case TYPE_NOMATCH: break; /* avoid gcc warning */
4623 }
4624 }
4625 else
4626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627 s1 = get_tv_string_buf(rettv, buf1);
4628 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4630 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4631 else
4632 i = 0;
4633 n1 = FALSE;
4634 switch (type)
4635 {
4636 case TYPE_EQUAL: n1 = (i == 0); break;
4637 case TYPE_NEQUAL: n1 = (i != 0); break;
4638 case TYPE_GREATER: n1 = (i > 0); break;
4639 case TYPE_GEQUAL: n1 = (i >= 0); break;
4640 case TYPE_SMALLER: n1 = (i < 0); break;
4641 case TYPE_SEQUAL: n1 = (i <= 0); break;
4642
4643 case TYPE_MATCH:
4644 case TYPE_NOMATCH:
4645 /* avoid 'l' flag in 'cpoptions' */
4646 save_cpo = p_cpo;
4647 p_cpo = (char_u *)"";
4648 regmatch.regprog = vim_regcomp(s2,
4649 RE_MAGIC + RE_STRING);
4650 regmatch.rm_ic = ic;
4651 if (regmatch.regprog != NULL)
4652 {
4653 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004654 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (type == TYPE_NOMATCH)
4656 n1 = !n1;
4657 }
4658 p_cpo = save_cpo;
4659 break;
4660
4661 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4662 }
4663 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004664 clear_tv(rettv);
4665 clear_tv(&var2);
4666 rettv->v_type = VAR_NUMBER;
4667 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669 }
4670
4671 return OK;
4672}
4673
4674/*
4675 * Handle fourth level expression:
4676 * + number addition
4677 * - number subtraction
4678 * . string concatenation
4679 *
4680 * "arg" must point to the first non-white of the expression.
4681 * "arg" is advanced to the next non-white after the recognized expression.
4682 *
4683 * Return OK or FAIL.
4684 */
4685 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004686eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 int evaluate;
4690{
Bram Moolenaar33570922005-01-25 22:26:29 +00004691 typval_T var2;
4692 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 int op;
4694 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004695#ifdef FEAT_FLOAT
4696 float_T f1 = 0, f2 = 0;
4697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 char_u *s1, *s2;
4699 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4700 char_u *p;
4701
4702 /*
4703 * Get the first variable.
4704 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004705 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 return FAIL;
4707
4708 /*
4709 * Repeat computing, until no '+', '-' or '.' is following.
4710 */
4711 for (;;)
4712 {
4713 op = **arg;
4714 if (op != '+' && op != '-' && op != '.')
4715 break;
4716
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004717 if ((op != '+' || rettv->v_type != VAR_LIST)
4718#ifdef FEAT_FLOAT
4719 && (op == '.' || rettv->v_type != VAR_FLOAT)
4720#endif
4721 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004722 {
4723 /* For "list + ...", an illegal use of the first operand as
4724 * a number cannot be determined before evaluating the 2nd
4725 * operand: if this is also a list, all is ok.
4726 * For "something . ...", "something - ..." or "non-list + ...",
4727 * we know that the first operand needs to be a string or number
4728 * without evaluating the 2nd operand. So check before to avoid
4729 * side effects after an error. */
4730 if (evaluate && get_tv_string_chk(rettv) == NULL)
4731 {
4732 clear_tv(rettv);
4733 return FAIL;
4734 }
4735 }
4736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 /*
4738 * Get the second variable.
4739 */
4740 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004741 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 return FAIL;
4745 }
4746
4747 if (evaluate)
4748 {
4749 /*
4750 * Compute the result.
4751 */
4752 if (op == '.')
4753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4755 s2 = get_tv_string_buf_chk(&var2, buf2);
4756 if (s2 == NULL) /* type error ? */
4757 {
4758 clear_tv(rettv);
4759 clear_tv(&var2);
4760 return FAIL;
4761 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004762 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 clear_tv(rettv);
4764 rettv->v_type = VAR_STRING;
4765 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004767 else if (op == '+' && rettv->v_type == VAR_LIST
4768 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004769 {
4770 /* concatenate Lists */
4771 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4772 &var3) == FAIL)
4773 {
4774 clear_tv(rettv);
4775 clear_tv(&var2);
4776 return FAIL;
4777 }
4778 clear_tv(rettv);
4779 *rettv = var3;
4780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 else
4782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004783 int error = FALSE;
4784
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004785#ifdef FEAT_FLOAT
4786 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788 f1 = rettv->vval.v_float;
4789 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004790 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791 else
4792#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794 n1 = get_tv_number_chk(rettv, &error);
4795 if (error)
4796 {
4797 /* This can only happen for "list + non-list". For
4798 * "non-list + ..." or "something - ...", we returned
4799 * before evaluating the 2nd operand. */
4800 clear_tv(rettv);
4801 return FAIL;
4802 }
4803#ifdef FEAT_FLOAT
4804 if (var2.v_type == VAR_FLOAT)
4805 f1 = n1;
4806#endif
4807 }
4808#ifdef FEAT_FLOAT
4809 if (var2.v_type == VAR_FLOAT)
4810 {
4811 f2 = var2.vval.v_float;
4812 n2 = 0;
4813 }
4814 else
4815#endif
4816 {
4817 n2 = get_tv_number_chk(&var2, &error);
4818 if (error)
4819 {
4820 clear_tv(rettv);
4821 clear_tv(&var2);
4822 return FAIL;
4823 }
4824#ifdef FEAT_FLOAT
4825 if (rettv->v_type == VAR_FLOAT)
4826 f2 = n2;
4827#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830
4831#ifdef FEAT_FLOAT
4832 /* If there is a float on either side the result is a float. */
4833 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4834 {
4835 if (op == '+')
4836 f1 = f1 + f2;
4837 else
4838 f1 = f1 - f2;
4839 rettv->v_type = VAR_FLOAT;
4840 rettv->vval.v_float = f1;
4841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004843#endif
4844 {
4845 if (op == '+')
4846 n1 = n1 + n2;
4847 else
4848 n1 = n1 - n2;
4849 rettv->v_type = VAR_NUMBER;
4850 rettv->vval.v_number = n1;
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004853 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 }
4856 return OK;
4857}
4858
4859/*
4860 * Handle fifth level expression:
4861 * * number multiplication
4862 * / number division
4863 * % number modulo
4864 *
4865 * "arg" must point to the first non-white of the expression.
4866 * "arg" is advanced to the next non-white after the recognized expression.
4867 *
4868 * Return OK or FAIL.
4869 */
4870 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004871eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004875 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876{
Bram Moolenaar33570922005-01-25 22:26:29 +00004877 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 int op;
4879 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880#ifdef FEAT_FLOAT
4881 int use_float = FALSE;
4882 float_T f1 = 0, f2;
4883#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004884 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885
4886 /*
4887 * Get the first variable.
4888 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004889 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 return FAIL;
4891
4892 /*
4893 * Repeat computing, until no '*', '/' or '%' is following.
4894 */
4895 for (;;)
4896 {
4897 op = **arg;
4898 if (op != '*' && op != '/' && op != '%')
4899 break;
4900
4901 if (evaluate)
4902 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903#ifdef FEAT_FLOAT
4904 if (rettv->v_type == VAR_FLOAT)
4905 {
4906 f1 = rettv->vval.v_float;
4907 use_float = TRUE;
4908 n1 = 0;
4909 }
4910 else
4911#endif
4912 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004914 if (error)
4915 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 else
4918 n1 = 0;
4919
4920 /*
4921 * Get the second variable.
4922 */
4923 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004924 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return FAIL;
4926
4927 if (evaluate)
4928 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#ifdef FEAT_FLOAT
4930 if (var2.v_type == VAR_FLOAT)
4931 {
4932 if (!use_float)
4933 {
4934 f1 = n1;
4935 use_float = TRUE;
4936 }
4937 f2 = var2.vval.v_float;
4938 n2 = 0;
4939 }
4940 else
4941#endif
4942 {
4943 n2 = get_tv_number_chk(&var2, &error);
4944 clear_tv(&var2);
4945 if (error)
4946 return FAIL;
4947#ifdef FEAT_FLOAT
4948 if (use_float)
4949 f2 = n2;
4950#endif
4951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
4953 /*
4954 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004957#ifdef FEAT_FLOAT
4958 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004960 if (op == '*')
4961 f1 = f1 * f2;
4962 else if (op == '/')
4963 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004964# ifdef VMS
4965 /* VMS crashes on divide by zero, work around it */
4966 if (f2 == 0.0)
4967 {
4968 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004973 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004974 }
4975 else
4976 f1 = f1 / f2;
4977# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 /* We rely on the floating point library to handle divide
4979 * by zero to result in "inf" and not a crash. */
4980 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004985 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 return FAIL;
4987 }
4988 rettv->v_type = VAR_FLOAT;
4989 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994 if (op == '*')
4995 n1 = n1 * n2;
4996 else if (op == '/')
4997 {
4998 if (n2 == 0) /* give an error message? */
4999 {
5000 if (n1 == 0)
5001 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5002 else if (n1 < 0)
5003 n1 = -0x7fffffffL;
5004 else
5005 n1 = 0x7fffffffL;
5006 }
5007 else
5008 n1 = n1 / n2;
5009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 {
5012 if (n2 == 0) /* give an error message? */
5013 n1 = 0;
5014 else
5015 n1 = n1 % n2;
5016 }
5017 rettv->v_type = VAR_NUMBER;
5018 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021 }
5022
5023 return OK;
5024}
5025
5026/*
5027 * Handle sixth level expression:
5028 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005029 * "string" string constant
5030 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 * &option-name option value
5032 * @r register contents
5033 * identifier variable value
5034 * function() function call
5035 * $VAR environment variable
5036 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005037 * [expr, expr] List
5038 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 *
5040 * Also handle:
5041 * ! in front logical NOT
5042 * - in front unary minus
5043 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 * trailing [] subscript in String or List
5045 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 *
5047 * "arg" must point to the first non-white of the expression.
5048 * "arg" is advanced to the next non-white after the recognized expression.
5049 *
5050 * Return OK or FAIL.
5051 */
5052 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005053eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005057 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 long n;
5060 int len;
5061 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 char_u *start_leader, *end_leader;
5063 int ret = OK;
5064 char_u *alias;
5065
5066 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005068 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
5072 /*
5073 * Skip '!' and '-' characters. They are handled later.
5074 */
5075 start_leader = *arg;
5076 while (**arg == '!' || **arg == '-' || **arg == '+')
5077 *arg = skipwhite(*arg + 1);
5078 end_leader = *arg;
5079
5080 switch (**arg)
5081 {
5082 /*
5083 * Number constant.
5084 */
5085 case '0':
5086 case '1':
5087 case '2':
5088 case '3':
5089 case '4':
5090 case '5':
5091 case '6':
5092 case '7':
5093 case '8':
5094 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005095 {
5096#ifdef FEAT_FLOAT
5097 char_u *p = skipdigits(*arg + 1);
5098 int get_float = FALSE;
5099
5100 /* We accept a float when the format matches
5101 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005102 * strict to avoid backwards compatibility problems.
5103 * Don't look for a float after the "." operator, so that
5104 * ":let vers = 1.2.3" doesn't fail. */
5105 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005107 get_float = TRUE;
5108 p = skipdigits(p + 2);
5109 if (*p == 'e' || *p == 'E')
5110 {
5111 ++p;
5112 if (*p == '-' || *p == '+')
5113 ++p;
5114 if (!vim_isdigit(*p))
5115 get_float = FALSE;
5116 else
5117 p = skipdigits(p + 1);
5118 }
5119 if (ASCII_ISALPHA(*p) || *p == '.')
5120 get_float = FALSE;
5121 }
5122 if (get_float)
5123 {
5124 float_T f;
5125
5126 *arg += string2float(*arg, &f);
5127 if (evaluate)
5128 {
5129 rettv->v_type = VAR_FLOAT;
5130 rettv->vval.v_float = f;
5131 }
5132 }
5133 else
5134#endif
5135 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005136 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005137 *arg += len;
5138 if (evaluate)
5139 {
5140 rettv->v_type = VAR_NUMBER;
5141 rettv->vval.v_number = n;
5142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
5147 /*
5148 * String constant: "string".
5149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 break;
5152
5153 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 break;
5158
5159 /*
5160 * List: [expr, expr]
5161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
5164
5165 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 * Dictionary: {key: val, key: val}
5167 */
5168 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5169 break;
5170
5171 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 break;
5176
5177 /*
5178 * Environment variable: $VAR.
5179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182
5183 /*
5184 * Register contents: @r.
5185 */
5186 case '@': ++*arg;
5187 if (evaluate)
5188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005190 rettv->vval.v_string = get_reg_contents(**arg,
5191 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193 if (**arg != NUL)
5194 ++*arg;
5195 break;
5196
5197 /*
5198 * nested expression: (expression).
5199 */
5200 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if (**arg == ')')
5203 ++*arg;
5204 else if (ret == OK)
5205 {
5206 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 ret = FAIL;
5209 }
5210 break;
5211
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 break;
5214 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215
5216 if (ret == NOTDONE)
5217 {
5218 /*
5219 * Must be a variable or function name.
5220 * Can also be a curly-braces kind of name: {expr}.
5221 */
5222 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005223 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 if (alias != NULL)
5225 s = alias;
5226
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005227 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 ret = FAIL;
5229 else
5230 {
5231 if (**arg == '(') /* recursive! */
5232 {
5233 /* If "s" is the name of a variable of type VAR_FUNC
5234 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005235 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236
5237 /* Invoke the function. */
5238 ret = get_func_tv(s, len, rettv, arg,
5239 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005240 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005241
5242 /* If evaluate is FALSE rettv->v_type was not set in
5243 * get_func_tv, but it's needed in handle_subscript() to parse
5244 * what follows. So set it here. */
5245 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5246 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005247 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005248 rettv->v_type = VAR_FUNC;
5249 }
5250
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 /* Stop the expression evaluation when immediately
5252 * aborting on error, or when an interrupt occurred or
5253 * an exception was thrown but not caught. */
5254 if (aborting())
5255 {
5256 if (ret == OK)
5257 clear_tv(rettv);
5258 ret = FAIL;
5259 }
5260 }
5261 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005262 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005263 else
5264 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005266 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 }
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 *arg = skipwhite(*arg);
5270
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005271 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5272 * expr(expr). */
5273 if (ret == OK)
5274 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
5276 /*
5277 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5278 */
5279 if (ret == OK && evaluate && end_leader > start_leader)
5280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005282 int val = 0;
5283#ifdef FEAT_FLOAT
5284 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005286 if (rettv->v_type == VAR_FLOAT)
5287 f = rettv->vval.v_float;
5288 else
5289#endif
5290 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005293 clear_tv(rettv);
5294 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296 else
5297 {
5298 while (end_leader > start_leader)
5299 {
5300 --end_leader;
5301 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005302 {
5303#ifdef FEAT_FLOAT
5304 if (rettv->v_type == VAR_FLOAT)
5305 f = !f;
5306 else
5307#endif
5308 val = !val;
5309 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005311 {
5312#ifdef FEAT_FLOAT
5313 if (rettv->v_type == VAR_FLOAT)
5314 f = -f;
5315 else
5316#endif
5317 val = -val;
5318 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005320#ifdef FEAT_FLOAT
5321 if (rettv->v_type == VAR_FLOAT)
5322 {
5323 clear_tv(rettv);
5324 rettv->vval.v_float = f;
5325 }
5326 else
5327#endif
5328 {
5329 clear_tv(rettv);
5330 rettv->v_type = VAR_NUMBER;
5331 rettv->vval.v_number = val;
5332 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335
5336 return ret;
5337}
5338
5339/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005340 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5341 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5343 */
5344 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005345eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005347 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005349 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350{
5351 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005354 long len = -1;
5355 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005359 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005361 if (verbose)
5362 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 return FAIL;
5364 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005365#ifdef FEAT_FLOAT
5366 else if (rettv->v_type == VAR_FLOAT)
5367 {
5368 if (verbose)
5369 EMSG(_(e_float_as_string));
5370 return FAIL;
5371 }
5372#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376 /*
5377 * dict.name
5378 */
5379 key = *arg + 1;
5380 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5381 ;
5382 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005384 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 }
5386 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005388 /*
5389 * something[idx]
5390 *
5391 * Get the (first) variable from inside the [].
5392 */
5393 *arg = skipwhite(*arg + 1);
5394 if (**arg == ':')
5395 empty1 = TRUE;
5396 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5397 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005398 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5399 {
5400 /* not a number or string */
5401 clear_tv(&var1);
5402 return FAIL;
5403 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005404
5405 /*
5406 * Get the second variable from inside the [:].
5407 */
5408 if (**arg == ':')
5409 {
5410 range = TRUE;
5411 *arg = skipwhite(*arg + 1);
5412 if (**arg == ']')
5413 empty2 = TRUE;
5414 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005416 if (!empty1)
5417 clear_tv(&var1);
5418 return FAIL;
5419 }
5420 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5421 {
5422 /* not a number or string */
5423 if (!empty1)
5424 clear_tv(&var1);
5425 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005426 return FAIL;
5427 }
5428 }
5429
5430 /* Check for the ']'. */
5431 if (**arg != ']')
5432 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005433 if (verbose)
5434 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005435 clear_tv(&var1);
5436 if (range)
5437 clear_tv(&var2);
5438 return FAIL;
5439 }
5440 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 }
5442
5443 if (evaluate)
5444 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 n1 = 0;
5446 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 n1 = get_tv_number(&var1);
5449 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 }
5451 if (range)
5452 {
5453 if (empty2)
5454 n2 = -1;
5455 else
5456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 n2 = get_tv_number(&var2);
5458 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460 }
5461
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005462 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 {
5464 case VAR_NUMBER:
5465 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 len = (long)STRLEN(s);
5468 if (range)
5469 {
5470 /* The resulting variable is a substring. If the indexes
5471 * are out of range the result is empty. */
5472 if (n1 < 0)
5473 {
5474 n1 = len + n1;
5475 if (n1 < 0)
5476 n1 = 0;
5477 }
5478 if (n2 < 0)
5479 n2 = len + n2;
5480 else if (n2 >= len)
5481 n2 = len;
5482 if (n1 >= len || n2 < 0 || n1 > n2)
5483 s = NULL;
5484 else
5485 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5486 }
5487 else
5488 {
5489 /* The resulting variable is a string of a single
5490 * character. If the index is too big or negative the
5491 * result is empty. */
5492 if (n1 >= len || n1 < 0)
5493 s = NULL;
5494 else
5495 s = vim_strnsave(s + n1, 1);
5496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497 clear_tv(rettv);
5498 rettv->v_type = VAR_STRING;
5499 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 break;
5501
5502 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 if (n1 < 0)
5505 n1 = len + n1;
5506 if (!empty1 && (n1 < 0 || n1 >= len))
5507 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005508 /* For a range we allow invalid values and return an empty
5509 * list. A list index out of range is an error. */
5510 if (!range)
5511 {
5512 if (verbose)
5513 EMSGN(_(e_listidx), n1);
5514 return FAIL;
5515 }
5516 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 }
5518 if (range)
5519 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005520 list_T *l;
5521 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522
5523 if (n2 < 0)
5524 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005525 else if (n2 >= len)
5526 n2 = len - 1;
5527 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005528 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 l = list_alloc();
5530 if (l == NULL)
5531 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 n1 <= n2; ++n1)
5534 {
5535 if (list_append_tv(l, &item->li_tv) == FAIL)
5536 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005537 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 return FAIL;
5539 }
5540 item = item->li_next;
5541 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 clear_tv(rettv);
5543 rettv->v_type = VAR_LIST;
5544 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005545 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 }
5547 else
5548 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005549 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 clear_tv(rettv);
5551 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 }
5553 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005554
5555 case VAR_DICT:
5556 if (range)
5557 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005558 if (verbose)
5559 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005560 if (len == -1)
5561 clear_tv(&var1);
5562 return FAIL;
5563 }
5564 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005565 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566
5567 if (len == -1)
5568 {
5569 key = get_tv_string(&var1);
5570 if (*key == NUL)
5571 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005572 if (verbose)
5573 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574 clear_tv(&var1);
5575 return FAIL;
5576 }
5577 }
5578
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005579 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005581 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005582 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 if (len == -1)
5584 clear_tv(&var1);
5585 if (item == NULL)
5586 return FAIL;
5587
5588 copy_tv(&item->di_tv, &var1);
5589 clear_tv(rettv);
5590 *rettv = var1;
5591 }
5592 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 }
5594 }
5595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 return OK;
5597}
5598
5599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 * Get an option value.
5601 * "arg" points to the '&' or '+' before the option name.
5602 * "arg" is advanced to character after the option name.
5603 * Return OK or FAIL.
5604 */
5605 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005606get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005608 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 int evaluate;
5610{
5611 char_u *option_end;
5612 long numval;
5613 char_u *stringval;
5614 int opt_type;
5615 int c;
5616 int working = (**arg == '+'); /* has("+option") */
5617 int ret = OK;
5618 int opt_flags;
5619
5620 /*
5621 * Isolate the option name and find its value.
5622 */
5623 option_end = find_option_end(arg, &opt_flags);
5624 if (option_end == NULL)
5625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005626 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 EMSG2(_("E112: Option name missing: %s"), *arg);
5628 return FAIL;
5629 }
5630
5631 if (!evaluate)
5632 {
5633 *arg = option_end;
5634 return OK;
5635 }
5636
5637 c = *option_end;
5638 *option_end = NUL;
5639 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005640 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
5642 if (opt_type == -3) /* invalid name */
5643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 EMSG2(_("E113: Unknown option: %s"), *arg);
5646 ret = FAIL;
5647 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
5650 if (opt_type == -2) /* hidden string option */
5651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 rettv->v_type = VAR_STRING;
5653 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
5655 else if (opt_type == -1) /* hidden number option */
5656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 rettv->v_type = VAR_NUMBER;
5658 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660 else if (opt_type == 1) /* number option */
5661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005662 rettv->v_type = VAR_NUMBER;
5663 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
5665 else /* string option */
5666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 rettv->v_type = VAR_STRING;
5668 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670 }
5671 else if (working && (opt_type == -2 || opt_type == -1))
5672 ret = FAIL;
5673
5674 *option_end = c; /* put back for error messages */
5675 *arg = option_end;
5676
5677 return ret;
5678}
5679
5680/*
5681 * Allocate a variable for a string constant.
5682 * Return OK or FAIL.
5683 */
5684 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005685get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 int evaluate;
5689{
5690 char_u *p;
5691 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 int extra = 0;
5693
5694 /*
5695 * Find the end of the string, skipping backslashed characters.
5696 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005697 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 {
5699 if (*p == '\\' && p[1] != NUL)
5700 {
5701 ++p;
5702 /* A "\<x>" form occupies at least 4 characters, and produces up
5703 * to 6 characters: reserve space for 2 extra */
5704 if (*p == '<')
5705 extra += 2;
5706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708
5709 if (*p != '"')
5710 {
5711 EMSG2(_("E114: Missing quote: %s"), *arg);
5712 return FAIL;
5713 }
5714
5715 /* If only parsing, set *arg and return here */
5716 if (!evaluate)
5717 {
5718 *arg = p + 1;
5719 return OK;
5720 }
5721
5722 /*
5723 * Copy the string into allocated memory, handling backslashed
5724 * characters.
5725 */
5726 name = alloc((unsigned)(p - *arg + extra));
5727 if (name == NULL)
5728 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 rettv->v_type = VAR_STRING;
5730 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 {
5734 if (*p == '\\')
5735 {
5736 switch (*++p)
5737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 case 'b': *name++ = BS; ++p; break;
5739 case 'e': *name++ = ESC; ++p; break;
5740 case 'f': *name++ = FF; ++p; break;
5741 case 'n': *name++ = NL; ++p; break;
5742 case 'r': *name++ = CAR; ++p; break;
5743 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
5745 case 'X': /* hex: "\x1", "\x12" */
5746 case 'x':
5747 case 'u': /* Unicode: "\u0023" */
5748 case 'U':
5749 if (vim_isxdigit(p[1]))
5750 {
5751 int n, nr;
5752 int c = toupper(*p);
5753
5754 if (c == 'X')
5755 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005756 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005758 else
5759 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 nr = 0;
5761 while (--n >= 0 && vim_isxdigit(p[1]))
5762 {
5763 ++p;
5764 nr = (nr << 4) + hex2nr(*p);
5765 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767#ifdef FEAT_MBYTE
5768 /* For "\u" store the number according to
5769 * 'encoding'. */
5770 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 else
5773#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 break;
5777
5778 /* octal: "\1", "\12", "\123" */
5779 case '0':
5780 case '1':
5781 case '2':
5782 case '3':
5783 case '4':
5784 case '5':
5785 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 case '7': *name = *p++ - '0';
5787 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 *name = (*name << 3) + *p++ - '0';
5790 if (*p >= '0' && *p <= '7')
5791 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 break;
5795
5796 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 if (extra != 0)
5799 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 break;
5802 }
5803 /* FALLTHROUGH */
5804
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 break;
5807 }
5808 }
5809 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 *arg = p + 1;
5815
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 return OK;
5817}
5818
5819/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 * Return OK or FAIL.
5822 */
5823 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005824get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 int evaluate;
5828{
5829 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005830 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005831 int reduce = 0;
5832
5833 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 */
5836 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 break;
5842 ++reduce;
5843 ++p;
5844 }
5845 }
5846
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 return FAIL;
5851 }
5852
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 if (!evaluate)
5855 {
5856 *arg = p + 1;
5857 return OK;
5858 }
5859
5860 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 */
5863 str = alloc((unsigned)((p - *arg) - reduce));
5864 if (str == NULL)
5865 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 rettv->v_type = VAR_STRING;
5867 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 break;
5875 ++p;
5876 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 *arg = p + 1;
5881
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 return OK;
5883}
5884
5885/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 * Allocate a variable for a List and fill it from "*arg".
5887 * Return OK or FAIL.
5888 */
5889 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005890get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005892 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893 int evaluate;
5894{
Bram Moolenaar33570922005-01-25 22:26:29 +00005895 list_T *l = NULL;
5896 typval_T tv;
5897 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898
5899 if (evaluate)
5900 {
5901 l = list_alloc();
5902 if (l == NULL)
5903 return FAIL;
5904 }
5905
5906 *arg = skipwhite(*arg + 1);
5907 while (**arg != ']' && **arg != NUL)
5908 {
5909 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5910 goto failret;
5911 if (evaluate)
5912 {
5913 item = listitem_alloc();
5914 if (item != NULL)
5915 {
5916 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005917 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 list_append(l, item);
5919 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005920 else
5921 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 }
5923
5924 if (**arg == ']')
5925 break;
5926 if (**arg != ',')
5927 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 goto failret;
5930 }
5931 *arg = skipwhite(*arg + 1);
5932 }
5933
5934 if (**arg != ']')
5935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937failret:
5938 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005939 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 return FAIL;
5941 }
5942
5943 *arg = skipwhite(*arg + 1);
5944 if (evaluate)
5945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005946 rettv->v_type = VAR_LIST;
5947 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 ++l->lv_refcount;
5949 }
5950
5951 return OK;
5952}
5953
5954/*
5955 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005956 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005958 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959list_alloc()
5960{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005961 list_T *l;
5962
5963 l = (list_T *)alloc_clear(sizeof(list_T));
5964 if (l != NULL)
5965 {
5966 /* Prepend the list to the list of lists for garbage collection. */
5967 if (first_list != NULL)
5968 first_list->lv_used_prev = l;
5969 l->lv_used_prev = NULL;
5970 l->lv_used_next = first_list;
5971 first_list = l;
5972 }
5973 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974}
5975
5976/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005977 * Allocate an empty list for a return value.
5978 * Returns OK or FAIL.
5979 */
5980 static int
5981rettv_list_alloc(rettv)
5982 typval_T *rettv;
5983{
5984 list_T *l = list_alloc();
5985
5986 if (l == NULL)
5987 return FAIL;
5988
5989 rettv->vval.v_list = l;
5990 rettv->v_type = VAR_LIST;
5991 ++l->lv_refcount;
5992 return OK;
5993}
5994
5995/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 * Unreference a list: decrement the reference count and free it when it
5997 * becomes zero.
5998 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005999 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006003 if (l != NULL && --l->lv_refcount <= 0)
6004 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005}
6006
6007/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006008 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 * Ignores the reference count.
6010 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006011 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006012list_free(l, recurse)
6013 list_T *l;
6014 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015{
Bram Moolenaar33570922005-01-25 22:26:29 +00006016 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006018 /* Remove the list from the list of lists for garbage collection. */
6019 if (l->lv_used_prev == NULL)
6020 first_list = l->lv_used_next;
6021 else
6022 l->lv_used_prev->lv_used_next = l->lv_used_next;
6023 if (l->lv_used_next != NULL)
6024 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6025
Bram Moolenaard9fba312005-06-26 22:34:35 +00006026 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006028 /* Remove the item before deleting it. */
6029 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006030 if (recurse || (item->li_tv.v_type != VAR_LIST
6031 && item->li_tv.v_type != VAR_DICT))
6032 clear_tv(&item->li_tv);
6033 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 }
6035 vim_free(l);
6036}
6037
6038/*
6039 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006040 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006042 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043listitem_alloc()
6044{
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046}
6047
6048/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006049 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006051 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006055 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 vim_free(item);
6057}
6058
6059/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006060 * Remove a list item from a List and free it. Also clears the value.
6061 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006062 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006063listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 list_T *l;
6065 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006066{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006067 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006068 listitem_free(item);
6069}
6070
6071/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 * Get the number of items in a list.
6073 */
6074 static long
6075list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078 if (l == NULL)
6079 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006080 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081}
6082
6083/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006084 * Return TRUE when two lists have exactly the same values.
6085 */
6086 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006087list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 list_T *l1;
6089 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006091 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092{
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006094
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006095 if (l1 == NULL || l2 == NULL)
6096 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006097 if (l1 == l2)
6098 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006099 if (list_len(l1) != list_len(l2))
6100 return FALSE;
6101
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 for (item1 = l1->lv_first, item2 = l2->lv_first;
6103 item1 != NULL && item2 != NULL;
6104 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106 return FALSE;
6107 return item1 == NULL && item2 == NULL;
6108}
6109
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006110#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6111 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006112/*
6113 * Return the dictitem that an entry in a hashtable points to.
6114 */
6115 dictitem_T *
6116dict_lookup(hi)
6117 hashitem_T *hi;
6118{
6119 return HI2DI(hi);
6120}
6121#endif
6122
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006124 * Return TRUE when two dictionaries have exactly the same key/values.
6125 */
6126 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006127dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006128 dict_T *d1;
6129 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006130 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006131 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132{
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 hashitem_T *hi;
6134 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006135 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006137 if (d1 == NULL || d2 == NULL)
6138 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006139 if (d1 == d2)
6140 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141 if (dict_len(d1) != dict_len(d2))
6142 return FALSE;
6143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006144 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006147 if (!HASHITEM_EMPTY(hi))
6148 {
6149 item2 = dict_find(d2, hi->hi_key, -1);
6150 if (item2 == NULL)
6151 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006152 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006153 return FALSE;
6154 --todo;
6155 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006156 }
6157 return TRUE;
6158}
6159
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160static int tv_equal_recurse_limit;
6161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006162/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006163 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006164 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006165 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166 */
6167 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006169 typval_T *tv1;
6170 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171 int ic; /* ignore case */
6172 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173{
6174 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006175 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006176 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006177 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006179 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006180 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006182 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 * recursiveness to a limit. We guess they are equal then.
6184 * A fixed limit has the problem of still taking an awful long time.
6185 * Reduce the limit every time running into it. That should work fine for
6186 * deeply linked structures that are not recursively linked and catch
6187 * recursiveness quickly. */
6188 if (!recursive)
6189 tv_equal_recurse_limit = 1000;
6190 if (recursive_cnt >= tv_equal_recurse_limit)
6191 {
6192 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006193 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006195
6196 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006199 ++recursive_cnt;
6200 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6201 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006202 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203
6204 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 ++recursive_cnt;
6206 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6207 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006208 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006209
6210 case VAR_FUNC:
6211 return (tv1->vval.v_string != NULL
6212 && tv2->vval.v_string != NULL
6213 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6214
6215 case VAR_NUMBER:
6216 return tv1->vval.v_number == tv2->vval.v_number;
6217
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006218#ifdef FEAT_FLOAT
6219 case VAR_FLOAT:
6220 return tv1->vval.v_float == tv2->vval.v_float;
6221#endif
6222
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006223 case VAR_STRING:
6224 s1 = get_tv_string_buf(tv1, buf1);
6225 s2 = get_tv_string_buf(tv2, buf2);
6226 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228
6229 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230 return TRUE;
6231}
6232
6233/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006234 * Locate item with index "n" in list "l" and return it.
6235 * A negative index is counted from the end; -1 is the last item.
6236 * Returns NULL when "n" is out of range.
6237 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006238 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006239list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006241 long n;
6242{
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006244 long idx;
6245
6246 if (l == NULL)
6247 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006248
6249 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006250 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006251 n = l->lv_len + n;
6252
6253 /* Check for index out of range. */
6254 if (n < 0 || n >= l->lv_len)
6255 return NULL;
6256
6257 /* When there is a cached index may start search from there. */
6258 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006259 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006260 if (n < l->lv_idx / 2)
6261 {
6262 /* closest to the start of the list */
6263 item = l->lv_first;
6264 idx = 0;
6265 }
6266 else if (n > (l->lv_idx + l->lv_len) / 2)
6267 {
6268 /* closest to the end of the list */
6269 item = l->lv_last;
6270 idx = l->lv_len - 1;
6271 }
6272 else
6273 {
6274 /* closest to the cached index */
6275 item = l->lv_idx_item;
6276 idx = l->lv_idx;
6277 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 }
6279 else
6280 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006281 if (n < l->lv_len / 2)
6282 {
6283 /* closest to the start of the list */
6284 item = l->lv_first;
6285 idx = 0;
6286 }
6287 else
6288 {
6289 /* closest to the end of the list */
6290 item = l->lv_last;
6291 idx = l->lv_len - 1;
6292 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006294
6295 while (n > idx)
6296 {
6297 /* search forward */
6298 item = item->li_next;
6299 ++idx;
6300 }
6301 while (n < idx)
6302 {
6303 /* search backward */
6304 item = item->li_prev;
6305 --idx;
6306 }
6307
6308 /* cache the used index */
6309 l->lv_idx = idx;
6310 l->lv_idx_item = item;
6311
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 return item;
6313}
6314
6315/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006316 * Get list item "l[idx]" as a number.
6317 */
6318 static long
6319list_find_nr(l, idx, errorp)
6320 list_T *l;
6321 long idx;
6322 int *errorp; /* set to TRUE when something wrong */
6323{
6324 listitem_T *li;
6325
6326 li = list_find(l, idx);
6327 if (li == NULL)
6328 {
6329 if (errorp != NULL)
6330 *errorp = TRUE;
6331 return -1L;
6332 }
6333 return get_tv_number_chk(&li->li_tv, errorp);
6334}
6335
6336/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006337 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6338 */
6339 char_u *
6340list_find_str(l, idx)
6341 list_T *l;
6342 long idx;
6343{
6344 listitem_T *li;
6345
6346 li = list_find(l, idx - 1);
6347 if (li == NULL)
6348 {
6349 EMSGN(_(e_listidx), idx);
6350 return NULL;
6351 }
6352 return get_tv_string(&li->li_tv);
6353}
6354
6355/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006356 * Locate "item" list "l" and return its index.
6357 * Returns -1 when "item" is not in the list.
6358 */
6359 static long
6360list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 list_T *l;
6362 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006363{
6364 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006366
6367 if (l == NULL)
6368 return -1;
6369 idx = 0;
6370 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6371 ++idx;
6372 if (li == NULL)
6373 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006374 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375}
6376
6377/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378 * Append item "item" to the end of list "l".
6379 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006380 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 list_T *l;
6383 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384{
6385 if (l->lv_last == NULL)
6386 {
6387 /* empty list */
6388 l->lv_first = item;
6389 l->lv_last = item;
6390 item->li_prev = NULL;
6391 }
6392 else
6393 {
6394 l->lv_last->li_next = item;
6395 item->li_prev = l->lv_last;
6396 l->lv_last = item;
6397 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006398 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399 item->li_next = NULL;
6400}
6401
6402/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006404 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006406 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 list_T *l;
6409 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006411 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412
Bram Moolenaar05159a02005-02-26 23:04:13 +00006413 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006415 copy_tv(tv, &li->li_tv);
6416 list_append(l, li);
6417 return OK;
6418}
6419
6420/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006421 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006422 * Return FAIL when out of memory.
6423 */
6424 int
6425list_append_dict(list, dict)
6426 list_T *list;
6427 dict_T *dict;
6428{
6429 listitem_T *li = listitem_alloc();
6430
6431 if (li == NULL)
6432 return FAIL;
6433 li->li_tv.v_type = VAR_DICT;
6434 li->li_tv.v_lock = 0;
6435 li->li_tv.vval.v_dict = dict;
6436 list_append(list, li);
6437 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438 return OK;
6439}
6440
6441/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006442 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006443 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006444 * Returns FAIL when out of memory.
6445 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006446 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006447list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448 list_T *l;
6449 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006450 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451{
6452 listitem_T *li = listitem_alloc();
6453
6454 if (li == NULL)
6455 return FAIL;
6456 list_append(l, li);
6457 li->li_tv.v_type = VAR_STRING;
6458 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006459 if (str == NULL)
6460 li->li_tv.vval.v_string = NULL;
6461 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006462 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463 return FAIL;
6464 return OK;
6465}
6466
6467/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006468 * Append "n" to list "l".
6469 * Returns FAIL when out of memory.
6470 */
6471 static int
6472list_append_number(l, n)
6473 list_T *l;
6474 varnumber_T n;
6475{
6476 listitem_T *li;
6477
6478 li = listitem_alloc();
6479 if (li == NULL)
6480 return FAIL;
6481 li->li_tv.v_type = VAR_NUMBER;
6482 li->li_tv.v_lock = 0;
6483 li->li_tv.vval.v_number = n;
6484 list_append(l, li);
6485 return OK;
6486}
6487
6488/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490 * If "item" is NULL append at the end.
6491 * Return FAIL when out of memory.
6492 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006493 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 list_T *l;
6496 typval_T *tv;
6497 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498{
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500
6501 if (ni == NULL)
6502 return FAIL;
6503 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006504 list_insert(l, ni, item);
6505 return OK;
6506}
6507
6508 void
6509list_insert(l, ni, item)
6510 list_T *l;
6511 listitem_T *ni;
6512 listitem_T *item;
6513{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006514 if (item == NULL)
6515 /* Append new item at end of list. */
6516 list_append(l, ni);
6517 else
6518 {
6519 /* Insert new item before existing item. */
6520 ni->li_prev = item->li_prev;
6521 ni->li_next = item;
6522 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006525 ++l->lv_idx;
6526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006530 l->lv_idx_item = NULL;
6531 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006533 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535}
6536
6537/*
6538 * Extend "l1" with "l2".
6539 * If "bef" is NULL append at the end, otherwise insert before this item.
6540 * Returns FAIL when out of memory.
6541 */
6542 static int
6543list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 list_T *l1;
6545 list_T *l2;
6546 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547{
Bram Moolenaar33570922005-01-25 22:26:29 +00006548 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006549 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006551 /* We also quit the loop when we have inserted the original item count of
6552 * the list, avoid a hang when we extend a list with itself. */
6553 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6555 return FAIL;
6556 return OK;
6557}
6558
6559/*
6560 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6561 * Return FAIL when out of memory.
6562 */
6563 static int
6564list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006565 list_T *l1;
6566 list_T *l2;
6567 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568{
Bram Moolenaar33570922005-01-25 22:26:29 +00006569 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006570
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006571 if (l1 == NULL || l2 == NULL)
6572 return FAIL;
6573
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006575 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576 if (l == NULL)
6577 return FAIL;
6578 tv->v_type = VAR_LIST;
6579 tv->vval.v_list = l;
6580
6581 /* append all items from the second list */
6582 return list_extend(l, l2, NULL);
6583}
6584
6585/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006586 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006588 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 * Returns NULL when out of memory.
6590 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006592list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006593 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596{
Bram Moolenaar33570922005-01-25 22:26:29 +00006597 list_T *copy;
6598 listitem_T *item;
6599 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600
6601 if (orig == NULL)
6602 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603
6604 copy = list_alloc();
6605 if (copy != NULL)
6606 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 if (copyID != 0)
6608 {
6609 /* Do this before adding the items, because one of the items may
6610 * refer back to this list. */
6611 orig->lv_copyID = copyID;
6612 orig->lv_copylist = copy;
6613 }
6614 for (item = orig->lv_first; item != NULL && !got_int;
6615 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616 {
6617 ni = listitem_alloc();
6618 if (ni == NULL)
6619 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006620 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006621 {
6622 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6623 {
6624 vim_free(ni);
6625 break;
6626 }
6627 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006629 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 list_append(copy, ni);
6631 }
6632 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006633 if (item != NULL)
6634 {
6635 list_unref(copy);
6636 copy = NULL;
6637 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 }
6639
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640 return copy;
6641}
6642
6643/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006645 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006646 * This used to be called list_remove, but that conflicts with a Sun header
6647 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006649 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006650vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006651 list_T *l;
6652 listitem_T *item;
6653 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006654{
Bram Moolenaar33570922005-01-25 22:26:29 +00006655 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657 /* notify watchers */
6658 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006660 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661 list_fix_watch(l, ip);
6662 if (ip == item2)
6663 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006665
6666 if (item2->li_next == NULL)
6667 l->lv_last = item->li_prev;
6668 else
6669 item2->li_next->li_prev = item->li_prev;
6670 if (item->li_prev == NULL)
6671 l->lv_first = item2->li_next;
6672 else
6673 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006674 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675}
6676
6677/*
6678 * Return an allocated string with the string representation of a list.
6679 * May return NULL.
6680 */
6681 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006682list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006683 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006684 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006685{
6686 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687
6688 if (tv->vval.v_list == NULL)
6689 return NULL;
6690 ga_init2(&ga, (int)sizeof(char), 80);
6691 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006692 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006693 {
6694 vim_free(ga.ga_data);
6695 return NULL;
6696 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697 ga_append(&ga, ']');
6698 ga_append(&ga, NUL);
6699 return (char_u *)ga.ga_data;
6700}
6701
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006702typedef struct join_S {
6703 char_u *s;
6704 char_u *tofree;
6705} join_T;
6706
6707 static int
6708list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6709 garray_T *gap; /* to store the result in */
6710 list_T *l;
6711 char_u *sep;
6712 int echo_style;
6713 int copyID;
6714 garray_T *join_gap; /* to keep each list item string */
6715{
6716 int i;
6717 join_T *p;
6718 int len;
6719 int sumlen = 0;
6720 int first = TRUE;
6721 char_u *tofree;
6722 char_u numbuf[NUMBUFLEN];
6723 listitem_T *item;
6724 char_u *s;
6725
6726 /* Stringify each item in the list. */
6727 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6728 {
6729 if (echo_style)
6730 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6731 else
6732 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6733 if (s == NULL)
6734 return FAIL;
6735
6736 len = (int)STRLEN(s);
6737 sumlen += len;
6738
6739 ga_grow(join_gap, 1);
6740 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6741 if (tofree != NULL || s != numbuf)
6742 {
6743 p->s = s;
6744 p->tofree = tofree;
6745 }
6746 else
6747 {
6748 p->s = vim_strnsave(s, len);
6749 p->tofree = p->s;
6750 }
6751
6752 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006753 if (did_echo_string_emsg) /* recursion error, bail out */
6754 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006755 }
6756
6757 /* Allocate result buffer with its total size, avoid re-allocation and
6758 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6759 if (join_gap->ga_len >= 2)
6760 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6761 if (ga_grow(gap, sumlen + 2) == FAIL)
6762 return FAIL;
6763
6764 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6765 {
6766 if (first)
6767 first = FALSE;
6768 else
6769 ga_concat(gap, sep);
6770 p = ((join_T *)join_gap->ga_data) + i;
6771
6772 if (p->s != NULL)
6773 ga_concat(gap, p->s);
6774 line_breakcheck();
6775 }
6776
6777 return OK;
6778}
6779
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006781 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006782 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006783 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006785 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006786list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006788 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006790 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006791 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006793 garray_T join_ga;
6794 int retval;
6795 join_T *p;
6796 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797
Bram Moolenaard39a7512015-04-16 22:51:22 +02006798 if (l->lv_len < 1)
6799 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006800 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6801 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6802
6803 /* Dispose each item in join_ga. */
6804 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006806 p = (join_T *)join_ga.ga_data;
6807 for (i = 0; i < join_ga.ga_len; ++i)
6808 {
6809 vim_free(p->tofree);
6810 ++p;
6811 }
6812 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006813 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006814
6815 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006816}
6817
6818/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006819 * Garbage collection for lists and dictionaries.
6820 *
6821 * We use reference counts to be able to free most items right away when they
6822 * are no longer used. But for composite items it's possible that it becomes
6823 * unused while the reference count is > 0: When there is a recursive
6824 * reference. Example:
6825 * :let l = [1, 2, 3]
6826 * :let d = {9: l}
6827 * :let l[1] = d
6828 *
6829 * Since this is quite unusual we handle this with garbage collection: every
6830 * once in a while find out which lists and dicts are not referenced from any
6831 * variable.
6832 *
6833 * Here is a good reference text about garbage collection (refers to Python
6834 * but it applies to all reference-counting mechanisms):
6835 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006837
6838/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 * Do garbage collection for lists and dicts.
6840 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006841 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 int
6843garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006845 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006846 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006847 buf_T *buf;
6848 win_T *wp;
6849 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006850 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006851 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006852 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006853#ifdef FEAT_WINDOWS
6854 tabpage_T *tp;
6855#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006857 /* Only do this once. */
6858 want_garbage_collect = FALSE;
6859 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006860 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006861
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862 /* We advance by two because we add one for items referenced through
6863 * previous_funccal. */
6864 current_copyID += COPYID_INC;
6865 copyID = current_copyID;
6866
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 /*
6868 * 1. Go through all accessible variables and mark all lists and dicts
6869 * with copyID.
6870 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871
6872 /* Don't free variables in the previous_funccal list unless they are only
6873 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006874 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6876 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6878 NULL);
6879 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6880 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 }
6882
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883 /* script-local variables */
6884 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886
6887 /* buffer-local variables */
6888 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6890 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891
6892 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006893 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006894 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6895 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006896#ifdef FEAT_AUTOCMD
6897 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006898 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6899 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006900#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006902#ifdef FEAT_WINDOWS
6903 /* tabpage-local variables */
6904 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6906 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006907#endif
6908
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911
6912 /* function-local variables */
6913 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6914 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6916 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 }
6918
Bram Moolenaard812df62008-11-09 12:46:09 +00006919 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006921
Bram Moolenaar1dced572012-04-05 16:54:08 +02006922#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006924#endif
6925
Bram Moolenaardb913952012-06-29 12:54:53 +02006926#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006928#endif
6929
6930#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006932#endif
6933
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006935 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006936 /*
6937 * 2. Free lists and dictionaries that are not referenced.
6938 */
6939 did_free = free_unref_items(copyID);
6940
6941 /*
6942 * 3. Check if any funccal can be freed now.
6943 */
6944 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006945 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 if (can_free_funccal(*pfc, copyID))
6947 {
6948 fc = *pfc;
6949 *pfc = fc->caller;
6950 free_funccal(fc, TRUE);
6951 did_free = TRUE;
6952 did_free_funccal = TRUE;
6953 }
6954 else
6955 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006956 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 if (did_free_funccal)
6958 /* When a funccal was freed some more items might be garbage
6959 * collected, so run again. */
6960 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006962 else if (p_verbose > 0)
6963 {
6964 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6965 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966
6967 return did_free;
6968}
6969
6970/*
6971 * Free lists and dictionaries that are no longer referenced.
6972 */
6973 static int
6974free_unref_items(copyID)
6975 int copyID;
6976{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006977 dict_T *dd, *dd_next;
6978 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979 int did_free = FALSE;
6980
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 */
6984 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006985 {
6986 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006989 /* Free the Dictionary and ordinary items it contains, but don't
6990 * recurse into Lists and Dictionaries, they will be in the list
6991 * of dicts or list of lists. */
6992 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006995 dd = dd_next;
6996 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997
6998 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006999 * Go through the list of lists and free items without the copyID.
7000 * But don't free a list that has a watcher (used in a for loop), these
7001 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 */
7003 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007004 {
7005 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7007 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007009 /* Free the List and ordinary items it contains, but don't recurse
7010 * into Lists and Dictionaries, they will be in the list of dicts
7011 * or list of lists. */
7012 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007015 ll = ll_next;
7016 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 return did_free;
7018}
7019
7020/*
7021 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007022 * "list_stack" is used to add lists to be marked. Can be NULL.
7023 *
7024 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007026 int
7027set_ref_in_ht(ht, copyID, list_stack)
7028 hashtab_T *ht;
7029 int copyID;
7030 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031{
7032 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007035 hashtab_T *cur_ht;
7036 ht_stack_T *ht_stack = NULL;
7037 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 cur_ht = ht;
7040 for (;;)
7041 {
7042 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 /* Mark each item in the hashtab. If the item contains a hashtab
7045 * it is added to ht_stack, if it contains a list it is added to
7046 * list_stack. */
7047 todo = (int)cur_ht->ht_used;
7048 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7049 if (!HASHITEM_EMPTY(hi))
7050 {
7051 --todo;
7052 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7053 &ht_stack, list_stack);
7054 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007056
7057 if (ht_stack == NULL)
7058 break;
7059
7060 /* take an item from the stack */
7061 cur_ht = ht_stack->ht;
7062 tempitem = ht_stack;
7063 ht_stack = ht_stack->prev;
7064 free(tempitem);
7065 }
7066
7067 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068}
7069
7070/*
7071 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7073 *
7074 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076 int
7077set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007078 list_T *l;
7079 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007082 listitem_T *li;
7083 int abort = FALSE;
7084 list_T *cur_l;
7085 list_stack_T *list_stack = NULL;
7086 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 cur_l = l;
7089 for (;;)
7090 {
7091 if (!abort)
7092 /* Mark each item in the list. If the item contains a hashtab
7093 * it is added to ht_stack, if it contains a list it is added to
7094 * list_stack. */
7095 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7096 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7097 ht_stack, &list_stack);
7098 if (list_stack == NULL)
7099 break;
7100
7101 /* take an item from the stack */
7102 cur_l = list_stack->list;
7103 tempitem = list_stack;
7104 list_stack = list_stack->prev;
7105 free(tempitem);
7106 }
7107
7108 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109}
7110
7111/*
7112 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007113 * "list_stack" is used to add lists to be marked. Can be NULL.
7114 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7115 *
7116 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007118 int
7119set_ref_in_item(tv, copyID, ht_stack, list_stack)
7120 typval_T *tv;
7121 int copyID;
7122 ht_stack_T **ht_stack;
7123 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124{
7125 dict_T *dd;
7126 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007128
7129 switch (tv->v_type)
7130 {
7131 case VAR_DICT:
7132 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007133 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007134 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007135 /* Didn't see this dict yet. */
7136 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 if (ht_stack == NULL)
7138 {
7139 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7140 }
7141 else
7142 {
7143 ht_stack_T *newitem = (ht_stack_T*)malloc(
7144 sizeof(ht_stack_T));
7145 if (newitem == NULL)
7146 abort = TRUE;
7147 else
7148 {
7149 newitem->ht = &dd->dv_hashtab;
7150 newitem->prev = *ht_stack;
7151 *ht_stack = newitem;
7152 }
7153 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007155 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007156
7157 case VAR_LIST:
7158 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007159 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007160 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007161 /* Didn't see this list yet. */
7162 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007163 if (list_stack == NULL)
7164 {
7165 abort = set_ref_in_list(ll, copyID, ht_stack);
7166 }
7167 else
7168 {
7169 list_stack_T *newitem = (list_stack_T*)malloc(
7170 sizeof(list_stack_T));
7171 if (newitem == NULL)
7172 abort = TRUE;
7173 else
7174 {
7175 newitem->list = ll;
7176 newitem->prev = *list_stack;
7177 *list_stack = newitem;
7178 }
7179 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007180 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007181 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007182 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007183 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007184}
7185
7186/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007187 * Allocate an empty header for a dictionary.
7188 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007189 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190dict_alloc()
7191{
Bram Moolenaar33570922005-01-25 22:26:29 +00007192 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007196 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007197 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007198 if (first_dict != NULL)
7199 first_dict->dv_used_prev = d;
7200 d->dv_used_next = first_dict;
7201 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007202 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007203
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007205 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007206 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007207 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007209 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211}
7212
7213/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007214 * Allocate an empty dict for a return value.
7215 * Returns OK or FAIL.
7216 */
7217 static int
7218rettv_dict_alloc(rettv)
7219 typval_T *rettv;
7220{
7221 dict_T *d = dict_alloc();
7222
7223 if (d == NULL)
7224 return FAIL;
7225
7226 rettv->vval.v_dict = d;
7227 rettv->v_type = VAR_DICT;
7228 ++d->dv_refcount;
7229 return OK;
7230}
7231
7232
7233/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007234 * Unreference a Dictionary: decrement the reference count and free it when it
7235 * becomes zero.
7236 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007237 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007241 if (d != NULL && --d->dv_refcount <= 0)
7242 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243}
7244
7245/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007246 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247 * Ignores the reference count.
7248 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007249 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007250dict_free(d, recurse)
7251 dict_T *d;
7252 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007254 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007256 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007258 /* Remove the dict from the list of dicts for garbage collection. */
7259 if (d->dv_used_prev == NULL)
7260 first_dict = d->dv_used_next;
7261 else
7262 d->dv_used_prev->dv_used_next = d->dv_used_next;
7263 if (d->dv_used_next != NULL)
7264 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7265
7266 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007267 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007268 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 if (!HASHITEM_EMPTY(hi))
7272 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007273 /* Remove the item before deleting it, just in case there is
7274 * something recursive causing trouble. */
7275 di = HI2DI(hi);
7276 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007277 if (recurse || (di->di_tv.v_type != VAR_LIST
7278 && di->di_tv.v_type != VAR_DICT))
7279 clear_tv(&di->di_tv);
7280 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007281 --todo;
7282 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285 vim_free(d);
7286}
7287
7288/*
7289 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 * The "key" is copied to the new item.
7291 * Note that the value of the item "di_tv" still needs to be initialized!
7292 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007294 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007295dictitem_alloc(key)
7296 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297{
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007300 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007304 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007305 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307}
7308
7309/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007310 * Make a copy of a Dictionary item.
7311 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007312 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315{
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007318 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7319 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 if (di != NULL)
7321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007323 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 copy_tv(&org->di_tv, &di->di_tv);
7325 }
7326 return di;
7327}
7328
7329/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 * Remove item "item" from Dictionary "dict" and free it.
7331 */
7332 static void
7333dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 dict_T *dict;
7335 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336{
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340 if (HASHITEM_EMPTY(hi))
7341 EMSG2(_(e_intern2), "dictitem_remove()");
7342 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344 dictitem_free(item);
7345}
7346
7347/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007348 * Free a dict item. Also clears the value.
7349 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007350 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007355 if (item->di_flags & DI_FLAGS_ALLOC)
7356 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357}
7358
7359/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7361 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007362 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 * Returns NULL when out of memory.
7364 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007366dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007369 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370{
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 dict_T *copy;
7372 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007373 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375
7376 if (orig == NULL)
7377 return NULL;
7378
7379 copy = dict_alloc();
7380 if (copy != NULL)
7381 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007382 if (copyID != 0)
7383 {
7384 orig->dv_copyID = copyID;
7385 orig->dv_copydict = copy;
7386 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007387 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007388 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007391 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 --todo;
7393
7394 di = dictitem_alloc(hi->hi_key);
7395 if (di == NULL)
7396 break;
7397 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007398 {
7399 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7400 copyID) == FAIL)
7401 {
7402 vim_free(di);
7403 break;
7404 }
7405 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007406 else
7407 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7408 if (dict_add(copy, di) == FAIL)
7409 {
7410 dictitem_free(di);
7411 break;
7412 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007415
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417 if (todo > 0)
7418 {
7419 dict_unref(copy);
7420 copy = NULL;
7421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422 }
7423
7424 return copy;
7425}
7426
7427/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007429 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007431 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007433 dict_T *d;
7434 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435{
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437}
7438
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007440 * Add a number or string entry to dictionary "d".
7441 * When "str" is NULL use number "nr", otherwise use "str".
7442 * Returns FAIL when out of memory and when key already exists.
7443 */
7444 int
7445dict_add_nr_str(d, key, nr, str)
7446 dict_T *d;
7447 char *key;
7448 long nr;
7449 char_u *str;
7450{
7451 dictitem_T *item;
7452
7453 item = dictitem_alloc((char_u *)key);
7454 if (item == NULL)
7455 return FAIL;
7456 item->di_tv.v_lock = 0;
7457 if (str == NULL)
7458 {
7459 item->di_tv.v_type = VAR_NUMBER;
7460 item->di_tv.vval.v_number = nr;
7461 }
7462 else
7463 {
7464 item->di_tv.v_type = VAR_STRING;
7465 item->di_tv.vval.v_string = vim_strsave(str);
7466 }
7467 if (dict_add(d, item) == FAIL)
7468 {
7469 dictitem_free(item);
7470 return FAIL;
7471 }
7472 return OK;
7473}
7474
7475/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007476 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007477 * Returns FAIL when out of memory and when key already exists.
7478 */
7479 int
7480dict_add_list(d, key, list)
7481 dict_T *d;
7482 char *key;
7483 list_T *list;
7484{
7485 dictitem_T *item;
7486
7487 item = dictitem_alloc((char_u *)key);
7488 if (item == NULL)
7489 return FAIL;
7490 item->di_tv.v_lock = 0;
7491 item->di_tv.v_type = VAR_LIST;
7492 item->di_tv.vval.v_list = list;
7493 if (dict_add(d, item) == FAIL)
7494 {
7495 dictitem_free(item);
7496 return FAIL;
7497 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007498 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007499 return OK;
7500}
7501
7502/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007503 * Get the number of items in a Dictionary.
7504 */
7505 static long
7506dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007508{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007509 if (d == NULL)
7510 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007511 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512}
7513
7514/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 * Find item "key[len]" in Dictionary "d".
7516 * If "len" is negative use strlen(key).
7517 * Returns NULL when not found.
7518 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007519 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007521 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 char_u *key;
7523 int len;
7524{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525#define AKEYLEN 200
7526 char_u buf[AKEYLEN];
7527 char_u *akey;
7528 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 if (len < 0)
7532 akey = key;
7533 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007534 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 tofree = akey = vim_strnsave(key, len);
7536 if (akey == NULL)
7537 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007538 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 else
7540 {
7541 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007542 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 akey = buf;
7544 }
7545
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 vim_free(tofree);
7548 if (HASHITEM_EMPTY(hi))
7549 return NULL;
7550 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551}
7552
7553/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007554 * Get a string item from a dictionary.
7555 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007556 * Returns NULL if the entry doesn't exist or out of memory.
7557 */
7558 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007559get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007560 dict_T *d;
7561 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007562 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007563{
7564 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007565 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007566
7567 di = dict_find(d, key, -1);
7568 if (di == NULL)
7569 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007570 s = get_tv_string(&di->di_tv);
7571 if (save && s != NULL)
7572 s = vim_strsave(s);
7573 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007574}
7575
7576/*
7577 * Get a number item from a dictionary.
7578 * Returns 0 if the entry doesn't exist or out of memory.
7579 */
7580 long
7581get_dict_number(d, key)
7582 dict_T *d;
7583 char_u *key;
7584{
7585 dictitem_T *di;
7586
7587 di = dict_find(d, key, -1);
7588 if (di == NULL)
7589 return 0;
7590 return get_tv_number(&di->di_tv);
7591}
7592
7593/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 * Return an allocated string with the string representation of a Dictionary.
7595 * May return NULL.
7596 */
7597 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007599 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007600 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601{
7602 garray_T ga;
7603 int first = TRUE;
7604 char_u *tofree;
7605 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007609 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007611 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 return NULL;
7613 ga_init2(&ga, (int)sizeof(char), 80);
7614 ga_append(&ga, '{');
7615
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007616 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007617 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007619 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007621 --todo;
7622
7623 if (first)
7624 first = FALSE;
7625 else
7626 ga_concat(&ga, (char_u *)", ");
7627
7628 tofree = string_quote(hi->hi_key, FALSE);
7629 if (tofree != NULL)
7630 {
7631 ga_concat(&ga, tofree);
7632 vim_free(tofree);
7633 }
7634 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007635 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 if (s != NULL)
7637 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007639 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007640 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007641 line_breakcheck();
7642
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007644 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007645 if (todo > 0)
7646 {
7647 vim_free(ga.ga_data);
7648 return NULL;
7649 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650
7651 ga_append(&ga, '}');
7652 ga_append(&ga, NUL);
7653 return (char_u *)ga.ga_data;
7654}
7655
7656/*
7657 * Allocate a variable for a Dictionary and fill it from "*arg".
7658 * Return OK or FAIL. Returns NOTDONE for {expr}.
7659 */
7660 static int
7661get_dict_tv(arg, rettv, evaluate)
7662 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 int evaluate;
7665{
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 dict_T *d = NULL;
7667 typval_T tvkey;
7668 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007669 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007672 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673
7674 /*
7675 * First check if it's not a curly-braces thing: {expr}.
7676 * Must do this without evaluating, otherwise a function may be called
7677 * twice. Unfortunately this means we need to call eval1() twice for the
7678 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007679 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 if (*start != '}')
7682 {
7683 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7684 return FAIL;
7685 if (*start == '}')
7686 return NOTDONE;
7687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688
7689 if (evaluate)
7690 {
7691 d = dict_alloc();
7692 if (d == NULL)
7693 return FAIL;
7694 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 tvkey.v_type = VAR_UNKNOWN;
7696 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697
7698 *arg = skipwhite(*arg + 1);
7699 while (**arg != '}' && **arg != NUL)
7700 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007701 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 goto failret;
7703 if (**arg != ':')
7704 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007705 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007706 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707 goto failret;
7708 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007709 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007711 key = get_tv_string_buf_chk(&tvkey, buf);
7712 if (key == NULL || *key == NUL)
7713 {
7714 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7715 if (key != NULL)
7716 EMSG(_(e_emptykey));
7717 clear_tv(&tvkey);
7718 goto failret;
7719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721
7722 *arg = skipwhite(*arg + 1);
7723 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7724 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007725 if (evaluate)
7726 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 goto failret;
7728 }
7729 if (evaluate)
7730 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007731 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 if (item != NULL)
7733 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007734 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007735 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007736 clear_tv(&tv);
7737 goto failret;
7738 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007739 item = dictitem_alloc(key);
7740 clear_tv(&tvkey);
7741 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007742 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007744 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007745 if (dict_add(d, item) == FAIL)
7746 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747 }
7748 }
7749
7750 if (**arg == '}')
7751 break;
7752 if (**arg != ',')
7753 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007754 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 goto failret;
7756 }
7757 *arg = skipwhite(*arg + 1);
7758 }
7759
7760 if (**arg != '}')
7761 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007762 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763failret:
7764 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007765 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766 return FAIL;
7767 }
7768
7769 *arg = skipwhite(*arg + 1);
7770 if (evaluate)
7771 {
7772 rettv->v_type = VAR_DICT;
7773 rettv->vval.v_dict = d;
7774 ++d->dv_refcount;
7775 }
7776
7777 return OK;
7778}
7779
Bram Moolenaar8c711452005-01-14 21:53:12 +00007780/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007781 * Return a string with the string representation of a variable.
7782 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007783 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007784 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007785 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007786 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007787 */
7788 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007789echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007790 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007791 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007792 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007793 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007795 static int recurse = 0;
7796 char_u *r = NULL;
7797
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007799 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007800 if (!did_echo_string_emsg)
7801 {
7802 /* Only give this message once for a recursive call to avoid
7803 * flooding the user with errors. And stop iterating over lists
7804 * and dicts. */
7805 did_echo_string_emsg = TRUE;
7806 EMSG(_("E724: variable nested too deep for displaying"));
7807 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007808 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007809 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007810 }
7811 ++recurse;
7812
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 switch (tv->v_type)
7814 {
7815 case VAR_FUNC:
7816 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 r = tv->vval.v_string;
7818 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007819
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007821 if (tv->vval.v_list == NULL)
7822 {
7823 *tofree = NULL;
7824 r = NULL;
7825 }
7826 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7827 {
7828 *tofree = NULL;
7829 r = (char_u *)"[...]";
7830 }
7831 else
7832 {
7833 tv->vval.v_list->lv_copyID = copyID;
7834 *tofree = list2string(tv, copyID);
7835 r = *tofree;
7836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007838
Bram Moolenaar8c711452005-01-14 21:53:12 +00007839 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007840 if (tv->vval.v_dict == NULL)
7841 {
7842 *tofree = NULL;
7843 r = NULL;
7844 }
7845 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7846 {
7847 *tofree = NULL;
7848 r = (char_u *)"{...}";
7849 }
7850 else
7851 {
7852 tv->vval.v_dict->dv_copyID = copyID;
7853 *tofree = dict2string(tv, copyID);
7854 r = *tofree;
7855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007858 case VAR_STRING:
7859 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007860 *tofree = NULL;
7861 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007862 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007863
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007864#ifdef FEAT_FLOAT
7865 case VAR_FLOAT:
7866 *tofree = NULL;
7867 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7868 r = numbuf;
7869 break;
7870#endif
7871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007872 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007873 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007875 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876
Bram Moolenaar8502c702014-06-17 12:51:16 +02007877 if (--recurse == 0)
7878 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880}
7881
7882/*
7883 * Return a string with the string representation of a variable.
7884 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7885 * "numbuf" is used for a number.
7886 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007887 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007888 */
7889 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007890tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007891 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892 char_u **tofree;
7893 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007894 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895{
7896 switch (tv->v_type)
7897 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007898 case VAR_FUNC:
7899 *tofree = string_quote(tv->vval.v_string, TRUE);
7900 return *tofree;
7901 case VAR_STRING:
7902 *tofree = string_quote(tv->vval.v_string, FALSE);
7903 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904#ifdef FEAT_FLOAT
7905 case VAR_FLOAT:
7906 *tofree = NULL;
7907 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7908 return numbuf;
7909#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007910 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007912 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007913 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007915 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007916 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007917 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007918}
7919
7920/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 * Return string "str" in ' quotes, doubling ' characters.
7922 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007923 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007924 */
7925 static char_u *
7926string_quote(str, function)
7927 char_u *str;
7928 int function;
7929{
Bram Moolenaar33570922005-01-25 22:26:29 +00007930 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007931 char_u *p, *r, *s;
7932
Bram Moolenaar33570922005-01-25 22:26:29 +00007933 len = (function ? 13 : 3);
7934 if (str != NULL)
7935 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007936 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 for (p = str; *p != NUL; mb_ptr_adv(p))
7938 if (*p == '\'')
7939 ++len;
7940 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007941 s = r = alloc(len);
7942 if (r != NULL)
7943 {
7944 if (function)
7945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007947 r += 10;
7948 }
7949 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 if (str != NULL)
7952 for (p = str; *p != NUL; )
7953 {
7954 if (*p == '\'')
7955 *r++ = '\'';
7956 MB_COPY_CHAR(p, r);
7957 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007958 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007959 if (function)
7960 *r++ = ')';
7961 *r++ = NUL;
7962 }
7963 return s;
7964}
7965
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007966#ifdef FEAT_FLOAT
7967/*
7968 * Convert the string "text" to a floating point number.
7969 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7970 * this always uses a decimal point.
7971 * Returns the length of the text that was consumed.
7972 */
7973 static int
7974string2float(text, value)
7975 char_u *text;
7976 float_T *value; /* result stored here */
7977{
7978 char *s = (char *)text;
7979 float_T f;
7980
7981 f = strtod(s, &s);
7982 *value = f;
7983 return (int)((char_u *)s - text);
7984}
7985#endif
7986
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 * Get the value of an environment variable.
7989 * "arg" is pointing to the '$'. It is advanced to after the name.
7990 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007991 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 */
7993 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007994get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 int evaluate;
7998{
7999 char_u *string = NULL;
8000 int len;
8001 int cc;
8002 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008003 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004
8005 ++*arg;
8006 name = *arg;
8007 len = get_env_len(arg);
8008 if (evaluate)
8009 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008010 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008011 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008012
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008013 cc = name[len];
8014 name[len] = NUL;
8015 /* first try vim_getenv(), fast for normal environment vars */
8016 string = vim_getenv(name, &mustfree);
8017 if (string != NULL && *string != NUL)
8018 {
8019 if (!mustfree)
8020 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008022 else
8023 {
8024 if (mustfree)
8025 vim_free(string);
8026
8027 /* next try expanding things like $VIM and ${HOME} */
8028 string = expand_env_save(name - 1);
8029 if (string != NULL && *string == '$')
8030 {
8031 vim_free(string);
8032 string = NULL;
8033 }
8034 }
8035 name[len] = cc;
8036
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008037 rettv->v_type = VAR_STRING;
8038 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 }
8040
8041 return OK;
8042}
8043
8044/*
8045 * Array with names and number of arguments of all internal functions
8046 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8047 */
8048static struct fst
8049{
8050 char *f_name; /* function name */
8051 char f_min_argc; /* minimal number of arguments */
8052 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008054 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055} functions[] =
8056{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008057#ifdef FEAT_FLOAT
8058 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008059 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008060#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008061 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008062 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"append", 2, 2, f_append},
8064 {"argc", 0, 0, f_argc},
8065 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008066 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008067 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008069 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008071 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008074 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"bufexists", 1, 1, f_bufexists},
8076 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8077 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8078 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8079 {"buflisted", 1, 1, f_buflisted},
8080 {"bufloaded", 1, 1, f_bufloaded},
8081 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008082 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"bufwinnr", 1, 1, f_bufwinnr},
8084 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008085 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008086 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008087 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088#ifdef FEAT_FLOAT
8089 {"ceil", 1, 1, f_ceil},
8090#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008091 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008092 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008094 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008096#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008097 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008098 {"complete_add", 1, 1, f_complete_add},
8099 {"complete_check", 0, 0, f_complete_check},
8100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008102 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008103#ifdef FEAT_FLOAT
8104 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008105 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008106#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008107 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008109 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008110 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"delete", 1, 1, f_delete},
8112 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008113 {"diff_filler", 1, 1, f_diff_filler},
8114 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008115 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008117 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"eventhandler", 0, 0, f_eventhandler},
8119 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008120 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008122#ifdef FEAT_FLOAT
8123 {"exp", 1, 1, f_exp},
8124#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008125 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008127 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8129 {"filereadable", 1, 1, f_filereadable},
8130 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008131 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008132 {"finddir", 1, 3, f_finddir},
8133 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008134#ifdef FEAT_FLOAT
8135 {"float2nr", 1, 1, f_float2nr},
8136 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008137 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008138#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008139 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"fnamemodify", 2, 2, f_fnamemodify},
8141 {"foldclosed", 1, 1, f_foldclosed},
8142 {"foldclosedend", 1, 1, f_foldclosedend},
8143 {"foldlevel", 1, 1, f_foldlevel},
8144 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008145 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008147 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008148 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008149 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008150 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008151 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {"getchar", 0, 1, f_getchar},
8153 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008154 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"getcmdline", 0, 0, f_getcmdline},
8156 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008157 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008158 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008159 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008161 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008162 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"getfsize", 1, 1, f_getfsize},
8164 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008165 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008166 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008167 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008168 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008169 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008170 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008171 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008172 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008174 {"gettabvar", 2, 3, f_gettabvar},
8175 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"getwinposx", 0, 0, f_getwinposx},
8177 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008178 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008179 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008180 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008181 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008183 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008184 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008185 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8187 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8188 {"histadd", 2, 2, f_histadd},
8189 {"histdel", 1, 2, f_histdel},
8190 {"histget", 1, 2, f_histget},
8191 {"histnr", 1, 1, f_histnr},
8192 {"hlID", 1, 1, f_hlID},
8193 {"hlexists", 1, 1, f_hlexists},
8194 {"hostname", 0, 0, f_hostname},
8195 {"iconv", 3, 3, f_iconv},
8196 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008197 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008198 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008200 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {"inputrestore", 0, 0, f_inputrestore},
8202 {"inputsave", 0, 0, f_inputsave},
8203 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008204 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008205 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008207 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008208 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008209 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008210 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008212 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"libcall", 3, 3, f_libcall},
8214 {"libcallnr", 3, 3, f_libcallnr},
8215 {"line", 1, 1, f_line},
8216 {"line2byte", 1, 1, f_line2byte},
8217 {"lispindent", 1, 1, f_lispindent},
8218 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008219#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008220 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008221 {"log10", 1, 1, f_log10},
8222#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008223#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008224 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008225#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008226 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008227 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008228 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008229 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008230 {"matchadd", 2, 5, f_matchadd},
8231 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008232 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008233 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008234 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008235 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008236 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008237 {"max", 1, 1, f_max},
8238 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008239#ifdef vim_mkdir
8240 {"mkdir", 1, 3, f_mkdir},
8241#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008242 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008243#ifdef FEAT_MZSCHEME
8244 {"mzeval", 1, 1, f_mzeval},
8245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008247 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008248 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008249 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008250#ifdef FEAT_FLOAT
8251 {"pow", 2, 2, f_pow},
8252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008254 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008255 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008256#ifdef FEAT_PYTHON3
8257 {"py3eval", 1, 1, f_py3eval},
8258#endif
8259#ifdef FEAT_PYTHON
8260 {"pyeval", 1, 1, f_pyeval},
8261#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008262 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008263 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008264 {"reltime", 0, 2, f_reltime},
8265 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {"remote_expr", 2, 3, f_remote_expr},
8267 {"remote_foreground", 1, 1, f_remote_foreground},
8268 {"remote_peek", 1, 2, f_remote_peek},
8269 {"remote_read", 1, 1, f_remote_read},
8270 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008271 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008273 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008275 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008276#ifdef FEAT_FLOAT
8277 {"round", 1, 1, f_round},
8278#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008279 {"screenattr", 2, 2, f_screenattr},
8280 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008281 {"screencol", 0, 0, f_screencol},
8282 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008283 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008284 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008285 {"searchpair", 3, 7, f_searchpair},
8286 {"searchpairpos", 3, 7, f_searchpairpos},
8287 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {"server2client", 2, 2, f_server2client},
8289 {"serverlist", 0, 0, f_serverlist},
8290 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008291 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"setcmdpos", 1, 1, f_setcmdpos},
8293 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008294 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008295 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008296 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008297 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008299 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008300 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008302#ifdef FEAT_CRYPT
8303 {"sha256", 1, 1, f_sha256},
8304#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008305 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008306 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008308#ifdef FEAT_FLOAT
8309 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008310 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008311#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008312 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008313 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008314 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008315 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008316 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008317#ifdef FEAT_FLOAT
8318 {"sqrt", 1, 1, f_sqrt},
8319 {"str2float", 1, 1, f_str2float},
8320#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008321 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008322 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008323 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324#ifdef HAVE_STRFTIME
8325 {"strftime", 1, 2, f_strftime},
8326#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008327 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008328 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {"strlen", 1, 1, f_strlen},
8330 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008331 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008333 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008334 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"substitute", 4, 4, f_substitute},
8336 {"synID", 3, 3, f_synID},
8337 {"synIDattr", 2, 3, f_synIDattr},
8338 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008339 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008340 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008341 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008342 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008343 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008344 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008345 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008346 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008347 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008348#ifdef FEAT_FLOAT
8349 {"tan", 1, 1, f_tan},
8350 {"tanh", 1, 1, f_tanh},
8351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008353 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {"tolower", 1, 1, f_tolower},
8355 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008356 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008357#ifdef FEAT_FLOAT
8358 {"trunc", 1, 1, f_trunc},
8359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008361 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008362 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008363 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008364 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"virtcol", 1, 1, f_virtcol},
8366 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008367 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {"winbufnr", 1, 1, f_winbufnr},
8369 {"wincol", 0, 0, f_wincol},
8370 {"winheight", 1, 1, f_winheight},
8371 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008372 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008374 {"winrestview", 1, 1, f_winrestview},
8375 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008377 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008378 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379};
8380
8381#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8382
8383/*
8384 * Function given to ExpandGeneric() to obtain the list of internal
8385 * or user defined function names.
8386 */
8387 char_u *
8388get_function_name(xp, idx)
8389 expand_T *xp;
8390 int idx;
8391{
8392 static int intidx = -1;
8393 char_u *name;
8394
8395 if (idx == 0)
8396 intidx = -1;
8397 if (intidx < 0)
8398 {
8399 name = get_user_func_name(xp, idx);
8400 if (name != NULL)
8401 return name;
8402 }
8403 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8404 {
8405 STRCPY(IObuff, functions[intidx].f_name);
8406 STRCAT(IObuff, "(");
8407 if (functions[intidx].f_max_argc == 0)
8408 STRCAT(IObuff, ")");
8409 return IObuff;
8410 }
8411
8412 return NULL;
8413}
8414
8415/*
8416 * Function given to ExpandGeneric() to obtain the list of internal or
8417 * user defined variable or function names.
8418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 char_u *
8420get_expr_name(xp, idx)
8421 expand_T *xp;
8422 int idx;
8423{
8424 static int intidx = -1;
8425 char_u *name;
8426
8427 if (idx == 0)
8428 intidx = -1;
8429 if (intidx < 0)
8430 {
8431 name = get_function_name(xp, idx);
8432 if (name != NULL)
8433 return name;
8434 }
8435 return get_user_var_name(xp, ++intidx);
8436}
8437
8438#endif /* FEAT_CMDL_COMPL */
8439
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008440#if defined(EBCDIC) || defined(PROTO)
8441/*
8442 * Compare struct fst by function name.
8443 */
8444 static int
8445compare_func_name(s1, s2)
8446 const void *s1;
8447 const void *s2;
8448{
8449 struct fst *p1 = (struct fst *)s1;
8450 struct fst *p2 = (struct fst *)s2;
8451
8452 return STRCMP(p1->f_name, p2->f_name);
8453}
8454
8455/*
8456 * Sort the function table by function name.
8457 * The sorting of the table above is ASCII dependant.
8458 * On machines using EBCDIC we have to sort it.
8459 */
8460 static void
8461sortFunctions()
8462{
8463 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8464
8465 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8466}
8467#endif
8468
8469
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470/*
8471 * Find internal function in table above.
8472 * Return index, or -1 if not found
8473 */
8474 static int
8475find_internal_func(name)
8476 char_u *name; /* name of the function */
8477{
8478 int first = 0;
8479 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8480 int cmp;
8481 int x;
8482
8483 /*
8484 * Find the function name in the table. Binary search.
8485 */
8486 while (first <= last)
8487 {
8488 x = first + ((unsigned)(last - first) >> 1);
8489 cmp = STRCMP(name, functions[x].f_name);
8490 if (cmp < 0)
8491 last = x - 1;
8492 else if (cmp > 0)
8493 first = x + 1;
8494 else
8495 return x;
8496 }
8497 return -1;
8498}
8499
8500/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8502 * name it contains, otherwise return "name".
8503 */
8504 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008505deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008506 char_u *name;
8507 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008508 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509{
Bram Moolenaar33570922005-01-25 22:26:29 +00008510 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008511 int cc;
8512
8513 cc = name[*lenp];
8514 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008515 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008517 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008518 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008519 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008520 {
8521 *lenp = 0;
8522 return (char_u *)""; /* just in case */
8523 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008524 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008525 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 }
8527
8528 return name;
8529}
8530
8531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 * Allocate a variable for the result of a function.
8533 * Return OK or FAIL.
8534 */
8535 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008536get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8537 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 char_u *name; /* name of the function */
8539 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 char_u **arg; /* argument, pointing to the '(' */
8542 linenr_T firstline; /* first line of range */
8543 linenr_T lastline; /* last line of range */
8544 int *doesrange; /* return: function handled range */
8545 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547{
8548 char_u *argp;
8549 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008550 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 int argcount = 0; /* number of arguments found */
8552
8553 /*
8554 * Get the arguments.
8555 */
8556 argp = *arg;
8557 while (argcount < MAX_FUNC_ARGS)
8558 {
8559 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8560 if (*argp == ')' || *argp == ',' || *argp == NUL)
8561 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8563 {
8564 ret = FAIL;
8565 break;
8566 }
8567 ++argcount;
8568 if (*argp != ',')
8569 break;
8570 }
8571 if (*argp == ')')
8572 ++argp;
8573 else
8574 ret = FAIL;
8575
8576 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008578 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008580 {
8581 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008582 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008583 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008584 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586
8587 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
8590 *arg = skipwhite(argp);
8591 return ret;
8592}
8593
8594
8595/*
8596 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008597 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008598 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 */
8600 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008601call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008602 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008603 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008605 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008607 typval_T *argvars; /* vars for arguments, must have "argcount"
8608 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 linenr_T firstline; /* first line of range */
8610 linenr_T lastline; /* last line of range */
8611 int *doesrange; /* return: function handled range */
8612 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008613 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
8615 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616#define ERROR_UNKNOWN 0
8617#define ERROR_TOOMANY 1
8618#define ERROR_TOOFEW 2
8619#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008620#define ERROR_DICT 4
8621#define ERROR_NONE 5
8622#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 int error = ERROR_NONE;
8624 int i;
8625 int llen;
8626 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627#define FLEN_FIXED 40
8628 char_u fname_buf[FLEN_FIXED + 1];
8629 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008630 char_u *name;
8631
8632 /* Make a copy of the name, if it comes from a funcref variable it could
8633 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008634 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008635 if (name == NULL)
8636 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
8638 /*
8639 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8640 * Change <SNR>123_name() to K_SNR 123_name().
8641 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 llen = eval_fname_script(name);
8644 if (llen > 0)
8645 {
8646 fname_buf[0] = K_SPECIAL;
8647 fname_buf[1] = KS_EXTRA;
8648 fname_buf[2] = (int)KE_SNR;
8649 i = 3;
8650 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8651 {
8652 if (current_SID <= 0)
8653 error = ERROR_SCRIPT;
8654 else
8655 {
8656 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8657 i = (int)STRLEN(fname_buf);
8658 }
8659 }
8660 if (i + STRLEN(name + llen) < FLEN_FIXED)
8661 {
8662 STRCPY(fname_buf + i, name + llen);
8663 fname = fname_buf;
8664 }
8665 else
8666 {
8667 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8668 if (fname == NULL)
8669 error = ERROR_OTHER;
8670 else
8671 {
8672 mch_memmove(fname, fname_buf, (size_t)i);
8673 STRCPY(fname + i, name + llen);
8674 }
8675 }
8676 }
8677 else
8678 fname = name;
8679
8680 *doesrange = FALSE;
8681
8682
8683 /* execute the function if no errors detected and executing */
8684 if (evaluate && error == ERROR_NONE)
8685 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008686 char_u *rfname = fname;
8687
8688 /* Ignore "g:" before a function name. */
8689 if (fname[0] == 'g' && fname[1] == ':')
8690 rfname = fname + 2;
8691
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008692 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8693 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 error = ERROR_UNKNOWN;
8695
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008696 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {
8698 /*
8699 * User defined function.
8700 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008701 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008702
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008704 /* Trigger FuncUndefined event, may load the function. */
8705 if (fp == NULL
8706 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008707 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008708 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008710 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008711 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 }
8713#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008714 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008715 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008716 {
8717 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008718 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008719 }
8720
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 if (fp != NULL)
8722 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008723 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008725 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008727 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008729 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008730 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 else
8732 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008733 int did_save_redo = FALSE;
8734
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 /*
8736 * Call the user function.
8737 * Save and restore search patterns, script variables and
8738 * redo buffer.
8739 */
8740 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008741 if (!ins_compl_active())
8742 {
8743 saveRedobuff();
8744 did_save_redo = TRUE;
8745 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008746 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008748 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008749 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8750 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8751 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008752 /* Function was unreferenced while being used, free it
8753 * now. */
8754 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008755 if (did_save_redo)
8756 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 restore_search_patterns();
8758 error = ERROR_NONE;
8759 }
8760 }
8761 }
8762 else
8763 {
8764 /*
8765 * Find the function name in the table, call its implementation.
8766 */
8767 i = find_internal_func(fname);
8768 if (i >= 0)
8769 {
8770 if (argcount < functions[i].f_min_argc)
8771 error = ERROR_TOOFEW;
8772 else if (argcount > functions[i].f_max_argc)
8773 error = ERROR_TOOMANY;
8774 else
8775 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008776 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 error = ERROR_NONE;
8779 }
8780 }
8781 }
8782 /*
8783 * The function call (or "FuncUndefined" autocommand sequence) might
8784 * have been aborted by an error, an interrupt, or an explicitly thrown
8785 * exception that has not been caught so far. This situation can be
8786 * tested for by calling aborting(). For an error in an internal
8787 * function or for the "E132" error in call_user_func(), however, the
8788 * throw point at which the "force_abort" flag (temporarily reset by
8789 * emsg()) is normally updated has not been reached yet. We need to
8790 * update that flag first to make aborting() reliable.
8791 */
8792 update_force_abort();
8793 }
8794 if (error == ERROR_NONE)
8795 ret = OK;
8796
8797 /*
8798 * Report an error unless the argument evaluation or function call has been
8799 * cancelled due to an aborting error, an interrupt, or an exception.
8800 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008801 if (!aborting())
8802 {
8803 switch (error)
8804 {
8805 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008806 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008807 break;
8808 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008809 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008810 break;
8811 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008812 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008813 name);
8814 break;
8815 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008816 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008817 name);
8818 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008819 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008820 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008821 name);
8822 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008823 }
8824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 if (fname != name && fname != fname_buf)
8827 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008828 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829
8830 return ret;
8831}
8832
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008833/*
8834 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008835 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008836 */
8837 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008838emsg_funcname(ermsg, name)
8839 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008840 char_u *name;
8841{
8842 char_u *p;
8843
8844 if (*name == K_SPECIAL)
8845 p = concat_str((char_u *)"<SNR>", name + 3);
8846 else
8847 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008848 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008849 if (p != name)
8850 vim_free(p);
8851}
8852
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008853/*
8854 * Return TRUE for a non-zero Number and a non-empty String.
8855 */
8856 static int
8857non_zero_arg(argvars)
8858 typval_T *argvars;
8859{
8860 return ((argvars[0].v_type == VAR_NUMBER
8861 && argvars[0].vval.v_number != 0)
8862 || (argvars[0].v_type == VAR_STRING
8863 && argvars[0].vval.v_string != NULL
8864 && *argvars[0].vval.v_string != NUL));
8865}
8866
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867/*********************************************
8868 * Implementation of the built-in functions
8869 */
8870
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008871#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008872static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8873
8874/*
8875 * Get the float value of "argvars[0]" into "f".
8876 * Returns FAIL when the argument is not a Number or Float.
8877 */
8878 static int
8879get_float_arg(argvars, f)
8880 typval_T *argvars;
8881 float_T *f;
8882{
8883 if (argvars[0].v_type == VAR_FLOAT)
8884 {
8885 *f = argvars[0].vval.v_float;
8886 return OK;
8887 }
8888 if (argvars[0].v_type == VAR_NUMBER)
8889 {
8890 *f = (float_T)argvars[0].vval.v_number;
8891 return OK;
8892 }
8893 EMSG(_("E808: Number or Float required"));
8894 return FAIL;
8895}
8896
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008897/*
8898 * "abs(expr)" function
8899 */
8900 static void
8901f_abs(argvars, rettv)
8902 typval_T *argvars;
8903 typval_T *rettv;
8904{
8905 if (argvars[0].v_type == VAR_FLOAT)
8906 {
8907 rettv->v_type = VAR_FLOAT;
8908 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8909 }
8910 else
8911 {
8912 varnumber_T n;
8913 int error = FALSE;
8914
8915 n = get_tv_number_chk(&argvars[0], &error);
8916 if (error)
8917 rettv->vval.v_number = -1;
8918 else if (n > 0)
8919 rettv->vval.v_number = n;
8920 else
8921 rettv->vval.v_number = -n;
8922 }
8923}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008924
8925/*
8926 * "acos()" function
8927 */
8928 static void
8929f_acos(argvars, rettv)
8930 typval_T *argvars;
8931 typval_T *rettv;
8932{
8933 float_T f;
8934
8935 rettv->v_type = VAR_FLOAT;
8936 if (get_float_arg(argvars, &f) == OK)
8937 rettv->vval.v_float = acos(f);
8938 else
8939 rettv->vval.v_float = 0.0;
8940}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008941#endif
8942
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008944 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 */
8946 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008947f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008948 typval_T *argvars;
8949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950{
Bram Moolenaar33570922005-01-25 22:26:29 +00008951 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008956 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008957 && !tv_check_lock(l->lv_lock,
8958 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008959 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008961 }
8962 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008963 EMSG(_(e_listreq));
8964}
8965
8966/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008967 * "and(expr, expr)" function
8968 */
8969 static void
8970f_and(argvars, rettv)
8971 typval_T *argvars;
8972 typval_T *rettv;
8973{
8974 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8975 & get_tv_number_chk(&argvars[1], NULL);
8976}
8977
8978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008979 * "append(lnum, string/list)" function
8980 */
8981 static void
8982f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008983 typval_T *argvars;
8984 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008985{
8986 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 list_T *l = NULL;
8989 listitem_T *li = NULL;
8990 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008991 long added = 0;
8992
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008993 /* When coming here from Insert mode, sync undo, so that this can be
8994 * undone separately from what was previously inserted. */
8995 if (u_sync_once == 2)
8996 {
8997 u_sync_once = 1; /* notify that u_sync() was called */
8998 u_sync(TRUE);
8999 }
9000
Bram Moolenaar0d660222005-01-07 21:51:51 +00009001 lnum = get_tv_lnum(argvars);
9002 if (lnum >= 0
9003 && lnum <= curbuf->b_ml.ml_line_count
9004 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009006 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009007 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009008 l = argvars[1].vval.v_list;
9009 if (l == NULL)
9010 return;
9011 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009013 for (;;)
9014 {
9015 if (l == NULL)
9016 tv = &argvars[1]; /* append a string */
9017 else if (li == NULL)
9018 break; /* end of list */
9019 else
9020 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021 line = get_tv_string_chk(tv);
9022 if (line == NULL) /* type error */
9023 {
9024 rettv->vval.v_number = 1; /* Failed */
9025 break;
9026 }
9027 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009028 ++added;
9029 if (l == NULL)
9030 break;
9031 li = li->li_next;
9032 }
9033
9034 appended_lines_mark(lnum, added);
9035 if (curwin->w_cursor.lnum > lnum)
9036 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009038 else
9039 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040}
9041
9042/*
9043 * "argc()" function
9044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009046f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009047 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051}
9052
9053/*
9054 * "argidx()" function
9055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009058 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062}
9063
9064/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009065 * "arglistid()" function
9066 */
9067 static void
9068f_arglistid(argvars, rettv)
9069 typval_T *argvars UNUSED;
9070 typval_T *rettv;
9071{
9072 win_T *wp;
9073 tabpage_T *tp = NULL;
9074 long n;
9075
9076 rettv->vval.v_number = -1;
9077 if (argvars[0].v_type != VAR_UNKNOWN)
9078 {
9079 if (argvars[1].v_type != VAR_UNKNOWN)
9080 {
9081 n = get_tv_number(&argvars[1]);
9082 if (n >= 0)
9083 tp = find_tabpage(n);
9084 }
9085 else
9086 tp = curtab;
9087
9088 if (tp != NULL)
9089 {
9090 wp = find_win_by_nr(&argvars[0], tp);
9091 if (wp != NULL)
9092 rettv->vval.v_number = wp->w_alist->id;
9093 }
9094 }
9095 else
9096 rettv->vval.v_number = curwin->w_alist->id;
9097}
9098
9099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 * "argv(nr)" function
9101 */
9102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009104 typval_T *argvars;
9105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 int idx;
9108
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009109 if (argvars[0].v_type != VAR_UNKNOWN)
9110 {
9111 idx = get_tv_number_chk(&argvars[0], NULL);
9112 if (idx >= 0 && idx < ARGCOUNT)
9113 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9114 else
9115 rettv->vval.v_string = NULL;
9116 rettv->v_type = VAR_STRING;
9117 }
9118 else if (rettv_list_alloc(rettv) == OK)
9119 for (idx = 0; idx < ARGCOUNT; ++idx)
9120 list_append_string(rettv->vval.v_list,
9121 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122}
9123
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009124#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009125/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009126 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009127 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009128 static void
9129f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009130 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009131 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009132{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009133 float_T f;
9134
9135 rettv->v_type = VAR_FLOAT;
9136 if (get_float_arg(argvars, &f) == OK)
9137 rettv->vval.v_float = asin(f);
9138 else
9139 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009140}
9141
9142/*
9143 * "atan()" function
9144 */
9145 static void
9146f_atan(argvars, rettv)
9147 typval_T *argvars;
9148 typval_T *rettv;
9149{
9150 float_T f;
9151
9152 rettv->v_type = VAR_FLOAT;
9153 if (get_float_arg(argvars, &f) == OK)
9154 rettv->vval.v_float = atan(f);
9155 else
9156 rettv->vval.v_float = 0.0;
9157}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009158
9159/*
9160 * "atan2()" function
9161 */
9162 static void
9163f_atan2(argvars, rettv)
9164 typval_T *argvars;
9165 typval_T *rettv;
9166{
9167 float_T fx, fy;
9168
9169 rettv->v_type = VAR_FLOAT;
9170 if (get_float_arg(argvars, &fx) == OK
9171 && get_float_arg(&argvars[1], &fy) == OK)
9172 rettv->vval.v_float = atan2(fx, fy);
9173 else
9174 rettv->vval.v_float = 0.0;
9175}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009176#endif
9177
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178/*
9179 * "browse(save, title, initdir, default)" function
9180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009183 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185{
9186#ifdef FEAT_BROWSE
9187 int save;
9188 char_u *title;
9189 char_u *initdir;
9190 char_u *defname;
9191 char_u buf[NUMBUFLEN];
9192 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009193 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009195 save = get_tv_number_chk(&argvars[0], &error);
9196 title = get_tv_string_chk(&argvars[1]);
9197 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9198 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009200 if (error || title == NULL || initdir == NULL || defname == NULL)
9201 rettv->vval.v_string = NULL;
9202 else
9203 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009204 do_browse(save ? BROWSE_SAVE : 0,
9205 title, defname, NULL, initdir, NULL, curbuf);
9206#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009208#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009210}
9211
9212/*
9213 * "browsedir(title, initdir)" function
9214 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009217 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009218 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009219{
9220#ifdef FEAT_BROWSE
9221 char_u *title;
9222 char_u *initdir;
9223 char_u buf[NUMBUFLEN];
9224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009225 title = get_tv_string_chk(&argvars[0]);
9226 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009227
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009228 if (title == NULL || initdir == NULL)
9229 rettv->vval.v_string = NULL;
9230 else
9231 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009232 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009234 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009236 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237}
9238
Bram Moolenaar33570922005-01-25 22:26:29 +00009239static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009240
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241/*
9242 * Find a buffer by number or exact name.
9243 */
9244 static buf_T *
9245find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009246 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247{
9248 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009250 if (avar->v_type == VAR_NUMBER)
9251 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009252 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009254 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009255 if (buf == NULL)
9256 {
9257 /* No full path name match, try a match with a URL or a "nofile"
9258 * buffer, these don't use the full path. */
9259 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9260 if (buf->b_fname != NULL
9261 && (path_with_url(buf->b_fname)
9262#ifdef FEAT_QUICKFIX
9263 || bt_nofile(buf)
9264#endif
9265 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009266 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009267 break;
9268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 }
9270 return buf;
9271}
9272
9273/*
9274 * "bufexists(expr)" function
9275 */
9276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009278 typval_T *argvars;
9279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009281 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282}
9283
9284/*
9285 * "buflisted(expr)" function
9286 */
9287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009288f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009289 typval_T *argvars;
9290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291{
9292 buf_T *buf;
9293
9294 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009295 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296}
9297
9298/*
9299 * "bufloaded(expr)" function
9300 */
9301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009302f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009303 typval_T *argvars;
9304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305{
9306 buf_T *buf;
9307
9308 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310}
9311
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009312static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009313
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314/*
9315 * Get buffer by number or pattern.
9316 */
9317 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009318get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009320 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009322 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 int save_magic;
9324 char_u *save_cpo;
9325 buf_T *buf;
9326
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327 if (tv->v_type == VAR_NUMBER)
9328 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009329 if (tv->v_type != VAR_STRING)
9330 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 if (name == NULL || *name == NUL)
9332 return curbuf;
9333 if (name[0] == '$' && name[1] == NUL)
9334 return lastbuf;
9335
9336 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9337 save_magic = p_magic;
9338 p_magic = TRUE;
9339 save_cpo = p_cpo;
9340 p_cpo = (char_u *)"";
9341
9342 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009343 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344
9345 p_magic = save_magic;
9346 p_cpo = save_cpo;
9347
9348 /* If not found, try expanding the name, like done for bufexists(). */
9349 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351
9352 return buf;
9353}
9354
9355/*
9356 * "bufname(expr)" function
9357 */
9358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009359f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009360 typval_T *argvars;
9361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362{
9363 buf_T *buf;
9364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009365 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009367 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 --emsg_off;
9374}
9375
9376/*
9377 * "bufnr(expr)" function
9378 */
9379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009381 typval_T *argvars;
9382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009385 int error = FALSE;
9386 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009390 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009391 --emsg_off;
9392
9393 /* If the buffer isn't found and the second argument is not zero create a
9394 * new buffer. */
9395 if (buf == NULL
9396 && argvars[1].v_type != VAR_UNKNOWN
9397 && get_tv_number_chk(&argvars[1], &error) != 0
9398 && !error
9399 && (name = get_tv_string_chk(&argvars[0])) != NULL
9400 && !error)
9401 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9402
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407}
9408
9409/*
9410 * "bufwinnr(nr)" function
9411 */
9412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009414 typval_T *argvars;
9415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416{
9417#ifdef FEAT_WINDOWS
9418 win_T *wp;
9419 int winnr = 0;
9420#endif
9421 buf_T *buf;
9422
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009425 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426#ifdef FEAT_WINDOWS
9427 for (wp = firstwin; wp; wp = wp->w_next)
9428 {
9429 ++winnr;
9430 if (wp->w_buffer == buf)
9431 break;
9432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436#endif
9437 --emsg_off;
9438}
9439
9440/*
9441 * "byte2line(byte)" function
9442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009445 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450#else
9451 long boff = 0;
9452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 (linenr_T)0, &boff);
9459#endif
9460}
9461
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009462 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009463byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009464 typval_T *argvars;
9465 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009466 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009467{
9468#ifdef FEAT_MBYTE
9469 char_u *t;
9470#endif
9471 char_u *str;
9472 long idx;
9473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 str = get_tv_string_chk(&argvars[0]);
9475 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009477 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009478 return;
9479
9480#ifdef FEAT_MBYTE
9481 t = str;
9482 for ( ; idx > 0; idx--)
9483 {
9484 if (*t == NUL) /* EOL reached */
9485 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009486 if (enc_utf8 && comp)
9487 t += utf_ptr2len(t);
9488 else
9489 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009490 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009491 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009492#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009493 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009494 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009495#endif
9496}
9497
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009498/*
9499 * "byteidx()" function
9500 */
9501 static void
9502f_byteidx(argvars, rettv)
9503 typval_T *argvars;
9504 typval_T *rettv;
9505{
9506 byteidx(argvars, rettv, FALSE);
9507}
9508
9509/*
9510 * "byteidxcomp()" function
9511 */
9512 static void
9513f_byteidxcomp(argvars, rettv)
9514 typval_T *argvars;
9515 typval_T *rettv;
9516{
9517 byteidx(argvars, rettv, TRUE);
9518}
9519
Bram Moolenaardb913952012-06-29 12:54:53 +02009520 int
9521func_call(name, args, selfdict, rettv)
9522 char_u *name;
9523 typval_T *args;
9524 dict_T *selfdict;
9525 typval_T *rettv;
9526{
9527 listitem_T *item;
9528 typval_T argv[MAX_FUNC_ARGS + 1];
9529 int argc = 0;
9530 int dummy;
9531 int r = 0;
9532
9533 for (item = args->vval.v_list->lv_first; item != NULL;
9534 item = item->li_next)
9535 {
9536 if (argc == MAX_FUNC_ARGS)
9537 {
9538 EMSG(_("E699: Too many arguments"));
9539 break;
9540 }
9541 /* Make a copy of each argument. This is needed to be able to set
9542 * v_lock to VAR_FIXED in the copy without changing the original list.
9543 */
9544 copy_tv(&item->li_tv, &argv[argc++]);
9545 }
9546
9547 if (item == NULL)
9548 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9549 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9550 &dummy, TRUE, selfdict);
9551
9552 /* Free the arguments. */
9553 while (argc > 0)
9554 clear_tv(&argv[--argc]);
9555
9556 return r;
9557}
9558
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009559/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009560 * "call(func, arglist)" function
9561 */
9562 static void
9563f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009564 typval_T *argvars;
9565 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009566{
9567 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009568 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009569
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009570 if (argvars[1].v_type != VAR_LIST)
9571 {
9572 EMSG(_(e_listreq));
9573 return;
9574 }
9575 if (argvars[1].vval.v_list == NULL)
9576 return;
9577
9578 if (argvars[0].v_type == VAR_FUNC)
9579 func = argvars[0].vval.v_string;
9580 else
9581 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009582 if (*func == NUL)
9583 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009584
Bram Moolenaare9a41262005-01-15 22:18:47 +00009585 if (argvars[2].v_type != VAR_UNKNOWN)
9586 {
9587 if (argvars[2].v_type != VAR_DICT)
9588 {
9589 EMSG(_(e_dictreq));
9590 return;
9591 }
9592 selfdict = argvars[2].vval.v_dict;
9593 }
9594
Bram Moolenaardb913952012-06-29 12:54:53 +02009595 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009596}
9597
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009598#ifdef FEAT_FLOAT
9599/*
9600 * "ceil({float})" function
9601 */
9602 static void
9603f_ceil(argvars, rettv)
9604 typval_T *argvars;
9605 typval_T *rettv;
9606{
9607 float_T f;
9608
9609 rettv->v_type = VAR_FLOAT;
9610 if (get_float_arg(argvars, &f) == OK)
9611 rettv->vval.v_float = ceil(f);
9612 else
9613 rettv->vval.v_float = 0.0;
9614}
9615#endif
9616
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009617/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009618 * "changenr()" function
9619 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009620 static void
9621f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009622 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009623 typval_T *rettv;
9624{
9625 rettv->vval.v_number = curbuf->b_u_seq_cur;
9626}
9627
9628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 * "char2nr(string)" function
9630 */
9631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009633 typval_T *argvars;
9634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635{
9636#ifdef FEAT_MBYTE
9637 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009638 {
9639 int utf8 = 0;
9640
9641 if (argvars[1].v_type != VAR_UNKNOWN)
9642 utf8 = get_tv_number_chk(&argvars[1], NULL);
9643
9644 if (utf8)
9645 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9646 else
9647 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 else
9650#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652}
9653
9654/*
9655 * "cindent(lnum)" function
9656 */
9657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009659 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662#ifdef FEAT_CINDENT
9663 pos_T pos;
9664 linenr_T lnum;
9665
9666 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009667 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9669 {
9670 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 curwin->w_cursor = pos;
9673 }
9674 else
9675#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677}
9678
9679/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009680 * "clearmatches()" function
9681 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009682 static void
9683f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009684 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009685 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009686{
9687#ifdef FEAT_SEARCH_EXTRA
9688 clear_matches(curwin);
9689#endif
9690}
9691
9692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 * "col(string)" function
9694 */
9695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009697 typval_T *argvars;
9698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
9700 colnr_T col = 0;
9701 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009702 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009704 fp = var2fpos(&argvars[0], FALSE, &fnum);
9705 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 {
9707 if (fp->col == MAXCOL)
9708 {
9709 /* '> can be MAXCOL, get the length of the line then */
9710 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009711 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 else
9713 col = MAXCOL;
9714 }
9715 else
9716 {
9717 col = fp->col + 1;
9718#ifdef FEAT_VIRTUALEDIT
9719 /* col(".") when the cursor is on the NUL at the end of the line
9720 * because of "coladd" can be seen as an extra column. */
9721 if (virtual_active() && fp == &curwin->w_cursor)
9722 {
9723 char_u *p = ml_get_cursor();
9724
9725 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9726 curwin->w_virtcol - curwin->w_cursor.coladd))
9727 {
9728# ifdef FEAT_MBYTE
9729 int l;
9730
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009731 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 col += l;
9733# else
9734 if (*p != NUL && p[1] == NUL)
9735 ++col;
9736# endif
9737 }
9738 }
9739#endif
9740 }
9741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743}
9744
Bram Moolenaar572cb562005-08-05 21:35:02 +00009745#if defined(FEAT_INS_EXPAND)
9746/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009747 * "complete()" function
9748 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009749 static void
9750f_complete(argvars, rettv)
9751 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009752 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009753{
9754 int startcol;
9755
9756 if ((State & INSERT) == 0)
9757 {
9758 EMSG(_("E785: complete() can only be used in Insert mode"));
9759 return;
9760 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009761
9762 /* Check for undo allowed here, because if something was already inserted
9763 * the line was already saved for undo and this check isn't done. */
9764 if (!undo_allowed())
9765 return;
9766
Bram Moolenaarade00832006-03-10 21:46:58 +00009767 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9768 {
9769 EMSG(_(e_invarg));
9770 return;
9771 }
9772
9773 startcol = get_tv_number_chk(&argvars[0], NULL);
9774 if (startcol <= 0)
9775 return;
9776
9777 set_completion(startcol - 1, argvars[1].vval.v_list);
9778}
9779
9780/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009781 * "complete_add()" function
9782 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009783 static void
9784f_complete_add(argvars, rettv)
9785 typval_T *argvars;
9786 typval_T *rettv;
9787{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009788 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009789}
9790
9791/*
9792 * "complete_check()" function
9793 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009794 static void
9795f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009796 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009797 typval_T *rettv;
9798{
9799 int saved = RedrawingDisabled;
9800
9801 RedrawingDisabled = 0;
9802 ins_compl_check_keys(0);
9803 rettv->vval.v_number = compl_interrupted;
9804 RedrawingDisabled = saved;
9805}
9806#endif
9807
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808/*
9809 * "confirm(message, buttons[, default [, type]])" function
9810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009813 typval_T *argvars UNUSED;
9814 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815{
9816#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9817 char_u *message;
9818 char_u *buttons = NULL;
9819 char_u buf[NUMBUFLEN];
9820 char_u buf2[NUMBUFLEN];
9821 int def = 1;
9822 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009823 char_u *typestr;
9824 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 message = get_tv_string_chk(&argvars[0]);
9827 if (message == NULL)
9828 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009829 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009831 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9832 if (buttons == NULL)
9833 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009834 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009836 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009837 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9840 if (typestr == NULL)
9841 error = TRUE;
9842 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009844 switch (TOUPPER_ASC(*typestr))
9845 {
9846 case 'E': type = VIM_ERROR; break;
9847 case 'Q': type = VIM_QUESTION; break;
9848 case 'I': type = VIM_INFO; break;
9849 case 'W': type = VIM_WARNING; break;
9850 case 'G': type = VIM_GENERIC; break;
9851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 }
9853 }
9854 }
9855 }
9856
9857 if (buttons == NULL || *buttons == NUL)
9858 buttons = (char_u *)_("&Ok");
9859
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009860 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009861 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009862 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863#endif
9864}
9865
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009866/*
9867 * "copy()" function
9868 */
9869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009871 typval_T *argvars;
9872 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009873{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009874 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009875}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009877#ifdef FEAT_FLOAT
9878/*
9879 * "cos()" function
9880 */
9881 static void
9882f_cos(argvars, rettv)
9883 typval_T *argvars;
9884 typval_T *rettv;
9885{
9886 float_T f;
9887
9888 rettv->v_type = VAR_FLOAT;
9889 if (get_float_arg(argvars, &f) == OK)
9890 rettv->vval.v_float = cos(f);
9891 else
9892 rettv->vval.v_float = 0.0;
9893}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009894
9895/*
9896 * "cosh()" function
9897 */
9898 static void
9899f_cosh(argvars, rettv)
9900 typval_T *argvars;
9901 typval_T *rettv;
9902{
9903 float_T f;
9904
9905 rettv->v_type = VAR_FLOAT;
9906 if (get_float_arg(argvars, &f) == OK)
9907 rettv->vval.v_float = cosh(f);
9908 else
9909 rettv->vval.v_float = 0.0;
9910}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009911#endif
9912
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009914 * "count()" function
9915 */
9916 static void
9917f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009918 typval_T *argvars;
9919 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009920{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009921 long n = 0;
9922 int ic = FALSE;
9923
Bram Moolenaare9a41262005-01-15 22:18:47 +00009924 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009926 listitem_T *li;
9927 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009929
Bram Moolenaare9a41262005-01-15 22:18:47 +00009930 if ((l = argvars[0].vval.v_list) != NULL)
9931 {
9932 li = l->lv_first;
9933 if (argvars[2].v_type != VAR_UNKNOWN)
9934 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 int error = FALSE;
9936
9937 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009938 if (argvars[3].v_type != VAR_UNKNOWN)
9939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009940 idx = get_tv_number_chk(&argvars[3], &error);
9941 if (!error)
9942 {
9943 li = list_find(l, idx);
9944 if (li == NULL)
9945 EMSGN(_(e_listidx), idx);
9946 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009947 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 if (error)
9949 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009950 }
9951
9952 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009953 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009954 ++n;
9955 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009956 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009957 else if (argvars[0].v_type == VAR_DICT)
9958 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009959 int todo;
9960 dict_T *d;
9961 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009962
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009963 if ((d = argvars[0].vval.v_dict) != NULL)
9964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 int error = FALSE;
9966
Bram Moolenaare9a41262005-01-15 22:18:47 +00009967 if (argvars[2].v_type != VAR_UNKNOWN)
9968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009969 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 if (argvars[3].v_type != VAR_UNKNOWN)
9971 EMSG(_(e_invarg));
9972 }
9973
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009974 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009975 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009976 {
9977 if (!HASHITEM_EMPTY(hi))
9978 {
9979 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009980 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009981 ++n;
9982 }
9983 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009984 }
9985 }
9986 else
9987 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009988 rettv->vval.v_number = n;
9989}
9990
9991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9993 *
9994 * Checks the existence of a cscope connection.
9995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009998 typval_T *argvars UNUSED;
9999 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000{
10001#ifdef FEAT_CSCOPE
10002 int num = 0;
10003 char_u *dbpath = NULL;
10004 char_u *prepend = NULL;
10005 char_u buf[NUMBUFLEN];
10006
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010007 if (argvars[0].v_type != VAR_UNKNOWN
10008 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010 num = (int)get_tv_number(&argvars[0]);
10011 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010012 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 }
10015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017#endif
10018}
10019
10020/*
10021 * "cursor(lnum, col)" function
10022 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010023 * Moves the cursor to the specified line and column.
10024 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 typval_T *argvars;
10029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
10031 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010032#ifdef FEAT_VIRTUALEDIT
10033 long coladd = 0;
10034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010036 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010037 if (argvars[1].v_type == VAR_UNKNOWN)
10038 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010039 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010040 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010041
Bram Moolenaar493c1782014-05-28 14:34:46 +020010042 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010043 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010044 line = pos.lnum;
10045 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010046#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010047 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010048#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010049 if (curswant >= 0)
10050 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010051 }
10052 else
10053 {
10054 line = get_tv_lnum(argvars);
10055 col = get_tv_number_chk(&argvars[1], NULL);
10056#ifdef FEAT_VIRTUALEDIT
10057 if (argvars[2].v_type != VAR_UNKNOWN)
10058 coladd = get_tv_number_chk(&argvars[2], NULL);
10059#endif
10060 }
10061 if (line < 0 || col < 0
10062#ifdef FEAT_VIRTUALEDIT
10063 || coladd < 0
10064#endif
10065 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010066 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 if (line > 0)
10068 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (col > 0)
10070 curwin->w_cursor.col = col - 1;
10071#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010072 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073#endif
10074
10075 /* Make sure the cursor is in a valid position. */
10076 check_cursor();
10077#ifdef FEAT_MBYTE
10078 /* Correct cursor for multi-byte character. */
10079 if (has_mbyte)
10080 mb_adjust_cursor();
10081#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010082
10083 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010084 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085}
10086
10087/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010088 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 */
10090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010092 typval_T *argvars;
10093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010095 int noref = 0;
10096
10097 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010099 if (noref < 0 || noref > 1)
10100 EMSG(_(e_invarg));
10101 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010102 {
10103 current_copyID += COPYID_INC;
10104 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106}
10107
10108/*
10109 * "delete()" function
10110 */
10111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010113 typval_T *argvars;
10114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115{
10116 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120}
10121
10122/*
10123 * "did_filetype()" function
10124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010126f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010127 typval_T *argvars UNUSED;
10128 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129{
10130#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#endif
10133}
10134
10135/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010136 * "diff_filler()" function
10137 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010140 typval_T *argvars UNUSED;
10141 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010142{
10143#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010145#endif
10146}
10147
10148/*
10149 * "diff_hlID()" function
10150 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010153 typval_T *argvars UNUSED;
10154 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010155{
10156#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010158 static linenr_T prev_lnum = 0;
10159 static int changedtick = 0;
10160 static int fnum = 0;
10161 static int change_start = 0;
10162 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010163 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010164 int filler_lines;
10165 int col;
10166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010167 if (lnum < 0) /* ignore type error in {lnum} arg */
10168 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010169 if (lnum != prev_lnum
10170 || changedtick != curbuf->b_changedtick
10171 || fnum != curbuf->b_fnum)
10172 {
10173 /* New line, buffer, change: need to get the values. */
10174 filler_lines = diff_check(curwin, lnum);
10175 if (filler_lines < 0)
10176 {
10177 if (filler_lines == -1)
10178 {
10179 change_start = MAXCOL;
10180 change_end = -1;
10181 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10182 hlID = HLF_ADD; /* added line */
10183 else
10184 hlID = HLF_CHD; /* changed line */
10185 }
10186 else
10187 hlID = HLF_ADD; /* added line */
10188 }
10189 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010190 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010191 prev_lnum = lnum;
10192 changedtick = curbuf->b_changedtick;
10193 fnum = curbuf->b_fnum;
10194 }
10195
10196 if (hlID == HLF_CHD || hlID == HLF_TXD)
10197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010199 if (col >= change_start && col <= change_end)
10200 hlID = HLF_TXD; /* changed text */
10201 else
10202 hlID = HLF_CHD; /* changed line */
10203 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010204 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010205#endif
10206}
10207
10208/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010209 * "empty({expr})" function
10210 */
10211 static void
10212f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010213 typval_T *argvars;
10214 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010215{
10216 int n;
10217
10218 switch (argvars[0].v_type)
10219 {
10220 case VAR_STRING:
10221 case VAR_FUNC:
10222 n = argvars[0].vval.v_string == NULL
10223 || *argvars[0].vval.v_string == NUL;
10224 break;
10225 case VAR_NUMBER:
10226 n = argvars[0].vval.v_number == 0;
10227 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010228#ifdef FEAT_FLOAT
10229 case VAR_FLOAT:
10230 n = argvars[0].vval.v_float == 0.0;
10231 break;
10232#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010233 case VAR_LIST:
10234 n = argvars[0].vval.v_list == NULL
10235 || argvars[0].vval.v_list->lv_first == NULL;
10236 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010237 case VAR_DICT:
10238 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010239 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010241 default:
10242 EMSG2(_(e_intern2), "f_empty()");
10243 n = 0;
10244 }
10245
10246 rettv->vval.v_number = n;
10247}
10248
10249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 * "escape({string}, {chars})" function
10251 */
10252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010253f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010254 typval_T *argvars;
10255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256{
10257 char_u buf[NUMBUFLEN];
10258
Bram Moolenaar758711c2005-02-02 23:11:38 +000010259 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10260 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010261 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262}
10263
10264/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010265 * "eval()" function
10266 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010267 static void
10268f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010269 typval_T *argvars;
10270 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010271{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010272 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 s = get_tv_string_chk(&argvars[0]);
10275 if (s != NULL)
10276 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010277
Bram Moolenaar615b9972015-01-14 17:15:05 +010010278 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010279 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10280 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010281 if (p != NULL && !aborting())
10282 EMSG2(_(e_invexpr2), p);
10283 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010284 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010285 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010286 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010287 else if (*s != NUL)
10288 EMSG(_(e_trailing));
10289}
10290
10291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 * "eventhandler()" function
10293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010296 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300}
10301
10302/*
10303 * "executable()" function
10304 */
10305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010306f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010307 typval_T *argvars;
10308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010310 char_u *name = get_tv_string(&argvars[0]);
10311
10312 /* Check in $PATH and also check directly if there is a directory name. */
10313 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10314 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010315}
10316
10317/*
10318 * "exepath()" function
10319 */
10320 static void
10321f_exepath(argvars, rettv)
10322 typval_T *argvars;
10323 typval_T *rettv;
10324{
10325 char_u *p = NULL;
10326
Bram Moolenaarb5971142015-03-21 17:32:19 +010010327 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010328 rettv->v_type = VAR_STRING;
10329 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330}
10331
10332/*
10333 * "exists()" function
10334 */
10335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010336f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010337 typval_T *argvars;
10338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339{
10340 char_u *p;
10341 char_u *name;
10342 int n = FALSE;
10343 int len = 0;
10344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010345 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 if (*p == '$') /* environment variable */
10347 {
10348 /* first try "normal" environment variables (fast) */
10349 if (mch_getenv(p + 1) != NULL)
10350 n = TRUE;
10351 else
10352 {
10353 /* try expanding things like $VIM and ${HOME} */
10354 p = expand_env_save(p);
10355 if (p != NULL && *p != '$')
10356 n = TRUE;
10357 vim_free(p);
10358 }
10359 }
10360 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010362 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010363 if (*skipwhite(p) != NUL)
10364 n = FALSE; /* trailing garbage */
10365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 else if (*p == '*') /* internal or user defined function */
10367 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010368 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 }
10370 else if (*p == ':')
10371 {
10372 n = cmd_exists(p + 1);
10373 }
10374 else if (*p == '#')
10375 {
10376#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010377 if (p[1] == '#')
10378 n = autocmd_supported(p + 2);
10379 else
10380 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381#endif
10382 }
10383 else /* internal variable */
10384 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010385 char_u *tofree;
10386 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010388 /* get_name_len() takes care of expanding curly braces */
10389 name = p;
10390 len = get_name_len(&p, &tofree, TRUE, FALSE);
10391 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010393 if (tofree != NULL)
10394 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010395 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010396 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010398 /* handle d.key, l[idx], f(expr) */
10399 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10400 if (n)
10401 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 }
10403 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010404 if (*p != NUL)
10405 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010407 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 }
10409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411}
10412
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010413#ifdef FEAT_FLOAT
10414/*
10415 * "exp()" function
10416 */
10417 static void
10418f_exp(argvars, rettv)
10419 typval_T *argvars;
10420 typval_T *rettv;
10421{
10422 float_T f;
10423
10424 rettv->v_type = VAR_FLOAT;
10425 if (get_float_arg(argvars, &f) == OK)
10426 rettv->vval.v_float = exp(f);
10427 else
10428 rettv->vval.v_float = 0.0;
10429}
10430#endif
10431
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432/*
10433 * "expand()" function
10434 */
10435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010437 typval_T *argvars;
10438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439{
10440 char_u *s;
10441 int len;
10442 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010443 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010446 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010448 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010449 if (argvars[1].v_type != VAR_UNKNOWN
10450 && argvars[2].v_type != VAR_UNKNOWN
10451 && get_tv_number_chk(&argvars[2], &error)
10452 && !error)
10453 {
10454 rettv->v_type = VAR_LIST;
10455 rettv->vval.v_list = NULL;
10456 }
10457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010458 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 if (*s == '%' || *s == '#' || *s == '<')
10460 {
10461 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010462 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010464 if (rettv->v_type == VAR_LIST)
10465 {
10466 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10467 list_append_string(rettv->vval.v_list, result, -1);
10468 else
10469 vim_free(result);
10470 }
10471 else
10472 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 }
10474 else
10475 {
10476 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010477 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010478 if (argvars[1].v_type != VAR_UNKNOWN
10479 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010480 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 if (!error)
10482 {
10483 ExpandInit(&xpc);
10484 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010485 if (p_wic)
10486 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010487 if (rettv->v_type == VAR_STRING)
10488 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10489 options, WILD_ALL);
10490 else if (rettv_list_alloc(rettv) != FAIL)
10491 {
10492 int i;
10493
10494 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10495 for (i = 0; i < xpc.xp_numfiles; i++)
10496 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10497 ExpandCleanup(&xpc);
10498 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010499 }
10500 else
10501 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 }
10503}
10504
10505/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010506 * Go over all entries in "d2" and add them to "d1".
10507 * When "action" is "error" then a duplicate key is an error.
10508 * When "action" is "force" then a duplicate key is overwritten.
10509 * Otherwise duplicate keys are ignored ("action" is "keep").
10510 */
10511 void
10512dict_extend(d1, d2, action)
10513 dict_T *d1;
10514 dict_T *d2;
10515 char_u *action;
10516{
10517 dictitem_T *di1;
10518 hashitem_T *hi2;
10519 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010520 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010521
10522 todo = (int)d2->dv_hashtab.ht_used;
10523 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10524 {
10525 if (!HASHITEM_EMPTY(hi2))
10526 {
10527 --todo;
10528 di1 = dict_find(d1, hi2->hi_key, -1);
10529 if (d1->dv_scope != 0)
10530 {
10531 /* Disallow replacing a builtin function in l: and g:.
10532 * Check the key to be valid when adding to any
10533 * scope. */
10534 if (d1->dv_scope == VAR_DEF_SCOPE
10535 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10536 && var_check_func_name(hi2->hi_key,
10537 di1 == NULL))
10538 break;
10539 if (!valid_varname(hi2->hi_key))
10540 break;
10541 }
10542 if (di1 == NULL)
10543 {
10544 di1 = dictitem_copy(HI2DI(hi2));
10545 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10546 dictitem_free(di1);
10547 }
10548 else if (*action == 'e')
10549 {
10550 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10551 break;
10552 }
10553 else if (*action == 'f' && HI2DI(hi2) != di1)
10554 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010555 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10556 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010557 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010558 clear_tv(&di1->di_tv);
10559 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10560 }
10561 }
10562 }
10563}
10564
10565/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010566 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010567 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010568 */
10569 static void
10570f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010571 typval_T *argvars;
10572 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010573{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010574 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010575
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010577 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010578 list_T *l1, *l2;
10579 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010581 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010582
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 l1 = argvars[0].vval.v_list;
10584 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010585 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010586 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 {
10588 if (argvars[2].v_type != VAR_UNKNOWN)
10589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010590 before = get_tv_number_chk(&argvars[2], &error);
10591 if (error)
10592 return; /* type error; errmsg already given */
10593
Bram Moolenaar758711c2005-02-02 23:11:38 +000010594 if (before == l1->lv_len)
10595 item = NULL;
10596 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010598 item = list_find(l1, before);
10599 if (item == NULL)
10600 {
10601 EMSGN(_(e_listidx), before);
10602 return;
10603 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010604 }
10605 }
10606 else
10607 item = NULL;
10608 list_extend(l1, l2, item);
10609
Bram Moolenaare9a41262005-01-15 22:18:47 +000010610 copy_tv(&argvars[0], rettv);
10611 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010612 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10614 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010615 dict_T *d1, *d2;
10616 char_u *action;
10617 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010618
10619 d1 = argvars[0].vval.v_dict;
10620 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010621 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010622 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010623 {
10624 /* Check the third argument. */
10625 if (argvars[2].v_type != VAR_UNKNOWN)
10626 {
10627 static char *(av[]) = {"keep", "force", "error"};
10628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 action = get_tv_string_chk(&argvars[2]);
10630 if (action == NULL)
10631 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010632 for (i = 0; i < 3; ++i)
10633 if (STRCMP(action, av[i]) == 0)
10634 break;
10635 if (i == 3)
10636 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010637 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638 return;
10639 }
10640 }
10641 else
10642 action = (char_u *)"force";
10643
Bram Moolenaara9922d62013-05-30 13:01:18 +020010644 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010645
Bram Moolenaare9a41262005-01-15 22:18:47 +000010646 copy_tv(&argvars[0], rettv);
10647 }
10648 }
10649 else
10650 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010651}
10652
10653/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010654 * "feedkeys()" function
10655 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010656 static void
10657f_feedkeys(argvars, rettv)
10658 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010659 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010660{
10661 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010662 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010663 char_u *keys, *flags;
10664 char_u nbuf[NUMBUFLEN];
10665 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010666 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010667
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010668 /* This is not allowed in the sandbox. If the commands would still be
10669 * executed in the sandbox it would be OK, but it probably happens later,
10670 * when "sandbox" is no longer set. */
10671 if (check_secure())
10672 return;
10673
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010674 keys = get_tv_string(&argvars[0]);
10675 if (*keys != NUL)
10676 {
10677 if (argvars[1].v_type != VAR_UNKNOWN)
10678 {
10679 flags = get_tv_string_buf(&argvars[1], nbuf);
10680 for ( ; *flags != NUL; ++flags)
10681 {
10682 switch (*flags)
10683 {
10684 case 'n': remap = FALSE; break;
10685 case 'm': remap = TRUE; break;
10686 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010687 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010688 }
10689 }
10690 }
10691
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010692 /* Need to escape K_SPECIAL and CSI before putting the string in the
10693 * typeahead buffer. */
10694 keys_esc = vim_strsave_escape_csi(keys);
10695 if (keys_esc != NULL)
10696 {
10697 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010698 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010699 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010700 if (vgetc_busy)
10701 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010702 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010703 }
10704}
10705
10706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 * "filereadable()" function
10708 */
10709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010711 typval_T *argvars;
10712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010714 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 char_u *p;
10716 int n;
10717
Bram Moolenaarc236c162008-07-13 17:41:49 +000010718#ifndef O_NONBLOCK
10719# define O_NONBLOCK 0
10720#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010722 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10723 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 {
10725 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010726 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 }
10728 else
10729 n = FALSE;
10730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732}
10733
10734/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010735 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 * rights to write into.
10737 */
10738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010739f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010740 typval_T *argvars;
10741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010743 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744}
10745
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010746static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010747
10748 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010749findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010751 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010752 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010753{
10754#ifdef FEAT_SEARCHPATH
10755 char_u *fname;
10756 char_u *fresult = NULL;
10757 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10758 char_u *p;
10759 char_u pathbuf[NUMBUFLEN];
10760 int count = 1;
10761 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010762 int error = FALSE;
10763#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010764
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010765 rettv->vval.v_string = NULL;
10766 rettv->v_type = VAR_STRING;
10767
10768#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010770
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010771 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10774 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010775 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010776 else
10777 {
10778 if (*p != NUL)
10779 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010781 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010782 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010784 }
10785
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010786 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10787 error = TRUE;
10788
10789 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010790 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 do
10792 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010793 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010794 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 fresult = find_file_in_path_option(first ? fname : NULL,
10796 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010797 0, first, path,
10798 find_what,
10799 curbuf->b_ffname,
10800 find_what == FINDFILE_DIR
10801 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010802 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010803
10804 if (fresult != NULL && rettv->v_type == VAR_LIST)
10805 list_append_string(rettv->vval.v_list, fresult, -1);
10806
10807 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010808 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010809
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010810 if (rettv->v_type == VAR_STRING)
10811 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010812#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010813}
10814
Bram Moolenaar33570922005-01-25 22:26:29 +000010815static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10816static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010817
10818/*
10819 * Implementation of map() and filter().
10820 */
10821 static void
10822filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010823 typval_T *argvars;
10824 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010825 int map;
10826{
10827 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010828 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010829 listitem_T *li, *nli;
10830 list_T *l = NULL;
10831 dictitem_T *di;
10832 hashtab_T *ht;
10833 hashitem_T *hi;
10834 dict_T *d = NULL;
10835 typval_T save_val;
10836 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010837 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010838 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010839 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010840 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010841 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010842 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010843 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010844
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010846 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010847 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010848 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010849 return;
10850 }
10851 else if (argvars[0].v_type == VAR_DICT)
10852 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010853 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010854 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 return;
10856 }
10857 else
10858 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010859 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010860 return;
10861 }
10862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010863 expr = get_tv_string_buf_chk(&argvars[1], buf);
10864 /* On type errors, the preceding call has already displayed an error
10865 * message. Avoid a misleading error message for an empty string that
10866 * was not passed as argument. */
10867 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010869 prepare_vimvar(VV_VAL, &save_val);
10870 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010871
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010872 /* We reset "did_emsg" to be able to detect whether an error
10873 * occurred during evaluation of the expression. */
10874 save_did_emsg = did_emsg;
10875 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010876
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010877 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010880 vimvars[VV_KEY].vv_type = VAR_STRING;
10881
10882 ht = &d->dv_hashtab;
10883 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010884 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010885 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010887 if (!HASHITEM_EMPTY(hi))
10888 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010889 int r;
10890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 --todo;
10892 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010893 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020010894 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10895 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010896 break;
10897 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010898 r = filter_map_one(&di->di_tv, expr, map, &rem);
10899 clear_tv(&vimvars[VV_KEY].vv_tv);
10900 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010901 break;
10902 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010903 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010904 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10905 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010906 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010908 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010909 }
10910 }
10911 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010912 }
10913 else
10914 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010915 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 for (li = l->lv_first; li != NULL; li = nli)
10918 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010919 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010920 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010921 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010922 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010923 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010924 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010925 break;
10926 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010928 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010929 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010930 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010931
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010932 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010933 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010934
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010935 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937
10938 copy_tv(&argvars[0], rettv);
10939}
10940
10941 static int
10942filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010943 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010944 char_u *expr;
10945 int map;
10946 int *remp;
10947{
Bram Moolenaar33570922005-01-25 22:26:29 +000010948 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010949 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010950 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010951
Bram Moolenaar33570922005-01-25 22:26:29 +000010952 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010953 s = expr;
10954 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010955 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010956 if (*s != NUL) /* check for trailing chars after expr */
10957 {
10958 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010959 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010960 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 }
10962 if (map)
10963 {
10964 /* map(): replace the list item value */
10965 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010966 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010967 *tv = rettv;
10968 }
10969 else
10970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010971 int error = FALSE;
10972
Bram Moolenaare9a41262005-01-15 22:18:47 +000010973 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010975 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010976 /* On type error, nothing has been removed; return FAIL to stop the
10977 * loop. The error message was given by get_tv_number_chk(). */
10978 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010979 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010980 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010981 retval = OK;
10982theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010983 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010984 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010985}
10986
10987/*
10988 * "filter()" function
10989 */
10990 static void
10991f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010992 typval_T *argvars;
10993 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010994{
10995 filter_map(argvars, rettv, FALSE);
10996}
10997
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010998/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010999 * "finddir({fname}[, {path}[, {count}]])" function
11000 */
11001 static void
11002f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011003 typval_T *argvars;
11004 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011005{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011006 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011007}
11008
11009/*
11010 * "findfile({fname}[, {path}[, {count}]])" function
11011 */
11012 static void
11013f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011014 typval_T *argvars;
11015 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011016{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011017 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011018}
11019
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011020#ifdef FEAT_FLOAT
11021/*
11022 * "float2nr({float})" function
11023 */
11024 static void
11025f_float2nr(argvars, rettv)
11026 typval_T *argvars;
11027 typval_T *rettv;
11028{
11029 float_T f;
11030
11031 if (get_float_arg(argvars, &f) == OK)
11032 {
11033 if (f < -0x7fffffff)
11034 rettv->vval.v_number = -0x7fffffff;
11035 else if (f > 0x7fffffff)
11036 rettv->vval.v_number = 0x7fffffff;
11037 else
11038 rettv->vval.v_number = (varnumber_T)f;
11039 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011040}
11041
11042/*
11043 * "floor({float})" function
11044 */
11045 static void
11046f_floor(argvars, rettv)
11047 typval_T *argvars;
11048 typval_T *rettv;
11049{
11050 float_T f;
11051
11052 rettv->v_type = VAR_FLOAT;
11053 if (get_float_arg(argvars, &f) == OK)
11054 rettv->vval.v_float = floor(f);
11055 else
11056 rettv->vval.v_float = 0.0;
11057}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011058
11059/*
11060 * "fmod()" function
11061 */
11062 static void
11063f_fmod(argvars, rettv)
11064 typval_T *argvars;
11065 typval_T *rettv;
11066{
11067 float_T fx, fy;
11068
11069 rettv->v_type = VAR_FLOAT;
11070 if (get_float_arg(argvars, &fx) == OK
11071 && get_float_arg(&argvars[1], &fy) == OK)
11072 rettv->vval.v_float = fmod(fx, fy);
11073 else
11074 rettv->vval.v_float = 0.0;
11075}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011076#endif
11077
Bram Moolenaar0d660222005-01-07 21:51:51 +000011078/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011079 * "fnameescape({string})" function
11080 */
11081 static void
11082f_fnameescape(argvars, rettv)
11083 typval_T *argvars;
11084 typval_T *rettv;
11085{
11086 rettv->vval.v_string = vim_strsave_fnameescape(
11087 get_tv_string(&argvars[0]), FALSE);
11088 rettv->v_type = VAR_STRING;
11089}
11090
11091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 * "fnamemodify({fname}, {mods})" function
11093 */
11094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011096 typval_T *argvars;
11097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098{
11099 char_u *fname;
11100 char_u *mods;
11101 int usedlen = 0;
11102 int len;
11103 char_u *fbuf = NULL;
11104 char_u buf[NUMBUFLEN];
11105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011106 fname = get_tv_string_chk(&argvars[0]);
11107 mods = get_tv_string_buf_chk(&argvars[1], buf);
11108 if (fname == NULL || mods == NULL)
11109 fname = NULL;
11110 else
11111 {
11112 len = (int)STRLEN(fname);
11113 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011118 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 vim_free(fbuf);
11122}
11123
Bram Moolenaar33570922005-01-25 22:26:29 +000011124static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125
11126/*
11127 * "foldclosed()" function
11128 */
11129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011130foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011131 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011132 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011133 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134{
11135#ifdef FEAT_FOLDING
11136 linenr_T lnum;
11137 linenr_T first, last;
11138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11141 {
11142 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11143 {
11144 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 return;
11149 }
11150 }
11151#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153}
11154
11155/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011156 * "foldclosed()" function
11157 */
11158 static void
11159f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011160 typval_T *argvars;
11161 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011162{
11163 foldclosed_both(argvars, rettv, FALSE);
11164}
11165
11166/*
11167 * "foldclosedend()" function
11168 */
11169 static void
11170f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011171 typval_T *argvars;
11172 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011173{
11174 foldclosed_both(argvars, rettv, TRUE);
11175}
11176
11177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 * "foldlevel()" function
11179 */
11180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011182 typval_T *argvars UNUSED;
11183 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184{
11185#ifdef FEAT_FOLDING
11186 linenr_T lnum;
11187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011188 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192}
11193
11194/*
11195 * "foldtext()" function
11196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011198f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011199 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201{
11202#ifdef FEAT_FOLDING
11203 linenr_T lnum;
11204 char_u *s;
11205 char_u *r;
11206 int len;
11207 char *txt;
11208#endif
11209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210 rettv->v_type = VAR_STRING;
11211 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011213 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11214 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11215 <= curbuf->b_ml.ml_line_count
11216 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 {
11218 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011219 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11220 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 {
11222 if (!linewhite(lnum))
11223 break;
11224 ++lnum;
11225 }
11226
11227 /* Find interesting text in this line. */
11228 s = skipwhite(ml_get(lnum));
11229 /* skip C comment-start */
11230 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011233 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011234 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011235 {
11236 s = skipwhite(ml_get(lnum + 1));
11237 if (*s == '*')
11238 s = skipwhite(s + 1);
11239 }
11240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 txt = _("+-%s%3ld lines: ");
11242 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011243 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 + 20 /* for %3ld */
11245 + STRLEN(s))); /* concatenated */
11246 if (r != NULL)
11247 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011248 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11249 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11250 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 len = (int)STRLEN(r);
11252 STRCAT(r, s);
11253 /* remove 'foldmarker' and 'commentstring' */
11254 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 }
11257 }
11258#endif
11259}
11260
11261/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011262 * "foldtextresult(lnum)" function
11263 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011266 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011267 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011268{
11269#ifdef FEAT_FOLDING
11270 linenr_T lnum;
11271 char_u *text;
11272 char_u buf[51];
11273 foldinfo_T foldinfo;
11274 int fold_count;
11275#endif
11276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 rettv->v_type = VAR_STRING;
11278 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011279#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011281 /* treat illegal types and illegal string values for {lnum} the same */
11282 if (lnum < 0)
11283 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011284 fold_count = foldedCount(curwin, lnum, &foldinfo);
11285 if (fold_count > 0)
11286 {
11287 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11288 &foldinfo, buf);
11289 if (text == buf)
11290 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011291 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011292 }
11293#endif
11294}
11295
11296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 * "foreground()" function
11298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011301 typval_T *argvars UNUSED;
11302 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304#ifdef FEAT_GUI
11305 if (gui.in_use)
11306 gui_mch_set_foreground();
11307#else
11308# ifdef WIN32
11309 win32_set_foreground();
11310# endif
11311#endif
11312}
11313
11314/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011315 * "function()" function
11316 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *argvars;
11320 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011321{
11322 char_u *s;
11323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011325 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011326 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011327 /* Don't check an autoload name for existence here. */
11328 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011329 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011330 else
11331 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011332 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011333 {
11334 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011335 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011336
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011337 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11338 * also be called from another script. Using trans_function_name()
11339 * would also work, but some plugins depend on the name being
11340 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011341 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011342 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011343 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011344 if (rettv->vval.v_string != NULL)
11345 {
11346 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011347 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011348 }
11349 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011350 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011351 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011353 }
11354}
11355
11356/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011357 * "garbagecollect()" function
11358 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011359 static void
11360f_garbagecollect(argvars, rettv)
11361 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011362 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011363{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011364 /* This is postponed until we are back at the toplevel, because we may be
11365 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11366 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011367
11368 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11369 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011370}
11371
11372/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011373 * "get()" function
11374 */
11375 static void
11376f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011377 typval_T *argvars;
11378 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011379{
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 listitem_T *li;
11381 list_T *l;
11382 dictitem_T *di;
11383 dict_T *d;
11384 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011385
Bram Moolenaare9a41262005-01-15 22:18:47 +000011386 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011387 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011388 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011390 int error = FALSE;
11391
11392 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11393 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011394 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011395 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011397 else if (argvars[0].v_type == VAR_DICT)
11398 {
11399 if ((d = argvars[0].vval.v_dict) != NULL)
11400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011401 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011402 if (di != NULL)
11403 tv = &di->di_tv;
11404 }
11405 }
11406 else
11407 EMSG2(_(e_listdictarg), "get()");
11408
11409 if (tv == NULL)
11410 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011411 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011412 copy_tv(&argvars[2], rettv);
11413 }
11414 else
11415 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011416}
11417
Bram Moolenaar342337a2005-07-21 21:11:17 +000011418static 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 +000011419
11420/*
11421 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011422 * Return a range (from start to end) of lines in rettv from the specified
11423 * buffer.
11424 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011425 */
11426 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011427get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011428 buf_T *buf;
11429 linenr_T start;
11430 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011431 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011432 typval_T *rettv;
11433{
11434 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011435
Bram Moolenaar959a1432013-12-14 12:17:38 +010011436 rettv->v_type = VAR_STRING;
11437 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011438 if (retlist && rettv_list_alloc(rettv) == FAIL)
11439 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011440
11441 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11442 return;
11443
11444 if (!retlist)
11445 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011446 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11447 p = ml_get_buf(buf, start, FALSE);
11448 else
11449 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011450 rettv->vval.v_string = vim_strsave(p);
11451 }
11452 else
11453 {
11454 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011455 return;
11456
11457 if (start < 1)
11458 start = 1;
11459 if (end > buf->b_ml.ml_line_count)
11460 end = buf->b_ml.ml_line_count;
11461 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011462 if (list_append_string(rettv->vval.v_list,
11463 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011464 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011465 }
11466}
11467
11468/*
11469 * "getbufline()" function
11470 */
11471 static void
11472f_getbufline(argvars, rettv)
11473 typval_T *argvars;
11474 typval_T *rettv;
11475{
11476 linenr_T lnum;
11477 linenr_T end;
11478 buf_T *buf;
11479
11480 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11481 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011482 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011483 --emsg_off;
11484
Bram Moolenaar661b1822005-07-28 22:36:45 +000011485 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011486 if (argvars[2].v_type == VAR_UNKNOWN)
11487 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011488 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011489 end = get_tv_lnum_buf(&argvars[2], buf);
11490
Bram Moolenaar342337a2005-07-21 21:11:17 +000011491 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011492}
11493
Bram Moolenaar0d660222005-01-07 21:51:51 +000011494/*
11495 * "getbufvar()" function
11496 */
11497 static void
11498f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011499 typval_T *argvars;
11500 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011501{
11502 buf_T *buf;
11503 buf_T *save_curbuf;
11504 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011505 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011506 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011508 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11509 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011510 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011511 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011512
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011513 rettv->v_type = VAR_STRING;
11514 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011515
11516 if (buf != NULL && varname != NULL)
11517 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011518 /* set curbuf to be our buf, temporarily */
11519 save_curbuf = curbuf;
11520 curbuf = buf;
11521
Bram Moolenaar0d660222005-01-07 21:51:51 +000011522 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011523 {
11524 if (get_option_tv(&varname, rettv, TRUE) == OK)
11525 done = TRUE;
11526 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011527 else if (STRCMP(varname, "changedtick") == 0)
11528 {
11529 rettv->v_type = VAR_NUMBER;
11530 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011531 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011532 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011533 else
11534 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011535 /* Look up the variable. */
11536 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11537 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11538 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011539 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011540 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011541 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011542 done = TRUE;
11543 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011545
11546 /* restore previous notion of curbuf */
11547 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011548 }
11549
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011550 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11551 /* use the default value */
11552 copy_tv(&argvars[2], rettv);
11553
Bram Moolenaar0d660222005-01-07 21:51:51 +000011554 --emsg_off;
11555}
11556
11557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 * "getchar()" function
11559 */
11560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011562 typval_T *argvars;
11563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564{
11565 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011566 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011568 /* Position the cursor. Needed after a message that ends in a space. */
11569 windgoto(msg_row, msg_col);
11570
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571 ++no_mapping;
11572 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011573 for (;;)
11574 {
11575 if (argvars[0].v_type == VAR_UNKNOWN)
11576 /* getchar(): blocking wait. */
11577 n = safe_vgetc();
11578 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11579 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011580 n = vpeekc_any();
11581 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011582 /* illegal argument or getchar(0) and no char avail: return zero */
11583 n = 0;
11584 else
11585 /* getchar(0) and char avail: return char */
11586 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011587
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011588 if (n == K_IGNORE)
11589 continue;
11590 break;
11591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 --no_mapping;
11593 --allow_keys;
11594
Bram Moolenaar219b8702006-11-01 14:32:36 +000011595 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11596 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11597 vimvars[VV_MOUSE_COL].vv_nr = 0;
11598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 if (IS_SPECIAL(n) || mod_mask != 0)
11601 {
11602 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11603 int i = 0;
11604
11605 /* Turn a special key into three bytes, plus modifier. */
11606 if (mod_mask != 0)
11607 {
11608 temp[i++] = K_SPECIAL;
11609 temp[i++] = KS_MODIFIER;
11610 temp[i++] = mod_mask;
11611 }
11612 if (IS_SPECIAL(n))
11613 {
11614 temp[i++] = K_SPECIAL;
11615 temp[i++] = K_SECOND(n);
11616 temp[i++] = K_THIRD(n);
11617 }
11618#ifdef FEAT_MBYTE
11619 else if (has_mbyte)
11620 i += (*mb_char2bytes)(n, temp + i);
11621#endif
11622 else
11623 temp[i++] = n;
11624 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011625 rettv->v_type = VAR_STRING;
11626 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011627
11628#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011629 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011630 {
11631 int row = mouse_row;
11632 int col = mouse_col;
11633 win_T *win;
11634 linenr_T lnum;
11635# ifdef FEAT_WINDOWS
11636 win_T *wp;
11637# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011638 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011639
11640 if (row >= 0 && col >= 0)
11641 {
11642 /* Find the window at the mouse coordinates and compute the
11643 * text position. */
11644 win = mouse_find_win(&row, &col);
11645 (void)mouse_comp_pos(win, &row, &col, &lnum);
11646# ifdef FEAT_WINDOWS
11647 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011648 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011649# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011650 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011651 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11652 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11653 }
11654 }
11655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 }
11657}
11658
11659/*
11660 * "getcharmod()" function
11661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011664 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668}
11669
11670/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011671 * "getcharsearch()" function
11672 */
11673 static void
11674f_getcharsearch(argvars, rettv)
11675 typval_T *argvars UNUSED;
11676 typval_T *rettv;
11677{
11678 if (rettv_dict_alloc(rettv) != FAIL)
11679 {
11680 dict_T *dict = rettv->vval.v_dict;
11681
11682 dict_add_nr_str(dict, "char", 0L, last_csearch());
11683 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11684 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11685 }
11686}
11687
11688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 * "getcmdline()" function
11690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011692f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011693 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->v_type = VAR_STRING;
11697 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698}
11699
11700/*
11701 * "getcmdpos()" function
11702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011705 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709}
11710
11711/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011712 * "getcmdtype()" function
11713 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011714 static void
11715f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011716 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011717 typval_T *rettv;
11718{
11719 rettv->v_type = VAR_STRING;
11720 rettv->vval.v_string = alloc(2);
11721 if (rettv->vval.v_string != NULL)
11722 {
11723 rettv->vval.v_string[0] = get_cmdline_type();
11724 rettv->vval.v_string[1] = NUL;
11725 }
11726}
11727
11728/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011729 * "getcmdwintype()" function
11730 */
11731 static void
11732f_getcmdwintype(argvars, rettv)
11733 typval_T *argvars UNUSED;
11734 typval_T *rettv;
11735{
11736 rettv->v_type = VAR_STRING;
11737 rettv->vval.v_string = NULL;
11738#ifdef FEAT_CMDWIN
11739 rettv->vval.v_string = alloc(2);
11740 if (rettv->vval.v_string != NULL)
11741 {
11742 rettv->vval.v_string[0] = cmdwin_type;
11743 rettv->vval.v_string[1] = NUL;
11744 }
11745#endif
11746}
11747
11748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 * "getcwd()" function
11750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011753 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011756 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011759 rettv->vval.v_string = NULL;
11760 cwd = alloc(MAXPATHL);
11761 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011763 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11764 {
11765 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011767 if (rettv->vval.v_string != NULL)
11768 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011770 }
11771 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 }
11773}
11774
11775/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011776 * "getfontname()" function
11777 */
11778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011781 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783 rettv->v_type = VAR_STRING;
11784 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011785#ifdef FEAT_GUI
11786 if (gui.in_use)
11787 {
11788 GuiFont font;
11789 char_u *name = NULL;
11790
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011791 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011792 {
11793 /* Get the "Normal" font. Either the name saved by
11794 * hl_set_font_name() or from the font ID. */
11795 font = gui.norm_font;
11796 name = hl_get_font_name();
11797 }
11798 else
11799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011801 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11802 return;
11803 font = gui_mch_get_font(name, FALSE);
11804 if (font == NOFONT)
11805 return; /* Invalid font name, return empty string. */
11806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011808 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011809 gui_mch_free_font(font);
11810 }
11811#endif
11812}
11813
11814/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011815 * "getfperm({fname})" function
11816 */
11817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011819 typval_T *argvars;
11820 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011821{
11822 char_u *fname;
11823 struct stat st;
11824 char_u *perm = NULL;
11825 char_u flags[] = "rwx";
11826 int i;
11827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011831 if (mch_stat((char *)fname, &st) >= 0)
11832 {
11833 perm = vim_strsave((char_u *)"---------");
11834 if (perm != NULL)
11835 {
11836 for (i = 0; i < 9; i++)
11837 {
11838 if (st.st_mode & (1 << (8 - i)))
11839 perm[i] = flags[i % 3];
11840 }
11841 }
11842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011843 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011844}
11845
11846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 * "getfsize({fname})" function
11848 */
11849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011850f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011851 typval_T *argvars;
11852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853{
11854 char_u *fname;
11855 struct stat st;
11856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860
11861 if (mch_stat((char *)fname, &st) >= 0)
11862 {
11863 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011868
11869 /* non-perfect check for overflow */
11870 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11871 rettv->vval.v_number = -2;
11872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 }
11874 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876}
11877
11878/*
11879 * "getftime({fname})" function
11880 */
11881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011882f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011883 typval_T *argvars;
11884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885{
11886 char_u *fname;
11887 struct stat st;
11888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011889 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890
11891 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011892 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011894 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895}
11896
11897/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011898 * "getftype({fname})" function
11899 */
11900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 typval_T *argvars;
11903 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011904{
11905 char_u *fname;
11906 struct stat st;
11907 char_u *type = NULL;
11908 char *t;
11909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011910 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011912 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011913 if (mch_lstat((char *)fname, &st) >= 0)
11914 {
11915#ifdef S_ISREG
11916 if (S_ISREG(st.st_mode))
11917 t = "file";
11918 else if (S_ISDIR(st.st_mode))
11919 t = "dir";
11920# ifdef S_ISLNK
11921 else if (S_ISLNK(st.st_mode))
11922 t = "link";
11923# endif
11924# ifdef S_ISBLK
11925 else if (S_ISBLK(st.st_mode))
11926 t = "bdev";
11927# endif
11928# ifdef S_ISCHR
11929 else if (S_ISCHR(st.st_mode))
11930 t = "cdev";
11931# endif
11932# ifdef S_ISFIFO
11933 else if (S_ISFIFO(st.st_mode))
11934 t = "fifo";
11935# endif
11936# ifdef S_ISSOCK
11937 else if (S_ISSOCK(st.st_mode))
11938 t = "fifo";
11939# endif
11940 else
11941 t = "other";
11942#else
11943# ifdef S_IFMT
11944 switch (st.st_mode & S_IFMT)
11945 {
11946 case S_IFREG: t = "file"; break;
11947 case S_IFDIR: t = "dir"; break;
11948# ifdef S_IFLNK
11949 case S_IFLNK: t = "link"; break;
11950# endif
11951# ifdef S_IFBLK
11952 case S_IFBLK: t = "bdev"; break;
11953# endif
11954# ifdef S_IFCHR
11955 case S_IFCHR: t = "cdev"; break;
11956# endif
11957# ifdef S_IFIFO
11958 case S_IFIFO: t = "fifo"; break;
11959# endif
11960# ifdef S_IFSOCK
11961 case S_IFSOCK: t = "socket"; break;
11962# endif
11963 default: t = "other";
11964 }
11965# else
11966 if (mch_isdir(fname))
11967 t = "dir";
11968 else
11969 t = "file";
11970# endif
11971#endif
11972 type = vim_strsave((char_u *)t);
11973 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011975}
11976
11977/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011978 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011979 */
11980 static void
11981f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011982 typval_T *argvars;
11983 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011984{
11985 linenr_T lnum;
11986 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011987 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011988
11989 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011990 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011991 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011992 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011993 retlist = FALSE;
11994 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011995 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011996 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011997 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011998 retlist = TRUE;
11999 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012000
Bram Moolenaar342337a2005-07-21 21:11:17 +000012001 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012002}
12003
12004/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012005 * "getmatches()" function
12006 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012007 static void
12008f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012009 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012010 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012011{
12012#ifdef FEAT_SEARCH_EXTRA
12013 dict_T *dict;
12014 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012015 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012016
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012017 if (rettv_list_alloc(rettv) == OK)
12018 {
12019 while (cur != NULL)
12020 {
12021 dict = dict_alloc();
12022 if (dict == NULL)
12023 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012024 if (cur->match.regprog == NULL)
12025 {
12026 /* match added with matchaddpos() */
12027 for (i = 0; i < MAXPOSMATCH; ++i)
12028 {
12029 llpos_T *llpos;
12030 char buf[6];
12031 list_T *l;
12032
12033 llpos = &cur->pos.pos[i];
12034 if (llpos->lnum == 0)
12035 break;
12036 l = list_alloc();
12037 if (l == NULL)
12038 break;
12039 list_append_number(l, (varnumber_T)llpos->lnum);
12040 if (llpos->col > 0)
12041 {
12042 list_append_number(l, (varnumber_T)llpos->col);
12043 list_append_number(l, (varnumber_T)llpos->len);
12044 }
12045 sprintf(buf, "pos%d", i + 1);
12046 dict_add_list(dict, buf, l);
12047 }
12048 }
12049 else
12050 {
12051 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12052 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012053 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012054 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12055 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012056# ifdef FEAT_CONCEAL
12057 if (cur->conceal_char)
12058 {
12059 char_u buf[MB_MAXBYTES + 1];
12060
12061 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12062 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12063 }
12064# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012065 list_append_dict(rettv->vval.v_list, dict);
12066 cur = cur->next;
12067 }
12068 }
12069#endif
12070}
12071
12072/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012073 * "getpid()" function
12074 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012075 static void
12076f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012077 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012078 typval_T *rettv;
12079{
12080 rettv->vval.v_number = mch_get_pid();
12081}
12082
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012083static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12084
12085/*
12086 * "getcurpos()" function
12087 */
12088 static void
12089f_getcurpos(argvars, rettv)
12090 typval_T *argvars;
12091 typval_T *rettv;
12092{
12093 getpos_both(argvars, rettv, TRUE);
12094}
12095
Bram Moolenaar18081e32008-02-20 19:11:07 +000012096/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012097 * "getpos(string)" function
12098 */
12099 static void
12100f_getpos(argvars, rettv)
12101 typval_T *argvars;
12102 typval_T *rettv;
12103{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012104 getpos_both(argvars, rettv, FALSE);
12105}
12106
12107 static void
12108getpos_both(argvars, rettv, getcurpos)
12109 typval_T *argvars;
12110 typval_T *rettv;
12111 int getcurpos;
12112{
Bram Moolenaara5525202006-03-02 22:52:09 +000012113 pos_T *fp;
12114 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012115 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012116
12117 if (rettv_list_alloc(rettv) == OK)
12118 {
12119 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012120 if (getcurpos)
12121 fp = &curwin->w_cursor;
12122 else
12123 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012124 if (fnum != -1)
12125 list_append_number(l, (varnumber_T)fnum);
12126 else
12127 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012128 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12129 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012130 list_append_number(l, (fp != NULL)
12131 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012132 : (varnumber_T)0);
12133 list_append_number(l,
12134#ifdef FEAT_VIRTUALEDIT
12135 (fp != NULL) ? (varnumber_T)fp->coladd :
12136#endif
12137 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012138 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012139 list_append_number(l, curwin->w_curswant == MAXCOL ?
12140 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012141 }
12142 else
12143 rettv->vval.v_number = FALSE;
12144}
12145
12146/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012147 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012148 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012149 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012150f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012151 typval_T *argvars UNUSED;
12152 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012153{
12154#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012155 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012156#endif
12157
Bram Moolenaar2641f772005-03-25 21:58:17 +000012158#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012159 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012160 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012161 wp = NULL;
12162 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12163 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012164 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012165 if (wp == NULL)
12166 return;
12167 }
12168
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012169 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012170 }
12171#endif
12172}
12173
12174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 * "getreg()" function
12176 */
12177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012178f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012179 typval_T *argvars;
12180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181{
12182 char_u *strregname;
12183 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012184 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012185 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012186 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012188 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012190 strregname = get_tv_string_chk(&argvars[0]);
12191 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012192 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012194 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012195 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12196 return_list = get_tv_number_chk(&argvars[2], &error);
12197 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012200 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012201
12202 if (error)
12203 return;
12204
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 regname = (strregname == NULL ? '"' : *strregname);
12206 if (regname == 0)
12207 regname = '"';
12208
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012209 if (return_list)
12210 {
12211 rettv->v_type = VAR_LIST;
12212 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12213 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012214 if (rettv->vval.v_list != NULL)
12215 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012216 }
12217 else
12218 {
12219 rettv->v_type = VAR_STRING;
12220 rettv->vval.v_string = get_reg_contents(regname,
12221 arg2 ? GREG_EXPR_SRC : 0);
12222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223}
12224
12225/*
12226 * "getregtype()" function
12227 */
12228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012229f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012230 typval_T *argvars;
12231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232{
12233 char_u *strregname;
12234 int regname;
12235 char_u buf[NUMBUFLEN + 2];
12236 long reglen = 0;
12237
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012238 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012239 {
12240 strregname = get_tv_string_chk(&argvars[0]);
12241 if (strregname == NULL) /* type error; errmsg already given */
12242 {
12243 rettv->v_type = VAR_STRING;
12244 rettv->vval.v_string = NULL;
12245 return;
12246 }
12247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 else
12249 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012250 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251
12252 regname = (strregname == NULL ? '"' : *strregname);
12253 if (regname == 0)
12254 regname = '"';
12255
12256 buf[0] = NUL;
12257 buf[1] = NUL;
12258 switch (get_reg_type(regname, &reglen))
12259 {
12260 case MLINE: buf[0] = 'V'; break;
12261 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 case MBLOCK:
12263 buf[0] = Ctrl_V;
12264 sprintf((char *)buf + 1, "%ld", reglen + 1);
12265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267 rettv->v_type = VAR_STRING;
12268 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269}
12270
12271/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012272 * "gettabvar()" function
12273 */
12274 static void
12275f_gettabvar(argvars, rettv)
12276 typval_T *argvars;
12277 typval_T *rettv;
12278{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012279 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012280 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012281 dictitem_T *v;
12282 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012283 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012284
12285 rettv->v_type = VAR_STRING;
12286 rettv->vval.v_string = NULL;
12287
12288 varname = get_tv_string_chk(&argvars[1]);
12289 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12290 if (tp != NULL && varname != NULL)
12291 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012292 /* Set tp to be our tabpage, temporarily. Also set the window to the
12293 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012294 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12295 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012296 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012297 /* look up the variable */
12298 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12299 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12300 if (v != NULL)
12301 {
12302 copy_tv(&v->di_tv, rettv);
12303 done = TRUE;
12304 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012305 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012306
12307 /* restore previous notion of curwin */
12308 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012309 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012310
12311 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012312 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012313}
12314
12315/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012316 * "gettabwinvar()" function
12317 */
12318 static void
12319f_gettabwinvar(argvars, rettv)
12320 typval_T *argvars;
12321 typval_T *rettv;
12322{
12323 getwinvar(argvars, rettv, 1);
12324}
12325
12326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327 * "getwinposx()" function
12328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012331 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012334 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335#ifdef FEAT_GUI
12336 if (gui.in_use)
12337 {
12338 int x, y;
12339
12340 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 }
12343#endif
12344}
12345
12346/*
12347 * "getwinposy()" function
12348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012350f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012351 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355#ifdef FEAT_GUI
12356 if (gui.in_use)
12357 {
12358 int x, y;
12359
12360 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 }
12363#endif
12364}
12365
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012366/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012367 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012368 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012369 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012370find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012371 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012372 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012373{
12374#ifdef FEAT_WINDOWS
12375 win_T *wp;
12376#endif
12377 int nr;
12378
12379 nr = get_tv_number_chk(vp, NULL);
12380
12381#ifdef FEAT_WINDOWS
12382 if (nr < 0)
12383 return NULL;
12384 if (nr == 0)
12385 return curwin;
12386
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012387 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12388 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012389 if (--nr <= 0)
12390 break;
12391 return wp;
12392#else
12393 if (nr == 0 || nr == 1)
12394 return curwin;
12395 return NULL;
12396#endif
12397}
12398
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399/*
12400 * "getwinvar()" function
12401 */
12402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012403f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012404 typval_T *argvars;
12405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012407 getwinvar(argvars, rettv, 0);
12408}
12409
12410/*
12411 * getwinvar() and gettabwinvar()
12412 */
12413 static void
12414getwinvar(argvars, rettv, off)
12415 typval_T *argvars;
12416 typval_T *rettv;
12417 int off; /* 1 for gettabwinvar() */
12418{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 win_T *win, *oldcurwin;
12420 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012421 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012422 tabpage_T *tp = NULL;
12423 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012424 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012426#ifdef FEAT_WINDOWS
12427 if (off == 1)
12428 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12429 else
12430 tp = curtab;
12431#endif
12432 win = find_win_by_nr(&argvars[off], tp);
12433 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012434 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012436 rettv->v_type = VAR_STRING;
12437 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438
12439 if (win != NULL && varname != NULL)
12440 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012441 /* Set curwin to be our win, temporarily. Also set the tabpage,
12442 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012443 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012444 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012445 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012446 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012447 if (get_option_tv(&varname, rettv, 1) == OK)
12448 done = TRUE;
12449 }
12450 else
12451 {
12452 /* Look up the variable. */
12453 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12454 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12455 varname, FALSE);
12456 if (v != NULL)
12457 {
12458 copy_tv(&v->di_tv, rettv);
12459 done = TRUE;
12460 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012463
12464 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012465 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466 }
12467
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012468 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12469 /* use the default return value */
12470 copy_tv(&argvars[off + 2], rettv);
12471
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 --emsg_off;
12473}
12474
12475/*
12476 * "glob()" function
12477 */
12478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012480 typval_T *argvars;
12481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012483 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012485 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012487 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012488 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012489 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012490 if (argvars[1].v_type != VAR_UNKNOWN)
12491 {
12492 if (get_tv_number_chk(&argvars[1], &error))
12493 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012494 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012495 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012496 if (get_tv_number_chk(&argvars[2], &error))
12497 {
12498 rettv->v_type = VAR_LIST;
12499 rettv->vval.v_list = NULL;
12500 }
12501 if (argvars[3].v_type != VAR_UNKNOWN
12502 && get_tv_number_chk(&argvars[3], &error))
12503 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012504 }
12505 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012506 if (!error)
12507 {
12508 ExpandInit(&xpc);
12509 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012510 if (p_wic)
12511 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012512 if (rettv->v_type == VAR_STRING)
12513 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012514 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012515 else if (rettv_list_alloc(rettv) != FAIL)
12516 {
12517 int i;
12518
12519 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12520 NULL, options, WILD_ALL_KEEP);
12521 for (i = 0; i < xpc.xp_numfiles; i++)
12522 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12523
12524 ExpandCleanup(&xpc);
12525 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012526 }
12527 else
12528 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529}
12530
12531/*
12532 * "globpath()" function
12533 */
12534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012535f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012536 typval_T *argvars;
12537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012539 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012541 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012542 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012543 garray_T ga;
12544 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012546 /* When the optional second argument is non-zero, don't remove matches
12547 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012548 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012549 if (argvars[2].v_type != VAR_UNKNOWN)
12550 {
12551 if (get_tv_number_chk(&argvars[2], &error))
12552 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012553 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012554 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012555 if (get_tv_number_chk(&argvars[3], &error))
12556 {
12557 rettv->v_type = VAR_LIST;
12558 rettv->vval.v_list = NULL;
12559 }
12560 if (argvars[4].v_type != VAR_UNKNOWN
12561 && get_tv_number_chk(&argvars[4], &error))
12562 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012563 }
12564 }
12565 if (file != NULL && !error)
12566 {
12567 ga_init2(&ga, (int)sizeof(char_u *), 10);
12568 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12569 if (rettv->v_type == VAR_STRING)
12570 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12571 else if (rettv_list_alloc(rettv) != FAIL)
12572 for (i = 0; i < ga.ga_len; ++i)
12573 list_append_string(rettv->vval.v_list,
12574 ((char_u **)(ga.ga_data))[i], -1);
12575 ga_clear_strings(&ga);
12576 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012577 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012578 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579}
12580
12581/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012582 * "glob2regpat()" function
12583 */
12584 static void
12585f_glob2regpat(argvars, rettv)
12586 typval_T *argvars;
12587 typval_T *rettv;
12588{
12589 char_u *pat = get_tv_string_chk(&argvars[0]);
12590
12591 rettv->v_type = VAR_STRING;
12592 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12593}
12594
12595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 * "has()" function
12597 */
12598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012599f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012600 typval_T *argvars;
12601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602{
12603 int i;
12604 char_u *name;
12605 int n = FALSE;
12606 static char *(has_list[]) =
12607 {
12608#ifdef AMIGA
12609 "amiga",
12610# ifdef FEAT_ARP
12611 "arp",
12612# endif
12613#endif
12614#ifdef __BEOS__
12615 "beos",
12616#endif
12617#ifdef MSDOS
12618# ifdef DJGPP
12619 "dos32",
12620# else
12621 "dos16",
12622# endif
12623#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012624#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012625 "mac",
12626#endif
12627#if defined(MACOS_X_UNIX)
12628 "macunix",
12629#endif
12630#ifdef OS2
12631 "os2",
12632#endif
12633#ifdef __QNX__
12634 "qnx",
12635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636#ifdef UNIX
12637 "unix",
12638#endif
12639#ifdef VMS
12640 "vms",
12641#endif
12642#ifdef WIN16
12643 "win16",
12644#endif
12645#ifdef WIN32
12646 "win32",
12647#endif
12648#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12649 "win32unix",
12650#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012651#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 "win64",
12653#endif
12654#ifdef EBCDIC
12655 "ebcdic",
12656#endif
12657#ifndef CASE_INSENSITIVE_FILENAME
12658 "fname_case",
12659#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012660#ifdef HAVE_ACL
12661 "acl",
12662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663#ifdef FEAT_ARABIC
12664 "arabic",
12665#endif
12666#ifdef FEAT_AUTOCMD
12667 "autocmd",
12668#endif
12669#ifdef FEAT_BEVAL
12670 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012671# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12672 "balloon_multiline",
12673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674#endif
12675#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12676 "builtin_terms",
12677# ifdef ALL_BUILTIN_TCAPS
12678 "all_builtin_terms",
12679# endif
12680#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012681#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12682 || defined(FEAT_GUI_W32) \
12683 || defined(FEAT_GUI_MOTIF))
12684 "browsefilter",
12685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686#ifdef FEAT_BYTEOFF
12687 "byte_offset",
12688#endif
12689#ifdef FEAT_CINDENT
12690 "cindent",
12691#endif
12692#ifdef FEAT_CLIENTSERVER
12693 "clientserver",
12694#endif
12695#ifdef FEAT_CLIPBOARD
12696 "clipboard",
12697#endif
12698#ifdef FEAT_CMDL_COMPL
12699 "cmdline_compl",
12700#endif
12701#ifdef FEAT_CMDHIST
12702 "cmdline_hist",
12703#endif
12704#ifdef FEAT_COMMENTS
12705 "comments",
12706#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012707#ifdef FEAT_CONCEAL
12708 "conceal",
12709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710#ifdef FEAT_CRYPT
12711 "cryptv",
12712#endif
12713#ifdef FEAT_CSCOPE
12714 "cscope",
12715#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012716#ifdef FEAT_CURSORBIND
12717 "cursorbind",
12718#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012719#ifdef CURSOR_SHAPE
12720 "cursorshape",
12721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722#ifdef DEBUG
12723 "debug",
12724#endif
12725#ifdef FEAT_CON_DIALOG
12726 "dialog_con",
12727#endif
12728#ifdef FEAT_GUI_DIALOG
12729 "dialog_gui",
12730#endif
12731#ifdef FEAT_DIFF
12732 "diff",
12733#endif
12734#ifdef FEAT_DIGRAPHS
12735 "digraphs",
12736#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012737#ifdef FEAT_DIRECTX
12738 "directx",
12739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740#ifdef FEAT_DND
12741 "dnd",
12742#endif
12743#ifdef FEAT_EMACS_TAGS
12744 "emacs_tags",
12745#endif
12746 "eval", /* always present, of course! */
12747#ifdef FEAT_EX_EXTRA
12748 "ex_extra",
12749#endif
12750#ifdef FEAT_SEARCH_EXTRA
12751 "extra_search",
12752#endif
12753#ifdef FEAT_FKMAP
12754 "farsi",
12755#endif
12756#ifdef FEAT_SEARCHPATH
12757 "file_in_path",
12758#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012759#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012760 "filterpipe",
12761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762#ifdef FEAT_FIND_ID
12763 "find_in_path",
12764#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012765#ifdef FEAT_FLOAT
12766 "float",
12767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768#ifdef FEAT_FOLDING
12769 "folding",
12770#endif
12771#ifdef FEAT_FOOTER
12772 "footer",
12773#endif
12774#if !defined(USE_SYSTEM) && defined(UNIX)
12775 "fork",
12776#endif
12777#ifdef FEAT_GETTEXT
12778 "gettext",
12779#endif
12780#ifdef FEAT_GUI
12781 "gui",
12782#endif
12783#ifdef FEAT_GUI_ATHENA
12784# ifdef FEAT_GUI_NEXTAW
12785 "gui_neXtaw",
12786# else
12787 "gui_athena",
12788# endif
12789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790#ifdef FEAT_GUI_GTK
12791 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012794#ifdef FEAT_GUI_GNOME
12795 "gui_gnome",
12796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797#ifdef FEAT_GUI_MAC
12798 "gui_mac",
12799#endif
12800#ifdef FEAT_GUI_MOTIF
12801 "gui_motif",
12802#endif
12803#ifdef FEAT_GUI_PHOTON
12804 "gui_photon",
12805#endif
12806#ifdef FEAT_GUI_W16
12807 "gui_win16",
12808#endif
12809#ifdef FEAT_GUI_W32
12810 "gui_win32",
12811#endif
12812#ifdef FEAT_HANGULIN
12813 "hangul_input",
12814#endif
12815#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12816 "iconv",
12817#endif
12818#ifdef FEAT_INS_EXPAND
12819 "insert_expand",
12820#endif
12821#ifdef FEAT_JUMPLIST
12822 "jumplist",
12823#endif
12824#ifdef FEAT_KEYMAP
12825 "keymap",
12826#endif
12827#ifdef FEAT_LANGMAP
12828 "langmap",
12829#endif
12830#ifdef FEAT_LIBCALL
12831 "libcall",
12832#endif
12833#ifdef FEAT_LINEBREAK
12834 "linebreak",
12835#endif
12836#ifdef FEAT_LISP
12837 "lispindent",
12838#endif
12839#ifdef FEAT_LISTCMDS
12840 "listcmds",
12841#endif
12842#ifdef FEAT_LOCALMAP
12843 "localmap",
12844#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012845#ifdef FEAT_LUA
12846# ifndef DYNAMIC_LUA
12847 "lua",
12848# endif
12849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850#ifdef FEAT_MENU
12851 "menu",
12852#endif
12853#ifdef FEAT_SESSION
12854 "mksession",
12855#endif
12856#ifdef FEAT_MODIFY_FNAME
12857 "modify_fname",
12858#endif
12859#ifdef FEAT_MOUSE
12860 "mouse",
12861#endif
12862#ifdef FEAT_MOUSESHAPE
12863 "mouseshape",
12864#endif
12865#if defined(UNIX) || defined(VMS)
12866# ifdef FEAT_MOUSE_DEC
12867 "mouse_dec",
12868# endif
12869# ifdef FEAT_MOUSE_GPM
12870 "mouse_gpm",
12871# endif
12872# ifdef FEAT_MOUSE_JSB
12873 "mouse_jsbterm",
12874# endif
12875# ifdef FEAT_MOUSE_NET
12876 "mouse_netterm",
12877# endif
12878# ifdef FEAT_MOUSE_PTERM
12879 "mouse_pterm",
12880# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012881# ifdef FEAT_MOUSE_SGR
12882 "mouse_sgr",
12883# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012884# ifdef FEAT_SYSMOUSE
12885 "mouse_sysmouse",
12886# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012887# ifdef FEAT_MOUSE_URXVT
12888 "mouse_urxvt",
12889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890# ifdef FEAT_MOUSE_XTERM
12891 "mouse_xterm",
12892# endif
12893#endif
12894#ifdef FEAT_MBYTE
12895 "multi_byte",
12896#endif
12897#ifdef FEAT_MBYTE_IME
12898 "multi_byte_ime",
12899#endif
12900#ifdef FEAT_MULTI_LANG
12901 "multi_lang",
12902#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012903#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012904#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012905 "mzscheme",
12906#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908#ifdef FEAT_OLE
12909 "ole",
12910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911#ifdef FEAT_PATH_EXTRA
12912 "path_extra",
12913#endif
12914#ifdef FEAT_PERL
12915#ifndef DYNAMIC_PERL
12916 "perl",
12917#endif
12918#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012919#ifdef FEAT_PERSISTENT_UNDO
12920 "persistent_undo",
12921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922#ifdef FEAT_PYTHON
12923#ifndef DYNAMIC_PYTHON
12924 "python",
12925#endif
12926#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012927#ifdef FEAT_PYTHON3
12928#ifndef DYNAMIC_PYTHON3
12929 "python3",
12930#endif
12931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932#ifdef FEAT_POSTSCRIPT
12933 "postscript",
12934#endif
12935#ifdef FEAT_PRINTER
12936 "printer",
12937#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012938#ifdef FEAT_PROFILE
12939 "profile",
12940#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012941#ifdef FEAT_RELTIME
12942 "reltime",
12943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944#ifdef FEAT_QUICKFIX
12945 "quickfix",
12946#endif
12947#ifdef FEAT_RIGHTLEFT
12948 "rightleft",
12949#endif
12950#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12951 "ruby",
12952#endif
12953#ifdef FEAT_SCROLLBIND
12954 "scrollbind",
12955#endif
12956#ifdef FEAT_CMDL_INFO
12957 "showcmd",
12958 "cmdline_info",
12959#endif
12960#ifdef FEAT_SIGNS
12961 "signs",
12962#endif
12963#ifdef FEAT_SMARTINDENT
12964 "smartindent",
12965#endif
12966#ifdef FEAT_SNIFF
12967 "sniff",
12968#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012969#ifdef STARTUPTIME
12970 "startuptime",
12971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972#ifdef FEAT_STL_OPT
12973 "statusline",
12974#endif
12975#ifdef FEAT_SUN_WORKSHOP
12976 "sun_workshop",
12977#endif
12978#ifdef FEAT_NETBEANS_INTG
12979 "netbeans_intg",
12980#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012981#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012982 "spell",
12983#endif
12984#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985 "syntax",
12986#endif
12987#if defined(USE_SYSTEM) || !defined(UNIX)
12988 "system",
12989#endif
12990#ifdef FEAT_TAG_BINS
12991 "tag_binary",
12992#endif
12993#ifdef FEAT_TAG_OLDSTATIC
12994 "tag_old_static",
12995#endif
12996#ifdef FEAT_TAG_ANYWHITE
12997 "tag_any_white",
12998#endif
12999#ifdef FEAT_TCL
13000# ifndef DYNAMIC_TCL
13001 "tcl",
13002# endif
13003#endif
13004#ifdef TERMINFO
13005 "terminfo",
13006#endif
13007#ifdef FEAT_TERMRESPONSE
13008 "termresponse",
13009#endif
13010#ifdef FEAT_TEXTOBJ
13011 "textobjects",
13012#endif
13013#ifdef HAVE_TGETENT
13014 "tgetent",
13015#endif
13016#ifdef FEAT_TITLE
13017 "title",
13018#endif
13019#ifdef FEAT_TOOLBAR
13020 "toolbar",
13021#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013022#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13023 "unnamedplus",
13024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025#ifdef FEAT_USR_CMDS
13026 "user-commands", /* was accidentally included in 5.4 */
13027 "user_commands",
13028#endif
13029#ifdef FEAT_VIMINFO
13030 "viminfo",
13031#endif
13032#ifdef FEAT_VERTSPLIT
13033 "vertsplit",
13034#endif
13035#ifdef FEAT_VIRTUALEDIT
13036 "virtualedit",
13037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039#ifdef FEAT_VISUALEXTRA
13040 "visualextra",
13041#endif
13042#ifdef FEAT_VREPLACE
13043 "vreplace",
13044#endif
13045#ifdef FEAT_WILDIGN
13046 "wildignore",
13047#endif
13048#ifdef FEAT_WILDMENU
13049 "wildmenu",
13050#endif
13051#ifdef FEAT_WINDOWS
13052 "windows",
13053#endif
13054#ifdef FEAT_WAK
13055 "winaltkeys",
13056#endif
13057#ifdef FEAT_WRITEBACKUP
13058 "writebackup",
13059#endif
13060#ifdef FEAT_XIM
13061 "xim",
13062#endif
13063#ifdef FEAT_XFONTSET
13064 "xfontset",
13065#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013066#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013067 "xpm",
13068 "xpm_w32", /* for backward compatibility */
13069#else
13070# if defined(HAVE_XPM)
13071 "xpm",
13072# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074#ifdef USE_XSMP
13075 "xsmp",
13076#endif
13077#ifdef USE_XSMP_INTERACT
13078 "xsmp_interact",
13079#endif
13080#ifdef FEAT_XCLIPBOARD
13081 "xterm_clipboard",
13082#endif
13083#ifdef FEAT_XTERM_SAVE
13084 "xterm_save",
13085#endif
13086#if defined(UNIX) && defined(FEAT_X11)
13087 "X11",
13088#endif
13089 NULL
13090 };
13091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013092 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093 for (i = 0; has_list[i] != NULL; ++i)
13094 if (STRICMP(name, has_list[i]) == 0)
13095 {
13096 n = TRUE;
13097 break;
13098 }
13099
13100 if (n == FALSE)
13101 {
13102 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013103 {
13104 if (name[5] == '-'
13105 && STRLEN(name) > 11
13106 && vim_isdigit(name[6])
13107 && vim_isdigit(name[8])
13108 && vim_isdigit(name[10]))
13109 {
13110 int major = atoi((char *)name + 6);
13111 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013112
13113 /* Expect "patch-9.9.01234". */
13114 n = (major < VIM_VERSION_MAJOR
13115 || (major == VIM_VERSION_MAJOR
13116 && (minor < VIM_VERSION_MINOR
13117 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013118 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013119 }
13120 else
13121 n = has_patch(atoi((char *)name + 5));
13122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 else if (STRICMP(name, "vim_starting") == 0)
13124 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013125#ifdef FEAT_MBYTE
13126 else if (STRICMP(name, "multi_byte_encoding") == 0)
13127 n = has_mbyte;
13128#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013129#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13130 else if (STRICMP(name, "balloon_multiline") == 0)
13131 n = multiline_balloon_available();
13132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133#ifdef DYNAMIC_TCL
13134 else if (STRICMP(name, "tcl") == 0)
13135 n = tcl_enabled(FALSE);
13136#endif
13137#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13138 else if (STRICMP(name, "iconv") == 0)
13139 n = iconv_enabled(FALSE);
13140#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013141#ifdef DYNAMIC_LUA
13142 else if (STRICMP(name, "lua") == 0)
13143 n = lua_enabled(FALSE);
13144#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013145#ifdef DYNAMIC_MZSCHEME
13146 else if (STRICMP(name, "mzscheme") == 0)
13147 n = mzscheme_enabled(FALSE);
13148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149#ifdef DYNAMIC_RUBY
13150 else if (STRICMP(name, "ruby") == 0)
13151 n = ruby_enabled(FALSE);
13152#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013153#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154#ifdef DYNAMIC_PYTHON
13155 else if (STRICMP(name, "python") == 0)
13156 n = python_enabled(FALSE);
13157#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013158#endif
13159#ifdef FEAT_PYTHON3
13160#ifdef DYNAMIC_PYTHON3
13161 else if (STRICMP(name, "python3") == 0)
13162 n = python3_enabled(FALSE);
13163#endif
13164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165#ifdef DYNAMIC_PERL
13166 else if (STRICMP(name, "perl") == 0)
13167 n = perl_enabled(FALSE);
13168#endif
13169#ifdef FEAT_GUI
13170 else if (STRICMP(name, "gui_running") == 0)
13171 n = (gui.in_use || gui.starting);
13172# ifdef FEAT_GUI_W32
13173 else if (STRICMP(name, "gui_win32s") == 0)
13174 n = gui_is_win32s();
13175# endif
13176# ifdef FEAT_BROWSE
13177 else if (STRICMP(name, "browse") == 0)
13178 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13179# endif
13180#endif
13181#ifdef FEAT_SYN_HL
13182 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013183 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184#endif
13185#if defined(WIN3264)
13186 else if (STRICMP(name, "win95") == 0)
13187 n = mch_windows95();
13188#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013189#ifdef FEAT_NETBEANS_INTG
13190 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013191 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 }
13194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013195 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196}
13197
13198/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013199 * "has_key()" function
13200 */
13201 static void
13202f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 typval_T *argvars;
13204 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013205{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013206 if (argvars[0].v_type != VAR_DICT)
13207 {
13208 EMSG(_(e_dictreq));
13209 return;
13210 }
13211 if (argvars[0].vval.v_dict == NULL)
13212 return;
13213
13214 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013215 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013216}
13217
13218/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013219 * "haslocaldir()" function
13220 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013221 static void
13222f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013223 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013224 typval_T *rettv;
13225{
13226 rettv->vval.v_number = (curwin->w_localdir != NULL);
13227}
13228
13229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 * "hasmapto()" function
13231 */
13232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013234 typval_T *argvars;
13235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236{
13237 char_u *name;
13238 char_u *mode;
13239 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013240 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013242 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013243 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 mode = (char_u *)"nvo";
13245 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013247 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013248 if (argvars[2].v_type != VAR_UNKNOWN)
13249 abbr = get_tv_number(&argvars[2]);
13250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251
Bram Moolenaar2c932302006-03-18 21:42:09 +000013252 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013253 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013255 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256}
13257
13258/*
13259 * "histadd()" function
13260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013262f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013263 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265{
13266#ifdef FEAT_CMDHIST
13267 int histype;
13268 char_u *str;
13269 char_u buf[NUMBUFLEN];
13270#endif
13271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273 if (check_restricted() || check_secure())
13274 return;
13275#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013276 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13277 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 if (histype >= 0)
13279 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013280 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 if (*str != NUL)
13282 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013283 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013285 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286 return;
13287 }
13288 }
13289#endif
13290}
13291
13292/*
13293 * "histdel()" function
13294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013296f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013297 typval_T *argvars UNUSED;
13298 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299{
13300#ifdef FEAT_CMDHIST
13301 int n;
13302 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013303 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013305 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13306 if (str == NULL)
13307 n = 0;
13308 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013310 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013311 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013313 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013314 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315 else
13316 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013317 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013318 get_tv_string_buf(&argvars[1], buf));
13319 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320#endif
13321}
13322
13323/*
13324 * "histget()" function
13325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013327f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013328 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330{
13331#ifdef FEAT_CMDHIST
13332 int type;
13333 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013334 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013336 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13337 if (str == NULL)
13338 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013340 {
13341 type = get_histtype(str);
13342 if (argvars[1].v_type == VAR_UNKNOWN)
13343 idx = get_history_idx(type);
13344 else
13345 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13346 /* -1 on type error */
13347 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353}
13354
13355/*
13356 * "histnr()" function
13357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362{
13363 int i;
13364
13365#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013366 char_u *history = get_tv_string_chk(&argvars[0]);
13367
13368 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 if (i >= HIST_CMD && i < HIST_COUNT)
13370 i = get_history_idx(i);
13371 else
13372#endif
13373 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013374 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375}
13376
13377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 * "highlightID(name)" function
13379 */
13380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013381f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 typval_T *argvars;
13383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013385 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386}
13387
13388/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013389 * "highlight_exists()" function
13390 */
13391 static void
13392f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013393 typval_T *argvars;
13394 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013395{
13396 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13397}
13398
13399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400 * "hostname()" function
13401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013403f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013404 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406{
13407 char_u hostname[256];
13408
13409 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013410 rettv->v_type = VAR_STRING;
13411 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412}
13413
13414/*
13415 * iconv() function
13416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013419 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421{
13422#ifdef FEAT_MBYTE
13423 char_u buf1[NUMBUFLEN];
13424 char_u buf2[NUMBUFLEN];
13425 char_u *from, *to, *str;
13426 vimconv_T vimconv;
13427#endif
13428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429 rettv->v_type = VAR_STRING;
13430 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431
13432#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013433 str = get_tv_string(&argvars[0]);
13434 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13435 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 vimconv.vc_type = CONV_NONE;
13437 convert_setup(&vimconv, from, to);
13438
13439 /* If the encodings are equal, no conversion needed. */
13440 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013443 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444
13445 convert_setup(&vimconv, NULL, NULL);
13446 vim_free(from);
13447 vim_free(to);
13448#endif
13449}
13450
13451/*
13452 * "indent()" function
13453 */
13454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013456 typval_T *argvars;
13457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458{
13459 linenr_T lnum;
13460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013463 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013465 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466}
13467
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013468/*
13469 * "index()" function
13470 */
13471 static void
13472f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013473 typval_T *argvars;
13474 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013475{
Bram Moolenaar33570922005-01-25 22:26:29 +000013476 list_T *l;
13477 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013478 long idx = 0;
13479 int ic = FALSE;
13480
13481 rettv->vval.v_number = -1;
13482 if (argvars[0].v_type != VAR_LIST)
13483 {
13484 EMSG(_(e_listreq));
13485 return;
13486 }
13487 l = argvars[0].vval.v_list;
13488 if (l != NULL)
13489 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013490 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013491 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493 int error = FALSE;
13494
Bram Moolenaar758711c2005-02-02 23:11:38 +000013495 /* Start at specified item. Use the cached index that list_find()
13496 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013498 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013499 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013500 ic = get_tv_number_chk(&argvars[3], &error);
13501 if (error)
13502 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013503 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013504
Bram Moolenaar758711c2005-02-02 23:11:38 +000013505 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013506 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013507 {
13508 rettv->vval.v_number = idx;
13509 break;
13510 }
13511 }
13512}
13513
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514static int inputsecret_flag = 0;
13515
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013516static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13517
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013519 * This function is used by f_input() and f_inputdialog() functions. The third
13520 * argument to f_input() specifies the type of completion to use at the
13521 * prompt. The third argument to f_inputdialog() specifies the value to return
13522 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 */
13524 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013525get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013526 typval_T *argvars;
13527 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013528 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 char_u *p = NULL;
13532 int c;
13533 char_u buf[NUMBUFLEN];
13534 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013535 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013536 int xp_type = EXPAND_NOTHING;
13537 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013539 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013540 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541
13542#ifdef NO_CONSOLE_INPUT
13543 /* While starting up, there is no place to enter text. */
13544 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546#endif
13547
13548 cmd_silent = FALSE; /* Want to see the prompt. */
13549 if (prompt != NULL)
13550 {
13551 /* Only the part of the message after the last NL is considered as
13552 * prompt for the command line */
13553 p = vim_strrchr(prompt, '\n');
13554 if (p == NULL)
13555 p = prompt;
13556 else
13557 {
13558 ++p;
13559 c = *p;
13560 *p = NUL;
13561 msg_start();
13562 msg_clr_eos();
13563 msg_puts_attr(prompt, echo_attr);
13564 msg_didout = FALSE;
13565 msg_starthere();
13566 *p = c;
13567 }
13568 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013570 if (argvars[1].v_type != VAR_UNKNOWN)
13571 {
13572 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13573 if (defstr != NULL)
13574 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013576 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013577 {
13578 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013579 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013580 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013581
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013582 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013583 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013584
Bram Moolenaar4463f292005-09-25 22:20:24 +000013585 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13586 if (xp_name == NULL)
13587 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013588
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013589 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013590
Bram Moolenaar4463f292005-09-25 22:20:24 +000013591 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13592 &xp_arg) == FAIL)
13593 return;
13594 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013595 }
13596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013597 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013598 {
13599# ifdef FEAT_EX_EXTRA
13600 int save_ex_normal_busy = ex_normal_busy;
13601 ex_normal_busy = 0;
13602# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013603 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013604 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13605 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013606# ifdef FEAT_EX_EXTRA
13607 ex_normal_busy = save_ex_normal_busy;
13608# endif
13609 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013610 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013611 && argvars[1].v_type != VAR_UNKNOWN
13612 && argvars[2].v_type != VAR_UNKNOWN)
13613 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13614 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013615
13616 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013618 /* since the user typed this, no need to wait for return */
13619 need_wait_return = FALSE;
13620 msg_didout = FALSE;
13621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622 cmd_silent = cmd_silent_save;
13623}
13624
13625/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013626 * "input()" function
13627 * Also handles inputsecret() when inputsecret is set.
13628 */
13629 static void
13630f_input(argvars, rettv)
13631 typval_T *argvars;
13632 typval_T *rettv;
13633{
13634 get_user_input(argvars, rettv, FALSE);
13635}
13636
13637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 * "inputdialog()" function
13639 */
13640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013641f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013642 typval_T *argvars;
13643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644{
13645#if defined(FEAT_GUI_TEXTDIALOG)
13646 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13647 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13648 {
13649 char_u *message;
13650 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013651 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013653 message = get_tv_string_chk(&argvars[0]);
13654 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013655 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013656 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 else
13658 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013659 if (message != NULL && defstr != NULL
13660 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013661 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 else
13664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013665 if (message != NULL && defstr != NULL
13666 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013667 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 rettv->vval.v_string = vim_strsave(
13669 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013673 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 }
13675 else
13676#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013677 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678}
13679
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013680/*
13681 * "inputlist()" function
13682 */
13683 static void
13684f_inputlist(argvars, rettv)
13685 typval_T *argvars;
13686 typval_T *rettv;
13687{
13688 listitem_T *li;
13689 int selected;
13690 int mouse_used;
13691
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013692#ifdef NO_CONSOLE_INPUT
13693 /* While starting up, there is no place to enter text. */
13694 if (no_console_input())
13695 return;
13696#endif
13697 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13698 {
13699 EMSG2(_(e_listarg), "inputlist()");
13700 return;
13701 }
13702
13703 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013704 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013705 lines_left = Rows; /* avoid more prompt */
13706 msg_scroll = TRUE;
13707 msg_clr_eos();
13708
13709 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13710 {
13711 msg_puts(get_tv_string(&li->li_tv));
13712 msg_putchar('\n');
13713 }
13714
13715 /* Ask for choice. */
13716 selected = prompt_for_number(&mouse_used);
13717 if (mouse_used)
13718 selected -= lines_left;
13719
13720 rettv->vval.v_number = selected;
13721}
13722
13723
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13725
13726/*
13727 * "inputrestore()" function
13728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013730f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013731 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733{
13734 if (ga_userinput.ga_len > 0)
13735 {
13736 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13738 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013739 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 }
13741 else if (p_verbose > 1)
13742 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013743 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013744 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 }
13746}
13747
13748/*
13749 * "inputsave()" function
13750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013753 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013756 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757 if (ga_grow(&ga_userinput, 1) == OK)
13758 {
13759 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13760 + ga_userinput.ga_len);
13761 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013762 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763 }
13764 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766}
13767
13768/*
13769 * "inputsecret()" function
13770 */
13771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013772f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *argvars;
13774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775{
13776 ++cmdline_star;
13777 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 --cmdline_star;
13780 --inputsecret_flag;
13781}
13782
13783/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013784 * "insert()" function
13785 */
13786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013787f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013788 typval_T *argvars;
13789 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013790{
13791 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013792 listitem_T *item;
13793 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013794 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013795
13796 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013797 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013798 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013799 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013800 {
13801 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013802 before = get_tv_number_chk(&argvars[2], &error);
13803 if (error)
13804 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013805
Bram Moolenaar758711c2005-02-02 23:11:38 +000013806 if (before == l->lv_len)
13807 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013808 else
13809 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013810 item = list_find(l, before);
13811 if (item == NULL)
13812 {
13813 EMSGN(_(e_listidx), before);
13814 l = NULL;
13815 }
13816 }
13817 if (l != NULL)
13818 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013819 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013820 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013821 }
13822 }
13823}
13824
13825/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013826 * "invert(expr)" function
13827 */
13828 static void
13829f_invert(argvars, rettv)
13830 typval_T *argvars;
13831 typval_T *rettv;
13832{
13833 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13834}
13835
13836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 * "isdirectory()" function
13838 */
13839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013840f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013841 typval_T *argvars;
13842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013844 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845}
13846
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013847/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013848 * "islocked()" function
13849 */
13850 static void
13851f_islocked(argvars, rettv)
13852 typval_T *argvars;
13853 typval_T *rettv;
13854{
13855 lval_T lv;
13856 char_u *end;
13857 dictitem_T *di;
13858
13859 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013860 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13861 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013862 if (end != NULL && lv.ll_name != NULL)
13863 {
13864 if (*end != NUL)
13865 EMSG(_(e_trailing));
13866 else
13867 {
13868 if (lv.ll_tv == NULL)
13869 {
13870 if (check_changedtick(lv.ll_name))
13871 rettv->vval.v_number = 1; /* always locked */
13872 else
13873 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013874 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013875 if (di != NULL)
13876 {
13877 /* Consider a variable locked when:
13878 * 1. the variable itself is locked
13879 * 2. the value of the variable is locked.
13880 * 3. the List or Dict value is locked.
13881 */
13882 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13883 || tv_islocked(&di->di_tv));
13884 }
13885 }
13886 }
13887 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013888 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013889 else if (lv.ll_newkey != NULL)
13890 EMSG2(_(e_dictkey), lv.ll_newkey);
13891 else if (lv.ll_list != NULL)
13892 /* List item. */
13893 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13894 else
13895 /* Dictionary item. */
13896 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13897 }
13898 }
13899
13900 clear_lval(&lv);
13901}
13902
Bram Moolenaar33570922005-01-25 22:26:29 +000013903static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013904
13905/*
13906 * Turn a dict into a list:
13907 * "what" == 0: list of keys
13908 * "what" == 1: list of values
13909 * "what" == 2: list of items
13910 */
13911 static void
13912dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013913 typval_T *argvars;
13914 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013915 int what;
13916{
Bram Moolenaar33570922005-01-25 22:26:29 +000013917 list_T *l2;
13918 dictitem_T *di;
13919 hashitem_T *hi;
13920 listitem_T *li;
13921 listitem_T *li2;
13922 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013923 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013924
Bram Moolenaar8c711452005-01-14 21:53:12 +000013925 if (argvars[0].v_type != VAR_DICT)
13926 {
13927 EMSG(_(e_dictreq));
13928 return;
13929 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013930 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013931 return;
13932
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013933 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013934 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013935
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013936 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013937 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013938 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013939 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013940 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013941 --todo;
13942 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013943
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013944 li = listitem_alloc();
13945 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013946 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013947 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013948
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013949 if (what == 0)
13950 {
13951 /* keys() */
13952 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013953 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013954 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13955 }
13956 else if (what == 1)
13957 {
13958 /* values() */
13959 copy_tv(&di->di_tv, &li->li_tv);
13960 }
13961 else
13962 {
13963 /* items() */
13964 l2 = list_alloc();
13965 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013966 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013967 li->li_tv.vval.v_list = l2;
13968 if (l2 == NULL)
13969 break;
13970 ++l2->lv_refcount;
13971
13972 li2 = listitem_alloc();
13973 if (li2 == NULL)
13974 break;
13975 list_append(l2, li2);
13976 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013977 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013978 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13979
13980 li2 = listitem_alloc();
13981 if (li2 == NULL)
13982 break;
13983 list_append(l2, li2);
13984 copy_tv(&di->di_tv, &li2->li_tv);
13985 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013986 }
13987 }
13988}
13989
13990/*
13991 * "items(dict)" function
13992 */
13993 static void
13994f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013995 typval_T *argvars;
13996 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013997{
13998 dict_list(argvars, rettv, 2);
13999}
14000
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014002 * "join()" function
14003 */
14004 static void
14005f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014006 typval_T *argvars;
14007 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014008{
14009 garray_T ga;
14010 char_u *sep;
14011
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014012 if (argvars[0].v_type != VAR_LIST)
14013 {
14014 EMSG(_(e_listreq));
14015 return;
14016 }
14017 if (argvars[0].vval.v_list == NULL)
14018 return;
14019 if (argvars[1].v_type == VAR_UNKNOWN)
14020 sep = (char_u *)" ";
14021 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014022 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014023
14024 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014025
14026 if (sep != NULL)
14027 {
14028 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014029 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014030 ga_append(&ga, NUL);
14031 rettv->vval.v_string = (char_u *)ga.ga_data;
14032 }
14033 else
14034 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014035}
14036
14037/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014038 * "keys()" function
14039 */
14040 static void
14041f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014042 typval_T *argvars;
14043 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014044{
14045 dict_list(argvars, rettv, 0);
14046}
14047
14048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049 * "last_buffer_nr()" function.
14050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014052f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014053 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055{
14056 int n = 0;
14057 buf_T *buf;
14058
14059 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14060 if (n < buf->b_fnum)
14061 n = buf->b_fnum;
14062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014063 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014064}
14065
14066/*
14067 * "len()" function
14068 */
14069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014070f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014071 typval_T *argvars;
14072 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014073{
14074 switch (argvars[0].v_type)
14075 {
14076 case VAR_STRING:
14077 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014078 rettv->vval.v_number = (varnumber_T)STRLEN(
14079 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014080 break;
14081 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014083 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014084 case VAR_DICT:
14085 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14086 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014087 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014088 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014089 break;
14090 }
14091}
14092
Bram Moolenaar33570922005-01-25 22:26:29 +000014093static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014094
14095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014096libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014097 typval_T *argvars;
14098 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014099 int type;
14100{
14101#ifdef FEAT_LIBCALL
14102 char_u *string_in;
14103 char_u **string_result;
14104 int nr_result;
14105#endif
14106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014108 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014109 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014110
14111 if (check_restricted() || check_secure())
14112 return;
14113
14114#ifdef FEAT_LIBCALL
14115 /* The first two args must be strings, otherwise its meaningless */
14116 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14117 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014118 string_in = NULL;
14119 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014120 string_in = argvars[2].vval.v_string;
14121 if (type == VAR_NUMBER)
14122 string_result = NULL;
14123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014124 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014125 if (mch_libcall(argvars[0].vval.v_string,
14126 argvars[1].vval.v_string,
14127 string_in,
14128 argvars[2].vval.v_number,
14129 string_result,
14130 &nr_result) == OK
14131 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014133 }
14134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135}
14136
14137/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014138 * "libcall()" function
14139 */
14140 static void
14141f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014142 typval_T *argvars;
14143 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014144{
14145 libcall_common(argvars, rettv, VAR_STRING);
14146}
14147
14148/*
14149 * "libcallnr()" function
14150 */
14151 static void
14152f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014153 typval_T *argvars;
14154 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014155{
14156 libcall_common(argvars, rettv, VAR_NUMBER);
14157}
14158
14159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 * "line(string)" function
14161 */
14162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014163f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014164 typval_T *argvars;
14165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166{
14167 linenr_T lnum = 0;
14168 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014169 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014171 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 if (fp != NULL)
14173 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014174 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175}
14176
14177/*
14178 * "line2byte(lnum)" function
14179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014181f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014182 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184{
14185#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014186 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187#else
14188 linenr_T lnum;
14189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014190 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014192 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014194 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14195 if (rettv->vval.v_number >= 0)
14196 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197#endif
14198}
14199
14200/*
14201 * "lispindent(lnum)" function
14202 */
14203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014204f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014205 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207{
14208#ifdef FEAT_LISP
14209 pos_T pos;
14210 linenr_T lnum;
14211
14212 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014213 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14215 {
14216 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 curwin->w_cursor = pos;
14219 }
14220 else
14221#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014222 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223}
14224
14225/*
14226 * "localtime()" function
14227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014229f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014230 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014233 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234}
14235
Bram Moolenaar33570922005-01-25 22:26:29 +000014236static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237
14238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014240 typval_T *argvars;
14241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 int exact;
14243{
14244 char_u *keys;
14245 char_u *which;
14246 char_u buf[NUMBUFLEN];
14247 char_u *keys_buf = NULL;
14248 char_u *rhs;
14249 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014250 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014251 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014252 mapblock_T *mp;
14253 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254
14255 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014256 rettv->v_type = VAR_STRING;
14257 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014259 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 if (*keys == NUL)
14261 return;
14262
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014263 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014266 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014267 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014268 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014269 if (argvars[3].v_type != VAR_UNKNOWN)
14270 get_dict = get_tv_number(&argvars[3]);
14271 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273 else
14274 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014275 if (which == NULL)
14276 return;
14277
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 mode = get_map_mode(&which, 0);
14279
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014280 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014281 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014283
14284 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014286 /* Return a string. */
14287 if (rhs != NULL)
14288 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289
Bram Moolenaarbd743252010-10-20 21:23:33 +020014290 }
14291 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14292 {
14293 /* Return a dictionary. */
14294 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14295 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14296 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297
Bram Moolenaarbd743252010-10-20 21:23:33 +020014298 dict_add_nr_str(dict, "lhs", 0L, lhs);
14299 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14300 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14301 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14302 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14303 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14304 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014305 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014306 dict_add_nr_str(dict, "mode", 0L, mapmode);
14307
14308 vim_free(lhs);
14309 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 }
14311}
14312
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014313#ifdef FEAT_FLOAT
14314/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014315 * "log()" function
14316 */
14317 static void
14318f_log(argvars, rettv)
14319 typval_T *argvars;
14320 typval_T *rettv;
14321{
14322 float_T f;
14323
14324 rettv->v_type = VAR_FLOAT;
14325 if (get_float_arg(argvars, &f) == OK)
14326 rettv->vval.v_float = log(f);
14327 else
14328 rettv->vval.v_float = 0.0;
14329}
14330
14331/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014332 * "log10()" function
14333 */
14334 static void
14335f_log10(argvars, rettv)
14336 typval_T *argvars;
14337 typval_T *rettv;
14338{
14339 float_T f;
14340
14341 rettv->v_type = VAR_FLOAT;
14342 if (get_float_arg(argvars, &f) == OK)
14343 rettv->vval.v_float = log10(f);
14344 else
14345 rettv->vval.v_float = 0.0;
14346}
14347#endif
14348
Bram Moolenaar1dced572012-04-05 16:54:08 +020014349#ifdef FEAT_LUA
14350/*
14351 * "luaeval()" function
14352 */
14353 static void
14354f_luaeval(argvars, rettv)
14355 typval_T *argvars;
14356 typval_T *rettv;
14357{
14358 char_u *str;
14359 char_u buf[NUMBUFLEN];
14360
14361 str = get_tv_string_buf(&argvars[0], buf);
14362 do_luaeval(str, argvars + 1, rettv);
14363}
14364#endif
14365
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014367 * "map()" function
14368 */
14369 static void
14370f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014371 typval_T *argvars;
14372 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014373{
14374 filter_map(argvars, rettv, TRUE);
14375}
14376
14377/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014378 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379 */
14380 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014381f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014382 typval_T *argvars;
14383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014385 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386}
14387
14388/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 */
14391 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014392f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 typval_T *argvars;
14394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014396 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397}
14398
Bram Moolenaar33570922005-01-25 22:26:29 +000014399static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400
14401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014402find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014403 typval_T *argvars;
14404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 int type;
14406{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014407 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014408 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014409 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410 char_u *pat;
14411 regmatch_T regmatch;
14412 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014413 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 char_u *save_cpo;
14415 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014416 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014417 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014418 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014419 list_T *l = NULL;
14420 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014421 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014422 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423
14424 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14425 save_cpo = p_cpo;
14426 p_cpo = (char_u *)"";
14427
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014428 rettv->vval.v_number = -1;
14429 if (type == 3)
14430 {
14431 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014432 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014433 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014434 }
14435 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437 rettv->v_type = VAR_STRING;
14438 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014441 if (argvars[0].v_type == VAR_LIST)
14442 {
14443 if ((l = argvars[0].vval.v_list) == NULL)
14444 goto theend;
14445 li = l->lv_first;
14446 }
14447 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014448 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014449 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014450 len = (long)STRLEN(str);
14451 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014453 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14454 if (pat == NULL)
14455 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014456
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014457 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014459 int error = FALSE;
14460
14461 start = get_tv_number_chk(&argvars[2], &error);
14462 if (error)
14463 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014464 if (l != NULL)
14465 {
14466 li = list_find(l, start);
14467 if (li == NULL)
14468 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014469 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014470 }
14471 else
14472 {
14473 if (start < 0)
14474 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014475 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014476 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014477 /* When "count" argument is there ignore matches before "start",
14478 * otherwise skip part of the string. Differs when pattern is "^"
14479 * or "\<". */
14480 if (argvars[3].v_type != VAR_UNKNOWN)
14481 startcol = start;
14482 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014483 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014484 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014485 len -= start;
14486 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014487 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014488
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014489 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014490 nth = get_tv_number_chk(&argvars[3], &error);
14491 if (error)
14492 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 }
14494
14495 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14496 if (regmatch.regprog != NULL)
14497 {
14498 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014499
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014500 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014501 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014502 if (l != NULL)
14503 {
14504 if (li == NULL)
14505 {
14506 match = FALSE;
14507 break;
14508 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014509 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014510 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014511 if (str == NULL)
14512 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014513 }
14514
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014515 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014516
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014517 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014518 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014519 if (l == NULL && !match)
14520 break;
14521
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014522 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014523 if (l != NULL)
14524 {
14525 li = li->li_next;
14526 ++idx;
14527 }
14528 else
14529 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014530#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014531 startcol = (colnr_T)(regmatch.startp[0]
14532 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014533#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014534 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014535#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014536 if (startcol > (colnr_T)len
14537 || str + startcol <= regmatch.startp[0])
14538 {
14539 match = FALSE;
14540 break;
14541 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014542 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014543 }
14544
14545 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014547 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014548 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014549 int i;
14550
14551 /* return list with matched string and submatches */
14552 for (i = 0; i < NSUBEXP; ++i)
14553 {
14554 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014555 {
14556 if (list_append_string(rettv->vval.v_list,
14557 (char_u *)"", 0) == FAIL)
14558 break;
14559 }
14560 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014561 regmatch.startp[i],
14562 (int)(regmatch.endp[i] - regmatch.startp[i]))
14563 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014564 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014565 }
14566 }
14567 else if (type == 2)
14568 {
14569 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014570 if (l != NULL)
14571 copy_tv(&li->li_tv, rettv);
14572 else
14573 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014575 }
14576 else if (l != NULL)
14577 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 else
14579 {
14580 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014581 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 (varnumber_T)(regmatch.startp[0] - str);
14583 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014584 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014586 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587 }
14588 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014589 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 }
14591
14592theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014593 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 p_cpo = save_cpo;
14595}
14596
14597/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014598 * "match()" function
14599 */
14600 static void
14601f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014602 typval_T *argvars;
14603 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604{
14605 find_some_match(argvars, rettv, 1);
14606}
14607
14608/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014609 * "matchadd()" function
14610 */
14611 static void
14612f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014613 typval_T *argvars UNUSED;
14614 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014615{
14616#ifdef FEAT_SEARCH_EXTRA
14617 char_u buf[NUMBUFLEN];
14618 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14619 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14620 int prio = 10; /* default priority */
14621 int id = -1;
14622 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014623 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014624
14625 rettv->vval.v_number = -1;
14626
14627 if (grp == NULL || pat == NULL)
14628 return;
14629 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014630 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014631 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014632 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014633 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014634 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014635 if (argvars[4].v_type != VAR_UNKNOWN)
14636 {
14637 if (argvars[4].v_type != VAR_DICT)
14638 {
14639 EMSG(_(e_dictreq));
14640 return;
14641 }
14642 if (dict_find(argvars[4].vval.v_dict,
14643 (char_u *)"conceal", -1) != NULL)
14644 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14645 (char_u *)"conceal", FALSE);
14646 }
14647 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014648 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014649 if (error == TRUE)
14650 return;
14651 if (id >= 1 && id <= 3)
14652 {
14653 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14654 return;
14655 }
14656
Bram Moolenaar6561d522015-07-21 15:48:27 +020014657 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14658 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014659#endif
14660}
14661
14662/*
14663 * "matchaddpos()" function
14664 */
14665 static void
14666f_matchaddpos(argvars, rettv)
14667 typval_T *argvars UNUSED;
14668 typval_T *rettv UNUSED;
14669{
14670#ifdef FEAT_SEARCH_EXTRA
14671 char_u buf[NUMBUFLEN];
14672 char_u *group;
14673 int prio = 10;
14674 int id = -1;
14675 int error = FALSE;
14676 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014677 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014678
14679 rettv->vval.v_number = -1;
14680
14681 group = get_tv_string_buf_chk(&argvars[0], buf);
14682 if (group == NULL)
14683 return;
14684
14685 if (argvars[1].v_type != VAR_LIST)
14686 {
14687 EMSG2(_(e_listarg), "matchaddpos()");
14688 return;
14689 }
14690 l = argvars[1].vval.v_list;
14691 if (l == NULL)
14692 return;
14693
14694 if (argvars[2].v_type != VAR_UNKNOWN)
14695 {
14696 prio = get_tv_number_chk(&argvars[2], &error);
14697 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014698 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014699 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014700 if (argvars[4].v_type != VAR_UNKNOWN)
14701 {
14702 if (argvars[4].v_type != VAR_DICT)
14703 {
14704 EMSG(_(e_dictreq));
14705 return;
14706 }
14707 if (dict_find(argvars[4].vval.v_dict,
14708 (char_u *)"conceal", -1) != NULL)
14709 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14710 (char_u *)"conceal", FALSE);
14711 }
14712 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014713 }
14714 if (error == TRUE)
14715 return;
14716
14717 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14718 if (id == 1 || id == 2)
14719 {
14720 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14721 return;
14722 }
14723
Bram Moolenaar6561d522015-07-21 15:48:27 +020014724 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14725 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014726#endif
14727}
14728
14729/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014730 * "matcharg()" function
14731 */
14732 static void
14733f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014734 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014735 typval_T *rettv;
14736{
14737 if (rettv_list_alloc(rettv) == OK)
14738 {
14739#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014740 int id = get_tv_number(&argvars[0]);
14741 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014742
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014743 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014744 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014745 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14746 {
14747 list_append_string(rettv->vval.v_list,
14748 syn_id2name(m->hlg_id), -1);
14749 list_append_string(rettv->vval.v_list, m->pattern, -1);
14750 }
14751 else
14752 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014753 list_append_string(rettv->vval.v_list, NULL, -1);
14754 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014755 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014756 }
14757#endif
14758 }
14759}
14760
14761/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014762 * "matchdelete()" function
14763 */
14764 static void
14765f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014766 typval_T *argvars UNUSED;
14767 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014768{
14769#ifdef FEAT_SEARCH_EXTRA
14770 rettv->vval.v_number = match_delete(curwin,
14771 (int)get_tv_number(&argvars[0]), TRUE);
14772#endif
14773}
14774
14775/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014776 * "matchend()" function
14777 */
14778 static void
14779f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014780 typval_T *argvars;
14781 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014782{
14783 find_some_match(argvars, rettv, 0);
14784}
14785
14786/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014787 * "matchlist()" function
14788 */
14789 static void
14790f_matchlist(argvars, rettv)
14791 typval_T *argvars;
14792 typval_T *rettv;
14793{
14794 find_some_match(argvars, rettv, 3);
14795}
14796
14797/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014798 * "matchstr()" function
14799 */
14800 static void
14801f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014802 typval_T *argvars;
14803 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014804{
14805 find_some_match(argvars, rettv, 2);
14806}
14807
Bram Moolenaar33570922005-01-25 22:26:29 +000014808static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014809
14810 static void
14811max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014812 typval_T *argvars;
14813 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014814 int domax;
14815{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014816 long n = 0;
14817 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014818 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014819
14820 if (argvars[0].v_type == VAR_LIST)
14821 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014822 list_T *l;
14823 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014824
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014825 l = argvars[0].vval.v_list;
14826 if (l != NULL)
14827 {
14828 li = l->lv_first;
14829 if (li != NULL)
14830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014831 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014832 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014833 {
14834 li = li->li_next;
14835 if (li == NULL)
14836 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014837 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014838 if (domax ? i > n : i < n)
14839 n = i;
14840 }
14841 }
14842 }
14843 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014844 else if (argvars[0].v_type == VAR_DICT)
14845 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014846 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014847 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014848 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014849 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014850
14851 d = argvars[0].vval.v_dict;
14852 if (d != NULL)
14853 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014854 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014855 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014856 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014857 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014858 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014859 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014860 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014861 if (first)
14862 {
14863 n = i;
14864 first = FALSE;
14865 }
14866 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014867 n = i;
14868 }
14869 }
14870 }
14871 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014872 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014873 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014874 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014875}
14876
14877/*
14878 * "max()" function
14879 */
14880 static void
14881f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014882 typval_T *argvars;
14883 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014884{
14885 max_min(argvars, rettv, TRUE);
14886}
14887
14888/*
14889 * "min()" function
14890 */
14891 static void
14892f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014893 typval_T *argvars;
14894 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014895{
14896 max_min(argvars, rettv, FALSE);
14897}
14898
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014899static int mkdir_recurse __ARGS((char_u *dir, int prot));
14900
14901/*
14902 * Create the directory in which "dir" is located, and higher levels when
14903 * needed.
14904 */
14905 static int
14906mkdir_recurse(dir, prot)
14907 char_u *dir;
14908 int prot;
14909{
14910 char_u *p;
14911 char_u *updir;
14912 int r = FAIL;
14913
14914 /* Get end of directory name in "dir".
14915 * We're done when it's "/" or "c:/". */
14916 p = gettail_sep(dir);
14917 if (p <= get_past_head(dir))
14918 return OK;
14919
14920 /* If the directory exists we're done. Otherwise: create it.*/
14921 updir = vim_strnsave(dir, (int)(p - dir));
14922 if (updir == NULL)
14923 return FAIL;
14924 if (mch_isdir(updir))
14925 r = OK;
14926 else if (mkdir_recurse(updir, prot) == OK)
14927 r = vim_mkdir_emsg(updir, prot);
14928 vim_free(updir);
14929 return r;
14930}
14931
14932#ifdef vim_mkdir
14933/*
14934 * "mkdir()" function
14935 */
14936 static void
14937f_mkdir(argvars, rettv)
14938 typval_T *argvars;
14939 typval_T *rettv;
14940{
14941 char_u *dir;
14942 char_u buf[NUMBUFLEN];
14943 int prot = 0755;
14944
14945 rettv->vval.v_number = FAIL;
14946 if (check_restricted() || check_secure())
14947 return;
14948
14949 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014950 if (*dir == NUL)
14951 rettv->vval.v_number = FAIL;
14952 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014953 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014954 if (*gettail(dir) == NUL)
14955 /* remove trailing slashes */
14956 *gettail_sep(dir) = NUL;
14957
14958 if (argvars[1].v_type != VAR_UNKNOWN)
14959 {
14960 if (argvars[2].v_type != VAR_UNKNOWN)
14961 prot = get_tv_number_chk(&argvars[2], NULL);
14962 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14963 mkdir_recurse(dir, prot);
14964 }
14965 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014966 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014967}
14968#endif
14969
Bram Moolenaar0d660222005-01-07 21:51:51 +000014970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971 * "mode()" function
14972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014974f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014975 typval_T *argvars;
14976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014978 char_u buf[3];
14979
14980 buf[1] = NUL;
14981 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 if (VIsual_active)
14984 {
14985 if (VIsual_select)
14986 buf[0] = VIsual_mode + 's' - 'v';
14987 else
14988 buf[0] = VIsual_mode;
14989 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014990 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014991 || State == CONFIRM)
14992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014994 if (State == ASKMORE)
14995 buf[1] = 'm';
14996 else if (State == CONFIRM)
14997 buf[1] = '?';
14998 }
14999 else if (State == EXTERNCMD)
15000 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001 else if (State & INSERT)
15002 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015003#ifdef FEAT_VREPLACE
15004 if (State & VREPLACE_FLAG)
15005 {
15006 buf[0] = 'R';
15007 buf[1] = 'v';
15008 }
15009 else
15010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 if (State & REPLACE_FLAG)
15012 buf[0] = 'R';
15013 else
15014 buf[0] = 'i';
15015 }
15016 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015019 if (exmode_active)
15020 buf[1] = 'v';
15021 }
15022 else if (exmode_active)
15023 {
15024 buf[0] = 'c';
15025 buf[1] = 'e';
15026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015030 if (finish_op)
15031 buf[1] = 'o';
15032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015034 /* Clear out the minor mode when the argument is not a non-zero number or
15035 * non-empty string. */
15036 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015037 buf[1] = NUL;
15038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015039 rettv->vval.v_string = vim_strsave(buf);
15040 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041}
15042
Bram Moolenaar429fa852013-04-15 12:27:36 +020015043#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015044/*
15045 * "mzeval()" function
15046 */
15047 static void
15048f_mzeval(argvars, rettv)
15049 typval_T *argvars;
15050 typval_T *rettv;
15051{
15052 char_u *str;
15053 char_u buf[NUMBUFLEN];
15054
15055 str = get_tv_string_buf(&argvars[0], buf);
15056 do_mzeval(str, rettv);
15057}
Bram Moolenaar75676462013-01-30 14:55:42 +010015058
15059 void
15060mzscheme_call_vim(name, args, rettv)
15061 char_u *name;
15062 typval_T *args;
15063 typval_T *rettv;
15064{
15065 typval_T argvars[3];
15066
15067 argvars[0].v_type = VAR_STRING;
15068 argvars[0].vval.v_string = name;
15069 copy_tv(args, &argvars[1]);
15070 argvars[2].v_type = VAR_UNKNOWN;
15071 f_call(argvars, rettv);
15072 clear_tv(&argvars[1]);
15073}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015074#endif
15075
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077 * "nextnonblank()" function
15078 */
15079 static void
15080f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015081 typval_T *argvars;
15082 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083{
15084 linenr_T lnum;
15085
15086 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015088 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089 {
15090 lnum = 0;
15091 break;
15092 }
15093 if (*skipwhite(ml_get(lnum)) != NUL)
15094 break;
15095 }
15096 rettv->vval.v_number = lnum;
15097}
15098
15099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 * "nr2char()" function
15101 */
15102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015103f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015104 typval_T *argvars;
15105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106{
15107 char_u buf[NUMBUFLEN];
15108
15109#ifdef FEAT_MBYTE
15110 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015111 {
15112 int utf8 = 0;
15113
15114 if (argvars[1].v_type != VAR_UNKNOWN)
15115 utf8 = get_tv_number_chk(&argvars[1], NULL);
15116 if (utf8)
15117 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15118 else
15119 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121 else
15122#endif
15123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015124 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125 buf[1] = NUL;
15126 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015127 rettv->v_type = VAR_STRING;
15128 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015129}
15130
15131/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015132 * "or(expr, expr)" function
15133 */
15134 static void
15135f_or(argvars, rettv)
15136 typval_T *argvars;
15137 typval_T *rettv;
15138{
15139 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15140 | get_tv_number_chk(&argvars[1], NULL);
15141}
15142
15143/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015144 * "pathshorten()" function
15145 */
15146 static void
15147f_pathshorten(argvars, rettv)
15148 typval_T *argvars;
15149 typval_T *rettv;
15150{
15151 char_u *p;
15152
15153 rettv->v_type = VAR_STRING;
15154 p = get_tv_string_chk(&argvars[0]);
15155 if (p == NULL)
15156 rettv->vval.v_string = NULL;
15157 else
15158 {
15159 p = vim_strsave(p);
15160 rettv->vval.v_string = p;
15161 if (p != NULL)
15162 shorten_dir(p);
15163 }
15164}
15165
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015166#ifdef FEAT_FLOAT
15167/*
15168 * "pow()" function
15169 */
15170 static void
15171f_pow(argvars, rettv)
15172 typval_T *argvars;
15173 typval_T *rettv;
15174{
15175 float_T fx, fy;
15176
15177 rettv->v_type = VAR_FLOAT;
15178 if (get_float_arg(argvars, &fx) == OK
15179 && get_float_arg(&argvars[1], &fy) == OK)
15180 rettv->vval.v_float = pow(fx, fy);
15181 else
15182 rettv->vval.v_float = 0.0;
15183}
15184#endif
15185
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015186/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015187 * "prevnonblank()" function
15188 */
15189 static void
15190f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015191 typval_T *argvars;
15192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193{
15194 linenr_T lnum;
15195
15196 lnum = get_tv_lnum(argvars);
15197 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15198 lnum = 0;
15199 else
15200 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15201 --lnum;
15202 rettv->vval.v_number = lnum;
15203}
15204
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015205#ifdef HAVE_STDARG_H
15206/* This dummy va_list is here because:
15207 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15208 * - locally in the function results in a "used before set" warning
15209 * - using va_start() to initialize it gives "function with fixed args" error */
15210static va_list ap;
15211#endif
15212
Bram Moolenaar8c711452005-01-14 21:53:12 +000015213/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015214 * "printf()" function
15215 */
15216 static void
15217f_printf(argvars, rettv)
15218 typval_T *argvars;
15219 typval_T *rettv;
15220{
15221 rettv->v_type = VAR_STRING;
15222 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015223#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015224 {
15225 char_u buf[NUMBUFLEN];
15226 int len;
15227 char_u *s;
15228 int saved_did_emsg = did_emsg;
15229 char *fmt;
15230
15231 /* Get the required length, allocate the buffer and do it for real. */
15232 did_emsg = FALSE;
15233 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015234 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015235 if (!did_emsg)
15236 {
15237 s = alloc(len + 1);
15238 if (s != NULL)
15239 {
15240 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015241 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015242 }
15243 }
15244 did_emsg |= saved_did_emsg;
15245 }
15246#endif
15247}
15248
15249/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015250 * "pumvisible()" function
15251 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015252 static void
15253f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015254 typval_T *argvars UNUSED;
15255 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015256{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015257#ifdef FEAT_INS_EXPAND
15258 if (pum_visible())
15259 rettv->vval.v_number = 1;
15260#endif
15261}
15262
Bram Moolenaardb913952012-06-29 12:54:53 +020015263#ifdef FEAT_PYTHON3
15264/*
15265 * "py3eval()" function
15266 */
15267 static void
15268f_py3eval(argvars, rettv)
15269 typval_T *argvars;
15270 typval_T *rettv;
15271{
15272 char_u *str;
15273 char_u buf[NUMBUFLEN];
15274
15275 str = get_tv_string_buf(&argvars[0], buf);
15276 do_py3eval(str, rettv);
15277}
15278#endif
15279
15280#ifdef FEAT_PYTHON
15281/*
15282 * "pyeval()" function
15283 */
15284 static void
15285f_pyeval(argvars, rettv)
15286 typval_T *argvars;
15287 typval_T *rettv;
15288{
15289 char_u *str;
15290 char_u buf[NUMBUFLEN];
15291
15292 str = get_tv_string_buf(&argvars[0], buf);
15293 do_pyeval(str, rettv);
15294}
15295#endif
15296
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015297/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015298 * "range()" function
15299 */
15300 static void
15301f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015302 typval_T *argvars;
15303 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015304{
15305 long start;
15306 long end;
15307 long stride = 1;
15308 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015311 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015312 if (argvars[1].v_type == VAR_UNKNOWN)
15313 {
15314 end = start - 1;
15315 start = 0;
15316 }
15317 else
15318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015319 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015320 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015321 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015322 }
15323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015324 if (error)
15325 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015326 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015327 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015328 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015329 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015330 else
15331 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015332 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015333 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015334 if (list_append_number(rettv->vval.v_list,
15335 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015336 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015337 }
15338}
15339
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015340/*
15341 * "readfile()" function
15342 */
15343 static void
15344f_readfile(argvars, rettv)
15345 typval_T *argvars;
15346 typval_T *rettv;
15347{
15348 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015349 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015350 char_u *fname;
15351 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015352 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15353 int io_size = sizeof(buf);
15354 int readlen; /* size of last fread() */
15355 char_u *prev = NULL; /* previously read bytes, if any */
15356 long prevlen = 0; /* length of data in prev */
15357 long prevsize = 0; /* size of prev buffer */
15358 long maxline = MAXLNUM;
15359 long cnt = 0;
15360 char_u *p; /* position in buf */
15361 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015362
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015363 if (argvars[1].v_type != VAR_UNKNOWN)
15364 {
15365 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15366 binary = TRUE;
15367 if (argvars[2].v_type != VAR_UNKNOWN)
15368 maxline = get_tv_number(&argvars[2]);
15369 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015370
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015371 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015372 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015373
15374 /* Always open the file in binary mode, library functions have a mind of
15375 * their own about CR-LF conversion. */
15376 fname = get_tv_string(&argvars[0]);
15377 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15378 {
15379 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15380 return;
15381 }
15382
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015383 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015384 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015385 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015386
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015387 /* This for loop processes what was read, but is also entered at end
15388 * of file so that either:
15389 * - an incomplete line gets written
15390 * - a "binary" file gets an empty line at the end if it ends in a
15391 * newline. */
15392 for (p = buf, start = buf;
15393 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15394 ++p)
15395 {
15396 if (*p == '\n' || readlen <= 0)
15397 {
15398 listitem_T *li;
15399 char_u *s = NULL;
15400 long_u len = p - start;
15401
15402 /* Finished a line. Remove CRs before NL. */
15403 if (readlen > 0 && !binary)
15404 {
15405 while (len > 0 && start[len - 1] == '\r')
15406 --len;
15407 /* removal may cross back to the "prev" string */
15408 if (len == 0)
15409 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15410 --prevlen;
15411 }
15412 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015413 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015414 else
15415 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015416 /* Change "prev" buffer to be the right size. This way
15417 * the bytes are only copied once, and very long lines are
15418 * allocated only once. */
15419 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015420 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015421 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015422 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015423 prev = NULL; /* the list will own the string */
15424 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015425 }
15426 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015427 if (s == NULL)
15428 {
15429 do_outofmem_msg((long_u) prevlen + len + 1);
15430 failed = TRUE;
15431 break;
15432 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015433
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015434 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015435 {
15436 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015437 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015438 break;
15439 }
15440 li->li_tv.v_type = VAR_STRING;
15441 li->li_tv.v_lock = 0;
15442 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015443 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015444
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015445 start = p + 1; /* step over newline */
15446 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015447 break;
15448 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015449 else if (*p == NUL)
15450 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015451#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015452 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15453 * when finding the BF and check the previous two bytes. */
15454 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015455 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015456 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15457 * + 1, these may be in the "prev" string. */
15458 char_u back1 = p >= buf + 1 ? p[-1]
15459 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15460 char_u back2 = p >= buf + 2 ? p[-2]
15461 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15462 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015463
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015464 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015465 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015466 char_u *dest = p - 2;
15467
15468 /* Usually a BOM is at the beginning of a file, and so at
15469 * the beginning of a line; then we can just step over it.
15470 */
15471 if (start == dest)
15472 start = p + 1;
15473 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015474 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015475 /* have to shuffle buf to close gap */
15476 int adjust_prevlen = 0;
15477
15478 if (dest < buf)
15479 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015480 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015481 dest = buf;
15482 }
15483 if (readlen > p - buf + 1)
15484 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15485 readlen -= 3 - adjust_prevlen;
15486 prevlen -= adjust_prevlen;
15487 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015488 }
15489 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015490 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015491#endif
15492 } /* for */
15493
15494 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15495 break;
15496 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015497 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015498 /* There's part of a line in buf, store it in "prev". */
15499 if (p - start + prevlen >= prevsize)
15500 {
15501 /* need bigger "prev" buffer */
15502 char_u *newprev;
15503
15504 /* A common use case is ordinary text files and "prev" gets a
15505 * fragment of a line, so the first allocation is made
15506 * small, to avoid repeatedly 'allocing' large and
15507 * 'reallocing' small. */
15508 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015509 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015510 else
15511 {
15512 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015513 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015514 prevsize = grow50pc > growmin ? grow50pc : growmin;
15515 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015516 newprev = prev == NULL ? alloc(prevsize)
15517 : vim_realloc(prev, prevsize);
15518 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015519 {
15520 do_outofmem_msg((long_u)prevsize);
15521 failed = TRUE;
15522 break;
15523 }
15524 prev = newprev;
15525 }
15526 /* Add the line part to end of "prev". */
15527 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015528 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015529 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015530 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015531
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015532 /*
15533 * For a negative line count use only the lines at the end of the file,
15534 * free the rest.
15535 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015536 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015537 while (cnt > -maxline)
15538 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015539 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015540 --cnt;
15541 }
15542
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015543 if (failed)
15544 {
15545 list_free(rettv->vval.v_list, TRUE);
15546 /* readfile doc says an empty list is returned on error */
15547 rettv->vval.v_list = list_alloc();
15548 }
15549
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015550 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015551 fclose(fd);
15552}
15553
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015554#if defined(FEAT_RELTIME)
15555static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15556
15557/*
15558 * Convert a List to proftime_T.
15559 * Return FAIL when there is something wrong.
15560 */
15561 static int
15562list2proftime(arg, tm)
15563 typval_T *arg;
15564 proftime_T *tm;
15565{
15566 long n1, n2;
15567 int error = FALSE;
15568
15569 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15570 || arg->vval.v_list->lv_len != 2)
15571 return FAIL;
15572 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15573 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15574# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015575 tm->HighPart = n1;
15576 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015577# else
15578 tm->tv_sec = n1;
15579 tm->tv_usec = n2;
15580# endif
15581 return error ? FAIL : OK;
15582}
15583#endif /* FEAT_RELTIME */
15584
15585/*
15586 * "reltime()" function
15587 */
15588 static void
15589f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015590 typval_T *argvars UNUSED;
15591 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015592{
15593#ifdef FEAT_RELTIME
15594 proftime_T res;
15595 proftime_T start;
15596
15597 if (argvars[0].v_type == VAR_UNKNOWN)
15598 {
15599 /* No arguments: get current time. */
15600 profile_start(&res);
15601 }
15602 else if (argvars[1].v_type == VAR_UNKNOWN)
15603 {
15604 if (list2proftime(&argvars[0], &res) == FAIL)
15605 return;
15606 profile_end(&res);
15607 }
15608 else
15609 {
15610 /* Two arguments: compute the difference. */
15611 if (list2proftime(&argvars[0], &start) == FAIL
15612 || list2proftime(&argvars[1], &res) == FAIL)
15613 return;
15614 profile_sub(&res, &start);
15615 }
15616
15617 if (rettv_list_alloc(rettv) == OK)
15618 {
15619 long n1, n2;
15620
15621# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015622 n1 = res.HighPart;
15623 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015624# else
15625 n1 = res.tv_sec;
15626 n2 = res.tv_usec;
15627# endif
15628 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15629 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15630 }
15631#endif
15632}
15633
15634/*
15635 * "reltimestr()" function
15636 */
15637 static void
15638f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015639 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015640 typval_T *rettv;
15641{
15642#ifdef FEAT_RELTIME
15643 proftime_T tm;
15644#endif
15645
15646 rettv->v_type = VAR_STRING;
15647 rettv->vval.v_string = NULL;
15648#ifdef FEAT_RELTIME
15649 if (list2proftime(&argvars[0], &tm) == OK)
15650 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15651#endif
15652}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015653
Bram Moolenaar0d660222005-01-07 21:51:51 +000015654#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15655static void make_connection __ARGS((void));
15656static int check_connection __ARGS((void));
15657
15658 static void
15659make_connection()
15660{
15661 if (X_DISPLAY == NULL
15662# ifdef FEAT_GUI
15663 && !gui.in_use
15664# endif
15665 )
15666 {
15667 x_force_connect = TRUE;
15668 setup_term_clip();
15669 x_force_connect = FALSE;
15670 }
15671}
15672
15673 static int
15674check_connection()
15675{
15676 make_connection();
15677 if (X_DISPLAY == NULL)
15678 {
15679 EMSG(_("E240: No connection to Vim server"));
15680 return FAIL;
15681 }
15682 return OK;
15683}
15684#endif
15685
15686#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015687static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015688
15689 static void
15690remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015691 typval_T *argvars;
15692 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015693 int expr;
15694{
15695 char_u *server_name;
15696 char_u *keys;
15697 char_u *r = NULL;
15698 char_u buf[NUMBUFLEN];
15699# ifdef WIN32
15700 HWND w;
15701# else
15702 Window w;
15703# endif
15704
15705 if (check_restricted() || check_secure())
15706 return;
15707
15708# ifdef FEAT_X11
15709 if (check_connection() == FAIL)
15710 return;
15711# endif
15712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015713 server_name = get_tv_string_chk(&argvars[0]);
15714 if (server_name == NULL)
15715 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015716 keys = get_tv_string_buf(&argvars[1], buf);
15717# ifdef WIN32
15718 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15719# else
15720 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15721 < 0)
15722# endif
15723 {
15724 if (r != NULL)
15725 EMSG(r); /* sending worked but evaluation failed */
15726 else
15727 EMSG2(_("E241: Unable to send to %s"), server_name);
15728 return;
15729 }
15730
15731 rettv->vval.v_string = r;
15732
15733 if (argvars[2].v_type != VAR_UNKNOWN)
15734 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015735 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015736 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015737 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015738
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015739 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015740 v.di_tv.v_type = VAR_STRING;
15741 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015742 idvar = get_tv_string_chk(&argvars[2]);
15743 if (idvar != NULL)
15744 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015745 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015746 }
15747}
15748#endif
15749
15750/*
15751 * "remote_expr()" function
15752 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753 static void
15754f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015755 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015756 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015757{
15758 rettv->v_type = VAR_STRING;
15759 rettv->vval.v_string = NULL;
15760#ifdef FEAT_CLIENTSERVER
15761 remote_common(argvars, rettv, TRUE);
15762#endif
15763}
15764
15765/*
15766 * "remote_foreground()" function
15767 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015768 static void
15769f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015770 typval_T *argvars UNUSED;
15771 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015772{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015773#ifdef FEAT_CLIENTSERVER
15774# ifdef WIN32
15775 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015776 {
15777 char_u *server_name = get_tv_string_chk(&argvars[0]);
15778
15779 if (server_name != NULL)
15780 serverForeground(server_name);
15781 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015782# else
15783 /* Send a foreground() expression to the server. */
15784 argvars[1].v_type = VAR_STRING;
15785 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15786 argvars[2].v_type = VAR_UNKNOWN;
15787 remote_common(argvars, rettv, TRUE);
15788 vim_free(argvars[1].vval.v_string);
15789# endif
15790#endif
15791}
15792
Bram Moolenaar0d660222005-01-07 21:51:51 +000015793 static void
15794f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015795 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015796 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015797{
15798#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015799 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015800 char_u *s = NULL;
15801# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015802 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015803# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015804 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015805
15806 if (check_restricted() || check_secure())
15807 {
15808 rettv->vval.v_number = -1;
15809 return;
15810 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015811 serverid = get_tv_string_chk(&argvars[0]);
15812 if (serverid == NULL)
15813 {
15814 rettv->vval.v_number = -1;
15815 return; /* type error; errmsg already given */
15816 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015817# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015818 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015819 if (n == 0)
15820 rettv->vval.v_number = -1;
15821 else
15822 {
15823 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15824 rettv->vval.v_number = (s != NULL);
15825 }
15826# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015827 if (check_connection() == FAIL)
15828 return;
15829
15830 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015831 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015832# endif
15833
15834 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015836 char_u *retvar;
15837
Bram Moolenaar33570922005-01-25 22:26:29 +000015838 v.di_tv.v_type = VAR_STRING;
15839 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015840 retvar = get_tv_string_chk(&argvars[1]);
15841 if (retvar != NULL)
15842 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015843 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015844 }
15845#else
15846 rettv->vval.v_number = -1;
15847#endif
15848}
15849
Bram Moolenaar0d660222005-01-07 21:51:51 +000015850 static void
15851f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015852 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015853 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015854{
15855 char_u *r = NULL;
15856
15857#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015858 char_u *serverid = get_tv_string_chk(&argvars[0]);
15859
15860 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015861 {
15862# ifdef WIN32
15863 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015864 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015865
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015866 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015867 if (n != 0)
15868 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15869 if (r == NULL)
15870# else
15871 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015872 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015873# endif
15874 EMSG(_("E277: Unable to read a server reply"));
15875 }
15876#endif
15877 rettv->v_type = VAR_STRING;
15878 rettv->vval.v_string = r;
15879}
15880
15881/*
15882 * "remote_send()" function
15883 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015884 static void
15885f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015886 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015887 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015888{
15889 rettv->v_type = VAR_STRING;
15890 rettv->vval.v_string = NULL;
15891#ifdef FEAT_CLIENTSERVER
15892 remote_common(argvars, rettv, FALSE);
15893#endif
15894}
15895
15896/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015897 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015898 */
15899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015900f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015901 typval_T *argvars;
15902 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015903{
Bram Moolenaar33570922005-01-25 22:26:29 +000015904 list_T *l;
15905 listitem_T *item, *item2;
15906 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015907 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015908 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015909 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015910 dict_T *d;
15911 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015912 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015913
Bram Moolenaar8c711452005-01-14 21:53:12 +000015914 if (argvars[0].v_type == VAR_DICT)
15915 {
15916 if (argvars[2].v_type != VAR_UNKNOWN)
15917 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015918 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015919 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015921 key = get_tv_string_chk(&argvars[1]);
15922 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015924 di = dict_find(d, key, -1);
15925 if (di == NULL)
15926 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015927 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15928 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015929 {
15930 *rettv = di->di_tv;
15931 init_tv(&di->di_tv);
15932 dictitem_remove(d, di);
15933 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015935 }
15936 }
15937 else if (argvars[0].v_type != VAR_LIST)
15938 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015939 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015940 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015942 int error = FALSE;
15943
15944 idx = get_tv_number_chk(&argvars[1], &error);
15945 if (error)
15946 ; /* type error: do nothing, errmsg already given */
15947 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015948 EMSGN(_(e_listidx), idx);
15949 else
15950 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015951 if (argvars[2].v_type == VAR_UNKNOWN)
15952 {
15953 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015954 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015955 *rettv = item->li_tv;
15956 vim_free(item);
15957 }
15958 else
15959 {
15960 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015961 end = get_tv_number_chk(&argvars[2], &error);
15962 if (error)
15963 ; /* type error: do nothing */
15964 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015965 EMSGN(_(e_listidx), end);
15966 else
15967 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015968 int cnt = 0;
15969
15970 for (li = item; li != NULL; li = li->li_next)
15971 {
15972 ++cnt;
15973 if (li == item2)
15974 break;
15975 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015976 if (li == NULL) /* didn't find "item2" after "item" */
15977 EMSG(_(e_invrange));
15978 else
15979 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015980 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015981 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015982 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015983 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015984 l->lv_first = item;
15985 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015986 item->li_prev = NULL;
15987 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015988 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015989 }
15990 }
15991 }
15992 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015993 }
15994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995}
15996
15997/*
15998 * "rename({from}, {to})" function
15999 */
16000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016001f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 typval_T *argvars;
16003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004{
16005 char_u buf[NUMBUFLEN];
16006
16007 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016008 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016010 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16011 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012}
16013
16014/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016015 * "repeat()" function
16016 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016017 static void
16018f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016019 typval_T *argvars;
16020 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016021{
16022 char_u *p;
16023 int n;
16024 int slen;
16025 int len;
16026 char_u *r;
16027 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016028
16029 n = get_tv_number(&argvars[1]);
16030 if (argvars[0].v_type == VAR_LIST)
16031 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016032 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016033 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016034 if (list_extend(rettv->vval.v_list,
16035 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016036 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016037 }
16038 else
16039 {
16040 p = get_tv_string(&argvars[0]);
16041 rettv->v_type = VAR_STRING;
16042 rettv->vval.v_string = NULL;
16043
16044 slen = (int)STRLEN(p);
16045 len = slen * n;
16046 if (len <= 0)
16047 return;
16048
16049 r = alloc(len + 1);
16050 if (r != NULL)
16051 {
16052 for (i = 0; i < n; i++)
16053 mch_memmove(r + i * slen, p, (size_t)slen);
16054 r[len] = NUL;
16055 }
16056
16057 rettv->vval.v_string = r;
16058 }
16059}
16060
16061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 * "resolve()" function
16063 */
16064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016065f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016066 typval_T *argvars;
16067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068{
16069 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016070#ifdef HAVE_READLINK
16071 char_u *buf = NULL;
16072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016074 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075#ifdef FEAT_SHORTCUT
16076 {
16077 char_u *v = NULL;
16078
16079 v = mch_resolve_shortcut(p);
16080 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016081 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016083 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 }
16085#else
16086# ifdef HAVE_READLINK
16087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 char_u *cpy;
16089 int len;
16090 char_u *remain = NULL;
16091 char_u *q;
16092 int is_relative_to_current = FALSE;
16093 int has_trailing_pathsep = FALSE;
16094 int limit = 100;
16095
16096 p = vim_strsave(p);
16097
16098 if (p[0] == '.' && (vim_ispathsep(p[1])
16099 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16100 is_relative_to_current = TRUE;
16101
16102 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016103 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016106 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108
16109 q = getnextcomp(p);
16110 if (*q != NUL)
16111 {
16112 /* Separate the first path component in "p", and keep the
16113 * remainder (beginning with the path separator). */
16114 remain = vim_strsave(q - 1);
16115 q[-1] = NUL;
16116 }
16117
Bram Moolenaard9462e32011-04-11 21:35:11 +020016118 buf = alloc(MAXPATHL + 1);
16119 if (buf == NULL)
16120 goto fail;
16121
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 for (;;)
16123 {
16124 for (;;)
16125 {
16126 len = readlink((char *)p, (char *)buf, MAXPATHL);
16127 if (len <= 0)
16128 break;
16129 buf[len] = NUL;
16130
16131 if (limit-- == 0)
16132 {
16133 vim_free(p);
16134 vim_free(remain);
16135 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016136 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137 goto fail;
16138 }
16139
16140 /* Ensure that the result will have a trailing path separator
16141 * if the argument has one. */
16142 if (remain == NULL && has_trailing_pathsep)
16143 add_pathsep(buf);
16144
16145 /* Separate the first path component in the link value and
16146 * concatenate the remainders. */
16147 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16148 if (*q != NUL)
16149 {
16150 if (remain == NULL)
16151 remain = vim_strsave(q - 1);
16152 else
16153 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016154 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 if (cpy != NULL)
16156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 vim_free(remain);
16158 remain = cpy;
16159 }
16160 }
16161 q[-1] = NUL;
16162 }
16163
16164 q = gettail(p);
16165 if (q > p && *q == NUL)
16166 {
16167 /* Ignore trailing path separator. */
16168 q[-1] = NUL;
16169 q = gettail(p);
16170 }
16171 if (q > p && !mch_isFullName(buf))
16172 {
16173 /* symlink is relative to directory of argument */
16174 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16175 if (cpy != NULL)
16176 {
16177 STRCPY(cpy, p);
16178 STRCPY(gettail(cpy), buf);
16179 vim_free(p);
16180 p = cpy;
16181 }
16182 }
16183 else
16184 {
16185 vim_free(p);
16186 p = vim_strsave(buf);
16187 }
16188 }
16189
16190 if (remain == NULL)
16191 break;
16192
16193 /* Append the first path component of "remain" to "p". */
16194 q = getnextcomp(remain + 1);
16195 len = q - remain - (*q != NUL);
16196 cpy = vim_strnsave(p, STRLEN(p) + len);
16197 if (cpy != NULL)
16198 {
16199 STRNCAT(cpy, remain, len);
16200 vim_free(p);
16201 p = cpy;
16202 }
16203 /* Shorten "remain". */
16204 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016205 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 else
16207 {
16208 vim_free(remain);
16209 remain = NULL;
16210 }
16211 }
16212
16213 /* If the result is a relative path name, make it explicitly relative to
16214 * the current directory if and only if the argument had this form. */
16215 if (!vim_ispathsep(*p))
16216 {
16217 if (is_relative_to_current
16218 && *p != NUL
16219 && !(p[0] == '.'
16220 && (p[1] == NUL
16221 || vim_ispathsep(p[1])
16222 || (p[1] == '.'
16223 && (p[2] == NUL
16224 || vim_ispathsep(p[2]))))))
16225 {
16226 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016227 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 if (cpy != NULL)
16229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 vim_free(p);
16231 p = cpy;
16232 }
16233 }
16234 else if (!is_relative_to_current)
16235 {
16236 /* Strip leading "./". */
16237 q = p;
16238 while (q[0] == '.' && vim_ispathsep(q[1]))
16239 q += 2;
16240 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016241 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 }
16243 }
16244
16245 /* Ensure that the result will have no trailing path separator
16246 * if the argument had none. But keep "/" or "//". */
16247 if (!has_trailing_pathsep)
16248 {
16249 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016250 if (after_pathsep(p, q))
16251 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 }
16253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016254 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 }
16256# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016257 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258# endif
16259#endif
16260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016261 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262
16263#ifdef HAVE_READLINK
16264fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016265 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016267 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268}
16269
16270/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016271 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272 */
16273 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016274f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016275 typval_T *argvars;
16276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277{
Bram Moolenaar33570922005-01-25 22:26:29 +000016278 list_T *l;
16279 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280
Bram Moolenaar0d660222005-01-07 21:51:51 +000016281 if (argvars[0].v_type != VAR_LIST)
16282 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016283 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016284 && !tv_check_lock(l->lv_lock,
16285 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286 {
16287 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016288 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016289 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290 while (li != NULL)
16291 {
16292 ni = li->li_prev;
16293 list_append(l, li);
16294 li = ni;
16295 }
16296 rettv->vval.v_list = l;
16297 rettv->v_type = VAR_LIST;
16298 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016299 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301}
16302
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016303#define SP_NOMOVE 0x01 /* don't move cursor */
16304#define SP_REPEAT 0x02 /* repeat to find outer pair */
16305#define SP_RETCOUNT 0x04 /* return matchcount */
16306#define SP_SETPCMARK 0x08 /* set previous context mark */
16307#define SP_START 0x10 /* accept match at start position */
16308#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16309#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016310
Bram Moolenaar33570922005-01-25 22:26:29 +000016311static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312
16313/*
16314 * Get flags for a search function.
16315 * Possibly sets "p_ws".
16316 * Returns BACKWARD, FORWARD or zero (for an error).
16317 */
16318 static int
16319get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016320 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016321 int *flagsp;
16322{
16323 int dir = FORWARD;
16324 char_u *flags;
16325 char_u nbuf[NUMBUFLEN];
16326 int mask;
16327
16328 if (varp->v_type != VAR_UNKNOWN)
16329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016330 flags = get_tv_string_buf_chk(varp, nbuf);
16331 if (flags == NULL)
16332 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016333 while (*flags != NUL)
16334 {
16335 switch (*flags)
16336 {
16337 case 'b': dir = BACKWARD; break;
16338 case 'w': p_ws = TRUE; break;
16339 case 'W': p_ws = FALSE; break;
16340 default: mask = 0;
16341 if (flagsp != NULL)
16342 switch (*flags)
16343 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016344 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016345 case 'e': mask = SP_END; break;
16346 case 'm': mask = SP_RETCOUNT; break;
16347 case 'n': mask = SP_NOMOVE; break;
16348 case 'p': mask = SP_SUBPAT; break;
16349 case 'r': mask = SP_REPEAT; break;
16350 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351 }
16352 if (mask == 0)
16353 {
16354 EMSG2(_(e_invarg2), flags);
16355 dir = 0;
16356 }
16357 else
16358 *flagsp |= mask;
16359 }
16360 if (dir == 0)
16361 break;
16362 ++flags;
16363 }
16364 }
16365 return dir;
16366}
16367
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016369 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016371 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016372search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016373 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016374 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016375 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016377 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 char_u *pat;
16379 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016380 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 int save_p_ws = p_ws;
16382 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016383 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016384 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016385 proftime_T tm;
16386#ifdef FEAT_RELTIME
16387 long time_limit = 0;
16388#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016389 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016390 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016392 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016393 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016394 if (dir == 0)
16395 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016396 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016397 if (flags & SP_START)
16398 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016399 if (flags & SP_END)
16400 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016401
Bram Moolenaar76929292008-01-06 19:07:36 +000016402 /* Optional arguments: line number to stop searching and timeout. */
16403 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016404 {
16405 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16406 if (lnum_stop < 0)
16407 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016408#ifdef FEAT_RELTIME
16409 if (argvars[3].v_type != VAR_UNKNOWN)
16410 {
16411 time_limit = get_tv_number_chk(&argvars[3], NULL);
16412 if (time_limit < 0)
16413 goto theend;
16414 }
16415#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016416 }
16417
Bram Moolenaar76929292008-01-06 19:07:36 +000016418#ifdef FEAT_RELTIME
16419 /* Set the time limit, if there is one. */
16420 profile_setlimit(time_limit, &tm);
16421#endif
16422
Bram Moolenaar231334e2005-07-25 20:46:57 +000016423 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016424 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016425 * Check to make sure only those flags are set.
16426 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16427 * flags cannot be set. Check for that condition also.
16428 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016429 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016430 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016432 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016433 goto theend;
16434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016436 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016437 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016438 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016439 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016441 if (flags & SP_SUBPAT)
16442 retval = subpatnum;
16443 else
16444 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016445 if (flags & SP_SETPCMARK)
16446 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016448 if (match_pos != NULL)
16449 {
16450 /* Store the match cursor position */
16451 match_pos->lnum = pos.lnum;
16452 match_pos->col = pos.col + 1;
16453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 /* "/$" will put the cursor after the end of the line, may need to
16455 * correct that here */
16456 check_cursor();
16457 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016458
16459 /* If 'n' flag is used: restore cursor position. */
16460 if (flags & SP_NOMOVE)
16461 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016462 else
16463 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016464theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016466
16467 return retval;
16468}
16469
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016470#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016471
16472/*
16473 * round() is not in C90, use ceil() or floor() instead.
16474 */
16475 float_T
16476vim_round(f)
16477 float_T f;
16478{
16479 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16480}
16481
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016482/*
16483 * "round({float})" function
16484 */
16485 static void
16486f_round(argvars, rettv)
16487 typval_T *argvars;
16488 typval_T *rettv;
16489{
16490 float_T f;
16491
16492 rettv->v_type = VAR_FLOAT;
16493 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016494 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016495 else
16496 rettv->vval.v_float = 0.0;
16497}
16498#endif
16499
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016500/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016501 * "screenattr()" function
16502 */
16503 static void
16504f_screenattr(argvars, rettv)
16505 typval_T *argvars UNUSED;
16506 typval_T *rettv;
16507{
16508 int row;
16509 int col;
16510 int c;
16511
16512 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16513 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16514 if (row < 0 || row >= screen_Rows
16515 || col < 0 || col >= screen_Columns)
16516 c = -1;
16517 else
16518 c = ScreenAttrs[LineOffset[row] + col];
16519 rettv->vval.v_number = c;
16520}
16521
16522/*
16523 * "screenchar()" function
16524 */
16525 static void
16526f_screenchar(argvars, rettv)
16527 typval_T *argvars UNUSED;
16528 typval_T *rettv;
16529{
16530 int row;
16531 int col;
16532 int off;
16533 int c;
16534
16535 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16536 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16537 if (row < 0 || row >= screen_Rows
16538 || col < 0 || col >= screen_Columns)
16539 c = -1;
16540 else
16541 {
16542 off = LineOffset[row] + col;
16543#ifdef FEAT_MBYTE
16544 if (enc_utf8 && ScreenLinesUC[off] != 0)
16545 c = ScreenLinesUC[off];
16546 else
16547#endif
16548 c = ScreenLines[off];
16549 }
16550 rettv->vval.v_number = c;
16551}
16552
16553/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016554 * "screencol()" function
16555 *
16556 * First column is 1 to be consistent with virtcol().
16557 */
16558 static void
16559f_screencol(argvars, rettv)
16560 typval_T *argvars UNUSED;
16561 typval_T *rettv;
16562{
16563 rettv->vval.v_number = screen_screencol() + 1;
16564}
16565
16566/*
16567 * "screenrow()" function
16568 */
16569 static void
16570f_screenrow(argvars, rettv)
16571 typval_T *argvars UNUSED;
16572 typval_T *rettv;
16573{
16574 rettv->vval.v_number = screen_screenrow() + 1;
16575}
16576
16577/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016578 * "search()" function
16579 */
16580 static void
16581f_search(argvars, rettv)
16582 typval_T *argvars;
16583 typval_T *rettv;
16584{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016585 int flags = 0;
16586
16587 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588}
16589
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016591 * "searchdecl()" function
16592 */
16593 static void
16594f_searchdecl(argvars, rettv)
16595 typval_T *argvars;
16596 typval_T *rettv;
16597{
16598 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016599 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016600 int error = FALSE;
16601 char_u *name;
16602
16603 rettv->vval.v_number = 1; /* default: FAIL */
16604
16605 name = get_tv_string_chk(&argvars[0]);
16606 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016607 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016608 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016609 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16610 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16611 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016612 if (!error && name != NULL)
16613 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016614 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016615}
16616
16617/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016618 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016620 static int
16621searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016622 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016623 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624{
16625 char_u *spat, *mpat, *epat;
16626 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 int dir;
16629 int flags = 0;
16630 char_u nbuf1[NUMBUFLEN];
16631 char_u nbuf2[NUMBUFLEN];
16632 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016633 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016634 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016635 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016638 spat = get_tv_string_chk(&argvars[0]);
16639 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16640 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16641 if (spat == NULL || mpat == NULL || epat == NULL)
16642 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 /* Handle the optional fourth argument: flags */
16645 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016646 if (dir == 0)
16647 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016648
16649 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016650 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16651 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016652 if ((flags & (SP_END | SP_SUBPAT)) != 0
16653 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016654 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016655 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016656 goto theend;
16657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016659 /* Using 'r' implies 'W', otherwise it doesn't work. */
16660 if (flags & SP_REPEAT)
16661 p_ws = FALSE;
16662
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016663 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016664 if (argvars[3].v_type == VAR_UNKNOWN
16665 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666 skip = (char_u *)"";
16667 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016669 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016670 if (argvars[5].v_type != VAR_UNKNOWN)
16671 {
16672 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16673 if (lnum_stop < 0)
16674 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016675#ifdef FEAT_RELTIME
16676 if (argvars[6].v_type != VAR_UNKNOWN)
16677 {
16678 time_limit = get_tv_number_chk(&argvars[6], NULL);
16679 if (time_limit < 0)
16680 goto theend;
16681 }
16682#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016683 }
16684 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016685 if (skip == NULL)
16686 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016688 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016689 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016690
16691theend:
16692 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016693
16694 return retval;
16695}
16696
16697/*
16698 * "searchpair()" function
16699 */
16700 static void
16701f_searchpair(argvars, rettv)
16702 typval_T *argvars;
16703 typval_T *rettv;
16704{
16705 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16706}
16707
16708/*
16709 * "searchpairpos()" function
16710 */
16711 static void
16712f_searchpairpos(argvars, rettv)
16713 typval_T *argvars;
16714 typval_T *rettv;
16715{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016716 pos_T match_pos;
16717 int lnum = 0;
16718 int col = 0;
16719
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016720 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016721 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016722
16723 if (searchpair_cmn(argvars, &match_pos) > 0)
16724 {
16725 lnum = match_pos.lnum;
16726 col = match_pos.col;
16727 }
16728
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016729 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16730 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016731}
16732
16733/*
16734 * Search for a start/middle/end thing.
16735 * Used by searchpair(), see its documentation for the details.
16736 * Returns 0 or -1 for no match,
16737 */
16738 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016739do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16740 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016741 char_u *spat; /* start pattern */
16742 char_u *mpat; /* middle pattern */
16743 char_u *epat; /* end pattern */
16744 int dir; /* BACKWARD or FORWARD */
16745 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016746 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016747 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016748 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016749 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016750{
16751 char_u *save_cpo;
16752 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16753 long retval = 0;
16754 pos_T pos;
16755 pos_T firstpos;
16756 pos_T foundpos;
16757 pos_T save_cursor;
16758 pos_T save_pos;
16759 int n;
16760 int r;
16761 int nest = 1;
16762 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016763 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016764 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016765
16766 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16767 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016768 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016769
Bram Moolenaar76929292008-01-06 19:07:36 +000016770#ifdef FEAT_RELTIME
16771 /* Set the time limit, if there is one. */
16772 profile_setlimit(time_limit, &tm);
16773#endif
16774
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016775 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16776 * start/middle/end (pat3, for the top pair). */
16777 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16778 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16779 if (pat2 == NULL || pat3 == NULL)
16780 goto theend;
16781 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16782 if (*mpat == NUL)
16783 STRCPY(pat3, pat2);
16784 else
16785 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16786 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016787 if (flags & SP_START)
16788 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016789
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 save_cursor = curwin->w_cursor;
16791 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016792 clearpos(&firstpos);
16793 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 pat = pat3;
16795 for (;;)
16796 {
16797 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016798 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16800 /* didn't find it or found the first match again: FAIL */
16801 break;
16802
16803 if (firstpos.lnum == 0)
16804 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016805 if (equalpos(pos, foundpos))
16806 {
16807 /* Found the same position again. Can happen with a pattern that
16808 * has "\zs" at the end and searching backwards. Advance one
16809 * character and try again. */
16810 if (dir == BACKWARD)
16811 decl(&pos);
16812 else
16813 incl(&pos);
16814 }
16815 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016817 /* clear the start flag to avoid getting stuck here */
16818 options &= ~SEARCH_START;
16819
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820 /* If the skip pattern matches, ignore this match. */
16821 if (*skip != NUL)
16822 {
16823 save_pos = curwin->w_cursor;
16824 curwin->w_cursor = pos;
16825 r = eval_to_bool(skip, &err, NULL, FALSE);
16826 curwin->w_cursor = save_pos;
16827 if (err)
16828 {
16829 /* Evaluating {skip} caused an error, break here. */
16830 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016831 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 break;
16833 }
16834 if (r)
16835 continue;
16836 }
16837
16838 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16839 {
16840 /* Found end when searching backwards or start when searching
16841 * forward: nested pair. */
16842 ++nest;
16843 pat = pat2; /* nested, don't search for middle */
16844 }
16845 else
16846 {
16847 /* Found end when searching forward or start when searching
16848 * backward: end of (nested) pair; or found middle in outer pair. */
16849 if (--nest == 1)
16850 pat = pat3; /* outer level, search for middle */
16851 }
16852
16853 if (nest == 0)
16854 {
16855 /* Found the match: return matchcount or line number. */
16856 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016857 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016859 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016860 if (flags & SP_SETPCMARK)
16861 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 curwin->w_cursor = pos;
16863 if (!(flags & SP_REPEAT))
16864 break;
16865 nest = 1; /* search for next unmatched */
16866 }
16867 }
16868
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016869 if (match_pos != NULL)
16870 {
16871 /* Store the match cursor position */
16872 match_pos->lnum = curwin->w_cursor.lnum;
16873 match_pos->col = curwin->w_cursor.col + 1;
16874 }
16875
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016877 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 curwin->w_cursor = save_cursor;
16879
16880theend:
16881 vim_free(pat2);
16882 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016883 if (p_cpo == empty_option)
16884 p_cpo = save_cpo;
16885 else
16886 /* Darn, evaluating the {skip} expression changed the value. */
16887 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016888
16889 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890}
16891
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016892/*
16893 * "searchpos()" function
16894 */
16895 static void
16896f_searchpos(argvars, rettv)
16897 typval_T *argvars;
16898 typval_T *rettv;
16899{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016900 pos_T match_pos;
16901 int lnum = 0;
16902 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016903 int n;
16904 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016905
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016906 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016907 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016908
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016909 n = search_cmn(argvars, &match_pos, &flags);
16910 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016911 {
16912 lnum = match_pos.lnum;
16913 col = match_pos.col;
16914 }
16915
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016916 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16917 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016918 if (flags & SP_SUBPAT)
16919 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016920}
16921
16922
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 static void
16924f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016925 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928#ifdef FEAT_CLIENTSERVER
16929 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016930 char_u *server = get_tv_string_chk(&argvars[0]);
16931 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016934 if (server == NULL || reply == NULL)
16935 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936 if (check_restricted() || check_secure())
16937 return;
16938# ifdef FEAT_X11
16939 if (check_connection() == FAIL)
16940 return;
16941# endif
16942
16943 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945 EMSG(_("E258: Unable to send to client"));
16946 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 rettv->vval.v_number = 0;
16949#else
16950 rettv->vval.v_number = -1;
16951#endif
16952}
16953
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954 static void
16955f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016956 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016957 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958{
16959 char_u *r = NULL;
16960
16961#ifdef FEAT_CLIENTSERVER
16962# ifdef WIN32
16963 r = serverGetVimNames();
16964# else
16965 make_connection();
16966 if (X_DISPLAY != NULL)
16967 r = serverGetVimNames(X_DISPLAY);
16968# endif
16969#endif
16970 rettv->v_type = VAR_STRING;
16971 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972}
16973
16974/*
16975 * "setbufvar()" function
16976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016978f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016979 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016980 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981{
16982 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016985 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 char_u nbuf[NUMBUFLEN];
16987
16988 if (check_restricted() || check_secure())
16989 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016990 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16991 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016992 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 varp = &argvars[2];
16994
16995 if (buf != NULL && varname != NULL && varp != NULL)
16996 {
16997 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999
17000 if (*varname == '&')
17001 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017002 long numval;
17003 char_u *strval;
17004 int error = FALSE;
17005
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017007 numval = get_tv_number_chk(varp, &error);
17008 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 if (!error && strval != NULL)
17010 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 }
17012 else
17013 {
17014 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17015 if (bufvarname != NULL)
17016 {
17017 STRCPY(bufvarname, "b:");
17018 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017019 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 vim_free(bufvarname);
17021 }
17022 }
17023
17024 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027}
17028
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017029 static void
17030f_setcharsearch(argvars, rettv)
17031 typval_T *argvars;
17032 typval_T *rettv UNUSED;
17033{
17034 dict_T *d;
17035 dictitem_T *di;
17036 char_u *csearch;
17037
17038 if (argvars[0].v_type != VAR_DICT)
17039 {
17040 EMSG(_(e_dictreq));
17041 return;
17042 }
17043
17044 if ((d = argvars[0].vval.v_dict) != NULL)
17045 {
17046 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17047 if (csearch != NULL)
17048 {
17049 if (enc_utf8)
17050 {
17051 int pcc[MAX_MCO];
17052 int c = utfc_ptr2char(csearch, pcc);
17053 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17054 }
17055 else
17056 set_last_csearch(mb_ptr2char(csearch),
17057 csearch, mb_ptr2len(csearch));
17058 }
17059
17060 di = dict_find(d, (char_u *)"forward", -1);
17061 if (di != NULL)
17062 set_csearch_direction(get_tv_number(&di->di_tv)
17063 ? FORWARD : BACKWARD);
17064
17065 di = dict_find(d, (char_u *)"until", -1);
17066 if (di != NULL)
17067 set_csearch_until(!!get_tv_number(&di->di_tv));
17068 }
17069}
17070
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071/*
17072 * "setcmdpos()" function
17073 */
17074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017075f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017076 typval_T *argvars;
17077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017079 int pos = (int)get_tv_number(&argvars[0]) - 1;
17080
17081 if (pos >= 0)
17082 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083}
17084
17085/*
17086 * "setline()" function
17087 */
17088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017089f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017090 typval_T *argvars;
17091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092{
17093 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017094 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017095 list_T *l = NULL;
17096 listitem_T *li = NULL;
17097 long added = 0;
17098 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017100 lnum = get_tv_lnum(&argvars[0]);
17101 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017103 l = argvars[1].vval.v_list;
17104 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017106 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017107 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017108
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017109 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017110 for (;;)
17111 {
17112 if (l != NULL)
17113 {
17114 /* list argument, get next string */
17115 if (li == NULL)
17116 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017117 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017118 li = li->li_next;
17119 }
17120
17121 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017122 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017123 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017124
17125 /* When coming here from Insert mode, sync undo, so that this can be
17126 * undone separately from what was previously inserted. */
17127 if (u_sync_once == 2)
17128 {
17129 u_sync_once = 1; /* notify that u_sync() was called */
17130 u_sync(TRUE);
17131 }
17132
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017133 if (lnum <= curbuf->b_ml.ml_line_count)
17134 {
17135 /* existing line, replace it */
17136 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17137 {
17138 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017139 if (lnum == curwin->w_cursor.lnum)
17140 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017141 rettv->vval.v_number = 0; /* OK */
17142 }
17143 }
17144 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17145 {
17146 /* lnum is one past the last line, append the line */
17147 ++added;
17148 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17149 rettv->vval.v_number = 0; /* OK */
17150 }
17151
17152 if (l == NULL) /* only one string argument */
17153 break;
17154 ++lnum;
17155 }
17156
17157 if (added > 0)
17158 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159}
17160
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017161static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17162
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017164 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017165 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017166 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017167set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017168 win_T *wp UNUSED;
17169 typval_T *list_arg UNUSED;
17170 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017171 typval_T *rettv;
17172{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017173#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017174 char_u *act;
17175 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017176#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017177
Bram Moolenaar2641f772005-03-25 21:58:17 +000017178 rettv->vval.v_number = -1;
17179
17180#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017181 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017182 EMSG(_(e_listreq));
17183 else
17184 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017185 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017186
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017187 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017188 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017189 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017190 if (act == NULL)
17191 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017192 if (*act == 'a' || *act == 'r')
17193 action = *act;
17194 }
17195
Bram Moolenaar81484f42012-12-05 15:16:47 +010017196 if (l != NULL && set_errorlist(wp, l, action,
17197 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017198 rettv->vval.v_number = 0;
17199 }
17200#endif
17201}
17202
17203/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017204 * "setloclist()" function
17205 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017206 static void
17207f_setloclist(argvars, rettv)
17208 typval_T *argvars;
17209 typval_T *rettv;
17210{
17211 win_T *win;
17212
17213 rettv->vval.v_number = -1;
17214
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017215 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017216 if (win != NULL)
17217 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17218}
17219
17220/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017221 * "setmatches()" function
17222 */
17223 static void
17224f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017225 typval_T *argvars UNUSED;
17226 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017227{
17228#ifdef FEAT_SEARCH_EXTRA
17229 list_T *l;
17230 listitem_T *li;
17231 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017232 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017233
17234 rettv->vval.v_number = -1;
17235 if (argvars[0].v_type != VAR_LIST)
17236 {
17237 EMSG(_(e_listreq));
17238 return;
17239 }
17240 if ((l = argvars[0].vval.v_list) != NULL)
17241 {
17242
17243 /* To some extent make sure that we are dealing with a list from
17244 * "getmatches()". */
17245 li = l->lv_first;
17246 while (li != NULL)
17247 {
17248 if (li->li_tv.v_type != VAR_DICT
17249 || (d = li->li_tv.vval.v_dict) == NULL)
17250 {
17251 EMSG(_(e_invarg));
17252 return;
17253 }
17254 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017255 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17256 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017257 && dict_find(d, (char_u *)"priority", -1) != NULL
17258 && dict_find(d, (char_u *)"id", -1) != NULL))
17259 {
17260 EMSG(_(e_invarg));
17261 return;
17262 }
17263 li = li->li_next;
17264 }
17265
17266 clear_matches(curwin);
17267 li = l->lv_first;
17268 while (li != NULL)
17269 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017270 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017271 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017272 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017273 char_u *group;
17274 int priority;
17275 int id;
17276 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017277
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017278 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017279 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17280 {
17281 if (s == NULL)
17282 {
17283 s = list_alloc();
17284 if (s == NULL)
17285 return;
17286 }
17287
17288 /* match from matchaddpos() */
17289 for (i = 1; i < 9; i++)
17290 {
17291 sprintf((char *)buf, (char *)"pos%d", i);
17292 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17293 {
17294 if (di->di_tv.v_type != VAR_LIST)
17295 return;
17296
17297 list_append_tv(s, &di->di_tv);
17298 s->lv_refcount++;
17299 }
17300 else
17301 break;
17302 }
17303 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017304
17305 group = get_dict_string(d, (char_u *)"group", FALSE);
17306 priority = (int)get_dict_number(d, (char_u *)"priority");
17307 id = (int)get_dict_number(d, (char_u *)"id");
17308 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17309 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17310 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017311 if (i == 0)
17312 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017313 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017314 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017315 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017316 }
17317 else
17318 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017319 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017320 list_unref(s);
17321 s = NULL;
17322 }
17323
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017324 li = li->li_next;
17325 }
17326 rettv->vval.v_number = 0;
17327 }
17328#endif
17329}
17330
17331/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017332 * "setpos()" function
17333 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017334 static void
17335f_setpos(argvars, rettv)
17336 typval_T *argvars;
17337 typval_T *rettv;
17338{
17339 pos_T pos;
17340 int fnum;
17341 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017342 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017343
Bram Moolenaar08250432008-02-13 11:42:46 +000017344 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017345 name = get_tv_string_chk(argvars);
17346 if (name != NULL)
17347 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017348 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017349 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017350 if (--pos.col < 0)
17351 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017352 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017353 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017354 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017355 if (fnum == curbuf->b_fnum)
17356 {
17357 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017358 if (curswant >= 0)
17359 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017360 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017361 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017362 }
17363 else
17364 EMSG(_(e_invarg));
17365 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017366 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17367 {
17368 /* set mark */
17369 if (setmark_pos(name[1], &pos, fnum) == OK)
17370 rettv->vval.v_number = 0;
17371 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017372 else
17373 EMSG(_(e_invarg));
17374 }
17375 }
17376}
17377
17378/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017379 * "setqflist()" function
17380 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017381 static void
17382f_setqflist(argvars, rettv)
17383 typval_T *argvars;
17384 typval_T *rettv;
17385{
17386 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17387}
17388
17389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 * "setreg()" function
17391 */
17392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017393f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017394 typval_T *argvars;
17395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396{
17397 int regname;
17398 char_u *strregname;
17399 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017400 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 int append;
17402 char_u yank_type;
17403 long block_len;
17404
17405 block_len = -1;
17406 yank_type = MAUTO;
17407 append = FALSE;
17408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017409 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017410 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017412 if (strregname == NULL)
17413 return; /* type error; errmsg already given */
17414 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415 if (regname == 0 || regname == '@')
17416 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017418 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017420 stropt = get_tv_string_chk(&argvars[2]);
17421 if (stropt == NULL)
17422 return; /* type error */
17423 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 switch (*stropt)
17425 {
17426 case 'a': case 'A': /* append */
17427 append = TRUE;
17428 break;
17429 case 'v': case 'c': /* character-wise selection */
17430 yank_type = MCHAR;
17431 break;
17432 case 'V': case 'l': /* line-wise selection */
17433 yank_type = MLINE;
17434 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 case 'b': case Ctrl_V: /* block-wise selection */
17436 yank_type = MBLOCK;
17437 if (VIM_ISDIGIT(stropt[1]))
17438 {
17439 ++stropt;
17440 block_len = getdigits(&stropt) - 1;
17441 --stropt;
17442 }
17443 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444 }
17445 }
17446
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017447 if (argvars[1].v_type == VAR_LIST)
17448 {
17449 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017450 char_u **allocval;
17451 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017452 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017453 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017454 int len = argvars[1].vval.v_list->lv_len;
17455 listitem_T *li;
17456
Bram Moolenaar7d647822014-04-05 21:28:56 +020017457 /* First half: use for pointers to result lines; second half: use for
17458 * pointers to allocated copies. */
17459 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017460 if (lstval == NULL)
17461 return;
17462 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017463 allocval = lstval + len + 2;
17464 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017465
17466 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17467 li = li->li_next)
17468 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017469 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017470 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017471 goto free_lstval;
17472 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017473 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017474 /* Need to make a copy, next get_tv_string_buf_chk() will
17475 * overwrite the string. */
17476 strval = vim_strsave(buf);
17477 if (strval == NULL)
17478 goto free_lstval;
17479 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017480 }
17481 *curval++ = strval;
17482 }
17483 *curval++ = NULL;
17484
17485 write_reg_contents_lst(regname, lstval, -1,
17486 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017487free_lstval:
17488 while (curallocval > allocval)
17489 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017490 vim_free(lstval);
17491 }
17492 else
17493 {
17494 strval = get_tv_string_chk(&argvars[1]);
17495 if (strval == NULL)
17496 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017497 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017500 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501}
17502
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017503/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017504 * "settabvar()" function
17505 */
17506 static void
17507f_settabvar(argvars, rettv)
17508 typval_T *argvars;
17509 typval_T *rettv;
17510{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017511#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017512 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017513 tabpage_T *tp;
17514#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017515 char_u *varname, *tabvarname;
17516 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017517
17518 rettv->vval.v_number = 0;
17519
17520 if (check_restricted() || check_secure())
17521 return;
17522
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017523#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017524 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017525#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017526 varname = get_tv_string_chk(&argvars[1]);
17527 varp = &argvars[2];
17528
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017529 if (varname != NULL && varp != NULL
17530#ifdef FEAT_WINDOWS
17531 && tp != NULL
17532#endif
17533 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017534 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017535#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017536 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017537 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017538#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017539
17540 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17541 if (tabvarname != NULL)
17542 {
17543 STRCPY(tabvarname, "t:");
17544 STRCPY(tabvarname + 2, varname);
17545 set_var(tabvarname, varp, TRUE);
17546 vim_free(tabvarname);
17547 }
17548
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017549#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017550 /* Restore current tabpage */
17551 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017552 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017553#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017554 }
17555}
17556
17557/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017558 * "settabwinvar()" function
17559 */
17560 static void
17561f_settabwinvar(argvars, rettv)
17562 typval_T *argvars;
17563 typval_T *rettv;
17564{
17565 setwinvar(argvars, rettv, 1);
17566}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567
17568/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017569 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017572f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017573 typval_T *argvars;
17574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017576 setwinvar(argvars, rettv, 0);
17577}
17578
17579/*
17580 * "setwinvar()" and "settabwinvar()" functions
17581 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017582
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017583 static void
17584setwinvar(argvars, rettv, off)
17585 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017586 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017587 int off;
17588{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 win_T *win;
17590#ifdef FEAT_WINDOWS
17591 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017592 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593#endif
17594 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017595 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017597 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598
17599 if (check_restricted() || check_secure())
17600 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017601
17602#ifdef FEAT_WINDOWS
17603 if (off == 1)
17604 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17605 else
17606 tp = curtab;
17607#endif
17608 win = find_win_by_nr(&argvars[off], tp);
17609 varname = get_tv_string_chk(&argvars[off + 1]);
17610 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611
17612 if (win != NULL && varname != NULL && varp != NULL)
17613 {
17614#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017615 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017618 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017620 long numval;
17621 char_u *strval;
17622 int error = FALSE;
17623
17624 ++varname;
17625 numval = get_tv_number_chk(varp, &error);
17626 strval = get_tv_string_buf_chk(varp, nbuf);
17627 if (!error && strval != NULL)
17628 set_option_value(varname, numval, strval, OPT_LOCAL);
17629 }
17630 else
17631 {
17632 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17633 if (winvarname != NULL)
17634 {
17635 STRCPY(winvarname, "w:");
17636 STRCPY(winvarname + 2, varname);
17637 set_var(winvarname, varp, TRUE);
17638 vim_free(winvarname);
17639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 }
17641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017643 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644#endif
17645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646}
17647
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017648#ifdef FEAT_CRYPT
17649/*
17650 * "sha256({string})" function
17651 */
17652 static void
17653f_sha256(argvars, rettv)
17654 typval_T *argvars;
17655 typval_T *rettv;
17656{
17657 char_u *p;
17658
17659 p = get_tv_string(&argvars[0]);
17660 rettv->vval.v_string = vim_strsave(
17661 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17662 rettv->v_type = VAR_STRING;
17663}
17664#endif /* FEAT_CRYPT */
17665
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017667 * "shellescape({string})" function
17668 */
17669 static void
17670f_shellescape(argvars, rettv)
17671 typval_T *argvars;
17672 typval_T *rettv;
17673{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017674 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017675 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017676 rettv->v_type = VAR_STRING;
17677}
17678
17679/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017680 * shiftwidth() function
17681 */
17682 static void
17683f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017684 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017685 typval_T *rettv;
17686{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017687 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017688}
17689
17690/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017691 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692 */
17693 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017694f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017695 typval_T *argvars;
17696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017698 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699
Bram Moolenaar0d660222005-01-07 21:51:51 +000017700 p = get_tv_string(&argvars[0]);
17701 rettv->vval.v_string = vim_strsave(p);
17702 simplify_filename(rettv->vval.v_string); /* simplify in place */
17703 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704}
17705
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017706#ifdef FEAT_FLOAT
17707/*
17708 * "sin()" function
17709 */
17710 static void
17711f_sin(argvars, rettv)
17712 typval_T *argvars;
17713 typval_T *rettv;
17714{
17715 float_T f;
17716
17717 rettv->v_type = VAR_FLOAT;
17718 if (get_float_arg(argvars, &f) == OK)
17719 rettv->vval.v_float = sin(f);
17720 else
17721 rettv->vval.v_float = 0.0;
17722}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017723
17724/*
17725 * "sinh()" function
17726 */
17727 static void
17728f_sinh(argvars, rettv)
17729 typval_T *argvars;
17730 typval_T *rettv;
17731{
17732 float_T f;
17733
17734 rettv->v_type = VAR_FLOAT;
17735 if (get_float_arg(argvars, &f) == OK)
17736 rettv->vval.v_float = sinh(f);
17737 else
17738 rettv->vval.v_float = 0.0;
17739}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017740#endif
17741
Bram Moolenaar0d660222005-01-07 21:51:51 +000017742static int
17743#ifdef __BORLANDC__
17744 _RTLENTRYF
17745#endif
17746 item_compare __ARGS((const void *s1, const void *s2));
17747static int
17748#ifdef __BORLANDC__
17749 _RTLENTRYF
17750#endif
17751 item_compare2 __ARGS((const void *s1, const void *s2));
17752
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017753/* struct used in the array that's given to qsort() */
17754typedef struct
17755{
17756 listitem_T *item;
17757 int idx;
17758} sortItem_T;
17759
Bram Moolenaar0d660222005-01-07 21:51:51 +000017760static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017761static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017762static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017763static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017764static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017765static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017766static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017767#define ITEM_COMPARE_FAIL 999
17768
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017770 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017772 static int
17773#ifdef __BORLANDC__
17774_RTLENTRYF
17775#endif
17776item_compare(s1, s2)
17777 const void *s1;
17778 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017780 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017781 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017782 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017783 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017784 int res;
17785 char_u numbuf1[NUMBUFLEN];
17786 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017788 si1 = (sortItem_T *)s1;
17789 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017790 tv1 = &si1->item->li_tv;
17791 tv2 = &si2->item->li_tv;
17792 /* tv2string() puts quotes around a string and allocates memory. Don't do
17793 * that for string variables. Use a single quote when comparing with a
17794 * non-string to do what the docs promise. */
17795 if (tv1->v_type == VAR_STRING)
17796 {
17797 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17798 p1 = (char_u *)"'";
17799 else
17800 p1 = tv1->vval.v_string;
17801 }
17802 else
17803 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17804 if (tv2->v_type == VAR_STRING)
17805 {
17806 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17807 p2 = (char_u *)"'";
17808 else
17809 p2 = tv2->vval.v_string;
17810 }
17811 else
17812 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017813 if (p1 == NULL)
17814 p1 = (char_u *)"";
17815 if (p2 == NULL)
17816 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017817 if (!item_compare_numeric)
17818 {
17819 if (item_compare_ic)
17820 res = STRICMP(p1, p2);
17821 else
17822 res = STRCMP(p1, p2);
17823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017825 {
17826 double n1, n2;
17827 n1 = strtod((char *)p1, (char **)&p1);
17828 n2 = strtod((char *)p2, (char **)&p2);
17829 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17830 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017831
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017832 /* When the result would be zero, compare the item indexes. Makes the
17833 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017834 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017835 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017836
Bram Moolenaar0d660222005-01-07 21:51:51 +000017837 vim_free(tofree1);
17838 vim_free(tofree2);
17839 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840}
17841
17842 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017843#ifdef __BORLANDC__
17844_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017846item_compare2(s1, s2)
17847 const void *s1;
17848 const void *s2;
17849{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017850 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017851 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017852 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017853 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017854 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017856 /* shortcut after failure in previous call; compare all items equal */
17857 if (item_compare_func_err)
17858 return 0;
17859
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017860 si1 = (sortItem_T *)s1;
17861 si2 = (sortItem_T *)s2;
17862
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017863 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017864 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017865 copy_tv(&si1->item->li_tv, &argv[0]);
17866 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017867
17868 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017869 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017870 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17871 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017872 clear_tv(&argv[0]);
17873 clear_tv(&argv[1]);
17874
17875 if (res == FAIL)
17876 res = ITEM_COMPARE_FAIL;
17877 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017878 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17879 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017880 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017881 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017882
17883 /* When the result would be zero, compare the pointers themselves. Makes
17884 * the sort stable. */
17885 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017886 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017887
Bram Moolenaar0d660222005-01-07 21:51:51 +000017888 return res;
17889}
17890
17891/*
17892 * "sort({list})" function
17893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017895do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017896 typval_T *argvars;
17897 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017898 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899{
Bram Moolenaar33570922005-01-25 22:26:29 +000017900 list_T *l;
17901 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017902 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017903 long len;
17904 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905
Bram Moolenaar0d660222005-01-07 21:51:51 +000017906 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017907 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 else
17909 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017910 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017911 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017912 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17913 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017914 return;
17915 rettv->vval.v_list = l;
17916 rettv->v_type = VAR_LIST;
17917 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918
Bram Moolenaar0d660222005-01-07 21:51:51 +000017919 len = list_len(l);
17920 if (len <= 1)
17921 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922
Bram Moolenaar0d660222005-01-07 21:51:51 +000017923 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017924 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017925 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017926 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017927 if (argvars[1].v_type != VAR_UNKNOWN)
17928 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017929 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017930 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017931 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017932 else
17933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017934 int error = FALSE;
17935
17936 i = get_tv_number_chk(&argvars[1], &error);
17937 if (error)
17938 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017939 if (i == 1)
17940 item_compare_ic = TRUE;
17941 else
17942 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017943 if (item_compare_func != NULL)
17944 {
17945 if (STRCMP(item_compare_func, "n") == 0)
17946 {
17947 item_compare_func = NULL;
17948 item_compare_numeric = TRUE;
17949 }
17950 else if (STRCMP(item_compare_func, "i") == 0)
17951 {
17952 item_compare_func = NULL;
17953 item_compare_ic = TRUE;
17954 }
17955 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017956 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017957
17958 if (argvars[2].v_type != VAR_UNKNOWN)
17959 {
17960 /* optional third argument: {dict} */
17961 if (argvars[2].v_type != VAR_DICT)
17962 {
17963 EMSG(_(e_dictreq));
17964 return;
17965 }
17966 item_compare_selfdict = argvars[2].vval.v_dict;
17967 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969
Bram Moolenaar0d660222005-01-07 21:51:51 +000017970 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017971 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017972 if (ptrs == NULL)
17973 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974
Bram Moolenaar327aa022014-03-25 18:24:23 +010017975 i = 0;
17976 if (sort)
17977 {
17978 /* sort(): ptrs will be the list to sort */
17979 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017980 {
17981 ptrs[i].item = li;
17982 ptrs[i].idx = i;
17983 ++i;
17984 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017985
17986 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017987 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017988 /* test the compare function */
17989 if (item_compare_func != NULL
17990 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017991 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017992 EMSG(_("E702: Sort compare function failed"));
17993 else
17994 {
17995 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017996 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017997 item_compare_func == NULL ? item_compare : item_compare2);
17998
17999 if (!item_compare_func_err)
18000 {
18001 /* Clear the List and append the items in sorted order. */
18002 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18003 l->lv_len = 0;
18004 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018005 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018006 }
18007 }
18008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018010 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018011 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18012
18013 /* f_uniq(): ptrs will be a stack of items to remove */
18014 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018015 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018016 item_compare_func_ptr = item_compare_func
18017 ? item_compare2 : item_compare;
18018
18019 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18020 li = li->li_next)
18021 {
18022 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18023 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018024 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018025 if (item_compare_func_err)
18026 {
18027 EMSG(_("E882: Uniq compare function failed"));
18028 break;
18029 }
18030 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018031
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018032 if (!item_compare_func_err)
18033 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018034 while (--i >= 0)
18035 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018036 li = ptrs[i].item->li_next;
18037 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018038 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018039 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018040 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018041 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018042 list_fix_watch(l, li);
18043 listitem_free(li);
18044 l->lv_len--;
18045 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018046 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018047 }
18048
18049 vim_free(ptrs);
18050 }
18051}
18052
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018053/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018054 * "sort({list})" function
18055 */
18056 static void
18057f_sort(argvars, rettv)
18058 typval_T *argvars;
18059 typval_T *rettv;
18060{
18061 do_sort_uniq(argvars, rettv, TRUE);
18062}
18063
18064/*
18065 * "uniq({list})" function
18066 */
18067 static void
18068f_uniq(argvars, rettv)
18069 typval_T *argvars;
18070 typval_T *rettv;
18071{
18072 do_sort_uniq(argvars, rettv, FALSE);
18073}
18074
18075/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018076 * "soundfold({word})" function
18077 */
18078 static void
18079f_soundfold(argvars, rettv)
18080 typval_T *argvars;
18081 typval_T *rettv;
18082{
18083 char_u *s;
18084
18085 rettv->v_type = VAR_STRING;
18086 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018087#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018088 rettv->vval.v_string = eval_soundfold(s);
18089#else
18090 rettv->vval.v_string = vim_strsave(s);
18091#endif
18092}
18093
18094/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018095 * "spellbadword()" function
18096 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018097 static void
18098f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018099 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018100 typval_T *rettv;
18101{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018102 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018103 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018104 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018105
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018106 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018107 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018108
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018109#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018110 if (argvars[0].v_type == VAR_UNKNOWN)
18111 {
18112 /* Find the start and length of the badly spelled word. */
18113 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18114 if (len != 0)
18115 word = ml_get_cursor();
18116 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018117 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018118 {
18119 char_u *str = get_tv_string_chk(&argvars[0]);
18120 int capcol = -1;
18121
18122 if (str != NULL)
18123 {
18124 /* Check the argument for spelling. */
18125 while (*str != NUL)
18126 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018127 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018128 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018129 {
18130 word = str;
18131 break;
18132 }
18133 str += len;
18134 }
18135 }
18136 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018137#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018138
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018139 list_append_string(rettv->vval.v_list, word, len);
18140 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018141 attr == HLF_SPB ? "bad" :
18142 attr == HLF_SPR ? "rare" :
18143 attr == HLF_SPL ? "local" :
18144 attr == HLF_SPC ? "caps" :
18145 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018146}
18147
18148/*
18149 * "spellsuggest()" function
18150 */
18151 static void
18152f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018153 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018154 typval_T *rettv;
18155{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018156#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018157 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018158 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018159 int maxcount;
18160 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018161 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018162 listitem_T *li;
18163 int need_capital = FALSE;
18164#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018165
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018166 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018167 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018168
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018169#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018170 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018171 {
18172 str = get_tv_string(&argvars[0]);
18173 if (argvars[1].v_type != VAR_UNKNOWN)
18174 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018175 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018176 if (maxcount <= 0)
18177 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018178 if (argvars[2].v_type != VAR_UNKNOWN)
18179 {
18180 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18181 if (typeerr)
18182 return;
18183 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018184 }
18185 else
18186 maxcount = 25;
18187
Bram Moolenaar4770d092006-01-12 23:22:24 +000018188 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018189
18190 for (i = 0; i < ga.ga_len; ++i)
18191 {
18192 str = ((char_u **)ga.ga_data)[i];
18193
18194 li = listitem_alloc();
18195 if (li == NULL)
18196 vim_free(str);
18197 else
18198 {
18199 li->li_tv.v_type = VAR_STRING;
18200 li->li_tv.v_lock = 0;
18201 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018202 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018203 }
18204 }
18205 ga_clear(&ga);
18206 }
18207#endif
18208}
18209
Bram Moolenaar0d660222005-01-07 21:51:51 +000018210 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018211f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018212 typval_T *argvars;
18213 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018214{
18215 char_u *str;
18216 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018217 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018218 regmatch_T regmatch;
18219 char_u patbuf[NUMBUFLEN];
18220 char_u *save_cpo;
18221 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018222 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018223 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018224 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018225
18226 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18227 save_cpo = p_cpo;
18228 p_cpo = (char_u *)"";
18229
18230 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018231 if (argvars[1].v_type != VAR_UNKNOWN)
18232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018233 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18234 if (pat == NULL)
18235 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018236 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018237 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018238 }
18239 if (pat == NULL || *pat == NUL)
18240 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018241
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018242 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018244 if (typeerr)
18245 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246
Bram Moolenaar0d660222005-01-07 21:51:51 +000018247 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18248 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018250 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018251 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018252 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018253 if (*str == NUL)
18254 match = FALSE; /* empty item at the end */
18255 else
18256 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018257 if (match)
18258 end = regmatch.startp[0];
18259 else
18260 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018261 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18262 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018263 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018264 if (list_append_string(rettv->vval.v_list, str,
18265 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018266 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018267 }
18268 if (!match)
18269 break;
18270 /* Advance to just after the match. */
18271 if (regmatch.endp[0] > str)
18272 col = 0;
18273 else
18274 {
18275 /* Don't get stuck at the same match. */
18276#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018277 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018278#else
18279 col = 1;
18280#endif
18281 }
18282 str = regmatch.endp[0];
18283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284
Bram Moolenaar473de612013-06-08 18:19:48 +020018285 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287
Bram Moolenaar0d660222005-01-07 21:51:51 +000018288 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289}
18290
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018291#ifdef FEAT_FLOAT
18292/*
18293 * "sqrt()" function
18294 */
18295 static void
18296f_sqrt(argvars, rettv)
18297 typval_T *argvars;
18298 typval_T *rettv;
18299{
18300 float_T f;
18301
18302 rettv->v_type = VAR_FLOAT;
18303 if (get_float_arg(argvars, &f) == OK)
18304 rettv->vval.v_float = sqrt(f);
18305 else
18306 rettv->vval.v_float = 0.0;
18307}
18308
18309/*
18310 * "str2float()" function
18311 */
18312 static void
18313f_str2float(argvars, rettv)
18314 typval_T *argvars;
18315 typval_T *rettv;
18316{
18317 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18318
18319 if (*p == '+')
18320 p = skipwhite(p + 1);
18321 (void)string2float(p, &rettv->vval.v_float);
18322 rettv->v_type = VAR_FLOAT;
18323}
18324#endif
18325
Bram Moolenaar2c932302006-03-18 21:42:09 +000018326/*
18327 * "str2nr()" function
18328 */
18329 static void
18330f_str2nr(argvars, rettv)
18331 typval_T *argvars;
18332 typval_T *rettv;
18333{
18334 int base = 10;
18335 char_u *p;
18336 long n;
18337
18338 if (argvars[1].v_type != VAR_UNKNOWN)
18339 {
18340 base = get_tv_number(&argvars[1]);
18341 if (base != 8 && base != 10 && base != 16)
18342 {
18343 EMSG(_(e_invarg));
18344 return;
18345 }
18346 }
18347
18348 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018349 if (*p == '+')
18350 p = skipwhite(p + 1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020018351 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018352 rettv->vval.v_number = n;
18353}
18354
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355#ifdef HAVE_STRFTIME
18356/*
18357 * "strftime({format}[, {time}])" function
18358 */
18359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018360f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018361 typval_T *argvars;
18362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363{
18364 char_u result_buf[256];
18365 struct tm *curtime;
18366 time_t seconds;
18367 char_u *p;
18368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018369 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018371 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018372 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 seconds = time(NULL);
18374 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018375 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376 curtime = localtime(&seconds);
18377 /* MSVC returns NULL for an invalid value of seconds. */
18378 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018379 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 else
18381 {
18382# ifdef FEAT_MBYTE
18383 vimconv_T conv;
18384 char_u *enc;
18385
18386 conv.vc_type = CONV_NONE;
18387 enc = enc_locale();
18388 convert_setup(&conv, p_enc, enc);
18389 if (conv.vc_type != CONV_NONE)
18390 p = string_convert(&conv, p, NULL);
18391# endif
18392 if (p != NULL)
18393 (void)strftime((char *)result_buf, sizeof(result_buf),
18394 (char *)p, curtime);
18395 else
18396 result_buf[0] = NUL;
18397
18398# ifdef FEAT_MBYTE
18399 if (conv.vc_type != CONV_NONE)
18400 vim_free(p);
18401 convert_setup(&conv, enc, p_enc);
18402 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018403 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404 else
18405# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018406 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407
18408# ifdef FEAT_MBYTE
18409 /* Release conversion descriptors */
18410 convert_setup(&conv, NULL, NULL);
18411 vim_free(enc);
18412# endif
18413 }
18414}
18415#endif
18416
18417/*
18418 * "stridx()" function
18419 */
18420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018421f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018422 typval_T *argvars;
18423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424{
18425 char_u buf[NUMBUFLEN];
18426 char_u *needle;
18427 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018428 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018432 needle = get_tv_string_chk(&argvars[1]);
18433 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018434 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018435 if (needle == NULL || haystack == NULL)
18436 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437
Bram Moolenaar33570922005-01-25 22:26:29 +000018438 if (argvars[2].v_type != VAR_UNKNOWN)
18439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018440 int error = FALSE;
18441
18442 start_idx = get_tv_number_chk(&argvars[2], &error);
18443 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018444 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018445 if (start_idx >= 0)
18446 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018447 }
18448
18449 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18450 if (pos != NULL)
18451 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452}
18453
18454/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018455 * "string()" function
18456 */
18457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018458f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018459 typval_T *argvars;
18460 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018461{
18462 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018463 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018465 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018466 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018467 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018468 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018469 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470}
18471
18472/*
18473 * "strlen()" function
18474 */
18475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018476f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018477 typval_T *argvars;
18478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018480 rettv->vval.v_number = (varnumber_T)(STRLEN(
18481 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482}
18483
18484/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018485 * "strchars()" function
18486 */
18487 static void
18488f_strchars(argvars, rettv)
18489 typval_T *argvars;
18490 typval_T *rettv;
18491{
18492 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018493 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018494#ifdef FEAT_MBYTE
18495 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018496 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018497#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018498
18499 if (argvars[1].v_type != VAR_UNKNOWN)
18500 skipcc = get_tv_number_chk(&argvars[1], NULL);
18501 if (skipcc < 0 || skipcc > 1)
18502 EMSG(_(e_invarg));
18503 else
18504 {
18505#ifdef FEAT_MBYTE
18506 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18507 while (*s != NUL)
18508 {
18509 func_mb_ptr2char_adv(&s);
18510 ++len;
18511 }
18512 rettv->vval.v_number = len;
18513#else
18514 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18515#endif
18516 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018517}
18518
18519/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018520 * "strdisplaywidth()" function
18521 */
18522 static void
18523f_strdisplaywidth(argvars, rettv)
18524 typval_T *argvars;
18525 typval_T *rettv;
18526{
18527 char_u *s = get_tv_string(&argvars[0]);
18528 int col = 0;
18529
18530 if (argvars[1].v_type != VAR_UNKNOWN)
18531 col = get_tv_number(&argvars[1]);
18532
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018533 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018534}
18535
18536/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018537 * "strwidth()" function
18538 */
18539 static void
18540f_strwidth(argvars, rettv)
18541 typval_T *argvars;
18542 typval_T *rettv;
18543{
18544 char_u *s = get_tv_string(&argvars[0]);
18545
18546 rettv->vval.v_number = (varnumber_T)(
18547#ifdef FEAT_MBYTE
18548 mb_string2cells(s, -1)
18549#else
18550 STRLEN(s)
18551#endif
18552 );
18553}
18554
18555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 * "strpart()" function
18557 */
18558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018559f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018560 typval_T *argvars;
18561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562{
18563 char_u *p;
18564 int n;
18565 int len;
18566 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018567 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018569 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 slen = (int)STRLEN(p);
18571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018572 n = get_tv_number_chk(&argvars[1], &error);
18573 if (error)
18574 len = 0;
18575 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018576 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 else
18578 len = slen - n; /* default len: all bytes that are available. */
18579
18580 /*
18581 * Only return the overlap between the specified part and the actual
18582 * string.
18583 */
18584 if (n < 0)
18585 {
18586 len += n;
18587 n = 0;
18588 }
18589 else if (n > slen)
18590 n = slen;
18591 if (len < 0)
18592 len = 0;
18593 else if (n + len > slen)
18594 len = slen - n;
18595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018596 rettv->v_type = VAR_STRING;
18597 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598}
18599
18600/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018601 * "strridx()" function
18602 */
18603 static void
18604f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018605 typval_T *argvars;
18606 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018607{
18608 char_u buf[NUMBUFLEN];
18609 char_u *needle;
18610 char_u *haystack;
18611 char_u *rest;
18612 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018613 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018615 needle = get_tv_string_chk(&argvars[1]);
18616 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018617
18618 rettv->vval.v_number = -1;
18619 if (needle == NULL || haystack == NULL)
18620 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018621
18622 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018623 if (argvars[2].v_type != VAR_UNKNOWN)
18624 {
18625 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018626 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018627 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018628 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018629 }
18630 else
18631 end_idx = haystack_len;
18632
Bram Moolenaar0d660222005-01-07 21:51:51 +000018633 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018634 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018635 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018636 lastmatch = haystack + end_idx;
18637 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018638 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018639 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018640 for (rest = haystack; *rest != '\0'; ++rest)
18641 {
18642 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018643 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018644 break;
18645 lastmatch = rest;
18646 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018647 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018648
18649 if (lastmatch == NULL)
18650 rettv->vval.v_number = -1;
18651 else
18652 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18653}
18654
18655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 * "strtrans()" function
18657 */
18658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018659f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018660 typval_T *argvars;
18661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663 rettv->v_type = VAR_STRING;
18664 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665}
18666
18667/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018668 * "submatch()" function
18669 */
18670 static void
18671f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018672 typval_T *argvars;
18673 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018674{
Bram Moolenaar41571762014-04-02 19:00:58 +020018675 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018676 int no;
18677 int retList = 0;
18678
18679 no = (int)get_tv_number_chk(&argvars[0], &error);
18680 if (error)
18681 return;
18682 error = FALSE;
18683 if (argvars[1].v_type != VAR_UNKNOWN)
18684 retList = get_tv_number_chk(&argvars[1], &error);
18685 if (error)
18686 return;
18687
18688 if (retList == 0)
18689 {
18690 rettv->v_type = VAR_STRING;
18691 rettv->vval.v_string = reg_submatch(no);
18692 }
18693 else
18694 {
18695 rettv->v_type = VAR_LIST;
18696 rettv->vval.v_list = reg_submatch_list(no);
18697 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018698}
18699
18700/*
18701 * "substitute()" function
18702 */
18703 static void
18704f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018705 typval_T *argvars;
18706 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018707{
18708 char_u patbuf[NUMBUFLEN];
18709 char_u subbuf[NUMBUFLEN];
18710 char_u flagsbuf[NUMBUFLEN];
18711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018712 char_u *str = get_tv_string_chk(&argvars[0]);
18713 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18714 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18715 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18716
Bram Moolenaar0d660222005-01-07 21:51:51 +000018717 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018718 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18719 rettv->vval.v_string = NULL;
18720 else
18721 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018722}
18723
18724/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018725 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018729 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731{
18732 int id = 0;
18733#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018734 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 long col;
18736 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018737 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018739 lnum = get_tv_lnum(argvars); /* -1 on type error */
18740 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18741 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018743 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018744 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018745 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746#endif
18747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018748 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749}
18750
18751/*
18752 * "synIDattr(id, what [, mode])" function
18753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018755f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758{
18759 char_u *p = NULL;
18760#ifdef FEAT_SYN_HL
18761 int id;
18762 char_u *what;
18763 char_u *mode;
18764 char_u modebuf[NUMBUFLEN];
18765 int modec;
18766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018767 id = get_tv_number(&argvars[0]);
18768 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018769 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018771 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018773 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774 modec = 0; /* replace invalid with current */
18775 }
18776 else
18777 {
18778#ifdef FEAT_GUI
18779 if (gui.in_use)
18780 modec = 'g';
18781 else
18782#endif
18783 if (t_colors > 1)
18784 modec = 'c';
18785 else
18786 modec = 't';
18787 }
18788
18789
18790 switch (TOLOWER_ASC(what[0]))
18791 {
18792 case 'b':
18793 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18794 p = highlight_color(id, what, modec);
18795 else /* bold */
18796 p = highlight_has_attr(id, HL_BOLD, modec);
18797 break;
18798
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018799 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800 p = highlight_color(id, what, modec);
18801 break;
18802
18803 case 'i':
18804 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18805 p = highlight_has_attr(id, HL_INVERSE, modec);
18806 else /* italic */
18807 p = highlight_has_attr(id, HL_ITALIC, modec);
18808 break;
18809
18810 case 'n': /* name */
18811 p = get_highlight_name(NULL, id - 1);
18812 break;
18813
18814 case 'r': /* reverse */
18815 p = highlight_has_attr(id, HL_INVERSE, modec);
18816 break;
18817
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018818 case 's':
18819 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18820 p = highlight_color(id, what, modec);
18821 else /* standout */
18822 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 break;
18824
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018825 case 'u':
18826 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18827 /* underline */
18828 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18829 else
18830 /* undercurl */
18831 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 break;
18833 }
18834
18835 if (p != NULL)
18836 p = vim_strsave(p);
18837#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018838 rettv->v_type = VAR_STRING;
18839 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840}
18841
18842/*
18843 * "synIDtrans(id)" function
18844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018846f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018847 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849{
18850 int id;
18851
18852#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018853 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854
18855 if (id > 0)
18856 id = syn_get_final_id(id);
18857 else
18858#endif
18859 id = 0;
18860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018861 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862}
18863
18864/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018865 * "synconcealed(lnum, col)" function
18866 */
18867 static void
18868f_synconcealed(argvars, rettv)
18869 typval_T *argvars UNUSED;
18870 typval_T *rettv;
18871{
18872#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18873 long lnum;
18874 long col;
18875 int syntax_flags = 0;
18876 int cchar;
18877 int matchid = 0;
18878 char_u str[NUMBUFLEN];
18879#endif
18880
18881 rettv->v_type = VAR_LIST;
18882 rettv->vval.v_list = NULL;
18883
18884#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18885 lnum = get_tv_lnum(argvars); /* -1 on type error */
18886 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18887
18888 vim_memset(str, NUL, sizeof(str));
18889
18890 if (rettv_list_alloc(rettv) != FAIL)
18891 {
18892 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18893 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18894 && curwin->w_p_cole > 0)
18895 {
18896 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18897 syntax_flags = get_syntax_info(&matchid);
18898
18899 /* get the conceal character */
18900 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18901 {
18902 cchar = syn_get_sub_char();
18903 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18904 cchar = lcs_conceal;
18905 if (cchar != NUL)
18906 {
18907# ifdef FEAT_MBYTE
18908 if (has_mbyte)
18909 (*mb_char2bytes)(cchar, str);
18910 else
18911# endif
18912 str[0] = cchar;
18913 }
18914 }
18915 }
18916
18917 list_append_number(rettv->vval.v_list,
18918 (syntax_flags & HL_CONCEAL) != 0);
18919 /* -1 to auto-determine strlen */
18920 list_append_string(rettv->vval.v_list, str, -1);
18921 list_append_number(rettv->vval.v_list, matchid);
18922 }
18923#endif
18924}
18925
18926/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018927 * "synstack(lnum, col)" function
18928 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018929 static void
18930f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018931 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018932 typval_T *rettv;
18933{
18934#ifdef FEAT_SYN_HL
18935 long lnum;
18936 long col;
18937 int i;
18938 int id;
18939#endif
18940
18941 rettv->v_type = VAR_LIST;
18942 rettv->vval.v_list = NULL;
18943
18944#ifdef FEAT_SYN_HL
18945 lnum = get_tv_lnum(argvars); /* -1 on type error */
18946 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18947
18948 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018949 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018950 && rettv_list_alloc(rettv) != FAIL)
18951 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018952 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018953 for (i = 0; ; ++i)
18954 {
18955 id = syn_get_stack_item(i);
18956 if (id < 0)
18957 break;
18958 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18959 break;
18960 }
18961 }
18962#endif
18963}
18964
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018966get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018967 typval_T *argvars;
18968 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018969 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018971 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018973 char_u *infile = NULL;
18974 char_u buf[NUMBUFLEN];
18975 int err = FALSE;
18976 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018977 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018978 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018980 rettv->v_type = VAR_STRING;
18981 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018982 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018983 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018984
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018985 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018986 {
18987 /*
18988 * Write the string to a temp file, to be used for input of the shell
18989 * command.
18990 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018991 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018992 {
18993 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018994 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018995 }
18996
18997 fd = mch_fopen((char *)infile, WRITEBIN);
18998 if (fd == NULL)
18999 {
19000 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019001 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019002 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019003 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019004 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019005 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19006 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019007 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019008 else
19009 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019010 size_t len;
19011
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019012 p = get_tv_string_buf_chk(&argvars[1], buf);
19013 if (p == NULL)
19014 {
19015 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019016 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019017 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019018 len = STRLEN(p);
19019 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019020 err = TRUE;
19021 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019022 if (fclose(fd) != 0)
19023 err = TRUE;
19024 if (err)
19025 {
19026 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019027 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019028 }
19029 }
19030
Bram Moolenaar52a72462014-08-29 15:53:52 +020019031 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19032 * echoes typeahead, that messes up the display. */
19033 if (!msg_silent)
19034 flags += SHELL_COOKED;
19035
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019036 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019038 int len;
19039 listitem_T *li;
19040 char_u *s = NULL;
19041 char_u *start;
19042 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019043 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044
Bram Moolenaar52a72462014-08-29 15:53:52 +020019045 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019046 if (res == NULL)
19047 goto errret;
19048
19049 list = list_alloc();
19050 if (list == NULL)
19051 goto errret;
19052
19053 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019055 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019056 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019057 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019058 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019059
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019060 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019061 if (s == NULL)
19062 goto errret;
19063
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019064 for (p = s; start < end; ++p, ++start)
19065 *p = *start == NUL ? NL : *start;
19066 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019067
19068 li = listitem_alloc();
19069 if (li == NULL)
19070 {
19071 vim_free(s);
19072 goto errret;
19073 }
19074 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019075 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019076 li->li_tv.vval.v_string = s;
19077 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019079
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019080 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019081 rettv->v_type = VAR_LIST;
19082 rettv->vval.v_list = list;
19083 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019085 else
19086 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019087 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019088#ifdef USE_CR
19089 /* translate <CR> into <NL> */
19090 if (res != NULL)
19091 {
19092 char_u *s;
19093
19094 for (s = res; *s; ++s)
19095 {
19096 if (*s == CAR)
19097 *s = NL;
19098 }
19099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100#else
19101# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019102 /* translate <CR><NL> into <NL> */
19103 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019105 char_u *s, *d;
19106
19107 d = res;
19108 for (s = res; *s; ++s)
19109 {
19110 if (s[0] == CAR && s[1] == NL)
19111 ++s;
19112 *d++ = *s;
19113 }
19114 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116# endif
19117#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019118 rettv->vval.v_string = res;
19119 res = NULL;
19120 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019121
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019122errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019123 if (infile != NULL)
19124 {
19125 mch_remove(infile);
19126 vim_free(infile);
19127 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019128 if (res != NULL)
19129 vim_free(res);
19130 if (list != NULL)
19131 list_free(list, TRUE);
19132}
19133
19134/*
19135 * "system()" function
19136 */
19137 static void
19138f_system(argvars, rettv)
19139 typval_T *argvars;
19140 typval_T *rettv;
19141{
19142 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19143}
19144
19145/*
19146 * "systemlist()" function
19147 */
19148 static void
19149f_systemlist(argvars, rettv)
19150 typval_T *argvars;
19151 typval_T *rettv;
19152{
19153 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154}
19155
19156/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019157 * "tabpagebuflist()" function
19158 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019159 static void
19160f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019161 typval_T *argvars UNUSED;
19162 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019163{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019164#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019165 tabpage_T *tp;
19166 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019167
19168 if (argvars[0].v_type == VAR_UNKNOWN)
19169 wp = firstwin;
19170 else
19171 {
19172 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19173 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019174 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019175 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019176 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019177 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019178 for (; wp != NULL; wp = wp->w_next)
19179 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019180 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019181 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019182 }
19183#endif
19184}
19185
19186
19187/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019188 * "tabpagenr()" function
19189 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019190 static void
19191f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019192 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019193 typval_T *rettv;
19194{
19195 int nr = 1;
19196#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019197 char_u *arg;
19198
19199 if (argvars[0].v_type != VAR_UNKNOWN)
19200 {
19201 arg = get_tv_string_chk(&argvars[0]);
19202 nr = 0;
19203 if (arg != NULL)
19204 {
19205 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019206 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019207 else
19208 EMSG2(_(e_invexpr2), arg);
19209 }
19210 }
19211 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019212 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019213#endif
19214 rettv->vval.v_number = nr;
19215}
19216
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019217
19218#ifdef FEAT_WINDOWS
19219static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19220
19221/*
19222 * Common code for tabpagewinnr() and winnr().
19223 */
19224 static int
19225get_winnr(tp, argvar)
19226 tabpage_T *tp;
19227 typval_T *argvar;
19228{
19229 win_T *twin;
19230 int nr = 1;
19231 win_T *wp;
19232 char_u *arg;
19233
19234 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19235 if (argvar->v_type != VAR_UNKNOWN)
19236 {
19237 arg = get_tv_string_chk(argvar);
19238 if (arg == NULL)
19239 nr = 0; /* type error; errmsg already given */
19240 else if (STRCMP(arg, "$") == 0)
19241 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19242 else if (STRCMP(arg, "#") == 0)
19243 {
19244 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19245 if (twin == NULL)
19246 nr = 0;
19247 }
19248 else
19249 {
19250 EMSG2(_(e_invexpr2), arg);
19251 nr = 0;
19252 }
19253 }
19254
19255 if (nr > 0)
19256 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19257 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019258 {
19259 if (wp == NULL)
19260 {
19261 /* didn't find it in this tabpage */
19262 nr = 0;
19263 break;
19264 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019265 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019266 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019267 return nr;
19268}
19269#endif
19270
19271/*
19272 * "tabpagewinnr()" function
19273 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019274 static void
19275f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019276 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019277 typval_T *rettv;
19278{
19279 int nr = 1;
19280#ifdef FEAT_WINDOWS
19281 tabpage_T *tp;
19282
19283 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19284 if (tp == NULL)
19285 nr = 0;
19286 else
19287 nr = get_winnr(tp, &argvars[1]);
19288#endif
19289 rettv->vval.v_number = nr;
19290}
19291
19292
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019293/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019294 * "tagfiles()" function
19295 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019296 static void
19297f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019298 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019299 typval_T *rettv;
19300{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019301 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019302 tagname_T tn;
19303 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019304
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019305 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019306 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019307 fname = alloc(MAXPATHL);
19308 if (fname == NULL)
19309 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019310
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019311 for (first = TRUE; ; first = FALSE)
19312 if (get_tagfname(&tn, first, fname) == FAIL
19313 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019314 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019315 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019316 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019317}
19318
19319/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019320 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019321 */
19322 static void
19323f_taglist(argvars, rettv)
19324 typval_T *argvars;
19325 typval_T *rettv;
19326{
19327 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019328
19329 tag_pattern = get_tv_string(&argvars[0]);
19330
19331 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019332 if (*tag_pattern == NUL)
19333 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019334
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019335 if (rettv_list_alloc(rettv) == OK)
19336 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019337}
19338
19339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340 * "tempname()" function
19341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019343f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019344 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346{
19347 static int x = 'A';
19348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019349 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019350 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351
19352 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19353 * names. Skip 'I' and 'O', they are used for shell redirection. */
19354 do
19355 {
19356 if (x == 'Z')
19357 x = '0';
19358 else if (x == '9')
19359 x = 'A';
19360 else
19361 {
19362#ifdef EBCDIC
19363 if (x == 'I')
19364 x = 'J';
19365 else if (x == 'R')
19366 x = 'S';
19367 else
19368#endif
19369 ++x;
19370 }
19371 } while (x == 'I' || x == 'O');
19372}
19373
19374/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019375 * "test(list)" function: Just checking the walls...
19376 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019377 static void
19378f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019379 typval_T *argvars UNUSED;
19380 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019381{
19382 /* Used for unit testing. Change the code below to your liking. */
19383#if 0
19384 listitem_T *li;
19385 list_T *l;
19386 char_u *bad, *good;
19387
19388 if (argvars[0].v_type != VAR_LIST)
19389 return;
19390 l = argvars[0].vval.v_list;
19391 if (l == NULL)
19392 return;
19393 li = l->lv_first;
19394 if (li == NULL)
19395 return;
19396 bad = get_tv_string(&li->li_tv);
19397 li = li->li_next;
19398 if (li == NULL)
19399 return;
19400 good = get_tv_string(&li->li_tv);
19401 rettv->vval.v_number = test_edit_score(bad, good);
19402#endif
19403}
19404
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019405#ifdef FEAT_FLOAT
19406/*
19407 * "tan()" function
19408 */
19409 static void
19410f_tan(argvars, rettv)
19411 typval_T *argvars;
19412 typval_T *rettv;
19413{
19414 float_T f;
19415
19416 rettv->v_type = VAR_FLOAT;
19417 if (get_float_arg(argvars, &f) == OK)
19418 rettv->vval.v_float = tan(f);
19419 else
19420 rettv->vval.v_float = 0.0;
19421}
19422
19423/*
19424 * "tanh()" function
19425 */
19426 static void
19427f_tanh(argvars, rettv)
19428 typval_T *argvars;
19429 typval_T *rettv;
19430{
19431 float_T f;
19432
19433 rettv->v_type = VAR_FLOAT;
19434 if (get_float_arg(argvars, &f) == OK)
19435 rettv->vval.v_float = tanh(f);
19436 else
19437 rettv->vval.v_float = 0.0;
19438}
19439#endif
19440
Bram Moolenaard52d9742005-08-21 22:20:28 +000019441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442 * "tolower(string)" function
19443 */
19444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019445f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019446 typval_T *argvars;
19447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448{
19449 char_u *p;
19450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019451 p = vim_strsave(get_tv_string(&argvars[0]));
19452 rettv->v_type = VAR_STRING;
19453 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454
19455 if (p != NULL)
19456 while (*p != NUL)
19457 {
19458#ifdef FEAT_MBYTE
19459 int l;
19460
19461 if (enc_utf8)
19462 {
19463 int c, lc;
19464
19465 c = utf_ptr2char(p);
19466 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019467 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 /* TODO: reallocate string when byte count changes. */
19469 if (utf_char2len(lc) == l)
19470 utf_char2bytes(lc, p);
19471 p += l;
19472 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019473 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 p += l; /* skip multi-byte character */
19475 else
19476#endif
19477 {
19478 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19479 ++p;
19480 }
19481 }
19482}
19483
19484/*
19485 * "toupper(string)" function
19486 */
19487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019488f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019489 typval_T *argvars;
19490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019492 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019493 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494}
19495
19496/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019497 * "tr(string, fromstr, tostr)" function
19498 */
19499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019500f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019501 typval_T *argvars;
19502 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019503{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019504 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019505 char_u *fromstr;
19506 char_u *tostr;
19507 char_u *p;
19508#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019509 int inlen;
19510 int fromlen;
19511 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019512 int idx;
19513 char_u *cpstr;
19514 int cplen;
19515 int first = TRUE;
19516#endif
19517 char_u buf[NUMBUFLEN];
19518 char_u buf2[NUMBUFLEN];
19519 garray_T ga;
19520
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019521 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019522 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19523 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019524
19525 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019526 rettv->v_type = VAR_STRING;
19527 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019528 if (fromstr == NULL || tostr == NULL)
19529 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019530 ga_init2(&ga, (int)sizeof(char), 80);
19531
19532#ifdef FEAT_MBYTE
19533 if (!has_mbyte)
19534#endif
19535 /* not multi-byte: fromstr and tostr must be the same length */
19536 if (STRLEN(fromstr) != STRLEN(tostr))
19537 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019538#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019539error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019540#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019541 EMSG2(_(e_invarg2), fromstr);
19542 ga_clear(&ga);
19543 return;
19544 }
19545
19546 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019547 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019548 {
19549#ifdef FEAT_MBYTE
19550 if (has_mbyte)
19551 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019552 inlen = (*mb_ptr2len)(in_str);
19553 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019554 cplen = inlen;
19555 idx = 0;
19556 for (p = fromstr; *p != NUL; p += fromlen)
19557 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019558 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019559 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019560 {
19561 for (p = tostr; *p != NUL; p += tolen)
19562 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019563 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019564 if (idx-- == 0)
19565 {
19566 cplen = tolen;
19567 cpstr = p;
19568 break;
19569 }
19570 }
19571 if (*p == NUL) /* tostr is shorter than fromstr */
19572 goto error;
19573 break;
19574 }
19575 ++idx;
19576 }
19577
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019578 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019579 {
19580 /* Check that fromstr and tostr have the same number of
19581 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019582 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019583 first = FALSE;
19584 for (p = tostr; *p != NUL; p += tolen)
19585 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019586 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019587 --idx;
19588 }
19589 if (idx != 0)
19590 goto error;
19591 }
19592
19593 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019594 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019595 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019596
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019597 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019598 }
19599 else
19600#endif
19601 {
19602 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019603 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019604 if (p != NULL)
19605 ga_append(&ga, tostr[p - fromstr]);
19606 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019607 ga_append(&ga, *in_str);
19608 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019609 }
19610 }
19611
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019612 /* add a terminating NUL */
19613 ga_grow(&ga, 1);
19614 ga_append(&ga, NUL);
19615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019616 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019617}
19618
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019619#ifdef FEAT_FLOAT
19620/*
19621 * "trunc({float})" function
19622 */
19623 static void
19624f_trunc(argvars, rettv)
19625 typval_T *argvars;
19626 typval_T *rettv;
19627{
19628 float_T f;
19629
19630 rettv->v_type = VAR_FLOAT;
19631 if (get_float_arg(argvars, &f) == OK)
19632 /* trunc() is not in C90, use floor() or ceil() instead. */
19633 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19634 else
19635 rettv->vval.v_float = 0.0;
19636}
19637#endif
19638
Bram Moolenaar8299df92004-07-10 09:47:34 +000019639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 * "type(expr)" function
19641 */
19642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019643f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 typval_T *argvars;
19645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019647 int n;
19648
19649 switch (argvars[0].v_type)
19650 {
19651 case VAR_NUMBER: n = 0; break;
19652 case VAR_STRING: n = 1; break;
19653 case VAR_FUNC: n = 2; break;
19654 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019655 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019656#ifdef FEAT_FLOAT
19657 case VAR_FLOAT: n = 5; break;
19658#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019659 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19660 }
19661 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662}
19663
19664/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019665 * "undofile(name)" function
19666 */
19667 static void
19668f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019669 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019670 typval_T *rettv;
19671{
19672 rettv->v_type = VAR_STRING;
19673#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019674 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019675 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019676
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019677 if (*fname == NUL)
19678 {
19679 /* If there is no file name there will be no undo file. */
19680 rettv->vval.v_string = NULL;
19681 }
19682 else
19683 {
19684 char_u *ffname = FullName_save(fname, FALSE);
19685
19686 if (ffname != NULL)
19687 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19688 vim_free(ffname);
19689 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019690 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019691#else
19692 rettv->vval.v_string = NULL;
19693#endif
19694}
19695
19696/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019697 * "undotree()" function
19698 */
19699 static void
19700f_undotree(argvars, rettv)
19701 typval_T *argvars UNUSED;
19702 typval_T *rettv;
19703{
19704 if (rettv_dict_alloc(rettv) == OK)
19705 {
19706 dict_T *dict = rettv->vval.v_dict;
19707 list_T *list;
19708
Bram Moolenaar730cde92010-06-27 05:18:54 +020019709 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019710 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019711 dict_add_nr_str(dict, "save_last",
19712 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019713 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19714 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019715 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019716
19717 list = list_alloc();
19718 if (list != NULL)
19719 {
19720 u_eval_tree(curbuf->b_u_oldhead, list);
19721 dict_add_list(dict, "entries", list);
19722 }
19723 }
19724}
19725
19726/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019727 * "values(dict)" function
19728 */
19729 static void
19730f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019731 typval_T *argvars;
19732 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019733{
19734 dict_list(argvars, rettv, 1);
19735}
19736
19737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738 * "virtcol(string)" function
19739 */
19740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019741f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019742 typval_T *argvars;
19743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744{
19745 colnr_T vcol = 0;
19746 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019747 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019749 fp = var2fpos(&argvars[0], FALSE, &fnum);
19750 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19751 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 {
19753 getvvcol(curwin, fp, NULL, NULL, &vcol);
19754 ++vcol;
19755 }
19756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758}
19759
19760/*
19761 * "visualmode()" function
19762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019764f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019765 typval_T *argvars UNUSED;
19766 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 char_u str[2];
19769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019770 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 str[0] = curbuf->b_visual_mode_eval;
19772 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774
19775 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019776 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778}
19779
19780/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019781 * "wildmenumode()" function
19782 */
19783 static void
19784f_wildmenumode(argvars, rettv)
19785 typval_T *argvars UNUSED;
19786 typval_T *rettv UNUSED;
19787{
19788#ifdef FEAT_WILDMENU
19789 if (wild_menu_showing)
19790 rettv->vval.v_number = 1;
19791#endif
19792}
19793
19794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 * "winbufnr(nr)" function
19796 */
19797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019798f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019799 typval_T *argvars;
19800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801{
19802 win_T *wp;
19803
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019804 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019808 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809}
19810
19811/*
19812 * "wincol()" function
19813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019815f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019816 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818{
19819 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019820 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821}
19822
19823/*
19824 * "winheight(nr)" function
19825 */
19826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019827f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019828 typval_T *argvars;
19829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830{
19831 win_T *wp;
19832
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019833 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019835 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019837 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838}
19839
19840/*
19841 * "winline()" function
19842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019844f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019845 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847{
19848 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019849 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850}
19851
19852/*
19853 * "winnr()" function
19854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019856f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019857 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859{
19860 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019861
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019863 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019865 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866}
19867
19868/*
19869 * "winrestcmd()" function
19870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019872f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019873 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875{
19876#ifdef FEAT_WINDOWS
19877 win_T *wp;
19878 int winnr = 1;
19879 garray_T ga;
19880 char_u buf[50];
19881
19882 ga_init2(&ga, (int)sizeof(char), 70);
19883 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19884 {
19885 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19886 ga_concat(&ga, buf);
19887# ifdef FEAT_VERTSPLIT
19888 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19889 ga_concat(&ga, buf);
19890# endif
19891 ++winnr;
19892 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019893 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019895 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019897 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019899 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900}
19901
19902/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019903 * "winrestview()" function
19904 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019905 static void
19906f_winrestview(argvars, rettv)
19907 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019908 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019909{
19910 dict_T *dict;
19911
19912 if (argvars[0].v_type != VAR_DICT
19913 || (dict = argvars[0].vval.v_dict) == NULL)
19914 EMSG(_(e_invarg));
19915 else
19916 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019917 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19918 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19919 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19920 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019921#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019922 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19923 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019924#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019925 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19926 {
19927 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19928 curwin->w_set_curswant = FALSE;
19929 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019930
Bram Moolenaar82c25852014-05-28 16:47:16 +020019931 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19932 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019933#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019934 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19935 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019936#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019937 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19938 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19939 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19940 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019941
19942 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019943 win_new_height(curwin, curwin->w_height);
19944# ifdef FEAT_VERTSPLIT
19945 win_new_width(curwin, W_WIDTH(curwin));
19946# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019947 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019948
Bram Moolenaarb851a962014-10-31 15:45:52 +010019949 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019950 curwin->w_topline = 1;
19951 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19952 curwin->w_topline = curbuf->b_ml.ml_line_count;
19953#ifdef FEAT_DIFF
19954 check_topfill(curwin, TRUE);
19955#endif
19956 }
19957}
19958
19959/*
19960 * "winsaveview()" function
19961 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019962 static void
19963f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019964 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019965 typval_T *rettv;
19966{
19967 dict_T *dict;
19968
Bram Moolenaara800b422010-06-27 01:15:55 +020019969 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019970 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019971 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019972
19973 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19974 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19975#ifdef FEAT_VIRTUALEDIT
19976 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19977#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019978 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019979 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19980
19981 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19982#ifdef FEAT_DIFF
19983 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19984#endif
19985 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19986 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19987}
19988
19989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 * "winwidth(nr)" function
19991 */
19992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019993f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019994 typval_T *argvars;
19995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996{
19997 win_T *wp;
19998
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019999 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020001 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 else
20003#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020006 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007#endif
20008}
20009
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020011 * Write list of strings to file
20012 */
20013 static int
20014write_list(fd, list, binary)
20015 FILE *fd;
20016 list_T *list;
20017 int binary;
20018{
20019 listitem_T *li;
20020 int c;
20021 int ret = OK;
20022 char_u *s;
20023
20024 for (li = list->lv_first; li != NULL; li = li->li_next)
20025 {
20026 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20027 {
20028 if (*s == '\n')
20029 c = putc(NUL, fd);
20030 else
20031 c = putc(*s, fd);
20032 if (c == EOF)
20033 {
20034 ret = FAIL;
20035 break;
20036 }
20037 }
20038 if (!binary || li->li_next != NULL)
20039 if (putc('\n', fd) == EOF)
20040 {
20041 ret = FAIL;
20042 break;
20043 }
20044 if (ret == FAIL)
20045 {
20046 EMSG(_(e_write));
20047 break;
20048 }
20049 }
20050 return ret;
20051}
20052
20053/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020054 * "writefile()" function
20055 */
20056 static void
20057f_writefile(argvars, rettv)
20058 typval_T *argvars;
20059 typval_T *rettv;
20060{
20061 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020062 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020063 char_u *fname;
20064 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020065 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020066
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020067 if (check_restricted() || check_secure())
20068 return;
20069
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020070 if (argvars[0].v_type != VAR_LIST)
20071 {
20072 EMSG2(_(e_listarg), "writefile()");
20073 return;
20074 }
20075 if (argvars[0].vval.v_list == NULL)
20076 return;
20077
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020078 if (argvars[2].v_type != VAR_UNKNOWN)
20079 {
20080 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20081 binary = TRUE;
20082 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20083 append = TRUE;
20084 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020085
20086 /* Always open the file in binary mode, library functions have a mind of
20087 * their own about CR-LF conversion. */
20088 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020089 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20090 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020091 {
20092 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20093 ret = -1;
20094 }
20095 else
20096 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020097 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20098 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020099 fclose(fd);
20100 }
20101
20102 rettv->vval.v_number = ret;
20103}
20104
20105/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020106 * "xor(expr, expr)" function
20107 */
20108 static void
20109f_xor(argvars, rettv)
20110 typval_T *argvars;
20111 typval_T *rettv;
20112{
20113 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20114 ^ get_tv_number_chk(&argvars[1], NULL);
20115}
20116
20117
20118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020120 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 */
20122 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020123var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020124 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020125 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020126 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020128 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020130 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131
Bram Moolenaara5525202006-03-02 22:52:09 +000020132 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020133 if (varp->v_type == VAR_LIST)
20134 {
20135 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020136 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020137 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020138 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020139
20140 l = varp->vval.v_list;
20141 if (l == NULL)
20142 return NULL;
20143
20144 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020145 pos.lnum = list_find_nr(l, 0L, &error);
20146 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020147 return NULL; /* invalid line number */
20148
20149 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020150 pos.col = list_find_nr(l, 1L, &error);
20151 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020152 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020153 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020154
20155 /* We accept "$" for the column number: last column. */
20156 li = list_find(l, 1L);
20157 if (li != NULL && li->li_tv.v_type == VAR_STRING
20158 && li->li_tv.vval.v_string != NULL
20159 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20160 pos.col = len + 1;
20161
Bram Moolenaara5525202006-03-02 22:52:09 +000020162 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020163 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020164 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020165 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020166
Bram Moolenaara5525202006-03-02 22:52:09 +000020167#ifdef FEAT_VIRTUALEDIT
20168 /* Get the virtual offset. Defaults to zero. */
20169 pos.coladd = list_find_nr(l, 2L, &error);
20170 if (error)
20171 pos.coladd = 0;
20172#endif
20173
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020174 return &pos;
20175 }
20176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020177 name = get_tv_string_chk(varp);
20178 if (name == NULL)
20179 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020180 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020182 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20183 {
20184 if (VIsual_active)
20185 return &VIsual;
20186 return &curwin->w_cursor;
20187 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020188 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020190 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20192 return NULL;
20193 return pp;
20194 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020195
20196#ifdef FEAT_VIRTUALEDIT
20197 pos.coladd = 0;
20198#endif
20199
Bram Moolenaar477933c2007-07-17 14:32:23 +000020200 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020201 {
20202 pos.col = 0;
20203 if (name[1] == '0') /* "w0": first visible line */
20204 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020205 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020206 pos.lnum = curwin->w_topline;
20207 return &pos;
20208 }
20209 else if (name[1] == '$') /* "w$": last visible line */
20210 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020211 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020212 pos.lnum = curwin->w_botline - 1;
20213 return &pos;
20214 }
20215 }
20216 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020218 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 {
20220 pos.lnum = curbuf->b_ml.ml_line_count;
20221 pos.col = 0;
20222 }
20223 else
20224 {
20225 pos.lnum = curwin->w_cursor.lnum;
20226 pos.col = (colnr_T)STRLEN(ml_get_curline());
20227 }
20228 return &pos;
20229 }
20230 return NULL;
20231}
20232
20233/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020234 * Convert list in "arg" into a position and optional file number.
20235 * When "fnump" is NULL there is no file number, only 3 items.
20236 * Note that the column is passed on as-is, the caller may want to decrement
20237 * it to use 1 for the first column.
20238 * Return FAIL when conversion is not possible, doesn't check the position for
20239 * validity.
20240 */
20241 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020242list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020243 typval_T *arg;
20244 pos_T *posp;
20245 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020246 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020247{
20248 list_T *l = arg->vval.v_list;
20249 long i = 0;
20250 long n;
20251
Bram Moolenaar493c1782014-05-28 14:34:46 +020020252 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20253 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020254 if (arg->v_type != VAR_LIST
20255 || l == NULL
20256 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020257 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020258 return FAIL;
20259
20260 if (fnump != NULL)
20261 {
20262 n = list_find_nr(l, i++, NULL); /* fnum */
20263 if (n < 0)
20264 return FAIL;
20265 if (n == 0)
20266 n = curbuf->b_fnum; /* current buffer */
20267 *fnump = n;
20268 }
20269
20270 n = list_find_nr(l, i++, NULL); /* lnum */
20271 if (n < 0)
20272 return FAIL;
20273 posp->lnum = n;
20274
20275 n = list_find_nr(l, i++, NULL); /* col */
20276 if (n < 0)
20277 return FAIL;
20278 posp->col = n;
20279
20280#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020281 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020282 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020283 posp->coladd = 0;
20284 else
20285 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020286#endif
20287
Bram Moolenaar493c1782014-05-28 14:34:46 +020020288 if (curswantp != NULL)
20289 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20290
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020291 return OK;
20292}
20293
20294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 * Get the length of an environment variable name.
20296 * Advance "arg" to the first character after the name.
20297 * Return 0 for error.
20298 */
20299 static int
20300get_env_len(arg)
20301 char_u **arg;
20302{
20303 char_u *p;
20304 int len;
20305
20306 for (p = *arg; vim_isIDc(*p); ++p)
20307 ;
20308 if (p == *arg) /* no name found */
20309 return 0;
20310
20311 len = (int)(p - *arg);
20312 *arg = p;
20313 return len;
20314}
20315
20316/*
20317 * Get the length of the name of a function or internal variable.
20318 * "arg" is advanced to the first non-white character after the name.
20319 * Return 0 if something is wrong.
20320 */
20321 static int
20322get_id_len(arg)
20323 char_u **arg;
20324{
20325 char_u *p;
20326 int len;
20327
20328 /* Find the end of the name. */
20329 for (p = *arg; eval_isnamec(*p); ++p)
20330 ;
20331 if (p == *arg) /* no name found */
20332 return 0;
20333
20334 len = (int)(p - *arg);
20335 *arg = skipwhite(p);
20336
20337 return len;
20338}
20339
20340/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020341 * Get the length of the name of a variable or function.
20342 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020344 * Return -1 if curly braces expansion failed.
20345 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 * If the name contains 'magic' {}'s, expand them and return the
20347 * expanded name in an allocated string via 'alias' - caller must free.
20348 */
20349 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020350get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 char_u **arg;
20352 char_u **alias;
20353 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020354 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355{
20356 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 char_u *p;
20358 char_u *expr_start;
20359 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360
20361 *alias = NULL; /* default to no alias */
20362
20363 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20364 && (*arg)[2] == (int)KE_SNR)
20365 {
20366 /* hard coded <SNR>, already translated */
20367 *arg += 3;
20368 return get_id_len(arg) + 3;
20369 }
20370 len = eval_fname_script(*arg);
20371 if (len > 0)
20372 {
20373 /* literal "<SID>", "s:" or "<SNR>" */
20374 *arg += len;
20375 }
20376
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020378 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020380 p = find_name_end(*arg, &expr_start, &expr_end,
20381 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 if (expr_start != NULL)
20383 {
20384 char_u *temp_string;
20385
20386 if (!evaluate)
20387 {
20388 len += (int)(p - *arg);
20389 *arg = skipwhite(p);
20390 return len;
20391 }
20392
20393 /*
20394 * Include any <SID> etc in the expanded string:
20395 * Thus the -len here.
20396 */
20397 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20398 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020399 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 *alias = temp_string;
20401 *arg = skipwhite(p);
20402 return (int)STRLEN(temp_string);
20403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404
20405 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020406 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 EMSG2(_(e_invexpr2), *arg);
20408
20409 return len;
20410}
20411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020412/*
20413 * Find the end of a variable or function name, taking care of magic braces.
20414 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20415 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020416 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020417 * Return a pointer to just after the name. Equal to "arg" if there is no
20418 * valid name.
20419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020421find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 char_u *arg;
20423 char_u **expr_start;
20424 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020425 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020427 int mb_nest = 0;
20428 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 char_u *p;
20430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020431 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020433 *expr_start = NULL;
20434 *expr_end = NULL;
20435 }
20436
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020437 /* Quick check for valid starting character. */
20438 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20439 return arg;
20440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020441 for (p = arg; *p != NUL
20442 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020443 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020444 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020445 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020446 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020447 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020448 if (*p == '\'')
20449 {
20450 /* skip over 'string' to avoid counting [ and ] inside it. */
20451 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20452 ;
20453 if (*p == NUL)
20454 break;
20455 }
20456 else if (*p == '"')
20457 {
20458 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20459 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20460 if (*p == '\\' && p[1] != NUL)
20461 ++p;
20462 if (*p == NUL)
20463 break;
20464 }
20465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020466 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020468 if (*p == '[')
20469 ++br_nest;
20470 else if (*p == ']')
20471 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020474 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020476 if (*p == '{')
20477 {
20478 mb_nest++;
20479 if (expr_start != NULL && *expr_start == NULL)
20480 *expr_start = p;
20481 }
20482 else if (*p == '}')
20483 {
20484 mb_nest--;
20485 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20486 *expr_end = p;
20487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 }
20490
20491 return p;
20492}
20493
20494/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020495 * Expands out the 'magic' {}'s in a variable/function name.
20496 * Note that this can call itself recursively, to deal with
20497 * constructs like foo{bar}{baz}{bam}
20498 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20499 * "in_start" ^
20500 * "expr_start" ^
20501 * "expr_end" ^
20502 * "in_end" ^
20503 *
20504 * Returns a new allocated string, which the caller must free.
20505 * Returns NULL for failure.
20506 */
20507 static char_u *
20508make_expanded_name(in_start, expr_start, expr_end, in_end)
20509 char_u *in_start;
20510 char_u *expr_start;
20511 char_u *expr_end;
20512 char_u *in_end;
20513{
20514 char_u c1;
20515 char_u *retval = NULL;
20516 char_u *temp_result;
20517 char_u *nextcmd = NULL;
20518
20519 if (expr_end == NULL || in_end == NULL)
20520 return NULL;
20521 *expr_start = NUL;
20522 *expr_end = NUL;
20523 c1 = *in_end;
20524 *in_end = NUL;
20525
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020526 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020527 if (temp_result != NULL && nextcmd == NULL)
20528 {
20529 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20530 + (in_end - expr_end) + 1));
20531 if (retval != NULL)
20532 {
20533 STRCPY(retval, in_start);
20534 STRCAT(retval, temp_result);
20535 STRCAT(retval, expr_end + 1);
20536 }
20537 }
20538 vim_free(temp_result);
20539
20540 *in_end = c1; /* put char back for error messages */
20541 *expr_start = '{';
20542 *expr_end = '}';
20543
20544 if (retval != NULL)
20545 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020546 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020547 if (expr_start != NULL)
20548 {
20549 /* Further expansion! */
20550 temp_result = make_expanded_name(retval, expr_start,
20551 expr_end, temp_result);
20552 vim_free(retval);
20553 retval = temp_result;
20554 }
20555 }
20556
20557 return retval;
20558}
20559
20560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020562 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 */
20564 static int
20565eval_isnamec(c)
20566 int c;
20567{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020568 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20569}
20570
20571/*
20572 * Return TRUE if character "c" can be used as the first character in a
20573 * variable or function name (excluding '{' and '}').
20574 */
20575 static int
20576eval_isnamec1(c)
20577 int c;
20578{
20579 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580}
20581
20582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 * Set number v: variable to "val".
20584 */
20585 void
20586set_vim_var_nr(idx, val)
20587 int idx;
20588 long val;
20589{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020590 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591}
20592
20593/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020594 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 */
20596 long
20597get_vim_var_nr(idx)
20598 int idx;
20599{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020600 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601}
20602
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020603/*
20604 * Get string v: variable value. Uses a static buffer, can only be used once.
20605 */
20606 char_u *
20607get_vim_var_str(idx)
20608 int idx;
20609{
20610 return get_tv_string(&vimvars[idx].vv_tv);
20611}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020612
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020614 * Get List v: variable value. Caller must take care of reference count when
20615 * needed.
20616 */
20617 list_T *
20618get_vim_var_list(idx)
20619 int idx;
20620{
20621 return vimvars[idx].vv_list;
20622}
20623
20624/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020625 * Set v:char to character "c".
20626 */
20627 void
20628set_vim_var_char(c)
20629 int c;
20630{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020631 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020632
20633#ifdef FEAT_MBYTE
20634 if (has_mbyte)
20635 buf[(*mb_char2bytes)(c, buf)] = NUL;
20636 else
20637#endif
20638 {
20639 buf[0] = c;
20640 buf[1] = NUL;
20641 }
20642 set_vim_var_string(VV_CHAR, buf, -1);
20643}
20644
20645/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020646 * Set v:count to "count" and v:count1 to "count1".
20647 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648 */
20649 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020650set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651 long count;
20652 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020653 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020655 if (set_prevcount)
20656 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020657 vimvars[VV_COUNT].vv_nr = count;
20658 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659}
20660
20661/*
20662 * Set string v: variable to a copy of "val".
20663 */
20664 void
20665set_vim_var_string(idx, val, len)
20666 int idx;
20667 char_u *val;
20668 int len; /* length of "val" to use or -1 (whole string) */
20669{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020670 /* Need to do this (at least) once, since we can't initialize a union.
20671 * Will always be invoked when "v:progname" is set. */
20672 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20673
Bram Moolenaare9a41262005-01-15 22:18:47 +000020674 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020676 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020678 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020680 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681}
20682
20683/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020684 * Set List v: variable to "val".
20685 */
20686 void
20687set_vim_var_list(idx, val)
20688 int idx;
20689 list_T *val;
20690{
20691 list_unref(vimvars[idx].vv_list);
20692 vimvars[idx].vv_list = val;
20693 if (val != NULL)
20694 ++val->lv_refcount;
20695}
20696
20697/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020698 * Set Dictionary v: variable to "val".
20699 */
20700 void
20701set_vim_var_dict(idx, val)
20702 int idx;
20703 dict_T *val;
20704{
20705 int todo;
20706 hashitem_T *hi;
20707
20708 dict_unref(vimvars[idx].vv_dict);
20709 vimvars[idx].vv_dict = val;
20710 if (val != NULL)
20711 {
20712 ++val->dv_refcount;
20713
20714 /* Set readonly */
20715 todo = (int)val->dv_hashtab.ht_used;
20716 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20717 {
20718 if (HASHITEM_EMPTY(hi))
20719 continue;
20720 --todo;
20721 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20722 }
20723 }
20724}
20725
20726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727 * Set v:register if needed.
20728 */
20729 void
20730set_reg_var(c)
20731 int c;
20732{
20733 char_u regname;
20734
20735 if (c == 0 || c == ' ')
20736 regname = '"';
20737 else
20738 regname = c;
20739 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020740 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 set_vim_var_string(VV_REG, &regname, 1);
20742}
20743
20744/*
20745 * Get or set v:exception. If "oldval" == NULL, return the current value.
20746 * Otherwise, restore the value to "oldval" and return NULL.
20747 * Must always be called in pairs to save and restore v:exception! Does not
20748 * take care of memory allocations.
20749 */
20750 char_u *
20751v_exception(oldval)
20752 char_u *oldval;
20753{
20754 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020755 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756
Bram Moolenaare9a41262005-01-15 22:18:47 +000020757 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 return NULL;
20759}
20760
20761/*
20762 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20763 * Otherwise, restore the value to "oldval" and return NULL.
20764 * Must always be called in pairs to save and restore v:throwpoint! Does not
20765 * take care of memory allocations.
20766 */
20767 char_u *
20768v_throwpoint(oldval)
20769 char_u *oldval;
20770{
20771 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020772 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773
Bram Moolenaare9a41262005-01-15 22:18:47 +000020774 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775 return NULL;
20776}
20777
20778#if defined(FEAT_AUTOCMD) || defined(PROTO)
20779/*
20780 * Set v:cmdarg.
20781 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20782 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20783 * Must always be called in pairs!
20784 */
20785 char_u *
20786set_cmdarg(eap, oldarg)
20787 exarg_T *eap;
20788 char_u *oldarg;
20789{
20790 char_u *oldval;
20791 char_u *newval;
20792 unsigned len;
20793
Bram Moolenaare9a41262005-01-15 22:18:47 +000020794 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020795 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020797 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020798 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020799 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 }
20801
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020802 if (eap->force_bin == FORCE_BIN)
20803 len = 6;
20804 else if (eap->force_bin == FORCE_NOBIN)
20805 len = 8;
20806 else
20807 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020808
20809 if (eap->read_edit)
20810 len += 7;
20811
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020812 if (eap->force_ff != 0)
20813 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20814# ifdef FEAT_MBYTE
20815 if (eap->force_enc != 0)
20816 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020817 if (eap->bad_char != 0)
20818 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020819# endif
20820
20821 newval = alloc(len + 1);
20822 if (newval == NULL)
20823 return NULL;
20824
20825 if (eap->force_bin == FORCE_BIN)
20826 sprintf((char *)newval, " ++bin");
20827 else if (eap->force_bin == FORCE_NOBIN)
20828 sprintf((char *)newval, " ++nobin");
20829 else
20830 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020831
20832 if (eap->read_edit)
20833 STRCAT(newval, " ++edit");
20834
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020835 if (eap->force_ff != 0)
20836 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20837 eap->cmd + eap->force_ff);
20838# ifdef FEAT_MBYTE
20839 if (eap->force_enc != 0)
20840 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20841 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020842 if (eap->bad_char == BAD_KEEP)
20843 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20844 else if (eap->bad_char == BAD_DROP)
20845 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20846 else if (eap->bad_char != 0)
20847 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020848# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020849 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020850 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851}
20852#endif
20853
20854/*
20855 * Get the value of internal variable "name".
20856 * Return OK or FAIL.
20857 */
20858 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020859get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 char_u *name;
20861 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020862 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020863 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020864 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020865 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866{
20867 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020868 typval_T *tv = NULL;
20869 typval_T atv;
20870 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872
20873 /* truncate the name, so that we can use strcmp() */
20874 cc = name[len];
20875 name[len] = NUL;
20876
20877 /*
20878 * Check for "b:changedtick".
20879 */
20880 if (STRCMP(name, "b:changedtick") == 0)
20881 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020882 atv.v_type = VAR_NUMBER;
20883 atv.vval.v_number = curbuf->b_changedtick;
20884 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 }
20886
20887 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 * Check for user-defined variables.
20889 */
20890 else
20891 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020892 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020894 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020895 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020896 if (dip != NULL)
20897 *dip = v;
20898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 }
20900
Bram Moolenaare9a41262005-01-15 22:18:47 +000020901 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020903 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020904 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 ret = FAIL;
20906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020907 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020908 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909
20910 name[len] = cc;
20911
20912 return ret;
20913}
20914
20915/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020916 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20917 * Also handle function call with Funcref variable: func(expr)
20918 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20919 */
20920 static int
20921handle_subscript(arg, rettv, evaluate, verbose)
20922 char_u **arg;
20923 typval_T *rettv;
20924 int evaluate; /* do more than finding the end */
20925 int verbose; /* give error messages */
20926{
20927 int ret = OK;
20928 dict_T *selfdict = NULL;
20929 char_u *s;
20930 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020931 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020932
20933 while (ret == OK
20934 && (**arg == '['
20935 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020936 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020937 && !vim_iswhite(*(*arg - 1)))
20938 {
20939 if (**arg == '(')
20940 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020941 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020942 if (evaluate)
20943 {
20944 functv = *rettv;
20945 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020946
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020947 /* Invoke the function. Recursive! */
20948 s = functv.vval.v_string;
20949 }
20950 else
20951 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020952 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020953 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20954 &len, evaluate, selfdict);
20955
20956 /* Clear the funcref afterwards, so that deleting it while
20957 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020958 if (evaluate)
20959 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020960
20961 /* Stop the expression evaluation when immediately aborting on
20962 * error, or when an interrupt occurred or an exception was thrown
20963 * but not caught. */
20964 if (aborting())
20965 {
20966 if (ret == OK)
20967 clear_tv(rettv);
20968 ret = FAIL;
20969 }
20970 dict_unref(selfdict);
20971 selfdict = NULL;
20972 }
20973 else /* **arg == '[' || **arg == '.' */
20974 {
20975 dict_unref(selfdict);
20976 if (rettv->v_type == VAR_DICT)
20977 {
20978 selfdict = rettv->vval.v_dict;
20979 if (selfdict != NULL)
20980 ++selfdict->dv_refcount;
20981 }
20982 else
20983 selfdict = NULL;
20984 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20985 {
20986 clear_tv(rettv);
20987 ret = FAIL;
20988 }
20989 }
20990 }
20991 dict_unref(selfdict);
20992 return ret;
20993}
20994
20995/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020996 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020997 * value).
20998 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020999 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021000alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021001{
Bram Moolenaar33570922005-01-25 22:26:29 +000021002 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021003}
21004
21005/*
21006 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 * The string "s" must have been allocated, it is consumed.
21008 * Return NULL for out of memory, the variable otherwise.
21009 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021010 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021011alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 char_u *s;
21013{
Bram Moolenaar33570922005-01-25 22:26:29 +000021014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021016 rettv = alloc_tv();
21017 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021019 rettv->v_type = VAR_STRING;
21020 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 }
21022 else
21023 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021024 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025}
21026
21027/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021028 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021030 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021031free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021032 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033{
21034 if (varp != NULL)
21035 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021036 switch (varp->v_type)
21037 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021038 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021039 func_unref(varp->vval.v_string);
21040 /*FALLTHROUGH*/
21041 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021042 vim_free(varp->vval.v_string);
21043 break;
21044 case VAR_LIST:
21045 list_unref(varp->vval.v_list);
21046 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021047 case VAR_DICT:
21048 dict_unref(varp->vval.v_dict);
21049 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021050 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021051#ifdef FEAT_FLOAT
21052 case VAR_FLOAT:
21053#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021054 case VAR_UNKNOWN:
21055 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021056 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021057 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021058 break;
21059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021060 vim_free(varp);
21061 }
21062}
21063
21064/*
21065 * Free the memory for a variable value and set the value to NULL or 0.
21066 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021067 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021068clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021069 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070{
21071 if (varp != NULL)
21072 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021073 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021075 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021076 func_unref(varp->vval.v_string);
21077 /*FALLTHROUGH*/
21078 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021079 vim_free(varp->vval.v_string);
21080 varp->vval.v_string = NULL;
21081 break;
21082 case VAR_LIST:
21083 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021084 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021085 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021086 case VAR_DICT:
21087 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021088 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021089 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021090 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021091 varp->vval.v_number = 0;
21092 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021093#ifdef FEAT_FLOAT
21094 case VAR_FLOAT:
21095 varp->vval.v_float = 0.0;
21096 break;
21097#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021098 case VAR_UNKNOWN:
21099 break;
21100 default:
21101 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021103 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 }
21105}
21106
21107/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021108 * Set the value of a variable to NULL without freeing items.
21109 */
21110 static void
21111init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021112 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021113{
21114 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021115 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021116}
21117
21118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 * Get the number value of a variable.
21120 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021121 * For incompatible types, return 0.
21122 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21123 * caller of incompatible types: it sets *denote to TRUE if "denote"
21124 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 */
21126 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021127get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021128 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021130 int error = FALSE;
21131
21132 return get_tv_number_chk(varp, &error); /* return 0L on error */
21133}
21134
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021135 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021136get_tv_number_chk(varp, denote)
21137 typval_T *varp;
21138 int *denote;
21139{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021140 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021142 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021144 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021145 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021146#ifdef FEAT_FLOAT
21147 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021148 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021149 break;
21150#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021151 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021152 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021153 break;
21154 case VAR_STRING:
21155 if (varp->vval.v_string != NULL)
21156 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020021157 TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021158 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021159 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021160 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021161 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021162 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021163 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021164 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021165 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021166 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021167 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021169 if (denote == NULL) /* useful for values that must be unsigned */
21170 n = -1;
21171 else
21172 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021173 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174}
21175
21176/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021177 * Get the lnum from the first argument.
21178 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021179 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 */
21181 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021182get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021183 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184{
Bram Moolenaar33570922005-01-25 22:26:29 +000021185 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186 linenr_T lnum;
21187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021188 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 if (lnum == 0) /* no valid number, try using line() */
21190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021191 rettv.v_type = VAR_NUMBER;
21192 f_line(argvars, &rettv);
21193 lnum = rettv.vval.v_number;
21194 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 }
21196 return lnum;
21197}
21198
21199/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021200 * Get the lnum from the first argument.
21201 * Also accepts "$", then "buf" is used.
21202 * Returns 0 on error.
21203 */
21204 static linenr_T
21205get_tv_lnum_buf(argvars, buf)
21206 typval_T *argvars;
21207 buf_T *buf;
21208{
21209 if (argvars[0].v_type == VAR_STRING
21210 && argvars[0].vval.v_string != NULL
21211 && argvars[0].vval.v_string[0] == '$'
21212 && buf != NULL)
21213 return buf->b_ml.ml_line_count;
21214 return get_tv_number_chk(&argvars[0], NULL);
21215}
21216
21217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218 * Get the string value of a variable.
21219 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021220 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21221 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222 * If the String variable has never been set, return an empty string.
21223 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021224 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21225 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 */
21227 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021228get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021229 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230{
21231 static char_u mybuf[NUMBUFLEN];
21232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021233 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234}
21235
21236 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021237get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021238 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021239 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021241 char_u *res = get_tv_string_buf_chk(varp, buf);
21242
21243 return res != NULL ? res : (char_u *)"";
21244}
21245
Bram Moolenaar7d647822014-04-05 21:28:56 +020021246/*
21247 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21248 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021249 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021250get_tv_string_chk(varp)
21251 typval_T *varp;
21252{
21253 static char_u mybuf[NUMBUFLEN];
21254
21255 return get_tv_string_buf_chk(varp, mybuf);
21256}
21257
21258 static char_u *
21259get_tv_string_buf_chk(varp, buf)
21260 typval_T *varp;
21261 char_u *buf;
21262{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021263 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021265 case VAR_NUMBER:
21266 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21267 return buf;
21268 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021269 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021270 break;
21271 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021272 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021273 break;
21274 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021275 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021276 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021277#ifdef FEAT_FLOAT
21278 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021279 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021280 break;
21281#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021282 case VAR_STRING:
21283 if (varp->vval.v_string != NULL)
21284 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021285 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021286 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021287 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021290 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291}
21292
21293/*
21294 * Find variable "name" in the list of variables.
21295 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021296 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021297 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021298 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021300 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021301find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021303 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021304 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021307 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308
Bram Moolenaara7043832005-01-21 11:56:39 +000021309 ht = find_var_ht(name, &varname);
21310 if (htp != NULL)
21311 *htp = ht;
21312 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021314 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315}
21316
21317/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021318 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021319 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021321 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021322find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021323 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021324 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021325 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021326 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021327{
Bram Moolenaar33570922005-01-25 22:26:29 +000021328 hashitem_T *hi;
21329
21330 if (*varname == NUL)
21331 {
21332 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021333 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021334 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021335 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021336 case 'g': return &globvars_var;
21337 case 'v': return &vimvars_var;
21338 case 'b': return &curbuf->b_bufvar;
21339 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021340#ifdef FEAT_WINDOWS
21341 case 't': return &curtab->tp_winvar;
21342#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021343 case 'l': return current_funccal == NULL
21344 ? NULL : &current_funccal->l_vars_var;
21345 case 'a': return current_funccal == NULL
21346 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021347 }
21348 return NULL;
21349 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021350
21351 hi = hash_find(ht, varname);
21352 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021353 {
21354 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021355 * worked find the variable again. Don't auto-load a script if it was
21356 * loaded already, otherwise it would be loaded every time when
21357 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021358 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021359 {
21360 /* Note: script_autoload() may make "hi" invalid. It must either
21361 * be obtained again or not used. */
21362 if (!script_autoload(varname, FALSE) || aborting())
21363 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021364 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021365 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021366 if (HASHITEM_EMPTY(hi))
21367 return NULL;
21368 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021369 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021370}
21371
21372/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021373 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021374 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021375 * Set "varname" to the start of name without ':'.
21376 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021377 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021378find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 char_u *name;
21380 char_u **varname;
21381{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021382 hashitem_T *hi;
21383
Bram Moolenaar73627d02015-08-11 15:46:09 +020021384 if (name[0] == NUL)
21385 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 if (name[1] != ':')
21387 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021388 /* The name must not start with a colon or #. */
21389 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 return NULL;
21391 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021392
21393 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021394 hi = hash_find(&compat_hashtab, name);
21395 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021396 return &compat_hashtab;
21397
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021399 return &globvarht; /* global variable */
21400 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 }
21402 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021403 if (*name == 'g') /* global variable */
21404 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021405 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21406 */
21407 if (vim_strchr(name + 2, ':') != NULL
21408 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021409 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021411 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021413 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021414#ifdef FEAT_WINDOWS
21415 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021416 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021417#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021418 if (*name == 'v') /* v: variable */
21419 return &vimvarht;
21420 if (*name == 'a' && current_funccal != NULL) /* function argument */
21421 return &current_funccal->l_avars.dv_hashtab;
21422 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21423 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 if (*name == 's' /* script variable */
21425 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21426 return &SCRIPT_VARS(current_SID);
21427 return NULL;
21428}
21429
21430/*
21431 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021432 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 * Returns NULL when it doesn't exist.
21434 */
21435 char_u *
21436get_var_value(name)
21437 char_u *name;
21438{
Bram Moolenaar33570922005-01-25 22:26:29 +000021439 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021441 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 if (v == NULL)
21443 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021444 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445}
21446
21447/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021448 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 * sourcing this script and when executing functions defined in the script.
21450 */
21451 void
21452new_script_vars(id)
21453 scid_T id;
21454{
Bram Moolenaara7043832005-01-21 11:56:39 +000021455 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021456 hashtab_T *ht;
21457 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021458
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21460 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021461 /* Re-allocating ga_data means that an ht_array pointing to
21462 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021463 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021464 for (i = 1; i <= ga_scripts.ga_len; ++i)
21465 {
21466 ht = &SCRIPT_VARS(i);
21467 if (ht->ht_mask == HT_INIT_SIZE - 1)
21468 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021469 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021470 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021471 }
21472
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473 while (ga_scripts.ga_len < id)
21474 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021475 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021476 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021477 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479 }
21480 }
21481}
21482
21483/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021484 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21485 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 */
21487 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021488init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021489 dict_T *dict;
21490 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021491 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492{
Bram Moolenaar33570922005-01-25 22:26:29 +000021493 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021494 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021495 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021496 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021497 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021498 dict_var->di_tv.vval.v_dict = dict;
21499 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021500 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021501 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21502 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503}
21504
21505/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021506 * Unreference a dictionary initialized by init_var_dict().
21507 */
21508 void
21509unref_var_dict(dict)
21510 dict_T *dict;
21511{
21512 /* Now the dict needs to be freed if no one else is using it, go back to
21513 * normal reference counting. */
21514 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21515 dict_unref(dict);
21516}
21517
21518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021520 * Frees all allocated variables and the value they contain.
21521 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 */
21523 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021524vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021525 hashtab_T *ht;
21526{
21527 vars_clear_ext(ht, TRUE);
21528}
21529
21530/*
21531 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21532 */
21533 static void
21534vars_clear_ext(ht, free_val)
21535 hashtab_T *ht;
21536 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537{
Bram Moolenaara7043832005-01-21 11:56:39 +000021538 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021539 hashitem_T *hi;
21540 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021543 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021544 for (hi = ht->ht_array; todo > 0; ++hi)
21545 {
21546 if (!HASHITEM_EMPTY(hi))
21547 {
21548 --todo;
21549
Bram Moolenaar33570922005-01-25 22:26:29 +000021550 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021551 * ht_array might change then. hash_clear() takes care of it
21552 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021553 v = HI2DI(hi);
21554 if (free_val)
21555 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021556 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021557 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021558 }
21559 }
21560 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021561 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562}
21563
Bram Moolenaara7043832005-01-21 11:56:39 +000021564/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021565 * Delete a variable from hashtab "ht" at item "hi".
21566 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021569delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021570 hashtab_T *ht;
21571 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572{
Bram Moolenaar33570922005-01-25 22:26:29 +000021573 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021574
21575 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021576 clear_tv(&di->di_tv);
21577 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578}
21579
21580/*
21581 * List the value of one internal variable.
21582 */
21583 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021584list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021585 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021587 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021589 char_u *tofree;
21590 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021591 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021592
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021593 current_copyID += COPYID_INC;
21594 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021595 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021596 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021597 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598}
21599
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021601list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 char_u *prefix;
21603 char_u *name;
21604 int type;
21605 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021606 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607{
Bram Moolenaar31859182007-08-14 20:41:13 +000021608 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21609 msg_start();
21610 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611 if (name != NULL) /* "a:" vars don't have a name stored */
21612 msg_puts(name);
21613 msg_putchar(' ');
21614 msg_advance(22);
21615 if (type == VAR_NUMBER)
21616 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021617 else if (type == VAR_FUNC)
21618 msg_putchar('*');
21619 else if (type == VAR_LIST)
21620 {
21621 msg_putchar('[');
21622 if (*string == '[')
21623 ++string;
21624 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021625 else if (type == VAR_DICT)
21626 {
21627 msg_putchar('{');
21628 if (*string == '{')
21629 ++string;
21630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 else
21632 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021633
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021635
21636 if (type == VAR_FUNC)
21637 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021638 if (*first)
21639 {
21640 msg_clr_eos();
21641 *first = FALSE;
21642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643}
21644
21645/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021646 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 * If the variable already exists, the value is updated.
21648 * Otherwise the variable is created.
21649 */
21650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021651set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021653 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021654 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655{
Bram Moolenaar33570922005-01-25 22:26:29 +000021656 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021658 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021660 ht = find_var_ht(name, &varname);
21661 if (ht == NULL || *varname == NUL)
21662 {
21663 EMSG2(_(e_illvar), name);
21664 return;
21665 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021666 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021667
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021668 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21669 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021670
Bram Moolenaar33570922005-01-25 22:26:29 +000021671 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021673 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021674 if (var_check_ro(v->di_flags, name, FALSE)
21675 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021676 return;
21677 if (v->di_tv.v_type != tv->v_type
21678 && !((v->di_tv.v_type == VAR_STRING
21679 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021680 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021681 || tv->v_type == VAR_NUMBER))
21682#ifdef FEAT_FLOAT
21683 && !((v->di_tv.v_type == VAR_NUMBER
21684 || v->di_tv.v_type == VAR_FLOAT)
21685 && (tv->v_type == VAR_NUMBER
21686 || tv->v_type == VAR_FLOAT))
21687#endif
21688 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021689 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021690 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021691 return;
21692 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021693
21694 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021695 * Handle setting internal v: variables separately where needed to
21696 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021697 */
21698 if (ht == &vimvarht)
21699 {
21700 if (v->di_tv.v_type == VAR_STRING)
21701 {
21702 vim_free(v->di_tv.vval.v_string);
21703 if (copy || tv->v_type != VAR_STRING)
21704 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21705 else
21706 {
21707 /* Take over the string to avoid an extra alloc/free. */
21708 v->di_tv.vval.v_string = tv->vval.v_string;
21709 tv->vval.v_string = NULL;
21710 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021711 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021712 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021713 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021714 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021715 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021716 if (STRCMP(varname, "searchforward") == 0)
21717 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021718#ifdef FEAT_SEARCH_EXTRA
21719 else if (STRCMP(varname, "hlsearch") == 0)
21720 {
21721 no_hlsearch = !v->di_tv.vval.v_number;
21722 redraw_all_later(SOME_VALID);
21723 }
21724#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021725 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021726 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021727 else if (v->di_tv.v_type != tv->v_type)
21728 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021729 }
21730
21731 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 }
21733 else /* add a new variable */
21734 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021735 /* Can't add "v:" variable. */
21736 if (ht == &vimvarht)
21737 {
21738 EMSG2(_(e_illvar), name);
21739 return;
21740 }
21741
Bram Moolenaar92124a32005-06-17 22:03:40 +000021742 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021743 if (!valid_varname(varname))
21744 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021745
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021746 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21747 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021748 if (v == NULL)
21749 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021750 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021751 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021753 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 return;
21755 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021756 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021758
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021759 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021760 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021761 else
21762 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021763 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021764 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021765 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767}
21768
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021769/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021770 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021771 * Also give an error message.
21772 */
21773 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021774var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021775 int flags;
21776 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021777 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021778{
21779 if (flags & DI_FLAGS_RO)
21780 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021781 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021782 return TRUE;
21783 }
21784 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21785 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021786 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021787 return TRUE;
21788 }
21789 return FALSE;
21790}
21791
21792/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021793 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21794 * Also give an error message.
21795 */
21796 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021797var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021798 int flags;
21799 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021800 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021801{
21802 if (flags & DI_FLAGS_FIX)
21803 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021804 EMSG2(_("E795: Cannot delete variable %s"),
21805 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021806 return TRUE;
21807 }
21808 return FALSE;
21809}
21810
21811/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021812 * Check if a funcref is assigned to a valid variable name.
21813 * Return TRUE and give an error if not.
21814 */
21815 static int
21816var_check_func_name(name, new_var)
21817 char_u *name; /* points to start of variable name */
21818 int new_var; /* TRUE when creating the variable */
21819{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021820 /* Allow for w: b: s: and t:. */
21821 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021822 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21823 ? name[2] : name[0]))
21824 {
21825 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21826 name);
21827 return TRUE;
21828 }
21829 /* Don't allow hiding a function. When "v" is not NULL we might be
21830 * assigning another function to the same var, the type is checked
21831 * below. */
21832 if (new_var && function_exists(name))
21833 {
21834 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21835 name);
21836 return TRUE;
21837 }
21838 return FALSE;
21839}
21840
21841/*
21842 * Check if a variable name is valid.
21843 * Return FALSE and give an error if not.
21844 */
21845 static int
21846valid_varname(varname)
21847 char_u *varname;
21848{
21849 char_u *p;
21850
21851 for (p = varname; *p != NUL; ++p)
21852 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21853 && *p != AUTOLOAD_CHAR)
21854 {
21855 EMSG2(_(e_illvar), varname);
21856 return FALSE;
21857 }
21858 return TRUE;
21859}
21860
21861/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021862 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021863 * Also give an error message, using "name" or _("name") when use_gettext is
21864 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021865 */
21866 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021867tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021868 int lock;
21869 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021870 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021871{
21872 if (lock & VAR_LOCKED)
21873 {
21874 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021875 name == NULL ? (char_u *)_("Unknown")
21876 : use_gettext ? (char_u *)_(name)
21877 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021878 return TRUE;
21879 }
21880 if (lock & VAR_FIXED)
21881 {
21882 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021883 name == NULL ? (char_u *)_("Unknown")
21884 : use_gettext ? (char_u *)_(name)
21885 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021886 return TRUE;
21887 }
21888 return FALSE;
21889}
21890
21891/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021892 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021893 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021894 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021895 * It is OK for "from" and "to" to point to the same item. This is used to
21896 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021897 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021898 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021899copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021900 typval_T *from;
21901 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021903 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021904 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021905 switch (from->v_type)
21906 {
21907 case VAR_NUMBER:
21908 to->vval.v_number = from->vval.v_number;
21909 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021910#ifdef FEAT_FLOAT
21911 case VAR_FLOAT:
21912 to->vval.v_float = from->vval.v_float;
21913 break;
21914#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021915 case VAR_STRING:
21916 case VAR_FUNC:
21917 if (from->vval.v_string == NULL)
21918 to->vval.v_string = NULL;
21919 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021920 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021921 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021922 if (from->v_type == VAR_FUNC)
21923 func_ref(to->vval.v_string);
21924 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021925 break;
21926 case VAR_LIST:
21927 if (from->vval.v_list == NULL)
21928 to->vval.v_list = NULL;
21929 else
21930 {
21931 to->vval.v_list = from->vval.v_list;
21932 ++to->vval.v_list->lv_refcount;
21933 }
21934 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021935 case VAR_DICT:
21936 if (from->vval.v_dict == NULL)
21937 to->vval.v_dict = NULL;
21938 else
21939 {
21940 to->vval.v_dict = from->vval.v_dict;
21941 ++to->vval.v_dict->dv_refcount;
21942 }
21943 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021944 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021945 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021946 break;
21947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948}
21949
21950/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021951 * Make a copy of an item.
21952 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021953 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21954 * reference to an already copied list/dict can be used.
21955 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021956 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021957 static int
21958item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021959 typval_T *from;
21960 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021961 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021962 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021963{
21964 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021965 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021966
Bram Moolenaar33570922005-01-25 22:26:29 +000021967 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021968 {
21969 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021970 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021971 }
21972 ++recurse;
21973
21974 switch (from->v_type)
21975 {
21976 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021977#ifdef FEAT_FLOAT
21978 case VAR_FLOAT:
21979#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021980 case VAR_STRING:
21981 case VAR_FUNC:
21982 copy_tv(from, to);
21983 break;
21984 case VAR_LIST:
21985 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021986 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021987 if (from->vval.v_list == NULL)
21988 to->vval.v_list = NULL;
21989 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21990 {
21991 /* use the copy made earlier */
21992 to->vval.v_list = from->vval.v_list->lv_copylist;
21993 ++to->vval.v_list->lv_refcount;
21994 }
21995 else
21996 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21997 if (to->vval.v_list == NULL)
21998 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021999 break;
22000 case VAR_DICT:
22001 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022002 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022003 if (from->vval.v_dict == NULL)
22004 to->vval.v_dict = NULL;
22005 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22006 {
22007 /* use the copy made earlier */
22008 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22009 ++to->vval.v_dict->dv_refcount;
22010 }
22011 else
22012 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22013 if (to->vval.v_dict == NULL)
22014 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022015 break;
22016 default:
22017 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022018 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022019 }
22020 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022021 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022022}
22023
22024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 * ":echo expr1 ..." print each argument separated with a space, add a
22026 * newline at the end.
22027 * ":echon expr1 ..." print each argument plain.
22028 */
22029 void
22030ex_echo(eap)
22031 exarg_T *eap;
22032{
22033 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022034 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022035 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 char_u *p;
22037 int needclr = TRUE;
22038 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022039 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040
22041 if (eap->skip)
22042 ++emsg_skip;
22043 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22044 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022045 /* If eval1() causes an error message the text from the command may
22046 * still need to be cleared. E.g., "echo 22,44". */
22047 need_clr_eos = needclr;
22048
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022050 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 {
22052 /*
22053 * Report the invalid expression unless the expression evaluation
22054 * has been cancelled due to an aborting error, an interrupt, or an
22055 * exception.
22056 */
22057 if (!aborting())
22058 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022059 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 break;
22061 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022062 need_clr_eos = FALSE;
22063
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 if (!eap->skip)
22065 {
22066 if (atstart)
22067 {
22068 atstart = FALSE;
22069 /* Call msg_start() after eval1(), evaluating the expression
22070 * may cause a message to appear. */
22071 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022072 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022073 /* Mark the saved text as finishing the line, so that what
22074 * follows is displayed on a new line when scrolling back
22075 * at the more prompt. */
22076 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 }
22080 else if (eap->cmdidx == CMD_echo)
22081 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022082 current_copyID += COPYID_INC;
22083 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022084 if (p != NULL)
22085 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022087 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022089 if (*p != TAB && needclr)
22090 {
22091 /* remove any text still there from the command */
22092 msg_clr_eos();
22093 needclr = FALSE;
22094 }
22095 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 }
22097 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022098 {
22099#ifdef FEAT_MBYTE
22100 if (has_mbyte)
22101 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022102 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022103
22104 (void)msg_outtrans_len_attr(p, i, echo_attr);
22105 p += i - 1;
22106 }
22107 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022109 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022112 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115 arg = skipwhite(arg);
22116 }
22117 eap->nextcmd = check_nextcmd(arg);
22118
22119 if (eap->skip)
22120 --emsg_skip;
22121 else
22122 {
22123 /* remove text that may still be there from the command */
22124 if (needclr)
22125 msg_clr_eos();
22126 if (eap->cmdidx == CMD_echo)
22127 msg_end();
22128 }
22129}
22130
22131/*
22132 * ":echohl {name}".
22133 */
22134 void
22135ex_echohl(eap)
22136 exarg_T *eap;
22137{
22138 int id;
22139
22140 id = syn_name2id(eap->arg);
22141 if (id == 0)
22142 echo_attr = 0;
22143 else
22144 echo_attr = syn_id2attr(id);
22145}
22146
22147/*
22148 * ":execute expr1 ..." execute the result of an expression.
22149 * ":echomsg expr1 ..." Print a message
22150 * ":echoerr expr1 ..." Print an error
22151 * Each gets spaces around each argument and a newline at the end for
22152 * echo commands
22153 */
22154 void
22155ex_execute(eap)
22156 exarg_T *eap;
22157{
22158 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022159 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 int ret = OK;
22161 char_u *p;
22162 garray_T ga;
22163 int len;
22164 int save_did_emsg;
22165
22166 ga_init2(&ga, 1, 80);
22167
22168 if (eap->skip)
22169 ++emsg_skip;
22170 while (*arg != NUL && *arg != '|' && *arg != '\n')
22171 {
22172 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022173 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 {
22175 /*
22176 * Report the invalid expression unless the expression evaluation
22177 * has been cancelled due to an aborting error, an interrupt, or an
22178 * exception.
22179 */
22180 if (!aborting())
22181 EMSG2(_(e_invexpr2), p);
22182 ret = FAIL;
22183 break;
22184 }
22185
22186 if (!eap->skip)
22187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022188 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 len = (int)STRLEN(p);
22190 if (ga_grow(&ga, len + 2) == FAIL)
22191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022192 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193 ret = FAIL;
22194 break;
22195 }
22196 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 ga.ga_len += len;
22200 }
22201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022202 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 arg = skipwhite(arg);
22204 }
22205
22206 if (ret != FAIL && ga.ga_data != NULL)
22207 {
22208 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022211 out_flush();
22212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 else if (eap->cmdidx == CMD_echoerr)
22214 {
22215 /* We don't want to abort following commands, restore did_emsg. */
22216 save_did_emsg = did_emsg;
22217 EMSG((char_u *)ga.ga_data);
22218 if (!force_abort)
22219 did_emsg = save_did_emsg;
22220 }
22221 else if (eap->cmdidx == CMD_execute)
22222 do_cmdline((char_u *)ga.ga_data,
22223 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22224 }
22225
22226 ga_clear(&ga);
22227
22228 if (eap->skip)
22229 --emsg_skip;
22230
22231 eap->nextcmd = check_nextcmd(arg);
22232}
22233
22234/*
22235 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22236 * "arg" points to the "&" or '+' when called, to "option" when returning.
22237 * Returns NULL when no option name found. Otherwise pointer to the char
22238 * after the option name.
22239 */
22240 static char_u *
22241find_option_end(arg, opt_flags)
22242 char_u **arg;
22243 int *opt_flags;
22244{
22245 char_u *p = *arg;
22246
22247 ++p;
22248 if (*p == 'g' && p[1] == ':')
22249 {
22250 *opt_flags = OPT_GLOBAL;
22251 p += 2;
22252 }
22253 else if (*p == 'l' && p[1] == ':')
22254 {
22255 *opt_flags = OPT_LOCAL;
22256 p += 2;
22257 }
22258 else
22259 *opt_flags = 0;
22260
22261 if (!ASCII_ISALPHA(*p))
22262 return NULL;
22263 *arg = p;
22264
22265 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22266 p += 4; /* termcap option */
22267 else
22268 while (ASCII_ISALPHA(*p))
22269 ++p;
22270 return p;
22271}
22272
22273/*
22274 * ":function"
22275 */
22276 void
22277ex_function(eap)
22278 exarg_T *eap;
22279{
22280 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022281 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 int j;
22283 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022285 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 char_u *name = NULL;
22287 char_u *p;
22288 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022289 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 garray_T newargs;
22291 garray_T newlines;
22292 int varargs = FALSE;
22293 int mustend = FALSE;
22294 int flags = 0;
22295 ufunc_T *fp;
22296 int indent;
22297 int nesting;
22298 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022299 dictitem_T *v;
22300 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022301 static int func_nr = 0; /* number for nameless function */
22302 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022303 hashtab_T *ht;
22304 int todo;
22305 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022306 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307
22308 /*
22309 * ":function" without argument: list functions.
22310 */
22311 if (ends_excmd(*eap->arg))
22312 {
22313 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022314 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022315 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022316 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022317 {
22318 if (!HASHITEM_EMPTY(hi))
22319 {
22320 --todo;
22321 fp = HI2UF(hi);
22322 if (!isdigit(*fp->uf_name))
22323 list_func_head(fp, FALSE);
22324 }
22325 }
22326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 eap->nextcmd = check_nextcmd(eap->arg);
22328 return;
22329 }
22330
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022331 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022332 * ":function /pat": list functions matching pattern.
22333 */
22334 if (*eap->arg == '/')
22335 {
22336 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22337 if (!eap->skip)
22338 {
22339 regmatch_T regmatch;
22340
22341 c = *p;
22342 *p = NUL;
22343 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22344 *p = c;
22345 if (regmatch.regprog != NULL)
22346 {
22347 regmatch.rm_ic = p_ic;
22348
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022349 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022350 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22351 {
22352 if (!HASHITEM_EMPTY(hi))
22353 {
22354 --todo;
22355 fp = HI2UF(hi);
22356 if (!isdigit(*fp->uf_name)
22357 && vim_regexec(&regmatch, fp->uf_name, 0))
22358 list_func_head(fp, FALSE);
22359 }
22360 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022361 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022362 }
22363 }
22364 if (*p == '/')
22365 ++p;
22366 eap->nextcmd = check_nextcmd(p);
22367 return;
22368 }
22369
22370 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022371 * Get the function name. There are these situations:
22372 * func normal function name
22373 * "name" == func, "fudi.fd_dict" == NULL
22374 * dict.func new dictionary entry
22375 * "name" == NULL, "fudi.fd_dict" set,
22376 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22377 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022378 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022379 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22380 * dict.func existing dict entry that's not a Funcref
22381 * "name" == NULL, "fudi.fd_dict" set,
22382 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022383 * s:func script-local function name
22384 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022387 name = trans_function_name(&p, eap->skip, 0, &fudi);
22388 paren = (vim_strchr(p, '(') != NULL);
22389 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 {
22391 /*
22392 * Return on an invalid expression in braces, unless the expression
22393 * evaluation has been cancelled due to an aborting error, an
22394 * interrupt, or an exception.
22395 */
22396 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022397 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022398 if (!eap->skip && fudi.fd_newkey != NULL)
22399 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022400 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 else
22404 eap->skip = TRUE;
22405 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022406
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 /* An error in a function call during evaluation of an expression in magic
22408 * braces should not cause the function not to be defined. */
22409 saved_did_emsg = did_emsg;
22410 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411
22412 /*
22413 * ":function func" with only function name: list function.
22414 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022415 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 {
22417 if (!ends_excmd(*skipwhite(p)))
22418 {
22419 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022420 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 }
22422 eap->nextcmd = check_nextcmd(p);
22423 if (eap->nextcmd != NULL)
22424 *p = NUL;
22425 if (!eap->skip && !got_int)
22426 {
22427 fp = find_func(name);
22428 if (fp != NULL)
22429 {
22430 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022431 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022433 if (FUNCLINE(fp, j) == NULL)
22434 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435 msg_putchar('\n');
22436 msg_outnum((long)(j + 1));
22437 if (j < 9)
22438 msg_putchar(' ');
22439 if (j < 99)
22440 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022441 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 out_flush(); /* show a line at a time */
22443 ui_breakcheck();
22444 }
22445 if (!got_int)
22446 {
22447 msg_putchar('\n');
22448 msg_puts((char_u *)" endfunction");
22449 }
22450 }
22451 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022452 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022454 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 }
22456
22457 /*
22458 * ":function name(arg1, arg2)" Define function.
22459 */
22460 p = skipwhite(p);
22461 if (*p != '(')
22462 {
22463 if (!eap->skip)
22464 {
22465 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022466 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 }
22468 /* attempt to continue by skipping some text */
22469 if (vim_strchr(p, '(') != NULL)
22470 p = vim_strchr(p, '(');
22471 }
22472 p = skipwhite(p + 1);
22473
22474 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22475 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22476
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022477 if (!eap->skip)
22478 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022479 /* Check the name of the function. Unless it's a dictionary function
22480 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022481 if (name != NULL)
22482 arg = name;
22483 else
22484 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022485 if (arg != NULL && (fudi.fd_di == NULL
22486 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022487 {
22488 if (*arg == K_SPECIAL)
22489 j = 3;
22490 else
22491 j = 0;
22492 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22493 : eval_isnamec(arg[j])))
22494 ++j;
22495 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022496 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022497 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022498 /* Disallow using the g: dict. */
22499 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22500 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022501 }
22502
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 /*
22504 * Isolate the arguments: "arg1, arg2, ...)"
22505 */
22506 while (*p != ')')
22507 {
22508 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22509 {
22510 varargs = TRUE;
22511 p += 3;
22512 mustend = TRUE;
22513 }
22514 else
22515 {
22516 arg = p;
22517 while (ASCII_ISALNUM(*p) || *p == '_')
22518 ++p;
22519 if (arg == p || isdigit(*arg)
22520 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22521 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22522 {
22523 if (!eap->skip)
22524 EMSG2(_("E125: Illegal argument: %s"), arg);
22525 break;
22526 }
22527 if (ga_grow(&newargs, 1) == FAIL)
22528 goto erret;
22529 c = *p;
22530 *p = NUL;
22531 arg = vim_strsave(arg);
22532 if (arg == NULL)
22533 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022534
22535 /* Check for duplicate argument name. */
22536 for (i = 0; i < newargs.ga_len; ++i)
22537 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22538 {
22539 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022540 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022541 goto erret;
22542 }
22543
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22545 *p = c;
22546 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 if (*p == ',')
22548 ++p;
22549 else
22550 mustend = TRUE;
22551 }
22552 p = skipwhite(p);
22553 if (mustend && *p != ')')
22554 {
22555 if (!eap->skip)
22556 EMSG2(_(e_invarg2), eap->arg);
22557 break;
22558 }
22559 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022560 if (*p != ')')
22561 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 ++p; /* skip the ')' */
22563
Bram Moolenaare9a41262005-01-15 22:18:47 +000022564 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 for (;;)
22566 {
22567 p = skipwhite(p);
22568 if (STRNCMP(p, "range", 5) == 0)
22569 {
22570 flags |= FC_RANGE;
22571 p += 5;
22572 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022573 else if (STRNCMP(p, "dict", 4) == 0)
22574 {
22575 flags |= FC_DICT;
22576 p += 4;
22577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 else if (STRNCMP(p, "abort", 5) == 0)
22579 {
22580 flags |= FC_ABORT;
22581 p += 5;
22582 }
22583 else
22584 break;
22585 }
22586
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022587 /* When there is a line break use what follows for the function body.
22588 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22589 if (*p == '\n')
22590 line_arg = p + 1;
22591 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 EMSG(_(e_trailing));
22593
22594 /*
22595 * Read the body of the function, until ":endfunction" is found.
22596 */
22597 if (KeyTyped)
22598 {
22599 /* Check if the function already exists, don't let the user type the
22600 * whole function before telling him it doesn't work! For a script we
22601 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022602 if (!eap->skip && !eap->forceit)
22603 {
22604 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22605 EMSG(_(e_funcdict));
22606 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022607 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022610 if (!eap->skip && did_emsg)
22611 goto erret;
22612
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 msg_putchar('\n'); /* don't overwrite the function name */
22614 cmdline_row = msg_row;
22615 }
22616
22617 indent = 2;
22618 nesting = 0;
22619 for (;;)
22620 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022621 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022622 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022623 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022624 saved_wait_return = FALSE;
22625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022627 sourcing_lnum_off = sourcing_lnum;
22628
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022629 if (line_arg != NULL)
22630 {
22631 /* Use eap->arg, split up in parts by line breaks. */
22632 theline = line_arg;
22633 p = vim_strchr(theline, '\n');
22634 if (p == NULL)
22635 line_arg += STRLEN(line_arg);
22636 else
22637 {
22638 *p = NUL;
22639 line_arg = p + 1;
22640 }
22641 }
22642 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 theline = getcmdline(':', 0L, indent);
22644 else
22645 theline = eap->getline(':', eap->cookie, indent);
22646 if (KeyTyped)
22647 lines_left = Rows - 1;
22648 if (theline == NULL)
22649 {
22650 EMSG(_("E126: Missing :endfunction"));
22651 goto erret;
22652 }
22653
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022654 /* Detect line continuation: sourcing_lnum increased more than one. */
22655 if (sourcing_lnum > sourcing_lnum_off + 1)
22656 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22657 else
22658 sourcing_lnum_off = 0;
22659
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 if (skip_until != NULL)
22661 {
22662 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22663 * don't check for ":endfunc". */
22664 if (STRCMP(theline, skip_until) == 0)
22665 {
22666 vim_free(skip_until);
22667 skip_until = NULL;
22668 }
22669 }
22670 else
22671 {
22672 /* skip ':' and blanks*/
22673 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22674 ;
22675
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022676 /* Check for "endfunction". */
22677 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022679 if (line_arg == NULL)
22680 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 break;
22682 }
22683
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022684 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 * at "end". */
22686 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22687 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022688 else if (STRNCMP(p, "if", 2) == 0
22689 || STRNCMP(p, "wh", 2) == 0
22690 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 || STRNCMP(p, "try", 3) == 0)
22692 indent += 2;
22693
22694 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022695 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022697 if (*p == '!')
22698 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022700 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22701 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022703 ++nesting;
22704 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 }
22706 }
22707
22708 /* Check for ":append" or ":insert". */
22709 p = skip_range(p, NULL);
22710 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22711 || (p[0] == 'i'
22712 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22713 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22714 skip_until = vim_strsave((char_u *)".");
22715
22716 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22717 arg = skipwhite(skiptowhite(p));
22718 if (arg[0] == '<' && arg[1] =='<'
22719 && ((p[0] == 'p' && p[1] == 'y'
22720 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22721 || (p[0] == 'p' && p[1] == 'e'
22722 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22723 || (p[0] == 't' && p[1] == 'c'
22724 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022725 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22726 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22728 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022729 || (p[0] == 'm' && p[1] == 'z'
22730 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 ))
22732 {
22733 /* ":python <<" continues until a dot, like ":append" */
22734 p = skipwhite(arg + 2);
22735 if (*p == NUL)
22736 skip_until = vim_strsave((char_u *)".");
22737 else
22738 skip_until = vim_strsave(p);
22739 }
22740 }
22741
22742 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022743 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022744 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022745 if (line_arg == NULL)
22746 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022748 }
22749
22750 /* Copy the line to newly allocated memory. get_one_sourceline()
22751 * allocates 250 bytes per line, this saves 80% on average. The cost
22752 * is an extra alloc/free. */
22753 p = vim_strsave(theline);
22754 if (p != NULL)
22755 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022756 if (line_arg == NULL)
22757 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022758 theline = p;
22759 }
22760
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022761 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22762
22763 /* Add NULL lines for continuation lines, so that the line count is
22764 * equal to the index in the growarray. */
22765 while (sourcing_lnum_off-- > 0)
22766 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022767
22768 /* Check for end of eap->arg. */
22769 if (line_arg != NULL && *line_arg == NUL)
22770 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 }
22772
22773 /* Don't define the function when skipping commands or when an error was
22774 * detected. */
22775 if (eap->skip || did_emsg)
22776 goto erret;
22777
22778 /*
22779 * If there are no errors, add the function
22780 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022781 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022782 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022783 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022784 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022785 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022786 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022787 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022788 goto erret;
22789 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022790
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022791 fp = find_func(name);
22792 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022794 if (!eap->forceit)
22795 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022796 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022797 goto erret;
22798 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022799 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022800 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022801 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022802 name);
22803 goto erret;
22804 }
22805 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022806 ga_clear_strings(&(fp->uf_args));
22807 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022808 vim_free(name);
22809 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 }
22812 else
22813 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022814 char numbuf[20];
22815
22816 fp = NULL;
22817 if (fudi.fd_newkey == NULL && !eap->forceit)
22818 {
22819 EMSG(_(e_funcdict));
22820 goto erret;
22821 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022822 if (fudi.fd_di == NULL)
22823 {
22824 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022825 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022826 goto erret;
22827 }
22828 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022829 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022830 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022831
22832 /* Give the function a sequential number. Can only be used with a
22833 * Funcref! */
22834 vim_free(name);
22835 sprintf(numbuf, "%d", ++func_nr);
22836 name = vim_strsave((char_u *)numbuf);
22837 if (name == NULL)
22838 goto erret;
22839 }
22840
22841 if (fp == NULL)
22842 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022843 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022844 {
22845 int slen, plen;
22846 char_u *scriptname;
22847
22848 /* Check that the autoload name matches the script name. */
22849 j = FAIL;
22850 if (sourcing_name != NULL)
22851 {
22852 scriptname = autoload_name(name);
22853 if (scriptname != NULL)
22854 {
22855 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022856 plen = (int)STRLEN(p);
22857 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022858 if (slen > plen && fnamecmp(p,
22859 sourcing_name + slen - plen) == 0)
22860 j = OK;
22861 vim_free(scriptname);
22862 }
22863 }
22864 if (j == FAIL)
22865 {
22866 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22867 goto erret;
22868 }
22869 }
22870
22871 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 if (fp == NULL)
22873 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022874
22875 if (fudi.fd_dict != NULL)
22876 {
22877 if (fudi.fd_di == NULL)
22878 {
22879 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022880 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022881 if (fudi.fd_di == NULL)
22882 {
22883 vim_free(fp);
22884 goto erret;
22885 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022886 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22887 {
22888 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022889 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022890 goto erret;
22891 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022892 }
22893 else
22894 /* overwrite existing dict entry */
22895 clear_tv(&fudi.fd_di->di_tv);
22896 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022897 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022898 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022899 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022900
22901 /* behave like "dict" was used */
22902 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022903 }
22904
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022906 STRCPY(fp->uf_name, name);
22907 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022909 fp->uf_args = newargs;
22910 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022911#ifdef FEAT_PROFILE
22912 fp->uf_tml_count = NULL;
22913 fp->uf_tml_total = NULL;
22914 fp->uf_tml_self = NULL;
22915 fp->uf_profiling = FALSE;
22916 if (prof_def_func())
22917 func_do_profile(fp);
22918#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022919 fp->uf_varargs = varargs;
22920 fp->uf_flags = flags;
22921 fp->uf_calls = 0;
22922 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022923 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924
22925erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 ga_clear_strings(&newargs);
22927 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022928ret_free:
22929 vim_free(skip_until);
22930 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022933 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934}
22935
22936/*
22937 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022938 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022940 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022941 * TFN_INT: internal function name OK
22942 * TFN_QUIET: be quiet
22943 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944 * Advances "pp" to just after the function name (if no error).
22945 */
22946 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022947trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 char_u **pp;
22949 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022950 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022951 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022953 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 char_u *start;
22955 char_u *end;
22956 int lead;
22957 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022959 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022960
22961 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022962 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022964
22965 /* Check for hard coded <SNR>: already translated function ID (from a user
22966 * command). */
22967 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22968 && (*pp)[2] == (int)KE_SNR)
22969 {
22970 *pp += 3;
22971 len = get_id_len(pp) + 3;
22972 return vim_strnsave(start, len);
22973 }
22974
22975 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22976 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022978 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022980
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022981 /* Note that TFN_ flags use the same values as GLV_ flags. */
22982 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022983 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022984 if (end == start)
22985 {
22986 if (!skip)
22987 EMSG(_("E129: Function name required"));
22988 goto theend;
22989 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022990 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022991 {
22992 /*
22993 * Report an invalid expression in braces, unless the expression
22994 * evaluation has been cancelled due to an aborting error, an
22995 * interrupt, or an exception.
22996 */
22997 if (!aborting())
22998 {
22999 if (end != NULL)
23000 EMSG2(_(e_invarg2), start);
23001 }
23002 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023003 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023004 goto theend;
23005 }
23006
23007 if (lv.ll_tv != NULL)
23008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023009 if (fdp != NULL)
23010 {
23011 fdp->fd_dict = lv.ll_dict;
23012 fdp->fd_newkey = lv.ll_newkey;
23013 lv.ll_newkey = NULL;
23014 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023015 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023016 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23017 {
23018 name = vim_strsave(lv.ll_tv->vval.v_string);
23019 *pp = end;
23020 }
23021 else
23022 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023023 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23024 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023025 EMSG(_(e_funcref));
23026 else
23027 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023028 name = NULL;
23029 }
23030 goto theend;
23031 }
23032
23033 if (lv.ll_name == NULL)
23034 {
23035 /* Error found, but continue after the function name. */
23036 *pp = end;
23037 goto theend;
23038 }
23039
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023040 /* Check if the name is a Funcref. If so, use the value. */
23041 if (lv.ll_exp_name != NULL)
23042 {
23043 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023044 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023045 if (name == lv.ll_exp_name)
23046 name = NULL;
23047 }
23048 else
23049 {
23050 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023051 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023052 if (name == *pp)
23053 name = NULL;
23054 }
23055 if (name != NULL)
23056 {
23057 name = vim_strsave(name);
23058 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023059 if (STRNCMP(name, "<SNR>", 5) == 0)
23060 {
23061 /* Change "<SNR>" to the byte sequence. */
23062 name[0] = K_SPECIAL;
23063 name[1] = KS_EXTRA;
23064 name[2] = (int)KE_SNR;
23065 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23066 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023067 goto theend;
23068 }
23069
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023070 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023071 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023072 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023073 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23074 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23075 {
23076 /* When there was "s:" already or the name expanded to get a
23077 * leading "s:" then remove it. */
23078 lv.ll_name += 2;
23079 len -= 2;
23080 lead = 2;
23081 }
23082 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023083 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023084 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023085 /* skip over "s:" and "g:" */
23086 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023087 lv.ll_name += 2;
23088 len = (int)(end - lv.ll_name);
23089 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023090
23091 /*
23092 * Copy the function name to allocated memory.
23093 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23094 * Accept <SNR>123_name() outside a script.
23095 */
23096 if (skip)
23097 lead = 0; /* do nothing */
23098 else if (lead > 0)
23099 {
23100 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023101 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23102 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023103 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023104 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023105 if (current_SID <= 0)
23106 {
23107 EMSG(_(e_usingsid));
23108 goto theend;
23109 }
23110 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23111 lead += (int)STRLEN(sid_buf);
23112 }
23113 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023114 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023115 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023116 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023117 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023118 goto theend;
23119 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023120 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023121 {
23122 char_u *cp = vim_strchr(lv.ll_name, ':');
23123
23124 if (cp != NULL && cp < end)
23125 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023126 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023127 goto theend;
23128 }
23129 }
23130
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023131 name = alloc((unsigned)(len + lead + 1));
23132 if (name != NULL)
23133 {
23134 if (lead > 0)
23135 {
23136 name[0] = K_SPECIAL;
23137 name[1] = KS_EXTRA;
23138 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023139 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023140 STRCPY(name + 3, sid_buf);
23141 }
23142 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023143 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023144 }
23145 *pp = end;
23146
23147theend:
23148 clear_lval(&lv);
23149 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150}
23151
23152/*
23153 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23154 * Return 2 if "p" starts with "s:".
23155 * Return 0 otherwise.
23156 */
23157 static int
23158eval_fname_script(p)
23159 char_u *p;
23160{
23161 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23162 || STRNICMP(p + 1, "SNR>", 4) == 0))
23163 return 5;
23164 if (p[0] == 's' && p[1] == ':')
23165 return 2;
23166 return 0;
23167}
23168
23169/*
23170 * Return TRUE if "p" starts with "<SID>" or "s:".
23171 * Only works if eval_fname_script() returned non-zero for "p"!
23172 */
23173 static int
23174eval_fname_sid(p)
23175 char_u *p;
23176{
23177 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23178}
23179
23180/*
23181 * List the head of the function: "name(arg1, arg2)".
23182 */
23183 static void
23184list_func_head(fp, indent)
23185 ufunc_T *fp;
23186 int indent;
23187{
23188 int j;
23189
23190 msg_start();
23191 if (indent)
23192 MSG_PUTS(" ");
23193 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023194 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 {
23196 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023197 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 }
23199 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023200 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023202 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203 {
23204 if (j)
23205 MSG_PUTS(", ");
23206 msg_puts(FUNCARG(fp, j));
23207 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023208 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 {
23210 if (j)
23211 MSG_PUTS(", ");
23212 MSG_PUTS("...");
23213 }
23214 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023215 if (fp->uf_flags & FC_ABORT)
23216 MSG_PUTS(" abort");
23217 if (fp->uf_flags & FC_RANGE)
23218 MSG_PUTS(" range");
23219 if (fp->uf_flags & FC_DICT)
23220 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023221 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023222 if (p_verbose > 0)
23223 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224}
23225
23226/*
23227 * Find a function by name, return pointer to it in ufuncs.
23228 * Return NULL for unknown function.
23229 */
23230 static ufunc_T *
23231find_func(name)
23232 char_u *name;
23233{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023234 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023236 hi = hash_find(&func_hashtab, name);
23237 if (!HASHITEM_EMPTY(hi))
23238 return HI2UF(hi);
23239 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240}
23241
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023242#if defined(EXITFREE) || defined(PROTO)
23243 void
23244free_all_functions()
23245{
23246 hashitem_T *hi;
23247
23248 /* Need to start all over every time, because func_free() may change the
23249 * hash table. */
23250 while (func_hashtab.ht_used > 0)
23251 for (hi = func_hashtab.ht_array; ; ++hi)
23252 if (!HASHITEM_EMPTY(hi))
23253 {
23254 func_free(HI2UF(hi));
23255 break;
23256 }
23257}
23258#endif
23259
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023260 int
23261translated_function_exists(name)
23262 char_u *name;
23263{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023264 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023265 return find_internal_func(name) >= 0;
23266 return find_func(name) != NULL;
23267}
23268
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023269/*
23270 * Return TRUE if a function "name" exists.
23271 */
23272 static int
23273function_exists(name)
23274 char_u *name;
23275{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023276 char_u *nm = name;
23277 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023278 int n = FALSE;
23279
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023280 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23281 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023282 nm = skipwhite(nm);
23283
23284 /* Only accept "funcname", "funcname ", "funcname (..." and
23285 * "funcname(...", not "funcname!...". */
23286 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023287 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023288 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023289 return n;
23290}
23291
Bram Moolenaara1544c02013-05-30 12:35:52 +020023292 char_u *
23293get_expanded_name(name, check)
23294 char_u *name;
23295 int check;
23296{
23297 char_u *nm = name;
23298 char_u *p;
23299
23300 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23301
23302 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023303 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023304 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023305
Bram Moolenaara1544c02013-05-30 12:35:52 +020023306 vim_free(p);
23307 return NULL;
23308}
23309
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023310/*
23311 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023312 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23313 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023314 */
23315 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023316builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023317 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023318 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023319{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023320 char_u *p;
23321
23322 if (!ASCII_ISLOWER(name[0]))
23323 return FALSE;
23324 p = vim_strchr(name, AUTOLOAD_CHAR);
23325 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023326}
23327
Bram Moolenaar05159a02005-02-26 23:04:13 +000023328#if defined(FEAT_PROFILE) || defined(PROTO)
23329/*
23330 * Start profiling function "fp".
23331 */
23332 static void
23333func_do_profile(fp)
23334 ufunc_T *fp;
23335{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023336 int len = fp->uf_lines.ga_len;
23337
23338 if (len == 0)
23339 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023340 fp->uf_tm_count = 0;
23341 profile_zero(&fp->uf_tm_self);
23342 profile_zero(&fp->uf_tm_total);
23343 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023344 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023345 if (fp->uf_tml_total == NULL)
23346 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023347 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023348 if (fp->uf_tml_self == NULL)
23349 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023350 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023351 fp->uf_tml_idx = -1;
23352 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23353 || fp->uf_tml_self == NULL)
23354 return; /* out of memory */
23355
23356 fp->uf_profiling = TRUE;
23357}
23358
23359/*
23360 * Dump the profiling results for all functions in file "fd".
23361 */
23362 void
23363func_dump_profile(fd)
23364 FILE *fd;
23365{
23366 hashitem_T *hi;
23367 int todo;
23368 ufunc_T *fp;
23369 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023370 ufunc_T **sorttab;
23371 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023372
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023373 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023374 if (todo == 0)
23375 return; /* nothing to dump */
23376
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023377 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023378
Bram Moolenaar05159a02005-02-26 23:04:13 +000023379 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23380 {
23381 if (!HASHITEM_EMPTY(hi))
23382 {
23383 --todo;
23384 fp = HI2UF(hi);
23385 if (fp->uf_profiling)
23386 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023387 if (sorttab != NULL)
23388 sorttab[st_len++] = fp;
23389
Bram Moolenaar05159a02005-02-26 23:04:13 +000023390 if (fp->uf_name[0] == K_SPECIAL)
23391 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23392 else
23393 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23394 if (fp->uf_tm_count == 1)
23395 fprintf(fd, "Called 1 time\n");
23396 else
23397 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23398 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23399 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23400 fprintf(fd, "\n");
23401 fprintf(fd, "count total (s) self (s)\n");
23402
23403 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23404 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023405 if (FUNCLINE(fp, i) == NULL)
23406 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023407 prof_func_line(fd, fp->uf_tml_count[i],
23408 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023409 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23410 }
23411 fprintf(fd, "\n");
23412 }
23413 }
23414 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023415
23416 if (sorttab != NULL && st_len > 0)
23417 {
23418 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23419 prof_total_cmp);
23420 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23421 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23422 prof_self_cmp);
23423 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23424 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023425
23426 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023427}
Bram Moolenaar73830342005-02-28 22:48:19 +000023428
23429 static void
23430prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23431 FILE *fd;
23432 ufunc_T **sorttab;
23433 int st_len;
23434 char *title;
23435 int prefer_self; /* when equal print only self time */
23436{
23437 int i;
23438 ufunc_T *fp;
23439
23440 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23441 fprintf(fd, "count total (s) self (s) function\n");
23442 for (i = 0; i < 20 && i < st_len; ++i)
23443 {
23444 fp = sorttab[i];
23445 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23446 prefer_self);
23447 if (fp->uf_name[0] == K_SPECIAL)
23448 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23449 else
23450 fprintf(fd, " %s()\n", fp->uf_name);
23451 }
23452 fprintf(fd, "\n");
23453}
23454
23455/*
23456 * Print the count and times for one function or function line.
23457 */
23458 static void
23459prof_func_line(fd, count, total, self, prefer_self)
23460 FILE *fd;
23461 int count;
23462 proftime_T *total;
23463 proftime_T *self;
23464 int prefer_self; /* when equal print only self time */
23465{
23466 if (count > 0)
23467 {
23468 fprintf(fd, "%5d ", count);
23469 if (prefer_self && profile_equal(total, self))
23470 fprintf(fd, " ");
23471 else
23472 fprintf(fd, "%s ", profile_msg(total));
23473 if (!prefer_self && profile_equal(total, self))
23474 fprintf(fd, " ");
23475 else
23476 fprintf(fd, "%s ", profile_msg(self));
23477 }
23478 else
23479 fprintf(fd, " ");
23480}
23481
23482/*
23483 * Compare function for total time sorting.
23484 */
23485 static int
23486#ifdef __BORLANDC__
23487_RTLENTRYF
23488#endif
23489prof_total_cmp(s1, s2)
23490 const void *s1;
23491 const void *s2;
23492{
23493 ufunc_T *p1, *p2;
23494
23495 p1 = *(ufunc_T **)s1;
23496 p2 = *(ufunc_T **)s2;
23497 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23498}
23499
23500/*
23501 * Compare function for self time sorting.
23502 */
23503 static int
23504#ifdef __BORLANDC__
23505_RTLENTRYF
23506#endif
23507prof_self_cmp(s1, s2)
23508 const void *s1;
23509 const void *s2;
23510{
23511 ufunc_T *p1, *p2;
23512
23513 p1 = *(ufunc_T **)s1;
23514 p2 = *(ufunc_T **)s2;
23515 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23516}
23517
Bram Moolenaar05159a02005-02-26 23:04:13 +000023518#endif
23519
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023520/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023521 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023522 * Return TRUE if a package was loaded.
23523 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023524 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023525script_autoload(name, reload)
23526 char_u *name;
23527 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023528{
23529 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023530 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023531 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023532 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023533
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023534 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023535 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023536 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023537 return FALSE;
23538
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023539 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023540
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023541 /* Find the name in the list of previously loaded package names. Skip
23542 * "autoload/", it's always the same. */
23543 for (i = 0; i < ga_loaded.ga_len; ++i)
23544 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23545 break;
23546 if (!reload && i < ga_loaded.ga_len)
23547 ret = FALSE; /* was loaded already */
23548 else
23549 {
23550 /* Remember the name if it wasn't loaded already. */
23551 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23552 {
23553 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23554 tofree = NULL;
23555 }
23556
23557 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023558 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023559 ret = TRUE;
23560 }
23561
23562 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023563 return ret;
23564}
23565
23566/*
23567 * Return the autoload script name for a function or variable name.
23568 * Returns NULL when out of memory.
23569 */
23570 static char_u *
23571autoload_name(name)
23572 char_u *name;
23573{
23574 char_u *p;
23575 char_u *scriptname;
23576
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023577 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023578 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23579 if (scriptname == NULL)
23580 return FALSE;
23581 STRCPY(scriptname, "autoload/");
23582 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023583 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023584 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023585 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023586 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023587 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023588}
23589
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23591
23592/*
23593 * Function given to ExpandGeneric() to obtain the list of user defined
23594 * function names.
23595 */
23596 char_u *
23597get_user_func_name(xp, idx)
23598 expand_T *xp;
23599 int idx;
23600{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023601 static long_u done;
23602 static hashitem_T *hi;
23603 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604
23605 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023607 done = 0;
23608 hi = func_hashtab.ht_array;
23609 }
23610 if (done < func_hashtab.ht_used)
23611 {
23612 if (done++ > 0)
23613 ++hi;
23614 while (HASHITEM_EMPTY(hi))
23615 ++hi;
23616 fp = HI2UF(hi);
23617
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023618 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023619 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023620
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023621 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23622 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623
23624 cat_func_name(IObuff, fp);
23625 if (xp->xp_context != EXPAND_USER_FUNC)
23626 {
23627 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023628 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 STRCAT(IObuff, ")");
23630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631 return IObuff;
23632 }
23633 return NULL;
23634}
23635
23636#endif /* FEAT_CMDL_COMPL */
23637
23638/*
23639 * Copy the function name of "fp" to buffer "buf".
23640 * "buf" must be able to hold the function name plus three bytes.
23641 * Takes care of script-local function names.
23642 */
23643 static void
23644cat_func_name(buf, fp)
23645 char_u *buf;
23646 ufunc_T *fp;
23647{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023648 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 {
23650 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023651 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 }
23653 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023654 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655}
23656
23657/*
23658 * ":delfunction {name}"
23659 */
23660 void
23661ex_delfunction(eap)
23662 exarg_T *eap;
23663{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023664 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 char_u *p;
23666 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023667 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668
23669 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023670 name = trans_function_name(&p, eap->skip, 0, &fudi);
23671 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023673 {
23674 if (fudi.fd_dict != NULL && !eap->skip)
23675 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023678 if (!ends_excmd(*skipwhite(p)))
23679 {
23680 vim_free(name);
23681 EMSG(_(e_trailing));
23682 return;
23683 }
23684 eap->nextcmd = check_nextcmd(p);
23685 if (eap->nextcmd != NULL)
23686 *p = NUL;
23687
23688 if (!eap->skip)
23689 fp = find_func(name);
23690 vim_free(name);
23691
23692 if (!eap->skip)
23693 {
23694 if (fp == NULL)
23695 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023696 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023697 return;
23698 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023699 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023700 {
23701 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23702 return;
23703 }
23704
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023705 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023706 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023707 /* Delete the dict item that refers to the function, it will
23708 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023709 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023711 else
23712 func_free(fp);
23713 }
23714}
23715
23716/*
23717 * Free a function and remove it from the list of functions.
23718 */
23719 static void
23720func_free(fp)
23721 ufunc_T *fp;
23722{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023723 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023724
23725 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023726 ga_clear_strings(&(fp->uf_args));
23727 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023728#ifdef FEAT_PROFILE
23729 vim_free(fp->uf_tml_count);
23730 vim_free(fp->uf_tml_total);
23731 vim_free(fp->uf_tml_self);
23732#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023733
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023734 /* remove the function from the function hashtable */
23735 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23736 if (HASHITEM_EMPTY(hi))
23737 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023738 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023739 hash_remove(&func_hashtab, hi);
23740
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023741 vim_free(fp);
23742}
23743
23744/*
23745 * Unreference a Function: decrement the reference count and free it when it
23746 * becomes zero. Only for numbered functions.
23747 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023748 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023749func_unref(name)
23750 char_u *name;
23751{
23752 ufunc_T *fp;
23753
23754 if (name != NULL && isdigit(*name))
23755 {
23756 fp = find_func(name);
23757 if (fp == NULL)
23758 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023759 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023760 {
23761 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023762 * when "uf_calls" becomes zero. */
23763 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023764 func_free(fp);
23765 }
23766 }
23767}
23768
23769/*
23770 * Count a reference to a Function.
23771 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023772 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023773func_ref(name)
23774 char_u *name;
23775{
23776 ufunc_T *fp;
23777
23778 if (name != NULL && isdigit(*name))
23779 {
23780 fp = find_func(name);
23781 if (fp == NULL)
23782 EMSG2(_(e_intern2), "func_ref()");
23783 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023784 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 }
23786}
23787
23788/*
23789 * Call a user function.
23790 */
23791 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023792call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793 ufunc_T *fp; /* pointer to function */
23794 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023795 typval_T *argvars; /* arguments */
23796 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023797 linenr_T firstline; /* first line of range */
23798 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023799 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800{
Bram Moolenaar33570922005-01-25 22:26:29 +000023801 char_u *save_sourcing_name;
23802 linenr_T save_sourcing_lnum;
23803 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023804 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023805 int save_did_emsg;
23806 static int depth = 0;
23807 dictitem_T *v;
23808 int fixvar_idx = 0; /* index in fixvar[] */
23809 int i;
23810 int ai;
23811 char_u numbuf[NUMBUFLEN];
23812 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023813#ifdef FEAT_PROFILE
23814 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023815 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817
23818 /* If depth of calling is getting too high, don't execute the function */
23819 if (depth >= p_mfd)
23820 {
23821 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023822 rettv->v_type = VAR_NUMBER;
23823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824 return;
23825 }
23826 ++depth;
23827
23828 line_breakcheck(); /* check for CTRL-C hit */
23829
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023830 fc = (funccall_T *)alloc(sizeof(funccall_T));
23831 fc->caller = current_funccal;
23832 current_funccal = fc;
23833 fc->func = fp;
23834 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023835 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023836 fc->linenr = 0;
23837 fc->returned = FALSE;
23838 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023840 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23841 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842
Bram Moolenaar33570922005-01-25 22:26:29 +000023843 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023844 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023845 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23846 * each argument variable and saves a lot of time.
23847 */
23848 /*
23849 * Init l: variables.
23850 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023851 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023852 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023853 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023854 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23855 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023856 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023857 name = v->di_key;
23858 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023859 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023860 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023861 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023862 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023863 v->di_tv.vval.v_dict = selfdict;
23864 ++selfdict->dv_refcount;
23865 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023866
Bram Moolenaar33570922005-01-25 22:26:29 +000023867 /*
23868 * Init a: variables.
23869 * Set a:0 to "argcount".
23870 * Set a:000 to a list with room for the "..." arguments.
23871 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023872 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023873 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023874 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023875 /* Use "name" to avoid a warning from some compiler that checks the
23876 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023877 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023878 name = v->di_key;
23879 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023880 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023881 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023882 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023883 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023884 v->di_tv.vval.v_list = &fc->l_varlist;
23885 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23886 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23887 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023888
23889 /*
23890 * Set a:firstline to "firstline" and a:lastline to "lastline".
23891 * Set a:name to named arguments.
23892 * Set a:N to the "..." arguments.
23893 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023894 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023895 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023896 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023897 (varnumber_T)lastline);
23898 for (i = 0; i < argcount; ++i)
23899 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023900 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023901 if (ai < 0)
23902 /* named argument a:name */
23903 name = FUNCARG(fp, i);
23904 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023905 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023906 /* "..." argument a:1, a:2, etc. */
23907 sprintf((char *)numbuf, "%d", ai + 1);
23908 name = numbuf;
23909 }
23910 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23911 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023912 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023913 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23914 }
23915 else
23916 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023917 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23918 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023919 if (v == NULL)
23920 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023921 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023922 }
23923 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023924 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023925
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023926 /* Note: the values are copied directly to avoid alloc/free.
23927 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023928 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023929 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023930
23931 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23932 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023933 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23934 fc->l_listitems[ai].li_tv = argvars[i];
23935 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023936 }
23937 }
23938
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939 /* Don't redraw while executing the function. */
23940 ++RedrawingDisabled;
23941 save_sourcing_name = sourcing_name;
23942 save_sourcing_lnum = sourcing_lnum;
23943 sourcing_lnum = 1;
23944 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023945 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 if (sourcing_name != NULL)
23947 {
23948 if (save_sourcing_name != NULL
23949 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23950 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23951 else
23952 STRCPY(sourcing_name, "function ");
23953 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23954
23955 if (p_verbose >= 12)
23956 {
23957 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023958 verbose_enter_scroll();
23959
Bram Moolenaar555b2802005-05-19 21:08:39 +000023960 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 if (p_verbose >= 14)
23962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023964 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023965 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023966 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023967
23968 msg_puts((char_u *)"(");
23969 for (i = 0; i < argcount; ++i)
23970 {
23971 if (i > 0)
23972 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023973 if (argvars[i].v_type == VAR_NUMBER)
23974 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023975 else
23976 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023977 /* Do not want errors such as E724 here. */
23978 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023979 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023980 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023981 if (s != NULL)
23982 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023983 if (vim_strsize(s) > MSG_BUF_CLEN)
23984 {
23985 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23986 s = buf;
23987 }
23988 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023989 vim_free(tofree);
23990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 }
23992 }
23993 msg_puts((char_u *)")");
23994 }
23995 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023996
23997 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998 --no_wait_return;
23999 }
24000 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024001#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024002 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024003 {
24004 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24005 func_do_profile(fp);
24006 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024007 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024008 {
24009 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024010 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024011 profile_zero(&fp->uf_tm_children);
24012 }
24013 script_prof_save(&wait_start);
24014 }
24015#endif
24016
Bram Moolenaar071d4272004-06-13 20:20:40 +000024017 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024018 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 save_did_emsg = did_emsg;
24020 did_emsg = FALSE;
24021
24022 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024023 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24025
24026 --RedrawingDisabled;
24027
24028 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024029 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024031 clear_tv(rettv);
24032 rettv->v_type = VAR_NUMBER;
24033 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034 }
24035
Bram Moolenaar05159a02005-02-26 23:04:13 +000024036#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024037 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024038 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024039 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024040 profile_end(&call_start);
24041 profile_sub_wait(&wait_start, &call_start);
24042 profile_add(&fp->uf_tm_total, &call_start);
24043 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024044 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024045 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024046 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24047 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024048 }
24049 }
24050#endif
24051
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052 /* when being verbose, mention the return value */
24053 if (p_verbose >= 12)
24054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024056 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024059 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024060 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024061 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024062 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024065 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024066 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024067 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024068 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024069
Bram Moolenaar555b2802005-05-19 21:08:39 +000024070 /* The value may be very long. Skip the middle part, so that we
24071 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024072 * truncate it at the end. Don't want errors such as E724 here. */
24073 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024074 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024075 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024076 if (s != NULL)
24077 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024078 if (vim_strsize(s) > MSG_BUF_CLEN)
24079 {
24080 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24081 s = buf;
24082 }
24083 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024084 vim_free(tofree);
24085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024086 }
24087 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024088
24089 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 --no_wait_return;
24091 }
24092
24093 vim_free(sourcing_name);
24094 sourcing_name = save_sourcing_name;
24095 sourcing_lnum = save_sourcing_lnum;
24096 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024097#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024098 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024099 script_prof_restore(&wait_start);
24100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101
24102 if (p_verbose >= 12 && sourcing_name != NULL)
24103 {
24104 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024105 verbose_enter_scroll();
24106
Bram Moolenaar555b2802005-05-19 21:08:39 +000024107 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024109
24110 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 --no_wait_return;
24112 }
24113
24114 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024115 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024117
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024118 /* If the a:000 list and the l: and a: dicts are not referenced we can
24119 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024120 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24121 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24122 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24123 {
24124 free_funccal(fc, FALSE);
24125 }
24126 else
24127 {
24128 hashitem_T *hi;
24129 listitem_T *li;
24130 int todo;
24131
24132 /* "fc" is still in use. This can happen when returning "a:000" or
24133 * assigning "l:" to a global variable.
24134 * Link "fc" in the list for garbage collection later. */
24135 fc->caller = previous_funccal;
24136 previous_funccal = fc;
24137
24138 /* Make a copy of the a: variables, since we didn't do that above. */
24139 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24140 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24141 {
24142 if (!HASHITEM_EMPTY(hi))
24143 {
24144 --todo;
24145 v = HI2DI(hi);
24146 copy_tv(&v->di_tv, &v->di_tv);
24147 }
24148 }
24149
24150 /* Make a copy of the a:000 items, since we didn't do that above. */
24151 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24152 copy_tv(&li->li_tv, &li->li_tv);
24153 }
24154}
24155
24156/*
24157 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024158 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024159 */
24160 static int
24161can_free_funccal(fc, copyID)
24162 funccall_T *fc;
24163 int copyID;
24164{
24165 return (fc->l_varlist.lv_copyID != copyID
24166 && fc->l_vars.dv_copyID != copyID
24167 && fc->l_avars.dv_copyID != copyID);
24168}
24169
24170/*
24171 * Free "fc" and what it contains.
24172 */
24173 static void
24174free_funccal(fc, free_val)
24175 funccall_T *fc;
24176 int free_val; /* a: vars were allocated */
24177{
24178 listitem_T *li;
24179
24180 /* The a: variables typevals may not have been allocated, only free the
24181 * allocated variables. */
24182 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24183
24184 /* free all l: variables */
24185 vars_clear(&fc->l_vars.dv_hashtab);
24186
24187 /* Free the a:000 variables if they were allocated. */
24188 if (free_val)
24189 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24190 clear_tv(&li->li_tv);
24191
24192 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024193}
24194
24195/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024196 * Add a number variable "name" to dict "dp" with value "nr".
24197 */
24198 static void
24199add_nr_var(dp, v, name, nr)
24200 dict_T *dp;
24201 dictitem_T *v;
24202 char *name;
24203 varnumber_T nr;
24204{
24205 STRCPY(v->di_key, name);
24206 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24207 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24208 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024209 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024210 v->di_tv.vval.v_number = nr;
24211}
24212
24213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 * ":return [expr]"
24215 */
24216 void
24217ex_return(eap)
24218 exarg_T *eap;
24219{
24220 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024221 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222 int returning = FALSE;
24223
24224 if (current_funccal == NULL)
24225 {
24226 EMSG(_("E133: :return not inside a function"));
24227 return;
24228 }
24229
24230 if (eap->skip)
24231 ++emsg_skip;
24232
24233 eap->nextcmd = NULL;
24234 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024235 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 {
24237 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024238 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024240 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 }
24242 /* It's safer to return also on error. */
24243 else if (!eap->skip)
24244 {
24245 /*
24246 * Return unless the expression evaluation has been cancelled due to an
24247 * aborting error, an interrupt, or an exception.
24248 */
24249 if (!aborting())
24250 returning = do_return(eap, FALSE, TRUE, NULL);
24251 }
24252
24253 /* When skipping or the return gets pending, advance to the next command
24254 * in this line (!returning). Otherwise, ignore the rest of the line.
24255 * Following lines will be ignored by get_func_line(). */
24256 if (returning)
24257 eap->nextcmd = NULL;
24258 else if (eap->nextcmd == NULL) /* no argument */
24259 eap->nextcmd = check_nextcmd(arg);
24260
24261 if (eap->skip)
24262 --emsg_skip;
24263}
24264
24265/*
24266 * Return from a function. Possibly makes the return pending. Also called
24267 * for a pending return at the ":endtry" or after returning from an extra
24268 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024269 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024270 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271 * FALSE when the return gets pending.
24272 */
24273 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024274do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275 exarg_T *eap;
24276 int reanimate;
24277 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024278 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279{
24280 int idx;
24281 struct condstack *cstack = eap->cstack;
24282
24283 if (reanimate)
24284 /* Undo the return. */
24285 current_funccal->returned = FALSE;
24286
24287 /*
24288 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24289 * not in its finally clause (which then is to be executed next) is found.
24290 * In this case, make the ":return" pending for execution at the ":endtry".
24291 * Otherwise, return normally.
24292 */
24293 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24294 if (idx >= 0)
24295 {
24296 cstack->cs_pending[idx] = CSTP_RETURN;
24297
24298 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024299 /* A pending return again gets pending. "rettv" points to an
24300 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024302 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 else
24304 {
24305 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024306 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024308 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024310 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311 {
24312 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024313 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024314 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 else
24316 EMSG(_(e_outofmem));
24317 }
24318 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024319 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024320
24321 if (reanimate)
24322 {
24323 /* The pending return value could be overwritten by a ":return"
24324 * without argument in a finally clause; reset the default
24325 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024326 current_funccal->rettv->v_type = VAR_NUMBER;
24327 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 }
24329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024330 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 }
24332 else
24333 {
24334 current_funccal->returned = TRUE;
24335
24336 /* If the return is carried out now, store the return value. For
24337 * a return immediately after reanimation, the value is already
24338 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024339 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024341 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024342 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024343 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024344 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024345 }
24346 }
24347
24348 return idx < 0;
24349}
24350
24351/*
24352 * Free the variable with a pending return value.
24353 */
24354 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024355discard_pending_return(rettv)
24356 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357{
Bram Moolenaar33570922005-01-25 22:26:29 +000024358 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359}
24360
24361/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024362 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363 * is an allocated string. Used by report_pending() for verbose messages.
24364 */
24365 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024366get_return_cmd(rettv)
24367 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024368{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024369 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024370 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024371 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024373 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024374 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024375 if (s == NULL)
24376 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024377
24378 STRCPY(IObuff, ":return ");
24379 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24380 if (STRLEN(s) + 8 >= IOSIZE)
24381 STRCPY(IObuff + IOSIZE - 4, "...");
24382 vim_free(tofree);
24383 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024384}
24385
24386/*
24387 * Get next function line.
24388 * Called by do_cmdline() to get the next line.
24389 * Returns allocated string, or NULL for end of function.
24390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391 char_u *
24392get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024393 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024394 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024395 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024396{
Bram Moolenaar33570922005-01-25 22:26:29 +000024397 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024398 ufunc_T *fp = fcp->func;
24399 char_u *retval;
24400 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401
24402 /* If breakpoints have been added/deleted need to check for it. */
24403 if (fcp->dbg_tick != debug_tick)
24404 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024405 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 sourcing_lnum);
24407 fcp->dbg_tick = debug_tick;
24408 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024409#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024410 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024411 func_line_end(cookie);
24412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413
Bram Moolenaar05159a02005-02-26 23:04:13 +000024414 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024415 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24416 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417 retval = NULL;
24418 else
24419 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024420 /* Skip NULL lines (continuation lines). */
24421 while (fcp->linenr < gap->ga_len
24422 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24423 ++fcp->linenr;
24424 if (fcp->linenr >= gap->ga_len)
24425 retval = NULL;
24426 else
24427 {
24428 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24429 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024430#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024431 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024432 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024433#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024435 }
24436
24437 /* Did we encounter a breakpoint? */
24438 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24439 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024440 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024441 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024442 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443 sourcing_lnum);
24444 fcp->dbg_tick = debug_tick;
24445 }
24446
24447 return retval;
24448}
24449
Bram Moolenaar05159a02005-02-26 23:04:13 +000024450#if defined(FEAT_PROFILE) || defined(PROTO)
24451/*
24452 * Called when starting to read a function line.
24453 * "sourcing_lnum" must be correct!
24454 * When skipping lines it may not actually be executed, but we won't find out
24455 * until later and we need to store the time now.
24456 */
24457 void
24458func_line_start(cookie)
24459 void *cookie;
24460{
24461 funccall_T *fcp = (funccall_T *)cookie;
24462 ufunc_T *fp = fcp->func;
24463
24464 if (fp->uf_profiling && sourcing_lnum >= 1
24465 && sourcing_lnum <= fp->uf_lines.ga_len)
24466 {
24467 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024468 /* Skip continuation lines. */
24469 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24470 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024471 fp->uf_tml_execed = FALSE;
24472 profile_start(&fp->uf_tml_start);
24473 profile_zero(&fp->uf_tml_children);
24474 profile_get_wait(&fp->uf_tml_wait);
24475 }
24476}
24477
24478/*
24479 * Called when actually executing a function line.
24480 */
24481 void
24482func_line_exec(cookie)
24483 void *cookie;
24484{
24485 funccall_T *fcp = (funccall_T *)cookie;
24486 ufunc_T *fp = fcp->func;
24487
24488 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24489 fp->uf_tml_execed = TRUE;
24490}
24491
24492/*
24493 * Called when done with a function line.
24494 */
24495 void
24496func_line_end(cookie)
24497 void *cookie;
24498{
24499 funccall_T *fcp = (funccall_T *)cookie;
24500 ufunc_T *fp = fcp->func;
24501
24502 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24503 {
24504 if (fp->uf_tml_execed)
24505 {
24506 ++fp->uf_tml_count[fp->uf_tml_idx];
24507 profile_end(&fp->uf_tml_start);
24508 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024509 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024510 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24511 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024512 }
24513 fp->uf_tml_idx = -1;
24514 }
24515}
24516#endif
24517
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518/*
24519 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024520 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521 */
24522 int
24523func_has_ended(cookie)
24524 void *cookie;
24525{
Bram Moolenaar33570922005-01-25 22:26:29 +000024526 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527
24528 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24529 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024530 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 || fcp->returned);
24532}
24533
24534/*
24535 * return TRUE if cookie indicates a function which "abort"s on errors.
24536 */
24537 int
24538func_has_abort(cookie)
24539 void *cookie;
24540{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024541 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542}
24543
24544#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24545typedef enum
24546{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024547 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24548 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24549 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550} var_flavour_T;
24551
24552static var_flavour_T var_flavour __ARGS((char_u *varname));
24553
24554 static var_flavour_T
24555var_flavour(varname)
24556 char_u *varname;
24557{
24558 char_u *p = varname;
24559
24560 if (ASCII_ISUPPER(*p))
24561 {
24562 while (*(++p))
24563 if (ASCII_ISLOWER(*p))
24564 return VAR_FLAVOUR_SESSION;
24565 return VAR_FLAVOUR_VIMINFO;
24566 }
24567 else
24568 return VAR_FLAVOUR_DEFAULT;
24569}
24570#endif
24571
24572#if defined(FEAT_VIMINFO) || defined(PROTO)
24573/*
24574 * Restore global vars that start with a capital from the viminfo file
24575 */
24576 int
24577read_viminfo_varlist(virp, writing)
24578 vir_T *virp;
24579 int writing;
24580{
24581 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024582 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024583 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584
24585 if (!writing && (find_viminfo_parameter('!') != NULL))
24586 {
24587 tab = vim_strchr(virp->vir_line + 1, '\t');
24588 if (tab != NULL)
24589 {
24590 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024591 switch (*tab)
24592 {
24593 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024594#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024595 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024596#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024597 case 'D': type = VAR_DICT; break;
24598 case 'L': type = VAR_LIST; break;
24599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600
24601 tab = vim_strchr(tab, '\t');
24602 if (tab != NULL)
24603 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024604 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024605 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024606 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024608#ifdef FEAT_FLOAT
24609 else if (type == VAR_FLOAT)
24610 (void)string2float(tab + 1, &tv.vval.v_float);
24611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024613 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024614 if (type == VAR_DICT || type == VAR_LIST)
24615 {
24616 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24617
24618 if (etv == NULL)
24619 /* Failed to parse back the dict or list, use it as a
24620 * string. */
24621 tv.v_type = VAR_STRING;
24622 else
24623 {
24624 vim_free(tv.vval.v_string);
24625 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024626 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024627 }
24628 }
24629
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024630 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024631
24632 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024633 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024634 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24635 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 }
24637 }
24638 }
24639
24640 return viminfo_readline(virp);
24641}
24642
24643/*
24644 * Write global vars that start with a capital to the viminfo file
24645 */
24646 void
24647write_viminfo_varlist(fp)
24648 FILE *fp;
24649{
Bram Moolenaar33570922005-01-25 22:26:29 +000024650 hashitem_T *hi;
24651 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024652 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024653 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024654 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024655 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024656 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657
24658 if (find_viminfo_parameter('!') == NULL)
24659 return;
24660
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024661 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024662
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024663 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024664 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024666 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024668 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024669 this_var = HI2DI(hi);
24670 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024671 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024672 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024673 {
24674 case VAR_STRING: s = "STR"; break;
24675 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024676#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024677 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024678#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024679 case VAR_DICT: s = "DIC"; break;
24680 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024681 default: continue;
24682 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024683 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024684 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024685 if (p != NULL)
24686 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024687 vim_free(tofree);
24688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024689 }
24690 }
24691}
24692#endif
24693
24694#if defined(FEAT_SESSION) || defined(PROTO)
24695 int
24696store_session_globals(fd)
24697 FILE *fd;
24698{
Bram Moolenaar33570922005-01-25 22:26:29 +000024699 hashitem_T *hi;
24700 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024701 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702 char_u *p, *t;
24703
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024704 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024705 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024707 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024709 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024710 this_var = HI2DI(hi);
24711 if ((this_var->di_tv.v_type == VAR_NUMBER
24712 || this_var->di_tv.v_type == VAR_STRING)
24713 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024714 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024715 /* Escape special characters with a backslash. Turn a LF and
24716 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024717 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024718 (char_u *)"\\\"\n\r");
24719 if (p == NULL) /* out of memory */
24720 break;
24721 for (t = p; *t != NUL; ++t)
24722 if (*t == '\n')
24723 *t = 'n';
24724 else if (*t == '\r')
24725 *t = 'r';
24726 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024727 this_var->di_key,
24728 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24729 : ' ',
24730 p,
24731 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24732 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024733 || put_eol(fd) == FAIL)
24734 {
24735 vim_free(p);
24736 return FAIL;
24737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024738 vim_free(p);
24739 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024740#ifdef FEAT_FLOAT
24741 else if (this_var->di_tv.v_type == VAR_FLOAT
24742 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24743 {
24744 float_T f = this_var->di_tv.vval.v_float;
24745 int sign = ' ';
24746
24747 if (f < 0)
24748 {
24749 f = -f;
24750 sign = '-';
24751 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024752 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024753 this_var->di_key, sign, f) < 0)
24754 || put_eol(fd) == FAIL)
24755 return FAIL;
24756 }
24757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758 }
24759 }
24760 return OK;
24761}
24762#endif
24763
Bram Moolenaar661b1822005-07-28 22:36:45 +000024764/*
24765 * Display script name where an item was last set.
24766 * Should only be invoked when 'verbose' is non-zero.
24767 */
24768 void
24769last_set_msg(scriptID)
24770 scid_T scriptID;
24771{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024772 char_u *p;
24773
Bram Moolenaar661b1822005-07-28 22:36:45 +000024774 if (scriptID != 0)
24775 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024776 p = home_replace_save(NULL, get_scriptname(scriptID));
24777 if (p != NULL)
24778 {
24779 verbose_enter();
24780 MSG_PUTS(_("\n\tLast set from "));
24781 MSG_PUTS(p);
24782 vim_free(p);
24783 verbose_leave();
24784 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024785 }
24786}
24787
Bram Moolenaard812df62008-11-09 12:46:09 +000024788/*
24789 * List v:oldfiles in a nice way.
24790 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024791 void
24792ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024793 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024794{
24795 list_T *l = vimvars[VV_OLDFILES].vv_list;
24796 listitem_T *li;
24797 int nr = 0;
24798
24799 if (l == NULL)
24800 msg((char_u *)_("No old files"));
24801 else
24802 {
24803 msg_start();
24804 msg_scroll = TRUE;
24805 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24806 {
24807 msg_outnum((long)++nr);
24808 MSG_PUTS(": ");
24809 msg_outtrans(get_tv_string(&li->li_tv));
24810 msg_putchar('\n');
24811 out_flush(); /* output one line at a time */
24812 ui_breakcheck();
24813 }
24814 /* Assume "got_int" was set to truncate the listing. */
24815 got_int = FALSE;
24816
24817#ifdef FEAT_BROWSE_CMD
24818 if (cmdmod.browse)
24819 {
24820 quit_more = FALSE;
24821 nr = prompt_for_number(FALSE);
24822 msg_starthere();
24823 if (nr > 0)
24824 {
24825 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24826 (long)nr);
24827
24828 if (p != NULL)
24829 {
24830 p = expand_env_save(p);
24831 eap->arg = p;
24832 eap->cmdidx = CMD_edit;
24833 cmdmod.browse = FALSE;
24834 do_exedit(eap, NULL);
24835 vim_free(p);
24836 }
24837 }
24838 }
24839#endif
24840 }
24841}
24842
Bram Moolenaar53744302015-07-17 17:38:22 +020024843/* reset v:option_new, v:option_old and v:option_type */
24844 void
24845reset_v_option_vars()
24846{
24847 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24848 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24849 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24850}
24851
24852
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853#endif /* FEAT_EVAL */
24854
Bram Moolenaar071d4272004-06-13 20:20:40 +000024855
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024856#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857
24858#ifdef WIN3264
24859/*
24860 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24861 */
24862static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24863static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24864static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24865
24866/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024867 * Get the short path (8.3) for the filename in "fnamep".
24868 * Only works for a valid file name.
24869 * When the path gets longer "fnamep" is changed and the allocated buffer
24870 * is put in "bufp".
24871 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24872 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873 */
24874 static int
24875get_short_pathname(fnamep, bufp, fnamelen)
24876 char_u **fnamep;
24877 char_u **bufp;
24878 int *fnamelen;
24879{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024880 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024881 char_u *newbuf;
24882
24883 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884 l = GetShortPathName(*fnamep, *fnamep, len);
24885 if (l > len - 1)
24886 {
24887 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024888 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889 newbuf = vim_strnsave(*fnamep, l);
24890 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024891 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892
24893 vim_free(*bufp);
24894 *fnamep = *bufp = newbuf;
24895
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024896 /* Really should always succeed, as the buffer is big enough. */
24897 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024898 }
24899
24900 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024901 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902}
24903
24904/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024905 * Get the short path (8.3) for the filename in "fname". The converted
24906 * path is returned in "bufp".
24907 *
24908 * Some of the directories specified in "fname" may not exist. This function
24909 * will shorten the existing directories at the beginning of the path and then
24910 * append the remaining non-existing path.
24911 *
24912 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024913 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024914 * bufp - Pointer to an allocated buffer for the filename.
24915 * fnamelen - Length of the filename pointed to by fname
24916 *
24917 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918 */
24919 static int
24920shortpath_for_invalid_fname(fname, bufp, fnamelen)
24921 char_u **fname;
24922 char_u **bufp;
24923 int *fnamelen;
24924{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024925 char_u *short_fname, *save_fname, *pbuf_unused;
24926 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024928 int old_len, len;
24929 int new_len, sfx_len;
24930 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931
24932 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024933 old_len = *fnamelen;
24934 save_fname = vim_strnsave(*fname, old_len);
24935 pbuf_unused = NULL;
24936 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024938 endp = save_fname + old_len - 1; /* Find the end of the copy */
24939 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024941 /*
24942 * Try shortening the supplied path till it succeeds by removing one
24943 * directory at a time from the tail of the path.
24944 */
24945 len = 0;
24946 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024947 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024948 /* go back one path-separator */
24949 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24950 --endp;
24951 if (endp <= save_fname)
24952 break; /* processed the complete path */
24953
24954 /*
24955 * Replace the path separator with a NUL and try to shorten the
24956 * resulting path.
24957 */
24958 ch = *endp;
24959 *endp = 0;
24960 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024961 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024962 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24963 {
24964 retval = FAIL;
24965 goto theend;
24966 }
24967 *endp = ch; /* preserve the string */
24968
24969 if (len > 0)
24970 break; /* successfully shortened the path */
24971
24972 /* failed to shorten the path. Skip the path separator */
24973 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024974 }
24975
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024976 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024977 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024978 /*
24979 * Succeeded in shortening the path. Now concatenate the shortened
24980 * path with the remaining path at the tail.
24981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024982
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024983 /* Compute the length of the new path. */
24984 sfx_len = (int)(save_endp - endp) + 1;
24985 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024986
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024987 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024989 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024991 /* There is not enough space in the currently allocated string,
24992 * copy it to a buffer big enough. */
24993 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024994 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024995 {
24996 retval = FAIL;
24997 goto theend;
24998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024999 }
25000 else
25001 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025002 /* Transfer short_fname to the main buffer (it's big enough),
25003 * unless get_short_pathname() did its work in-place. */
25004 *fname = *bufp = save_fname;
25005 if (short_fname != save_fname)
25006 vim_strncpy(save_fname, short_fname, len);
25007 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025008 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025009
25010 /* concat the not-shortened part of the path */
25011 vim_strncpy(*fname + len, endp, sfx_len);
25012 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025014
25015theend:
25016 vim_free(pbuf_unused);
25017 vim_free(save_fname);
25018
25019 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025020}
25021
25022/*
25023 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025024 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025 */
25026 static int
25027shortpath_for_partial(fnamep, bufp, fnamelen)
25028 char_u **fnamep;
25029 char_u **bufp;
25030 int *fnamelen;
25031{
25032 int sepcount, len, tflen;
25033 char_u *p;
25034 char_u *pbuf, *tfname;
25035 int hasTilde;
25036
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025037 /* Count up the path separators from the RHS.. so we know which part
25038 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025040 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041 if (vim_ispathsep(*p))
25042 ++sepcount;
25043
25044 /* Need full path first (use expand_env() to remove a "~/") */
25045 hasTilde = (**fnamep == '~');
25046 if (hasTilde)
25047 pbuf = tfname = expand_env_save(*fnamep);
25048 else
25049 pbuf = tfname = FullName_save(*fnamep, FALSE);
25050
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025051 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025053 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25054 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025055
25056 if (len == 0)
25057 {
25058 /* Don't have a valid filename, so shorten the rest of the
25059 * path if we can. This CAN give us invalid 8.3 filenames, but
25060 * there's not a lot of point in guessing what it might be.
25061 */
25062 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025063 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25064 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065 }
25066
25067 /* Count the paths backward to find the beginning of the desired string. */
25068 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025069 {
25070#ifdef FEAT_MBYTE
25071 if (has_mbyte)
25072 p -= mb_head_off(tfname, p);
25073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074 if (vim_ispathsep(*p))
25075 {
25076 if (sepcount == 0 || (hasTilde && sepcount == 1))
25077 break;
25078 else
25079 sepcount --;
25080 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082 if (hasTilde)
25083 {
25084 --p;
25085 if (p >= tfname)
25086 *p = '~';
25087 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089 }
25090 else
25091 ++p;
25092
25093 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25094 vim_free(*bufp);
25095 *fnamelen = (int)STRLEN(p);
25096 *bufp = pbuf;
25097 *fnamep = p;
25098
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025099 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025100}
25101#endif /* WIN3264 */
25102
25103/*
25104 * Adjust a filename, according to a string of modifiers.
25105 * *fnamep must be NUL terminated when called. When returning, the length is
25106 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025107 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025108 * When there is an error, *fnamep is set to NULL.
25109 */
25110 int
25111modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25112 char_u *src; /* string with modifiers */
25113 int *usedlen; /* characters after src that are used */
25114 char_u **fnamep; /* file name so far */
25115 char_u **bufp; /* buffer for allocated file name or NULL */
25116 int *fnamelen; /* length of fnamep */
25117{
25118 int valid = 0;
25119 char_u *tail;
25120 char_u *s, *p, *pbuf;
25121 char_u dirname[MAXPATHL];
25122 int c;
25123 int has_fullname = 0;
25124#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025125 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025126 int has_shortname = 0;
25127#endif
25128
25129repeat:
25130 /* ":p" - full path/file_name */
25131 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25132 {
25133 has_fullname = 1;
25134
25135 valid |= VALID_PATH;
25136 *usedlen += 2;
25137
25138 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25139 if ((*fnamep)[0] == '~'
25140#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25141 && ((*fnamep)[1] == '/'
25142# ifdef BACKSLASH_IN_FILENAME
25143 || (*fnamep)[1] == '\\'
25144# endif
25145 || (*fnamep)[1] == NUL)
25146
25147#endif
25148 )
25149 {
25150 *fnamep = expand_env_save(*fnamep);
25151 vim_free(*bufp); /* free any allocated file name */
25152 *bufp = *fnamep;
25153 if (*fnamep == NULL)
25154 return -1;
25155 }
25156
25157 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025158 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159 {
25160 if (vim_ispathsep(*p)
25161 && p[1] == '.'
25162 && (p[2] == NUL
25163 || vim_ispathsep(p[2])
25164 || (p[2] == '.'
25165 && (p[3] == NUL || vim_ispathsep(p[3])))))
25166 break;
25167 }
25168
25169 /* FullName_save() is slow, don't use it when not needed. */
25170 if (*p != NUL || !vim_isAbsName(*fnamep))
25171 {
25172 *fnamep = FullName_save(*fnamep, *p != NUL);
25173 vim_free(*bufp); /* free any allocated file name */
25174 *bufp = *fnamep;
25175 if (*fnamep == NULL)
25176 return -1;
25177 }
25178
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025179#ifdef WIN3264
25180# if _WIN32_WINNT >= 0x0500
25181 if (vim_strchr(*fnamep, '~') != NULL)
25182 {
25183 /* Expand 8.3 filename to full path. Needed to make sure the same
25184 * file does not have two different names.
25185 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25186 p = alloc(_MAX_PATH + 1);
25187 if (p != NULL)
25188 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025189 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025190 {
25191 vim_free(*bufp);
25192 *bufp = *fnamep = p;
25193 }
25194 else
25195 vim_free(p);
25196 }
25197 }
25198# endif
25199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025200 /* Append a path separator to a directory. */
25201 if (mch_isdir(*fnamep))
25202 {
25203 /* Make room for one or two extra characters. */
25204 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25205 vim_free(*bufp); /* free any allocated file name */
25206 *bufp = *fnamep;
25207 if (*fnamep == NULL)
25208 return -1;
25209 add_pathsep(*fnamep);
25210 }
25211 }
25212
25213 /* ":." - path relative to the current directory */
25214 /* ":~" - path relative to the home directory */
25215 /* ":8" - shortname path - postponed till after */
25216 while (src[*usedlen] == ':'
25217 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25218 {
25219 *usedlen += 2;
25220 if (c == '8')
25221 {
25222#ifdef WIN3264
25223 has_shortname = 1; /* Postpone this. */
25224#endif
25225 continue;
25226 }
25227 pbuf = NULL;
25228 /* Need full path first (use expand_env() to remove a "~/") */
25229 if (!has_fullname)
25230 {
25231 if (c == '.' && **fnamep == '~')
25232 p = pbuf = expand_env_save(*fnamep);
25233 else
25234 p = pbuf = FullName_save(*fnamep, FALSE);
25235 }
25236 else
25237 p = *fnamep;
25238
25239 has_fullname = 0;
25240
25241 if (p != NULL)
25242 {
25243 if (c == '.')
25244 {
25245 mch_dirname(dirname, MAXPATHL);
25246 s = shorten_fname(p, dirname);
25247 if (s != NULL)
25248 {
25249 *fnamep = s;
25250 if (pbuf != NULL)
25251 {
25252 vim_free(*bufp); /* free any allocated file name */
25253 *bufp = pbuf;
25254 pbuf = NULL;
25255 }
25256 }
25257 }
25258 else
25259 {
25260 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25261 /* Only replace it when it starts with '~' */
25262 if (*dirname == '~')
25263 {
25264 s = vim_strsave(dirname);
25265 if (s != NULL)
25266 {
25267 *fnamep = s;
25268 vim_free(*bufp);
25269 *bufp = s;
25270 }
25271 }
25272 }
25273 vim_free(pbuf);
25274 }
25275 }
25276
25277 tail = gettail(*fnamep);
25278 *fnamelen = (int)STRLEN(*fnamep);
25279
25280 /* ":h" - head, remove "/file_name", can be repeated */
25281 /* Don't remove the first "/" or "c:\" */
25282 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25283 {
25284 valid |= VALID_HEAD;
25285 *usedlen += 2;
25286 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025287 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025288 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289 *fnamelen = (int)(tail - *fnamep);
25290#ifdef VMS
25291 if (*fnamelen > 0)
25292 *fnamelen += 1; /* the path separator is part of the path */
25293#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025294 if (*fnamelen == 0)
25295 {
25296 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25297 p = vim_strsave((char_u *)".");
25298 if (p == NULL)
25299 return -1;
25300 vim_free(*bufp);
25301 *bufp = *fnamep = tail = p;
25302 *fnamelen = 1;
25303 }
25304 else
25305 {
25306 while (tail > s && !after_pathsep(s, tail))
25307 mb_ptr_back(*fnamep, tail);
25308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025309 }
25310
25311 /* ":8" - shortname */
25312 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25313 {
25314 *usedlen += 2;
25315#ifdef WIN3264
25316 has_shortname = 1;
25317#endif
25318 }
25319
25320#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025321 /*
25322 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323 */
25324 if (has_shortname)
25325 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025326 /* Copy the string if it is shortened by :h and when it wasn't copied
25327 * yet, because we are going to change it in place. Avoids changing
25328 * the buffer name for "%:8". */
25329 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025330 {
25331 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025332 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025333 return -1;
25334 vim_free(*bufp);
25335 *bufp = *fnamep = p;
25336 }
25337
25338 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025339 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025340 if (!has_fullname && !vim_isAbsName(*fnamep))
25341 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025342 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 return -1;
25344 }
25345 else
25346 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025347 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025348
Bram Moolenaardc935552011-08-17 15:23:23 +020025349 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025350 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025351 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352 return -1;
25353
25354 if (l == 0)
25355 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025356 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025358 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359 return -1;
25360 }
25361 *fnamelen = l;
25362 }
25363 }
25364#endif /* WIN3264 */
25365
25366 /* ":t" - tail, just the basename */
25367 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25368 {
25369 *usedlen += 2;
25370 *fnamelen -= (int)(tail - *fnamep);
25371 *fnamep = tail;
25372 }
25373
25374 /* ":e" - extension, can be repeated */
25375 /* ":r" - root, without extension, can be repeated */
25376 while (src[*usedlen] == ':'
25377 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25378 {
25379 /* find a '.' in the tail:
25380 * - for second :e: before the current fname
25381 * - otherwise: The last '.'
25382 */
25383 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25384 s = *fnamep - 2;
25385 else
25386 s = *fnamep + *fnamelen - 1;
25387 for ( ; s > tail; --s)
25388 if (s[0] == '.')
25389 break;
25390 if (src[*usedlen + 1] == 'e') /* :e */
25391 {
25392 if (s > tail)
25393 {
25394 *fnamelen += (int)(*fnamep - (s + 1));
25395 *fnamep = s + 1;
25396#ifdef VMS
25397 /* cut version from the extension */
25398 s = *fnamep + *fnamelen - 1;
25399 for ( ; s > *fnamep; --s)
25400 if (s[0] == ';')
25401 break;
25402 if (s > *fnamep)
25403 *fnamelen = s - *fnamep;
25404#endif
25405 }
25406 else if (*fnamep <= tail)
25407 *fnamelen = 0;
25408 }
25409 else /* :r */
25410 {
25411 if (s > tail) /* remove one extension */
25412 *fnamelen = (int)(s - *fnamep);
25413 }
25414 *usedlen += 2;
25415 }
25416
25417 /* ":s?pat?foo?" - substitute */
25418 /* ":gs?pat?foo?" - global substitute */
25419 if (src[*usedlen] == ':'
25420 && (src[*usedlen + 1] == 's'
25421 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25422 {
25423 char_u *str;
25424 char_u *pat;
25425 char_u *sub;
25426 int sep;
25427 char_u *flags;
25428 int didit = FALSE;
25429
25430 flags = (char_u *)"";
25431 s = src + *usedlen + 2;
25432 if (src[*usedlen + 1] == 'g')
25433 {
25434 flags = (char_u *)"g";
25435 ++s;
25436 }
25437
25438 sep = *s++;
25439 if (sep)
25440 {
25441 /* find end of pattern */
25442 p = vim_strchr(s, sep);
25443 if (p != NULL)
25444 {
25445 pat = vim_strnsave(s, (int)(p - s));
25446 if (pat != NULL)
25447 {
25448 s = p + 1;
25449 /* find end of substitution */
25450 p = vim_strchr(s, sep);
25451 if (p != NULL)
25452 {
25453 sub = vim_strnsave(s, (int)(p - s));
25454 str = vim_strnsave(*fnamep, *fnamelen);
25455 if (sub != NULL && str != NULL)
25456 {
25457 *usedlen = (int)(p + 1 - src);
25458 s = do_string_sub(str, pat, sub, flags);
25459 if (s != NULL)
25460 {
25461 *fnamep = s;
25462 *fnamelen = (int)STRLEN(s);
25463 vim_free(*bufp);
25464 *bufp = s;
25465 didit = TRUE;
25466 }
25467 }
25468 vim_free(sub);
25469 vim_free(str);
25470 }
25471 vim_free(pat);
25472 }
25473 }
25474 /* after using ":s", repeat all the modifiers */
25475 if (didit)
25476 goto repeat;
25477 }
25478 }
25479
Bram Moolenaar26df0922014-02-23 23:39:13 +010025480 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25481 {
25482 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25483 if (p == NULL)
25484 return -1;
25485 vim_free(*bufp);
25486 *bufp = *fnamep = p;
25487 *fnamelen = (int)STRLEN(p);
25488 *usedlen += 2;
25489 }
25490
Bram Moolenaar071d4272004-06-13 20:20:40 +000025491 return valid;
25492}
25493
25494/*
25495 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25496 * "flags" can be "g" to do a global substitute.
25497 * Returns an allocated string, NULL for error.
25498 */
25499 char_u *
25500do_string_sub(str, pat, sub, flags)
25501 char_u *str;
25502 char_u *pat;
25503 char_u *sub;
25504 char_u *flags;
25505{
25506 int sublen;
25507 regmatch_T regmatch;
25508 int i;
25509 int do_all;
25510 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025511 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512 garray_T ga;
25513 char_u *ret;
25514 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025515 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516
25517 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25518 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025519 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520
25521 ga_init2(&ga, 1, 200);
25522
25523 do_all = (flags[0] == 'g');
25524
25525 regmatch.rm_ic = p_ic;
25526 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25527 if (regmatch.regprog != NULL)
25528 {
25529 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025530 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025531 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25532 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025533 /* Skip empty match except for first match. */
25534 if (regmatch.startp[0] == regmatch.endp[0])
25535 {
25536 if (zero_width == regmatch.startp[0])
25537 {
25538 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025539 i = MB_PTR2LEN(tail);
25540 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25541 (size_t)i);
25542 ga.ga_len += i;
25543 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025544 continue;
25545 }
25546 zero_width = regmatch.startp[0];
25547 }
25548
Bram Moolenaar071d4272004-06-13 20:20:40 +000025549 /*
25550 * Get some space for a temporary buffer to do the substitution
25551 * into. It will contain:
25552 * - The text up to where the match is.
25553 * - The substituted text.
25554 * - The text after the match.
25555 */
25556 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025557 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025558 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25559 {
25560 ga_clear(&ga);
25561 break;
25562 }
25563
25564 /* copy the text up to where the match is */
25565 i = (int)(regmatch.startp[0] - tail);
25566 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25567 /* add the substituted text */
25568 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25569 + ga.ga_len + i, TRUE, TRUE, FALSE);
25570 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025571 tail = regmatch.endp[0];
25572 if (*tail == NUL)
25573 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 if (!do_all)
25575 break;
25576 }
25577
25578 if (ga.ga_data != NULL)
25579 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25580
Bram Moolenaar473de612013-06-08 18:19:48 +020025581 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025582 }
25583
25584 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25585 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025586 if (p_cpo == empty_option)
25587 p_cpo = save_cpo;
25588 else
25589 /* Darn, evaluating {sub} expression changed the value. */
25590 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591
25592 return ret;
25593}
25594
25595#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */