blob: 0537c4e32b8321fb0fd132d882a6505e44b107ff [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));
558static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000560static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200561static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000569static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000570static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200571static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000572static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000573static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200576static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000577static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100583static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000586static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000600static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100605static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000607static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000619#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200620static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000621static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200623#ifdef FEAT_LUA
624static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200631static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000632static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000633static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000635static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000639#ifdef vim_mkdir
640static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
641#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100643#ifdef FEAT_MZSCHEME
644static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
645#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100648static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000649static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000650#ifdef FEAT_FLOAT
651static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
652#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000654static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000655static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200656#ifdef FEAT_PYTHON3
657static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
658#endif
659#ifdef FEAT_PYTHON
660static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
661#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000663static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000664static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000676#ifdef FEAT_FLOAT
677static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
678#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200679static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100681static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000684static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000686static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
689static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000693static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000694static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000695static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000696static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200698static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000699static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100701#ifdef FEAT_CRYPT
702static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
703#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000704static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200705static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000707#ifdef FEAT_FLOAT
708static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200709static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000710#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000711static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000712static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000713static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#ifdef FEAT_FLOAT
717static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
719#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000720static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200721static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722#ifdef HAVE_STRFTIME
723static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
724#endif
725static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200731static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200732static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000738static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200739static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200741static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000742static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000743static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000744static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000745static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000746static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000748static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200749#ifdef FEAT_FLOAT
750static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
752#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756#ifdef FEAT_FLOAT
757static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
758#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200760static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200761static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100762static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100766static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
768static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
770static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
772static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000773static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000776static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100777static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778
Bram Moolenaar493c1782014-05-28 14:34:46 +0200779static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000780static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static int get_env_len __ARGS((char_u **arg));
782static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000783static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000784static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
785#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
786#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
787 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000788static 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 +0000789static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000790static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200791static 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 +0000792static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793static typval_T *alloc_tv __ARGS((void));
794static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static void init_tv __ARGS((typval_T *varp));
796static long get_tv_number __ARGS((typval_T *varp));
797static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000798static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static char_u *get_tv_string __ARGS((typval_T *varp));
800static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000801static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100802static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
803static 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 +0000804static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
805static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
806static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000807static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
808static 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 +0000809static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200810static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
811static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200812static int var_check_func_name __ARGS((char_u *name, int new_var));
813static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200814static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000815static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
817static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
818static int eval_fname_script __ARGS((char_u *p));
819static int eval_fname_sid __ARGS((char_u *p));
820static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821static ufunc_T *find_func __ARGS((char_u *name));
822static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200823static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000824#ifdef FEAT_PROFILE
825static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000826static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
827static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
828static int
829# ifdef __BORLANDC__
830 _RTLENTRYF
831# endif
832 prof_total_cmp __ARGS((const void *s1, const void *s2));
833static int
834# ifdef __BORLANDC__
835 _RTLENTRYF
836# endif
837 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000838#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200839static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000840static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000841static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000842static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000843static 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 +0000844static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
845static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000846static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000847static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
848static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000849static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000850static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000851static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200852static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200853static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000854
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200855
856#ifdef EBCDIC
857static int compare_func_name __ARGS((const void *s1, const void *s2));
858static void sortFunctions __ARGS(());
859#endif
860
Bram Moolenaar33570922005-01-25 22:26:29 +0000861/*
862 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000863 */
864 void
865eval_init()
866{
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 int i;
868 struct vimvar *p;
869
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200870 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
871 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200872 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000873 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000874 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875
876 for (i = 0; i < VV_LEN; ++i)
877 {
878 p = &vimvars[i];
879 STRCPY(p->vv_di.di_key, p->vv_name);
880 if (p->vv_flags & VV_RO)
881 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
882 else if (p->vv_flags & VV_RO_SBX)
883 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
884 else
885 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000886
887 /* add to v: scope dict, unless the value is not always available */
888 if (p->vv_type != VAR_UNKNOWN)
889 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000890 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000891 /* add to compat scope dict */
892 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000893 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000894 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100895 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200896 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200897 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200898
899#ifdef EBCDIC
900 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100901 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200902 */
903 sortFunctions();
904#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000905}
906
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000907#if defined(EXITFREE) || defined(PROTO)
908 void
909eval_clear()
910{
911 int i;
912 struct vimvar *p;
913
914 for (i = 0; i < VV_LEN; ++i)
915 {
916 p = &vimvars[i];
917 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000918 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000919 vim_free(p->vv_str);
920 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000921 }
922 else if (p->vv_di.di_tv.v_type == VAR_LIST)
923 {
924 list_unref(p->vv_list);
925 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000927 }
928 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000929 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930 hash_clear(&compat_hashtab);
931
Bram Moolenaard9fba312005-06-26 22:34:35 +0000932 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100933# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200934 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100935# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000936
937 /* global variables */
938 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000939
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000940 /* autoloaded script names */
941 ga_clear_strings(&ga_loaded);
942
Bram Moolenaarcca74132013-09-25 21:00:28 +0200943 /* Script-local variables. First clear all the variables and in a second
944 * loop free the scriptvar_T, because a variable in one script might hold
945 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200946 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200947 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200948 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200949 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200950 ga_clear(&ga_scripts);
951
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000952 /* unreferenced lists and dicts */
953 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000954
955 /* functions */
956 free_all_functions();
957 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000958}
959#endif
960
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 * Return the name of the executed function.
963 */
964 char_u *
965func_name(cookie)
966 void *cookie;
967{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000968 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969}
970
971/*
972 * Return the address holding the next breakpoint line for a funccall cookie.
973 */
974 linenr_T *
975func_breakpoint(cookie)
976 void *cookie;
977{
Bram Moolenaar33570922005-01-25 22:26:29 +0000978 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979}
980
981/*
982 * Return the address holding the debug tick for a funccall cookie.
983 */
984 int *
985func_dbg_tick(cookie)
986 void *cookie;
987{
Bram Moolenaar33570922005-01-25 22:26:29 +0000988 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990
991/*
992 * Return the nesting level for a funccall cookie.
993 */
994 int
995func_level(cookie)
996 void *cookie;
997{
Bram Moolenaar33570922005-01-25 22:26:29 +0000998 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999}
1000
1001/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001002funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001004/* pointer to list of previously used funccal, still around because some
1005 * item in it is still being used. */
1006funccall_T *previous_funccal = NULL;
1007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008/*
1009 * Return TRUE when a function was ended by a ":return" command.
1010 */
1011 int
1012current_func_returned()
1013{
1014 return current_funccal->returned;
1015}
1016
1017
1018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 * Set an internal variable to a string value. Creates the variable if it does
1020 * not already exist.
1021 */
1022 void
1023set_internal_string_var(name, value)
1024 char_u *name;
1025 char_u *value;
1026{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001027 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001028 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
1030 val = vim_strsave(value);
1031 if (val != NULL)
1032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001033 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001034 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001036 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001037 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 }
1039 }
1040}
1041
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001043static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044static char_u *redir_endp = NULL;
1045static char_u *redir_varname = NULL;
1046
1047/*
1048 * Start recording command output to a variable
1049 * Returns OK if successfully completed the setup. FAIL otherwise.
1050 */
1051 int
1052var_redir_start(name, append)
1053 char_u *name;
1054 int append; /* append to an existing variable */
1055{
1056 int save_emsg;
1057 int err;
1058 typval_T tv;
1059
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001060 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001061 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 {
1063 EMSG(_(e_invarg));
1064 return FAIL;
1065 }
1066
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 redir_varname = vim_strsave(name);
1069 if (redir_varname == NULL)
1070 return FAIL;
1071
1072 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1073 if (redir_lval == NULL)
1074 {
1075 var_redir_stop();
1076 return FAIL;
1077 }
1078
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001079 /* The output is stored in growarray "redir_ga" until redirection ends. */
1080 ga_init2(&redir_ga, (int)sizeof(char), 500);
1081
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001083 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001084 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1086 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001087 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 if (redir_endp != NULL && *redir_endp != NUL)
1089 /* Trailing characters are present after the variable name */
1090 EMSG(_(e_trailing));
1091 else
1092 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001093 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 var_redir_stop();
1095 return FAIL;
1096 }
1097
1098 /* check if we can write to the variable: set it to or append an empty
1099 * string */
1100 save_emsg = did_emsg;
1101 did_emsg = FALSE;
1102 tv.v_type = VAR_STRING;
1103 tv.vval.v_string = (char_u *)"";
1104 if (append)
1105 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1106 else
1107 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001108 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001110 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 if (err)
1112 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001113 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 var_redir_stop();
1115 return FAIL;
1116 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117
1118 return OK;
1119}
1120
1121/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122 * Append "value[value_len]" to the variable set by var_redir_start().
1123 * The actual appending is postponed until redirection ends, because the value
1124 * appended may in fact be the string we write to, changing it may cause freed
1125 * memory to be used:
1126 * :redir => foo
1127 * :let foo
1128 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 */
1130 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136
1137 if (redir_lval == NULL)
1138 return;
1139
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001141 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001143 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001145 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001146 {
1147 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001148 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001149 }
1150 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152}
1153
1154/*
1155 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 */
1158 void
1159var_redir_stop()
1160{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001161 typval_T tv;
1162
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 if (redir_lval != NULL)
1164 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 /* If there was no error: assign the text to the variable. */
1166 if (redir_endp != NULL)
1167 {
1168 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1169 tv.v_type = VAR_STRING;
1170 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001171 /* Call get_lval() again, if it's inside a Dict or List it may
1172 * have changed. */
1173 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001174 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001175 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1176 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1177 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001178 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001179
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 /* free the collected output */
1181 vim_free(redir_ga.ga_data);
1182 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001183
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184 vim_free(redir_lval);
1185 redir_lval = NULL;
1186 }
1187 vim_free(redir_varname);
1188 redir_varname = NULL;
1189}
1190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191# if defined(FEAT_MBYTE) || defined(PROTO)
1192 int
1193eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1194 char_u *enc_from;
1195 char_u *enc_to;
1196 char_u *fname_from;
1197 char_u *fname_to;
1198{
1199 int err = FALSE;
1200
1201 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1202 set_vim_var_string(VV_CC_TO, enc_to, -1);
1203 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1204 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1205 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1206 err = TRUE;
1207 set_vim_var_string(VV_CC_FROM, NULL, -1);
1208 set_vim_var_string(VV_CC_TO, NULL, -1);
1209 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1210 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1211
1212 if (err)
1213 return FAIL;
1214 return OK;
1215}
1216# endif
1217
1218# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1219 int
1220eval_printexpr(fname, args)
1221 char_u *fname;
1222 char_u *args;
1223{
1224 int err = FALSE;
1225
1226 set_vim_var_string(VV_FNAME_IN, fname, -1);
1227 set_vim_var_string(VV_CMDARG, args, -1);
1228 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1229 err = TRUE;
1230 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1231 set_vim_var_string(VV_CMDARG, NULL, -1);
1232
1233 if (err)
1234 {
1235 mch_remove(fname);
1236 return FAIL;
1237 }
1238 return OK;
1239}
1240# endif
1241
1242# if defined(FEAT_DIFF) || defined(PROTO)
1243 void
1244eval_diff(origfile, newfile, outfile)
1245 char_u *origfile;
1246 char_u *newfile;
1247 char_u *outfile;
1248{
1249 int err = FALSE;
1250
1251 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1252 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1253 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1254 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1255 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1256 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1257 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1258}
1259
1260 void
1261eval_patch(origfile, difffile, outfile)
1262 char_u *origfile;
1263 char_u *difffile;
1264 char_u *outfile;
1265{
1266 int err;
1267
1268 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1269 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1270 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1271 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1272 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1273 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1274 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1275}
1276# endif
1277
1278/*
1279 * Top level evaluation function, returning a boolean.
1280 * Sets "error" to TRUE if there was an error.
1281 * Return TRUE or FALSE.
1282 */
1283 int
1284eval_to_bool(arg, error, nextcmd, skip)
1285 char_u *arg;
1286 int *error;
1287 char_u **nextcmd;
1288 int skip; /* only parse, don't execute */
1289{
Bram Moolenaar33570922005-01-25 22:26:29 +00001290 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 int retval = FALSE;
1292
1293 if (skip)
1294 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 else
1298 {
1299 *error = FALSE;
1300 if (!skip)
1301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001302 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 }
1305 }
1306 if (skip)
1307 --emsg_skip;
1308
1309 return retval;
1310}
1311
1312/*
1313 * Top level evaluation function, returning a string. If "skip" is TRUE,
1314 * only parsing to "nextcmd" is done, without reporting errors. Return
1315 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1316 */
1317 char_u *
1318eval_to_string_skip(arg, nextcmd, skip)
1319 char_u *arg;
1320 char_u **nextcmd;
1321 int skip; /* only parse, don't execute */
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 char_u *retval;
1325
1326 if (skip)
1327 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001328 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 retval = NULL;
1330 else
1331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001332 retval = vim_strsave(get_tv_string(&tv));
1333 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 }
1335 if (skip)
1336 --emsg_skip;
1337
1338 return retval;
1339}
1340
1341/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001342 * Skip over an expression at "*pp".
1343 * Return FAIL for an error, OK otherwise.
1344 */
1345 int
1346skip_expr(pp)
1347 char_u **pp;
1348{
Bram Moolenaar33570922005-01-25 22:26:29 +00001349 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001350
1351 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001353}
1354
1355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001357 * When "convert" is TRUE convert a List into a sequence of lines and convert
1358 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 * Return pointer to allocated memory, or NULL for failure.
1360 */
1361 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 char_u *arg;
1364 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
Bram Moolenaar33570922005-01-25 22:26:29 +00001367 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001369 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001370#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 retval = NULL;
1376 else
1377 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001378 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001379 {
1380 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001381 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001382 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001383 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001384 if (tv.vval.v_list->lv_len > 0)
1385 ga_append(&ga, NL);
1386 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001387 ga_append(&ga, NUL);
1388 retval = (char_u *)ga.ga_data;
1389 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001390#ifdef FEAT_FLOAT
1391 else if (convert && tv.v_type == VAR_FLOAT)
1392 {
1393 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1394 retval = vim_strsave(numbuf);
1395 }
1396#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 else
1398 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401
1402 return retval;
1403}
1404
1405/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 * Call eval_to_string() without using current local variables and using
1407 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 */
1409 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 char_u *arg;
1412 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
1415 char_u *retval;
1416 void *save_funccalp;
1417
1418 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001419 if (use_sandbox)
1420 ++sandbox;
1421 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001422 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001423 if (use_sandbox)
1424 --sandbox;
1425 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 restore_funccal(save_funccalp);
1427 return retval;
1428}
1429
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430/*
1431 * Top level evaluation function, returning a number.
1432 * Evaluates "expr" silently.
1433 * Returns -1 for an error.
1434 */
1435 int
1436eval_to_number(expr)
1437 char_u *expr;
1438{
Bram Moolenaar33570922005-01-25 22:26:29 +00001439 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001441 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
1443 ++emsg_off;
1444
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001445 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 retval = -1;
1447 else
1448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001449 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001450 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452 --emsg_off;
1453
1454 return retval;
1455}
1456
Bram Moolenaara40058a2005-07-11 22:42:07 +00001457/*
1458 * Prepare v: variable "idx" to be used.
1459 * Save the current typeval in "save_tv".
1460 * When not used yet add the variable to the v: hashtable.
1461 */
1462 static void
1463prepare_vimvar(idx, save_tv)
1464 int idx;
1465 typval_T *save_tv;
1466{
1467 *save_tv = vimvars[idx].vv_tv;
1468 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1469 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1470}
1471
1472/*
1473 * Restore v: variable "idx" to typeval "save_tv".
1474 * When no longer defined, remove the variable from the v: hashtable.
1475 */
1476 static void
1477restore_vimvar(idx, save_tv)
1478 int idx;
1479 typval_T *save_tv;
1480{
1481 hashitem_T *hi;
1482
Bram Moolenaara40058a2005-07-11 22:42:07 +00001483 vimvars[idx].vv_tv = *save_tv;
1484 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1485 {
1486 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1487 if (HASHITEM_EMPTY(hi))
1488 EMSG2(_(e_intern2), "restore_vimvar()");
1489 else
1490 hash_remove(&vimvarht, hi);
1491 }
1492}
1493
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001494#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001495/*
1496 * Evaluate an expression to a list with suggestions.
1497 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001498 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001499 */
1500 list_T *
1501eval_spell_expr(badword, expr)
1502 char_u *badword;
1503 char_u *expr;
1504{
1505 typval_T save_val;
1506 typval_T rettv;
1507 list_T *list = NULL;
1508 char_u *p = skipwhite(expr);
1509
1510 /* Set "v:val" to the bad word. */
1511 prepare_vimvar(VV_VAL, &save_val);
1512 vimvars[VV_VAL].vv_type = VAR_STRING;
1513 vimvars[VV_VAL].vv_str = badword;
1514 if (p_verbose == 0)
1515 ++emsg_off;
1516
1517 if (eval1(&p, &rettv, TRUE) == OK)
1518 {
1519 if (rettv.v_type != VAR_LIST)
1520 clear_tv(&rettv);
1521 else
1522 list = rettv.vval.v_list;
1523 }
1524
1525 if (p_verbose == 0)
1526 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001527 restore_vimvar(VV_VAL, &save_val);
1528
1529 return list;
1530}
1531
1532/*
1533 * "list" is supposed to contain two items: a word and a number. Return the
1534 * word in "pp" and the number as the return value.
1535 * Return -1 if anything isn't right.
1536 * Used to get the good word and score from the eval_spell_expr() result.
1537 */
1538 int
1539get_spellword(list, pp)
1540 list_T *list;
1541 char_u **pp;
1542{
1543 listitem_T *li;
1544
1545 li = list->lv_first;
1546 if (li == NULL)
1547 return -1;
1548 *pp = get_tv_string(&li->li_tv);
1549
1550 li = li->li_next;
1551 if (li == NULL)
1552 return -1;
1553 return get_tv_number(&li->li_tv);
1554}
1555#endif
1556
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001557/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001558 * Top level evaluation function.
1559 * Returns an allocated typval_T with the result.
1560 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001561 */
1562 typval_T *
1563eval_expr(arg, nextcmd)
1564 char_u *arg;
1565 char_u **nextcmd;
1566{
1567 typval_T *tv;
1568
1569 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001570 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001571 {
1572 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001573 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001574 }
1575
1576 return tv;
1577}
1578
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001581 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001582 * Uses argv[argc] for the function arguments. Only Number and String
1583 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001586 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001587call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 char_u *func;
1589 int argc;
1590 char_u **argv;
1591 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001592 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594{
Bram Moolenaar33570922005-01-25 22:26:29 +00001595 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 long n;
1597 int len;
1598 int i;
1599 int doesrange;
1600 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001601 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001603 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
1607 for (i = 0; i < argc; i++)
1608 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001609 /* Pass a NULL or empty argument as an empty string */
1610 if (argv[i] == NULL || *argv[i] == NUL)
1611 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001612 argvars[i].v_type = VAR_STRING;
1613 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001614 continue;
1615 }
1616
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001617 if (str_arg_only)
1618 len = 0;
1619 else
1620 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001621 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (len != 0 && len == (int)STRLEN(argv[i]))
1623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001624 argvars[i].v_type = VAR_NUMBER;
1625 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
1627 else
1628 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001629 argvars[i].v_type = VAR_STRING;
1630 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
1632 }
1633
1634 if (safe)
1635 {
1636 save_funccalp = save_funccal();
1637 ++sandbox;
1638 }
1639
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1641 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (safe)
1645 {
1646 --sandbox;
1647 restore_funccal(save_funccalp);
1648 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 vim_free(argvars);
1650
1651 if (ret == FAIL)
1652 clear_tv(rettv);
1653
1654 return ret;
1655}
1656
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001657/*
1658 * Call vimL function "func" and return the result as a number.
1659 * Returns -1 when calling the function fails.
1660 * Uses argv[argc] for the function arguments.
1661 */
1662 long
1663call_func_retnr(func, argc, argv, safe)
1664 char_u *func;
1665 int argc;
1666 char_u **argv;
1667 int safe; /* use the sandbox */
1668{
1669 typval_T rettv;
1670 long retval;
1671
1672 /* All arguments are passed as strings, no conversion to number. */
1673 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1674 return -1;
1675
1676 retval = get_tv_number_chk(&rettv, NULL);
1677 clear_tv(&rettv);
1678 return retval;
1679}
1680
1681#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1682 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1683
Bram Moolenaar4f688582007-07-24 12:34:30 +00001684# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001686 * Call vimL function "func" and return the result as a string.
1687 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 * Uses argv[argc] for the function arguments.
1689 */
1690 void *
1691call_func_retstr(func, argc, argv, safe)
1692 char_u *func;
1693 int argc;
1694 char_u **argv;
1695 int safe; /* use the sandbox */
1696{
1697 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001698 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001700 /* All arguments are passed as strings, no conversion to number. */
1701 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702 return NULL;
1703
1704 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 return retval;
1707}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001708# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001710/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001711 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001713 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 */
1715 void *
1716call_func_retlist(func, argc, argv, safe)
1717 char_u *func;
1718 int argc;
1719 char_u **argv;
1720 int safe; /* use the sandbox */
1721{
1722 typval_T rettv;
1723
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001724 /* All arguments are passed as strings, no conversion to number. */
1725 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001726 return NULL;
1727
1728 if (rettv.v_type != VAR_LIST)
1729 {
1730 clear_tv(&rettv);
1731 return NULL;
1732 }
1733
1734 return rettv.vval.v_list;
1735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736#endif
1737
1738/*
1739 * Save the current function call pointer, and set it to NULL.
1740 * Used when executing autocommands and for ":source".
1741 */
1742 void *
1743save_funccal()
1744{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001745 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 current_funccal = NULL;
1748 return (void *)fc;
1749}
1750
1751 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001752restore_funccal(vfc)
1753 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001755 funccall_T *fc = (funccall_T *)vfc;
1756
1757 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758}
1759
Bram Moolenaar05159a02005-02-26 23:04:13 +00001760#if defined(FEAT_PROFILE) || defined(PROTO)
1761/*
1762 * Prepare profiling for entering a child or something else that is not
1763 * counted for the script/function itself.
1764 * Should always be called in pair with prof_child_exit().
1765 */
1766 void
1767prof_child_enter(tm)
1768 proftime_T *tm; /* place to store waittime */
1769{
1770 funccall_T *fc = current_funccal;
1771
1772 if (fc != NULL && fc->func->uf_profiling)
1773 profile_start(&fc->prof_child);
1774 script_prof_save(tm);
1775}
1776
1777/*
1778 * Take care of time spent in a child.
1779 * Should always be called after prof_child_enter().
1780 */
1781 void
1782prof_child_exit(tm)
1783 proftime_T *tm; /* where waittime was stored */
1784{
1785 funccall_T *fc = current_funccal;
1786
1787 if (fc != NULL && fc->func->uf_profiling)
1788 {
1789 profile_end(&fc->prof_child);
1790 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1791 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1792 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1793 }
1794 script_prof_restore(tm);
1795}
1796#endif
1797
1798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799#ifdef FEAT_FOLDING
1800/*
1801 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1802 * it in "*cp". Doesn't give error messages.
1803 */
1804 int
1805eval_foldexpr(arg, cp)
1806 char_u *arg;
1807 int *cp;
1808{
Bram Moolenaar33570922005-01-25 22:26:29 +00001809 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 int retval;
1811 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001812 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1813 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001816 if (use_sandbox)
1817 ++sandbox;
1818 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 retval = 0;
1822 else
1823 {
1824 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 if (tv.v_type == VAR_NUMBER)
1826 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001827 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 retval = 0;
1829 else
1830 {
1831 /* If the result is a string, check if there is a non-digit before
1832 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 if (!VIM_ISDIGIT(*s) && *s != '-')
1835 *cp = *s++;
1836 retval = atol((char *)s);
1837 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 }
1840 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001841 if (use_sandbox)
1842 --sandbox;
1843 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
1845 return retval;
1846}
1847#endif
1848
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 * ":let" list all variable values
1851 * ":let var1 var2" list variable values
1852 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001853 * ":let var += expr" assignment command.
1854 * ":let var -= expr" assignment command.
1855 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 */
1858 void
1859ex_let(eap)
1860 exarg_T *eap;
1861{
1862 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001864 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 int var_count = 0;
1867 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001868 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001869 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
Bram Moolenaardb552d602006-03-23 22:59:57 +00001872 argend = skip_var_list(arg, &var_count, &semicolon);
1873 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001875 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1876 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001877 expr = skipwhite(argend);
1878 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1879 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001881 /*
1882 * ":let" without "=": list variables
1883 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 if (*arg == '[')
1885 EMSG(_(e_invarg));
1886 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001888 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001892 list_glob_vars(&first);
1893 list_buf_vars(&first);
1894 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001895#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001896 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001897#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 list_script_vars(&first);
1899 list_func_vars(&first);
1900 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 eap->nextcmd = check_nextcmd(arg);
1903 }
1904 else
1905 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 op[0] = '=';
1907 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001910 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1911 op[0] = *expr; /* +=, -= or .= */
1912 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001914 else
1915 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (eap->skip)
1918 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (eap->skip)
1921 {
1922 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 --emsg_skip;
1925 }
1926 else if (i != FAIL)
1927 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 }
1932 }
1933}
1934
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935/*
1936 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1937 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 * When "nextchars" is not NULL it points to a string with characters that
1939 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1940 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 * Returns OK or FAIL;
1942 */
1943 static int
1944ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1945 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001946 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 int copy; /* copy values from "tv", don't move */
1948 int semicolon; /* from skip_var_list() */
1949 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951{
1952 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001953 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 listitem_T *item;
1956 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957
1958 if (*arg != '[')
1959 {
1960 /*
1961 * ":let var = expr" or ":for var in list"
1962 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 return FAIL;
1965 return OK;
1966 }
1967
1968 /*
1969 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1970 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001971 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 {
1973 EMSG(_(e_listreq));
1974 return FAIL;
1975 }
1976
1977 i = list_len(l);
1978 if (semicolon == 0 && var_count < i)
1979 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001980 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 return FAIL;
1982 }
1983 if (var_count - semicolon > i)
1984 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001985 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 return FAIL;
1987 }
1988
1989 item = l->lv_first;
1990 while (*arg != ']')
1991 {
1992 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 item = item->li_next;
1995 if (arg == NULL)
1996 return FAIL;
1997
1998 arg = skipwhite(arg);
1999 if (*arg == ';')
2000 {
2001 /* Put the rest of the list (may be empty) in the var after ';'.
2002 * Create a new list for this. */
2003 l = list_alloc();
2004 if (l == NULL)
2005 return FAIL;
2006 while (item != NULL)
2007 {
2008 list_append_tv(l, &item->li_tv);
2009 item = item->li_next;
2010 }
2011
2012 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002013 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 ltv.vval.v_list = l;
2015 l->lv_refcount = 1;
2016
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002017 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2018 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 clear_tv(&ltv);
2020 if (arg == NULL)
2021 return FAIL;
2022 break;
2023 }
2024 else if (*arg != ',' && *arg != ']')
2025 {
2026 EMSG2(_(e_intern2), "ex_let_vars()");
2027 return FAIL;
2028 }
2029 }
2030
2031 return OK;
2032}
2033
2034/*
2035 * Skip over assignable variable "var" or list of variables "[var, var]".
2036 * Used for ":let varvar = expr" and ":for varvar in expr".
2037 * For "[var, var]" increment "*var_count" for each variable.
2038 * for "[var, var; var]" set "semicolon".
2039 * Return NULL for an error.
2040 */
2041 static char_u *
2042skip_var_list(arg, var_count, semicolon)
2043 char_u *arg;
2044 int *var_count;
2045 int *semicolon;
2046{
2047 char_u *p, *s;
2048
2049 if (*arg == '[')
2050 {
2051 /* "[var, var]": find the matching ']'. */
2052 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002053 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 {
2055 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2056 s = skip_var_one(p);
2057 if (s == p)
2058 {
2059 EMSG2(_(e_invarg2), p);
2060 return NULL;
2061 }
2062 ++*var_count;
2063
2064 p = skipwhite(s);
2065 if (*p == ']')
2066 break;
2067 else if (*p == ';')
2068 {
2069 if (*semicolon == 1)
2070 {
2071 EMSG(_("Double ; in list of variables"));
2072 return NULL;
2073 }
2074 *semicolon = 1;
2075 }
2076 else if (*p != ',')
2077 {
2078 EMSG2(_(e_invarg2), p);
2079 return NULL;
2080 }
2081 }
2082 return p + 1;
2083 }
2084 else
2085 return skip_var_one(arg);
2086}
2087
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002088/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002089 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002090 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002091 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092 static char_u *
2093skip_var_one(arg)
2094 char_u *arg;
2095{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002096 if (*arg == '@' && arg[1] != NUL)
2097 return arg + 2;
2098 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2099 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002100}
2101
Bram Moolenaara7043832005-01-21 11:56:39 +00002102/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002103 * List variables for hashtab "ht" with prefix "prefix".
2104 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002108 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112{
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 hashitem_T *hi;
2114 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115 int todo;
2116
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002117 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2119 {
2120 if (!HASHITEM_EMPTY(hi))
2121 {
2122 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002123 di = HI2DI(hi);
2124 if (empty || di->di_tv.v_type != VAR_STRING
2125 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002127 }
2128 }
2129}
2130
2131/*
2132 * List global variables.
2133 */
2134 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135list_glob_vars(first)
2136 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002137{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002139}
2140
2141/*
2142 * List buffer variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_buf_vars(first)
2146 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002148 char_u numbuf[NUMBUFLEN];
2149
Bram Moolenaar429fa852013-04-15 12:27:36 +02002150 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002152
2153 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2155 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002156}
2157
2158/*
2159 * List window variables.
2160 */
2161 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162list_win_vars(first)
2163 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002164{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002165 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002167}
2168
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002169#ifdef FEAT_WINDOWS
2170/*
2171 * List tab page variables.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_tab_vars(first)
2175 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002176{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002177 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179}
2180#endif
2181
Bram Moolenaara7043832005-01-21 11:56:39 +00002182/*
2183 * List Vim variables.
2184 */
2185 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_vim_vars(first)
2187 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190}
2191
2192/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002193 * List script-local variables, if there is a script.
2194 */
2195 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196list_script_vars(first)
2197 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002198{
2199 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2201 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002202}
2203
2204/*
2205 * List function variables, if there is a function.
2206 */
2207 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208list_func_vars(first)
2209 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002210{
2211 if (current_funccal != NULL)
2212 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002213 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002214}
2215
2216/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 * List variables in "arg".
2218 */
2219 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 exarg_T *eap;
2222 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224{
2225 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 char_u *name_start;
2229 char_u *arg_subsc;
2230 char_u *tofree;
2231 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232
2233 while (!ends_excmd(*arg) && !got_int)
2234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002237 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2239 {
2240 emsg_severe = TRUE;
2241 EMSG(_(e_trailing));
2242 break;
2243 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 /* get_name_len() takes care of expanding curly braces */
2248 name_start = name = arg;
2249 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2250 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 /* This is mainly to keep test 49 working: when expanding
2253 * curly braces fails overrule the exception error message. */
2254 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 emsg_severe = TRUE;
2257 EMSG2(_(e_invarg2), arg);
2258 break;
2259 }
2260 error = TRUE;
2261 }
2262 else
2263 {
2264 if (tofree != NULL)
2265 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002266 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
2269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 /* handle d.key, l[idx], f(expr) */
2271 arg_subsc = arg;
2272 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'g': list_glob_vars(first); break;
2281 case 'b': list_buf_vars(first); break;
2282 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002283#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002284 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002285#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002286 case 'v': list_vim_vars(first); break;
2287 case 's': list_script_vars(first); break;
2288 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 default:
2290 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002292 }
2293 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 {
2295 char_u numbuf[NUMBUFLEN];
2296 char_u *tf;
2297 int c;
2298 char_u *s;
2299
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002300 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 c = *arg;
2302 *arg = NUL;
2303 list_one_var_a((char_u *)"",
2304 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002305 tv.v_type,
2306 s == NULL ? (char_u *)"" : s,
2307 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 *arg = c;
2309 vim_free(tf);
2310 }
2311 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 }
2314 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315
2316 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318
2319 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 }
2321
2322 return arg;
2323}
2324
2325/*
2326 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2327 * Returns a pointer to the char just after the var name.
2328 * Returns NULL if there is an error.
2329 */
2330 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002333 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002335 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337{
2338 int c1;
2339 char_u *name;
2340 char_u *p;
2341 char_u *arg_end = NULL;
2342 int len;
2343 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345
2346 /*
2347 * ":let $VAR = expr": Set environment variable.
2348 */
2349 if (*arg == '$')
2350 {
2351 /* Find the end of the name. */
2352 ++arg;
2353 name = arg;
2354 len = get_env_len(&arg);
2355 if (len == 0)
2356 EMSG2(_(e_invarg2), name - 1);
2357 else
2358 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 if (op != NULL && (*op == '+' || *op == '-'))
2360 EMSG2(_(e_letwrong), op);
2361 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002362 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002364 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 {
2366 c1 = name[len];
2367 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 p = get_tv_string_chk(tv);
2369 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 {
2371 int mustfree = FALSE;
2372 char_u *s = vim_getenv(name, &mustfree);
2373
2374 if (s != NULL)
2375 {
2376 p = tofree = concat_str(s, p);
2377 if (mustfree)
2378 vim_free(s);
2379 }
2380 }
2381 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 if (STRICMP(name, "HOME") == 0)
2385 init_homedir();
2386 else if (didset_vim && STRICMP(name, "VIM") == 0)
2387 didset_vim = FALSE;
2388 else if (didset_vimruntime
2389 && STRICMP(name, "VIMRUNTIME") == 0)
2390 didset_vimruntime = FALSE;
2391 arg_end = arg;
2392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 }
2396 }
2397 }
2398
2399 /*
2400 * ":let &option = expr": Set option value.
2401 * ":let &l:option = expr": Set local option value.
2402 * ":let &g:option = expr": Set global option value.
2403 */
2404 else if (*arg == '&')
2405 {
2406 /* Find the end of the name. */
2407 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002408 if (p == NULL || (endchars != NULL
2409 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 EMSG(_(e_letunexp));
2411 else
2412 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 long n;
2414 int opt_type;
2415 long numval;
2416 char_u *stringval = NULL;
2417 char_u *s;
2418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 c1 = *p;
2420 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002421
2422 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423 s = get_tv_string_chk(tv); /* != NULL if number or string */
2424 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002425 {
2426 opt_type = get_option_value(arg, &numval,
2427 &stringval, opt_flags);
2428 if ((opt_type == 1 && *op == '.')
2429 || (opt_type == 0 && *op != '.'))
2430 EMSG2(_(e_letwrong), op);
2431 else
2432 {
2433 if (opt_type == 1) /* number */
2434 {
2435 if (*op == '+')
2436 n = numval + n;
2437 else
2438 n = numval - n;
2439 }
2440 else if (opt_type == 0 && stringval != NULL) /* string */
2441 {
2442 s = concat_str(stringval, s);
2443 vim_free(stringval);
2444 stringval = s;
2445 }
2446 }
2447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002448 if (s != NULL)
2449 {
2450 set_option_value(arg, n, s, opt_flags);
2451 arg_end = p;
2452 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 }
2456 }
2457
2458 /*
2459 * ":let @r = expr": Set register contents.
2460 */
2461 else if (*arg == '@')
2462 {
2463 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 if (op != NULL && (*op == '+' || *op == '-'))
2465 EMSG2(_(e_letwrong), op);
2466 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002467 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 EMSG(_(e_letunexp));
2469 else
2470 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002471 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 char_u *s;
2473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 p = get_tv_string_chk(tv);
2475 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002477 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 if (s != NULL)
2479 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002480 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 vim_free(s);
2482 }
2483 }
2484 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002487 arg_end = arg + 1;
2488 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002489 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 }
2491 }
2492
2493 /*
2494 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002497 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002499 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002501 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2505 EMSG(_(e_letunexp));
2506 else
2507 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 arg_end = p;
2510 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 }
2514
2515 else
2516 EMSG2(_(e_invarg2), arg);
2517
2518 return arg_end;
2519}
2520
2521/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2523 */
2524 static int
2525check_changedtick(arg)
2526 char_u *arg;
2527{
2528 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2529 {
2530 EMSG2(_(e_readonlyvar), arg);
2531 return TRUE;
2532 }
2533 return FALSE;
2534}
2535
2536/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 * Get an lval: variable, Dict item or List item that can be assigned a value
2538 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2539 * "name.key", "name.key[expr]" etc.
2540 * Indexing only works if "name" is an existing List or Dictionary.
2541 * "name" points to the start of the name.
2542 * If "rettv" is not NULL it points to the value to be assigned.
2543 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2544 * wrong; must end in space or cmd separator.
2545 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002546 * flags:
2547 * GLV_QUIET: do not give error messages
2548 * GLV_NO_AUTOLOAD: do not use script autoloading
2549 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002551 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002553 */
2554 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002555get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 typval_T *rettv;
2558 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 int unlet;
2560 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002561 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002562 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 char_u *expr_start, *expr_end;
2566 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 dictitem_T *v;
2568 typval_T var1;
2569 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002572 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002574 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002575 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002578 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579
2580 if (skip)
2581 {
2582 /* When skipping just find the end of the name. */
2583 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002584 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 }
2586
2587 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002588 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (expr_start != NULL)
2590 {
2591 /* Don't expand the name when we already know there is an error. */
2592 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2593 && *p != '[' && *p != '.')
2594 {
2595 EMSG(_(e_trailing));
2596 return NULL;
2597 }
2598
2599 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2600 if (lp->ll_exp_name == NULL)
2601 {
2602 /* Report an invalid expression in braces, unless the
2603 * expression evaluation has been cancelled due to an
2604 * aborting error, an interrupt, or an exception. */
2605 if (!aborting() && !quiet)
2606 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002607 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 EMSG2(_(e_invarg2), name);
2609 return NULL;
2610 }
2611 }
2612 lp->ll_name = lp->ll_exp_name;
2613 }
2614 else
2615 lp->ll_name = name;
2616
2617 /* Without [idx] or .key we are done. */
2618 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2619 return p;
2620
2621 cc = *p;
2622 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002623 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (v == NULL && !quiet)
2625 EMSG2(_(e_undefvar), lp->ll_name);
2626 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 if (v == NULL)
2628 return NULL;
2629
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 /*
2631 * Loop until no more [idx] or .key is following.
2632 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002633 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2637 && !(lp->ll_tv->v_type == VAR_DICT
2638 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (!quiet)
2641 EMSG(_("E689: Can only index a List or Dictionary"));
2642 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
2647 EMSG(_("E708: [:] must come last"));
2648 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 len = -1;
2652 if (*p == '.')
2653 {
2654 key = p + 1;
2655 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2656 ;
2657 if (len == 0)
2658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_(e_emptykey));
2661 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 }
2663 p = key + len;
2664 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 else
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 if (*p == ':')
2670 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671 else
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 empty1 = FALSE;
2674 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002676 if (get_tv_string_chk(&var1) == NULL)
2677 {
2678 /* not a number or string */
2679 clear_tv(&var1);
2680 return NULL;
2681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
2683
2684 /* Optionally get the second index [ :expr]. */
2685 if (*p == ':')
2686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002690 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 if (!empty1)
2692 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (rettv != NULL && (rettv->v_type != VAR_LIST
2696 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
2699 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (!empty1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704 p = skipwhite(p + 1);
2705 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 else
2708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2711 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (!empty1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002716 if (get_tv_string_chk(&var2) == NULL)
2717 {
2718 /* not a number or string */
2719 if (!empty1)
2720 clear_tv(&var1);
2721 clear_tv(&var2);
2722 return NULL;
2723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002729
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (!quiet)
2733 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 if (!empty1)
2735 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 }
2740
2741 /* Skip to past ']'. */
2742 ++p;
2743 }
2744
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 {
2747 if (len == -1)
2748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002750 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 if (*key == NUL)
2752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (!quiet)
2754 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 }
2758 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759 lp->ll_list = NULL;
2760 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002761 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002762
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002763 /* When assigning to a scope dictionary check that a function and
2764 * variable name is valid (only variable name unless it is l: or
2765 * g: dictionary). Disallow overwriting a builtin function. */
2766 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002768 int prevval;
2769 int wrong;
2770
2771 if (len != -1)
2772 {
2773 prevval = key[len];
2774 key[len] = NUL;
2775 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002776 else
2777 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2779 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002780 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 || !valid_varname(key);
2782 if (len != -1)
2783 key[len] = prevval;
2784 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002785 return NULL;
2786 }
2787
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002790 /* Can't add "v:" variable. */
2791 if (lp->ll_dict == &vimvardict)
2792 {
2793 EMSG2(_(e_illvar), name);
2794 return NULL;
2795 }
2796
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002797 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002801 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 if (len == -1)
2803 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 }
2806 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 if (len == -1)
2811 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 p = NULL;
2814 break;
2815 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002816 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002817 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818 return NULL;
2819
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (len == -1)
2821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824 else
2825 {
2826 /*
2827 * Get the number and item for the only or first index of the List.
2828 */
2829 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 else
2832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002833 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 clear_tv(&var1);
2835 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002836 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 lp->ll_list = lp->ll_tv->vval.v_list;
2838 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2839 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002841 if (lp->ll_n1 < 0)
2842 {
2843 lp->ll_n1 = 0;
2844 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2845 }
2846 }
2847 if (lp->ll_li == NULL)
2848 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002851 if (!quiet)
2852 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 }
2855
2856 /*
2857 * May need to find the item or absolute index for the second
2858 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 * When no index given: "lp->ll_empty2" is TRUE.
2860 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002864 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002870 {
2871 if (!quiet)
2872 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002874 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 }
2877
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2879 if (lp->ll_n1 < 0)
2880 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2881 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002882 {
2883 if (!quiet)
2884 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002886 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002887 }
2888
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002891 }
2892
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 return p;
2894}
2895
2896/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 */
2899 static void
2900clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002901 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902{
2903 vim_free(lp->ll_exp_name);
2904 vim_free(lp->ll_newkey);
2905}
2906
2907/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002908 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 */
2912 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002913set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002918 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919{
2920 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 listitem_T *ri;
2922 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923
2924 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002925 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 cc = *endp;
2929 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 if (op != NULL && *op != '=')
2931 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002932 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002933
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002934 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002935 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002936 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002937 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002939 if ((di == NULL
2940 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2941 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2942 FALSE)))
2943 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 set_var(lp->ll_name, &tv, FALSE);
2945 clear_tv(&tv);
2946 }
2947 }
2948 else
2949 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002951 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 else if (tv_check_lock(lp->ll_newkey == NULL
2954 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002955 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002956 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 else if (lp->ll_range)
2958 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002959 listitem_T *ll_li = lp->ll_li;
2960 int ll_n1 = lp->ll_n1;
2961
2962 /*
2963 * Check whether any of the list items is locked
2964 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002965 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002966 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002967 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002968 return;
2969 ri = ri->li_next;
2970 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2971 break;
2972 ll_li = ll_li->li_next;
2973 ++ll_n1;
2974 }
2975
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 /*
2977 * Assign the List values to the list items.
2978 */
2979 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002980 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 if (op != NULL && *op != '=')
2982 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2983 else
2984 {
2985 clear_tv(&lp->ll_li->li_tv);
2986 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2987 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 ri = ri->li_next;
2989 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2990 break;
2991 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002994 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002995 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 ri = NULL;
2997 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002999 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 lp->ll_li = lp->ll_li->li_next;
3001 ++lp->ll_n1;
3002 }
3003 if (ri != NULL)
3004 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 else if (lp->ll_empty2
3006 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 : lp->ll_n1 != lp->ll_n2)
3008 EMSG(_("E711: List value has not enough items"));
3009 }
3010 else
3011 {
3012 /*
3013 * Assign to a List or Dictionary item.
3014 */
3015 if (lp->ll_newkey != NULL)
3016 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003017 if (op != NULL && *op != '=')
3018 {
3019 EMSG2(_(e_letwrong), op);
3020 return;
3021 }
3022
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003024 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 if (di == NULL)
3026 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003027 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3028 {
3029 vim_free(di);
3030 return;
3031 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003033 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003034 else if (op != NULL && *op != '=')
3035 {
3036 tv_op(lp->ll_tv, rettv, op);
3037 return;
3038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003039 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003041
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 /*
3043 * Assign the value to the variable or list item.
3044 */
3045 if (copy)
3046 copy_tv(rettv, lp->ll_tv);
3047 else
3048 {
3049 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003050 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003052 }
3053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054}
3055
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3058 * Returns OK or FAIL.
3059 */
3060 static int
3061tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003062 typval_T *tv1;
3063 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 char_u *op;
3065{
3066 long n;
3067 char_u numbuf[NUMBUFLEN];
3068 char_u *s;
3069
3070 /* Can't do anything with a Funcref or a Dict on the right. */
3071 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3072 {
3073 switch (tv1->v_type)
3074 {
3075 case VAR_DICT:
3076 case VAR_FUNC:
3077 break;
3078
3079 case VAR_LIST:
3080 if (*op != '+' || tv2->v_type != VAR_LIST)
3081 break;
3082 /* List += List */
3083 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3084 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3085 return OK;
3086
3087 case VAR_NUMBER:
3088 case VAR_STRING:
3089 if (tv2->v_type == VAR_LIST)
3090 break;
3091 if (*op == '+' || *op == '-')
3092 {
3093 /* nr += nr or nr -= nr*/
3094 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003095#ifdef FEAT_FLOAT
3096 if (tv2->v_type == VAR_FLOAT)
3097 {
3098 float_T f = n;
3099
3100 if (*op == '+')
3101 f += tv2->vval.v_float;
3102 else
3103 f -= tv2->vval.v_float;
3104 clear_tv(tv1);
3105 tv1->v_type = VAR_FLOAT;
3106 tv1->vval.v_float = f;
3107 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003108 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003109#endif
3110 {
3111 if (*op == '+')
3112 n += get_tv_number(tv2);
3113 else
3114 n -= get_tv_number(tv2);
3115 clear_tv(tv1);
3116 tv1->v_type = VAR_NUMBER;
3117 tv1->vval.v_number = n;
3118 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 }
3120 else
3121 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122 if (tv2->v_type == VAR_FLOAT)
3123 break;
3124
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003125 /* str .= str */
3126 s = get_tv_string(tv1);
3127 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3128 clear_tv(tv1);
3129 tv1->v_type = VAR_STRING;
3130 tv1->vval.v_string = s;
3131 }
3132 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003133
3134#ifdef FEAT_FLOAT
3135 case VAR_FLOAT:
3136 {
3137 float_T f;
3138
3139 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3140 && tv2->v_type != VAR_NUMBER
3141 && tv2->v_type != VAR_STRING))
3142 break;
3143 if (tv2->v_type == VAR_FLOAT)
3144 f = tv2->vval.v_float;
3145 else
3146 f = get_tv_number(tv2);
3147 if (*op == '+')
3148 tv1->vval.v_float += f;
3149 else
3150 tv1->vval.v_float -= f;
3151 }
3152 return OK;
3153#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 }
3155 }
3156
3157 EMSG2(_(e_letwrong), op);
3158 return FAIL;
3159}
3160
3161/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162 * Add a watcher to a list.
3163 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003164 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003165list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 list_T *l;
3167 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168{
3169 lw->lw_next = l->lv_watch;
3170 l->lv_watch = lw;
3171}
3172
3173/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003174 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175 * No warning when it isn't found...
3176 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003177 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003179 list_T *l;
3180 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181{
Bram Moolenaar33570922005-01-25 22:26:29 +00003182 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183
3184 lwp = &l->lv_watch;
3185 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3186 {
3187 if (lw == lwrem)
3188 {
3189 *lwp = lw->lw_next;
3190 break;
3191 }
3192 lwp = &lw->lw_next;
3193 }
3194}
3195
3196/*
3197 * Just before removing an item from a list: advance watchers to the next
3198 * item.
3199 */
3200 static void
3201list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003202 list_T *l;
3203 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204{
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206
3207 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3208 if (lw->lw_item == item)
3209 lw->lw_item = item->li_next;
3210}
3211
3212/*
3213 * Evaluate the expression used in a ":for var in expr" command.
3214 * "arg" points to "var".
3215 * Set "*errp" to TRUE for an error, FALSE otherwise;
3216 * Return a pointer that holds the info. Null when there is an error.
3217 */
3218 void *
3219eval_for_line(arg, errp, nextcmdp, skip)
3220 char_u *arg;
3221 int *errp;
3222 char_u **nextcmdp;
3223 int skip;
3224{
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 typval_T tv;
3228 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229
3230 *errp = TRUE; /* default: there is an error */
3231
Bram Moolenaar33570922005-01-25 22:26:29 +00003232 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 if (fi == NULL)
3234 return NULL;
3235
3236 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3237 if (expr == NULL)
3238 return fi;
3239
3240 expr = skipwhite(expr);
3241 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3242 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003243 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 return fi;
3245 }
3246
3247 if (skip)
3248 ++emsg_skip;
3249 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3250 {
3251 *errp = FALSE;
3252 if (!skip)
3253 {
3254 l = tv.vval.v_list;
3255 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 clear_tv(&tv);
3259 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 else
3261 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003262 /* No need to increment the refcount, it's already set for the
3263 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 fi->fi_list = l;
3265 list_add_watch(l, &fi->fi_lw);
3266 fi->fi_lw.lw_item = l->lv_first;
3267 }
3268 }
3269 }
3270 if (skip)
3271 --emsg_skip;
3272
3273 return fi;
3274}
3275
3276/*
3277 * Use the first item in a ":for" list. Advance to the next.
3278 * Assign the values to the variable (list). "arg" points to the first one.
3279 * Return TRUE when a valid item was found, FALSE when at end of list or
3280 * something wrong.
3281 */
3282 int
3283next_for_item(fi_void, arg)
3284 void *fi_void;
3285 char_u *arg;
3286{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003287 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003289 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290
3291 item = fi->fi_lw.lw_item;
3292 if (item == NULL)
3293 result = FALSE;
3294 else
3295 {
3296 fi->fi_lw.lw_item = item->li_next;
3297 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3298 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3299 }
3300 return result;
3301}
3302
3303/*
3304 * Free the structure used to store info used by ":for".
3305 */
3306 void
3307free_for_info(fi_void)
3308 void *fi_void;
3309{
Bram Moolenaar33570922005-01-25 22:26:29 +00003310 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003312 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003313 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003315 list_unref(fi->fi_list);
3316 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317 vim_free(fi);
3318}
3319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3321
3322 void
3323set_context_for_expression(xp, arg, cmdidx)
3324 expand_T *xp;
3325 char_u *arg;
3326 cmdidx_T cmdidx;
3327{
3328 int got_eq = FALSE;
3329 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003332 if (cmdidx == CMD_let)
3333 {
3334 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003335 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 {
3337 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003338 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 {
3340 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003341 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 if (vim_iswhite(*p))
3343 break;
3344 }
3345 return;
3346 }
3347 }
3348 else
3349 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3350 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 while ((xp->xp_pattern = vim_strpbrk(arg,
3352 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3353 {
3354 c = *xp->xp_pattern;
3355 if (c == '&')
3356 {
3357 c = xp->xp_pattern[1];
3358 if (c == '&')
3359 {
3360 ++xp->xp_pattern;
3361 xp->xp_context = cmdidx != CMD_let || got_eq
3362 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3363 }
3364 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003367 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3368 xp->xp_pattern += 2;
3369
3370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 }
3372 else if (c == '$')
3373 {
3374 /* environment variable */
3375 xp->xp_context = EXPAND_ENV_VARS;
3376 }
3377 else if (c == '=')
3378 {
3379 got_eq = TRUE;
3380 xp->xp_context = EXPAND_EXPRESSION;
3381 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003382 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 && xp->xp_context == EXPAND_FUNCTIONS
3384 && vim_strchr(xp->xp_pattern, '(') == NULL)
3385 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003386 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 break;
3388 }
3389 else if (cmdidx != CMD_let || got_eq)
3390 {
3391 if (c == '"') /* string */
3392 {
3393 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3394 if (c == '\\' && xp->xp_pattern[1] != NUL)
3395 ++xp->xp_pattern;
3396 xp->xp_context = EXPAND_NOTHING;
3397 }
3398 else if (c == '\'') /* literal string */
3399 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003400 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3402 /* skip */ ;
3403 xp->xp_context = EXPAND_NOTHING;
3404 }
3405 else if (c == '|')
3406 {
3407 if (xp->xp_pattern[1] == '|')
3408 {
3409 ++xp->xp_pattern;
3410 xp->xp_context = EXPAND_EXPRESSION;
3411 }
3412 else
3413 xp->xp_context = EXPAND_COMMANDS;
3414 }
3415 else
3416 xp->xp_context = EXPAND_EXPRESSION;
3417 }
3418 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003419 /* Doesn't look like something valid, expand as an expression
3420 * anyway. */
3421 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 arg = xp->xp_pattern;
3423 if (*arg != NUL)
3424 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3425 /* skip */ ;
3426 }
3427 xp->xp_pattern = arg;
3428}
3429
3430#endif /* FEAT_CMDL_COMPL */
3431
3432/*
3433 * ":1,25call func(arg1, arg2)" function call.
3434 */
3435 void
3436ex_call(eap)
3437 exarg_T *eap;
3438{
3439 char_u *arg = eap->arg;
3440 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003442 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003444 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 linenr_T lnum;
3446 int doesrange;
3447 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003450 if (eap->skip)
3451 {
3452 /* trans_function_name() doesn't work well when skipping, use eval0()
3453 * instead to skip to any following command, e.g. for:
3454 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003455 ++emsg_skip;
3456 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3457 clear_tv(&rettv);
3458 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003459 return;
3460 }
3461
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003462 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003463 if (fudi.fd_newkey != NULL)
3464 {
3465 /* Still need to give an error message for missing key. */
3466 EMSG2(_(e_dictkey), fudi.fd_newkey);
3467 vim_free(fudi.fd_newkey);
3468 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003469 if (tofree == NULL)
3470 return;
3471
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 /* Increase refcount on dictionary, it could get deleted when evaluating
3473 * the arguments. */
3474 if (fudi.fd_dict != NULL)
3475 ++fudi.fd_dict->dv_refcount;
3476
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003477 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003478 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003479 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
Bram Moolenaar532c7802005-01-27 14:44:31 +00003481 /* Skip white space to allow ":call func ()". Not good, but required for
3482 * backward compatibility. */
3483 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003484 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 if (*startarg != '(')
3487 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003488 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 goto end;
3490 }
3491
3492 /*
3493 * When skipping, evaluate the function once, to find the end of the
3494 * arguments.
3495 * When the function takes a range, this is discovered after the first
3496 * call, and the loop is broken.
3497 */
3498 if (eap->skip)
3499 {
3500 ++emsg_skip;
3501 lnum = eap->line2; /* do it once, also with an invalid range */
3502 }
3503 else
3504 lnum = eap->line1;
3505 for ( ; lnum <= eap->line2; ++lnum)
3506 {
3507 if (!eap->skip && eap->addr_count > 0)
3508 {
3509 curwin->w_cursor.lnum = lnum;
3510 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003511#ifdef FEAT_VIRTUALEDIT
3512 curwin->w_cursor.coladd = 0;
3513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003516 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003517 eap->line1, eap->line2, &doesrange,
3518 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
3520 failed = TRUE;
3521 break;
3522 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003523
3524 /* Handle a function returning a Funcref, Dictionary or List. */
3525 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3526 {
3527 failed = TRUE;
3528 break;
3529 }
3530
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003531 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (doesrange || eap->skip)
3533 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003536 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003538 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (aborting())
3540 break;
3541 }
3542 if (eap->skip)
3543 --emsg_skip;
3544
3545 if (!failed)
3546 {
3547 /* Check for trailing illegal characters and a following command. */
3548 if (!ends_excmd(*arg))
3549 {
3550 emsg_severe = TRUE;
3551 EMSG(_(e_trailing));
3552 }
3553 else
3554 eap->nextcmd = check_nextcmd(arg);
3555 }
3556
3557end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003558 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003559 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560}
3561
3562/*
3563 * ":unlet[!] var1 ... " command.
3564 */
3565 void
3566ex_unlet(eap)
3567 exarg_T *eap;
3568{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 ex_unletlock(eap, eap->arg, 0);
3570}
3571
3572/*
3573 * ":lockvar" and ":unlockvar" commands
3574 */
3575 void
3576ex_lockvar(eap)
3577 exarg_T *eap;
3578{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580 int deep = 2;
3581
3582 if (eap->forceit)
3583 deep = -1;
3584 else if (vim_isdigit(*arg))
3585 {
3586 deep = getdigits(&arg);
3587 arg = skipwhite(arg);
3588 }
3589
3590 ex_unletlock(eap, arg, deep);
3591}
3592
3593/*
3594 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3595 */
3596 static void
3597ex_unletlock(eap, argstart, deep)
3598 exarg_T *eap;
3599 char_u *argstart;
3600 int deep;
3601{
3602 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
3607 do
3608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003610 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003611 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 if (lv.ll_name == NULL)
3613 error = TRUE; /* error but continue parsing */
3614 if (name_end == NULL || (!vim_iswhite(*name_end)
3615 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 if (name_end != NULL)
3618 {
3619 emsg_severe = TRUE;
3620 EMSG(_(e_trailing));
3621 }
3622 if (!(eap->skip || error))
3623 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 break;
3625 }
3626
3627 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 {
3629 if (eap->cmdidx == CMD_unlet)
3630 {
3631 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3632 error = TRUE;
3633 }
3634 else
3635 {
3636 if (do_lock_var(&lv, name_end, deep,
3637 eap->cmdidx == CMD_lockvar) == FAIL)
3638 error = TRUE;
3639 }
3640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 if (!eap->skip)
3643 clear_lval(&lv);
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 arg = skipwhite(name_end);
3646 } while (!ends_excmd(*arg));
3647
3648 eap->nextcmd = check_nextcmd(arg);
3649}
3650
Bram Moolenaar8c711452005-01-14 21:53:12 +00003651 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003653 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 int forceit;
3656{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 int ret = OK;
3658 int cc;
3659
3660 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 cc = *name_end;
3663 *name_end = NUL;
3664
3665 /* Normal name or expanded name. */
3666 if (check_changedtick(lp->ll_name))
3667 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003669 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003672 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003673 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003674 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003676 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 else if (lp->ll_range)
3678 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003680 listitem_T *ll_li = lp->ll_li;
3681 int ll_n1 = lp->ll_n1;
3682
3683 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3684 {
3685 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003686 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003687 return FAIL;
3688 ll_li = li;
3689 ++ll_n1;
3690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003691
3692 /* Delete a range of List items. */
3693 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3694 {
3695 li = lp->ll_li->li_next;
3696 listitem_remove(lp->ll_list, lp->ll_li);
3697 lp->ll_li = li;
3698 ++lp->ll_n1;
3699 }
3700 }
3701 else
3702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003704 /* unlet a List item. */
3705 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003707 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003708 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 }
3710
3711 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003712}
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714/*
3715 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003716 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 */
3718 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003719do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003721 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 hashtab_T *ht;
3724 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003725 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003726 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003727 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
Bram Moolenaar33570922005-01-25 22:26:29 +00003729 ht = find_var_ht(name, &varname);
3730 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003732 if (ht == &globvarht)
3733 d = &globvardict;
3734 else if (current_funccal != NULL
3735 && ht == &current_funccal->l_vars.dv_hashtab)
3736 d = &current_funccal->l_vars;
3737 else
3738 {
3739 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3740 d = di->di_tv.vval.v_dict;
3741 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 hi = hash_find(ht, varname);
3743 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003744 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003745 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003746 if (var_check_fixed(di->di_flags, name, FALSE)
3747 || var_check_ro(di->di_flags, name, FALSE)
3748 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749 return FAIL;
3750 delete_var(ht, hi);
3751 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003754 if (forceit)
3755 return OK;
3756 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 return FAIL;
3758}
3759
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760/*
3761 * Lock or unlock variable indicated by "lp".
3762 * "deep" is the levels to go (-1 for unlimited);
3763 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3764 */
3765 static int
3766do_lock_var(lp, name_end, deep, lock)
3767 lval_T *lp;
3768 char_u *name_end;
3769 int deep;
3770 int lock;
3771{
3772 int ret = OK;
3773 int cc;
3774 dictitem_T *di;
3775
3776 if (deep == 0) /* nothing to do */
3777 return OK;
3778
3779 if (lp->ll_tv == NULL)
3780 {
3781 cc = *name_end;
3782 *name_end = NUL;
3783
3784 /* Normal name or expanded name. */
3785 if (check_changedtick(lp->ll_name))
3786 ret = FAIL;
3787 else
3788 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003789 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003790 if (di == NULL)
3791 ret = FAIL;
3792 else
3793 {
3794 if (lock)
3795 di->di_flags |= DI_FLAGS_LOCK;
3796 else
3797 di->di_flags &= ~DI_FLAGS_LOCK;
3798 item_lock(&di->di_tv, deep, lock);
3799 }
3800 }
3801 *name_end = cc;
3802 }
3803 else if (lp->ll_range)
3804 {
3805 listitem_T *li = lp->ll_li;
3806
3807 /* (un)lock a range of List items. */
3808 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3809 {
3810 item_lock(&li->li_tv, deep, lock);
3811 li = li->li_next;
3812 ++lp->ll_n1;
3813 }
3814 }
3815 else if (lp->ll_list != NULL)
3816 /* (un)lock a List item. */
3817 item_lock(&lp->ll_li->li_tv, deep, lock);
3818 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003819 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003820 item_lock(&lp->ll_di->di_tv, deep, lock);
3821
3822 return ret;
3823}
3824
3825/*
3826 * Lock or unlock an item. "deep" is nr of levels to go.
3827 */
3828 static void
3829item_lock(tv, deep, lock)
3830 typval_T *tv;
3831 int deep;
3832 int lock;
3833{
3834 static int recurse = 0;
3835 list_T *l;
3836 listitem_T *li;
3837 dict_T *d;
3838 hashitem_T *hi;
3839 int todo;
3840
3841 if (recurse >= DICT_MAXNEST)
3842 {
3843 EMSG(_("E743: variable nested too deep for (un)lock"));
3844 return;
3845 }
3846 if (deep == 0)
3847 return;
3848 ++recurse;
3849
3850 /* lock/unlock the item itself */
3851 if (lock)
3852 tv->v_lock |= VAR_LOCKED;
3853 else
3854 tv->v_lock &= ~VAR_LOCKED;
3855
3856 switch (tv->v_type)
3857 {
3858 case VAR_LIST:
3859 if ((l = tv->vval.v_list) != NULL)
3860 {
3861 if (lock)
3862 l->lv_lock |= VAR_LOCKED;
3863 else
3864 l->lv_lock &= ~VAR_LOCKED;
3865 if (deep < 0 || deep > 1)
3866 /* recursive: lock/unlock the items the List contains */
3867 for (li = l->lv_first; li != NULL; li = li->li_next)
3868 item_lock(&li->li_tv, deep - 1, lock);
3869 }
3870 break;
3871 case VAR_DICT:
3872 if ((d = tv->vval.v_dict) != NULL)
3873 {
3874 if (lock)
3875 d->dv_lock |= VAR_LOCKED;
3876 else
3877 d->dv_lock &= ~VAR_LOCKED;
3878 if (deep < 0 || deep > 1)
3879 {
3880 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003881 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003882 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3883 {
3884 if (!HASHITEM_EMPTY(hi))
3885 {
3886 --todo;
3887 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3888 }
3889 }
3890 }
3891 }
3892 }
3893 --recurse;
3894}
3895
Bram Moolenaara40058a2005-07-11 22:42:07 +00003896/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003897 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3898 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003899 */
3900 static int
3901tv_islocked(tv)
3902 typval_T *tv;
3903{
3904 return (tv->v_lock & VAR_LOCKED)
3905 || (tv->v_type == VAR_LIST
3906 && tv->vval.v_list != NULL
3907 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3908 || (tv->v_type == VAR_DICT
3909 && tv->vval.v_dict != NULL
3910 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3911}
3912
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3914/*
3915 * Delete all "menutrans_" variables.
3916 */
3917 void
3918del_menutrans_vars()
3919{
Bram Moolenaar33570922005-01-25 22:26:29 +00003920 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003921 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
Bram Moolenaar33570922005-01-25 22:26:29 +00003923 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003924 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003926 {
3927 if (!HASHITEM_EMPTY(hi))
3928 {
3929 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3931 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 }
3933 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935}
3936#endif
3937
3938#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3939
3940/*
3941 * Local string buffer for the next two functions to store a variable name
3942 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3943 * get_user_var_name().
3944 */
3945
3946static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3947
3948static char_u *varnamebuf = NULL;
3949static int varnamebuflen = 0;
3950
3951/*
3952 * Function to concatenate a prefix and a variable name.
3953 */
3954 static char_u *
3955cat_prefix_varname(prefix, name)
3956 int prefix;
3957 char_u *name;
3958{
3959 int len;
3960
3961 len = (int)STRLEN(name) + 3;
3962 if (len > varnamebuflen)
3963 {
3964 vim_free(varnamebuf);
3965 len += 10; /* some additional space */
3966 varnamebuf = alloc(len);
3967 if (varnamebuf == NULL)
3968 {
3969 varnamebuflen = 0;
3970 return NULL;
3971 }
3972 varnamebuflen = len;
3973 }
3974 *varnamebuf = prefix;
3975 varnamebuf[1] = ':';
3976 STRCPY(varnamebuf + 2, name);
3977 return varnamebuf;
3978}
3979
3980/*
3981 * Function given to ExpandGeneric() to obtain the list of user defined
3982 * (global/buffer/window/built-in) variable names.
3983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 char_u *
3985get_user_var_name(xp, idx)
3986 expand_T *xp;
3987 int idx;
3988{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003989 static long_u gdone;
3990 static long_u bdone;
3991 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003992#ifdef FEAT_WINDOWS
3993 static long_u tdone;
3994#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003995 static int vidx;
3996 static hashitem_T *hi;
3997 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004000 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004001 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002#ifdef FEAT_WINDOWS
4003 tdone = 0;
4004#endif
4005 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004006
4007 /* Global variables */
4008 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004010 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004011 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004012 else
4013 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004014 while (HASHITEM_EMPTY(hi))
4015 ++hi;
4016 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4017 return cat_prefix_varname('g', hi->hi_key);
4018 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004020
4021 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004022 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004023 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004025 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004026 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004027 else
4028 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 while (HASHITEM_EMPTY(hi))
4030 ++hi;
4031 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004033 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004035 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 return (char_u *)"b:changedtick";
4037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038
4039 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004040 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004043 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 else
4046 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 while (HASHITEM_EMPTY(hi))
4048 ++hi;
4049 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004051
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004052#ifdef FEAT_WINDOWS
4053 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004054 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004055 if (tdone < ht->ht_used)
4056 {
4057 if (tdone++ == 0)
4058 hi = ht->ht_array;
4059 else
4060 ++hi;
4061 while (HASHITEM_EMPTY(hi))
4062 ++hi;
4063 return cat_prefix_varname('t', hi->hi_key);
4064 }
4065#endif
4066
Bram Moolenaar33570922005-01-25 22:26:29 +00004067 /* v: variables */
4068 if (vidx < VV_LEN)
4069 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
4071 vim_free(varnamebuf);
4072 varnamebuf = NULL;
4073 varnamebuflen = 0;
4074 return NULL;
4075}
4076
4077#endif /* FEAT_CMDL_COMPL */
4078
4079/*
4080 * types for expressions.
4081 */
4082typedef enum
4083{
4084 TYPE_UNKNOWN = 0
4085 , TYPE_EQUAL /* == */
4086 , TYPE_NEQUAL /* != */
4087 , TYPE_GREATER /* > */
4088 , TYPE_GEQUAL /* >= */
4089 , TYPE_SMALLER /* < */
4090 , TYPE_SEQUAL /* <= */
4091 , TYPE_MATCH /* =~ */
4092 , TYPE_NOMATCH /* !~ */
4093} exptype_T;
4094
4095/*
4096 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4099 */
4100
4101/*
4102 * Handle zero level expression.
4103 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004105 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 * Return OK or FAIL.
4107 */
4108 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 char_u **nextcmd;
4113 int evaluate;
4114{
4115 int ret;
4116 char_u *p;
4117
4118 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (ret == FAIL || !ends_excmd(*p))
4121 {
4122 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 /*
4125 * Report the invalid expression unless the expression evaluation has
4126 * been cancelled due to an aborting error, an interrupt, or an
4127 * exception.
4128 */
4129 if (!aborting())
4130 EMSG2(_(e_invexpr2), arg);
4131 ret = FAIL;
4132 }
4133 if (nextcmd != NULL)
4134 *nextcmd = check_nextcmd(p);
4135
4136 return ret;
4137}
4138
4139/*
4140 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004141 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 *
4143 * "arg" must point to the first non-white of the expression.
4144 * "arg" is advanced to the next non-white after the recognized expression.
4145 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004146 * Note: "rettv.v_lock" is not set.
4147 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 * Return OK or FAIL.
4149 */
4150 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 int evaluate;
4155{
4156 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004157 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
4159 /*
4160 * Get the first variable.
4161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 return FAIL;
4164
4165 if ((*arg)[0] == '?')
4166 {
4167 result = FALSE;
4168 if (evaluate)
4169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 int error = FALSE;
4171
4172 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004175 if (error)
4176 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178
4179 /*
4180 * Get the second variable.
4181 */
4182 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return FAIL;
4185
4186 /*
4187 * Check for the ":".
4188 */
4189 if ((*arg)[0] != ':')
4190 {
4191 EMSG(_("E109: Missing ':' after '?'"));
4192 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 return FAIL;
4195 }
4196
4197 /*
4198 * Get the third variable.
4199 */
4200 *arg = skipwhite(*arg + 1);
4201 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4202 {
4203 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
4206 }
4207 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210
4211 return OK;
4212}
4213
4214/*
4215 * Handle first level expression:
4216 * expr2 || expr2 || expr2 logical OR
4217 *
4218 * "arg" must point to the first non-white of the expression.
4219 * "arg" is advanced to the next non-white after the recognized expression.
4220 *
4221 * Return OK or FAIL.
4222 */
4223 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 int evaluate;
4228{
Bram Moolenaar33570922005-01-25 22:26:29 +00004229 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 long result;
4231 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234 /*
4235 * Get the first variable.
4236 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return FAIL;
4239
4240 /*
4241 * Repeat until there is no following "||".
4242 */
4243 first = TRUE;
4244 result = FALSE;
4245 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4246 {
4247 if (evaluate && first)
4248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 if (error)
4253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 first = FALSE;
4255 }
4256
4257 /*
4258 * Get the second variable.
4259 */
4260 *arg = skipwhite(*arg + 2);
4261 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4262 return FAIL;
4263
4264 /*
4265 * Compute the result.
4266 */
4267 if (evaluate && !result)
4268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 if (error)
4273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 if (evaluate)
4276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 rettv->v_type = VAR_NUMBER;
4278 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 }
4281
4282 return OK;
4283}
4284
4285/*
4286 * Handle second level expression:
4287 * expr3 && expr3 && expr3 logical AND
4288 *
4289 * "arg" must point to the first non-white of the expression.
4290 * "arg" is advanced to the next non-white after the recognized expression.
4291 *
4292 * Return OK or FAIL.
4293 */
4294 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 int evaluate;
4299{
Bram Moolenaar33570922005-01-25 22:26:29 +00004300 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 long result;
4302 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304
4305 /*
4306 * Get the first variable.
4307 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return FAIL;
4310
4311 /*
4312 * Repeat until there is no following "&&".
4313 */
4314 first = TRUE;
4315 result = TRUE;
4316 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4317 {
4318 if (evaluate && first)
4319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 if (error)
4324 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 first = FALSE;
4326 }
4327
4328 /*
4329 * Get the second variable.
4330 */
4331 *arg = skipwhite(*arg + 2);
4332 if (eval4(arg, &var2, evaluate && result) == FAIL)
4333 return FAIL;
4334
4335 /*
4336 * Compute the result.
4337 */
4338 if (evaluate && result)
4339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 if (error)
4344 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
4346 if (evaluate)
4347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348 rettv->v_type = VAR_NUMBER;
4349 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
4351 }
4352
4353 return OK;
4354}
4355
4356/*
4357 * Handle third level expression:
4358 * var1 == var2
4359 * var1 =~ var2
4360 * var1 != var2
4361 * var1 !~ var2
4362 * var1 > var2
4363 * var1 >= var2
4364 * var1 < var2
4365 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004366 * var1 is var2
4367 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 *
4369 * "arg" must point to the first non-white of the expression.
4370 * "arg" is advanced to the next non-white after the recognized expression.
4371 *
4372 * Return OK or FAIL.
4373 */
4374 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004375eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 int evaluate;
4379{
Bram Moolenaar33570922005-01-25 22:26:29 +00004380 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 char_u *p;
4382 int i;
4383 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004384 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 int len = 2;
4386 long n1, n2;
4387 char_u *s1, *s2;
4388 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4389 regmatch_T regmatch;
4390 int ic;
4391 char_u *save_cpo;
4392
4393 /*
4394 * Get the first variable.
4395 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return FAIL;
4398
4399 p = *arg;
4400 switch (p[0])
4401 {
4402 case '=': if (p[1] == '=')
4403 type = TYPE_EQUAL;
4404 else if (p[1] == '~')
4405 type = TYPE_MATCH;
4406 break;
4407 case '!': if (p[1] == '=')
4408 type = TYPE_NEQUAL;
4409 else if (p[1] == '~')
4410 type = TYPE_NOMATCH;
4411 break;
4412 case '>': if (p[1] != '=')
4413 {
4414 type = TYPE_GREATER;
4415 len = 1;
4416 }
4417 else
4418 type = TYPE_GEQUAL;
4419 break;
4420 case '<': if (p[1] != '=')
4421 {
4422 type = TYPE_SMALLER;
4423 len = 1;
4424 }
4425 else
4426 type = TYPE_SEQUAL;
4427 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004428 case 'i': if (p[1] == 's')
4429 {
4430 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4431 len = 5;
4432 if (!vim_isIDc(p[len]))
4433 {
4434 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4435 type_is = TRUE;
4436 }
4437 }
4438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440
4441 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004442 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 */
4444 if (type != TYPE_UNKNOWN)
4445 {
4446 /* extra question mark appended: ignore case */
4447 if (p[len] == '?')
4448 {
4449 ic = TRUE;
4450 ++len;
4451 }
4452 /* extra '#' appended: match case */
4453 else if (p[len] == '#')
4454 {
4455 ic = FALSE;
4456 ++len;
4457 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004458 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 else
4460 ic = p_ic;
4461
4462 /*
4463 * Get the second variable.
4464 */
4465 *arg = skipwhite(p + len);
4466 if (eval5(arg, &var2, evaluate) == FAIL)
4467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004468 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 return FAIL;
4470 }
4471
4472 if (evaluate)
4473 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004474 if (type_is && rettv->v_type != var2.v_type)
4475 {
4476 /* For "is" a different type always means FALSE, for "notis"
4477 * it means TRUE. */
4478 n1 = (type == TYPE_NEQUAL);
4479 }
4480 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4481 {
4482 if (type_is)
4483 {
4484 n1 = (rettv->v_type == var2.v_type
4485 && rettv->vval.v_list == var2.vval.v_list);
4486 if (type == TYPE_NEQUAL)
4487 n1 = !n1;
4488 }
4489 else if (rettv->v_type != var2.v_type
4490 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4491 {
4492 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004493 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004494 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004495 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004496 clear_tv(rettv);
4497 clear_tv(&var2);
4498 return FAIL;
4499 }
4500 else
4501 {
4502 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004503 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4504 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004505 if (type == TYPE_NEQUAL)
4506 n1 = !n1;
4507 }
4508 }
4509
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004510 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4511 {
4512 if (type_is)
4513 {
4514 n1 = (rettv->v_type == var2.v_type
4515 && rettv->vval.v_dict == var2.vval.v_dict);
4516 if (type == TYPE_NEQUAL)
4517 n1 = !n1;
4518 }
4519 else if (rettv->v_type != var2.v_type
4520 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4521 {
4522 if (rettv->v_type != var2.v_type)
4523 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4524 else
4525 EMSG(_("E736: Invalid operation for Dictionary"));
4526 clear_tv(rettv);
4527 clear_tv(&var2);
4528 return FAIL;
4529 }
4530 else
4531 {
4532 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004533 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4534 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004535 if (type == TYPE_NEQUAL)
4536 n1 = !n1;
4537 }
4538 }
4539
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004540 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4541 {
4542 if (rettv->v_type != var2.v_type
4543 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4544 {
4545 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004546 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004547 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004548 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004549 clear_tv(rettv);
4550 clear_tv(&var2);
4551 return FAIL;
4552 }
4553 else
4554 {
4555 /* Compare two Funcrefs for being equal or unequal. */
4556 if (rettv->vval.v_string == NULL
4557 || var2.vval.v_string == NULL)
4558 n1 = FALSE;
4559 else
4560 n1 = STRCMP(rettv->vval.v_string,
4561 var2.vval.v_string) == 0;
4562 if (type == TYPE_NEQUAL)
4563 n1 = !n1;
4564 }
4565 }
4566
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004567#ifdef FEAT_FLOAT
4568 /*
4569 * If one of the two variables is a float, compare as a float.
4570 * When using "=~" or "!~", always compare as string.
4571 */
4572 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4573 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4574 {
4575 float_T f1, f2;
4576
4577 if (rettv->v_type == VAR_FLOAT)
4578 f1 = rettv->vval.v_float;
4579 else
4580 f1 = get_tv_number(rettv);
4581 if (var2.v_type == VAR_FLOAT)
4582 f2 = var2.vval.v_float;
4583 else
4584 f2 = get_tv_number(&var2);
4585 n1 = FALSE;
4586 switch (type)
4587 {
4588 case TYPE_EQUAL: n1 = (f1 == f2); break;
4589 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4590 case TYPE_GREATER: n1 = (f1 > f2); break;
4591 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4592 case TYPE_SMALLER: n1 = (f1 < f2); break;
4593 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4594 case TYPE_UNKNOWN:
4595 case TYPE_MATCH:
4596 case TYPE_NOMATCH: break; /* avoid gcc warning */
4597 }
4598 }
4599#endif
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 /*
4602 * If one of the two variables is a number, compare as a number.
4603 * When using "=~" or "!~", always compare as string.
4604 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004605 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 n1 = get_tv_number(rettv);
4609 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 switch (type)
4611 {
4612 case TYPE_EQUAL: n1 = (n1 == n2); break;
4613 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4614 case TYPE_GREATER: n1 = (n1 > n2); break;
4615 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4616 case TYPE_SMALLER: n1 = (n1 < n2); break;
4617 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4618 case TYPE_UNKNOWN:
4619 case TYPE_MATCH:
4620 case TYPE_NOMATCH: break; /* avoid gcc warning */
4621 }
4622 }
4623 else
4624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 s1 = get_tv_string_buf(rettv, buf1);
4626 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4628 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4629 else
4630 i = 0;
4631 n1 = FALSE;
4632 switch (type)
4633 {
4634 case TYPE_EQUAL: n1 = (i == 0); break;
4635 case TYPE_NEQUAL: n1 = (i != 0); break;
4636 case TYPE_GREATER: n1 = (i > 0); break;
4637 case TYPE_GEQUAL: n1 = (i >= 0); break;
4638 case TYPE_SMALLER: n1 = (i < 0); break;
4639 case TYPE_SEQUAL: n1 = (i <= 0); break;
4640
4641 case TYPE_MATCH:
4642 case TYPE_NOMATCH:
4643 /* avoid 'l' flag in 'cpoptions' */
4644 save_cpo = p_cpo;
4645 p_cpo = (char_u *)"";
4646 regmatch.regprog = vim_regcomp(s2,
4647 RE_MAGIC + RE_STRING);
4648 regmatch.rm_ic = ic;
4649 if (regmatch.regprog != NULL)
4650 {
4651 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004652 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 if (type == TYPE_NOMATCH)
4654 n1 = !n1;
4655 }
4656 p_cpo = save_cpo;
4657 break;
4658
4659 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4660 }
4661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004662 clear_tv(rettv);
4663 clear_tv(&var2);
4664 rettv->v_type = VAR_NUMBER;
4665 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
4667 }
4668
4669 return OK;
4670}
4671
4672/*
4673 * Handle fourth level expression:
4674 * + number addition
4675 * - number subtraction
4676 * . string concatenation
4677 *
4678 * "arg" must point to the first non-white of the expression.
4679 * "arg" is advanced to the next non-white after the recognized expression.
4680 *
4681 * Return OK or FAIL.
4682 */
4683 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 int evaluate;
4688{
Bram Moolenaar33570922005-01-25 22:26:29 +00004689 typval_T var2;
4690 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 int op;
4692 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004693#ifdef FEAT_FLOAT
4694 float_T f1 = 0, f2 = 0;
4695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 char_u *s1, *s2;
4697 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4698 char_u *p;
4699
4700 /*
4701 * Get the first variable.
4702 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004703 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return FAIL;
4705
4706 /*
4707 * Repeat computing, until no '+', '-' or '.' is following.
4708 */
4709 for (;;)
4710 {
4711 op = **arg;
4712 if (op != '+' && op != '-' && op != '.')
4713 break;
4714
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 if ((op != '+' || rettv->v_type != VAR_LIST)
4716#ifdef FEAT_FLOAT
4717 && (op == '.' || rettv->v_type != VAR_FLOAT)
4718#endif
4719 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 {
4721 /* For "list + ...", an illegal use of the first operand as
4722 * a number cannot be determined before evaluating the 2nd
4723 * operand: if this is also a list, all is ok.
4724 * For "something . ...", "something - ..." or "non-list + ...",
4725 * we know that the first operand needs to be a string or number
4726 * without evaluating the 2nd operand. So check before to avoid
4727 * side effects after an error. */
4728 if (evaluate && get_tv_string_chk(rettv) == NULL)
4729 {
4730 clear_tv(rettv);
4731 return FAIL;
4732 }
4733 }
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 /*
4736 * Get the second variable.
4737 */
4738 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004739 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004741 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return FAIL;
4743 }
4744
4745 if (evaluate)
4746 {
4747 /*
4748 * Compute the result.
4749 */
4750 if (op == '.')
4751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004752 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4753 s2 = get_tv_string_buf_chk(&var2, buf2);
4754 if (s2 == NULL) /* type error ? */
4755 {
4756 clear_tv(rettv);
4757 clear_tv(&var2);
4758 return FAIL;
4759 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004760 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 clear_tv(rettv);
4762 rettv->v_type = VAR_STRING;
4763 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004765 else if (op == '+' && rettv->v_type == VAR_LIST
4766 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004767 {
4768 /* concatenate Lists */
4769 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4770 &var3) == FAIL)
4771 {
4772 clear_tv(rettv);
4773 clear_tv(&var2);
4774 return FAIL;
4775 }
4776 clear_tv(rettv);
4777 *rettv = var3;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
4780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 int error = FALSE;
4782
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783#ifdef FEAT_FLOAT
4784 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 f1 = rettv->vval.v_float;
4787 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 else
4790#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 n1 = get_tv_number_chk(rettv, &error);
4793 if (error)
4794 {
4795 /* This can only happen for "list + non-list". For
4796 * "non-list + ..." or "something - ...", we returned
4797 * before evaluating the 2nd operand. */
4798 clear_tv(rettv);
4799 return FAIL;
4800 }
4801#ifdef FEAT_FLOAT
4802 if (var2.v_type == VAR_FLOAT)
4803 f1 = n1;
4804#endif
4805 }
4806#ifdef FEAT_FLOAT
4807 if (var2.v_type == VAR_FLOAT)
4808 {
4809 f2 = var2.vval.v_float;
4810 n2 = 0;
4811 }
4812 else
4813#endif
4814 {
4815 n2 = get_tv_number_chk(&var2, &error);
4816 if (error)
4817 {
4818 clear_tv(rettv);
4819 clear_tv(&var2);
4820 return FAIL;
4821 }
4822#ifdef FEAT_FLOAT
4823 if (rettv->v_type == VAR_FLOAT)
4824 f2 = n2;
4825#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828
4829#ifdef FEAT_FLOAT
4830 /* If there is a float on either side the result is a float. */
4831 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4832 {
4833 if (op == '+')
4834 f1 = f1 + f2;
4835 else
4836 f1 = f1 - f2;
4837 rettv->v_type = VAR_FLOAT;
4838 rettv->vval.v_float = f1;
4839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841#endif
4842 {
4843 if (op == '+')
4844 n1 = n1 + n2;
4845 else
4846 n1 = n1 - n2;
4847 rettv->v_type = VAR_NUMBER;
4848 rettv->vval.v_number = n1;
4849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 }
4854 return OK;
4855}
4856
4857/*
4858 * Handle fifth level expression:
4859 * * number multiplication
4860 * / number division
4861 * % number modulo
4862 *
4863 * "arg" must point to the first non-white of the expression.
4864 * "arg" is advanced to the next non-white after the recognized expression.
4865 *
4866 * Return OK or FAIL.
4867 */
4868 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004869eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004873 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874{
Bram Moolenaar33570922005-01-25 22:26:29 +00004875 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 int op;
4877 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878#ifdef FEAT_FLOAT
4879 int use_float = FALSE;
4880 float_T f1 = 0, f2;
4881#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 /*
4885 * Get the first variable.
4886 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004887 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 return FAIL;
4889
4890 /*
4891 * Repeat computing, until no '*', '/' or '%' is following.
4892 */
4893 for (;;)
4894 {
4895 op = **arg;
4896 if (op != '*' && op != '/' && op != '%')
4897 break;
4898
4899 if (evaluate)
4900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901#ifdef FEAT_FLOAT
4902 if (rettv->v_type == VAR_FLOAT)
4903 {
4904 f1 = rettv->vval.v_float;
4905 use_float = TRUE;
4906 n1 = 0;
4907 }
4908 else
4909#endif
4910 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004912 if (error)
4913 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 else
4916 n1 = 0;
4917
4918 /*
4919 * Get the second variable.
4920 */
4921 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004922 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return FAIL;
4924
4925 if (evaluate)
4926 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927#ifdef FEAT_FLOAT
4928 if (var2.v_type == VAR_FLOAT)
4929 {
4930 if (!use_float)
4931 {
4932 f1 = n1;
4933 use_float = TRUE;
4934 }
4935 f2 = var2.vval.v_float;
4936 n2 = 0;
4937 }
4938 else
4939#endif
4940 {
4941 n2 = get_tv_number_chk(&var2, &error);
4942 clear_tv(&var2);
4943 if (error)
4944 return FAIL;
4945#ifdef FEAT_FLOAT
4946 if (use_float)
4947 f2 = n2;
4948#endif
4949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 /*
4952 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958 if (op == '*')
4959 f1 = f1 * f2;
4960 else if (op == '/')
4961 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004962# ifdef VMS
4963 /* VMS crashes on divide by zero, work around it */
4964 if (f2 == 0.0)
4965 {
4966 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 }
4973 else
4974 f1 = f1 / f2;
4975# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 /* We rely on the floating point library to handle divide
4977 * by zero to result in "inf" and not a crash. */
4978 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004983 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 return FAIL;
4985 }
4986 rettv->v_type = VAR_FLOAT;
4987 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 if (op == '*')
4993 n1 = n1 * n2;
4994 else if (op == '/')
4995 {
4996 if (n2 == 0) /* give an error message? */
4997 {
4998 if (n1 == 0)
4999 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5000 else if (n1 < 0)
5001 n1 = -0x7fffffffL;
5002 else
5003 n1 = 0x7fffffffL;
5004 }
5005 else
5006 n1 = n1 / n2;
5007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 {
5010 if (n2 == 0) /* give an error message? */
5011 n1 = 0;
5012 else
5013 n1 = n1 % n2;
5014 }
5015 rettv->v_type = VAR_NUMBER;
5016 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 }
5020
5021 return OK;
5022}
5023
5024/*
5025 * Handle sixth level expression:
5026 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005027 * "string" string constant
5028 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 * &option-name option value
5030 * @r register contents
5031 * identifier variable value
5032 * function() function call
5033 * $VAR environment variable
5034 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005035 * [expr, expr] List
5036 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 *
5038 * Also handle:
5039 * ! in front logical NOT
5040 * - in front unary minus
5041 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 * trailing [] subscript in String or List
5043 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 *
5045 * "arg" must point to the first non-white of the expression.
5046 * "arg" is advanced to the next non-white after the recognized expression.
5047 *
5048 * Return OK or FAIL.
5049 */
5050 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005051eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005055 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 long n;
5058 int len;
5059 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 char_u *start_leader, *end_leader;
5061 int ret = OK;
5062 char_u *alias;
5063
5064 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005066 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 /*
5071 * Skip '!' and '-' characters. They are handled later.
5072 */
5073 start_leader = *arg;
5074 while (**arg == '!' || **arg == '-' || **arg == '+')
5075 *arg = skipwhite(*arg + 1);
5076 end_leader = *arg;
5077
5078 switch (**arg)
5079 {
5080 /*
5081 * Number constant.
5082 */
5083 case '0':
5084 case '1':
5085 case '2':
5086 case '3':
5087 case '4':
5088 case '5':
5089 case '6':
5090 case '7':
5091 case '8':
5092 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 {
5094#ifdef FEAT_FLOAT
5095 char_u *p = skipdigits(*arg + 1);
5096 int get_float = FALSE;
5097
5098 /* We accept a float when the format matches
5099 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005100 * strict to avoid backwards compatibility problems.
5101 * Don't look for a float after the "." operator, so that
5102 * ":let vers = 1.2.3" doesn't fail. */
5103 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 get_float = TRUE;
5106 p = skipdigits(p + 2);
5107 if (*p == 'e' || *p == 'E')
5108 {
5109 ++p;
5110 if (*p == '-' || *p == '+')
5111 ++p;
5112 if (!vim_isdigit(*p))
5113 get_float = FALSE;
5114 else
5115 p = skipdigits(p + 1);
5116 }
5117 if (ASCII_ISALPHA(*p) || *p == '.')
5118 get_float = FALSE;
5119 }
5120 if (get_float)
5121 {
5122 float_T f;
5123
5124 *arg += string2float(*arg, &f);
5125 if (evaluate)
5126 {
5127 rettv->v_type = VAR_FLOAT;
5128 rettv->vval.v_float = f;
5129 }
5130 }
5131 else
5132#endif
5133 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005134 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005135 *arg += len;
5136 if (evaluate)
5137 {
5138 rettv->v_type = VAR_NUMBER;
5139 rettv->vval.v_number = n;
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 /*
5146 * String constant: "string".
5147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 break;
5150
5151 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 break;
5156
5157 /*
5158 * List: [expr, expr]
5159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 break;
5162
5163 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 * Dictionary: {key: val, key: val}
5165 */
5166 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5167 break;
5168
5169 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
5176 * Environment variable: $VAR.
5177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 break;
5180
5181 /*
5182 * Register contents: @r.
5183 */
5184 case '@': ++*arg;
5185 if (evaluate)
5186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005188 rettv->vval.v_string = get_reg_contents(**arg,
5189 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 if (**arg != NUL)
5192 ++*arg;
5193 break;
5194
5195 /*
5196 * nested expression: (expression).
5197 */
5198 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (**arg == ')')
5201 ++*arg;
5202 else if (ret == OK)
5203 {
5204 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ret = FAIL;
5207 }
5208 break;
5209
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 break;
5212 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213
5214 if (ret == NOTDONE)
5215 {
5216 /*
5217 * Must be a variable or function name.
5218 * Can also be a curly-braces kind of name: {expr}.
5219 */
5220 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 if (alias != NULL)
5223 s = alias;
5224
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005225 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 ret = FAIL;
5227 else
5228 {
5229 if (**arg == '(') /* recursive! */
5230 {
5231 /* If "s" is the name of a variable of type VAR_FUNC
5232 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005233 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234
5235 /* Invoke the function. */
5236 ret = get_func_tv(s, len, rettv, arg,
5237 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005238 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005239
5240 /* If evaluate is FALSE rettv->v_type was not set in
5241 * get_func_tv, but it's needed in handle_subscript() to parse
5242 * what follows. So set it here. */
5243 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5244 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005245 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005246 rettv->v_type = VAR_FUNC;
5247 }
5248
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 /* Stop the expression evaluation when immediately
5250 * aborting on error, or when an interrupt occurred or
5251 * an exception was thrown but not caught. */
5252 if (aborting())
5253 {
5254 if (ret == OK)
5255 clear_tv(rettv);
5256 ret = FAIL;
5257 }
5258 }
5259 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005260 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005261 else
5262 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005264 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 }
5266
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 *arg = skipwhite(*arg);
5268
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5270 * expr(expr). */
5271 if (ret == OK)
5272 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
5274 /*
5275 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5276 */
5277 if (ret == OK && evaluate && end_leader > start_leader)
5278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005279 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005280 int val = 0;
5281#ifdef FEAT_FLOAT
5282 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284 if (rettv->v_type == VAR_FLOAT)
5285 f = rettv->vval.v_float;
5286 else
5287#endif
5288 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005289 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 clear_tv(rettv);
5292 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 else
5295 {
5296 while (end_leader > start_leader)
5297 {
5298 --end_leader;
5299 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005300 {
5301#ifdef FEAT_FLOAT
5302 if (rettv->v_type == VAR_FLOAT)
5303 f = !f;
5304 else
5305#endif
5306 val = !val;
5307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005309 {
5310#ifdef FEAT_FLOAT
5311 if (rettv->v_type == VAR_FLOAT)
5312 f = -f;
5313 else
5314#endif
5315 val = -val;
5316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005318#ifdef FEAT_FLOAT
5319 if (rettv->v_type == VAR_FLOAT)
5320 {
5321 clear_tv(rettv);
5322 rettv->vval.v_float = f;
5323 }
5324 else
5325#endif
5326 {
5327 clear_tv(rettv);
5328 rettv->v_type = VAR_NUMBER;
5329 rettv->vval.v_number = val;
5330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333
5334 return ret;
5335}
5336
5337/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005338 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5339 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5341 */
5342 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005343eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005345 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005347 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348{
5349 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005352 long len = -1;
5353 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005357 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005359 if (verbose)
5360 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 return FAIL;
5362 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005363#ifdef FEAT_FLOAT
5364 else if (rettv->v_type == VAR_FLOAT)
5365 {
5366 if (verbose)
5367 EMSG(_(e_float_as_string));
5368 return FAIL;
5369 }
5370#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 /*
5375 * dict.name
5376 */
5377 key = *arg + 1;
5378 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5379 ;
5380 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 }
5384 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 /*
5387 * something[idx]
5388 *
5389 * Get the (first) variable from inside the [].
5390 */
5391 *arg = skipwhite(*arg + 1);
5392 if (**arg == ':')
5393 empty1 = TRUE;
5394 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5395 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005396 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5397 {
5398 /* not a number or string */
5399 clear_tv(&var1);
5400 return FAIL;
5401 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402
5403 /*
5404 * Get the second variable from inside the [:].
5405 */
5406 if (**arg == ':')
5407 {
5408 range = TRUE;
5409 *arg = skipwhite(*arg + 1);
5410 if (**arg == ']')
5411 empty2 = TRUE;
5412 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005414 if (!empty1)
5415 clear_tv(&var1);
5416 return FAIL;
5417 }
5418 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5419 {
5420 /* not a number or string */
5421 if (!empty1)
5422 clear_tv(&var1);
5423 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424 return FAIL;
5425 }
5426 }
5427
5428 /* Check for the ']'. */
5429 if (**arg != ']')
5430 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005431 if (verbose)
5432 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005433 clear_tv(&var1);
5434 if (range)
5435 clear_tv(&var2);
5436 return FAIL;
5437 }
5438 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 }
5440
5441 if (evaluate)
5442 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443 n1 = 0;
5444 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005446 n1 = get_tv_number(&var1);
5447 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 }
5449 if (range)
5450 {
5451 if (empty2)
5452 n2 = -1;
5453 else
5454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005455 n2 = get_tv_number(&var2);
5456 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 }
5458 }
5459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 {
5462 case VAR_NUMBER:
5463 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 len = (long)STRLEN(s);
5466 if (range)
5467 {
5468 /* The resulting variable is a substring. If the indexes
5469 * are out of range the result is empty. */
5470 if (n1 < 0)
5471 {
5472 n1 = len + n1;
5473 if (n1 < 0)
5474 n1 = 0;
5475 }
5476 if (n2 < 0)
5477 n2 = len + n2;
5478 else if (n2 >= len)
5479 n2 = len;
5480 if (n1 >= len || n2 < 0 || n1 > n2)
5481 s = NULL;
5482 else
5483 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5484 }
5485 else
5486 {
5487 /* The resulting variable is a string of a single
5488 * character. If the index is too big or negative the
5489 * result is empty. */
5490 if (n1 >= len || n1 < 0)
5491 s = NULL;
5492 else
5493 s = vim_strnsave(s + n1, 1);
5494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005495 clear_tv(rettv);
5496 rettv->v_type = VAR_STRING;
5497 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498 break;
5499
5500 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005501 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 if (n1 < 0)
5503 n1 = len + n1;
5504 if (!empty1 && (n1 < 0 || n1 >= len))
5505 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005506 /* For a range we allow invalid values and return an empty
5507 * list. A list index out of range is an error. */
5508 if (!range)
5509 {
5510 if (verbose)
5511 EMSGN(_(e_listidx), n1);
5512 return FAIL;
5513 }
5514 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 }
5516 if (range)
5517 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005518 list_T *l;
5519 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520
5521 if (n2 < 0)
5522 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005523 else if (n2 >= len)
5524 n2 = len - 1;
5525 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005526 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 l = list_alloc();
5528 if (l == NULL)
5529 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 n1 <= n2; ++n1)
5532 {
5533 if (list_append_tv(l, &item->li_tv) == FAIL)
5534 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005535 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 return FAIL;
5537 }
5538 item = item->li_next;
5539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540 clear_tv(rettv);
5541 rettv->v_type = VAR_LIST;
5542 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005543 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 }
5545 else
5546 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005547 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 clear_tv(rettv);
5549 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 }
5551 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552
5553 case VAR_DICT:
5554 if (range)
5555 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005556 if (verbose)
5557 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005558 if (len == -1)
5559 clear_tv(&var1);
5560 return FAIL;
5561 }
5562 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005563 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005564
5565 if (len == -1)
5566 {
5567 key = get_tv_string(&var1);
5568 if (*key == NUL)
5569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005570 if (verbose)
5571 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572 clear_tv(&var1);
5573 return FAIL;
5574 }
5575 }
5576
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005577 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005579 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005580 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 if (len == -1)
5582 clear_tv(&var1);
5583 if (item == NULL)
5584 return FAIL;
5585
5586 copy_tv(&item->di_tv, &var1);
5587 clear_tv(rettv);
5588 *rettv = var1;
5589 }
5590 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 }
5592 }
5593
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 return OK;
5595}
5596
5597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 * Get an option value.
5599 * "arg" points to the '&' or '+' before the option name.
5600 * "arg" is advanced to character after the option name.
5601 * Return OK or FAIL.
5602 */
5603 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005606 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 int evaluate;
5608{
5609 char_u *option_end;
5610 long numval;
5611 char_u *stringval;
5612 int opt_type;
5613 int c;
5614 int working = (**arg == '+'); /* has("+option") */
5615 int ret = OK;
5616 int opt_flags;
5617
5618 /*
5619 * Isolate the option name and find its value.
5620 */
5621 option_end = find_option_end(arg, &opt_flags);
5622 if (option_end == NULL)
5623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 EMSG2(_("E112: Option name missing: %s"), *arg);
5626 return FAIL;
5627 }
5628
5629 if (!evaluate)
5630 {
5631 *arg = option_end;
5632 return OK;
5633 }
5634
5635 c = *option_end;
5636 *option_end = NUL;
5637 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005638 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
5640 if (opt_type == -3) /* invalid name */
5641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005642 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 EMSG2(_("E113: Unknown option: %s"), *arg);
5644 ret = FAIL;
5645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {
5648 if (opt_type == -2) /* hidden string option */
5649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005650 rettv->v_type = VAR_STRING;
5651 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 }
5653 else if (opt_type == -1) /* hidden number option */
5654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 rettv->v_type = VAR_NUMBER;
5656 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658 else if (opt_type == 1) /* number option */
5659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv->v_type = VAR_NUMBER;
5661 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663 else /* string option */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 rettv->v_type = VAR_STRING;
5666 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 }
5669 else if (working && (opt_type == -2 || opt_type == -1))
5670 ret = FAIL;
5671
5672 *option_end = c; /* put back for error messages */
5673 *arg = option_end;
5674
5675 return ret;
5676}
5677
5678/*
5679 * Allocate a variable for a string constant.
5680 * Return OK or FAIL.
5681 */
5682 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 int evaluate;
5687{
5688 char_u *p;
5689 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 int extra = 0;
5691
5692 /*
5693 * Find the end of the string, skipping backslashed characters.
5694 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005695 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
5697 if (*p == '\\' && p[1] != NUL)
5698 {
5699 ++p;
5700 /* A "\<x>" form occupies at least 4 characters, and produces up
5701 * to 6 characters: reserve space for 2 extra */
5702 if (*p == '<')
5703 extra += 2;
5704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706
5707 if (*p != '"')
5708 {
5709 EMSG2(_("E114: Missing quote: %s"), *arg);
5710 return FAIL;
5711 }
5712
5713 /* If only parsing, set *arg and return here */
5714 if (!evaluate)
5715 {
5716 *arg = p + 1;
5717 return OK;
5718 }
5719
5720 /*
5721 * Copy the string into allocated memory, handling backslashed
5722 * characters.
5723 */
5724 name = alloc((unsigned)(p - *arg + extra));
5725 if (name == NULL)
5726 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005727 rettv->v_type = VAR_STRING;
5728 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {
5732 if (*p == '\\')
5733 {
5734 switch (*++p)
5735 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 case 'b': *name++ = BS; ++p; break;
5737 case 'e': *name++ = ESC; ++p; break;
5738 case 'f': *name++ = FF; ++p; break;
5739 case 'n': *name++ = NL; ++p; break;
5740 case 'r': *name++ = CAR; ++p; break;
5741 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
5743 case 'X': /* hex: "\x1", "\x12" */
5744 case 'x':
5745 case 'u': /* Unicode: "\u0023" */
5746 case 'U':
5747 if (vim_isxdigit(p[1]))
5748 {
5749 int n, nr;
5750 int c = toupper(*p);
5751
5752 if (c == 'X')
5753 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005754 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005756 else
5757 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 nr = 0;
5759 while (--n >= 0 && vim_isxdigit(p[1]))
5760 {
5761 ++p;
5762 nr = (nr << 4) + hex2nr(*p);
5763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765#ifdef FEAT_MBYTE
5766 /* For "\u" store the number according to
5767 * 'encoding'. */
5768 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 else
5771#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 break;
5775
5776 /* octal: "\1", "\12", "\123" */
5777 case '0':
5778 case '1':
5779 case '2':
5780 case '3':
5781 case '4':
5782 case '5':
5783 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 case '7': *name = *p++ - '0';
5785 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 *name = (*name << 3) + *p++ - '0';
5788 if (*p >= '0' && *p <= '7')
5789 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 break;
5793
5794 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 if (extra != 0)
5797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 break;
5800 }
5801 /* FALLTHROUGH */
5802
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 break;
5805 }
5806 }
5807 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 *arg = p + 1;
5813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 return OK;
5815}
5816
5817/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 * Return OK or FAIL.
5820 */
5821 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005822get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 int evaluate;
5826{
5827 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005828 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005829 int reduce = 0;
5830
5831 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 */
5834 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5835 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 break;
5840 ++reduce;
5841 ++p;
5842 }
5843 }
5844
Bram Moolenaar8c711452005-01-14 21:53:12 +00005845 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 return FAIL;
5849 }
5850
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 if (!evaluate)
5853 {
5854 *arg = p + 1;
5855 return OK;
5856 }
5857
5858 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 */
5861 str = alloc((unsigned)((p - *arg) - reduce));
5862 if (str == NULL)
5863 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 rettv->v_type = VAR_STRING;
5865 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005866
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 break;
5873 ++p;
5874 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 *arg = p + 1;
5879
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 return OK;
5881}
5882
5883/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884 * Allocate a variable for a List and fill it from "*arg".
5885 * Return OK or FAIL.
5886 */
5887 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005888get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005890 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891 int evaluate;
5892{
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 list_T *l = NULL;
5894 typval_T tv;
5895 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896
5897 if (evaluate)
5898 {
5899 l = list_alloc();
5900 if (l == NULL)
5901 return FAIL;
5902 }
5903
5904 *arg = skipwhite(*arg + 1);
5905 while (**arg != ']' && **arg != NUL)
5906 {
5907 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5908 goto failret;
5909 if (evaluate)
5910 {
5911 item = listitem_alloc();
5912 if (item != NULL)
5913 {
5914 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005915 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 list_append(l, item);
5917 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005918 else
5919 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920 }
5921
5922 if (**arg == ']')
5923 break;
5924 if (**arg != ',')
5925 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 goto failret;
5928 }
5929 *arg = skipwhite(*arg + 1);
5930 }
5931
5932 if (**arg != ']')
5933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935failret:
5936 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005937 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 return FAIL;
5939 }
5940
5941 *arg = skipwhite(*arg + 1);
5942 if (evaluate)
5943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005944 rettv->v_type = VAR_LIST;
5945 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 ++l->lv_refcount;
5947 }
5948
5949 return OK;
5950}
5951
5952/*
5953 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005954 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005956 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957list_alloc()
5958{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005959 list_T *l;
5960
5961 l = (list_T *)alloc_clear(sizeof(list_T));
5962 if (l != NULL)
5963 {
5964 /* Prepend the list to the list of lists for garbage collection. */
5965 if (first_list != NULL)
5966 first_list->lv_used_prev = l;
5967 l->lv_used_prev = NULL;
5968 l->lv_used_next = first_list;
5969 first_list = l;
5970 }
5971 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972}
5973
5974/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005975 * Allocate an empty list for a return value.
5976 * Returns OK or FAIL.
5977 */
5978 static int
5979rettv_list_alloc(rettv)
5980 typval_T *rettv;
5981{
5982 list_T *l = list_alloc();
5983
5984 if (l == NULL)
5985 return FAIL;
5986
5987 rettv->vval.v_list = l;
5988 rettv->v_type = VAR_LIST;
5989 ++l->lv_refcount;
5990 return OK;
5991}
5992
5993/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 * Unreference a list: decrement the reference count and free it when it
5995 * becomes zero.
5996 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005997 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005999 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006001 if (l != NULL && --l->lv_refcount <= 0)
6002 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003}
6004
6005/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006006 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 * Ignores the reference count.
6008 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006009 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006010list_free(l, recurse)
6011 list_T *l;
6012 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013{
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006016 /* Remove the list from the list of lists for garbage collection. */
6017 if (l->lv_used_prev == NULL)
6018 first_list = l->lv_used_next;
6019 else
6020 l->lv_used_prev->lv_used_next = l->lv_used_next;
6021 if (l->lv_used_next != NULL)
6022 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6023
Bram Moolenaard9fba312005-06-26 22:34:35 +00006024 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006026 /* Remove the item before deleting it. */
6027 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006028 if (recurse || (item->li_tv.v_type != VAR_LIST
6029 && item->li_tv.v_type != VAR_DICT))
6030 clear_tv(&item->li_tv);
6031 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032 }
6033 vim_free(l);
6034}
6035
6036/*
6037 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006038 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006040 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041listitem_alloc()
6042{
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044}
6045
6046/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006047 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006049 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006051 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006053 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 vim_free(item);
6055}
6056
6057/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006058 * Remove a list item from a List and free it. Also clears the value.
6059 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006060 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006061listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 list_T *l;
6063 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006064{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006065 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006066 listitem_free(item);
6067}
6068
6069/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 * Get the number of items in a list.
6071 */
6072 static long
6073list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 if (l == NULL)
6077 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006078 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079}
6080
6081/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082 * Return TRUE when two lists have exactly the same values.
6083 */
6084 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006085list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 list_T *l1;
6087 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006088 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006089 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090{
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006093 if (l1 == NULL || l2 == NULL)
6094 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006095 if (l1 == l2)
6096 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006097 if (list_len(l1) != list_len(l2))
6098 return FALSE;
6099
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 for (item1 = l1->lv_first, item2 = l2->lv_first;
6101 item1 != NULL && item2 != NULL;
6102 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006103 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104 return FALSE;
6105 return item1 == NULL && item2 == NULL;
6106}
6107
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006108#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6109 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006110/*
6111 * Return the dictitem that an entry in a hashtable points to.
6112 */
6113 dictitem_T *
6114dict_lookup(hi)
6115 hashitem_T *hi;
6116{
6117 return HI2DI(hi);
6118}
6119#endif
6120
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006122 * Return TRUE when two dictionaries have exactly the same key/values.
6123 */
6124 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006125dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 dict_T *d1;
6127 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006128 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006130{
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 hashitem_T *hi;
6132 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006133 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006134
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006135 if (d1 == NULL || d2 == NULL)
6136 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006137 if (d1 == d2)
6138 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139 if (dict_len(d1) != dict_len(d2))
6140 return FALSE;
6141
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006142 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006145 if (!HASHITEM_EMPTY(hi))
6146 {
6147 item2 = dict_find(d2, hi->hi_key, -1);
6148 if (item2 == NULL)
6149 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006151 return FALSE;
6152 --todo;
6153 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 }
6155 return TRUE;
6156}
6157
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006158static int tv_equal_recurse_limit;
6159
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006160/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006161 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006162 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006163 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006164 */
6165 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006166tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 typval_T *tv1;
6168 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006169 int ic; /* ignore case */
6170 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171{
6172 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006173 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006174 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006175 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006177 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006178 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006180 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181 * recursiveness to a limit. We guess they are equal then.
6182 * A fixed limit has the problem of still taking an awful long time.
6183 * Reduce the limit every time running into it. That should work fine for
6184 * deeply linked structures that are not recursively linked and catch
6185 * recursiveness quickly. */
6186 if (!recursive)
6187 tv_equal_recurse_limit = 1000;
6188 if (recursive_cnt >= tv_equal_recurse_limit)
6189 {
6190 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006191 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006192 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193
6194 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006195 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006196 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006197 ++recursive_cnt;
6198 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6199 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006200 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006201
6202 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203 ++recursive_cnt;
6204 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6205 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006206 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006207
6208 case VAR_FUNC:
6209 return (tv1->vval.v_string != NULL
6210 && tv2->vval.v_string != NULL
6211 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6212
6213 case VAR_NUMBER:
6214 return tv1->vval.v_number == tv2->vval.v_number;
6215
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006216#ifdef FEAT_FLOAT
6217 case VAR_FLOAT:
6218 return tv1->vval.v_float == tv2->vval.v_float;
6219#endif
6220
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006221 case VAR_STRING:
6222 s1 = get_tv_string_buf(tv1, buf1);
6223 s2 = get_tv_string_buf(tv2, buf2);
6224 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006225 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006226
6227 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006228 return TRUE;
6229}
6230
6231/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006232 * Locate item with index "n" in list "l" and return it.
6233 * A negative index is counted from the end; -1 is the last item.
6234 * Returns NULL when "n" is out of range.
6235 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006236 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006237list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006238 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006239 long n;
6240{
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 long idx;
6243
6244 if (l == NULL)
6245 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006246
6247 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006249 n = l->lv_len + n;
6250
6251 /* Check for index out of range. */
6252 if (n < 0 || n >= l->lv_len)
6253 return NULL;
6254
6255 /* When there is a cached index may start search from there. */
6256 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006258 if (n < l->lv_idx / 2)
6259 {
6260 /* closest to the start of the list */
6261 item = l->lv_first;
6262 idx = 0;
6263 }
6264 else if (n > (l->lv_idx + l->lv_len) / 2)
6265 {
6266 /* closest to the end of the list */
6267 item = l->lv_last;
6268 idx = l->lv_len - 1;
6269 }
6270 else
6271 {
6272 /* closest to the cached index */
6273 item = l->lv_idx_item;
6274 idx = l->lv_idx;
6275 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006276 }
6277 else
6278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279 if (n < l->lv_len / 2)
6280 {
6281 /* closest to the start of the list */
6282 item = l->lv_first;
6283 idx = 0;
6284 }
6285 else
6286 {
6287 /* closest to the end of the list */
6288 item = l->lv_last;
6289 idx = l->lv_len - 1;
6290 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006292
6293 while (n > idx)
6294 {
6295 /* search forward */
6296 item = item->li_next;
6297 ++idx;
6298 }
6299 while (n < idx)
6300 {
6301 /* search backward */
6302 item = item->li_prev;
6303 --idx;
6304 }
6305
6306 /* cache the used index */
6307 l->lv_idx = idx;
6308 l->lv_idx_item = item;
6309
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 return item;
6311}
6312
6313/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006314 * Get list item "l[idx]" as a number.
6315 */
6316 static long
6317list_find_nr(l, idx, errorp)
6318 list_T *l;
6319 long idx;
6320 int *errorp; /* set to TRUE when something wrong */
6321{
6322 listitem_T *li;
6323
6324 li = list_find(l, idx);
6325 if (li == NULL)
6326 {
6327 if (errorp != NULL)
6328 *errorp = TRUE;
6329 return -1L;
6330 }
6331 return get_tv_number_chk(&li->li_tv, errorp);
6332}
6333
6334/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006335 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6336 */
6337 char_u *
6338list_find_str(l, idx)
6339 list_T *l;
6340 long idx;
6341{
6342 listitem_T *li;
6343
6344 li = list_find(l, idx - 1);
6345 if (li == NULL)
6346 {
6347 EMSGN(_(e_listidx), idx);
6348 return NULL;
6349 }
6350 return get_tv_string(&li->li_tv);
6351}
6352
6353/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006354 * Locate "item" list "l" and return its index.
6355 * Returns -1 when "item" is not in the list.
6356 */
6357 static long
6358list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006359 list_T *l;
6360 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006361{
6362 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006364
6365 if (l == NULL)
6366 return -1;
6367 idx = 0;
6368 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6369 ++idx;
6370 if (li == NULL)
6371 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006372 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006373}
6374
6375/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376 * Append item "item" to the end of list "l".
6377 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006378 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 list_T *l;
6381 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382{
6383 if (l->lv_last == NULL)
6384 {
6385 /* empty list */
6386 l->lv_first = item;
6387 l->lv_last = item;
6388 item->li_prev = NULL;
6389 }
6390 else
6391 {
6392 l->lv_last->li_next = item;
6393 item->li_prev = l->lv_last;
6394 l->lv_last = item;
6395 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006396 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 item->li_next = NULL;
6398}
6399
6400/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006404 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 list_T *l;
6407 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006409 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410
Bram Moolenaar05159a02005-02-26 23:04:13 +00006411 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006413 copy_tv(tv, &li->li_tv);
6414 list_append(l, li);
6415 return OK;
6416}
6417
6418/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006419 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006420 * Return FAIL when out of memory.
6421 */
6422 int
6423list_append_dict(list, dict)
6424 list_T *list;
6425 dict_T *dict;
6426{
6427 listitem_T *li = listitem_alloc();
6428
6429 if (li == NULL)
6430 return FAIL;
6431 li->li_tv.v_type = VAR_DICT;
6432 li->li_tv.v_lock = 0;
6433 li->li_tv.vval.v_dict = dict;
6434 list_append(list, li);
6435 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436 return OK;
6437}
6438
6439/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006440 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006441 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006442 * Returns FAIL when out of memory.
6443 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006444 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006445list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006446 list_T *l;
6447 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006448 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006449{
6450 listitem_T *li = listitem_alloc();
6451
6452 if (li == NULL)
6453 return FAIL;
6454 list_append(l, li);
6455 li->li_tv.v_type = VAR_STRING;
6456 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006457 if (str == NULL)
6458 li->li_tv.vval.v_string = NULL;
6459 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006460 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006461 return FAIL;
6462 return OK;
6463}
6464
6465/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006466 * Append "n" to list "l".
6467 * Returns FAIL when out of memory.
6468 */
6469 static int
6470list_append_number(l, n)
6471 list_T *l;
6472 varnumber_T n;
6473{
6474 listitem_T *li;
6475
6476 li = listitem_alloc();
6477 if (li == NULL)
6478 return FAIL;
6479 li->li_tv.v_type = VAR_NUMBER;
6480 li->li_tv.v_lock = 0;
6481 li->li_tv.vval.v_number = n;
6482 list_append(l, li);
6483 return OK;
6484}
6485
6486/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 * If "item" is NULL append at the end.
6489 * Return FAIL when out of memory.
6490 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006491 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *l;
6494 typval_T *tv;
6495 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496{
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498
6499 if (ni == NULL)
6500 return FAIL;
6501 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006502 list_insert(l, ni, item);
6503 return OK;
6504}
6505
6506 void
6507list_insert(l, ni, item)
6508 list_T *l;
6509 listitem_T *ni;
6510 listitem_T *item;
6511{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 if (item == NULL)
6513 /* Append new item at end of list. */
6514 list_append(l, ni);
6515 else
6516 {
6517 /* Insert new item before existing item. */
6518 ni->li_prev = item->li_prev;
6519 ni->li_next = item;
6520 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 ++l->lv_idx;
6524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 l->lv_idx_item = NULL;
6529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006531 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533}
6534
6535/*
6536 * Extend "l1" with "l2".
6537 * If "bef" is NULL append at the end, otherwise insert before this item.
6538 * Returns FAIL when out of memory.
6539 */
6540 static int
6541list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006542 list_T *l1;
6543 list_T *l2;
6544 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545{
Bram Moolenaar33570922005-01-25 22:26:29 +00006546 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006547 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006549 /* We also quit the loop when we have inserted the original item count of
6550 * the list, avoid a hang when we extend a list with itself. */
6551 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6553 return FAIL;
6554 return OK;
6555}
6556
6557/*
6558 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6559 * Return FAIL when out of memory.
6560 */
6561 static int
6562list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 list_T *l1;
6564 list_T *l2;
6565 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006569 if (l1 == NULL || l2 == NULL)
6570 return FAIL;
6571
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006572 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 if (l == NULL)
6575 return FAIL;
6576 tv->v_type = VAR_LIST;
6577 tv->vval.v_list = l;
6578
6579 /* append all items from the second list */
6580 return list_extend(l, l2, NULL);
6581}
6582
6583/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006584 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006586 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 * Returns NULL when out of memory.
6588 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006590list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006593 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594{
Bram Moolenaar33570922005-01-25 22:26:29 +00006595 list_T *copy;
6596 listitem_T *item;
6597 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598
6599 if (orig == NULL)
6600 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601
6602 copy = list_alloc();
6603 if (copy != NULL)
6604 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006605 if (copyID != 0)
6606 {
6607 /* Do this before adding the items, because one of the items may
6608 * refer back to this list. */
6609 orig->lv_copyID = copyID;
6610 orig->lv_copylist = copy;
6611 }
6612 for (item = orig->lv_first; item != NULL && !got_int;
6613 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 {
6615 ni = listitem_alloc();
6616 if (ni == NULL)
6617 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006618 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006619 {
6620 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6621 {
6622 vim_free(ni);
6623 break;
6624 }
6625 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006627 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 list_append(copy, ni);
6629 }
6630 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006631 if (item != NULL)
6632 {
6633 list_unref(copy);
6634 copy = NULL;
6635 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 }
6637
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 return copy;
6639}
6640
6641/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006642 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006643 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006644 * This used to be called list_remove, but that conflicts with a Sun header
6645 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006647 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006648vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006649 list_T *l;
6650 listitem_T *item;
6651 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652{
Bram Moolenaar33570922005-01-25 22:26:29 +00006653 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006655 /* notify watchers */
6656 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006658 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006659 list_fix_watch(l, ip);
6660 if (ip == item2)
6661 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663
6664 if (item2->li_next == NULL)
6665 l->lv_last = item->li_prev;
6666 else
6667 item2->li_next->li_prev = item->li_prev;
6668 if (item->li_prev == NULL)
6669 l->lv_first = item2->li_next;
6670 else
6671 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006672 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673}
6674
6675/*
6676 * Return an allocated string with the string representation of a list.
6677 * May return NULL.
6678 */
6679 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006680list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006681 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006682 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683{
6684 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006685
6686 if (tv->vval.v_list == NULL)
6687 return NULL;
6688 ga_init2(&ga, (int)sizeof(char), 80);
6689 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006690 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006691 {
6692 vim_free(ga.ga_data);
6693 return NULL;
6694 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695 ga_append(&ga, ']');
6696 ga_append(&ga, NUL);
6697 return (char_u *)ga.ga_data;
6698}
6699
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006700typedef struct join_S {
6701 char_u *s;
6702 char_u *tofree;
6703} join_T;
6704
6705 static int
6706list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6707 garray_T *gap; /* to store the result in */
6708 list_T *l;
6709 char_u *sep;
6710 int echo_style;
6711 int copyID;
6712 garray_T *join_gap; /* to keep each list item string */
6713{
6714 int i;
6715 join_T *p;
6716 int len;
6717 int sumlen = 0;
6718 int first = TRUE;
6719 char_u *tofree;
6720 char_u numbuf[NUMBUFLEN];
6721 listitem_T *item;
6722 char_u *s;
6723
6724 /* Stringify each item in the list. */
6725 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6726 {
6727 if (echo_style)
6728 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6729 else
6730 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6731 if (s == NULL)
6732 return FAIL;
6733
6734 len = (int)STRLEN(s);
6735 sumlen += len;
6736
6737 ga_grow(join_gap, 1);
6738 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6739 if (tofree != NULL || s != numbuf)
6740 {
6741 p->s = s;
6742 p->tofree = tofree;
6743 }
6744 else
6745 {
6746 p->s = vim_strnsave(s, len);
6747 p->tofree = p->s;
6748 }
6749
6750 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006751 if (did_echo_string_emsg) /* recursion error, bail out */
6752 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006753 }
6754
6755 /* Allocate result buffer with its total size, avoid re-allocation and
6756 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6757 if (join_gap->ga_len >= 2)
6758 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6759 if (ga_grow(gap, sumlen + 2) == FAIL)
6760 return FAIL;
6761
6762 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6763 {
6764 if (first)
6765 first = FALSE;
6766 else
6767 ga_concat(gap, sep);
6768 p = ((join_T *)join_gap->ga_data) + i;
6769
6770 if (p->s != NULL)
6771 ga_concat(gap, p->s);
6772 line_breakcheck();
6773 }
6774
6775 return OK;
6776}
6777
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006778/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006780 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006781 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006783 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006784list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006785 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006786 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006788 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006789 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791 garray_T join_ga;
6792 int retval;
6793 join_T *p;
6794 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795
Bram Moolenaard39a7512015-04-16 22:51:22 +02006796 if (l->lv_len < 1)
6797 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6799 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6800
6801 /* Dispose each item in join_ga. */
6802 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006804 p = (join_T *)join_ga.ga_data;
6805 for (i = 0; i < join_ga.ga_len; ++i)
6806 {
6807 vim_free(p->tofree);
6808 ++p;
6809 }
6810 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006812
6813 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814}
6815
6816/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006817 * Garbage collection for lists and dictionaries.
6818 *
6819 * We use reference counts to be able to free most items right away when they
6820 * are no longer used. But for composite items it's possible that it becomes
6821 * unused while the reference count is > 0: When there is a recursive
6822 * reference. Example:
6823 * :let l = [1, 2, 3]
6824 * :let d = {9: l}
6825 * :let l[1] = d
6826 *
6827 * Since this is quite unusual we handle this with garbage collection: every
6828 * once in a while find out which lists and dicts are not referenced from any
6829 * variable.
6830 *
6831 * Here is a good reference text about garbage collection (refers to Python
6832 * but it applies to all reference-counting mechanisms):
6833 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006834 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006835
6836/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 * Do garbage collection for lists and dicts.
6838 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 int
6841garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006842{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006843 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006844 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845 buf_T *buf;
6846 win_T *wp;
6847 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006848 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006849 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006850 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006851#ifdef FEAT_WINDOWS
6852 tabpage_T *tp;
6853#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006855 /* Only do this once. */
6856 want_garbage_collect = FALSE;
6857 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006858 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006859
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 /* We advance by two because we add one for items referenced through
6861 * previous_funccal. */
6862 current_copyID += COPYID_INC;
6863 copyID = current_copyID;
6864
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865 /*
6866 * 1. Go through all accessible variables and mark all lists and dicts
6867 * with copyID.
6868 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869
6870 /* Don't free variables in the previous_funccal list unless they are only
6871 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006872 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6874 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006875 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6876 NULL);
6877 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6878 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006879 }
6880
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881 /* script-local variables */
6882 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884
6885 /* buffer-local variables */
6886 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006887 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6888 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889
6890 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006891 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6893 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006894#ifdef FEAT_AUTOCMD
6895 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6897 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006898#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006900#ifdef FEAT_WINDOWS
6901 /* tabpage-local variables */
6902 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6904 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006905#endif
6906
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909
6910 /* function-local variables */
6911 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6912 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6914 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915 }
6916
Bram Moolenaard812df62008-11-09 12:46:09 +00006917 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006919
Bram Moolenaar1dced572012-04-05 16:54:08 +02006920#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006922#endif
6923
Bram Moolenaardb913952012-06-29 12:54:53 +02006924#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006926#endif
6927
6928#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006930#endif
6931
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 /*
6935 * 2. Free lists and dictionaries that are not referenced.
6936 */
6937 did_free = free_unref_items(copyID);
6938
6939 /*
6940 * 3. Check if any funccal can be freed now.
6941 */
6942 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 if (can_free_funccal(*pfc, copyID))
6945 {
6946 fc = *pfc;
6947 *pfc = fc->caller;
6948 free_funccal(fc, TRUE);
6949 did_free = TRUE;
6950 did_free_funccal = TRUE;
6951 }
6952 else
6953 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 if (did_free_funccal)
6956 /* When a funccal was freed some more items might be garbage
6957 * collected, so run again. */
6958 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006960 else if (p_verbose > 0)
6961 {
6962 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6963 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964
6965 return did_free;
6966}
6967
6968/*
6969 * Free lists and dictionaries that are no longer referenced.
6970 */
6971 static int
6972free_unref_items(copyID)
6973 int copyID;
6974{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006975 dict_T *dd, *dd_next;
6976 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 int did_free = FALSE;
6978
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006980 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 */
6982 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006983 {
6984 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006987 /* Free the Dictionary and ordinary items it contains, but don't
6988 * recurse into Lists and Dictionaries, they will be in the list
6989 * of dicts or list of lists. */
6990 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006993 dd = dd_next;
6994 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995
6996 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 * Go through the list of lists and free items without the copyID.
6998 * But don't free a list that has a watcher (used in a for loop), these
6999 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 */
7001 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007002 {
7003 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007004 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7005 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007007 /* Free the List and ordinary items it contains, but don't recurse
7008 * into Lists and Dictionaries, they will be in the list of dicts
7009 * or list of lists. */
7010 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007013 ll = ll_next;
7014 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 return did_free;
7016}
7017
7018/*
7019 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 * "list_stack" is used to add lists to be marked. Can be NULL.
7021 *
7022 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024 int
7025set_ref_in_ht(ht, copyID, list_stack)
7026 hashtab_T *ht;
7027 int copyID;
7028 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029{
7030 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 hashtab_T *cur_ht;
7034 ht_stack_T *ht_stack = NULL;
7035 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 cur_ht = ht;
7038 for (;;)
7039 {
7040 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 /* Mark each item in the hashtab. If the item contains a hashtab
7043 * it is added to ht_stack, if it contains a list it is added to
7044 * list_stack. */
7045 todo = (int)cur_ht->ht_used;
7046 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7047 if (!HASHITEM_EMPTY(hi))
7048 {
7049 --todo;
7050 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7051 &ht_stack, list_stack);
7052 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054
7055 if (ht_stack == NULL)
7056 break;
7057
7058 /* take an item from the stack */
7059 cur_ht = ht_stack->ht;
7060 tempitem = ht_stack;
7061 ht_stack = ht_stack->prev;
7062 free(tempitem);
7063 }
7064
7065 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066}
7067
7068/*
7069 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007070 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7071 *
7072 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074 int
7075set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076 list_T *l;
7077 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007078 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 listitem_T *li;
7081 int abort = FALSE;
7082 list_T *cur_l;
7083 list_stack_T *list_stack = NULL;
7084 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007086 cur_l = l;
7087 for (;;)
7088 {
7089 if (!abort)
7090 /* Mark each item in the list. If the item contains a hashtab
7091 * it is added to ht_stack, if it contains a list it is added to
7092 * list_stack. */
7093 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7094 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7095 ht_stack, &list_stack);
7096 if (list_stack == NULL)
7097 break;
7098
7099 /* take an item from the stack */
7100 cur_l = list_stack->list;
7101 tempitem = list_stack;
7102 list_stack = list_stack->prev;
7103 free(tempitem);
7104 }
7105
7106 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107}
7108
7109/*
7110 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007111 * "list_stack" is used to add lists to be marked. Can be NULL.
7112 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7113 *
7114 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 int
7117set_ref_in_item(tv, copyID, ht_stack, list_stack)
7118 typval_T *tv;
7119 int copyID;
7120 ht_stack_T **ht_stack;
7121 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007122{
7123 dict_T *dd;
7124 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007125 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007126
7127 switch (tv->v_type)
7128 {
7129 case VAR_DICT:
7130 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007131 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007132 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007133 /* Didn't see this dict yet. */
7134 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 if (ht_stack == NULL)
7136 {
7137 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7138 }
7139 else
7140 {
7141 ht_stack_T *newitem = (ht_stack_T*)malloc(
7142 sizeof(ht_stack_T));
7143 if (newitem == NULL)
7144 abort = TRUE;
7145 else
7146 {
7147 newitem->ht = &dd->dv_hashtab;
7148 newitem->prev = *ht_stack;
7149 *ht_stack = newitem;
7150 }
7151 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007152 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007153 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154
7155 case VAR_LIST:
7156 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007157 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007158 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007159 /* Didn't see this list yet. */
7160 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 if (list_stack == NULL)
7162 {
7163 abort = set_ref_in_list(ll, copyID, ht_stack);
7164 }
7165 else
7166 {
7167 list_stack_T *newitem = (list_stack_T*)malloc(
7168 sizeof(list_stack_T));
7169 if (newitem == NULL)
7170 abort = TRUE;
7171 else
7172 {
7173 newitem->list = ll;
7174 newitem->prev = *list_stack;
7175 *list_stack = newitem;
7176 }
7177 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007178 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007179 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007180 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007181 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007182}
7183
7184/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007185 * Allocate an empty header for a dictionary.
7186 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007187 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188dict_alloc()
7189{
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191
Bram Moolenaar33570922005-01-25 22:26:29 +00007192 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007194 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007195 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007196 if (first_dict != NULL)
7197 first_dict->dv_used_prev = d;
7198 d->dv_used_next = first_dict;
7199 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007200 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007201
Bram Moolenaar33570922005-01-25 22:26:29 +00007202 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007203 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007204 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007205 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007206 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007207 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209}
7210
7211/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007212 * Allocate an empty dict for a return value.
7213 * Returns OK or FAIL.
7214 */
7215 static int
7216rettv_dict_alloc(rettv)
7217 typval_T *rettv;
7218{
7219 dict_T *d = dict_alloc();
7220
7221 if (d == NULL)
7222 return FAIL;
7223
7224 rettv->vval.v_dict = d;
7225 rettv->v_type = VAR_DICT;
7226 ++d->dv_refcount;
7227 return OK;
7228}
7229
7230
7231/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 * Unreference a Dictionary: decrement the reference count and free it when it
7233 * becomes zero.
7234 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007235 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007239 if (d != NULL && --d->dv_refcount <= 0)
7240 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241}
7242
7243/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007244 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 * Ignores the reference count.
7246 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007247 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007248dict_free(d, recurse)
7249 dict_T *d;
7250 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007252 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007254 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007256 /* Remove the dict from the list of dicts for garbage collection. */
7257 if (d->dv_used_prev == NULL)
7258 first_dict = d->dv_used_next;
7259 else
7260 d->dv_used_prev->dv_used_next = d->dv_used_next;
7261 if (d->dv_used_next != NULL)
7262 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7263
7264 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007265 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007266 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 if (!HASHITEM_EMPTY(hi))
7270 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007271 /* Remove the item before deleting it, just in case there is
7272 * something recursive causing trouble. */
7273 di = HI2DI(hi);
7274 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007275 if (recurse || (di->di_tv.v_type != VAR_LIST
7276 && di->di_tv.v_type != VAR_DICT))
7277 clear_tv(&di->di_tv);
7278 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279 --todo;
7280 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283 vim_free(d);
7284}
7285
7286/*
7287 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 * The "key" is copied to the new item.
7289 * Note that the value of the item "di_tv" still needs to be initialized!
7290 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007292 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293dictitem_alloc(key)
7294 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295{
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007298 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007302 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007303 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305}
7306
7307/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308 * Make a copy of a Dictionary item.
7309 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007311dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007312 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313{
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007316 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7317 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 if (di != NULL)
7319 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007321 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322 copy_tv(&org->di_tv, &di->di_tv);
7323 }
7324 return di;
7325}
7326
7327/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 * Remove item "item" from Dictionary "dict" and free it.
7329 */
7330 static void
7331dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 dict_T *dict;
7333 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007334{
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338 if (HASHITEM_EMPTY(hi))
7339 EMSG2(_(e_intern2), "dictitem_remove()");
7340 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342 dictitem_free(item);
7343}
7344
7345/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346 * Free a dict item. Also clears the value.
7347 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007348 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007353 if (item->di_flags & DI_FLAGS_ALLOC)
7354 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355}
7356
7357/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7359 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007360 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361 * Returns NULL when out of memory.
7362 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007364dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007367 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368{
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 dict_T *copy;
7370 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007371 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007372 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373
7374 if (orig == NULL)
7375 return NULL;
7376
7377 copy = dict_alloc();
7378 if (copy != NULL)
7379 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007380 if (copyID != 0)
7381 {
7382 orig->dv_copyID = copyID;
7383 orig->dv_copydict = copy;
7384 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007385 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007386 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007388 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390 --todo;
7391
7392 di = dictitem_alloc(hi->hi_key);
7393 if (di == NULL)
7394 break;
7395 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007396 {
7397 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7398 copyID) == FAIL)
7399 {
7400 vim_free(di);
7401 break;
7402 }
7403 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404 else
7405 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7406 if (dict_add(copy, di) == FAIL)
7407 {
7408 dictitem_free(di);
7409 break;
7410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007413
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007415 if (todo > 0)
7416 {
7417 dict_unref(copy);
7418 copy = NULL;
7419 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007420 }
7421
7422 return copy;
7423}
7424
7425/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007427 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007429 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 dict_T *d;
7432 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433{
Bram Moolenaar33570922005-01-25 22:26:29 +00007434 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435}
7436
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007438 * Add a number or string entry to dictionary "d".
7439 * When "str" is NULL use number "nr", otherwise use "str".
7440 * Returns FAIL when out of memory and when key already exists.
7441 */
7442 int
7443dict_add_nr_str(d, key, nr, str)
7444 dict_T *d;
7445 char *key;
7446 long nr;
7447 char_u *str;
7448{
7449 dictitem_T *item;
7450
7451 item = dictitem_alloc((char_u *)key);
7452 if (item == NULL)
7453 return FAIL;
7454 item->di_tv.v_lock = 0;
7455 if (str == NULL)
7456 {
7457 item->di_tv.v_type = VAR_NUMBER;
7458 item->di_tv.vval.v_number = nr;
7459 }
7460 else
7461 {
7462 item->di_tv.v_type = VAR_STRING;
7463 item->di_tv.vval.v_string = vim_strsave(str);
7464 }
7465 if (dict_add(d, item) == FAIL)
7466 {
7467 dictitem_free(item);
7468 return FAIL;
7469 }
7470 return OK;
7471}
7472
7473/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007474 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007475 * Returns FAIL when out of memory and when key already exists.
7476 */
7477 int
7478dict_add_list(d, key, list)
7479 dict_T *d;
7480 char *key;
7481 list_T *list;
7482{
7483 dictitem_T *item;
7484
7485 item = dictitem_alloc((char_u *)key);
7486 if (item == NULL)
7487 return FAIL;
7488 item->di_tv.v_lock = 0;
7489 item->di_tv.v_type = VAR_LIST;
7490 item->di_tv.vval.v_list = list;
7491 if (dict_add(d, item) == FAIL)
7492 {
7493 dictitem_free(item);
7494 return FAIL;
7495 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007496 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007497 return OK;
7498}
7499
7500/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 * Get the number of items in a Dictionary.
7502 */
7503 static long
7504dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007505 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007506{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007507 if (d == NULL)
7508 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007509 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007510}
7511
7512/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 * Find item "key[len]" in Dictionary "d".
7514 * If "len" is negative use strlen(key).
7515 * Returns NULL when not found.
7516 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007517 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 char_u *key;
7521 int len;
7522{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523#define AKEYLEN 200
7524 char_u buf[AKEYLEN];
7525 char_u *akey;
7526 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529 if (len < 0)
7530 akey = key;
7531 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007532 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 tofree = akey = vim_strnsave(key, len);
7534 if (akey == NULL)
7535 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007536 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007537 else
7538 {
7539 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007540 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007541 akey = buf;
7542 }
7543
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545 vim_free(tofree);
7546 if (HASHITEM_EMPTY(hi))
7547 return NULL;
7548 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549}
7550
7551/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007552 * Get a string item from a dictionary.
7553 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007554 * Returns NULL if the entry doesn't exist or out of memory.
7555 */
7556 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007557get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007558 dict_T *d;
7559 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007560 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007561{
7562 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007563 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007564
7565 di = dict_find(d, key, -1);
7566 if (di == NULL)
7567 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007568 s = get_tv_string(&di->di_tv);
7569 if (save && s != NULL)
7570 s = vim_strsave(s);
7571 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007572}
7573
7574/*
7575 * Get a number item from a dictionary.
7576 * Returns 0 if the entry doesn't exist or out of memory.
7577 */
7578 long
7579get_dict_number(d, key)
7580 dict_T *d;
7581 char_u *key;
7582{
7583 dictitem_T *di;
7584
7585 di = dict_find(d, key, -1);
7586 if (di == NULL)
7587 return 0;
7588 return get_tv_number(&di->di_tv);
7589}
7590
7591/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 * Return an allocated string with the string representation of a Dictionary.
7593 * May return NULL.
7594 */
7595 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007596dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599{
7600 garray_T ga;
7601 int first = TRUE;
7602 char_u *tofree;
7603 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007604 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007607 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007609 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 return NULL;
7611 ga_init2(&ga, (int)sizeof(char), 80);
7612 ga_append(&ga, '{');
7613
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007614 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007615 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007617 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007619 --todo;
7620
7621 if (first)
7622 first = FALSE;
7623 else
7624 ga_concat(&ga, (char_u *)", ");
7625
7626 tofree = string_quote(hi->hi_key, FALSE);
7627 if (tofree != NULL)
7628 {
7629 ga_concat(&ga, tofree);
7630 vim_free(tofree);
7631 }
7632 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007633 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007634 if (s != NULL)
7635 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007637 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007638 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007639 line_breakcheck();
7640
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007642 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007643 if (todo > 0)
7644 {
7645 vim_free(ga.ga_data);
7646 return NULL;
7647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648
7649 ga_append(&ga, '}');
7650 ga_append(&ga, NUL);
7651 return (char_u *)ga.ga_data;
7652}
7653
7654/*
7655 * Allocate a variable for a Dictionary and fill it from "*arg".
7656 * Return OK or FAIL. Returns NOTDONE for {expr}.
7657 */
7658 static int
7659get_dict_tv(arg, rettv, evaluate)
7660 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 int evaluate;
7663{
Bram Moolenaar33570922005-01-25 22:26:29 +00007664 dict_T *d = NULL;
7665 typval_T tvkey;
7666 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007667 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007670 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671
7672 /*
7673 * First check if it's not a curly-braces thing: {expr}.
7674 * Must do this without evaluating, otherwise a function may be called
7675 * twice. Unfortunately this means we need to call eval1() twice for the
7676 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007679 if (*start != '}')
7680 {
7681 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7682 return FAIL;
7683 if (*start == '}')
7684 return NOTDONE;
7685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686
7687 if (evaluate)
7688 {
7689 d = dict_alloc();
7690 if (d == NULL)
7691 return FAIL;
7692 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007693 tvkey.v_type = VAR_UNKNOWN;
7694 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695
7696 *arg = skipwhite(*arg + 1);
7697 while (**arg != '}' && **arg != NUL)
7698 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 goto failret;
7701 if (**arg != ':')
7702 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007703 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007704 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 goto failret;
7706 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007707 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007709 key = get_tv_string_buf_chk(&tvkey, buf);
7710 if (key == NULL || *key == NUL)
7711 {
7712 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7713 if (key != NULL)
7714 EMSG(_(e_emptykey));
7715 clear_tv(&tvkey);
7716 goto failret;
7717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719
7720 *arg = skipwhite(*arg + 1);
7721 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7722 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007723 if (evaluate)
7724 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 goto failret;
7726 }
7727 if (evaluate)
7728 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007729 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 if (item != NULL)
7731 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007732 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007733 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734 clear_tv(&tv);
7735 goto failret;
7736 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007737 item = dictitem_alloc(key);
7738 clear_tv(&tvkey);
7739 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007741 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007742 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007743 if (dict_add(d, item) == FAIL)
7744 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 }
7746 }
7747
7748 if (**arg == '}')
7749 break;
7750 if (**arg != ',')
7751 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007752 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 goto failret;
7754 }
7755 *arg = skipwhite(*arg + 1);
7756 }
7757
7758 if (**arg != '}')
7759 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007760 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761failret:
7762 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007763 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764 return FAIL;
7765 }
7766
7767 *arg = skipwhite(*arg + 1);
7768 if (evaluate)
7769 {
7770 rettv->v_type = VAR_DICT;
7771 rettv->vval.v_dict = d;
7772 ++d->dv_refcount;
7773 }
7774
7775 return OK;
7776}
7777
Bram Moolenaar8c711452005-01-14 21:53:12 +00007778/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 * Return a string with the string representation of a variable.
7780 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007781 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007782 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007783 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007784 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007785 */
7786 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007787echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007788 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007790 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007791 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007792{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007793 static int recurse = 0;
7794 char_u *r = NULL;
7795
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007797 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007798 if (!did_echo_string_emsg)
7799 {
7800 /* Only give this message once for a recursive call to avoid
7801 * flooding the user with errors. And stop iterating over lists
7802 * and dicts. */
7803 did_echo_string_emsg = TRUE;
7804 EMSG(_("E724: variable nested too deep for displaying"));
7805 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007807 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007808 }
7809 ++recurse;
7810
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007811 switch (tv->v_type)
7812 {
7813 case VAR_FUNC:
7814 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007815 r = tv->vval.v_string;
7816 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007817
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007818 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007819 if (tv->vval.v_list == NULL)
7820 {
7821 *tofree = NULL;
7822 r = NULL;
7823 }
7824 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7825 {
7826 *tofree = NULL;
7827 r = (char_u *)"[...]";
7828 }
7829 else
7830 {
7831 tv->vval.v_list->lv_copyID = copyID;
7832 *tofree = list2string(tv, copyID);
7833 r = *tofree;
7834 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007835 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007836
Bram Moolenaar8c711452005-01-14 21:53:12 +00007837 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007838 if (tv->vval.v_dict == NULL)
7839 {
7840 *tofree = NULL;
7841 r = NULL;
7842 }
7843 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7844 {
7845 *tofree = NULL;
7846 r = (char_u *)"{...}";
7847 }
7848 else
7849 {
7850 tv->vval.v_dict->dv_copyID = copyID;
7851 *tofree = dict2string(tv, copyID);
7852 r = *tofree;
7853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007854 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007855
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007856 case VAR_STRING:
7857 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007858 *tofree = NULL;
7859 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007860 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007861
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007862#ifdef FEAT_FLOAT
7863 case VAR_FLOAT:
7864 *tofree = NULL;
7865 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7866 r = numbuf;
7867 break;
7868#endif
7869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007870 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007871 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007872 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874
Bram Moolenaar8502c702014-06-17 12:51:16 +02007875 if (--recurse == 0)
7876 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878}
7879
7880/*
7881 * Return a string with the string representation of a variable.
7882 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7883 * "numbuf" is used for a number.
7884 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007885 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007886 */
7887 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007888tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007889 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890 char_u **tofree;
7891 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007892 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893{
7894 switch (tv->v_type)
7895 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896 case VAR_FUNC:
7897 *tofree = string_quote(tv->vval.v_string, TRUE);
7898 return *tofree;
7899 case VAR_STRING:
7900 *tofree = string_quote(tv->vval.v_string, FALSE);
7901 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902#ifdef FEAT_FLOAT
7903 case VAR_FLOAT:
7904 *tofree = NULL;
7905 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7906 return numbuf;
7907#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007908 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007910 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007911 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007912 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007913 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007914 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007915 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007916}
7917
7918/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 * Return string "str" in ' quotes, doubling ' characters.
7920 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007921 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007922 */
7923 static char_u *
7924string_quote(str, function)
7925 char_u *str;
7926 int function;
7927{
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007929 char_u *p, *r, *s;
7930
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 len = (function ? 13 : 3);
7932 if (str != NULL)
7933 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007934 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007935 for (p = str; *p != NUL; mb_ptr_adv(p))
7936 if (*p == '\'')
7937 ++len;
7938 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939 s = r = alloc(len);
7940 if (r != NULL)
7941 {
7942 if (function)
7943 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007945 r += 10;
7946 }
7947 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 if (str != NULL)
7950 for (p = str; *p != NUL; )
7951 {
7952 if (*p == '\'')
7953 *r++ = '\'';
7954 MB_COPY_CHAR(p, r);
7955 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007957 if (function)
7958 *r++ = ')';
7959 *r++ = NUL;
7960 }
7961 return s;
7962}
7963
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007964#ifdef FEAT_FLOAT
7965/*
7966 * Convert the string "text" to a floating point number.
7967 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7968 * this always uses a decimal point.
7969 * Returns the length of the text that was consumed.
7970 */
7971 static int
7972string2float(text, value)
7973 char_u *text;
7974 float_T *value; /* result stored here */
7975{
7976 char *s = (char *)text;
7977 float_T f;
7978
7979 f = strtod(s, &s);
7980 *value = f;
7981 return (int)((char_u *)s - text);
7982}
7983#endif
7984
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 * Get the value of an environment variable.
7987 * "arg" is pointing to the '$'. It is advanced to after the name.
7988 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007989 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 */
7991 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007992get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 int evaluate;
7996{
7997 char_u *string = NULL;
7998 int len;
7999 int cc;
8000 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008001 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002
8003 ++*arg;
8004 name = *arg;
8005 len = get_env_len(arg);
8006 if (evaluate)
8007 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008008 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008009 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008010
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008011 cc = name[len];
8012 name[len] = NUL;
8013 /* first try vim_getenv(), fast for normal environment vars */
8014 string = vim_getenv(name, &mustfree);
8015 if (string != NULL && *string != NUL)
8016 {
8017 if (!mustfree)
8018 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008020 else
8021 {
8022 if (mustfree)
8023 vim_free(string);
8024
8025 /* next try expanding things like $VIM and ${HOME} */
8026 string = expand_env_save(name - 1);
8027 if (string != NULL && *string == '$')
8028 {
8029 vim_free(string);
8030 string = NULL;
8031 }
8032 }
8033 name[len] = cc;
8034
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008035 rettv->v_type = VAR_STRING;
8036 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 }
8038
8039 return OK;
8040}
8041
8042/*
8043 * Array with names and number of arguments of all internal functions
8044 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8045 */
8046static struct fst
8047{
8048 char *f_name; /* function name */
8049 char f_min_argc; /* minimal number of arguments */
8050 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008051 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008052 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053} functions[] =
8054{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055#ifdef FEAT_FLOAT
8056 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008057 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008058#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008059 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008060 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {"append", 2, 2, f_append},
8062 {"argc", 0, 0, f_argc},
8063 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008064 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008065 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008066#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008067 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008069 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008072 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"bufexists", 1, 1, f_bufexists},
8074 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8075 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8076 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8077 {"buflisted", 1, 1, f_buflisted},
8078 {"bufloaded", 1, 1, f_bufloaded},
8079 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008080 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {"bufwinnr", 1, 1, f_bufwinnr},
8082 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008083 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008084 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008085 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#ifdef FEAT_FLOAT
8087 {"ceil", 1, 1, f_ceil},
8088#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008089 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008090 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008092 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008094#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008095 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008096 {"complete_add", 1, 1, f_complete_add},
8097 {"complete_check", 0, 0, f_complete_check},
8098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008100 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008101#ifdef FEAT_FLOAT
8102 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008103 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008104#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008105 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008107 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008108 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"delete", 1, 1, f_delete},
8110 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008111 {"diff_filler", 1, 1, f_diff_filler},
8112 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008113 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008115 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {"eventhandler", 0, 0, f_eventhandler},
8117 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008118 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008120#ifdef FEAT_FLOAT
8121 {"exp", 1, 1, f_exp},
8122#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008123 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008124 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008125 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8127 {"filereadable", 1, 1, f_filereadable},
8128 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008129 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008130 {"finddir", 1, 3, f_finddir},
8131 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008132#ifdef FEAT_FLOAT
8133 {"float2nr", 1, 1, f_float2nr},
8134 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008135 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008136#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008137 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"fnamemodify", 2, 2, f_fnamemodify},
8139 {"foldclosed", 1, 1, f_foldclosed},
8140 {"foldclosedend", 1, 1, f_foldclosedend},
8141 {"foldlevel", 1, 1, f_foldlevel},
8142 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008143 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008146 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008147 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008148 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008149 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"getchar", 0, 1, f_getchar},
8151 {"getcharmod", 0, 0, f_getcharmod},
8152 {"getcmdline", 0, 0, f_getcmdline},
8153 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008154 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008155 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008156 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008158 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008159 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"getfsize", 1, 1, f_getfsize},
8161 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008162 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008163 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008164 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008165 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008166 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008167 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008168 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008169 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008171 {"gettabvar", 2, 3, f_gettabvar},
8172 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"getwinposx", 0, 0, f_getwinposx},
8174 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008175 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008176 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008177 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008178 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008180 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008181 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008182 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8184 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8185 {"histadd", 2, 2, f_histadd},
8186 {"histdel", 1, 2, f_histdel},
8187 {"histget", 1, 2, f_histget},
8188 {"histnr", 1, 1, f_histnr},
8189 {"hlID", 1, 1, f_hlID},
8190 {"hlexists", 1, 1, f_hlexists},
8191 {"hostname", 0, 0, f_hostname},
8192 {"iconv", 3, 3, f_iconv},
8193 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008194 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008195 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008197 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"inputrestore", 0, 0, f_inputrestore},
8199 {"inputsave", 0, 0, f_inputsave},
8200 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008201 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008202 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008204 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008205 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008206 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008207 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008209 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"libcall", 3, 3, f_libcall},
8211 {"libcallnr", 3, 3, f_libcallnr},
8212 {"line", 1, 1, f_line},
8213 {"line2byte", 1, 1, f_line2byte},
8214 {"lispindent", 1, 1, f_lispindent},
8215 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008216#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008217 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008218 {"log10", 1, 1, f_log10},
8219#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008220#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008221 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008222#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008223 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008224 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008225 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008226 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008227 {"matchadd", 2, 5, f_matchadd},
8228 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008229 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008230 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008231 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008232 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008233 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008234 {"max", 1, 1, f_max},
8235 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008236#ifdef vim_mkdir
8237 {"mkdir", 1, 3, f_mkdir},
8238#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008239 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008240#ifdef FEAT_MZSCHEME
8241 {"mzeval", 1, 1, f_mzeval},
8242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008244 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008245 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008246 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008247#ifdef FEAT_FLOAT
8248 {"pow", 2, 2, f_pow},
8249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008251 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008252 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008253#ifdef FEAT_PYTHON3
8254 {"py3eval", 1, 1, f_py3eval},
8255#endif
8256#ifdef FEAT_PYTHON
8257 {"pyeval", 1, 1, f_pyeval},
8258#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008259 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008260 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008261 {"reltime", 0, 2, f_reltime},
8262 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"remote_expr", 2, 3, f_remote_expr},
8264 {"remote_foreground", 1, 1, f_remote_foreground},
8265 {"remote_peek", 1, 2, f_remote_peek},
8266 {"remote_read", 1, 1, f_remote_read},
8267 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008268 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008270 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008272 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273#ifdef FEAT_FLOAT
8274 {"round", 1, 1, f_round},
8275#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008276 {"screenattr", 2, 2, f_screenattr},
8277 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008278 {"screencol", 0, 0, f_screencol},
8279 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008280 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008281 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008282 {"searchpair", 3, 7, f_searchpair},
8283 {"searchpairpos", 3, 7, f_searchpairpos},
8284 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {"server2client", 2, 2, f_server2client},
8286 {"serverlist", 0, 0, f_serverlist},
8287 {"setbufvar", 3, 3, f_setbufvar},
8288 {"setcmdpos", 1, 1, f_setcmdpos},
8289 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008290 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008291 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008292 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008293 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008295 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008296 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008298#ifdef FEAT_CRYPT
8299 {"sha256", 1, 1, f_sha256},
8300#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008301 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008302 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008304#ifdef FEAT_FLOAT
8305 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008306 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008307#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008308 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008309 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008310 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008311 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008312 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008313#ifdef FEAT_FLOAT
8314 {"sqrt", 1, 1, f_sqrt},
8315 {"str2float", 1, 1, f_str2float},
8316#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008317 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008318 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008319 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320#ifdef HAVE_STRFTIME
8321 {"strftime", 1, 2, f_strftime},
8322#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008323 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008324 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"strlen", 1, 1, f_strlen},
8326 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008327 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008329 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008330 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {"substitute", 4, 4, f_substitute},
8332 {"synID", 3, 3, f_synID},
8333 {"synIDattr", 2, 3, f_synIDattr},
8334 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008335 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008336 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008337 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008338 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008339 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008340 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008341 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008342 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008343 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008344#ifdef FEAT_FLOAT
8345 {"tan", 1, 1, f_tan},
8346 {"tanh", 1, 1, f_tanh},
8347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008349 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"tolower", 1, 1, f_tolower},
8351 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008352 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008353#ifdef FEAT_FLOAT
8354 {"trunc", 1, 1, f_trunc},
8355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008357 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008358 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008359 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008360 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"virtcol", 1, 1, f_virtcol},
8362 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008363 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"winbufnr", 1, 1, f_winbufnr},
8365 {"wincol", 0, 0, f_wincol},
8366 {"winheight", 1, 1, f_winheight},
8367 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008368 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008370 {"winrestview", 1, 1, f_winrestview},
8371 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008373 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008374 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375};
8376
8377#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8378
8379/*
8380 * Function given to ExpandGeneric() to obtain the list of internal
8381 * or user defined function names.
8382 */
8383 char_u *
8384get_function_name(xp, idx)
8385 expand_T *xp;
8386 int idx;
8387{
8388 static int intidx = -1;
8389 char_u *name;
8390
8391 if (idx == 0)
8392 intidx = -1;
8393 if (intidx < 0)
8394 {
8395 name = get_user_func_name(xp, idx);
8396 if (name != NULL)
8397 return name;
8398 }
8399 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8400 {
8401 STRCPY(IObuff, functions[intidx].f_name);
8402 STRCAT(IObuff, "(");
8403 if (functions[intidx].f_max_argc == 0)
8404 STRCAT(IObuff, ")");
8405 return IObuff;
8406 }
8407
8408 return NULL;
8409}
8410
8411/*
8412 * Function given to ExpandGeneric() to obtain the list of internal or
8413 * user defined variable or function names.
8414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 char_u *
8416get_expr_name(xp, idx)
8417 expand_T *xp;
8418 int idx;
8419{
8420 static int intidx = -1;
8421 char_u *name;
8422
8423 if (idx == 0)
8424 intidx = -1;
8425 if (intidx < 0)
8426 {
8427 name = get_function_name(xp, idx);
8428 if (name != NULL)
8429 return name;
8430 }
8431 return get_user_var_name(xp, ++intidx);
8432}
8433
8434#endif /* FEAT_CMDL_COMPL */
8435
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008436#if defined(EBCDIC) || defined(PROTO)
8437/*
8438 * Compare struct fst by function name.
8439 */
8440 static int
8441compare_func_name(s1, s2)
8442 const void *s1;
8443 const void *s2;
8444{
8445 struct fst *p1 = (struct fst *)s1;
8446 struct fst *p2 = (struct fst *)s2;
8447
8448 return STRCMP(p1->f_name, p2->f_name);
8449}
8450
8451/*
8452 * Sort the function table by function name.
8453 * The sorting of the table above is ASCII dependant.
8454 * On machines using EBCDIC we have to sort it.
8455 */
8456 static void
8457sortFunctions()
8458{
8459 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8460
8461 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8462}
8463#endif
8464
8465
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466/*
8467 * Find internal function in table above.
8468 * Return index, or -1 if not found
8469 */
8470 static int
8471find_internal_func(name)
8472 char_u *name; /* name of the function */
8473{
8474 int first = 0;
8475 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8476 int cmp;
8477 int x;
8478
8479 /*
8480 * Find the function name in the table. Binary search.
8481 */
8482 while (first <= last)
8483 {
8484 x = first + ((unsigned)(last - first) >> 1);
8485 cmp = STRCMP(name, functions[x].f_name);
8486 if (cmp < 0)
8487 last = x - 1;
8488 else if (cmp > 0)
8489 first = x + 1;
8490 else
8491 return x;
8492 }
8493 return -1;
8494}
8495
8496/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008497 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8498 * name it contains, otherwise return "name".
8499 */
8500 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008501deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008502 char_u *name;
8503 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008504 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008505{
Bram Moolenaar33570922005-01-25 22:26:29 +00008506 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008507 int cc;
8508
8509 cc = name[*lenp];
8510 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008511 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008512 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008514 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008515 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 {
8517 *lenp = 0;
8518 return (char_u *)""; /* just in case */
8519 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008520 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 }
8523
8524 return name;
8525}
8526
8527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 * Allocate a variable for the result of a function.
8529 * Return OK or FAIL.
8530 */
8531 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8533 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 char_u *name; /* name of the function */
8535 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 char_u **arg; /* argument, pointing to the '(' */
8538 linenr_T firstline; /* first line of range */
8539 linenr_T lastline; /* last line of range */
8540 int *doesrange; /* return: function handled range */
8541 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 char_u *argp;
8545 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008546 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 int argcount = 0; /* number of arguments found */
8548
8549 /*
8550 * Get the arguments.
8551 */
8552 argp = *arg;
8553 while (argcount < MAX_FUNC_ARGS)
8554 {
8555 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8556 if (*argp == ')' || *argp == ',' || *argp == NUL)
8557 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8559 {
8560 ret = FAIL;
8561 break;
8562 }
8563 ++argcount;
8564 if (*argp != ',')
8565 break;
8566 }
8567 if (*argp == ')')
8568 ++argp;
8569 else
8570 ret = FAIL;
8571
8572 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008574 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 {
8577 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008578 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008579 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008580 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
8583 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585
8586 *arg = skipwhite(argp);
8587 return ret;
8588}
8589
8590
8591/*
8592 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008593 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008594 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 */
8596 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008597call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008598 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008599 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008603 typval_T *argvars; /* vars for arguments, must have "argcount"
8604 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 linenr_T firstline; /* first line of range */
8606 linenr_T lastline; /* last line of range */
8607 int *doesrange; /* return: function handled range */
8608 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008609 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
8611 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612#define ERROR_UNKNOWN 0
8613#define ERROR_TOOMANY 1
8614#define ERROR_TOOFEW 2
8615#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008616#define ERROR_DICT 4
8617#define ERROR_NONE 5
8618#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 int error = ERROR_NONE;
8620 int i;
8621 int llen;
8622 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623#define FLEN_FIXED 40
8624 char_u fname_buf[FLEN_FIXED + 1];
8625 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008626 char_u *name;
8627
8628 /* Make a copy of the name, if it comes from a funcref variable it could
8629 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008630 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008631 if (name == NULL)
8632 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
8634 /*
8635 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8636 * Change <SNR>123_name() to K_SNR 123_name().
8637 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 llen = eval_fname_script(name);
8640 if (llen > 0)
8641 {
8642 fname_buf[0] = K_SPECIAL;
8643 fname_buf[1] = KS_EXTRA;
8644 fname_buf[2] = (int)KE_SNR;
8645 i = 3;
8646 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8647 {
8648 if (current_SID <= 0)
8649 error = ERROR_SCRIPT;
8650 else
8651 {
8652 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8653 i = (int)STRLEN(fname_buf);
8654 }
8655 }
8656 if (i + STRLEN(name + llen) < FLEN_FIXED)
8657 {
8658 STRCPY(fname_buf + i, name + llen);
8659 fname = fname_buf;
8660 }
8661 else
8662 {
8663 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8664 if (fname == NULL)
8665 error = ERROR_OTHER;
8666 else
8667 {
8668 mch_memmove(fname, fname_buf, (size_t)i);
8669 STRCPY(fname + i, name + llen);
8670 }
8671 }
8672 }
8673 else
8674 fname = name;
8675
8676 *doesrange = FALSE;
8677
8678
8679 /* execute the function if no errors detected and executing */
8680 if (evaluate && error == ERROR_NONE)
8681 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008682 char_u *rfname = fname;
8683
8684 /* Ignore "g:" before a function name. */
8685 if (fname[0] == 'g' && fname[1] == ':')
8686 rfname = fname + 2;
8687
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008688 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8689 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 error = ERROR_UNKNOWN;
8691
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008692 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 {
8694 /*
8695 * User defined function.
8696 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008697 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008698
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008700 /* Trigger FuncUndefined event, may load the function. */
8701 if (fp == NULL
8702 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008703 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008704 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008706 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008707 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 }
8709#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008710 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008711 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008712 {
8713 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008714 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008715 }
8716
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 if (fp != NULL)
8718 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008719 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008721 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008723 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008725 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008726 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 else
8728 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008729 int did_save_redo = FALSE;
8730
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 /*
8732 * Call the user function.
8733 * Save and restore search patterns, script variables and
8734 * redo buffer.
8735 */
8736 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008737 if (!ins_compl_active())
8738 {
8739 saveRedobuff();
8740 did_save_redo = TRUE;
8741 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008742 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008744 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008745 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8746 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8747 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008748 /* Function was unreferenced while being used, free it
8749 * now. */
8750 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008751 if (did_save_redo)
8752 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 restore_search_patterns();
8754 error = ERROR_NONE;
8755 }
8756 }
8757 }
8758 else
8759 {
8760 /*
8761 * Find the function name in the table, call its implementation.
8762 */
8763 i = find_internal_func(fname);
8764 if (i >= 0)
8765 {
8766 if (argcount < functions[i].f_min_argc)
8767 error = ERROR_TOOFEW;
8768 else if (argcount > functions[i].f_max_argc)
8769 error = ERROR_TOOMANY;
8770 else
8771 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008772 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 error = ERROR_NONE;
8775 }
8776 }
8777 }
8778 /*
8779 * The function call (or "FuncUndefined" autocommand sequence) might
8780 * have been aborted by an error, an interrupt, or an explicitly thrown
8781 * exception that has not been caught so far. This situation can be
8782 * tested for by calling aborting(). For an error in an internal
8783 * function or for the "E132" error in call_user_func(), however, the
8784 * throw point at which the "force_abort" flag (temporarily reset by
8785 * emsg()) is normally updated has not been reached yet. We need to
8786 * update that flag first to make aborting() reliable.
8787 */
8788 update_force_abort();
8789 }
8790 if (error == ERROR_NONE)
8791 ret = OK;
8792
8793 /*
8794 * Report an error unless the argument evaluation or function call has been
8795 * cancelled due to an aborting error, an interrupt, or an exception.
8796 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008797 if (!aborting())
8798 {
8799 switch (error)
8800 {
8801 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008802 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008803 break;
8804 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008805 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008806 break;
8807 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008808 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008809 name);
8810 break;
8811 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008812 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008813 name);
8814 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008815 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008816 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008817 name);
8818 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008819 }
8820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 if (fname != name && fname != fname_buf)
8823 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008824 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
8826 return ret;
8827}
8828
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008829/*
8830 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008831 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008832 */
8833 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008834emsg_funcname(ermsg, name)
8835 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008836 char_u *name;
8837{
8838 char_u *p;
8839
8840 if (*name == K_SPECIAL)
8841 p = concat_str((char_u *)"<SNR>", name + 3);
8842 else
8843 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008844 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008845 if (p != name)
8846 vim_free(p);
8847}
8848
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008849/*
8850 * Return TRUE for a non-zero Number and a non-empty String.
8851 */
8852 static int
8853non_zero_arg(argvars)
8854 typval_T *argvars;
8855{
8856 return ((argvars[0].v_type == VAR_NUMBER
8857 && argvars[0].vval.v_number != 0)
8858 || (argvars[0].v_type == VAR_STRING
8859 && argvars[0].vval.v_string != NULL
8860 && *argvars[0].vval.v_string != NUL));
8861}
8862
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863/*********************************************
8864 * Implementation of the built-in functions
8865 */
8866
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008867#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008868static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8869
8870/*
8871 * Get the float value of "argvars[0]" into "f".
8872 * Returns FAIL when the argument is not a Number or Float.
8873 */
8874 static int
8875get_float_arg(argvars, f)
8876 typval_T *argvars;
8877 float_T *f;
8878{
8879 if (argvars[0].v_type == VAR_FLOAT)
8880 {
8881 *f = argvars[0].vval.v_float;
8882 return OK;
8883 }
8884 if (argvars[0].v_type == VAR_NUMBER)
8885 {
8886 *f = (float_T)argvars[0].vval.v_number;
8887 return OK;
8888 }
8889 EMSG(_("E808: Number or Float required"));
8890 return FAIL;
8891}
8892
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008893/*
8894 * "abs(expr)" function
8895 */
8896 static void
8897f_abs(argvars, rettv)
8898 typval_T *argvars;
8899 typval_T *rettv;
8900{
8901 if (argvars[0].v_type == VAR_FLOAT)
8902 {
8903 rettv->v_type = VAR_FLOAT;
8904 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8905 }
8906 else
8907 {
8908 varnumber_T n;
8909 int error = FALSE;
8910
8911 n = get_tv_number_chk(&argvars[0], &error);
8912 if (error)
8913 rettv->vval.v_number = -1;
8914 else if (n > 0)
8915 rettv->vval.v_number = n;
8916 else
8917 rettv->vval.v_number = -n;
8918 }
8919}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008920
8921/*
8922 * "acos()" function
8923 */
8924 static void
8925f_acos(argvars, rettv)
8926 typval_T *argvars;
8927 typval_T *rettv;
8928{
8929 float_T f;
8930
8931 rettv->v_type = VAR_FLOAT;
8932 if (get_float_arg(argvars, &f) == OK)
8933 rettv->vval.v_float = acos(f);
8934 else
8935 rettv->vval.v_float = 0.0;
8936}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008937#endif
8938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008940 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 */
8942 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008943f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 typval_T *argvars;
8945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
Bram Moolenaar33570922005-01-25 22:26:29 +00008947 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008950 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008952 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008953 && !tv_check_lock(l->lv_lock,
8954 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008955 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008957 }
8958 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008959 EMSG(_(e_listreq));
8960}
8961
8962/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008963 * "and(expr, expr)" function
8964 */
8965 static void
8966f_and(argvars, rettv)
8967 typval_T *argvars;
8968 typval_T *rettv;
8969{
8970 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8971 & get_tv_number_chk(&argvars[1], NULL);
8972}
8973
8974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008975 * "append(lnum, string/list)" function
8976 */
8977 static void
8978f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008979 typval_T *argvars;
8980 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008981{
8982 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008984 list_T *l = NULL;
8985 listitem_T *li = NULL;
8986 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008987 long added = 0;
8988
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008989 /* When coming here from Insert mode, sync undo, so that this can be
8990 * undone separately from what was previously inserted. */
8991 if (u_sync_once == 2)
8992 {
8993 u_sync_once = 1; /* notify that u_sync() was called */
8994 u_sync(TRUE);
8995 }
8996
Bram Moolenaar0d660222005-01-07 21:51:51 +00008997 lnum = get_tv_lnum(argvars);
8998 if (lnum >= 0
8999 && lnum <= curbuf->b_ml.ml_line_count
9000 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009001 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009002 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009003 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009004 l = argvars[1].vval.v_list;
9005 if (l == NULL)
9006 return;
9007 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009008 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009009 for (;;)
9010 {
9011 if (l == NULL)
9012 tv = &argvars[1]; /* append a string */
9013 else if (li == NULL)
9014 break; /* end of list */
9015 else
9016 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009017 line = get_tv_string_chk(tv);
9018 if (line == NULL) /* type error */
9019 {
9020 rettv->vval.v_number = 1; /* Failed */
9021 break;
9022 }
9023 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009024 ++added;
9025 if (l == NULL)
9026 break;
9027 li = li->li_next;
9028 }
9029
9030 appended_lines_mark(lnum, added);
9031 if (curwin->w_cursor.lnum > lnum)
9032 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009034 else
9035 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036}
9037
9038/*
9039 * "argc()" function
9040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009043 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009046 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047}
9048
9049/*
9050 * "argidx()" function
9051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009053f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009054 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058}
9059
9060/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009061 * "arglistid()" function
9062 */
9063 static void
9064f_arglistid(argvars, rettv)
9065 typval_T *argvars UNUSED;
9066 typval_T *rettv;
9067{
9068 win_T *wp;
9069 tabpage_T *tp = NULL;
9070 long n;
9071
9072 rettv->vval.v_number = -1;
9073 if (argvars[0].v_type != VAR_UNKNOWN)
9074 {
9075 if (argvars[1].v_type != VAR_UNKNOWN)
9076 {
9077 n = get_tv_number(&argvars[1]);
9078 if (n >= 0)
9079 tp = find_tabpage(n);
9080 }
9081 else
9082 tp = curtab;
9083
9084 if (tp != NULL)
9085 {
9086 wp = find_win_by_nr(&argvars[0], tp);
9087 if (wp != NULL)
9088 rettv->vval.v_number = wp->w_alist->id;
9089 }
9090 }
9091 else
9092 rettv->vval.v_number = curwin->w_alist->id;
9093}
9094
9095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 * "argv(nr)" function
9097 */
9098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009100 typval_T *argvars;
9101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102{
9103 int idx;
9104
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009105 if (argvars[0].v_type != VAR_UNKNOWN)
9106 {
9107 idx = get_tv_number_chk(&argvars[0], NULL);
9108 if (idx >= 0 && idx < ARGCOUNT)
9109 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9110 else
9111 rettv->vval.v_string = NULL;
9112 rettv->v_type = VAR_STRING;
9113 }
9114 else if (rettv_list_alloc(rettv) == OK)
9115 for (idx = 0; idx < ARGCOUNT; ++idx)
9116 list_append_string(rettv->vval.v_list,
9117 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118}
9119
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009120#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009121/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009122 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009123 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009124 static void
9125f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009126 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009127 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009128{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009129 float_T f;
9130
9131 rettv->v_type = VAR_FLOAT;
9132 if (get_float_arg(argvars, &f) == OK)
9133 rettv->vval.v_float = asin(f);
9134 else
9135 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009136}
9137
9138/*
9139 * "atan()" function
9140 */
9141 static void
9142f_atan(argvars, rettv)
9143 typval_T *argvars;
9144 typval_T *rettv;
9145{
9146 float_T f;
9147
9148 rettv->v_type = VAR_FLOAT;
9149 if (get_float_arg(argvars, &f) == OK)
9150 rettv->vval.v_float = atan(f);
9151 else
9152 rettv->vval.v_float = 0.0;
9153}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009154
9155/*
9156 * "atan2()" function
9157 */
9158 static void
9159f_atan2(argvars, rettv)
9160 typval_T *argvars;
9161 typval_T *rettv;
9162{
9163 float_T fx, fy;
9164
9165 rettv->v_type = VAR_FLOAT;
9166 if (get_float_arg(argvars, &fx) == OK
9167 && get_float_arg(&argvars[1], &fy) == OK)
9168 rettv->vval.v_float = atan2(fx, fy);
9169 else
9170 rettv->vval.v_float = 0.0;
9171}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009172#endif
9173
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174/*
9175 * "browse(save, title, initdir, default)" function
9176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009179 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181{
9182#ifdef FEAT_BROWSE
9183 int save;
9184 char_u *title;
9185 char_u *initdir;
9186 char_u *defname;
9187 char_u buf[NUMBUFLEN];
9188 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009189 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009191 save = get_tv_number_chk(&argvars[0], &error);
9192 title = get_tv_string_chk(&argvars[1]);
9193 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9194 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 if (error || title == NULL || initdir == NULL || defname == NULL)
9197 rettv->vval.v_string = NULL;
9198 else
9199 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009200 do_browse(save ? BROWSE_SAVE : 0,
9201 title, defname, NULL, initdir, NULL, curbuf);
9202#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009204#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009205 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009206}
9207
9208/*
9209 * "browsedir(title, initdir)" function
9210 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009212f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009213 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009214 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009215{
9216#ifdef FEAT_BROWSE
9217 char_u *title;
9218 char_u *initdir;
9219 char_u buf[NUMBUFLEN];
9220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009221 title = get_tv_string_chk(&argvars[0]);
9222 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009224 if (title == NULL || initdir == NULL)
9225 rettv->vval.v_string = NULL;
9226 else
9227 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009228 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009230 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009232 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233}
9234
Bram Moolenaar33570922005-01-25 22:26:29 +00009235static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009236
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237/*
9238 * Find a buffer by number or exact name.
9239 */
9240 static buf_T *
9241find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009242 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243{
9244 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009246 if (avar->v_type == VAR_NUMBER)
9247 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009248 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009250 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009251 if (buf == NULL)
9252 {
9253 /* No full path name match, try a match with a URL or a "nofile"
9254 * buffer, these don't use the full path. */
9255 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9256 if (buf->b_fname != NULL
9257 && (path_with_url(buf->b_fname)
9258#ifdef FEAT_QUICKFIX
9259 || bt_nofile(buf)
9260#endif
9261 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009262 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009263 break;
9264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 }
9266 return buf;
9267}
9268
9269/*
9270 * "bufexists(expr)" function
9271 */
9272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009273f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009274 typval_T *argvars;
9275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278}
9279
9280/*
9281 * "buflisted(expr)" function
9282 */
9283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009285 typval_T *argvars;
9286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287{
9288 buf_T *buf;
9289
9290 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292}
9293
9294/*
9295 * "bufloaded(expr)" function
9296 */
9297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009298f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009299 typval_T *argvars;
9300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301{
9302 buf_T *buf;
9303
9304 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306}
9307
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009308static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009309
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310/*
9311 * Get buffer by number or pattern.
9312 */
9313 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009314get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009315 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009316 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009318 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 int save_magic;
9320 char_u *save_cpo;
9321 buf_T *buf;
9322
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323 if (tv->v_type == VAR_NUMBER)
9324 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009325 if (tv->v_type != VAR_STRING)
9326 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 if (name == NULL || *name == NUL)
9328 return curbuf;
9329 if (name[0] == '$' && name[1] == NUL)
9330 return lastbuf;
9331
9332 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9333 save_magic = p_magic;
9334 p_magic = TRUE;
9335 save_cpo = p_cpo;
9336 p_cpo = (char_u *)"";
9337
9338 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009339 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340
9341 p_magic = save_magic;
9342 p_cpo = save_cpo;
9343
9344 /* If not found, try expanding the name, like done for bufexists(). */
9345 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
9348 return buf;
9349}
9350
9351/*
9352 * "bufname(expr)" function
9353 */
9354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009356 typval_T *argvars;
9357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358{
9359 buf_T *buf;
9360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009361 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009363 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009366 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 --emsg_off;
9370}
9371
9372/*
9373 * "bufnr(expr)" function
9374 */
9375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009377 typval_T *argvars;
9378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379{
9380 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009381 int error = FALSE;
9382 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009384 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009386 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009387 --emsg_off;
9388
9389 /* If the buffer isn't found and the second argument is not zero create a
9390 * new buffer. */
9391 if (buf == NULL
9392 && argvars[1].v_type != VAR_UNKNOWN
9393 && get_tv_number_chk(&argvars[1], &error) != 0
9394 && !error
9395 && (name = get_tv_string_chk(&argvars[0])) != NULL
9396 && !error)
9397 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9398
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403}
9404
9405/*
9406 * "bufwinnr(nr)" function
9407 */
9408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009410 typval_T *argvars;
9411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412{
9413#ifdef FEAT_WINDOWS
9414 win_T *wp;
9415 int winnr = 0;
9416#endif
9417 buf_T *buf;
9418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009419 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009421 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422#ifdef FEAT_WINDOWS
9423 for (wp = firstwin; wp; wp = wp->w_next)
9424 {
9425 ++winnr;
9426 if (wp->w_buffer == buf)
9427 break;
9428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432#endif
9433 --emsg_off;
9434}
9435
9436/*
9437 * "byte2line(byte)" function
9438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009441 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443{
9444#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446#else
9447 long boff = 0;
9448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009449 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009453 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 (linenr_T)0, &boff);
9455#endif
9456}
9457
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009458 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009459byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009460 typval_T *argvars;
9461 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009462 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009463{
9464#ifdef FEAT_MBYTE
9465 char_u *t;
9466#endif
9467 char_u *str;
9468 long idx;
9469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 str = get_tv_string_chk(&argvars[0]);
9471 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009472 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009473 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009474 return;
9475
9476#ifdef FEAT_MBYTE
9477 t = str;
9478 for ( ; idx > 0; idx--)
9479 {
9480 if (*t == NUL) /* EOL reached */
9481 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009482 if (enc_utf8 && comp)
9483 t += utf_ptr2len(t);
9484 else
9485 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009486 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009487 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009488#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009489 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009491#endif
9492}
9493
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009494/*
9495 * "byteidx()" function
9496 */
9497 static void
9498f_byteidx(argvars, rettv)
9499 typval_T *argvars;
9500 typval_T *rettv;
9501{
9502 byteidx(argvars, rettv, FALSE);
9503}
9504
9505/*
9506 * "byteidxcomp()" function
9507 */
9508 static void
9509f_byteidxcomp(argvars, rettv)
9510 typval_T *argvars;
9511 typval_T *rettv;
9512{
9513 byteidx(argvars, rettv, TRUE);
9514}
9515
Bram Moolenaardb913952012-06-29 12:54:53 +02009516 int
9517func_call(name, args, selfdict, rettv)
9518 char_u *name;
9519 typval_T *args;
9520 dict_T *selfdict;
9521 typval_T *rettv;
9522{
9523 listitem_T *item;
9524 typval_T argv[MAX_FUNC_ARGS + 1];
9525 int argc = 0;
9526 int dummy;
9527 int r = 0;
9528
9529 for (item = args->vval.v_list->lv_first; item != NULL;
9530 item = item->li_next)
9531 {
9532 if (argc == MAX_FUNC_ARGS)
9533 {
9534 EMSG(_("E699: Too many arguments"));
9535 break;
9536 }
9537 /* Make a copy of each argument. This is needed to be able to set
9538 * v_lock to VAR_FIXED in the copy without changing the original list.
9539 */
9540 copy_tv(&item->li_tv, &argv[argc++]);
9541 }
9542
9543 if (item == NULL)
9544 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9545 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9546 &dummy, TRUE, selfdict);
9547
9548 /* Free the arguments. */
9549 while (argc > 0)
9550 clear_tv(&argv[--argc]);
9551
9552 return r;
9553}
9554
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009555/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009556 * "call(func, arglist)" function
9557 */
9558 static void
9559f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009560 typval_T *argvars;
9561 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009562{
9563 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009564 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009565
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009566 if (argvars[1].v_type != VAR_LIST)
9567 {
9568 EMSG(_(e_listreq));
9569 return;
9570 }
9571 if (argvars[1].vval.v_list == NULL)
9572 return;
9573
9574 if (argvars[0].v_type == VAR_FUNC)
9575 func = argvars[0].vval.v_string;
9576 else
9577 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009578 if (*func == NUL)
9579 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009580
Bram Moolenaare9a41262005-01-15 22:18:47 +00009581 if (argvars[2].v_type != VAR_UNKNOWN)
9582 {
9583 if (argvars[2].v_type != VAR_DICT)
9584 {
9585 EMSG(_(e_dictreq));
9586 return;
9587 }
9588 selfdict = argvars[2].vval.v_dict;
9589 }
9590
Bram Moolenaardb913952012-06-29 12:54:53 +02009591 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009592}
9593
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009594#ifdef FEAT_FLOAT
9595/*
9596 * "ceil({float})" function
9597 */
9598 static void
9599f_ceil(argvars, rettv)
9600 typval_T *argvars;
9601 typval_T *rettv;
9602{
9603 float_T f;
9604
9605 rettv->v_type = VAR_FLOAT;
9606 if (get_float_arg(argvars, &f) == OK)
9607 rettv->vval.v_float = ceil(f);
9608 else
9609 rettv->vval.v_float = 0.0;
9610}
9611#endif
9612
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009613/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009614 * "changenr()" function
9615 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009616 static void
9617f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009618 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009619 typval_T *rettv;
9620{
9621 rettv->vval.v_number = curbuf->b_u_seq_cur;
9622}
9623
9624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 * "char2nr(string)" function
9626 */
9627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009629 typval_T *argvars;
9630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631{
9632#ifdef FEAT_MBYTE
9633 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009634 {
9635 int utf8 = 0;
9636
9637 if (argvars[1].v_type != VAR_UNKNOWN)
9638 utf8 = get_tv_number_chk(&argvars[1], NULL);
9639
9640 if (utf8)
9641 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9642 else
9643 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 else
9646#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648}
9649
9650/*
9651 * "cindent(lnum)" function
9652 */
9653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009654f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009655 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
9658#ifdef FEAT_CINDENT
9659 pos_T pos;
9660 linenr_T lnum;
9661
9662 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9665 {
9666 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009667 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 curwin->w_cursor = pos;
9669 }
9670 else
9671#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673}
9674
9675/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009676 * "clearmatches()" function
9677 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009678 static void
9679f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009680 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009681 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009682{
9683#ifdef FEAT_SEARCH_EXTRA
9684 clear_matches(curwin);
9685#endif
9686}
9687
9688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 * "col(string)" function
9690 */
9691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009693 typval_T *argvars;
9694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695{
9696 colnr_T col = 0;
9697 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009698 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009700 fp = var2fpos(&argvars[0], FALSE, &fnum);
9701 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 {
9703 if (fp->col == MAXCOL)
9704 {
9705 /* '> can be MAXCOL, get the length of the line then */
9706 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009707 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 else
9709 col = MAXCOL;
9710 }
9711 else
9712 {
9713 col = fp->col + 1;
9714#ifdef FEAT_VIRTUALEDIT
9715 /* col(".") when the cursor is on the NUL at the end of the line
9716 * because of "coladd" can be seen as an extra column. */
9717 if (virtual_active() && fp == &curwin->w_cursor)
9718 {
9719 char_u *p = ml_get_cursor();
9720
9721 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9722 curwin->w_virtcol - curwin->w_cursor.coladd))
9723 {
9724# ifdef FEAT_MBYTE
9725 int l;
9726
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009727 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 col += l;
9729# else
9730 if (*p != NUL && p[1] == NUL)
9731 ++col;
9732# endif
9733 }
9734 }
9735#endif
9736 }
9737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739}
9740
Bram Moolenaar572cb562005-08-05 21:35:02 +00009741#if defined(FEAT_INS_EXPAND)
9742/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009743 * "complete()" function
9744 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009745 static void
9746f_complete(argvars, rettv)
9747 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009748 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009749{
9750 int startcol;
9751
9752 if ((State & INSERT) == 0)
9753 {
9754 EMSG(_("E785: complete() can only be used in Insert mode"));
9755 return;
9756 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009757
9758 /* Check for undo allowed here, because if something was already inserted
9759 * the line was already saved for undo and this check isn't done. */
9760 if (!undo_allowed())
9761 return;
9762
Bram Moolenaarade00832006-03-10 21:46:58 +00009763 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9764 {
9765 EMSG(_(e_invarg));
9766 return;
9767 }
9768
9769 startcol = get_tv_number_chk(&argvars[0], NULL);
9770 if (startcol <= 0)
9771 return;
9772
9773 set_completion(startcol - 1, argvars[1].vval.v_list);
9774}
9775
9776/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009777 * "complete_add()" function
9778 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009779 static void
9780f_complete_add(argvars, rettv)
9781 typval_T *argvars;
9782 typval_T *rettv;
9783{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009784 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009785}
9786
9787/*
9788 * "complete_check()" function
9789 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009790 static void
9791f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009792 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009793 typval_T *rettv;
9794{
9795 int saved = RedrawingDisabled;
9796
9797 RedrawingDisabled = 0;
9798 ins_compl_check_keys(0);
9799 rettv->vval.v_number = compl_interrupted;
9800 RedrawingDisabled = saved;
9801}
9802#endif
9803
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804/*
9805 * "confirm(message, buttons[, default [, type]])" function
9806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009809 typval_T *argvars UNUSED;
9810 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811{
9812#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9813 char_u *message;
9814 char_u *buttons = NULL;
9815 char_u buf[NUMBUFLEN];
9816 char_u buf2[NUMBUFLEN];
9817 int def = 1;
9818 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009819 char_u *typestr;
9820 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009822 message = get_tv_string_chk(&argvars[0]);
9823 if (message == NULL)
9824 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009825 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009827 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9828 if (buttons == NULL)
9829 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009830 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009832 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009833 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009835 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9836 if (typestr == NULL)
9837 error = TRUE;
9838 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 switch (TOUPPER_ASC(*typestr))
9841 {
9842 case 'E': type = VIM_ERROR; break;
9843 case 'Q': type = VIM_QUESTION; break;
9844 case 'I': type = VIM_INFO; break;
9845 case 'W': type = VIM_WARNING; break;
9846 case 'G': type = VIM_GENERIC; break;
9847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 }
9849 }
9850 }
9851 }
9852
9853 if (buttons == NULL || *buttons == NUL)
9854 buttons = (char_u *)_("&Ok");
9855
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009856 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009858 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859#endif
9860}
9861
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009862/*
9863 * "copy()" function
9864 */
9865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009867 typval_T *argvars;
9868 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009869{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009870 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009871}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009873#ifdef FEAT_FLOAT
9874/*
9875 * "cos()" function
9876 */
9877 static void
9878f_cos(argvars, rettv)
9879 typval_T *argvars;
9880 typval_T *rettv;
9881{
9882 float_T f;
9883
9884 rettv->v_type = VAR_FLOAT;
9885 if (get_float_arg(argvars, &f) == OK)
9886 rettv->vval.v_float = cos(f);
9887 else
9888 rettv->vval.v_float = 0.0;
9889}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009890
9891/*
9892 * "cosh()" function
9893 */
9894 static void
9895f_cosh(argvars, rettv)
9896 typval_T *argvars;
9897 typval_T *rettv;
9898{
9899 float_T f;
9900
9901 rettv->v_type = VAR_FLOAT;
9902 if (get_float_arg(argvars, &f) == OK)
9903 rettv->vval.v_float = cosh(f);
9904 else
9905 rettv->vval.v_float = 0.0;
9906}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009907#endif
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009910 * "count()" function
9911 */
9912 static void
9913f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009914 typval_T *argvars;
9915 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009916{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009917 long n = 0;
9918 int ic = FALSE;
9919
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009921 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 listitem_T *li;
9923 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009924 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009925
Bram Moolenaare9a41262005-01-15 22:18:47 +00009926 if ((l = argvars[0].vval.v_list) != NULL)
9927 {
9928 li = l->lv_first;
9929 if (argvars[2].v_type != VAR_UNKNOWN)
9930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009931 int error = FALSE;
9932
9933 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009934 if (argvars[3].v_type != VAR_UNKNOWN)
9935 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009936 idx = get_tv_number_chk(&argvars[3], &error);
9937 if (!error)
9938 {
9939 li = list_find(l, idx);
9940 if (li == NULL)
9941 EMSGN(_(e_listidx), idx);
9942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009943 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009944 if (error)
9945 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009946 }
9947
9948 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009949 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009950 ++n;
9951 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009952 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009953 else if (argvars[0].v_type == VAR_DICT)
9954 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009955 int todo;
9956 dict_T *d;
9957 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009958
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009959 if ((d = argvars[0].vval.v_dict) != NULL)
9960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009961 int error = FALSE;
9962
Bram Moolenaare9a41262005-01-15 22:18:47 +00009963 if (argvars[2].v_type != VAR_UNKNOWN)
9964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009966 if (argvars[3].v_type != VAR_UNKNOWN)
9967 EMSG(_(e_invarg));
9968 }
9969
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009970 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009971 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009972 {
9973 if (!HASHITEM_EMPTY(hi))
9974 {
9975 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009976 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009977 ++n;
9978 }
9979 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009980 }
9981 }
9982 else
9983 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009984 rettv->vval.v_number = n;
9985}
9986
9987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9989 *
9990 * Checks the existence of a cscope connection.
9991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009993f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009994 typval_T *argvars UNUSED;
9995 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996{
9997#ifdef FEAT_CSCOPE
9998 int num = 0;
9999 char_u *dbpath = NULL;
10000 char_u *prepend = NULL;
10001 char_u buf[NUMBUFLEN];
10002
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010003 if (argvars[0].v_type != VAR_UNKNOWN
10004 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 num = (int)get_tv_number(&argvars[0]);
10007 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010008 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 }
10011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013#endif
10014}
10015
10016/*
10017 * "cursor(lnum, col)" function
10018 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010019 * Moves the cursor to the specified line and column.
10020 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010024 typval_T *argvars;
10025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
10027 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010028#ifdef FEAT_VIRTUALEDIT
10029 long coladd = 0;
10030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010032 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010033 if (argvars[1].v_type == VAR_UNKNOWN)
10034 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010035 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010036 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010037
Bram Moolenaar493c1782014-05-28 14:34:46 +020010038 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010039 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010040 line = pos.lnum;
10041 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010042#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010043 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010044#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010045 if (curswant >= 0)
10046 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010047 }
10048 else
10049 {
10050 line = get_tv_lnum(argvars);
10051 col = get_tv_number_chk(&argvars[1], NULL);
10052#ifdef FEAT_VIRTUALEDIT
10053 if (argvars[2].v_type != VAR_UNKNOWN)
10054 coladd = get_tv_number_chk(&argvars[2], NULL);
10055#endif
10056 }
10057 if (line < 0 || col < 0
10058#ifdef FEAT_VIRTUALEDIT
10059 || coladd < 0
10060#endif
10061 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010062 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if (line > 0)
10064 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 if (col > 0)
10066 curwin->w_cursor.col = col - 1;
10067#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010068 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069#endif
10070
10071 /* Make sure the cursor is in a valid position. */
10072 check_cursor();
10073#ifdef FEAT_MBYTE
10074 /* Correct cursor for multi-byte character. */
10075 if (has_mbyte)
10076 mb_adjust_cursor();
10077#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010078
10079 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010080 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081}
10082
10083/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010084 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 */
10086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 typval_T *argvars;
10089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010091 int noref = 0;
10092
10093 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010094 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010095 if (noref < 0 || noref > 1)
10096 EMSG(_(e_invarg));
10097 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010098 {
10099 current_copyID += COPYID_INC;
10100 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102}
10103
10104/*
10105 * "delete()" function
10106 */
10107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010109 typval_T *argvars;
10110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
10112 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116}
10117
10118/*
10119 * "did_filetype()" function
10120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010123 typval_T *argvars UNUSED;
10124 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125{
10126#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128#endif
10129}
10130
10131/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010132 * "diff_filler()" function
10133 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010136 typval_T *argvars UNUSED;
10137 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010138{
10139#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010141#endif
10142}
10143
10144/*
10145 * "diff_hlID()" function
10146 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010149 typval_T *argvars UNUSED;
10150 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010151{
10152#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010154 static linenr_T prev_lnum = 0;
10155 static int changedtick = 0;
10156 static int fnum = 0;
10157 static int change_start = 0;
10158 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010159 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010160 int filler_lines;
10161 int col;
10162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010163 if (lnum < 0) /* ignore type error in {lnum} arg */
10164 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010165 if (lnum != prev_lnum
10166 || changedtick != curbuf->b_changedtick
10167 || fnum != curbuf->b_fnum)
10168 {
10169 /* New line, buffer, change: need to get the values. */
10170 filler_lines = diff_check(curwin, lnum);
10171 if (filler_lines < 0)
10172 {
10173 if (filler_lines == -1)
10174 {
10175 change_start = MAXCOL;
10176 change_end = -1;
10177 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10178 hlID = HLF_ADD; /* added line */
10179 else
10180 hlID = HLF_CHD; /* changed line */
10181 }
10182 else
10183 hlID = HLF_ADD; /* added line */
10184 }
10185 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010186 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010187 prev_lnum = lnum;
10188 changedtick = curbuf->b_changedtick;
10189 fnum = curbuf->b_fnum;
10190 }
10191
10192 if (hlID == HLF_CHD || hlID == HLF_TXD)
10193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010195 if (col >= change_start && col <= change_end)
10196 hlID = HLF_TXD; /* changed text */
10197 else
10198 hlID = HLF_CHD; /* changed line */
10199 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010200 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010201#endif
10202}
10203
10204/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010205 * "empty({expr})" function
10206 */
10207 static void
10208f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010209 typval_T *argvars;
10210 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010211{
10212 int n;
10213
10214 switch (argvars[0].v_type)
10215 {
10216 case VAR_STRING:
10217 case VAR_FUNC:
10218 n = argvars[0].vval.v_string == NULL
10219 || *argvars[0].vval.v_string == NUL;
10220 break;
10221 case VAR_NUMBER:
10222 n = argvars[0].vval.v_number == 0;
10223 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010224#ifdef FEAT_FLOAT
10225 case VAR_FLOAT:
10226 n = argvars[0].vval.v_float == 0.0;
10227 break;
10228#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010229 case VAR_LIST:
10230 n = argvars[0].vval.v_list == NULL
10231 || argvars[0].vval.v_list->lv_first == NULL;
10232 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010233 case VAR_DICT:
10234 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010235 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010237 default:
10238 EMSG2(_(e_intern2), "f_empty()");
10239 n = 0;
10240 }
10241
10242 rettv->vval.v_number = n;
10243}
10244
10245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 * "escape({string}, {chars})" function
10247 */
10248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010250 typval_T *argvars;
10251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252{
10253 char_u buf[NUMBUFLEN];
10254
Bram Moolenaar758711c2005-02-02 23:11:38 +000010255 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10256 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258}
10259
10260/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010261 * "eval()" function
10262 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010263 static void
10264f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010265 typval_T *argvars;
10266 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010267{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010268 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010270 s = get_tv_string_chk(&argvars[0]);
10271 if (s != NULL)
10272 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010273
Bram Moolenaar615b9972015-01-14 17:15:05 +010010274 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010275 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10276 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010277 if (p != NULL && !aborting())
10278 EMSG2(_(e_invexpr2), p);
10279 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010280 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010281 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010282 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010283 else if (*s != NUL)
10284 EMSG(_(e_trailing));
10285}
10286
10287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 * "eventhandler()" function
10289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010291f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010292 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296}
10297
10298/*
10299 * "executable()" function
10300 */
10301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010303 typval_T *argvars;
10304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010306 char_u *name = get_tv_string(&argvars[0]);
10307
10308 /* Check in $PATH and also check directly if there is a directory name. */
10309 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10310 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010311}
10312
10313/*
10314 * "exepath()" function
10315 */
10316 static void
10317f_exepath(argvars, rettv)
10318 typval_T *argvars;
10319 typval_T *rettv;
10320{
10321 char_u *p = NULL;
10322
Bram Moolenaarb5971142015-03-21 17:32:19 +010010323 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010324 rettv->v_type = VAR_STRING;
10325 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326}
10327
10328/*
10329 * "exists()" function
10330 */
10331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010332f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010333 typval_T *argvars;
10334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335{
10336 char_u *p;
10337 char_u *name;
10338 int n = FALSE;
10339 int len = 0;
10340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010341 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 if (*p == '$') /* environment variable */
10343 {
10344 /* first try "normal" environment variables (fast) */
10345 if (mch_getenv(p + 1) != NULL)
10346 n = TRUE;
10347 else
10348 {
10349 /* try expanding things like $VIM and ${HOME} */
10350 p = expand_env_save(p);
10351 if (p != NULL && *p != '$')
10352 n = TRUE;
10353 vim_free(p);
10354 }
10355 }
10356 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010358 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010359 if (*skipwhite(p) != NUL)
10360 n = FALSE; /* trailing garbage */
10361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 else if (*p == '*') /* internal or user defined function */
10363 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010364 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 }
10366 else if (*p == ':')
10367 {
10368 n = cmd_exists(p + 1);
10369 }
10370 else if (*p == '#')
10371 {
10372#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010373 if (p[1] == '#')
10374 n = autocmd_supported(p + 2);
10375 else
10376 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377#endif
10378 }
10379 else /* internal variable */
10380 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010381 char_u *tofree;
10382 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010384 /* get_name_len() takes care of expanding curly braces */
10385 name = p;
10386 len = get_name_len(&p, &tofree, TRUE, FALSE);
10387 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010389 if (tofree != NULL)
10390 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010391 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010392 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010394 /* handle d.key, l[idx], f(expr) */
10395 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10396 if (n)
10397 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 }
10399 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010400 if (*p != NUL)
10401 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010403 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 }
10405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010406 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407}
10408
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010409#ifdef FEAT_FLOAT
10410/*
10411 * "exp()" function
10412 */
10413 static void
10414f_exp(argvars, rettv)
10415 typval_T *argvars;
10416 typval_T *rettv;
10417{
10418 float_T f;
10419
10420 rettv->v_type = VAR_FLOAT;
10421 if (get_float_arg(argvars, &f) == OK)
10422 rettv->vval.v_float = exp(f);
10423 else
10424 rettv->vval.v_float = 0.0;
10425}
10426#endif
10427
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428/*
10429 * "expand()" function
10430 */
10431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010432f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010433 typval_T *argvars;
10434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435{
10436 char_u *s;
10437 int len;
10438 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010439 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010441 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010442 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010445 if (argvars[1].v_type != VAR_UNKNOWN
10446 && argvars[2].v_type != VAR_UNKNOWN
10447 && get_tv_number_chk(&argvars[2], &error)
10448 && !error)
10449 {
10450 rettv->v_type = VAR_LIST;
10451 rettv->vval.v_list = NULL;
10452 }
10453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 if (*s == '%' || *s == '#' || *s == '<')
10456 {
10457 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010458 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010460 if (rettv->v_type == VAR_LIST)
10461 {
10462 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10463 list_append_string(rettv->vval.v_list, result, -1);
10464 else
10465 vim_free(result);
10466 }
10467 else
10468 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 }
10470 else
10471 {
10472 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010473 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 if (argvars[1].v_type != VAR_UNKNOWN
10475 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010476 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010477 if (!error)
10478 {
10479 ExpandInit(&xpc);
10480 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010481 if (p_wic)
10482 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010483 if (rettv->v_type == VAR_STRING)
10484 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10485 options, WILD_ALL);
10486 else if (rettv_list_alloc(rettv) != FAIL)
10487 {
10488 int i;
10489
10490 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10491 for (i = 0; i < xpc.xp_numfiles; i++)
10492 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10493 ExpandCleanup(&xpc);
10494 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 }
10496 else
10497 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 }
10499}
10500
10501/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010502 * Go over all entries in "d2" and add them to "d1".
10503 * When "action" is "error" then a duplicate key is an error.
10504 * When "action" is "force" then a duplicate key is overwritten.
10505 * Otherwise duplicate keys are ignored ("action" is "keep").
10506 */
10507 void
10508dict_extend(d1, d2, action)
10509 dict_T *d1;
10510 dict_T *d2;
10511 char_u *action;
10512{
10513 dictitem_T *di1;
10514 hashitem_T *hi2;
10515 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010516 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010517
10518 todo = (int)d2->dv_hashtab.ht_used;
10519 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10520 {
10521 if (!HASHITEM_EMPTY(hi2))
10522 {
10523 --todo;
10524 di1 = dict_find(d1, hi2->hi_key, -1);
10525 if (d1->dv_scope != 0)
10526 {
10527 /* Disallow replacing a builtin function in l: and g:.
10528 * Check the key to be valid when adding to any
10529 * scope. */
10530 if (d1->dv_scope == VAR_DEF_SCOPE
10531 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10532 && var_check_func_name(hi2->hi_key,
10533 di1 == NULL))
10534 break;
10535 if (!valid_varname(hi2->hi_key))
10536 break;
10537 }
10538 if (di1 == NULL)
10539 {
10540 di1 = dictitem_copy(HI2DI(hi2));
10541 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10542 dictitem_free(di1);
10543 }
10544 else if (*action == 'e')
10545 {
10546 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10547 break;
10548 }
10549 else if (*action == 'f' && HI2DI(hi2) != di1)
10550 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010551 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10552 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010553 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010554 clear_tv(&di1->di_tv);
10555 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10556 }
10557 }
10558 }
10559}
10560
10561/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010562 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010564 */
10565 static void
10566f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010567 typval_T *argvars;
10568 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010569{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010570 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010571
Bram Moolenaare9a41262005-01-15 22:18:47 +000010572 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010573 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 list_T *l1, *l2;
10575 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010578
Bram Moolenaare9a41262005-01-15 22:18:47 +000010579 l1 = argvars[0].vval.v_list;
10580 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010581 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010582 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 {
10584 if (argvars[2].v_type != VAR_UNKNOWN)
10585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 before = get_tv_number_chk(&argvars[2], &error);
10587 if (error)
10588 return; /* type error; errmsg already given */
10589
Bram Moolenaar758711c2005-02-02 23:11:38 +000010590 if (before == l1->lv_len)
10591 item = NULL;
10592 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010594 item = list_find(l1, before);
10595 if (item == NULL)
10596 {
10597 EMSGN(_(e_listidx), before);
10598 return;
10599 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600 }
10601 }
10602 else
10603 item = NULL;
10604 list_extend(l1, l2, item);
10605
Bram Moolenaare9a41262005-01-15 22:18:47 +000010606 copy_tv(&argvars[0], rettv);
10607 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010608 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010609 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10610 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010611 dict_T *d1, *d2;
10612 char_u *action;
10613 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010614
10615 d1 = argvars[0].vval.v_dict;
10616 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010617 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010618 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010619 {
10620 /* Check the third argument. */
10621 if (argvars[2].v_type != VAR_UNKNOWN)
10622 {
10623 static char *(av[]) = {"keep", "force", "error"};
10624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010625 action = get_tv_string_chk(&argvars[2]);
10626 if (action == NULL)
10627 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010628 for (i = 0; i < 3; ++i)
10629 if (STRCMP(action, av[i]) == 0)
10630 break;
10631 if (i == 3)
10632 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010633 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634 return;
10635 }
10636 }
10637 else
10638 action = (char_u *)"force";
10639
Bram Moolenaara9922d62013-05-30 13:01:18 +020010640 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010641
Bram Moolenaare9a41262005-01-15 22:18:47 +000010642 copy_tv(&argvars[0], rettv);
10643 }
10644 }
10645 else
10646 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010647}
10648
10649/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010650 * "feedkeys()" function
10651 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010652 static void
10653f_feedkeys(argvars, rettv)
10654 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010655 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010656{
10657 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010658 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010659 char_u *keys, *flags;
10660 char_u nbuf[NUMBUFLEN];
10661 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010662 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010663
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010664 /* This is not allowed in the sandbox. If the commands would still be
10665 * executed in the sandbox it would be OK, but it probably happens later,
10666 * when "sandbox" is no longer set. */
10667 if (check_secure())
10668 return;
10669
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010670 keys = get_tv_string(&argvars[0]);
10671 if (*keys != NUL)
10672 {
10673 if (argvars[1].v_type != VAR_UNKNOWN)
10674 {
10675 flags = get_tv_string_buf(&argvars[1], nbuf);
10676 for ( ; *flags != NUL; ++flags)
10677 {
10678 switch (*flags)
10679 {
10680 case 'n': remap = FALSE; break;
10681 case 'm': remap = TRUE; break;
10682 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010683 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010684 }
10685 }
10686 }
10687
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010688 /* Need to escape K_SPECIAL and CSI before putting the string in the
10689 * typeahead buffer. */
10690 keys_esc = vim_strsave_escape_csi(keys);
10691 if (keys_esc != NULL)
10692 {
10693 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010694 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010695 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010696 if (vgetc_busy)
10697 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010698 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010699 }
10700}
10701
10702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 * "filereadable()" function
10704 */
10705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010706f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010707 typval_T *argvars;
10708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010710 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 char_u *p;
10712 int n;
10713
Bram Moolenaarc236c162008-07-13 17:41:49 +000010714#ifndef O_NONBLOCK
10715# define O_NONBLOCK 0
10716#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010717 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010718 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10719 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 {
10721 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010722 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 }
10724 else
10725 n = FALSE;
10726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728}
10729
10730/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010731 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 * rights to write into.
10733 */
10734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010736 typval_T *argvars;
10737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010739 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740}
10741
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010742static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010743
10744 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010745findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010746 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010747 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010748 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010749{
10750#ifdef FEAT_SEARCHPATH
10751 char_u *fname;
10752 char_u *fresult = NULL;
10753 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10754 char_u *p;
10755 char_u pathbuf[NUMBUFLEN];
10756 int count = 1;
10757 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010758 int error = FALSE;
10759#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010760
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010761 rettv->vval.v_string = NULL;
10762 rettv->v_type = VAR_STRING;
10763
10764#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010765 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010766
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010767 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010769 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10770 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010771 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010772 else
10773 {
10774 if (*p != NUL)
10775 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010778 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010780 }
10781
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010782 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10783 error = TRUE;
10784
10785 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010787 do
10788 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010789 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010790 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 fresult = find_file_in_path_option(first ? fname : NULL,
10792 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010793 0, first, path,
10794 find_what,
10795 curbuf->b_ffname,
10796 find_what == FINDFILE_DIR
10797 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010799
10800 if (fresult != NULL && rettv->v_type == VAR_LIST)
10801 list_append_string(rettv->vval.v_list, fresult, -1);
10802
10803 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010804 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010805
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010806 if (rettv->v_type == VAR_STRING)
10807 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010808#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010809}
10810
Bram Moolenaar33570922005-01-25 22:26:29 +000010811static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10812static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010813
10814/*
10815 * Implementation of map() and filter().
10816 */
10817 static void
10818filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010819 typval_T *argvars;
10820 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010821 int map;
10822{
10823 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010824 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010825 listitem_T *li, *nli;
10826 list_T *l = NULL;
10827 dictitem_T *di;
10828 hashtab_T *ht;
10829 hashitem_T *hi;
10830 dict_T *d = NULL;
10831 typval_T save_val;
10832 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010833 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010834 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010835 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010836 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010837 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010838 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010839 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010840
Bram Moolenaare9a41262005-01-15 22:18:47 +000010841 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010842 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010843 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010844 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 return;
10846 }
10847 else if (argvars[0].v_type == VAR_DICT)
10848 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010849 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010850 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010851 return;
10852 }
10853 else
10854 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010855 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010856 return;
10857 }
10858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010859 expr = get_tv_string_buf_chk(&argvars[1], buf);
10860 /* On type errors, the preceding call has already displayed an error
10861 * message. Avoid a misleading error message for an empty string that
10862 * was not passed as argument. */
10863 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 prepare_vimvar(VV_VAL, &save_val);
10866 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010867
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010868 /* We reset "did_emsg" to be able to detect whether an error
10869 * occurred during evaluation of the expression. */
10870 save_did_emsg = did_emsg;
10871 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010872
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010873 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010876 vimvars[VV_KEY].vv_type = VAR_STRING;
10877
10878 ht = &d->dv_hashtab;
10879 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010880 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 if (!HASHITEM_EMPTY(hi))
10884 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010885 int r;
10886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010887 --todo;
10888 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010889 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020010890 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10891 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010892 break;
10893 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010894 r = filter_map_one(&di->di_tv, expr, map, &rem);
10895 clear_tv(&vimvars[VV_KEY].vv_tv);
10896 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 break;
10898 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010899 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010900 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10901 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010902 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010904 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010905 }
10906 }
10907 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010908 }
10909 else
10910 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010911 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010913 for (li = l->lv_first; li != NULL; li = nli)
10914 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010915 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010916 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010918 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010919 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010920 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010921 break;
10922 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010924 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010925 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010926 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010927
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010928 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010930
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010931 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010932 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010933
10934 copy_tv(&argvars[0], rettv);
10935}
10936
10937 static int
10938filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010939 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940 char_u *expr;
10941 int map;
10942 int *remp;
10943{
Bram Moolenaar33570922005-01-25 22:26:29 +000010944 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010945 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010946 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947
Bram Moolenaar33570922005-01-25 22:26:29 +000010948 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010949 s = expr;
10950 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010951 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010952 if (*s != NUL) /* check for trailing chars after expr */
10953 {
10954 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010955 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010956 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010957 }
10958 if (map)
10959 {
10960 /* map(): replace the list item value */
10961 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010962 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010963 *tv = rettv;
10964 }
10965 else
10966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967 int error = FALSE;
10968
Bram Moolenaare9a41262005-01-15 22:18:47 +000010969 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010970 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010971 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 /* On type error, nothing has been removed; return FAIL to stop the
10973 * loop. The error message was given by get_tv_number_chk(). */
10974 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010975 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010976 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010977 retval = OK;
10978theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010979 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010980 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010981}
10982
10983/*
10984 * "filter()" function
10985 */
10986 static void
10987f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 typval_T *argvars;
10989 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010990{
10991 filter_map(argvars, rettv, FALSE);
10992}
10993
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010994/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010995 * "finddir({fname}[, {path}[, {count}]])" function
10996 */
10997 static void
10998f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010999 typval_T *argvars;
11000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011001{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011002 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011003}
11004
11005/*
11006 * "findfile({fname}[, {path}[, {count}]])" function
11007 */
11008 static void
11009f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011010 typval_T *argvars;
11011 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011012{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011013 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011014}
11015
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011016#ifdef FEAT_FLOAT
11017/*
11018 * "float2nr({float})" function
11019 */
11020 static void
11021f_float2nr(argvars, rettv)
11022 typval_T *argvars;
11023 typval_T *rettv;
11024{
11025 float_T f;
11026
11027 if (get_float_arg(argvars, &f) == OK)
11028 {
11029 if (f < -0x7fffffff)
11030 rettv->vval.v_number = -0x7fffffff;
11031 else if (f > 0x7fffffff)
11032 rettv->vval.v_number = 0x7fffffff;
11033 else
11034 rettv->vval.v_number = (varnumber_T)f;
11035 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011036}
11037
11038/*
11039 * "floor({float})" function
11040 */
11041 static void
11042f_floor(argvars, rettv)
11043 typval_T *argvars;
11044 typval_T *rettv;
11045{
11046 float_T f;
11047
11048 rettv->v_type = VAR_FLOAT;
11049 if (get_float_arg(argvars, &f) == OK)
11050 rettv->vval.v_float = floor(f);
11051 else
11052 rettv->vval.v_float = 0.0;
11053}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011054
11055/*
11056 * "fmod()" function
11057 */
11058 static void
11059f_fmod(argvars, rettv)
11060 typval_T *argvars;
11061 typval_T *rettv;
11062{
11063 float_T fx, fy;
11064
11065 rettv->v_type = VAR_FLOAT;
11066 if (get_float_arg(argvars, &fx) == OK
11067 && get_float_arg(&argvars[1], &fy) == OK)
11068 rettv->vval.v_float = fmod(fx, fy);
11069 else
11070 rettv->vval.v_float = 0.0;
11071}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011072#endif
11073
Bram Moolenaar0d660222005-01-07 21:51:51 +000011074/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011075 * "fnameescape({string})" function
11076 */
11077 static void
11078f_fnameescape(argvars, rettv)
11079 typval_T *argvars;
11080 typval_T *rettv;
11081{
11082 rettv->vval.v_string = vim_strsave_fnameescape(
11083 get_tv_string(&argvars[0]), FALSE);
11084 rettv->v_type = VAR_STRING;
11085}
11086
11087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 * "fnamemodify({fname}, {mods})" function
11089 */
11090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011092 typval_T *argvars;
11093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094{
11095 char_u *fname;
11096 char_u *mods;
11097 int usedlen = 0;
11098 int len;
11099 char_u *fbuf = NULL;
11100 char_u buf[NUMBUFLEN];
11101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102 fname = get_tv_string_chk(&argvars[0]);
11103 mods = get_tv_string_buf_chk(&argvars[1], buf);
11104 if (fname == NULL || mods == NULL)
11105 fname = NULL;
11106 else
11107 {
11108 len = (int)STRLEN(fname);
11109 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 vim_free(fbuf);
11118}
11119
Bram Moolenaar33570922005-01-25 22:26:29 +000011120static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121
11122/*
11123 * "foldclosed()" function
11124 */
11125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011127 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011129 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130{
11131#ifdef FEAT_FOLDING
11132 linenr_T lnum;
11133 linenr_T first, last;
11134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11137 {
11138 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11139 {
11140 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 return;
11145 }
11146 }
11147#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149}
11150
11151/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011152 * "foldclosed()" function
11153 */
11154 static void
11155f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011156 typval_T *argvars;
11157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011158{
11159 foldclosed_both(argvars, rettv, FALSE);
11160}
11161
11162/*
11163 * "foldclosedend()" function
11164 */
11165 static void
11166f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011167 typval_T *argvars;
11168 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011169{
11170 foldclosed_both(argvars, rettv, TRUE);
11171}
11172
11173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 * "foldlevel()" function
11175 */
11176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011178 typval_T *argvars UNUSED;
11179 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180{
11181#ifdef FEAT_FOLDING
11182 linenr_T lnum;
11183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188}
11189
11190/*
11191 * "foldtext()" function
11192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011195 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197{
11198#ifdef FEAT_FOLDING
11199 linenr_T lnum;
11200 char_u *s;
11201 char_u *r;
11202 int len;
11203 char *txt;
11204#endif
11205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->v_type = VAR_STRING;
11207 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011209 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11210 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11211 <= curbuf->b_ml.ml_line_count
11212 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 {
11214 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011215 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11216 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 {
11218 if (!linewhite(lnum))
11219 break;
11220 ++lnum;
11221 }
11222
11223 /* Find interesting text in this line. */
11224 s = skipwhite(ml_get(lnum));
11225 /* skip C comment-start */
11226 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011229 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011230 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011231 {
11232 s = skipwhite(ml_get(lnum + 1));
11233 if (*s == '*')
11234 s = skipwhite(s + 1);
11235 }
11236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 txt = _("+-%s%3ld lines: ");
11238 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 + 20 /* for %3ld */
11241 + STRLEN(s))); /* concatenated */
11242 if (r != NULL)
11243 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011244 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11245 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11246 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 len = (int)STRLEN(r);
11248 STRCAT(r, s);
11249 /* remove 'foldmarker' and 'commentstring' */
11250 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 }
11253 }
11254#endif
11255}
11256
11257/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011258 * "foldtextresult(lnum)" function
11259 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011262 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011264{
11265#ifdef FEAT_FOLDING
11266 linenr_T lnum;
11267 char_u *text;
11268 char_u buf[51];
11269 foldinfo_T foldinfo;
11270 int fold_count;
11271#endif
11272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->v_type = VAR_STRING;
11274 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011275#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011277 /* treat illegal types and illegal string values for {lnum} the same */
11278 if (lnum < 0)
11279 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011280 fold_count = foldedCount(curwin, lnum, &foldinfo);
11281 if (fold_count > 0)
11282 {
11283 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11284 &foldinfo, buf);
11285 if (text == buf)
11286 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011288 }
11289#endif
11290}
11291
11292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 * "foreground()" function
11294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011297 typval_T *argvars UNUSED;
11298 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300#ifdef FEAT_GUI
11301 if (gui.in_use)
11302 gui_mch_set_foreground();
11303#else
11304# ifdef WIN32
11305 win32_set_foreground();
11306# endif
11307#endif
11308}
11309
11310/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011311 * "function()" function
11312 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011315 typval_T *argvars;
11316 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011317{
11318 char_u *s;
11319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011321 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011322 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011323 /* Don't check an autoload name for existence here. */
11324 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011325 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011326 else
11327 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011328 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011329 {
11330 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011331 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011332
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011333 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11334 * also be called from another script. Using trans_function_name()
11335 * would also work, but some plugins depend on the name being
11336 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011337 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011338 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011339 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011340 if (rettv->vval.v_string != NULL)
11341 {
11342 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011343 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011344 }
11345 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011346 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011347 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011349 }
11350}
11351
11352/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011353 * "garbagecollect()" function
11354 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011355 static void
11356f_garbagecollect(argvars, rettv)
11357 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011358 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011359{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011360 /* This is postponed until we are back at the toplevel, because we may be
11361 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11362 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011363
11364 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11365 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011366}
11367
11368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011369 * "get()" function
11370 */
11371 static void
11372f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 typval_T *argvars;
11374 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011375{
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 listitem_T *li;
11377 list_T *l;
11378 dictitem_T *di;
11379 dict_T *d;
11380 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011381
Bram Moolenaare9a41262005-01-15 22:18:47 +000011382 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011383 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011384 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 int error = FALSE;
11387
11388 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11389 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011390 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011391 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011393 else if (argvars[0].v_type == VAR_DICT)
11394 {
11395 if ((d = argvars[0].vval.v_dict) != NULL)
11396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011397 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011398 if (di != NULL)
11399 tv = &di->di_tv;
11400 }
11401 }
11402 else
11403 EMSG2(_(e_listdictarg), "get()");
11404
11405 if (tv == NULL)
11406 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011407 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011408 copy_tv(&argvars[2], rettv);
11409 }
11410 else
11411 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011412}
11413
Bram Moolenaar342337a2005-07-21 21:11:17 +000011414static 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 +000011415
11416/*
11417 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011418 * Return a range (from start to end) of lines in rettv from the specified
11419 * buffer.
11420 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011421 */
11422 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011423get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011424 buf_T *buf;
11425 linenr_T start;
11426 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011427 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011428 typval_T *rettv;
11429{
11430 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011431
Bram Moolenaar959a1432013-12-14 12:17:38 +010011432 rettv->v_type = VAR_STRING;
11433 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011434 if (retlist && rettv_list_alloc(rettv) == FAIL)
11435 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011436
11437 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11438 return;
11439
11440 if (!retlist)
11441 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011442 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11443 p = ml_get_buf(buf, start, FALSE);
11444 else
11445 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011446 rettv->vval.v_string = vim_strsave(p);
11447 }
11448 else
11449 {
11450 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011451 return;
11452
11453 if (start < 1)
11454 start = 1;
11455 if (end > buf->b_ml.ml_line_count)
11456 end = buf->b_ml.ml_line_count;
11457 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011458 if (list_append_string(rettv->vval.v_list,
11459 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011460 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011461 }
11462}
11463
11464/*
11465 * "getbufline()" function
11466 */
11467 static void
11468f_getbufline(argvars, rettv)
11469 typval_T *argvars;
11470 typval_T *rettv;
11471{
11472 linenr_T lnum;
11473 linenr_T end;
11474 buf_T *buf;
11475
11476 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11477 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011478 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011479 --emsg_off;
11480
Bram Moolenaar661b1822005-07-28 22:36:45 +000011481 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011482 if (argvars[2].v_type == VAR_UNKNOWN)
11483 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011484 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011485 end = get_tv_lnum_buf(&argvars[2], buf);
11486
Bram Moolenaar342337a2005-07-21 21:11:17 +000011487 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011488}
11489
Bram Moolenaar0d660222005-01-07 21:51:51 +000011490/*
11491 * "getbufvar()" function
11492 */
11493 static void
11494f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 typval_T *argvars;
11496 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497{
11498 buf_T *buf;
11499 buf_T *save_curbuf;
11500 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011501 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011502 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011504 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11505 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011506 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011507 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011508
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011509 rettv->v_type = VAR_STRING;
11510 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011511
11512 if (buf != NULL && varname != NULL)
11513 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011514 /* set curbuf to be our buf, temporarily */
11515 save_curbuf = curbuf;
11516 curbuf = buf;
11517
Bram Moolenaar0d660222005-01-07 21:51:51 +000011518 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011519 {
11520 if (get_option_tv(&varname, rettv, TRUE) == OK)
11521 done = TRUE;
11522 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011523 else if (STRCMP(varname, "changedtick") == 0)
11524 {
11525 rettv->v_type = VAR_NUMBER;
11526 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011527 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011528 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011529 else
11530 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011531 /* Look up the variable. */
11532 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11533 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11534 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011535 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011536 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011537 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011538 done = TRUE;
11539 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011540 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011541
11542 /* restore previous notion of curbuf */
11543 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544 }
11545
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011546 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11547 /* use the default value */
11548 copy_tv(&argvars[2], rettv);
11549
Bram Moolenaar0d660222005-01-07 21:51:51 +000011550 --emsg_off;
11551}
11552
11553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 * "getchar()" function
11555 */
11556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011557f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011558 typval_T *argvars;
11559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560{
11561 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011562 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011564 /* Position the cursor. Needed after a message that ends in a space. */
11565 windgoto(msg_row, msg_col);
11566
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 ++no_mapping;
11568 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011569 for (;;)
11570 {
11571 if (argvars[0].v_type == VAR_UNKNOWN)
11572 /* getchar(): blocking wait. */
11573 n = safe_vgetc();
11574 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11575 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011576 n = vpeekc_any();
11577 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011578 /* illegal argument or getchar(0) and no char avail: return zero */
11579 n = 0;
11580 else
11581 /* getchar(0) and char avail: return char */
11582 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011583
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011584 if (n == K_IGNORE)
11585 continue;
11586 break;
11587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 --no_mapping;
11589 --allow_keys;
11590
Bram Moolenaar219b8702006-11-01 14:32:36 +000011591 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11592 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11593 vimvars[VV_MOUSE_COL].vv_nr = 0;
11594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 if (IS_SPECIAL(n) || mod_mask != 0)
11597 {
11598 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11599 int i = 0;
11600
11601 /* Turn a special key into three bytes, plus modifier. */
11602 if (mod_mask != 0)
11603 {
11604 temp[i++] = K_SPECIAL;
11605 temp[i++] = KS_MODIFIER;
11606 temp[i++] = mod_mask;
11607 }
11608 if (IS_SPECIAL(n))
11609 {
11610 temp[i++] = K_SPECIAL;
11611 temp[i++] = K_SECOND(n);
11612 temp[i++] = K_THIRD(n);
11613 }
11614#ifdef FEAT_MBYTE
11615 else if (has_mbyte)
11616 i += (*mb_char2bytes)(n, temp + i);
11617#endif
11618 else
11619 temp[i++] = n;
11620 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->v_type = VAR_STRING;
11622 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011623
11624#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011625 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011626 {
11627 int row = mouse_row;
11628 int col = mouse_col;
11629 win_T *win;
11630 linenr_T lnum;
11631# ifdef FEAT_WINDOWS
11632 win_T *wp;
11633# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011634 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011635
11636 if (row >= 0 && col >= 0)
11637 {
11638 /* Find the window at the mouse coordinates and compute the
11639 * text position. */
11640 win = mouse_find_win(&row, &col);
11641 (void)mouse_comp_pos(win, &row, &col, &lnum);
11642# ifdef FEAT_WINDOWS
11643 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011644 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011645# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011646 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011647 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11648 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11649 }
11650 }
11651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 }
11653}
11654
11655/*
11656 * "getcharmod()" function
11657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011660 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664}
11665
11666/*
11667 * "getcmdline()" function
11668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011671 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011674 rettv->v_type = VAR_STRING;
11675 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676}
11677
11678/*
11679 * "getcmdpos()" function
11680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011683 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011686 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687}
11688
11689/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011690 * "getcmdtype()" function
11691 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011692 static void
11693f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011694 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011695 typval_T *rettv;
11696{
11697 rettv->v_type = VAR_STRING;
11698 rettv->vval.v_string = alloc(2);
11699 if (rettv->vval.v_string != NULL)
11700 {
11701 rettv->vval.v_string[0] = get_cmdline_type();
11702 rettv->vval.v_string[1] = NUL;
11703 }
11704}
11705
11706/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011707 * "getcmdwintype()" function
11708 */
11709 static void
11710f_getcmdwintype(argvars, rettv)
11711 typval_T *argvars UNUSED;
11712 typval_T *rettv;
11713{
11714 rettv->v_type = VAR_STRING;
11715 rettv->vval.v_string = NULL;
11716#ifdef FEAT_CMDWIN
11717 rettv->vval.v_string = alloc(2);
11718 if (rettv->vval.v_string != NULL)
11719 {
11720 rettv->vval.v_string[0] = cmdwin_type;
11721 rettv->vval.v_string[1] = NUL;
11722 }
11723#endif
11724}
11725
11726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 * "getcwd()" function
11728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011731 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011734 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011736 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011737 rettv->vval.v_string = NULL;
11738 cwd = alloc(MAXPATHL);
11739 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011741 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11742 {
11743 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011745 if (rettv->vval.v_string != NULL)
11746 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011748 }
11749 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 }
11751}
11752
11753/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011754 * "getfontname()" function
11755 */
11756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011758 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011759 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011760{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->v_type = VAR_STRING;
11762 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011763#ifdef FEAT_GUI
11764 if (gui.in_use)
11765 {
11766 GuiFont font;
11767 char_u *name = NULL;
11768
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011769 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011770 {
11771 /* Get the "Normal" font. Either the name saved by
11772 * hl_set_font_name() or from the font ID. */
11773 font = gui.norm_font;
11774 name = hl_get_font_name();
11775 }
11776 else
11777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011779 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11780 return;
11781 font = gui_mch_get_font(name, FALSE);
11782 if (font == NOFONT)
11783 return; /* Invalid font name, return empty string. */
11784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011786 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011787 gui_mch_free_font(font);
11788 }
11789#endif
11790}
11791
11792/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011793 * "getfperm({fname})" function
11794 */
11795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011796f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011797 typval_T *argvars;
11798 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011799{
11800 char_u *fname;
11801 struct stat st;
11802 char_u *perm = NULL;
11803 char_u flags[] = "rwx";
11804 int i;
11805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011809 if (mch_stat((char *)fname, &st) >= 0)
11810 {
11811 perm = vim_strsave((char_u *)"---------");
11812 if (perm != NULL)
11813 {
11814 for (i = 0; i < 9; i++)
11815 {
11816 if (st.st_mode & (1 << (8 - i)))
11817 perm[i] = flags[i % 3];
11818 }
11819 }
11820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011821 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011822}
11823
11824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 * "getfsize({fname})" function
11826 */
11827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011829 typval_T *argvars;
11830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831{
11832 char_u *fname;
11833 struct stat st;
11834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011835 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011837 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838
11839 if (mch_stat((char *)fname, &st) >= 0)
11840 {
11841 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011846
11847 /* non-perfect check for overflow */
11848 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11849 rettv->vval.v_number = -2;
11850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 }
11852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854}
11855
11856/*
11857 * "getftime({fname})" function
11858 */
11859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011861 typval_T *argvars;
11862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863{
11864 char_u *fname;
11865 struct stat st;
11866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868
11869 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011870 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011872 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873}
11874
11875/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011876 * "getftype({fname})" function
11877 */
11878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011880 typval_T *argvars;
11881 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011882{
11883 char_u *fname;
11884 struct stat st;
11885 char_u *type = NULL;
11886 char *t;
11887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011891 if (mch_lstat((char *)fname, &st) >= 0)
11892 {
11893#ifdef S_ISREG
11894 if (S_ISREG(st.st_mode))
11895 t = "file";
11896 else if (S_ISDIR(st.st_mode))
11897 t = "dir";
11898# ifdef S_ISLNK
11899 else if (S_ISLNK(st.st_mode))
11900 t = "link";
11901# endif
11902# ifdef S_ISBLK
11903 else if (S_ISBLK(st.st_mode))
11904 t = "bdev";
11905# endif
11906# ifdef S_ISCHR
11907 else if (S_ISCHR(st.st_mode))
11908 t = "cdev";
11909# endif
11910# ifdef S_ISFIFO
11911 else if (S_ISFIFO(st.st_mode))
11912 t = "fifo";
11913# endif
11914# ifdef S_ISSOCK
11915 else if (S_ISSOCK(st.st_mode))
11916 t = "fifo";
11917# endif
11918 else
11919 t = "other";
11920#else
11921# ifdef S_IFMT
11922 switch (st.st_mode & S_IFMT)
11923 {
11924 case S_IFREG: t = "file"; break;
11925 case S_IFDIR: t = "dir"; break;
11926# ifdef S_IFLNK
11927 case S_IFLNK: t = "link"; break;
11928# endif
11929# ifdef S_IFBLK
11930 case S_IFBLK: t = "bdev"; break;
11931# endif
11932# ifdef S_IFCHR
11933 case S_IFCHR: t = "cdev"; break;
11934# endif
11935# ifdef S_IFIFO
11936 case S_IFIFO: t = "fifo"; break;
11937# endif
11938# ifdef S_IFSOCK
11939 case S_IFSOCK: t = "socket"; break;
11940# endif
11941 default: t = "other";
11942 }
11943# else
11944 if (mch_isdir(fname))
11945 t = "dir";
11946 else
11947 t = "file";
11948# endif
11949#endif
11950 type = vim_strsave((char_u *)t);
11951 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011952 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011953}
11954
11955/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011956 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011957 */
11958 static void
11959f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011960 typval_T *argvars;
11961 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011962{
11963 linenr_T lnum;
11964 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011965 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011966
11967 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011968 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011969 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011970 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011971 retlist = FALSE;
11972 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011973 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011974 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011975 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011976 retlist = TRUE;
11977 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011978
Bram Moolenaar342337a2005-07-21 21:11:17 +000011979 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011980}
11981
11982/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011983 * "getmatches()" function
11984 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011985 static void
11986f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011987 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011988 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011989{
11990#ifdef FEAT_SEARCH_EXTRA
11991 dict_T *dict;
11992 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011993 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011994
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011995 if (rettv_list_alloc(rettv) == OK)
11996 {
11997 while (cur != NULL)
11998 {
11999 dict = dict_alloc();
12000 if (dict == NULL)
12001 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012002 if (cur->match.regprog == NULL)
12003 {
12004 /* match added with matchaddpos() */
12005 for (i = 0; i < MAXPOSMATCH; ++i)
12006 {
12007 llpos_T *llpos;
12008 char buf[6];
12009 list_T *l;
12010
12011 llpos = &cur->pos.pos[i];
12012 if (llpos->lnum == 0)
12013 break;
12014 l = list_alloc();
12015 if (l == NULL)
12016 break;
12017 list_append_number(l, (varnumber_T)llpos->lnum);
12018 if (llpos->col > 0)
12019 {
12020 list_append_number(l, (varnumber_T)llpos->col);
12021 list_append_number(l, (varnumber_T)llpos->len);
12022 }
12023 sprintf(buf, "pos%d", i + 1);
12024 dict_add_list(dict, buf, l);
12025 }
12026 }
12027 else
12028 {
12029 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12030 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012031 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012032 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12033 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012034# ifdef FEAT_CONCEAL
12035 if (cur->conceal_char)
12036 {
12037 char_u buf[MB_MAXBYTES + 1];
12038
12039 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12040 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12041 }
12042# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012043 list_append_dict(rettv->vval.v_list, dict);
12044 cur = cur->next;
12045 }
12046 }
12047#endif
12048}
12049
12050/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012051 * "getpid()" function
12052 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012053 static void
12054f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012055 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012056 typval_T *rettv;
12057{
12058 rettv->vval.v_number = mch_get_pid();
12059}
12060
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012061static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12062
12063/*
12064 * "getcurpos()" function
12065 */
12066 static void
12067f_getcurpos(argvars, rettv)
12068 typval_T *argvars;
12069 typval_T *rettv;
12070{
12071 getpos_both(argvars, rettv, TRUE);
12072}
12073
Bram Moolenaar18081e32008-02-20 19:11:07 +000012074/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012075 * "getpos(string)" function
12076 */
12077 static void
12078f_getpos(argvars, rettv)
12079 typval_T *argvars;
12080 typval_T *rettv;
12081{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012082 getpos_both(argvars, rettv, FALSE);
12083}
12084
12085 static void
12086getpos_both(argvars, rettv, getcurpos)
12087 typval_T *argvars;
12088 typval_T *rettv;
12089 int getcurpos;
12090{
Bram Moolenaara5525202006-03-02 22:52:09 +000012091 pos_T *fp;
12092 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012093 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012094
12095 if (rettv_list_alloc(rettv) == OK)
12096 {
12097 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012098 if (getcurpos)
12099 fp = &curwin->w_cursor;
12100 else
12101 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012102 if (fnum != -1)
12103 list_append_number(l, (varnumber_T)fnum);
12104 else
12105 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012106 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12107 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012108 list_append_number(l, (fp != NULL)
12109 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012110 : (varnumber_T)0);
12111 list_append_number(l,
12112#ifdef FEAT_VIRTUALEDIT
12113 (fp != NULL) ? (varnumber_T)fp->coladd :
12114#endif
12115 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012116 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012117 list_append_number(l, curwin->w_curswant == MAXCOL ?
12118 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012119 }
12120 else
12121 rettv->vval.v_number = FALSE;
12122}
12123
12124/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012125 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012126 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012127 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012128f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012129 typval_T *argvars UNUSED;
12130 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012131{
12132#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012133 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012134#endif
12135
Bram Moolenaar2641f772005-03-25 21:58:17 +000012136#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012137 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012138 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012139 wp = NULL;
12140 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12141 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012142 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012143 if (wp == NULL)
12144 return;
12145 }
12146
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012147 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012148 }
12149#endif
12150}
12151
12152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 * "getreg()" function
12154 */
12155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012157 typval_T *argvars;
12158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159{
12160 char_u *strregname;
12161 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012162 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012163 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012164 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012166 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012168 strregname = get_tv_string_chk(&argvars[0]);
12169 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012170 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012172 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012173 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12174 return_list = get_tv_number_chk(&argvars[2], &error);
12175 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012178 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012179
12180 if (error)
12181 return;
12182
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 regname = (strregname == NULL ? '"' : *strregname);
12184 if (regname == 0)
12185 regname = '"';
12186
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012187 if (return_list)
12188 {
12189 rettv->v_type = VAR_LIST;
12190 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12191 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012192 if (rettv->vval.v_list != NULL)
12193 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012194 }
12195 else
12196 {
12197 rettv->v_type = VAR_STRING;
12198 rettv->vval.v_string = get_reg_contents(regname,
12199 arg2 ? GREG_EXPR_SRC : 0);
12200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201}
12202
12203/*
12204 * "getregtype()" function
12205 */
12206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012208 typval_T *argvars;
12209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210{
12211 char_u *strregname;
12212 int regname;
12213 char_u buf[NUMBUFLEN + 2];
12214 long reglen = 0;
12215
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012216 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012217 {
12218 strregname = get_tv_string_chk(&argvars[0]);
12219 if (strregname == NULL) /* type error; errmsg already given */
12220 {
12221 rettv->v_type = VAR_STRING;
12222 rettv->vval.v_string = NULL;
12223 return;
12224 }
12225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 else
12227 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012228 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229
12230 regname = (strregname == NULL ? '"' : *strregname);
12231 if (regname == 0)
12232 regname = '"';
12233
12234 buf[0] = NUL;
12235 buf[1] = NUL;
12236 switch (get_reg_type(regname, &reglen))
12237 {
12238 case MLINE: buf[0] = 'V'; break;
12239 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 case MBLOCK:
12241 buf[0] = Ctrl_V;
12242 sprintf((char *)buf + 1, "%ld", reglen + 1);
12243 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245 rettv->v_type = VAR_STRING;
12246 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247}
12248
12249/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012250 * "gettabvar()" function
12251 */
12252 static void
12253f_gettabvar(argvars, rettv)
12254 typval_T *argvars;
12255 typval_T *rettv;
12256{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012257 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012258 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012259 dictitem_T *v;
12260 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012261 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012262
12263 rettv->v_type = VAR_STRING;
12264 rettv->vval.v_string = NULL;
12265
12266 varname = get_tv_string_chk(&argvars[1]);
12267 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12268 if (tp != NULL && varname != NULL)
12269 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012270 /* Set tp to be our tabpage, temporarily. Also set the window to the
12271 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012272 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12273 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012274 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012275 /* look up the variable */
12276 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12277 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12278 if (v != NULL)
12279 {
12280 copy_tv(&v->di_tv, rettv);
12281 done = TRUE;
12282 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012283 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012284
12285 /* restore previous notion of curwin */
12286 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012287 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012288
12289 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012290 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012291}
12292
12293/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012294 * "gettabwinvar()" function
12295 */
12296 static void
12297f_gettabwinvar(argvars, rettv)
12298 typval_T *argvars;
12299 typval_T *rettv;
12300{
12301 getwinvar(argvars, rettv, 1);
12302}
12303
12304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 * "getwinposx()" function
12306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012309 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313#ifdef FEAT_GUI
12314 if (gui.in_use)
12315 {
12316 int x, y;
12317
12318 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012319 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 }
12321#endif
12322}
12323
12324/*
12325 * "getwinposy()" function
12326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012328f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012329 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012332 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333#ifdef FEAT_GUI
12334 if (gui.in_use)
12335 {
12336 int x, y;
12337
12338 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012339 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 }
12341#endif
12342}
12343
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012344/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012345 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012346 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012347 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012348find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012349 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012350 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012351{
12352#ifdef FEAT_WINDOWS
12353 win_T *wp;
12354#endif
12355 int nr;
12356
12357 nr = get_tv_number_chk(vp, NULL);
12358
12359#ifdef FEAT_WINDOWS
12360 if (nr < 0)
12361 return NULL;
12362 if (nr == 0)
12363 return curwin;
12364
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012365 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12366 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012367 if (--nr <= 0)
12368 break;
12369 return wp;
12370#else
12371 if (nr == 0 || nr == 1)
12372 return curwin;
12373 return NULL;
12374#endif
12375}
12376
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377/*
12378 * "getwinvar()" function
12379 */
12380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012381f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012382 typval_T *argvars;
12383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012385 getwinvar(argvars, rettv, 0);
12386}
12387
12388/*
12389 * getwinvar() and gettabwinvar()
12390 */
12391 static void
12392getwinvar(argvars, rettv, off)
12393 typval_T *argvars;
12394 typval_T *rettv;
12395 int off; /* 1 for gettabwinvar() */
12396{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 win_T *win, *oldcurwin;
12398 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012399 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012400 tabpage_T *tp = NULL;
12401 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012402 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012404#ifdef FEAT_WINDOWS
12405 if (off == 1)
12406 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12407 else
12408 tp = curtab;
12409#endif
12410 win = find_win_by_nr(&argvars[off], tp);
12411 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012412 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012414 rettv->v_type = VAR_STRING;
12415 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416
12417 if (win != NULL && varname != NULL)
12418 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012419 /* Set curwin to be our win, temporarily. Also set the tabpage,
12420 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012421 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012422 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012423 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012424 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012425 if (get_option_tv(&varname, rettv, 1) == OK)
12426 done = TRUE;
12427 }
12428 else
12429 {
12430 /* Look up the variable. */
12431 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12432 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12433 varname, FALSE);
12434 if (v != NULL)
12435 {
12436 copy_tv(&v->di_tv, rettv);
12437 done = TRUE;
12438 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012441
12442 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012443 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444 }
12445
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012446 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12447 /* use the default return value */
12448 copy_tv(&argvars[off + 2], rettv);
12449
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 --emsg_off;
12451}
12452
12453/*
12454 * "glob()" function
12455 */
12456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012458 typval_T *argvars;
12459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012461 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012463 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012465 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012466 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012467 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012468 if (argvars[1].v_type != VAR_UNKNOWN)
12469 {
12470 if (get_tv_number_chk(&argvars[1], &error))
12471 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012472 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012473 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012474 if (get_tv_number_chk(&argvars[2], &error))
12475 {
12476 rettv->v_type = VAR_LIST;
12477 rettv->vval.v_list = NULL;
12478 }
12479 if (argvars[3].v_type != VAR_UNKNOWN
12480 && get_tv_number_chk(&argvars[3], &error))
12481 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012482 }
12483 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012484 if (!error)
12485 {
12486 ExpandInit(&xpc);
12487 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012488 if (p_wic)
12489 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012490 if (rettv->v_type == VAR_STRING)
12491 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012492 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012493 else if (rettv_list_alloc(rettv) != FAIL)
12494 {
12495 int i;
12496
12497 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12498 NULL, options, WILD_ALL_KEEP);
12499 for (i = 0; i < xpc.xp_numfiles; i++)
12500 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12501
12502 ExpandCleanup(&xpc);
12503 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012504 }
12505 else
12506 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507}
12508
12509/*
12510 * "globpath()" function
12511 */
12512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012514 typval_T *argvars;
12515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012517 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012519 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012520 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012521 garray_T ga;
12522 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012524 /* When the optional second argument is non-zero, don't remove matches
12525 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012526 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012527 if (argvars[2].v_type != VAR_UNKNOWN)
12528 {
12529 if (get_tv_number_chk(&argvars[2], &error))
12530 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012531 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012532 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012533 if (get_tv_number_chk(&argvars[3], &error))
12534 {
12535 rettv->v_type = VAR_LIST;
12536 rettv->vval.v_list = NULL;
12537 }
12538 if (argvars[4].v_type != VAR_UNKNOWN
12539 && get_tv_number_chk(&argvars[4], &error))
12540 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012541 }
12542 }
12543 if (file != NULL && !error)
12544 {
12545 ga_init2(&ga, (int)sizeof(char_u *), 10);
12546 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12547 if (rettv->v_type == VAR_STRING)
12548 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12549 else if (rettv_list_alloc(rettv) != FAIL)
12550 for (i = 0; i < ga.ga_len; ++i)
12551 list_append_string(rettv->vval.v_list,
12552 ((char_u **)(ga.ga_data))[i], -1);
12553 ga_clear_strings(&ga);
12554 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012555 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012556 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557}
12558
12559/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012560 * "glob2regpat()" function
12561 */
12562 static void
12563f_glob2regpat(argvars, rettv)
12564 typval_T *argvars;
12565 typval_T *rettv;
12566{
12567 char_u *pat = get_tv_string_chk(&argvars[0]);
12568
12569 rettv->v_type = VAR_STRING;
12570 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12571}
12572
12573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574 * "has()" function
12575 */
12576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012577f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012578 typval_T *argvars;
12579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580{
12581 int i;
12582 char_u *name;
12583 int n = FALSE;
12584 static char *(has_list[]) =
12585 {
12586#ifdef AMIGA
12587 "amiga",
12588# ifdef FEAT_ARP
12589 "arp",
12590# endif
12591#endif
12592#ifdef __BEOS__
12593 "beos",
12594#endif
12595#ifdef MSDOS
12596# ifdef DJGPP
12597 "dos32",
12598# else
12599 "dos16",
12600# endif
12601#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012602#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603 "mac",
12604#endif
12605#if defined(MACOS_X_UNIX)
12606 "macunix",
12607#endif
12608#ifdef OS2
12609 "os2",
12610#endif
12611#ifdef __QNX__
12612 "qnx",
12613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614#ifdef UNIX
12615 "unix",
12616#endif
12617#ifdef VMS
12618 "vms",
12619#endif
12620#ifdef WIN16
12621 "win16",
12622#endif
12623#ifdef WIN32
12624 "win32",
12625#endif
12626#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12627 "win32unix",
12628#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012629#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630 "win64",
12631#endif
12632#ifdef EBCDIC
12633 "ebcdic",
12634#endif
12635#ifndef CASE_INSENSITIVE_FILENAME
12636 "fname_case",
12637#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012638#ifdef HAVE_ACL
12639 "acl",
12640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641#ifdef FEAT_ARABIC
12642 "arabic",
12643#endif
12644#ifdef FEAT_AUTOCMD
12645 "autocmd",
12646#endif
12647#ifdef FEAT_BEVAL
12648 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012649# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12650 "balloon_multiline",
12651# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652#endif
12653#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12654 "builtin_terms",
12655# ifdef ALL_BUILTIN_TCAPS
12656 "all_builtin_terms",
12657# endif
12658#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012659#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12660 || defined(FEAT_GUI_W32) \
12661 || defined(FEAT_GUI_MOTIF))
12662 "browsefilter",
12663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664#ifdef FEAT_BYTEOFF
12665 "byte_offset",
12666#endif
12667#ifdef FEAT_CINDENT
12668 "cindent",
12669#endif
12670#ifdef FEAT_CLIENTSERVER
12671 "clientserver",
12672#endif
12673#ifdef FEAT_CLIPBOARD
12674 "clipboard",
12675#endif
12676#ifdef FEAT_CMDL_COMPL
12677 "cmdline_compl",
12678#endif
12679#ifdef FEAT_CMDHIST
12680 "cmdline_hist",
12681#endif
12682#ifdef FEAT_COMMENTS
12683 "comments",
12684#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012685#ifdef FEAT_CONCEAL
12686 "conceal",
12687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688#ifdef FEAT_CRYPT
12689 "cryptv",
12690#endif
12691#ifdef FEAT_CSCOPE
12692 "cscope",
12693#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012694#ifdef FEAT_CURSORBIND
12695 "cursorbind",
12696#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012697#ifdef CURSOR_SHAPE
12698 "cursorshape",
12699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700#ifdef DEBUG
12701 "debug",
12702#endif
12703#ifdef FEAT_CON_DIALOG
12704 "dialog_con",
12705#endif
12706#ifdef FEAT_GUI_DIALOG
12707 "dialog_gui",
12708#endif
12709#ifdef FEAT_DIFF
12710 "diff",
12711#endif
12712#ifdef FEAT_DIGRAPHS
12713 "digraphs",
12714#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012715#ifdef FEAT_DIRECTX
12716 "directx",
12717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718#ifdef FEAT_DND
12719 "dnd",
12720#endif
12721#ifdef FEAT_EMACS_TAGS
12722 "emacs_tags",
12723#endif
12724 "eval", /* always present, of course! */
12725#ifdef FEAT_EX_EXTRA
12726 "ex_extra",
12727#endif
12728#ifdef FEAT_SEARCH_EXTRA
12729 "extra_search",
12730#endif
12731#ifdef FEAT_FKMAP
12732 "farsi",
12733#endif
12734#ifdef FEAT_SEARCHPATH
12735 "file_in_path",
12736#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012737#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012738 "filterpipe",
12739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740#ifdef FEAT_FIND_ID
12741 "find_in_path",
12742#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012743#ifdef FEAT_FLOAT
12744 "float",
12745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746#ifdef FEAT_FOLDING
12747 "folding",
12748#endif
12749#ifdef FEAT_FOOTER
12750 "footer",
12751#endif
12752#if !defined(USE_SYSTEM) && defined(UNIX)
12753 "fork",
12754#endif
12755#ifdef FEAT_GETTEXT
12756 "gettext",
12757#endif
12758#ifdef FEAT_GUI
12759 "gui",
12760#endif
12761#ifdef FEAT_GUI_ATHENA
12762# ifdef FEAT_GUI_NEXTAW
12763 "gui_neXtaw",
12764# else
12765 "gui_athena",
12766# endif
12767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768#ifdef FEAT_GUI_GTK
12769 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012772#ifdef FEAT_GUI_GNOME
12773 "gui_gnome",
12774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775#ifdef FEAT_GUI_MAC
12776 "gui_mac",
12777#endif
12778#ifdef FEAT_GUI_MOTIF
12779 "gui_motif",
12780#endif
12781#ifdef FEAT_GUI_PHOTON
12782 "gui_photon",
12783#endif
12784#ifdef FEAT_GUI_W16
12785 "gui_win16",
12786#endif
12787#ifdef FEAT_GUI_W32
12788 "gui_win32",
12789#endif
12790#ifdef FEAT_HANGULIN
12791 "hangul_input",
12792#endif
12793#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12794 "iconv",
12795#endif
12796#ifdef FEAT_INS_EXPAND
12797 "insert_expand",
12798#endif
12799#ifdef FEAT_JUMPLIST
12800 "jumplist",
12801#endif
12802#ifdef FEAT_KEYMAP
12803 "keymap",
12804#endif
12805#ifdef FEAT_LANGMAP
12806 "langmap",
12807#endif
12808#ifdef FEAT_LIBCALL
12809 "libcall",
12810#endif
12811#ifdef FEAT_LINEBREAK
12812 "linebreak",
12813#endif
12814#ifdef FEAT_LISP
12815 "lispindent",
12816#endif
12817#ifdef FEAT_LISTCMDS
12818 "listcmds",
12819#endif
12820#ifdef FEAT_LOCALMAP
12821 "localmap",
12822#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012823#ifdef FEAT_LUA
12824# ifndef DYNAMIC_LUA
12825 "lua",
12826# endif
12827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828#ifdef FEAT_MENU
12829 "menu",
12830#endif
12831#ifdef FEAT_SESSION
12832 "mksession",
12833#endif
12834#ifdef FEAT_MODIFY_FNAME
12835 "modify_fname",
12836#endif
12837#ifdef FEAT_MOUSE
12838 "mouse",
12839#endif
12840#ifdef FEAT_MOUSESHAPE
12841 "mouseshape",
12842#endif
12843#if defined(UNIX) || defined(VMS)
12844# ifdef FEAT_MOUSE_DEC
12845 "mouse_dec",
12846# endif
12847# ifdef FEAT_MOUSE_GPM
12848 "mouse_gpm",
12849# endif
12850# ifdef FEAT_MOUSE_JSB
12851 "mouse_jsbterm",
12852# endif
12853# ifdef FEAT_MOUSE_NET
12854 "mouse_netterm",
12855# endif
12856# ifdef FEAT_MOUSE_PTERM
12857 "mouse_pterm",
12858# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012859# ifdef FEAT_MOUSE_SGR
12860 "mouse_sgr",
12861# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012862# ifdef FEAT_SYSMOUSE
12863 "mouse_sysmouse",
12864# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012865# ifdef FEAT_MOUSE_URXVT
12866 "mouse_urxvt",
12867# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868# ifdef FEAT_MOUSE_XTERM
12869 "mouse_xterm",
12870# endif
12871#endif
12872#ifdef FEAT_MBYTE
12873 "multi_byte",
12874#endif
12875#ifdef FEAT_MBYTE_IME
12876 "multi_byte_ime",
12877#endif
12878#ifdef FEAT_MULTI_LANG
12879 "multi_lang",
12880#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012881#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012882#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012883 "mzscheme",
12884#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886#ifdef FEAT_OLE
12887 "ole",
12888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889#ifdef FEAT_PATH_EXTRA
12890 "path_extra",
12891#endif
12892#ifdef FEAT_PERL
12893#ifndef DYNAMIC_PERL
12894 "perl",
12895#endif
12896#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012897#ifdef FEAT_PERSISTENT_UNDO
12898 "persistent_undo",
12899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900#ifdef FEAT_PYTHON
12901#ifndef DYNAMIC_PYTHON
12902 "python",
12903#endif
12904#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012905#ifdef FEAT_PYTHON3
12906#ifndef DYNAMIC_PYTHON3
12907 "python3",
12908#endif
12909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910#ifdef FEAT_POSTSCRIPT
12911 "postscript",
12912#endif
12913#ifdef FEAT_PRINTER
12914 "printer",
12915#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012916#ifdef FEAT_PROFILE
12917 "profile",
12918#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012919#ifdef FEAT_RELTIME
12920 "reltime",
12921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922#ifdef FEAT_QUICKFIX
12923 "quickfix",
12924#endif
12925#ifdef FEAT_RIGHTLEFT
12926 "rightleft",
12927#endif
12928#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12929 "ruby",
12930#endif
12931#ifdef FEAT_SCROLLBIND
12932 "scrollbind",
12933#endif
12934#ifdef FEAT_CMDL_INFO
12935 "showcmd",
12936 "cmdline_info",
12937#endif
12938#ifdef FEAT_SIGNS
12939 "signs",
12940#endif
12941#ifdef FEAT_SMARTINDENT
12942 "smartindent",
12943#endif
12944#ifdef FEAT_SNIFF
12945 "sniff",
12946#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012947#ifdef STARTUPTIME
12948 "startuptime",
12949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950#ifdef FEAT_STL_OPT
12951 "statusline",
12952#endif
12953#ifdef FEAT_SUN_WORKSHOP
12954 "sun_workshop",
12955#endif
12956#ifdef FEAT_NETBEANS_INTG
12957 "netbeans_intg",
12958#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012959#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012960 "spell",
12961#endif
12962#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963 "syntax",
12964#endif
12965#if defined(USE_SYSTEM) || !defined(UNIX)
12966 "system",
12967#endif
12968#ifdef FEAT_TAG_BINS
12969 "tag_binary",
12970#endif
12971#ifdef FEAT_TAG_OLDSTATIC
12972 "tag_old_static",
12973#endif
12974#ifdef FEAT_TAG_ANYWHITE
12975 "tag_any_white",
12976#endif
12977#ifdef FEAT_TCL
12978# ifndef DYNAMIC_TCL
12979 "tcl",
12980# endif
12981#endif
12982#ifdef TERMINFO
12983 "terminfo",
12984#endif
12985#ifdef FEAT_TERMRESPONSE
12986 "termresponse",
12987#endif
12988#ifdef FEAT_TEXTOBJ
12989 "textobjects",
12990#endif
12991#ifdef HAVE_TGETENT
12992 "tgetent",
12993#endif
12994#ifdef FEAT_TITLE
12995 "title",
12996#endif
12997#ifdef FEAT_TOOLBAR
12998 "toolbar",
12999#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013000#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13001 "unnamedplus",
13002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003#ifdef FEAT_USR_CMDS
13004 "user-commands", /* was accidentally included in 5.4 */
13005 "user_commands",
13006#endif
13007#ifdef FEAT_VIMINFO
13008 "viminfo",
13009#endif
13010#ifdef FEAT_VERTSPLIT
13011 "vertsplit",
13012#endif
13013#ifdef FEAT_VIRTUALEDIT
13014 "virtualedit",
13015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017#ifdef FEAT_VISUALEXTRA
13018 "visualextra",
13019#endif
13020#ifdef FEAT_VREPLACE
13021 "vreplace",
13022#endif
13023#ifdef FEAT_WILDIGN
13024 "wildignore",
13025#endif
13026#ifdef FEAT_WILDMENU
13027 "wildmenu",
13028#endif
13029#ifdef FEAT_WINDOWS
13030 "windows",
13031#endif
13032#ifdef FEAT_WAK
13033 "winaltkeys",
13034#endif
13035#ifdef FEAT_WRITEBACKUP
13036 "writebackup",
13037#endif
13038#ifdef FEAT_XIM
13039 "xim",
13040#endif
13041#ifdef FEAT_XFONTSET
13042 "xfontset",
13043#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013044#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013045 "xpm",
13046 "xpm_w32", /* for backward compatibility */
13047#else
13048# if defined(HAVE_XPM)
13049 "xpm",
13050# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052#ifdef USE_XSMP
13053 "xsmp",
13054#endif
13055#ifdef USE_XSMP_INTERACT
13056 "xsmp_interact",
13057#endif
13058#ifdef FEAT_XCLIPBOARD
13059 "xterm_clipboard",
13060#endif
13061#ifdef FEAT_XTERM_SAVE
13062 "xterm_save",
13063#endif
13064#if defined(UNIX) && defined(FEAT_X11)
13065 "X11",
13066#endif
13067 NULL
13068 };
13069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013070 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 for (i = 0; has_list[i] != NULL; ++i)
13072 if (STRICMP(name, has_list[i]) == 0)
13073 {
13074 n = TRUE;
13075 break;
13076 }
13077
13078 if (n == FALSE)
13079 {
13080 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013081 {
13082 if (name[5] == '-'
13083 && STRLEN(name) > 11
13084 && vim_isdigit(name[6])
13085 && vim_isdigit(name[8])
13086 && vim_isdigit(name[10]))
13087 {
13088 int major = atoi((char *)name + 6);
13089 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013090
13091 /* Expect "patch-9.9.01234". */
13092 n = (major < VIM_VERSION_MAJOR
13093 || (major == VIM_VERSION_MAJOR
13094 && (minor < VIM_VERSION_MINOR
13095 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013096 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013097 }
13098 else
13099 n = has_patch(atoi((char *)name + 5));
13100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101 else if (STRICMP(name, "vim_starting") == 0)
13102 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013103#ifdef FEAT_MBYTE
13104 else if (STRICMP(name, "multi_byte_encoding") == 0)
13105 n = has_mbyte;
13106#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013107#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13108 else if (STRICMP(name, "balloon_multiline") == 0)
13109 n = multiline_balloon_available();
13110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111#ifdef DYNAMIC_TCL
13112 else if (STRICMP(name, "tcl") == 0)
13113 n = tcl_enabled(FALSE);
13114#endif
13115#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13116 else if (STRICMP(name, "iconv") == 0)
13117 n = iconv_enabled(FALSE);
13118#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013119#ifdef DYNAMIC_LUA
13120 else if (STRICMP(name, "lua") == 0)
13121 n = lua_enabled(FALSE);
13122#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013123#ifdef DYNAMIC_MZSCHEME
13124 else if (STRICMP(name, "mzscheme") == 0)
13125 n = mzscheme_enabled(FALSE);
13126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127#ifdef DYNAMIC_RUBY
13128 else if (STRICMP(name, "ruby") == 0)
13129 n = ruby_enabled(FALSE);
13130#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013131#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132#ifdef DYNAMIC_PYTHON
13133 else if (STRICMP(name, "python") == 0)
13134 n = python_enabled(FALSE);
13135#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013136#endif
13137#ifdef FEAT_PYTHON3
13138#ifdef DYNAMIC_PYTHON3
13139 else if (STRICMP(name, "python3") == 0)
13140 n = python3_enabled(FALSE);
13141#endif
13142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143#ifdef DYNAMIC_PERL
13144 else if (STRICMP(name, "perl") == 0)
13145 n = perl_enabled(FALSE);
13146#endif
13147#ifdef FEAT_GUI
13148 else if (STRICMP(name, "gui_running") == 0)
13149 n = (gui.in_use || gui.starting);
13150# ifdef FEAT_GUI_W32
13151 else if (STRICMP(name, "gui_win32s") == 0)
13152 n = gui_is_win32s();
13153# endif
13154# ifdef FEAT_BROWSE
13155 else if (STRICMP(name, "browse") == 0)
13156 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13157# endif
13158#endif
13159#ifdef FEAT_SYN_HL
13160 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013161 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162#endif
13163#if defined(WIN3264)
13164 else if (STRICMP(name, "win95") == 0)
13165 n = mch_windows95();
13166#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013167#ifdef FEAT_NETBEANS_INTG
13168 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013169 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 }
13172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174}
13175
13176/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013177 * "has_key()" function
13178 */
13179 static void
13180f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013181 typval_T *argvars;
13182 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013183{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013184 if (argvars[0].v_type != VAR_DICT)
13185 {
13186 EMSG(_(e_dictreq));
13187 return;
13188 }
13189 if (argvars[0].vval.v_dict == NULL)
13190 return;
13191
13192 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013193 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013194}
13195
13196/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013197 * "haslocaldir()" function
13198 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013199 static void
13200f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013201 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013202 typval_T *rettv;
13203{
13204 rettv->vval.v_number = (curwin->w_localdir != NULL);
13205}
13206
13207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 * "hasmapto()" function
13209 */
13210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013212 typval_T *argvars;
13213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214{
13215 char_u *name;
13216 char_u *mode;
13217 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013218 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013221 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 mode = (char_u *)"nvo";
13223 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013224 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013226 if (argvars[2].v_type != VAR_UNKNOWN)
13227 abbr = get_tv_number(&argvars[2]);
13228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229
Bram Moolenaar2c932302006-03-18 21:42:09 +000013230 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234}
13235
13236/*
13237 * "histadd()" function
13238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013241 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243{
13244#ifdef FEAT_CMDHIST
13245 int histype;
13246 char_u *str;
13247 char_u buf[NUMBUFLEN];
13248#endif
13249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013250 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251 if (check_restricted() || check_secure())
13252 return;
13253#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013254 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13255 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256 if (histype >= 0)
13257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013258 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 if (*str != NUL)
13260 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013261 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264 return;
13265 }
13266 }
13267#endif
13268}
13269
13270/*
13271 * "histdel()" function
13272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013274f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013275 typval_T *argvars UNUSED;
13276 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277{
13278#ifdef FEAT_CMDHIST
13279 int n;
13280 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013281 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013283 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13284 if (str == NULL)
13285 n = 0;
13286 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013288 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013289 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013291 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013292 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 else
13294 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013295 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013296 get_tv_string_buf(&argvars[1], buf));
13297 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298#endif
13299}
13300
13301/*
13302 * "histget()" function
13303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013305f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013306 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308{
13309#ifdef FEAT_CMDHIST
13310 int type;
13311 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013312 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013314 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13315 if (str == NULL)
13316 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013318 {
13319 type = get_histtype(str);
13320 if (argvars[1].v_type == VAR_UNKNOWN)
13321 idx = get_history_idx(type);
13322 else
13323 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13324 /* -1 on type error */
13325 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013328 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013330 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331}
13332
13333/*
13334 * "histnr()" function
13335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013337f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013338 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340{
13341 int i;
13342
13343#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013344 char_u *history = get_tv_string_chk(&argvars[0]);
13345
13346 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 if (i >= HIST_CMD && i < HIST_COUNT)
13348 i = get_history_idx(i);
13349 else
13350#endif
13351 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353}
13354
13355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356 * "highlightID(name)" function
13357 */
13358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013360 typval_T *argvars;
13361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364}
13365
13366/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013367 * "highlight_exists()" function
13368 */
13369 static void
13370f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013371 typval_T *argvars;
13372 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013373{
13374 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13375}
13376
13377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 * "hostname()" function
13379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013381f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013382 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384{
13385 char_u hostname[256];
13386
13387 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013388 rettv->v_type = VAR_STRING;
13389 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390}
13391
13392/*
13393 * iconv() function
13394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013396f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013397 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399{
13400#ifdef FEAT_MBYTE
13401 char_u buf1[NUMBUFLEN];
13402 char_u buf2[NUMBUFLEN];
13403 char_u *from, *to, *str;
13404 vimconv_T vimconv;
13405#endif
13406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013407 rettv->v_type = VAR_STRING;
13408 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409
13410#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411 str = get_tv_string(&argvars[0]);
13412 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13413 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 vimconv.vc_type = CONV_NONE;
13415 convert_setup(&vimconv, from, to);
13416
13417 /* If the encodings are equal, no conversion needed. */
13418 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013419 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422
13423 convert_setup(&vimconv, NULL, NULL);
13424 vim_free(from);
13425 vim_free(to);
13426#endif
13427}
13428
13429/*
13430 * "indent()" function
13431 */
13432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013433f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013434 typval_T *argvars;
13435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436{
13437 linenr_T lnum;
13438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013443 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444}
13445
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013446/*
13447 * "index()" function
13448 */
13449 static void
13450f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013451 typval_T *argvars;
13452 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013453{
Bram Moolenaar33570922005-01-25 22:26:29 +000013454 list_T *l;
13455 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013456 long idx = 0;
13457 int ic = FALSE;
13458
13459 rettv->vval.v_number = -1;
13460 if (argvars[0].v_type != VAR_LIST)
13461 {
13462 EMSG(_(e_listreq));
13463 return;
13464 }
13465 l = argvars[0].vval.v_list;
13466 if (l != NULL)
13467 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013468 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013469 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013471 int error = FALSE;
13472
Bram Moolenaar758711c2005-02-02 23:11:38 +000013473 /* Start at specified item. Use the cached index that list_find()
13474 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013475 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013476 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013477 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013478 ic = get_tv_number_chk(&argvars[3], &error);
13479 if (error)
13480 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013481 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013482
Bram Moolenaar758711c2005-02-02 23:11:38 +000013483 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013484 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013485 {
13486 rettv->vval.v_number = idx;
13487 break;
13488 }
13489 }
13490}
13491
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492static int inputsecret_flag = 0;
13493
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013494static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13495
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013497 * This function is used by f_input() and f_inputdialog() functions. The third
13498 * argument to f_input() specifies the type of completion to use at the
13499 * prompt. The third argument to f_inputdialog() specifies the value to return
13500 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501 */
13502 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013503get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013504 typval_T *argvars;
13505 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013506 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013508 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 char_u *p = NULL;
13510 int c;
13511 char_u buf[NUMBUFLEN];
13512 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013514 int xp_type = EXPAND_NOTHING;
13515 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013518 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519
13520#ifdef NO_CONSOLE_INPUT
13521 /* While starting up, there is no place to enter text. */
13522 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524#endif
13525
13526 cmd_silent = FALSE; /* Want to see the prompt. */
13527 if (prompt != NULL)
13528 {
13529 /* Only the part of the message after the last NL is considered as
13530 * prompt for the command line */
13531 p = vim_strrchr(prompt, '\n');
13532 if (p == NULL)
13533 p = prompt;
13534 else
13535 {
13536 ++p;
13537 c = *p;
13538 *p = NUL;
13539 msg_start();
13540 msg_clr_eos();
13541 msg_puts_attr(prompt, echo_attr);
13542 msg_didout = FALSE;
13543 msg_starthere();
13544 *p = c;
13545 }
13546 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013548 if (argvars[1].v_type != VAR_UNKNOWN)
13549 {
13550 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13551 if (defstr != NULL)
13552 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013554 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013555 {
13556 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013557 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013558 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013559
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013560 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013561 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013562
Bram Moolenaar4463f292005-09-25 22:20:24 +000013563 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13564 if (xp_name == NULL)
13565 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013566
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013567 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013568
Bram Moolenaar4463f292005-09-25 22:20:24 +000013569 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13570 &xp_arg) == FAIL)
13571 return;
13572 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013573 }
13574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013575 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013576 {
13577# ifdef FEAT_EX_EXTRA
13578 int save_ex_normal_busy = ex_normal_busy;
13579 ex_normal_busy = 0;
13580# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013581 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013582 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13583 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013584# ifdef FEAT_EX_EXTRA
13585 ex_normal_busy = save_ex_normal_busy;
13586# endif
13587 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013588 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013589 && argvars[1].v_type != VAR_UNKNOWN
13590 && argvars[2].v_type != VAR_UNKNOWN)
13591 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13592 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013593
13594 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013596 /* since the user typed this, no need to wait for return */
13597 need_wait_return = FALSE;
13598 msg_didout = FALSE;
13599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 cmd_silent = cmd_silent_save;
13601}
13602
13603/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013604 * "input()" function
13605 * Also handles inputsecret() when inputsecret is set.
13606 */
13607 static void
13608f_input(argvars, rettv)
13609 typval_T *argvars;
13610 typval_T *rettv;
13611{
13612 get_user_input(argvars, rettv, FALSE);
13613}
13614
13615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 * "inputdialog()" function
13617 */
13618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013620 typval_T *argvars;
13621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622{
13623#if defined(FEAT_GUI_TEXTDIALOG)
13624 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13625 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13626 {
13627 char_u *message;
13628 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013629 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013631 message = get_tv_string_chk(&argvars[0]);
13632 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013633 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013634 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 else
13636 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013637 if (message != NULL && defstr != NULL
13638 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013639 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 else
13642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013643 if (message != NULL && defstr != NULL
13644 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013645 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 rettv->vval.v_string = vim_strsave(
13647 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652 }
13653 else
13654#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013655 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656}
13657
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013658/*
13659 * "inputlist()" function
13660 */
13661 static void
13662f_inputlist(argvars, rettv)
13663 typval_T *argvars;
13664 typval_T *rettv;
13665{
13666 listitem_T *li;
13667 int selected;
13668 int mouse_used;
13669
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013670#ifdef NO_CONSOLE_INPUT
13671 /* While starting up, there is no place to enter text. */
13672 if (no_console_input())
13673 return;
13674#endif
13675 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13676 {
13677 EMSG2(_(e_listarg), "inputlist()");
13678 return;
13679 }
13680
13681 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013682 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013683 lines_left = Rows; /* avoid more prompt */
13684 msg_scroll = TRUE;
13685 msg_clr_eos();
13686
13687 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13688 {
13689 msg_puts(get_tv_string(&li->li_tv));
13690 msg_putchar('\n');
13691 }
13692
13693 /* Ask for choice. */
13694 selected = prompt_for_number(&mouse_used);
13695 if (mouse_used)
13696 selected -= lines_left;
13697
13698 rettv->vval.v_number = selected;
13699}
13700
13701
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13703
13704/*
13705 * "inputrestore()" function
13706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013708f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013709 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711{
13712 if (ga_userinput.ga_len > 0)
13713 {
13714 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13716 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013717 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 }
13719 else if (p_verbose > 1)
13720 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013721 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013722 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 }
13724}
13725
13726/*
13727 * "inputsave()" function
13728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013730f_inputsave(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{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013734 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 if (ga_grow(&ga_userinput, 1) == OK)
13736 {
13737 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13738 + ga_userinput.ga_len);
13739 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013740 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 }
13742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013743 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744}
13745
13746/*
13747 * "inputsecret()" function
13748 */
13749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013751 typval_T *argvars;
13752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753{
13754 ++cmdline_star;
13755 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013756 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757 --cmdline_star;
13758 --inputsecret_flag;
13759}
13760
13761/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013762 * "insert()" function
13763 */
13764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013766 typval_T *argvars;
13767 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013768{
13769 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013770 listitem_T *item;
13771 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013772 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013773
13774 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013775 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013776 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013777 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013778 {
13779 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013780 before = get_tv_number_chk(&argvars[2], &error);
13781 if (error)
13782 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013783
Bram Moolenaar758711c2005-02-02 23:11:38 +000013784 if (before == l->lv_len)
13785 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013786 else
13787 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013788 item = list_find(l, before);
13789 if (item == NULL)
13790 {
13791 EMSGN(_(e_listidx), before);
13792 l = NULL;
13793 }
13794 }
13795 if (l != NULL)
13796 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013797 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013798 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013799 }
13800 }
13801}
13802
13803/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013804 * "invert(expr)" function
13805 */
13806 static void
13807f_invert(argvars, rettv)
13808 typval_T *argvars;
13809 typval_T *rettv;
13810{
13811 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13812}
13813
13814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 * "isdirectory()" function
13816 */
13817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013819 typval_T *argvars;
13820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013822 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823}
13824
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013825/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013826 * "islocked()" function
13827 */
13828 static void
13829f_islocked(argvars, rettv)
13830 typval_T *argvars;
13831 typval_T *rettv;
13832{
13833 lval_T lv;
13834 char_u *end;
13835 dictitem_T *di;
13836
13837 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013838 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13839 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013840 if (end != NULL && lv.ll_name != NULL)
13841 {
13842 if (*end != NUL)
13843 EMSG(_(e_trailing));
13844 else
13845 {
13846 if (lv.ll_tv == NULL)
13847 {
13848 if (check_changedtick(lv.ll_name))
13849 rettv->vval.v_number = 1; /* always locked */
13850 else
13851 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013852 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013853 if (di != NULL)
13854 {
13855 /* Consider a variable locked when:
13856 * 1. the variable itself is locked
13857 * 2. the value of the variable is locked.
13858 * 3. the List or Dict value is locked.
13859 */
13860 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13861 || tv_islocked(&di->di_tv));
13862 }
13863 }
13864 }
13865 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013866 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013867 else if (lv.ll_newkey != NULL)
13868 EMSG2(_(e_dictkey), lv.ll_newkey);
13869 else if (lv.ll_list != NULL)
13870 /* List item. */
13871 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13872 else
13873 /* Dictionary item. */
13874 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13875 }
13876 }
13877
13878 clear_lval(&lv);
13879}
13880
Bram Moolenaar33570922005-01-25 22:26:29 +000013881static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013882
13883/*
13884 * Turn a dict into a list:
13885 * "what" == 0: list of keys
13886 * "what" == 1: list of values
13887 * "what" == 2: list of items
13888 */
13889 static void
13890dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013891 typval_T *argvars;
13892 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013893 int what;
13894{
Bram Moolenaar33570922005-01-25 22:26:29 +000013895 list_T *l2;
13896 dictitem_T *di;
13897 hashitem_T *hi;
13898 listitem_T *li;
13899 listitem_T *li2;
13900 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013901 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013902
Bram Moolenaar8c711452005-01-14 21:53:12 +000013903 if (argvars[0].v_type != VAR_DICT)
13904 {
13905 EMSG(_(e_dictreq));
13906 return;
13907 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013908 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013909 return;
13910
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013911 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013912 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013913
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013914 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013915 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013916 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013917 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013918 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013919 --todo;
13920 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013921
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013922 li = listitem_alloc();
13923 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013924 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013925 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013926
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013927 if (what == 0)
13928 {
13929 /* keys() */
13930 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013931 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013932 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13933 }
13934 else if (what == 1)
13935 {
13936 /* values() */
13937 copy_tv(&di->di_tv, &li->li_tv);
13938 }
13939 else
13940 {
13941 /* items() */
13942 l2 = list_alloc();
13943 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013944 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013945 li->li_tv.vval.v_list = l2;
13946 if (l2 == NULL)
13947 break;
13948 ++l2->lv_refcount;
13949
13950 li2 = listitem_alloc();
13951 if (li2 == NULL)
13952 break;
13953 list_append(l2, li2);
13954 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013955 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013956 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13957
13958 li2 = listitem_alloc();
13959 if (li2 == NULL)
13960 break;
13961 list_append(l2, li2);
13962 copy_tv(&di->di_tv, &li2->li_tv);
13963 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013964 }
13965 }
13966}
13967
13968/*
13969 * "items(dict)" function
13970 */
13971 static void
13972f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013973 typval_T *argvars;
13974 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013975{
13976 dict_list(argvars, rettv, 2);
13977}
13978
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013980 * "join()" function
13981 */
13982 static void
13983f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013984 typval_T *argvars;
13985 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013986{
13987 garray_T ga;
13988 char_u *sep;
13989
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013990 if (argvars[0].v_type != VAR_LIST)
13991 {
13992 EMSG(_(e_listreq));
13993 return;
13994 }
13995 if (argvars[0].vval.v_list == NULL)
13996 return;
13997 if (argvars[1].v_type == VAR_UNKNOWN)
13998 sep = (char_u *)" ";
13999 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014000 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014001
14002 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014003
14004 if (sep != NULL)
14005 {
14006 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014007 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014008 ga_append(&ga, NUL);
14009 rettv->vval.v_string = (char_u *)ga.ga_data;
14010 }
14011 else
14012 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014013}
14014
14015/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014016 * "keys()" function
14017 */
14018 static void
14019f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014020 typval_T *argvars;
14021 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014022{
14023 dict_list(argvars, rettv, 0);
14024}
14025
14026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 * "last_buffer_nr()" function.
14028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014030f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014031 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033{
14034 int n = 0;
14035 buf_T *buf;
14036
14037 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14038 if (n < buf->b_fnum)
14039 n = buf->b_fnum;
14040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014042}
14043
14044/*
14045 * "len()" function
14046 */
14047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014048f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014049 typval_T *argvars;
14050 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014051{
14052 switch (argvars[0].v_type)
14053 {
14054 case VAR_STRING:
14055 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014056 rettv->vval.v_number = (varnumber_T)STRLEN(
14057 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014058 break;
14059 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014060 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014061 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014062 case VAR_DICT:
14063 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14064 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014065 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014066 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014067 break;
14068 }
14069}
14070
Bram Moolenaar33570922005-01-25 22:26:29 +000014071static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014072
14073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014074libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014075 typval_T *argvars;
14076 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014077 int type;
14078{
14079#ifdef FEAT_LIBCALL
14080 char_u *string_in;
14081 char_u **string_result;
14082 int nr_result;
14083#endif
14084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014086 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014087 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014088
14089 if (check_restricted() || check_secure())
14090 return;
14091
14092#ifdef FEAT_LIBCALL
14093 /* The first two args must be strings, otherwise its meaningless */
14094 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14095 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014096 string_in = NULL;
14097 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014098 string_in = argvars[2].vval.v_string;
14099 if (type == VAR_NUMBER)
14100 string_result = NULL;
14101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014102 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014103 if (mch_libcall(argvars[0].vval.v_string,
14104 argvars[1].vval.v_string,
14105 string_in,
14106 argvars[2].vval.v_number,
14107 string_result,
14108 &nr_result) == OK
14109 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014110 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014111 }
14112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113}
14114
14115/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116 * "libcall()" function
14117 */
14118 static void
14119f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 typval_T *argvars;
14121 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014122{
14123 libcall_common(argvars, rettv, VAR_STRING);
14124}
14125
14126/*
14127 * "libcallnr()" function
14128 */
14129 static void
14130f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014131 typval_T *argvars;
14132 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014133{
14134 libcall_common(argvars, rettv, VAR_NUMBER);
14135}
14136
14137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 * "line(string)" function
14139 */
14140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014141f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014142 typval_T *argvars;
14143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144{
14145 linenr_T lnum = 0;
14146 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014147 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014149 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 if (fp != NULL)
14151 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153}
14154
14155/*
14156 * "line2byte(lnum)" function
14157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014159f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014160 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162{
14163#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014164 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165#else
14166 linenr_T lnum;
14167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014168 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014172 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14173 if (rettv->vval.v_number >= 0)
14174 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175#endif
14176}
14177
14178/*
14179 * "lispindent(lnum)" function
14180 */
14181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014183 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185{
14186#ifdef FEAT_LISP
14187 pos_T pos;
14188 linenr_T lnum;
14189
14190 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014191 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14193 {
14194 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014195 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 curwin->w_cursor = pos;
14197 }
14198 else
14199#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014200 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201}
14202
14203/*
14204 * "localtime()" function
14205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014207f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014208 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014211 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212}
14213
Bram Moolenaar33570922005-01-25 22:26:29 +000014214static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215
14216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014218 typval_T *argvars;
14219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 int exact;
14221{
14222 char_u *keys;
14223 char_u *which;
14224 char_u buf[NUMBUFLEN];
14225 char_u *keys_buf = NULL;
14226 char_u *rhs;
14227 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014228 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014229 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014230 mapblock_T *mp;
14231 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232
14233 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014234 rettv->v_type = VAR_STRING;
14235 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014237 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 if (*keys == NUL)
14239 return;
14240
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014241 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014244 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014245 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014246 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014247 if (argvars[3].v_type != VAR_UNKNOWN)
14248 get_dict = get_tv_number(&argvars[3]);
14249 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 else
14252 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014253 if (which == NULL)
14254 return;
14255
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 mode = get_map_mode(&which, 0);
14257
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014258 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014259 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014261
14262 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014264 /* Return a string. */
14265 if (rhs != NULL)
14266 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267
Bram Moolenaarbd743252010-10-20 21:23:33 +020014268 }
14269 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14270 {
14271 /* Return a dictionary. */
14272 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14273 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14274 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275
Bram Moolenaarbd743252010-10-20 21:23:33 +020014276 dict_add_nr_str(dict, "lhs", 0L, lhs);
14277 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14278 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14279 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14280 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14281 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14282 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014283 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014284 dict_add_nr_str(dict, "mode", 0L, mapmode);
14285
14286 vim_free(lhs);
14287 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 }
14289}
14290
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014291#ifdef FEAT_FLOAT
14292/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014293 * "log()" function
14294 */
14295 static void
14296f_log(argvars, rettv)
14297 typval_T *argvars;
14298 typval_T *rettv;
14299{
14300 float_T f;
14301
14302 rettv->v_type = VAR_FLOAT;
14303 if (get_float_arg(argvars, &f) == OK)
14304 rettv->vval.v_float = log(f);
14305 else
14306 rettv->vval.v_float = 0.0;
14307}
14308
14309/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014310 * "log10()" function
14311 */
14312 static void
14313f_log10(argvars, rettv)
14314 typval_T *argvars;
14315 typval_T *rettv;
14316{
14317 float_T f;
14318
14319 rettv->v_type = VAR_FLOAT;
14320 if (get_float_arg(argvars, &f) == OK)
14321 rettv->vval.v_float = log10(f);
14322 else
14323 rettv->vval.v_float = 0.0;
14324}
14325#endif
14326
Bram Moolenaar1dced572012-04-05 16:54:08 +020014327#ifdef FEAT_LUA
14328/*
14329 * "luaeval()" function
14330 */
14331 static void
14332f_luaeval(argvars, rettv)
14333 typval_T *argvars;
14334 typval_T *rettv;
14335{
14336 char_u *str;
14337 char_u buf[NUMBUFLEN];
14338
14339 str = get_tv_string_buf(&argvars[0], buf);
14340 do_luaeval(str, argvars + 1, rettv);
14341}
14342#endif
14343
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014345 * "map()" function
14346 */
14347 static void
14348f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014349 typval_T *argvars;
14350 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014351{
14352 filter_map(argvars, rettv, TRUE);
14353}
14354
14355/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014356 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 */
14358 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014359f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014360 typval_T *argvars;
14361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014363 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364}
14365
14366/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014367 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 */
14369 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014370f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014371 typval_T *argvars;
14372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014374 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375}
14376
Bram Moolenaar33570922005-01-25 22:26:29 +000014377static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378
14379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014380find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014381 typval_T *argvars;
14382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 int type;
14384{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014385 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014386 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014387 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 char_u *pat;
14389 regmatch_T regmatch;
14390 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014391 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 char_u *save_cpo;
14393 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014394 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014395 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014396 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 list_T *l = NULL;
14398 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014399 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014400 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401
14402 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14403 save_cpo = p_cpo;
14404 p_cpo = (char_u *)"";
14405
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014406 rettv->vval.v_number = -1;
14407 if (type == 3)
14408 {
14409 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014410 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014411 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014412 }
14413 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415 rettv->v_type = VAR_STRING;
14416 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014419 if (argvars[0].v_type == VAR_LIST)
14420 {
14421 if ((l = argvars[0].vval.v_list) == NULL)
14422 goto theend;
14423 li = l->lv_first;
14424 }
14425 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014426 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014427 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014428 len = (long)STRLEN(str);
14429 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014431 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14432 if (pat == NULL)
14433 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014434
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014435 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014437 int error = FALSE;
14438
14439 start = get_tv_number_chk(&argvars[2], &error);
14440 if (error)
14441 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014442 if (l != NULL)
14443 {
14444 li = list_find(l, start);
14445 if (li == NULL)
14446 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014447 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014448 }
14449 else
14450 {
14451 if (start < 0)
14452 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014453 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014454 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014455 /* When "count" argument is there ignore matches before "start",
14456 * otherwise skip part of the string. Differs when pattern is "^"
14457 * or "\<". */
14458 if (argvars[3].v_type != VAR_UNKNOWN)
14459 startcol = start;
14460 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014461 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014462 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014463 len -= start;
14464 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014465 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014466
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014467 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 nth = get_tv_number_chk(&argvars[3], &error);
14469 if (error)
14470 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 }
14472
14473 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14474 if (regmatch.regprog != NULL)
14475 {
14476 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014477
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014478 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014479 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014480 if (l != NULL)
14481 {
14482 if (li == NULL)
14483 {
14484 match = FALSE;
14485 break;
14486 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014487 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014488 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014489 if (str == NULL)
14490 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014491 }
14492
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014493 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014494
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014495 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014496 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014497 if (l == NULL && !match)
14498 break;
14499
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014500 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014501 if (l != NULL)
14502 {
14503 li = li->li_next;
14504 ++idx;
14505 }
14506 else
14507 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014508#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014509 startcol = (colnr_T)(regmatch.startp[0]
14510 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014511#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014512 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014513#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014514 if (startcol > (colnr_T)len
14515 || str + startcol <= regmatch.startp[0])
14516 {
14517 match = FALSE;
14518 break;
14519 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014520 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014521 }
14522
14523 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014525 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014526 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014527 int i;
14528
14529 /* return list with matched string and submatches */
14530 for (i = 0; i < NSUBEXP; ++i)
14531 {
14532 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014533 {
14534 if (list_append_string(rettv->vval.v_list,
14535 (char_u *)"", 0) == FAIL)
14536 break;
14537 }
14538 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014539 regmatch.startp[i],
14540 (int)(regmatch.endp[i] - regmatch.startp[i]))
14541 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014542 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014543 }
14544 }
14545 else if (type == 2)
14546 {
14547 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014548 if (l != NULL)
14549 copy_tv(&li->li_tv, rettv);
14550 else
14551 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014553 }
14554 else if (l != NULL)
14555 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 else
14557 {
14558 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 (varnumber_T)(regmatch.startp[0] - str);
14561 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014562 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014564 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 }
14566 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014567 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568 }
14569
14570theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014571 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572 p_cpo = save_cpo;
14573}
14574
14575/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576 * "match()" function
14577 */
14578 static void
14579f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014580 typval_T *argvars;
14581 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014582{
14583 find_some_match(argvars, rettv, 1);
14584}
14585
14586/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014587 * "matchadd()" function
14588 */
14589 static void
14590f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014591 typval_T *argvars UNUSED;
14592 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014593{
14594#ifdef FEAT_SEARCH_EXTRA
14595 char_u buf[NUMBUFLEN];
14596 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14597 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14598 int prio = 10; /* default priority */
14599 int id = -1;
14600 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014601 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014602
14603 rettv->vval.v_number = -1;
14604
14605 if (grp == NULL || pat == NULL)
14606 return;
14607 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014608 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014609 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014610 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014611 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014612 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014613 if (argvars[4].v_type != VAR_UNKNOWN)
14614 {
14615 if (argvars[4].v_type != VAR_DICT)
14616 {
14617 EMSG(_(e_dictreq));
14618 return;
14619 }
14620 if (dict_find(argvars[4].vval.v_dict,
14621 (char_u *)"conceal", -1) != NULL)
14622 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14623 (char_u *)"conceal", FALSE);
14624 }
14625 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014626 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014627 if (error == TRUE)
14628 return;
14629 if (id >= 1 && id <= 3)
14630 {
14631 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14632 return;
14633 }
14634
Bram Moolenaar6561d522015-07-21 15:48:27 +020014635 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14636 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014637#endif
14638}
14639
14640/*
14641 * "matchaddpos()" function
14642 */
14643 static void
14644f_matchaddpos(argvars, rettv)
14645 typval_T *argvars UNUSED;
14646 typval_T *rettv UNUSED;
14647{
14648#ifdef FEAT_SEARCH_EXTRA
14649 char_u buf[NUMBUFLEN];
14650 char_u *group;
14651 int prio = 10;
14652 int id = -1;
14653 int error = FALSE;
14654 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014655 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014656
14657 rettv->vval.v_number = -1;
14658
14659 group = get_tv_string_buf_chk(&argvars[0], buf);
14660 if (group == NULL)
14661 return;
14662
14663 if (argvars[1].v_type != VAR_LIST)
14664 {
14665 EMSG2(_(e_listarg), "matchaddpos()");
14666 return;
14667 }
14668 l = argvars[1].vval.v_list;
14669 if (l == NULL)
14670 return;
14671
14672 if (argvars[2].v_type != VAR_UNKNOWN)
14673 {
14674 prio = get_tv_number_chk(&argvars[2], &error);
14675 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014676 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014677 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014678 if (argvars[4].v_type != VAR_UNKNOWN)
14679 {
14680 if (argvars[4].v_type != VAR_DICT)
14681 {
14682 EMSG(_(e_dictreq));
14683 return;
14684 }
14685 if (dict_find(argvars[4].vval.v_dict,
14686 (char_u *)"conceal", -1) != NULL)
14687 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14688 (char_u *)"conceal", FALSE);
14689 }
14690 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014691 }
14692 if (error == TRUE)
14693 return;
14694
14695 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14696 if (id == 1 || id == 2)
14697 {
14698 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14699 return;
14700 }
14701
Bram Moolenaar6561d522015-07-21 15:48:27 +020014702 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14703 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014704#endif
14705}
14706
14707/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014708 * "matcharg()" function
14709 */
14710 static void
14711f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014712 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014713 typval_T *rettv;
14714{
14715 if (rettv_list_alloc(rettv) == OK)
14716 {
14717#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014718 int id = get_tv_number(&argvars[0]);
14719 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014720
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014721 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014722 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014723 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14724 {
14725 list_append_string(rettv->vval.v_list,
14726 syn_id2name(m->hlg_id), -1);
14727 list_append_string(rettv->vval.v_list, m->pattern, -1);
14728 }
14729 else
14730 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014731 list_append_string(rettv->vval.v_list, NULL, -1);
14732 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014733 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014734 }
14735#endif
14736 }
14737}
14738
14739/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014740 * "matchdelete()" function
14741 */
14742 static void
14743f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014744 typval_T *argvars UNUSED;
14745 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014746{
14747#ifdef FEAT_SEARCH_EXTRA
14748 rettv->vval.v_number = match_delete(curwin,
14749 (int)get_tv_number(&argvars[0]), TRUE);
14750#endif
14751}
14752
14753/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014754 * "matchend()" function
14755 */
14756 static void
14757f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014758 typval_T *argvars;
14759 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014760{
14761 find_some_match(argvars, rettv, 0);
14762}
14763
14764/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014765 * "matchlist()" function
14766 */
14767 static void
14768f_matchlist(argvars, rettv)
14769 typval_T *argvars;
14770 typval_T *rettv;
14771{
14772 find_some_match(argvars, rettv, 3);
14773}
14774
14775/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014776 * "matchstr()" function
14777 */
14778 static void
14779f_matchstr(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, 2);
14784}
14785
Bram Moolenaar33570922005-01-25 22:26:29 +000014786static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014787
14788 static void
14789max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014790 typval_T *argvars;
14791 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014792 int domax;
14793{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014794 long n = 0;
14795 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014797
14798 if (argvars[0].v_type == VAR_LIST)
14799 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014800 list_T *l;
14801 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014802
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014803 l = argvars[0].vval.v_list;
14804 if (l != NULL)
14805 {
14806 li = l->lv_first;
14807 if (li != NULL)
14808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014809 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014810 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014811 {
14812 li = li->li_next;
14813 if (li == NULL)
14814 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014815 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014816 if (domax ? i > n : i < n)
14817 n = i;
14818 }
14819 }
14820 }
14821 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014822 else if (argvars[0].v_type == VAR_DICT)
14823 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014824 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014825 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014826 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014827 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014828
14829 d = argvars[0].vval.v_dict;
14830 if (d != NULL)
14831 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014832 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014833 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014834 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014835 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014836 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014837 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014838 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014839 if (first)
14840 {
14841 n = i;
14842 first = FALSE;
14843 }
14844 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014845 n = i;
14846 }
14847 }
14848 }
14849 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014850 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014851 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014852 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014853}
14854
14855/*
14856 * "max()" function
14857 */
14858 static void
14859f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014860 typval_T *argvars;
14861 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014862{
14863 max_min(argvars, rettv, TRUE);
14864}
14865
14866/*
14867 * "min()" function
14868 */
14869 static void
14870f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014871 typval_T *argvars;
14872 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014873{
14874 max_min(argvars, rettv, FALSE);
14875}
14876
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014877static int mkdir_recurse __ARGS((char_u *dir, int prot));
14878
14879/*
14880 * Create the directory in which "dir" is located, and higher levels when
14881 * needed.
14882 */
14883 static int
14884mkdir_recurse(dir, prot)
14885 char_u *dir;
14886 int prot;
14887{
14888 char_u *p;
14889 char_u *updir;
14890 int r = FAIL;
14891
14892 /* Get end of directory name in "dir".
14893 * We're done when it's "/" or "c:/". */
14894 p = gettail_sep(dir);
14895 if (p <= get_past_head(dir))
14896 return OK;
14897
14898 /* If the directory exists we're done. Otherwise: create it.*/
14899 updir = vim_strnsave(dir, (int)(p - dir));
14900 if (updir == NULL)
14901 return FAIL;
14902 if (mch_isdir(updir))
14903 r = OK;
14904 else if (mkdir_recurse(updir, prot) == OK)
14905 r = vim_mkdir_emsg(updir, prot);
14906 vim_free(updir);
14907 return r;
14908}
14909
14910#ifdef vim_mkdir
14911/*
14912 * "mkdir()" function
14913 */
14914 static void
14915f_mkdir(argvars, rettv)
14916 typval_T *argvars;
14917 typval_T *rettv;
14918{
14919 char_u *dir;
14920 char_u buf[NUMBUFLEN];
14921 int prot = 0755;
14922
14923 rettv->vval.v_number = FAIL;
14924 if (check_restricted() || check_secure())
14925 return;
14926
14927 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014928 if (*dir == NUL)
14929 rettv->vval.v_number = FAIL;
14930 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014931 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014932 if (*gettail(dir) == NUL)
14933 /* remove trailing slashes */
14934 *gettail_sep(dir) = NUL;
14935
14936 if (argvars[1].v_type != VAR_UNKNOWN)
14937 {
14938 if (argvars[2].v_type != VAR_UNKNOWN)
14939 prot = get_tv_number_chk(&argvars[2], NULL);
14940 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14941 mkdir_recurse(dir, prot);
14942 }
14943 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014944 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014945}
14946#endif
14947
Bram Moolenaar0d660222005-01-07 21:51:51 +000014948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 * "mode()" function
14950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014952f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014953 typval_T *argvars;
14954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014956 char_u buf[3];
14957
14958 buf[1] = NUL;
14959 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 if (VIsual_active)
14962 {
14963 if (VIsual_select)
14964 buf[0] = VIsual_mode + 's' - 'v';
14965 else
14966 buf[0] = VIsual_mode;
14967 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014968 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014969 || State == CONFIRM)
14970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014972 if (State == ASKMORE)
14973 buf[1] = 'm';
14974 else if (State == CONFIRM)
14975 buf[1] = '?';
14976 }
14977 else if (State == EXTERNCMD)
14978 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 else if (State & INSERT)
14980 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014981#ifdef FEAT_VREPLACE
14982 if (State & VREPLACE_FLAG)
14983 {
14984 buf[0] = 'R';
14985 buf[1] = 'v';
14986 }
14987 else
14988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 if (State & REPLACE_FLAG)
14990 buf[0] = 'R';
14991 else
14992 buf[0] = 'i';
14993 }
14994 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014995 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014997 if (exmode_active)
14998 buf[1] = 'v';
14999 }
15000 else if (exmode_active)
15001 {
15002 buf[0] = 'c';
15003 buf[1] = 'e';
15004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015008 if (finish_op)
15009 buf[1] = 'o';
15010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015012 /* Clear out the minor mode when the argument is not a non-zero number or
15013 * non-empty string. */
15014 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015015 buf[1] = NUL;
15016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015017 rettv->vval.v_string = vim_strsave(buf);
15018 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019}
15020
Bram Moolenaar429fa852013-04-15 12:27:36 +020015021#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015022/*
15023 * "mzeval()" function
15024 */
15025 static void
15026f_mzeval(argvars, rettv)
15027 typval_T *argvars;
15028 typval_T *rettv;
15029{
15030 char_u *str;
15031 char_u buf[NUMBUFLEN];
15032
15033 str = get_tv_string_buf(&argvars[0], buf);
15034 do_mzeval(str, rettv);
15035}
Bram Moolenaar75676462013-01-30 14:55:42 +010015036
15037 void
15038mzscheme_call_vim(name, args, rettv)
15039 char_u *name;
15040 typval_T *args;
15041 typval_T *rettv;
15042{
15043 typval_T argvars[3];
15044
15045 argvars[0].v_type = VAR_STRING;
15046 argvars[0].vval.v_string = name;
15047 copy_tv(args, &argvars[1]);
15048 argvars[2].v_type = VAR_UNKNOWN;
15049 f_call(argvars, rettv);
15050 clear_tv(&argvars[1]);
15051}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015052#endif
15053
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055 * "nextnonblank()" function
15056 */
15057 static void
15058f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015059 typval_T *argvars;
15060 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061{
15062 linenr_T lnum;
15063
15064 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015066 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015067 {
15068 lnum = 0;
15069 break;
15070 }
15071 if (*skipwhite(ml_get(lnum)) != NUL)
15072 break;
15073 }
15074 rettv->vval.v_number = lnum;
15075}
15076
15077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078 * "nr2char()" function
15079 */
15080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015081f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015082 typval_T *argvars;
15083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084{
15085 char_u buf[NUMBUFLEN];
15086
15087#ifdef FEAT_MBYTE
15088 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015089 {
15090 int utf8 = 0;
15091
15092 if (argvars[1].v_type != VAR_UNKNOWN)
15093 utf8 = get_tv_number_chk(&argvars[1], NULL);
15094 if (utf8)
15095 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15096 else
15097 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015099 else
15100#endif
15101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015102 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 buf[1] = NUL;
15104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015105 rettv->v_type = VAR_STRING;
15106 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015107}
15108
15109/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015110 * "or(expr, expr)" function
15111 */
15112 static void
15113f_or(argvars, rettv)
15114 typval_T *argvars;
15115 typval_T *rettv;
15116{
15117 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15118 | get_tv_number_chk(&argvars[1], NULL);
15119}
15120
15121/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015122 * "pathshorten()" function
15123 */
15124 static void
15125f_pathshorten(argvars, rettv)
15126 typval_T *argvars;
15127 typval_T *rettv;
15128{
15129 char_u *p;
15130
15131 rettv->v_type = VAR_STRING;
15132 p = get_tv_string_chk(&argvars[0]);
15133 if (p == NULL)
15134 rettv->vval.v_string = NULL;
15135 else
15136 {
15137 p = vim_strsave(p);
15138 rettv->vval.v_string = p;
15139 if (p != NULL)
15140 shorten_dir(p);
15141 }
15142}
15143
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015144#ifdef FEAT_FLOAT
15145/*
15146 * "pow()" function
15147 */
15148 static void
15149f_pow(argvars, rettv)
15150 typval_T *argvars;
15151 typval_T *rettv;
15152{
15153 float_T fx, fy;
15154
15155 rettv->v_type = VAR_FLOAT;
15156 if (get_float_arg(argvars, &fx) == OK
15157 && get_float_arg(&argvars[1], &fy) == OK)
15158 rettv->vval.v_float = pow(fx, fy);
15159 else
15160 rettv->vval.v_float = 0.0;
15161}
15162#endif
15163
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015164/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165 * "prevnonblank()" function
15166 */
15167 static void
15168f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015169 typval_T *argvars;
15170 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171{
15172 linenr_T lnum;
15173
15174 lnum = get_tv_lnum(argvars);
15175 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15176 lnum = 0;
15177 else
15178 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15179 --lnum;
15180 rettv->vval.v_number = lnum;
15181}
15182
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015183#ifdef HAVE_STDARG_H
15184/* This dummy va_list is here because:
15185 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15186 * - locally in the function results in a "used before set" warning
15187 * - using va_start() to initialize it gives "function with fixed args" error */
15188static va_list ap;
15189#endif
15190
Bram Moolenaar8c711452005-01-14 21:53:12 +000015191/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015192 * "printf()" function
15193 */
15194 static void
15195f_printf(argvars, rettv)
15196 typval_T *argvars;
15197 typval_T *rettv;
15198{
15199 rettv->v_type = VAR_STRING;
15200 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015201#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015202 {
15203 char_u buf[NUMBUFLEN];
15204 int len;
15205 char_u *s;
15206 int saved_did_emsg = did_emsg;
15207 char *fmt;
15208
15209 /* Get the required length, allocate the buffer and do it for real. */
15210 did_emsg = FALSE;
15211 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015212 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015213 if (!did_emsg)
15214 {
15215 s = alloc(len + 1);
15216 if (s != NULL)
15217 {
15218 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015219 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015220 }
15221 }
15222 did_emsg |= saved_did_emsg;
15223 }
15224#endif
15225}
15226
15227/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015228 * "pumvisible()" function
15229 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015230 static void
15231f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015232 typval_T *argvars UNUSED;
15233 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015234{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015235#ifdef FEAT_INS_EXPAND
15236 if (pum_visible())
15237 rettv->vval.v_number = 1;
15238#endif
15239}
15240
Bram Moolenaardb913952012-06-29 12:54:53 +020015241#ifdef FEAT_PYTHON3
15242/*
15243 * "py3eval()" function
15244 */
15245 static void
15246f_py3eval(argvars, rettv)
15247 typval_T *argvars;
15248 typval_T *rettv;
15249{
15250 char_u *str;
15251 char_u buf[NUMBUFLEN];
15252
15253 str = get_tv_string_buf(&argvars[0], buf);
15254 do_py3eval(str, rettv);
15255}
15256#endif
15257
15258#ifdef FEAT_PYTHON
15259/*
15260 * "pyeval()" function
15261 */
15262 static void
15263f_pyeval(argvars, rettv)
15264 typval_T *argvars;
15265 typval_T *rettv;
15266{
15267 char_u *str;
15268 char_u buf[NUMBUFLEN];
15269
15270 str = get_tv_string_buf(&argvars[0], buf);
15271 do_pyeval(str, rettv);
15272}
15273#endif
15274
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015275/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015276 * "range()" function
15277 */
15278 static void
15279f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015280 typval_T *argvars;
15281 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015282{
15283 long start;
15284 long end;
15285 long stride = 1;
15286 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015287 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015289 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015290 if (argvars[1].v_type == VAR_UNKNOWN)
15291 {
15292 end = start - 1;
15293 start = 0;
15294 }
15295 else
15296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015297 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015298 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015299 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015300 }
15301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015302 if (error)
15303 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015304 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015305 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015306 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015307 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015308 else
15309 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015310 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015311 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015312 if (list_append_number(rettv->vval.v_list,
15313 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015314 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015315 }
15316}
15317
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015318/*
15319 * "readfile()" function
15320 */
15321 static void
15322f_readfile(argvars, rettv)
15323 typval_T *argvars;
15324 typval_T *rettv;
15325{
15326 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015327 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015328 char_u *fname;
15329 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015330 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15331 int io_size = sizeof(buf);
15332 int readlen; /* size of last fread() */
15333 char_u *prev = NULL; /* previously read bytes, if any */
15334 long prevlen = 0; /* length of data in prev */
15335 long prevsize = 0; /* size of prev buffer */
15336 long maxline = MAXLNUM;
15337 long cnt = 0;
15338 char_u *p; /* position in buf */
15339 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015340
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015341 if (argvars[1].v_type != VAR_UNKNOWN)
15342 {
15343 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15344 binary = TRUE;
15345 if (argvars[2].v_type != VAR_UNKNOWN)
15346 maxline = get_tv_number(&argvars[2]);
15347 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015348
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015349 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015350 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015351
15352 /* Always open the file in binary mode, library functions have a mind of
15353 * their own about CR-LF conversion. */
15354 fname = get_tv_string(&argvars[0]);
15355 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15356 {
15357 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15358 return;
15359 }
15360
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015361 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015362 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015363 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015364
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015365 /* This for loop processes what was read, but is also entered at end
15366 * of file so that either:
15367 * - an incomplete line gets written
15368 * - a "binary" file gets an empty line at the end if it ends in a
15369 * newline. */
15370 for (p = buf, start = buf;
15371 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15372 ++p)
15373 {
15374 if (*p == '\n' || readlen <= 0)
15375 {
15376 listitem_T *li;
15377 char_u *s = NULL;
15378 long_u len = p - start;
15379
15380 /* Finished a line. Remove CRs before NL. */
15381 if (readlen > 0 && !binary)
15382 {
15383 while (len > 0 && start[len - 1] == '\r')
15384 --len;
15385 /* removal may cross back to the "prev" string */
15386 if (len == 0)
15387 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15388 --prevlen;
15389 }
15390 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015391 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015392 else
15393 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015394 /* Change "prev" buffer to be the right size. This way
15395 * the bytes are only copied once, and very long lines are
15396 * allocated only once. */
15397 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015398 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015399 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015400 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015401 prev = NULL; /* the list will own the string */
15402 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015403 }
15404 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015405 if (s == NULL)
15406 {
15407 do_outofmem_msg((long_u) prevlen + len + 1);
15408 failed = TRUE;
15409 break;
15410 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015411
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015412 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015413 {
15414 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015415 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015416 break;
15417 }
15418 li->li_tv.v_type = VAR_STRING;
15419 li->li_tv.v_lock = 0;
15420 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015421 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015422
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015423 start = p + 1; /* step over newline */
15424 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015425 break;
15426 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015427 else if (*p == NUL)
15428 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015429#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015430 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15431 * when finding the BF and check the previous two bytes. */
15432 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015433 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015434 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15435 * + 1, these may be in the "prev" string. */
15436 char_u back1 = p >= buf + 1 ? p[-1]
15437 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15438 char_u back2 = p >= buf + 2 ? p[-2]
15439 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15440 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015441
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015442 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015443 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015444 char_u *dest = p - 2;
15445
15446 /* Usually a BOM is at the beginning of a file, and so at
15447 * the beginning of a line; then we can just step over it.
15448 */
15449 if (start == dest)
15450 start = p + 1;
15451 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015452 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015453 /* have to shuffle buf to close gap */
15454 int adjust_prevlen = 0;
15455
15456 if (dest < buf)
15457 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015458 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015459 dest = buf;
15460 }
15461 if (readlen > p - buf + 1)
15462 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15463 readlen -= 3 - adjust_prevlen;
15464 prevlen -= adjust_prevlen;
15465 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015466 }
15467 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015468 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015469#endif
15470 } /* for */
15471
15472 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15473 break;
15474 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015475 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015476 /* There's part of a line in buf, store it in "prev". */
15477 if (p - start + prevlen >= prevsize)
15478 {
15479 /* need bigger "prev" buffer */
15480 char_u *newprev;
15481
15482 /* A common use case is ordinary text files and "prev" gets a
15483 * fragment of a line, so the first allocation is made
15484 * small, to avoid repeatedly 'allocing' large and
15485 * 'reallocing' small. */
15486 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015487 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015488 else
15489 {
15490 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015491 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015492 prevsize = grow50pc > growmin ? grow50pc : growmin;
15493 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015494 newprev = prev == NULL ? alloc(prevsize)
15495 : vim_realloc(prev, prevsize);
15496 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015497 {
15498 do_outofmem_msg((long_u)prevsize);
15499 failed = TRUE;
15500 break;
15501 }
15502 prev = newprev;
15503 }
15504 /* Add the line part to end of "prev". */
15505 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015506 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015507 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015508 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015509
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015510 /*
15511 * For a negative line count use only the lines at the end of the file,
15512 * free the rest.
15513 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015514 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015515 while (cnt > -maxline)
15516 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015517 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015518 --cnt;
15519 }
15520
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015521 if (failed)
15522 {
15523 list_free(rettv->vval.v_list, TRUE);
15524 /* readfile doc says an empty list is returned on error */
15525 rettv->vval.v_list = list_alloc();
15526 }
15527
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015528 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015529 fclose(fd);
15530}
15531
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015532#if defined(FEAT_RELTIME)
15533static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15534
15535/*
15536 * Convert a List to proftime_T.
15537 * Return FAIL when there is something wrong.
15538 */
15539 static int
15540list2proftime(arg, tm)
15541 typval_T *arg;
15542 proftime_T *tm;
15543{
15544 long n1, n2;
15545 int error = FALSE;
15546
15547 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15548 || arg->vval.v_list->lv_len != 2)
15549 return FAIL;
15550 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15551 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15552# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015553 tm->HighPart = n1;
15554 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015555# else
15556 tm->tv_sec = n1;
15557 tm->tv_usec = n2;
15558# endif
15559 return error ? FAIL : OK;
15560}
15561#endif /* FEAT_RELTIME */
15562
15563/*
15564 * "reltime()" function
15565 */
15566 static void
15567f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015568 typval_T *argvars UNUSED;
15569 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015570{
15571#ifdef FEAT_RELTIME
15572 proftime_T res;
15573 proftime_T start;
15574
15575 if (argvars[0].v_type == VAR_UNKNOWN)
15576 {
15577 /* No arguments: get current time. */
15578 profile_start(&res);
15579 }
15580 else if (argvars[1].v_type == VAR_UNKNOWN)
15581 {
15582 if (list2proftime(&argvars[0], &res) == FAIL)
15583 return;
15584 profile_end(&res);
15585 }
15586 else
15587 {
15588 /* Two arguments: compute the difference. */
15589 if (list2proftime(&argvars[0], &start) == FAIL
15590 || list2proftime(&argvars[1], &res) == FAIL)
15591 return;
15592 profile_sub(&res, &start);
15593 }
15594
15595 if (rettv_list_alloc(rettv) == OK)
15596 {
15597 long n1, n2;
15598
15599# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015600 n1 = res.HighPart;
15601 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015602# else
15603 n1 = res.tv_sec;
15604 n2 = res.tv_usec;
15605# endif
15606 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15607 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15608 }
15609#endif
15610}
15611
15612/*
15613 * "reltimestr()" function
15614 */
15615 static void
15616f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015617 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015618 typval_T *rettv;
15619{
15620#ifdef FEAT_RELTIME
15621 proftime_T tm;
15622#endif
15623
15624 rettv->v_type = VAR_STRING;
15625 rettv->vval.v_string = NULL;
15626#ifdef FEAT_RELTIME
15627 if (list2proftime(&argvars[0], &tm) == OK)
15628 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15629#endif
15630}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015631
Bram Moolenaar0d660222005-01-07 21:51:51 +000015632#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15633static void make_connection __ARGS((void));
15634static int check_connection __ARGS((void));
15635
15636 static void
15637make_connection()
15638{
15639 if (X_DISPLAY == NULL
15640# ifdef FEAT_GUI
15641 && !gui.in_use
15642# endif
15643 )
15644 {
15645 x_force_connect = TRUE;
15646 setup_term_clip();
15647 x_force_connect = FALSE;
15648 }
15649}
15650
15651 static int
15652check_connection()
15653{
15654 make_connection();
15655 if (X_DISPLAY == NULL)
15656 {
15657 EMSG(_("E240: No connection to Vim server"));
15658 return FAIL;
15659 }
15660 return OK;
15661}
15662#endif
15663
15664#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015665static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015666
15667 static void
15668remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015669 typval_T *argvars;
15670 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015671 int expr;
15672{
15673 char_u *server_name;
15674 char_u *keys;
15675 char_u *r = NULL;
15676 char_u buf[NUMBUFLEN];
15677# ifdef WIN32
15678 HWND w;
15679# else
15680 Window w;
15681# endif
15682
15683 if (check_restricted() || check_secure())
15684 return;
15685
15686# ifdef FEAT_X11
15687 if (check_connection() == FAIL)
15688 return;
15689# endif
15690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015691 server_name = get_tv_string_chk(&argvars[0]);
15692 if (server_name == NULL)
15693 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015694 keys = get_tv_string_buf(&argvars[1], buf);
15695# ifdef WIN32
15696 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15697# else
15698 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15699 < 0)
15700# endif
15701 {
15702 if (r != NULL)
15703 EMSG(r); /* sending worked but evaluation failed */
15704 else
15705 EMSG2(_("E241: Unable to send to %s"), server_name);
15706 return;
15707 }
15708
15709 rettv->vval.v_string = r;
15710
15711 if (argvars[2].v_type != VAR_UNKNOWN)
15712 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015713 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015714 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015715 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015716
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015717 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015718 v.di_tv.v_type = VAR_STRING;
15719 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015720 idvar = get_tv_string_chk(&argvars[2]);
15721 if (idvar != NULL)
15722 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015723 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015724 }
15725}
15726#endif
15727
15728/*
15729 * "remote_expr()" function
15730 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731 static void
15732f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015733 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015734 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015735{
15736 rettv->v_type = VAR_STRING;
15737 rettv->vval.v_string = NULL;
15738#ifdef FEAT_CLIENTSERVER
15739 remote_common(argvars, rettv, TRUE);
15740#endif
15741}
15742
15743/*
15744 * "remote_foreground()" function
15745 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015746 static void
15747f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015748 typval_T *argvars UNUSED;
15749 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015750{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751#ifdef FEAT_CLIENTSERVER
15752# ifdef WIN32
15753 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015754 {
15755 char_u *server_name = get_tv_string_chk(&argvars[0]);
15756
15757 if (server_name != NULL)
15758 serverForeground(server_name);
15759 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015760# else
15761 /* Send a foreground() expression to the server. */
15762 argvars[1].v_type = VAR_STRING;
15763 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15764 argvars[2].v_type = VAR_UNKNOWN;
15765 remote_common(argvars, rettv, TRUE);
15766 vim_free(argvars[1].vval.v_string);
15767# endif
15768#endif
15769}
15770
Bram Moolenaar0d660222005-01-07 21:51:51 +000015771 static void
15772f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015773 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015774 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015775{
15776#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015777 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015778 char_u *s = NULL;
15779# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015780 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015781# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015782 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015783
15784 if (check_restricted() || check_secure())
15785 {
15786 rettv->vval.v_number = -1;
15787 return;
15788 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015789 serverid = get_tv_string_chk(&argvars[0]);
15790 if (serverid == NULL)
15791 {
15792 rettv->vval.v_number = -1;
15793 return; /* type error; errmsg already given */
15794 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015795# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015796 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015797 if (n == 0)
15798 rettv->vval.v_number = -1;
15799 else
15800 {
15801 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15802 rettv->vval.v_number = (s != NULL);
15803 }
15804# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015805 if (check_connection() == FAIL)
15806 return;
15807
15808 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015809 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015810# endif
15811
15812 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015814 char_u *retvar;
15815
Bram Moolenaar33570922005-01-25 22:26:29 +000015816 v.di_tv.v_type = VAR_STRING;
15817 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015818 retvar = get_tv_string_chk(&argvars[1]);
15819 if (retvar != NULL)
15820 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015821 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822 }
15823#else
15824 rettv->vval.v_number = -1;
15825#endif
15826}
15827
Bram Moolenaar0d660222005-01-07 21:51:51 +000015828 static void
15829f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015830 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015831 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015832{
15833 char_u *r = NULL;
15834
15835#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015836 char_u *serverid = get_tv_string_chk(&argvars[0]);
15837
15838 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015839 {
15840# ifdef WIN32
15841 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015842 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015843
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015844 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015845 if (n != 0)
15846 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15847 if (r == NULL)
15848# else
15849 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015850 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015851# endif
15852 EMSG(_("E277: Unable to read a server reply"));
15853 }
15854#endif
15855 rettv->v_type = VAR_STRING;
15856 rettv->vval.v_string = r;
15857}
15858
15859/*
15860 * "remote_send()" function
15861 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015862 static void
15863f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015864 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015865 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015866{
15867 rettv->v_type = VAR_STRING;
15868 rettv->vval.v_string = NULL;
15869#ifdef FEAT_CLIENTSERVER
15870 remote_common(argvars, rettv, FALSE);
15871#endif
15872}
15873
15874/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015875 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015876 */
15877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015878f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015879 typval_T *argvars;
15880 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015881{
Bram Moolenaar33570922005-01-25 22:26:29 +000015882 list_T *l;
15883 listitem_T *item, *item2;
15884 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015885 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015886 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015887 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015888 dict_T *d;
15889 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015890 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015891
Bram Moolenaar8c711452005-01-14 21:53:12 +000015892 if (argvars[0].v_type == VAR_DICT)
15893 {
15894 if (argvars[2].v_type != VAR_UNKNOWN)
15895 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015896 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015897 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015899 key = get_tv_string_chk(&argvars[1]);
15900 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015902 di = dict_find(d, key, -1);
15903 if (di == NULL)
15904 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015905 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15906 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015907 {
15908 *rettv = di->di_tv;
15909 init_tv(&di->di_tv);
15910 dictitem_remove(d, di);
15911 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015912 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015913 }
15914 }
15915 else if (argvars[0].v_type != VAR_LIST)
15916 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015917 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015918 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015920 int error = FALSE;
15921
15922 idx = get_tv_number_chk(&argvars[1], &error);
15923 if (error)
15924 ; /* type error: do nothing, errmsg already given */
15925 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015926 EMSGN(_(e_listidx), idx);
15927 else
15928 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015929 if (argvars[2].v_type == VAR_UNKNOWN)
15930 {
15931 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015932 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015933 *rettv = item->li_tv;
15934 vim_free(item);
15935 }
15936 else
15937 {
15938 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015939 end = get_tv_number_chk(&argvars[2], &error);
15940 if (error)
15941 ; /* type error: do nothing */
15942 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015943 EMSGN(_(e_listidx), end);
15944 else
15945 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015946 int cnt = 0;
15947
15948 for (li = item; li != NULL; li = li->li_next)
15949 {
15950 ++cnt;
15951 if (li == item2)
15952 break;
15953 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015954 if (li == NULL) /* didn't find "item2" after "item" */
15955 EMSG(_(e_invrange));
15956 else
15957 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015958 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015959 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015960 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015961 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015962 l->lv_first = item;
15963 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015964 item->li_prev = NULL;
15965 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015966 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015967 }
15968 }
15969 }
15970 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015971 }
15972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973}
15974
15975/*
15976 * "rename({from}, {to})" function
15977 */
15978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015979f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015980 typval_T *argvars;
15981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982{
15983 char_u buf[NUMBUFLEN];
15984
15985 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015986 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015988 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15989 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990}
15991
15992/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015993 * "repeat()" function
15994 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015995 static void
15996f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015997 typval_T *argvars;
15998 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015999{
16000 char_u *p;
16001 int n;
16002 int slen;
16003 int len;
16004 char_u *r;
16005 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016006
16007 n = get_tv_number(&argvars[1]);
16008 if (argvars[0].v_type == VAR_LIST)
16009 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016010 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016011 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016012 if (list_extend(rettv->vval.v_list,
16013 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016014 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016015 }
16016 else
16017 {
16018 p = get_tv_string(&argvars[0]);
16019 rettv->v_type = VAR_STRING;
16020 rettv->vval.v_string = NULL;
16021
16022 slen = (int)STRLEN(p);
16023 len = slen * n;
16024 if (len <= 0)
16025 return;
16026
16027 r = alloc(len + 1);
16028 if (r != NULL)
16029 {
16030 for (i = 0; i < n; i++)
16031 mch_memmove(r + i * slen, p, (size_t)slen);
16032 r[len] = NUL;
16033 }
16034
16035 rettv->vval.v_string = r;
16036 }
16037}
16038
16039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040 * "resolve()" function
16041 */
16042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016043f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016044 typval_T *argvars;
16045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046{
16047 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016048#ifdef HAVE_READLINK
16049 char_u *buf = NULL;
16050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016052 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053#ifdef FEAT_SHORTCUT
16054 {
16055 char_u *v = NULL;
16056
16057 v = mch_resolve_shortcut(p);
16058 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016059 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016061 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 }
16063#else
16064# ifdef HAVE_READLINK
16065 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066 char_u *cpy;
16067 int len;
16068 char_u *remain = NULL;
16069 char_u *q;
16070 int is_relative_to_current = FALSE;
16071 int has_trailing_pathsep = FALSE;
16072 int limit = 100;
16073
16074 p = vim_strsave(p);
16075
16076 if (p[0] == '.' && (vim_ispathsep(p[1])
16077 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16078 is_relative_to_current = TRUE;
16079
16080 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016081 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016082 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016084 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086
16087 q = getnextcomp(p);
16088 if (*q != NUL)
16089 {
16090 /* Separate the first path component in "p", and keep the
16091 * remainder (beginning with the path separator). */
16092 remain = vim_strsave(q - 1);
16093 q[-1] = NUL;
16094 }
16095
Bram Moolenaard9462e32011-04-11 21:35:11 +020016096 buf = alloc(MAXPATHL + 1);
16097 if (buf == NULL)
16098 goto fail;
16099
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 for (;;)
16101 {
16102 for (;;)
16103 {
16104 len = readlink((char *)p, (char *)buf, MAXPATHL);
16105 if (len <= 0)
16106 break;
16107 buf[len] = NUL;
16108
16109 if (limit-- == 0)
16110 {
16111 vim_free(p);
16112 vim_free(remain);
16113 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016114 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 goto fail;
16116 }
16117
16118 /* Ensure that the result will have a trailing path separator
16119 * if the argument has one. */
16120 if (remain == NULL && has_trailing_pathsep)
16121 add_pathsep(buf);
16122
16123 /* Separate the first path component in the link value and
16124 * concatenate the remainders. */
16125 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16126 if (*q != NUL)
16127 {
16128 if (remain == NULL)
16129 remain = vim_strsave(q - 1);
16130 else
16131 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016132 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 if (cpy != NULL)
16134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135 vim_free(remain);
16136 remain = cpy;
16137 }
16138 }
16139 q[-1] = NUL;
16140 }
16141
16142 q = gettail(p);
16143 if (q > p && *q == NUL)
16144 {
16145 /* Ignore trailing path separator. */
16146 q[-1] = NUL;
16147 q = gettail(p);
16148 }
16149 if (q > p && !mch_isFullName(buf))
16150 {
16151 /* symlink is relative to directory of argument */
16152 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16153 if (cpy != NULL)
16154 {
16155 STRCPY(cpy, p);
16156 STRCPY(gettail(cpy), buf);
16157 vim_free(p);
16158 p = cpy;
16159 }
16160 }
16161 else
16162 {
16163 vim_free(p);
16164 p = vim_strsave(buf);
16165 }
16166 }
16167
16168 if (remain == NULL)
16169 break;
16170
16171 /* Append the first path component of "remain" to "p". */
16172 q = getnextcomp(remain + 1);
16173 len = q - remain - (*q != NUL);
16174 cpy = vim_strnsave(p, STRLEN(p) + len);
16175 if (cpy != NULL)
16176 {
16177 STRNCAT(cpy, remain, len);
16178 vim_free(p);
16179 p = cpy;
16180 }
16181 /* Shorten "remain". */
16182 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016183 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 else
16185 {
16186 vim_free(remain);
16187 remain = NULL;
16188 }
16189 }
16190
16191 /* If the result is a relative path name, make it explicitly relative to
16192 * the current directory if and only if the argument had this form. */
16193 if (!vim_ispathsep(*p))
16194 {
16195 if (is_relative_to_current
16196 && *p != NUL
16197 && !(p[0] == '.'
16198 && (p[1] == NUL
16199 || vim_ispathsep(p[1])
16200 || (p[1] == '.'
16201 && (p[2] == NUL
16202 || vim_ispathsep(p[2]))))))
16203 {
16204 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016205 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 if (cpy != NULL)
16207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 vim_free(p);
16209 p = cpy;
16210 }
16211 }
16212 else if (!is_relative_to_current)
16213 {
16214 /* Strip leading "./". */
16215 q = p;
16216 while (q[0] == '.' && vim_ispathsep(q[1]))
16217 q += 2;
16218 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016219 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 }
16221 }
16222
16223 /* Ensure that the result will have no trailing path separator
16224 * if the argument had none. But keep "/" or "//". */
16225 if (!has_trailing_pathsep)
16226 {
16227 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016228 if (after_pathsep(p, q))
16229 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 }
16231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016232 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 }
16234# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016235 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236# endif
16237#endif
16238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016239 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240
16241#ifdef HAVE_READLINK
16242fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016243 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016245 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246}
16247
16248/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016249 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 */
16251 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016253 typval_T *argvars;
16254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255{
Bram Moolenaar33570922005-01-25 22:26:29 +000016256 list_T *l;
16257 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258
Bram Moolenaar0d660222005-01-07 21:51:51 +000016259 if (argvars[0].v_type != VAR_LIST)
16260 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016261 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016262 && !tv_check_lock(l->lv_lock,
16263 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016264 {
16265 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016266 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016267 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016268 while (li != NULL)
16269 {
16270 ni = li->li_prev;
16271 list_append(l, li);
16272 li = ni;
16273 }
16274 rettv->vval.v_list = l;
16275 rettv->v_type = VAR_LIST;
16276 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016277 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279}
16280
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016281#define SP_NOMOVE 0x01 /* don't move cursor */
16282#define SP_REPEAT 0x02 /* repeat to find outer pair */
16283#define SP_RETCOUNT 0x04 /* return matchcount */
16284#define SP_SETPCMARK 0x08 /* set previous context mark */
16285#define SP_START 0x10 /* accept match at start position */
16286#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16287#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016288
Bram Moolenaar33570922005-01-25 22:26:29 +000016289static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290
16291/*
16292 * Get flags for a search function.
16293 * Possibly sets "p_ws".
16294 * Returns BACKWARD, FORWARD or zero (for an error).
16295 */
16296 static int
16297get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016298 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016299 int *flagsp;
16300{
16301 int dir = FORWARD;
16302 char_u *flags;
16303 char_u nbuf[NUMBUFLEN];
16304 int mask;
16305
16306 if (varp->v_type != VAR_UNKNOWN)
16307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016308 flags = get_tv_string_buf_chk(varp, nbuf);
16309 if (flags == NULL)
16310 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016311 while (*flags != NUL)
16312 {
16313 switch (*flags)
16314 {
16315 case 'b': dir = BACKWARD; break;
16316 case 'w': p_ws = TRUE; break;
16317 case 'W': p_ws = FALSE; break;
16318 default: mask = 0;
16319 if (flagsp != NULL)
16320 switch (*flags)
16321 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016322 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016323 case 'e': mask = SP_END; break;
16324 case 'm': mask = SP_RETCOUNT; break;
16325 case 'n': mask = SP_NOMOVE; break;
16326 case 'p': mask = SP_SUBPAT; break;
16327 case 'r': mask = SP_REPEAT; break;
16328 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016329 }
16330 if (mask == 0)
16331 {
16332 EMSG2(_(e_invarg2), flags);
16333 dir = 0;
16334 }
16335 else
16336 *flagsp |= mask;
16337 }
16338 if (dir == 0)
16339 break;
16340 ++flags;
16341 }
16342 }
16343 return dir;
16344}
16345
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016347 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016349 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016350search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016351 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016352 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016353 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016355 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356 char_u *pat;
16357 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016358 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 int save_p_ws = p_ws;
16360 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016361 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016362 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016363 proftime_T tm;
16364#ifdef FEAT_RELTIME
16365 long time_limit = 0;
16366#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016367 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016368 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016370 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016371 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016372 if (dir == 0)
16373 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016374 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016375 if (flags & SP_START)
16376 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016377 if (flags & SP_END)
16378 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016379
Bram Moolenaar76929292008-01-06 19:07:36 +000016380 /* Optional arguments: line number to stop searching and timeout. */
16381 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016382 {
16383 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16384 if (lnum_stop < 0)
16385 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016386#ifdef FEAT_RELTIME
16387 if (argvars[3].v_type != VAR_UNKNOWN)
16388 {
16389 time_limit = get_tv_number_chk(&argvars[3], NULL);
16390 if (time_limit < 0)
16391 goto theend;
16392 }
16393#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016394 }
16395
Bram Moolenaar76929292008-01-06 19:07:36 +000016396#ifdef FEAT_RELTIME
16397 /* Set the time limit, if there is one. */
16398 profile_setlimit(time_limit, &tm);
16399#endif
16400
Bram Moolenaar231334e2005-07-25 20:46:57 +000016401 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016402 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016403 * Check to make sure only those flags are set.
16404 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16405 * flags cannot be set. Check for that condition also.
16406 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016407 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016408 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016410 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016411 goto theend;
16412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016414 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016415 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016416 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016417 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016419 if (flags & SP_SUBPAT)
16420 retval = subpatnum;
16421 else
16422 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016423 if (flags & SP_SETPCMARK)
16424 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016426 if (match_pos != NULL)
16427 {
16428 /* Store the match cursor position */
16429 match_pos->lnum = pos.lnum;
16430 match_pos->col = pos.col + 1;
16431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432 /* "/$" will put the cursor after the end of the line, may need to
16433 * correct that here */
16434 check_cursor();
16435 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016436
16437 /* If 'n' flag is used: restore cursor position. */
16438 if (flags & SP_NOMOVE)
16439 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016440 else
16441 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016442theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016444
16445 return retval;
16446}
16447
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016448#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016449
16450/*
16451 * round() is not in C90, use ceil() or floor() instead.
16452 */
16453 float_T
16454vim_round(f)
16455 float_T f;
16456{
16457 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16458}
16459
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016460/*
16461 * "round({float})" function
16462 */
16463 static void
16464f_round(argvars, rettv)
16465 typval_T *argvars;
16466 typval_T *rettv;
16467{
16468 float_T f;
16469
16470 rettv->v_type = VAR_FLOAT;
16471 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016472 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016473 else
16474 rettv->vval.v_float = 0.0;
16475}
16476#endif
16477
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016478/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016479 * "screenattr()" function
16480 */
16481 static void
16482f_screenattr(argvars, rettv)
16483 typval_T *argvars UNUSED;
16484 typval_T *rettv;
16485{
16486 int row;
16487 int col;
16488 int c;
16489
16490 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16491 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16492 if (row < 0 || row >= screen_Rows
16493 || col < 0 || col >= screen_Columns)
16494 c = -1;
16495 else
16496 c = ScreenAttrs[LineOffset[row] + col];
16497 rettv->vval.v_number = c;
16498}
16499
16500/*
16501 * "screenchar()" function
16502 */
16503 static void
16504f_screenchar(argvars, rettv)
16505 typval_T *argvars UNUSED;
16506 typval_T *rettv;
16507{
16508 int row;
16509 int col;
16510 int off;
16511 int c;
16512
16513 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16514 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16515 if (row < 0 || row >= screen_Rows
16516 || col < 0 || col >= screen_Columns)
16517 c = -1;
16518 else
16519 {
16520 off = LineOffset[row] + col;
16521#ifdef FEAT_MBYTE
16522 if (enc_utf8 && ScreenLinesUC[off] != 0)
16523 c = ScreenLinesUC[off];
16524 else
16525#endif
16526 c = ScreenLines[off];
16527 }
16528 rettv->vval.v_number = c;
16529}
16530
16531/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016532 * "screencol()" function
16533 *
16534 * First column is 1 to be consistent with virtcol().
16535 */
16536 static void
16537f_screencol(argvars, rettv)
16538 typval_T *argvars UNUSED;
16539 typval_T *rettv;
16540{
16541 rettv->vval.v_number = screen_screencol() + 1;
16542}
16543
16544/*
16545 * "screenrow()" function
16546 */
16547 static void
16548f_screenrow(argvars, rettv)
16549 typval_T *argvars UNUSED;
16550 typval_T *rettv;
16551{
16552 rettv->vval.v_number = screen_screenrow() + 1;
16553}
16554
16555/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016556 * "search()" function
16557 */
16558 static void
16559f_search(argvars, rettv)
16560 typval_T *argvars;
16561 typval_T *rettv;
16562{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016563 int flags = 0;
16564
16565 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566}
16567
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016569 * "searchdecl()" function
16570 */
16571 static void
16572f_searchdecl(argvars, rettv)
16573 typval_T *argvars;
16574 typval_T *rettv;
16575{
16576 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016577 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016578 int error = FALSE;
16579 char_u *name;
16580
16581 rettv->vval.v_number = 1; /* default: FAIL */
16582
16583 name = get_tv_string_chk(&argvars[0]);
16584 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016585 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016586 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016587 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16588 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16589 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016590 if (!error && name != NULL)
16591 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016592 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016593}
16594
16595/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016596 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016598 static int
16599searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016600 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016601 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602{
16603 char_u *spat, *mpat, *epat;
16604 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606 int dir;
16607 int flags = 0;
16608 char_u nbuf1[NUMBUFLEN];
16609 char_u nbuf2[NUMBUFLEN];
16610 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016611 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016612 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016613 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016616 spat = get_tv_string_chk(&argvars[0]);
16617 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16618 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16619 if (spat == NULL || mpat == NULL || epat == NULL)
16620 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 /* Handle the optional fourth argument: flags */
16623 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016624 if (dir == 0)
16625 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016626
16627 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016628 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16629 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016630 if ((flags & (SP_END | SP_SUBPAT)) != 0
16631 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016632 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016633 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016634 goto theend;
16635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016637 /* Using 'r' implies 'W', otherwise it doesn't work. */
16638 if (flags & SP_REPEAT)
16639 p_ws = FALSE;
16640
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016641 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016642 if (argvars[3].v_type == VAR_UNKNOWN
16643 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 skip = (char_u *)"";
16645 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016647 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016648 if (argvars[5].v_type != VAR_UNKNOWN)
16649 {
16650 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16651 if (lnum_stop < 0)
16652 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016653#ifdef FEAT_RELTIME
16654 if (argvars[6].v_type != VAR_UNKNOWN)
16655 {
16656 time_limit = get_tv_number_chk(&argvars[6], NULL);
16657 if (time_limit < 0)
16658 goto theend;
16659 }
16660#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016661 }
16662 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016663 if (skip == NULL)
16664 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016666 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016667 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016668
16669theend:
16670 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016671
16672 return retval;
16673}
16674
16675/*
16676 * "searchpair()" function
16677 */
16678 static void
16679f_searchpair(argvars, rettv)
16680 typval_T *argvars;
16681 typval_T *rettv;
16682{
16683 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16684}
16685
16686/*
16687 * "searchpairpos()" function
16688 */
16689 static void
16690f_searchpairpos(argvars, rettv)
16691 typval_T *argvars;
16692 typval_T *rettv;
16693{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016694 pos_T match_pos;
16695 int lnum = 0;
16696 int col = 0;
16697
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016698 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016699 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016700
16701 if (searchpair_cmn(argvars, &match_pos) > 0)
16702 {
16703 lnum = match_pos.lnum;
16704 col = match_pos.col;
16705 }
16706
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016707 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16708 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016709}
16710
16711/*
16712 * Search for a start/middle/end thing.
16713 * Used by searchpair(), see its documentation for the details.
16714 * Returns 0 or -1 for no match,
16715 */
16716 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016717do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16718 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016719 char_u *spat; /* start pattern */
16720 char_u *mpat; /* middle pattern */
16721 char_u *epat; /* end pattern */
16722 int dir; /* BACKWARD or FORWARD */
16723 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016724 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016725 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016726 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016727 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016728{
16729 char_u *save_cpo;
16730 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16731 long retval = 0;
16732 pos_T pos;
16733 pos_T firstpos;
16734 pos_T foundpos;
16735 pos_T save_cursor;
16736 pos_T save_pos;
16737 int n;
16738 int r;
16739 int nest = 1;
16740 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016741 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016742 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016743
16744 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16745 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016746 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016747
Bram Moolenaar76929292008-01-06 19:07:36 +000016748#ifdef FEAT_RELTIME
16749 /* Set the time limit, if there is one. */
16750 profile_setlimit(time_limit, &tm);
16751#endif
16752
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016753 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16754 * start/middle/end (pat3, for the top pair). */
16755 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16756 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16757 if (pat2 == NULL || pat3 == NULL)
16758 goto theend;
16759 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16760 if (*mpat == NUL)
16761 STRCPY(pat3, pat2);
16762 else
16763 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16764 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016765 if (flags & SP_START)
16766 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016767
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 save_cursor = curwin->w_cursor;
16769 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016770 clearpos(&firstpos);
16771 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 pat = pat3;
16773 for (;;)
16774 {
16775 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016776 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16778 /* didn't find it or found the first match again: FAIL */
16779 break;
16780
16781 if (firstpos.lnum == 0)
16782 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016783 if (equalpos(pos, foundpos))
16784 {
16785 /* Found the same position again. Can happen with a pattern that
16786 * has "\zs" at the end and searching backwards. Advance one
16787 * character and try again. */
16788 if (dir == BACKWARD)
16789 decl(&pos);
16790 else
16791 incl(&pos);
16792 }
16793 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016795 /* clear the start flag to avoid getting stuck here */
16796 options &= ~SEARCH_START;
16797
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 /* If the skip pattern matches, ignore this match. */
16799 if (*skip != NUL)
16800 {
16801 save_pos = curwin->w_cursor;
16802 curwin->w_cursor = pos;
16803 r = eval_to_bool(skip, &err, NULL, FALSE);
16804 curwin->w_cursor = save_pos;
16805 if (err)
16806 {
16807 /* Evaluating {skip} caused an error, break here. */
16808 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016809 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 break;
16811 }
16812 if (r)
16813 continue;
16814 }
16815
16816 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16817 {
16818 /* Found end when searching backwards or start when searching
16819 * forward: nested pair. */
16820 ++nest;
16821 pat = pat2; /* nested, don't search for middle */
16822 }
16823 else
16824 {
16825 /* Found end when searching forward or start when searching
16826 * backward: end of (nested) pair; or found middle in outer pair. */
16827 if (--nest == 1)
16828 pat = pat3; /* outer level, search for middle */
16829 }
16830
16831 if (nest == 0)
16832 {
16833 /* Found the match: return matchcount or line number. */
16834 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016835 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016837 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016838 if (flags & SP_SETPCMARK)
16839 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 curwin->w_cursor = pos;
16841 if (!(flags & SP_REPEAT))
16842 break;
16843 nest = 1; /* search for next unmatched */
16844 }
16845 }
16846
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016847 if (match_pos != NULL)
16848 {
16849 /* Store the match cursor position */
16850 match_pos->lnum = curwin->w_cursor.lnum;
16851 match_pos->col = curwin->w_cursor.col + 1;
16852 }
16853
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016855 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856 curwin->w_cursor = save_cursor;
16857
16858theend:
16859 vim_free(pat2);
16860 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016861 if (p_cpo == empty_option)
16862 p_cpo = save_cpo;
16863 else
16864 /* Darn, evaluating the {skip} expression changed the value. */
16865 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016866
16867 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868}
16869
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016870/*
16871 * "searchpos()" function
16872 */
16873 static void
16874f_searchpos(argvars, rettv)
16875 typval_T *argvars;
16876 typval_T *rettv;
16877{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016878 pos_T match_pos;
16879 int lnum = 0;
16880 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016881 int n;
16882 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016883
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016884 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016885 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016886
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016887 n = search_cmn(argvars, &match_pos, &flags);
16888 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016889 {
16890 lnum = match_pos.lnum;
16891 col = match_pos.col;
16892 }
16893
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016894 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16895 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016896 if (flags & SP_SUBPAT)
16897 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016898}
16899
16900
Bram Moolenaar0d660222005-01-07 21:51:51 +000016901 static void
16902f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016903 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906#ifdef FEAT_CLIENTSERVER
16907 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016908 char_u *server = get_tv_string_chk(&argvars[0]);
16909 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910
Bram Moolenaar0d660222005-01-07 21:51:51 +000016911 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016912 if (server == NULL || reply == NULL)
16913 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914 if (check_restricted() || check_secure())
16915 return;
16916# ifdef FEAT_X11
16917 if (check_connection() == FAIL)
16918 return;
16919# endif
16920
16921 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 EMSG(_("E258: Unable to send to client"));
16924 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016926 rettv->vval.v_number = 0;
16927#else
16928 rettv->vval.v_number = -1;
16929#endif
16930}
16931
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932 static void
16933f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016934 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016935 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936{
16937 char_u *r = NULL;
16938
16939#ifdef FEAT_CLIENTSERVER
16940# ifdef WIN32
16941 r = serverGetVimNames();
16942# else
16943 make_connection();
16944 if (X_DISPLAY != NULL)
16945 r = serverGetVimNames(X_DISPLAY);
16946# endif
16947#endif
16948 rettv->v_type = VAR_STRING;
16949 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950}
16951
16952/*
16953 * "setbufvar()" function
16954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016956f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016957 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016958 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959{
16960 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016963 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964 char_u nbuf[NUMBUFLEN];
16965
16966 if (check_restricted() || check_secure())
16967 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016968 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16969 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016970 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971 varp = &argvars[2];
16972
16973 if (buf != NULL && varname != NULL && varp != NULL)
16974 {
16975 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977
16978 if (*varname == '&')
16979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016980 long numval;
16981 char_u *strval;
16982 int error = FALSE;
16983
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016985 numval = get_tv_number_chk(varp, &error);
16986 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016987 if (!error && strval != NULL)
16988 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 }
16990 else
16991 {
16992 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16993 if (bufvarname != NULL)
16994 {
16995 STRCPY(bufvarname, "b:");
16996 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016997 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 vim_free(bufvarname);
16999 }
17000 }
17001
17002 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005}
17006
17007/*
17008 * "setcmdpos()" function
17009 */
17010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017011f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017012 typval_T *argvars;
17013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017015 int pos = (int)get_tv_number(&argvars[0]) - 1;
17016
17017 if (pos >= 0)
17018 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019}
17020
17021/*
17022 * "setline()" function
17023 */
17024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017025f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017026 typval_T *argvars;
17027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028{
17029 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017030 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017031 list_T *l = NULL;
17032 listitem_T *li = NULL;
17033 long added = 0;
17034 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017036 lnum = get_tv_lnum(&argvars[0]);
17037 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017039 l = argvars[1].vval.v_list;
17040 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017042 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017043 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017044
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017045 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017046 for (;;)
17047 {
17048 if (l != NULL)
17049 {
17050 /* list argument, get next string */
17051 if (li == NULL)
17052 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017053 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017054 li = li->li_next;
17055 }
17056
17057 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017058 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017059 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017060
17061 /* When coming here from Insert mode, sync undo, so that this can be
17062 * undone separately from what was previously inserted. */
17063 if (u_sync_once == 2)
17064 {
17065 u_sync_once = 1; /* notify that u_sync() was called */
17066 u_sync(TRUE);
17067 }
17068
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017069 if (lnum <= curbuf->b_ml.ml_line_count)
17070 {
17071 /* existing line, replace it */
17072 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17073 {
17074 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017075 if (lnum == curwin->w_cursor.lnum)
17076 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017077 rettv->vval.v_number = 0; /* OK */
17078 }
17079 }
17080 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17081 {
17082 /* lnum is one past the last line, append the line */
17083 ++added;
17084 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17085 rettv->vval.v_number = 0; /* OK */
17086 }
17087
17088 if (l == NULL) /* only one string argument */
17089 break;
17090 ++lnum;
17091 }
17092
17093 if (added > 0)
17094 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095}
17096
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017097static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17098
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017100 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017101 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017102 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017103set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017104 win_T *wp UNUSED;
17105 typval_T *list_arg UNUSED;
17106 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017107 typval_T *rettv;
17108{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017109#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017110 char_u *act;
17111 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017112#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017113
Bram Moolenaar2641f772005-03-25 21:58:17 +000017114 rettv->vval.v_number = -1;
17115
17116#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017117 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017118 EMSG(_(e_listreq));
17119 else
17120 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017121 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017122
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017123 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017124 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017125 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017126 if (act == NULL)
17127 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017128 if (*act == 'a' || *act == 'r')
17129 action = *act;
17130 }
17131
Bram Moolenaar81484f42012-12-05 15:16:47 +010017132 if (l != NULL && set_errorlist(wp, l, action,
17133 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017134 rettv->vval.v_number = 0;
17135 }
17136#endif
17137}
17138
17139/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017140 * "setloclist()" function
17141 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017142 static void
17143f_setloclist(argvars, rettv)
17144 typval_T *argvars;
17145 typval_T *rettv;
17146{
17147 win_T *win;
17148
17149 rettv->vval.v_number = -1;
17150
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017151 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017152 if (win != NULL)
17153 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17154}
17155
17156/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017157 * "setmatches()" function
17158 */
17159 static void
17160f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017161 typval_T *argvars UNUSED;
17162 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017163{
17164#ifdef FEAT_SEARCH_EXTRA
17165 list_T *l;
17166 listitem_T *li;
17167 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017168 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017169
17170 rettv->vval.v_number = -1;
17171 if (argvars[0].v_type != VAR_LIST)
17172 {
17173 EMSG(_(e_listreq));
17174 return;
17175 }
17176 if ((l = argvars[0].vval.v_list) != NULL)
17177 {
17178
17179 /* To some extent make sure that we are dealing with a list from
17180 * "getmatches()". */
17181 li = l->lv_first;
17182 while (li != NULL)
17183 {
17184 if (li->li_tv.v_type != VAR_DICT
17185 || (d = li->li_tv.vval.v_dict) == NULL)
17186 {
17187 EMSG(_(e_invarg));
17188 return;
17189 }
17190 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017191 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17192 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017193 && dict_find(d, (char_u *)"priority", -1) != NULL
17194 && dict_find(d, (char_u *)"id", -1) != NULL))
17195 {
17196 EMSG(_(e_invarg));
17197 return;
17198 }
17199 li = li->li_next;
17200 }
17201
17202 clear_matches(curwin);
17203 li = l->lv_first;
17204 while (li != NULL)
17205 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017206 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017207 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017208 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017209 char_u *group;
17210 int priority;
17211 int id;
17212 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017213
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017214 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017215 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17216 {
17217 if (s == NULL)
17218 {
17219 s = list_alloc();
17220 if (s == NULL)
17221 return;
17222 }
17223
17224 /* match from matchaddpos() */
17225 for (i = 1; i < 9; i++)
17226 {
17227 sprintf((char *)buf, (char *)"pos%d", i);
17228 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17229 {
17230 if (di->di_tv.v_type != VAR_LIST)
17231 return;
17232
17233 list_append_tv(s, &di->di_tv);
17234 s->lv_refcount++;
17235 }
17236 else
17237 break;
17238 }
17239 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017240
17241 group = get_dict_string(d, (char_u *)"group", FALSE);
17242 priority = (int)get_dict_number(d, (char_u *)"priority");
17243 id = (int)get_dict_number(d, (char_u *)"id");
17244 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17245 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17246 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017247 if (i == 0)
17248 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017249 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017250 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017251 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017252 }
17253 else
17254 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017255 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017256 list_unref(s);
17257 s = NULL;
17258 }
17259
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017260 li = li->li_next;
17261 }
17262 rettv->vval.v_number = 0;
17263 }
17264#endif
17265}
17266
17267/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017268 * "setpos()" function
17269 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017270 static void
17271f_setpos(argvars, rettv)
17272 typval_T *argvars;
17273 typval_T *rettv;
17274{
17275 pos_T pos;
17276 int fnum;
17277 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017278 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017279
Bram Moolenaar08250432008-02-13 11:42:46 +000017280 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017281 name = get_tv_string_chk(argvars);
17282 if (name != NULL)
17283 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017284 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017285 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017286 if (--pos.col < 0)
17287 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017288 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017289 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017290 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017291 if (fnum == curbuf->b_fnum)
17292 {
17293 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017294 if (curswant >= 0)
17295 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017296 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017297 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017298 }
17299 else
17300 EMSG(_(e_invarg));
17301 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017302 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17303 {
17304 /* set mark */
17305 if (setmark_pos(name[1], &pos, fnum) == OK)
17306 rettv->vval.v_number = 0;
17307 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017308 else
17309 EMSG(_(e_invarg));
17310 }
17311 }
17312}
17313
17314/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017315 * "setqflist()" function
17316 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017317 static void
17318f_setqflist(argvars, rettv)
17319 typval_T *argvars;
17320 typval_T *rettv;
17321{
17322 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17323}
17324
17325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 * "setreg()" function
17327 */
17328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017329f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017330 typval_T *argvars;
17331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332{
17333 int regname;
17334 char_u *strregname;
17335 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017336 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 int append;
17338 char_u yank_type;
17339 long block_len;
17340
17341 block_len = -1;
17342 yank_type = MAUTO;
17343 append = FALSE;
17344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017345 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017346 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017348 if (strregname == NULL)
17349 return; /* type error; errmsg already given */
17350 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 if (regname == 0 || regname == '@')
17352 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017354 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017356 stropt = get_tv_string_chk(&argvars[2]);
17357 if (stropt == NULL)
17358 return; /* type error */
17359 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 switch (*stropt)
17361 {
17362 case 'a': case 'A': /* append */
17363 append = TRUE;
17364 break;
17365 case 'v': case 'c': /* character-wise selection */
17366 yank_type = MCHAR;
17367 break;
17368 case 'V': case 'l': /* line-wise selection */
17369 yank_type = MLINE;
17370 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 case 'b': case Ctrl_V: /* block-wise selection */
17372 yank_type = MBLOCK;
17373 if (VIM_ISDIGIT(stropt[1]))
17374 {
17375 ++stropt;
17376 block_len = getdigits(&stropt) - 1;
17377 --stropt;
17378 }
17379 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380 }
17381 }
17382
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017383 if (argvars[1].v_type == VAR_LIST)
17384 {
17385 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017386 char_u **allocval;
17387 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017388 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017389 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017390 int len = argvars[1].vval.v_list->lv_len;
17391 listitem_T *li;
17392
Bram Moolenaar7d647822014-04-05 21:28:56 +020017393 /* First half: use for pointers to result lines; second half: use for
17394 * pointers to allocated copies. */
17395 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017396 if (lstval == NULL)
17397 return;
17398 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017399 allocval = lstval + len + 2;
17400 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017401
17402 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17403 li = li->li_next)
17404 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017405 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017406 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017407 goto free_lstval;
17408 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017409 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017410 /* Need to make a copy, next get_tv_string_buf_chk() will
17411 * overwrite the string. */
17412 strval = vim_strsave(buf);
17413 if (strval == NULL)
17414 goto free_lstval;
17415 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017416 }
17417 *curval++ = strval;
17418 }
17419 *curval++ = NULL;
17420
17421 write_reg_contents_lst(regname, lstval, -1,
17422 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017423free_lstval:
17424 while (curallocval > allocval)
17425 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017426 vim_free(lstval);
17427 }
17428 else
17429 {
17430 strval = get_tv_string_chk(&argvars[1]);
17431 if (strval == NULL)
17432 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017433 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017436 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437}
17438
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017439/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017440 * "settabvar()" function
17441 */
17442 static void
17443f_settabvar(argvars, rettv)
17444 typval_T *argvars;
17445 typval_T *rettv;
17446{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017447#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017448 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017449 tabpage_T *tp;
17450#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017451 char_u *varname, *tabvarname;
17452 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017453
17454 rettv->vval.v_number = 0;
17455
17456 if (check_restricted() || check_secure())
17457 return;
17458
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017459#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017460 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017461#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017462 varname = get_tv_string_chk(&argvars[1]);
17463 varp = &argvars[2];
17464
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017465 if (varname != NULL && varp != NULL
17466#ifdef FEAT_WINDOWS
17467 && tp != NULL
17468#endif
17469 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017470 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017471#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017472 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017473 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017474#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017475
17476 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17477 if (tabvarname != NULL)
17478 {
17479 STRCPY(tabvarname, "t:");
17480 STRCPY(tabvarname + 2, varname);
17481 set_var(tabvarname, varp, TRUE);
17482 vim_free(tabvarname);
17483 }
17484
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017485#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017486 /* Restore current tabpage */
17487 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017488 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017489#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017490 }
17491}
17492
17493/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017494 * "settabwinvar()" function
17495 */
17496 static void
17497f_settabwinvar(argvars, rettv)
17498 typval_T *argvars;
17499 typval_T *rettv;
17500{
17501 setwinvar(argvars, rettv, 1);
17502}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503
17504/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017505 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017508f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017509 typval_T *argvars;
17510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017512 setwinvar(argvars, rettv, 0);
17513}
17514
17515/*
17516 * "setwinvar()" and "settabwinvar()" functions
17517 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017518
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017519 static void
17520setwinvar(argvars, rettv, off)
17521 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017522 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017523 int off;
17524{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 win_T *win;
17526#ifdef FEAT_WINDOWS
17527 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017528 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529#endif
17530 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017531 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017533 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534
17535 if (check_restricted() || check_secure())
17536 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017537
17538#ifdef FEAT_WINDOWS
17539 if (off == 1)
17540 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17541 else
17542 tp = curtab;
17543#endif
17544 win = find_win_by_nr(&argvars[off], tp);
17545 varname = get_tv_string_chk(&argvars[off + 1]);
17546 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547
17548 if (win != NULL && varname != NULL && varp != NULL)
17549 {
17550#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017551 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017554 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017556 long numval;
17557 char_u *strval;
17558 int error = FALSE;
17559
17560 ++varname;
17561 numval = get_tv_number_chk(varp, &error);
17562 strval = get_tv_string_buf_chk(varp, nbuf);
17563 if (!error && strval != NULL)
17564 set_option_value(varname, numval, strval, OPT_LOCAL);
17565 }
17566 else
17567 {
17568 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17569 if (winvarname != NULL)
17570 {
17571 STRCPY(winvarname, "w:");
17572 STRCPY(winvarname + 2, varname);
17573 set_var(winvarname, varp, TRUE);
17574 vim_free(winvarname);
17575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 }
17577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017579 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580#endif
17581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582}
17583
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017584#ifdef FEAT_CRYPT
17585/*
17586 * "sha256({string})" function
17587 */
17588 static void
17589f_sha256(argvars, rettv)
17590 typval_T *argvars;
17591 typval_T *rettv;
17592{
17593 char_u *p;
17594
17595 p = get_tv_string(&argvars[0]);
17596 rettv->vval.v_string = vim_strsave(
17597 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17598 rettv->v_type = VAR_STRING;
17599}
17600#endif /* FEAT_CRYPT */
17601
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017603 * "shellescape({string})" function
17604 */
17605 static void
17606f_shellescape(argvars, rettv)
17607 typval_T *argvars;
17608 typval_T *rettv;
17609{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017610 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017611 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017612 rettv->v_type = VAR_STRING;
17613}
17614
17615/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017616 * shiftwidth() function
17617 */
17618 static void
17619f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017620 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017621 typval_T *rettv;
17622{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017623 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017624}
17625
17626/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017627 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 */
17629 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017630f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017631 typval_T *argvars;
17632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635
Bram Moolenaar0d660222005-01-07 21:51:51 +000017636 p = get_tv_string(&argvars[0]);
17637 rettv->vval.v_string = vim_strsave(p);
17638 simplify_filename(rettv->vval.v_string); /* simplify in place */
17639 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640}
17641
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017642#ifdef FEAT_FLOAT
17643/*
17644 * "sin()" function
17645 */
17646 static void
17647f_sin(argvars, rettv)
17648 typval_T *argvars;
17649 typval_T *rettv;
17650{
17651 float_T f;
17652
17653 rettv->v_type = VAR_FLOAT;
17654 if (get_float_arg(argvars, &f) == OK)
17655 rettv->vval.v_float = sin(f);
17656 else
17657 rettv->vval.v_float = 0.0;
17658}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017659
17660/*
17661 * "sinh()" function
17662 */
17663 static void
17664f_sinh(argvars, rettv)
17665 typval_T *argvars;
17666 typval_T *rettv;
17667{
17668 float_T f;
17669
17670 rettv->v_type = VAR_FLOAT;
17671 if (get_float_arg(argvars, &f) == OK)
17672 rettv->vval.v_float = sinh(f);
17673 else
17674 rettv->vval.v_float = 0.0;
17675}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017676#endif
17677
Bram Moolenaar0d660222005-01-07 21:51:51 +000017678static int
17679#ifdef __BORLANDC__
17680 _RTLENTRYF
17681#endif
17682 item_compare __ARGS((const void *s1, const void *s2));
17683static int
17684#ifdef __BORLANDC__
17685 _RTLENTRYF
17686#endif
17687 item_compare2 __ARGS((const void *s1, const void *s2));
17688
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017689/* struct used in the array that's given to qsort() */
17690typedef struct
17691{
17692 listitem_T *item;
17693 int idx;
17694} sortItem_T;
17695
Bram Moolenaar0d660222005-01-07 21:51:51 +000017696static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017697static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017698static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017699static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017700static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017701static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017702static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017703#define ITEM_COMPARE_FAIL 999
17704
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017706 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017708 static int
17709#ifdef __BORLANDC__
17710_RTLENTRYF
17711#endif
17712item_compare(s1, s2)
17713 const void *s1;
17714 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017716 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017717 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017718 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017719 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017720 int res;
17721 char_u numbuf1[NUMBUFLEN];
17722 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017724 si1 = (sortItem_T *)s1;
17725 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017726 tv1 = &si1->item->li_tv;
17727 tv2 = &si2->item->li_tv;
17728 /* tv2string() puts quotes around a string and allocates memory. Don't do
17729 * that for string variables. Use a single quote when comparing with a
17730 * non-string to do what the docs promise. */
17731 if (tv1->v_type == VAR_STRING)
17732 {
17733 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17734 p1 = (char_u *)"'";
17735 else
17736 p1 = tv1->vval.v_string;
17737 }
17738 else
17739 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17740 if (tv2->v_type == VAR_STRING)
17741 {
17742 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17743 p2 = (char_u *)"'";
17744 else
17745 p2 = tv2->vval.v_string;
17746 }
17747 else
17748 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017749 if (p1 == NULL)
17750 p1 = (char_u *)"";
17751 if (p2 == NULL)
17752 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017753 if (!item_compare_numeric)
17754 {
17755 if (item_compare_ic)
17756 res = STRICMP(p1, p2);
17757 else
17758 res = STRCMP(p1, p2);
17759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017761 {
17762 double n1, n2;
17763 n1 = strtod((char *)p1, (char **)&p1);
17764 n2 = strtod((char *)p2, (char **)&p2);
17765 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17766 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017767
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017768 /* When the result would be zero, compare the item indexes. Makes the
17769 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017770 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017771 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017772
Bram Moolenaar0d660222005-01-07 21:51:51 +000017773 vim_free(tofree1);
17774 vim_free(tofree2);
17775 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776}
17777
17778 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017779#ifdef __BORLANDC__
17780_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017782item_compare2(s1, s2)
17783 const void *s1;
17784 const void *s2;
17785{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017786 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017787 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017788 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017789 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017790 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017792 /* shortcut after failure in previous call; compare all items equal */
17793 if (item_compare_func_err)
17794 return 0;
17795
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017796 si1 = (sortItem_T *)s1;
17797 si2 = (sortItem_T *)s2;
17798
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017799 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017800 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017801 copy_tv(&si1->item->li_tv, &argv[0]);
17802 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017803
17804 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017805 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017806 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17807 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017808 clear_tv(&argv[0]);
17809 clear_tv(&argv[1]);
17810
17811 if (res == FAIL)
17812 res = ITEM_COMPARE_FAIL;
17813 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017814 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17815 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017816 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017817 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017818
17819 /* When the result would be zero, compare the pointers themselves. Makes
17820 * the sort stable. */
17821 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017822 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017823
Bram Moolenaar0d660222005-01-07 21:51:51 +000017824 return res;
17825}
17826
17827/*
17828 * "sort({list})" function
17829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017831do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017832 typval_T *argvars;
17833 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017834 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835{
Bram Moolenaar33570922005-01-25 22:26:29 +000017836 list_T *l;
17837 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017838 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017839 long len;
17840 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841
Bram Moolenaar0d660222005-01-07 21:51:51 +000017842 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017843 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844 else
17845 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017846 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017847 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017848 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17849 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017850 return;
17851 rettv->vval.v_list = l;
17852 rettv->v_type = VAR_LIST;
17853 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854
Bram Moolenaar0d660222005-01-07 21:51:51 +000017855 len = list_len(l);
17856 if (len <= 1)
17857 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858
Bram Moolenaar0d660222005-01-07 21:51:51 +000017859 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017860 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017861 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017862 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017863 if (argvars[1].v_type != VAR_UNKNOWN)
17864 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017865 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017866 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017867 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017868 else
17869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017870 int error = FALSE;
17871
17872 i = get_tv_number_chk(&argvars[1], &error);
17873 if (error)
17874 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017875 if (i == 1)
17876 item_compare_ic = TRUE;
17877 else
17878 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017879 if (item_compare_func != NULL)
17880 {
17881 if (STRCMP(item_compare_func, "n") == 0)
17882 {
17883 item_compare_func = NULL;
17884 item_compare_numeric = TRUE;
17885 }
17886 else if (STRCMP(item_compare_func, "i") == 0)
17887 {
17888 item_compare_func = NULL;
17889 item_compare_ic = TRUE;
17890 }
17891 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017892 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017893
17894 if (argvars[2].v_type != VAR_UNKNOWN)
17895 {
17896 /* optional third argument: {dict} */
17897 if (argvars[2].v_type != VAR_DICT)
17898 {
17899 EMSG(_(e_dictreq));
17900 return;
17901 }
17902 item_compare_selfdict = argvars[2].vval.v_dict;
17903 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905
Bram Moolenaar0d660222005-01-07 21:51:51 +000017906 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017907 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017908 if (ptrs == NULL)
17909 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910
Bram Moolenaar327aa022014-03-25 18:24:23 +010017911 i = 0;
17912 if (sort)
17913 {
17914 /* sort(): ptrs will be the list to sort */
17915 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017916 {
17917 ptrs[i].item = li;
17918 ptrs[i].idx = i;
17919 ++i;
17920 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017921
17922 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017923 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017924 /* test the compare function */
17925 if (item_compare_func != NULL
17926 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017927 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017928 EMSG(_("E702: Sort compare function failed"));
17929 else
17930 {
17931 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017932 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017933 item_compare_func == NULL ? item_compare : item_compare2);
17934
17935 if (!item_compare_func_err)
17936 {
17937 /* Clear the List and append the items in sorted order. */
17938 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17939 l->lv_len = 0;
17940 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017941 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017942 }
17943 }
17944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017946 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017947 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17948
17949 /* f_uniq(): ptrs will be a stack of items to remove */
17950 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017951 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017952 item_compare_func_ptr = item_compare_func
17953 ? item_compare2 : item_compare;
17954
17955 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17956 li = li->li_next)
17957 {
17958 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17959 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017960 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017961 if (item_compare_func_err)
17962 {
17963 EMSG(_("E882: Uniq compare function failed"));
17964 break;
17965 }
17966 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017968 if (!item_compare_func_err)
17969 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017970 while (--i >= 0)
17971 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017972 li = ptrs[i].item->li_next;
17973 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017974 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017975 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017976 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017977 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017978 list_fix_watch(l, li);
17979 listitem_free(li);
17980 l->lv_len--;
17981 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017982 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017983 }
17984
17985 vim_free(ptrs);
17986 }
17987}
17988
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017989/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017990 * "sort({list})" function
17991 */
17992 static void
17993f_sort(argvars, rettv)
17994 typval_T *argvars;
17995 typval_T *rettv;
17996{
17997 do_sort_uniq(argvars, rettv, TRUE);
17998}
17999
18000/*
18001 * "uniq({list})" function
18002 */
18003 static void
18004f_uniq(argvars, rettv)
18005 typval_T *argvars;
18006 typval_T *rettv;
18007{
18008 do_sort_uniq(argvars, rettv, FALSE);
18009}
18010
18011/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018012 * "soundfold({word})" function
18013 */
18014 static void
18015f_soundfold(argvars, rettv)
18016 typval_T *argvars;
18017 typval_T *rettv;
18018{
18019 char_u *s;
18020
18021 rettv->v_type = VAR_STRING;
18022 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018023#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018024 rettv->vval.v_string = eval_soundfold(s);
18025#else
18026 rettv->vval.v_string = vim_strsave(s);
18027#endif
18028}
18029
18030/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018031 * "spellbadword()" function
18032 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018033 static void
18034f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018035 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018036 typval_T *rettv;
18037{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018038 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018039 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018040 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018041
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018042 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018043 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018044
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018045#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018046 if (argvars[0].v_type == VAR_UNKNOWN)
18047 {
18048 /* Find the start and length of the badly spelled word. */
18049 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18050 if (len != 0)
18051 word = ml_get_cursor();
18052 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018053 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018054 {
18055 char_u *str = get_tv_string_chk(&argvars[0]);
18056 int capcol = -1;
18057
18058 if (str != NULL)
18059 {
18060 /* Check the argument for spelling. */
18061 while (*str != NUL)
18062 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018063 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018064 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018065 {
18066 word = str;
18067 break;
18068 }
18069 str += len;
18070 }
18071 }
18072 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018073#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018074
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018075 list_append_string(rettv->vval.v_list, word, len);
18076 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018077 attr == HLF_SPB ? "bad" :
18078 attr == HLF_SPR ? "rare" :
18079 attr == HLF_SPL ? "local" :
18080 attr == HLF_SPC ? "caps" :
18081 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018082}
18083
18084/*
18085 * "spellsuggest()" function
18086 */
18087 static void
18088f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018089 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018090 typval_T *rettv;
18091{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018092#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018093 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018094 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018095 int maxcount;
18096 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018097 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018098 listitem_T *li;
18099 int need_capital = FALSE;
18100#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018101
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018102 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018103 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018104
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018105#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018106 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018107 {
18108 str = get_tv_string(&argvars[0]);
18109 if (argvars[1].v_type != VAR_UNKNOWN)
18110 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018111 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018112 if (maxcount <= 0)
18113 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018114 if (argvars[2].v_type != VAR_UNKNOWN)
18115 {
18116 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18117 if (typeerr)
18118 return;
18119 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018120 }
18121 else
18122 maxcount = 25;
18123
Bram Moolenaar4770d092006-01-12 23:22:24 +000018124 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018125
18126 for (i = 0; i < ga.ga_len; ++i)
18127 {
18128 str = ((char_u **)ga.ga_data)[i];
18129
18130 li = listitem_alloc();
18131 if (li == NULL)
18132 vim_free(str);
18133 else
18134 {
18135 li->li_tv.v_type = VAR_STRING;
18136 li->li_tv.v_lock = 0;
18137 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018138 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018139 }
18140 }
18141 ga_clear(&ga);
18142 }
18143#endif
18144}
18145
Bram Moolenaar0d660222005-01-07 21:51:51 +000018146 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018147f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018148 typval_T *argvars;
18149 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018150{
18151 char_u *str;
18152 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018153 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018154 regmatch_T regmatch;
18155 char_u patbuf[NUMBUFLEN];
18156 char_u *save_cpo;
18157 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018158 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018159 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018160 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018161
18162 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18163 save_cpo = p_cpo;
18164 p_cpo = (char_u *)"";
18165
18166 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018167 if (argvars[1].v_type != VAR_UNKNOWN)
18168 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018169 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18170 if (pat == NULL)
18171 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018172 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018173 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018174 }
18175 if (pat == NULL || *pat == NUL)
18176 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018177
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018178 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018180 if (typeerr)
18181 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182
Bram Moolenaar0d660222005-01-07 21:51:51 +000018183 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18184 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018186 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018187 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018188 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018189 if (*str == NUL)
18190 match = FALSE; /* empty item at the end */
18191 else
18192 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018193 if (match)
18194 end = regmatch.startp[0];
18195 else
18196 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018197 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18198 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018199 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018200 if (list_append_string(rettv->vval.v_list, str,
18201 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018202 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018203 }
18204 if (!match)
18205 break;
18206 /* Advance to just after the match. */
18207 if (regmatch.endp[0] > str)
18208 col = 0;
18209 else
18210 {
18211 /* Don't get stuck at the same match. */
18212#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018213 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018214#else
18215 col = 1;
18216#endif
18217 }
18218 str = regmatch.endp[0];
18219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220
Bram Moolenaar473de612013-06-08 18:19:48 +020018221 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223
Bram Moolenaar0d660222005-01-07 21:51:51 +000018224 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225}
18226
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018227#ifdef FEAT_FLOAT
18228/*
18229 * "sqrt()" function
18230 */
18231 static void
18232f_sqrt(argvars, rettv)
18233 typval_T *argvars;
18234 typval_T *rettv;
18235{
18236 float_T f;
18237
18238 rettv->v_type = VAR_FLOAT;
18239 if (get_float_arg(argvars, &f) == OK)
18240 rettv->vval.v_float = sqrt(f);
18241 else
18242 rettv->vval.v_float = 0.0;
18243}
18244
18245/*
18246 * "str2float()" function
18247 */
18248 static void
18249f_str2float(argvars, rettv)
18250 typval_T *argvars;
18251 typval_T *rettv;
18252{
18253 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18254
18255 if (*p == '+')
18256 p = skipwhite(p + 1);
18257 (void)string2float(p, &rettv->vval.v_float);
18258 rettv->v_type = VAR_FLOAT;
18259}
18260#endif
18261
Bram Moolenaar2c932302006-03-18 21:42:09 +000018262/*
18263 * "str2nr()" function
18264 */
18265 static void
18266f_str2nr(argvars, rettv)
18267 typval_T *argvars;
18268 typval_T *rettv;
18269{
18270 int base = 10;
18271 char_u *p;
18272 long n;
18273
18274 if (argvars[1].v_type != VAR_UNKNOWN)
18275 {
18276 base = get_tv_number(&argvars[1]);
18277 if (base != 8 && base != 10 && base != 16)
18278 {
18279 EMSG(_(e_invarg));
18280 return;
18281 }
18282 }
18283
18284 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018285 if (*p == '+')
18286 p = skipwhite(p + 1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020018287 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018288 rettv->vval.v_number = n;
18289}
18290
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291#ifdef HAVE_STRFTIME
18292/*
18293 * "strftime({format}[, {time}])" function
18294 */
18295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018296f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018297 typval_T *argvars;
18298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299{
18300 char_u result_buf[256];
18301 struct tm *curtime;
18302 time_t seconds;
18303 char_u *p;
18304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018305 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018307 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018308 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 seconds = time(NULL);
18310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018311 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312 curtime = localtime(&seconds);
18313 /* MSVC returns NULL for an invalid value of seconds. */
18314 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018315 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 else
18317 {
18318# ifdef FEAT_MBYTE
18319 vimconv_T conv;
18320 char_u *enc;
18321
18322 conv.vc_type = CONV_NONE;
18323 enc = enc_locale();
18324 convert_setup(&conv, p_enc, enc);
18325 if (conv.vc_type != CONV_NONE)
18326 p = string_convert(&conv, p, NULL);
18327# endif
18328 if (p != NULL)
18329 (void)strftime((char *)result_buf, sizeof(result_buf),
18330 (char *)p, curtime);
18331 else
18332 result_buf[0] = NUL;
18333
18334# ifdef FEAT_MBYTE
18335 if (conv.vc_type != CONV_NONE)
18336 vim_free(p);
18337 convert_setup(&conv, enc, p_enc);
18338 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018339 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 else
18341# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018342 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343
18344# ifdef FEAT_MBYTE
18345 /* Release conversion descriptors */
18346 convert_setup(&conv, NULL, NULL);
18347 vim_free(enc);
18348# endif
18349 }
18350}
18351#endif
18352
18353/*
18354 * "stridx()" function
18355 */
18356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018357f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018358 typval_T *argvars;
18359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360{
18361 char_u buf[NUMBUFLEN];
18362 char_u *needle;
18363 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018364 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018366 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018368 needle = get_tv_string_chk(&argvars[1]);
18369 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018370 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018371 if (needle == NULL || haystack == NULL)
18372 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373
Bram Moolenaar33570922005-01-25 22:26:29 +000018374 if (argvars[2].v_type != VAR_UNKNOWN)
18375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018376 int error = FALSE;
18377
18378 start_idx = get_tv_number_chk(&argvars[2], &error);
18379 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018380 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018381 if (start_idx >= 0)
18382 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018383 }
18384
18385 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18386 if (pos != NULL)
18387 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388}
18389
18390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018391 * "string()" function
18392 */
18393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018394f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018395 typval_T *argvars;
18396 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018397{
18398 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018399 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018401 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018402 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018403 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018404 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018405 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406}
18407
18408/*
18409 * "strlen()" function
18410 */
18411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018412f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018413 typval_T *argvars;
18414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018416 rettv->vval.v_number = (varnumber_T)(STRLEN(
18417 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418}
18419
18420/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018421 * "strchars()" function
18422 */
18423 static void
18424f_strchars(argvars, rettv)
18425 typval_T *argvars;
18426 typval_T *rettv;
18427{
18428 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018429 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018430#ifdef FEAT_MBYTE
18431 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018432 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018433#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018434
18435 if (argvars[1].v_type != VAR_UNKNOWN)
18436 skipcc = get_tv_number_chk(&argvars[1], NULL);
18437 if (skipcc < 0 || skipcc > 1)
18438 EMSG(_(e_invarg));
18439 else
18440 {
18441#ifdef FEAT_MBYTE
18442 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18443 while (*s != NUL)
18444 {
18445 func_mb_ptr2char_adv(&s);
18446 ++len;
18447 }
18448 rettv->vval.v_number = len;
18449#else
18450 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18451#endif
18452 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018453}
18454
18455/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018456 * "strdisplaywidth()" function
18457 */
18458 static void
18459f_strdisplaywidth(argvars, rettv)
18460 typval_T *argvars;
18461 typval_T *rettv;
18462{
18463 char_u *s = get_tv_string(&argvars[0]);
18464 int col = 0;
18465
18466 if (argvars[1].v_type != VAR_UNKNOWN)
18467 col = get_tv_number(&argvars[1]);
18468
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018469 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018470}
18471
18472/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018473 * "strwidth()" function
18474 */
18475 static void
18476f_strwidth(argvars, rettv)
18477 typval_T *argvars;
18478 typval_T *rettv;
18479{
18480 char_u *s = get_tv_string(&argvars[0]);
18481
18482 rettv->vval.v_number = (varnumber_T)(
18483#ifdef FEAT_MBYTE
18484 mb_string2cells(s, -1)
18485#else
18486 STRLEN(s)
18487#endif
18488 );
18489}
18490
18491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492 * "strpart()" function
18493 */
18494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018495f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018496 typval_T *argvars;
18497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498{
18499 char_u *p;
18500 int n;
18501 int len;
18502 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018503 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018505 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506 slen = (int)STRLEN(p);
18507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018508 n = get_tv_number_chk(&argvars[1], &error);
18509 if (error)
18510 len = 0;
18511 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018512 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513 else
18514 len = slen - n; /* default len: all bytes that are available. */
18515
18516 /*
18517 * Only return the overlap between the specified part and the actual
18518 * string.
18519 */
18520 if (n < 0)
18521 {
18522 len += n;
18523 n = 0;
18524 }
18525 else if (n > slen)
18526 n = slen;
18527 if (len < 0)
18528 len = 0;
18529 else if (n + len > slen)
18530 len = slen - n;
18531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018532 rettv->v_type = VAR_STRING;
18533 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534}
18535
18536/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018537 * "strridx()" function
18538 */
18539 static void
18540f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018541 typval_T *argvars;
18542 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018543{
18544 char_u buf[NUMBUFLEN];
18545 char_u *needle;
18546 char_u *haystack;
18547 char_u *rest;
18548 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018549 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018550
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018551 needle = get_tv_string_chk(&argvars[1]);
18552 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018553
18554 rettv->vval.v_number = -1;
18555 if (needle == NULL || haystack == NULL)
18556 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018557
18558 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018559 if (argvars[2].v_type != VAR_UNKNOWN)
18560 {
18561 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018562 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018563 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018564 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018565 }
18566 else
18567 end_idx = haystack_len;
18568
Bram Moolenaar0d660222005-01-07 21:51:51 +000018569 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018570 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018571 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018572 lastmatch = haystack + end_idx;
18573 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018574 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018575 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018576 for (rest = haystack; *rest != '\0'; ++rest)
18577 {
18578 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018579 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018580 break;
18581 lastmatch = rest;
18582 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018583 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018584
18585 if (lastmatch == NULL)
18586 rettv->vval.v_number = -1;
18587 else
18588 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18589}
18590
18591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 * "strtrans()" function
18593 */
18594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018595f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018596 typval_T *argvars;
18597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018599 rettv->v_type = VAR_STRING;
18600 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601}
18602
18603/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018604 * "submatch()" function
18605 */
18606 static void
18607f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018608 typval_T *argvars;
18609 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018610{
Bram Moolenaar41571762014-04-02 19:00:58 +020018611 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018612 int no;
18613 int retList = 0;
18614
18615 no = (int)get_tv_number_chk(&argvars[0], &error);
18616 if (error)
18617 return;
18618 error = FALSE;
18619 if (argvars[1].v_type != VAR_UNKNOWN)
18620 retList = get_tv_number_chk(&argvars[1], &error);
18621 if (error)
18622 return;
18623
18624 if (retList == 0)
18625 {
18626 rettv->v_type = VAR_STRING;
18627 rettv->vval.v_string = reg_submatch(no);
18628 }
18629 else
18630 {
18631 rettv->v_type = VAR_LIST;
18632 rettv->vval.v_list = reg_submatch_list(no);
18633 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018634}
18635
18636/*
18637 * "substitute()" function
18638 */
18639 static void
18640f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018641 typval_T *argvars;
18642 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018643{
18644 char_u patbuf[NUMBUFLEN];
18645 char_u subbuf[NUMBUFLEN];
18646 char_u flagsbuf[NUMBUFLEN];
18647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018648 char_u *str = get_tv_string_chk(&argvars[0]);
18649 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18650 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18651 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18652
Bram Moolenaar0d660222005-01-07 21:51:51 +000018653 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018654 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18655 rettv->vval.v_string = NULL;
18656 else
18657 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018658}
18659
18660/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018661 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018665 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667{
18668 int id = 0;
18669#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018670 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 long col;
18672 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018673 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018675 lnum = get_tv_lnum(argvars); /* -1 on type error */
18676 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18677 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018679 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018680 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018681 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682#endif
18683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685}
18686
18687/*
18688 * "synIDattr(id, what [, mode])" function
18689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018691f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018692 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694{
18695 char_u *p = NULL;
18696#ifdef FEAT_SYN_HL
18697 int id;
18698 char_u *what;
18699 char_u *mode;
18700 char_u modebuf[NUMBUFLEN];
18701 int modec;
18702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018703 id = get_tv_number(&argvars[0]);
18704 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018705 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018707 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018709 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710 modec = 0; /* replace invalid with current */
18711 }
18712 else
18713 {
18714#ifdef FEAT_GUI
18715 if (gui.in_use)
18716 modec = 'g';
18717 else
18718#endif
18719 if (t_colors > 1)
18720 modec = 'c';
18721 else
18722 modec = 't';
18723 }
18724
18725
18726 switch (TOLOWER_ASC(what[0]))
18727 {
18728 case 'b':
18729 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18730 p = highlight_color(id, what, modec);
18731 else /* bold */
18732 p = highlight_has_attr(id, HL_BOLD, modec);
18733 break;
18734
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018735 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736 p = highlight_color(id, what, modec);
18737 break;
18738
18739 case 'i':
18740 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18741 p = highlight_has_attr(id, HL_INVERSE, modec);
18742 else /* italic */
18743 p = highlight_has_attr(id, HL_ITALIC, modec);
18744 break;
18745
18746 case 'n': /* name */
18747 p = get_highlight_name(NULL, id - 1);
18748 break;
18749
18750 case 'r': /* reverse */
18751 p = highlight_has_attr(id, HL_INVERSE, modec);
18752 break;
18753
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018754 case 's':
18755 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18756 p = highlight_color(id, what, modec);
18757 else /* standout */
18758 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 break;
18760
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018761 case 'u':
18762 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18763 /* underline */
18764 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18765 else
18766 /* undercurl */
18767 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 break;
18769 }
18770
18771 if (p != NULL)
18772 p = vim_strsave(p);
18773#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018774 rettv->v_type = VAR_STRING;
18775 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776}
18777
18778/*
18779 * "synIDtrans(id)" function
18780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018782f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018783 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785{
18786 int id;
18787
18788#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018789 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790
18791 if (id > 0)
18792 id = syn_get_final_id(id);
18793 else
18794#endif
18795 id = 0;
18796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018797 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798}
18799
18800/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018801 * "synconcealed(lnum, col)" function
18802 */
18803 static void
18804f_synconcealed(argvars, rettv)
18805 typval_T *argvars UNUSED;
18806 typval_T *rettv;
18807{
18808#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18809 long lnum;
18810 long col;
18811 int syntax_flags = 0;
18812 int cchar;
18813 int matchid = 0;
18814 char_u str[NUMBUFLEN];
18815#endif
18816
18817 rettv->v_type = VAR_LIST;
18818 rettv->vval.v_list = NULL;
18819
18820#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18821 lnum = get_tv_lnum(argvars); /* -1 on type error */
18822 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18823
18824 vim_memset(str, NUL, sizeof(str));
18825
18826 if (rettv_list_alloc(rettv) != FAIL)
18827 {
18828 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18829 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18830 && curwin->w_p_cole > 0)
18831 {
18832 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18833 syntax_flags = get_syntax_info(&matchid);
18834
18835 /* get the conceal character */
18836 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18837 {
18838 cchar = syn_get_sub_char();
18839 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18840 cchar = lcs_conceal;
18841 if (cchar != NUL)
18842 {
18843# ifdef FEAT_MBYTE
18844 if (has_mbyte)
18845 (*mb_char2bytes)(cchar, str);
18846 else
18847# endif
18848 str[0] = cchar;
18849 }
18850 }
18851 }
18852
18853 list_append_number(rettv->vval.v_list,
18854 (syntax_flags & HL_CONCEAL) != 0);
18855 /* -1 to auto-determine strlen */
18856 list_append_string(rettv->vval.v_list, str, -1);
18857 list_append_number(rettv->vval.v_list, matchid);
18858 }
18859#endif
18860}
18861
18862/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018863 * "synstack(lnum, col)" function
18864 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018865 static void
18866f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018867 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018868 typval_T *rettv;
18869{
18870#ifdef FEAT_SYN_HL
18871 long lnum;
18872 long col;
18873 int i;
18874 int id;
18875#endif
18876
18877 rettv->v_type = VAR_LIST;
18878 rettv->vval.v_list = NULL;
18879
18880#ifdef FEAT_SYN_HL
18881 lnum = get_tv_lnum(argvars); /* -1 on type error */
18882 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18883
18884 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018885 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018886 && rettv_list_alloc(rettv) != FAIL)
18887 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018888 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018889 for (i = 0; ; ++i)
18890 {
18891 id = syn_get_stack_item(i);
18892 if (id < 0)
18893 break;
18894 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18895 break;
18896 }
18897 }
18898#endif
18899}
18900
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018902get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018903 typval_T *argvars;
18904 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018905 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018907 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018909 char_u *infile = NULL;
18910 char_u buf[NUMBUFLEN];
18911 int err = FALSE;
18912 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018913 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018914 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018916 rettv->v_type = VAR_STRING;
18917 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018918 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018919 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018920
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018921 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018922 {
18923 /*
18924 * Write the string to a temp file, to be used for input of the shell
18925 * command.
18926 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018927 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018928 {
18929 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018930 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018931 }
18932
18933 fd = mch_fopen((char *)infile, WRITEBIN);
18934 if (fd == NULL)
18935 {
18936 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018937 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018938 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018939 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018940 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018941 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18942 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018943 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018944 else
18945 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018946 size_t len;
18947
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018948 p = get_tv_string_buf_chk(&argvars[1], buf);
18949 if (p == NULL)
18950 {
18951 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018952 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018953 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018954 len = STRLEN(p);
18955 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018956 err = TRUE;
18957 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018958 if (fclose(fd) != 0)
18959 err = TRUE;
18960 if (err)
18961 {
18962 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018963 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018964 }
18965 }
18966
Bram Moolenaar52a72462014-08-29 15:53:52 +020018967 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18968 * echoes typeahead, that messes up the display. */
18969 if (!msg_silent)
18970 flags += SHELL_COOKED;
18971
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018972 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018974 int len;
18975 listitem_T *li;
18976 char_u *s = NULL;
18977 char_u *start;
18978 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018979 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980
Bram Moolenaar52a72462014-08-29 15:53:52 +020018981 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018982 if (res == NULL)
18983 goto errret;
18984
18985 list = list_alloc();
18986 if (list == NULL)
18987 goto errret;
18988
18989 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018991 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018992 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018993 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018994 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018995
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018996 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018997 if (s == NULL)
18998 goto errret;
18999
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019000 for (p = s; start < end; ++p, ++start)
19001 *p = *start == NUL ? NL : *start;
19002 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019003
19004 li = listitem_alloc();
19005 if (li == NULL)
19006 {
19007 vim_free(s);
19008 goto errret;
19009 }
19010 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019011 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019012 li->li_tv.vval.v_string = s;
19013 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019015
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019016 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019017 rettv->v_type = VAR_LIST;
19018 rettv->vval.v_list = list;
19019 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019021 else
19022 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019023 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019024#ifdef USE_CR
19025 /* translate <CR> into <NL> */
19026 if (res != NULL)
19027 {
19028 char_u *s;
19029
19030 for (s = res; *s; ++s)
19031 {
19032 if (*s == CAR)
19033 *s = NL;
19034 }
19035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036#else
19037# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019038 /* translate <CR><NL> into <NL> */
19039 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019041 char_u *s, *d;
19042
19043 d = res;
19044 for (s = res; *s; ++s)
19045 {
19046 if (s[0] == CAR && s[1] == NL)
19047 ++s;
19048 *d++ = *s;
19049 }
19050 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052# endif
19053#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019054 rettv->vval.v_string = res;
19055 res = NULL;
19056 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019057
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019058errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019059 if (infile != NULL)
19060 {
19061 mch_remove(infile);
19062 vim_free(infile);
19063 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019064 if (res != NULL)
19065 vim_free(res);
19066 if (list != NULL)
19067 list_free(list, TRUE);
19068}
19069
19070/*
19071 * "system()" function
19072 */
19073 static void
19074f_system(argvars, rettv)
19075 typval_T *argvars;
19076 typval_T *rettv;
19077{
19078 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19079}
19080
19081/*
19082 * "systemlist()" function
19083 */
19084 static void
19085f_systemlist(argvars, rettv)
19086 typval_T *argvars;
19087 typval_T *rettv;
19088{
19089 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090}
19091
19092/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019093 * "tabpagebuflist()" function
19094 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019095 static void
19096f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019097 typval_T *argvars UNUSED;
19098 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019099{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019100#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019101 tabpage_T *tp;
19102 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019103
19104 if (argvars[0].v_type == VAR_UNKNOWN)
19105 wp = firstwin;
19106 else
19107 {
19108 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19109 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019110 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019111 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019112 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019113 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019114 for (; wp != NULL; wp = wp->w_next)
19115 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019116 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019117 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019118 }
19119#endif
19120}
19121
19122
19123/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019124 * "tabpagenr()" function
19125 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019126 static void
19127f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019128 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019129 typval_T *rettv;
19130{
19131 int nr = 1;
19132#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019133 char_u *arg;
19134
19135 if (argvars[0].v_type != VAR_UNKNOWN)
19136 {
19137 arg = get_tv_string_chk(&argvars[0]);
19138 nr = 0;
19139 if (arg != NULL)
19140 {
19141 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019142 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019143 else
19144 EMSG2(_(e_invexpr2), arg);
19145 }
19146 }
19147 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019148 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019149#endif
19150 rettv->vval.v_number = nr;
19151}
19152
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019153
19154#ifdef FEAT_WINDOWS
19155static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19156
19157/*
19158 * Common code for tabpagewinnr() and winnr().
19159 */
19160 static int
19161get_winnr(tp, argvar)
19162 tabpage_T *tp;
19163 typval_T *argvar;
19164{
19165 win_T *twin;
19166 int nr = 1;
19167 win_T *wp;
19168 char_u *arg;
19169
19170 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19171 if (argvar->v_type != VAR_UNKNOWN)
19172 {
19173 arg = get_tv_string_chk(argvar);
19174 if (arg == NULL)
19175 nr = 0; /* type error; errmsg already given */
19176 else if (STRCMP(arg, "$") == 0)
19177 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19178 else if (STRCMP(arg, "#") == 0)
19179 {
19180 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19181 if (twin == NULL)
19182 nr = 0;
19183 }
19184 else
19185 {
19186 EMSG2(_(e_invexpr2), arg);
19187 nr = 0;
19188 }
19189 }
19190
19191 if (nr > 0)
19192 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19193 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019194 {
19195 if (wp == NULL)
19196 {
19197 /* didn't find it in this tabpage */
19198 nr = 0;
19199 break;
19200 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019201 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019202 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019203 return nr;
19204}
19205#endif
19206
19207/*
19208 * "tabpagewinnr()" function
19209 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019210 static void
19211f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019212 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019213 typval_T *rettv;
19214{
19215 int nr = 1;
19216#ifdef FEAT_WINDOWS
19217 tabpage_T *tp;
19218
19219 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19220 if (tp == NULL)
19221 nr = 0;
19222 else
19223 nr = get_winnr(tp, &argvars[1]);
19224#endif
19225 rettv->vval.v_number = nr;
19226}
19227
19228
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019229/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019230 * "tagfiles()" function
19231 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019232 static void
19233f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019234 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019235 typval_T *rettv;
19236{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019237 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019238 tagname_T tn;
19239 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019240
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019241 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019242 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019243 fname = alloc(MAXPATHL);
19244 if (fname == NULL)
19245 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019246
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019247 for (first = TRUE; ; first = FALSE)
19248 if (get_tagfname(&tn, first, fname) == FAIL
19249 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019250 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019251 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019252 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019253}
19254
19255/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019256 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019257 */
19258 static void
19259f_taglist(argvars, rettv)
19260 typval_T *argvars;
19261 typval_T *rettv;
19262{
19263 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019264
19265 tag_pattern = get_tv_string(&argvars[0]);
19266
19267 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019268 if (*tag_pattern == NUL)
19269 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019270
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019271 if (rettv_list_alloc(rettv) == OK)
19272 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019273}
19274
19275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276 * "tempname()" function
19277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019279f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019280 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282{
19283 static int x = 'A';
19284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019285 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019286 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287
19288 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19289 * names. Skip 'I' and 'O', they are used for shell redirection. */
19290 do
19291 {
19292 if (x == 'Z')
19293 x = '0';
19294 else if (x == '9')
19295 x = 'A';
19296 else
19297 {
19298#ifdef EBCDIC
19299 if (x == 'I')
19300 x = 'J';
19301 else if (x == 'R')
19302 x = 'S';
19303 else
19304#endif
19305 ++x;
19306 }
19307 } while (x == 'I' || x == 'O');
19308}
19309
19310/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019311 * "test(list)" function: Just checking the walls...
19312 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019313 static void
19314f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019315 typval_T *argvars UNUSED;
19316 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019317{
19318 /* Used for unit testing. Change the code below to your liking. */
19319#if 0
19320 listitem_T *li;
19321 list_T *l;
19322 char_u *bad, *good;
19323
19324 if (argvars[0].v_type != VAR_LIST)
19325 return;
19326 l = argvars[0].vval.v_list;
19327 if (l == NULL)
19328 return;
19329 li = l->lv_first;
19330 if (li == NULL)
19331 return;
19332 bad = get_tv_string(&li->li_tv);
19333 li = li->li_next;
19334 if (li == NULL)
19335 return;
19336 good = get_tv_string(&li->li_tv);
19337 rettv->vval.v_number = test_edit_score(bad, good);
19338#endif
19339}
19340
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019341#ifdef FEAT_FLOAT
19342/*
19343 * "tan()" function
19344 */
19345 static void
19346f_tan(argvars, rettv)
19347 typval_T *argvars;
19348 typval_T *rettv;
19349{
19350 float_T f;
19351
19352 rettv->v_type = VAR_FLOAT;
19353 if (get_float_arg(argvars, &f) == OK)
19354 rettv->vval.v_float = tan(f);
19355 else
19356 rettv->vval.v_float = 0.0;
19357}
19358
19359/*
19360 * "tanh()" function
19361 */
19362 static void
19363f_tanh(argvars, rettv)
19364 typval_T *argvars;
19365 typval_T *rettv;
19366{
19367 float_T f;
19368
19369 rettv->v_type = VAR_FLOAT;
19370 if (get_float_arg(argvars, &f) == OK)
19371 rettv->vval.v_float = tanh(f);
19372 else
19373 rettv->vval.v_float = 0.0;
19374}
19375#endif
19376
Bram Moolenaard52d9742005-08-21 22:20:28 +000019377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 * "tolower(string)" function
19379 */
19380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019381f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019382 typval_T *argvars;
19383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384{
19385 char_u *p;
19386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019387 p = vim_strsave(get_tv_string(&argvars[0]));
19388 rettv->v_type = VAR_STRING;
19389 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390
19391 if (p != NULL)
19392 while (*p != NUL)
19393 {
19394#ifdef FEAT_MBYTE
19395 int l;
19396
19397 if (enc_utf8)
19398 {
19399 int c, lc;
19400
19401 c = utf_ptr2char(p);
19402 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019403 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 /* TODO: reallocate string when byte count changes. */
19405 if (utf_char2len(lc) == l)
19406 utf_char2bytes(lc, p);
19407 p += l;
19408 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019409 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 p += l; /* skip multi-byte character */
19411 else
19412#endif
19413 {
19414 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19415 ++p;
19416 }
19417 }
19418}
19419
19420/*
19421 * "toupper(string)" function
19422 */
19423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019424f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019425 typval_T *argvars;
19426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019428 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019429 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430}
19431
19432/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019433 * "tr(string, fromstr, tostr)" function
19434 */
19435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019436f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019437 typval_T *argvars;
19438 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019439{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019440 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019441 char_u *fromstr;
19442 char_u *tostr;
19443 char_u *p;
19444#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019445 int inlen;
19446 int fromlen;
19447 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019448 int idx;
19449 char_u *cpstr;
19450 int cplen;
19451 int first = TRUE;
19452#endif
19453 char_u buf[NUMBUFLEN];
19454 char_u buf2[NUMBUFLEN];
19455 garray_T ga;
19456
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019457 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019458 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19459 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019460
19461 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019462 rettv->v_type = VAR_STRING;
19463 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019464 if (fromstr == NULL || tostr == NULL)
19465 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019466 ga_init2(&ga, (int)sizeof(char), 80);
19467
19468#ifdef FEAT_MBYTE
19469 if (!has_mbyte)
19470#endif
19471 /* not multi-byte: fromstr and tostr must be the same length */
19472 if (STRLEN(fromstr) != STRLEN(tostr))
19473 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019474#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019475error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019476#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019477 EMSG2(_(e_invarg2), fromstr);
19478 ga_clear(&ga);
19479 return;
19480 }
19481
19482 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019483 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019484 {
19485#ifdef FEAT_MBYTE
19486 if (has_mbyte)
19487 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019488 inlen = (*mb_ptr2len)(in_str);
19489 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019490 cplen = inlen;
19491 idx = 0;
19492 for (p = fromstr; *p != NUL; p += fromlen)
19493 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019494 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019495 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019496 {
19497 for (p = tostr; *p != NUL; p += tolen)
19498 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019499 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019500 if (idx-- == 0)
19501 {
19502 cplen = tolen;
19503 cpstr = p;
19504 break;
19505 }
19506 }
19507 if (*p == NUL) /* tostr is shorter than fromstr */
19508 goto error;
19509 break;
19510 }
19511 ++idx;
19512 }
19513
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019514 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019515 {
19516 /* Check that fromstr and tostr have the same number of
19517 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019518 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019519 first = FALSE;
19520 for (p = tostr; *p != NUL; p += tolen)
19521 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019522 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019523 --idx;
19524 }
19525 if (idx != 0)
19526 goto error;
19527 }
19528
19529 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019530 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019531 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019532
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019533 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019534 }
19535 else
19536#endif
19537 {
19538 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019539 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019540 if (p != NULL)
19541 ga_append(&ga, tostr[p - fromstr]);
19542 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019543 ga_append(&ga, *in_str);
19544 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019545 }
19546 }
19547
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019548 /* add a terminating NUL */
19549 ga_grow(&ga, 1);
19550 ga_append(&ga, NUL);
19551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019552 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019553}
19554
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019555#ifdef FEAT_FLOAT
19556/*
19557 * "trunc({float})" function
19558 */
19559 static void
19560f_trunc(argvars, rettv)
19561 typval_T *argvars;
19562 typval_T *rettv;
19563{
19564 float_T f;
19565
19566 rettv->v_type = VAR_FLOAT;
19567 if (get_float_arg(argvars, &f) == OK)
19568 /* trunc() is not in C90, use floor() or ceil() instead. */
19569 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19570 else
19571 rettv->vval.v_float = 0.0;
19572}
19573#endif
19574
Bram Moolenaar8299df92004-07-10 09:47:34 +000019575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 * "type(expr)" function
19577 */
19578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019579f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019580 typval_T *argvars;
19581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019583 int n;
19584
19585 switch (argvars[0].v_type)
19586 {
19587 case VAR_NUMBER: n = 0; break;
19588 case VAR_STRING: n = 1; break;
19589 case VAR_FUNC: n = 2; break;
19590 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019591 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019592#ifdef FEAT_FLOAT
19593 case VAR_FLOAT: n = 5; break;
19594#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019595 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19596 }
19597 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598}
19599
19600/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019601 * "undofile(name)" function
19602 */
19603 static void
19604f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019605 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019606 typval_T *rettv;
19607{
19608 rettv->v_type = VAR_STRING;
19609#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019610 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019611 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019612
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019613 if (*fname == NUL)
19614 {
19615 /* If there is no file name there will be no undo file. */
19616 rettv->vval.v_string = NULL;
19617 }
19618 else
19619 {
19620 char_u *ffname = FullName_save(fname, FALSE);
19621
19622 if (ffname != NULL)
19623 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19624 vim_free(ffname);
19625 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019626 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019627#else
19628 rettv->vval.v_string = NULL;
19629#endif
19630}
19631
19632/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019633 * "undotree()" function
19634 */
19635 static void
19636f_undotree(argvars, rettv)
19637 typval_T *argvars UNUSED;
19638 typval_T *rettv;
19639{
19640 if (rettv_dict_alloc(rettv) == OK)
19641 {
19642 dict_T *dict = rettv->vval.v_dict;
19643 list_T *list;
19644
Bram Moolenaar730cde92010-06-27 05:18:54 +020019645 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019646 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019647 dict_add_nr_str(dict, "save_last",
19648 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019649 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19650 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019651 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019652
19653 list = list_alloc();
19654 if (list != NULL)
19655 {
19656 u_eval_tree(curbuf->b_u_oldhead, list);
19657 dict_add_list(dict, "entries", list);
19658 }
19659 }
19660}
19661
19662/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019663 * "values(dict)" function
19664 */
19665 static void
19666f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019667 typval_T *argvars;
19668 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019669{
19670 dict_list(argvars, rettv, 1);
19671}
19672
19673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 * "virtcol(string)" function
19675 */
19676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019677f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019678 typval_T *argvars;
19679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680{
19681 colnr_T vcol = 0;
19682 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019683 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019685 fp = var2fpos(&argvars[0], FALSE, &fnum);
19686 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19687 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 {
19689 getvvcol(curwin, fp, NULL, NULL, &vcol);
19690 ++vcol;
19691 }
19692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019693 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694}
19695
19696/*
19697 * "visualmode()" function
19698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019700f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019701 typval_T *argvars UNUSED;
19702 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704 char_u str[2];
19705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019706 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707 str[0] = curbuf->b_visual_mode_eval;
19708 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019709 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710
19711 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019712 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714}
19715
19716/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019717 * "wildmenumode()" function
19718 */
19719 static void
19720f_wildmenumode(argvars, rettv)
19721 typval_T *argvars UNUSED;
19722 typval_T *rettv UNUSED;
19723{
19724#ifdef FEAT_WILDMENU
19725 if (wild_menu_showing)
19726 rettv->vval.v_number = 1;
19727#endif
19728}
19729
19730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 * "winbufnr(nr)" function
19732 */
19733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019735 typval_T *argvars;
19736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737{
19738 win_T *wp;
19739
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019740 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019742 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019744 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745}
19746
19747/*
19748 * "wincol()" function
19749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019751f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019752 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754{
19755 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019756 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757}
19758
19759/*
19760 * "winheight(nr)" function
19761 */
19762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019763f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019764 typval_T *argvars;
19765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766{
19767 win_T *wp;
19768
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019769 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019771 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774}
19775
19776/*
19777 * "winline()" function
19778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019780f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019781 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783{
19784 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019785 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786}
19787
19788/*
19789 * "winnr()" function
19790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019792f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019793 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795{
19796 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019797
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019799 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019801 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802}
19803
19804/*
19805 * "winrestcmd()" function
19806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019808f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019809 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811{
19812#ifdef FEAT_WINDOWS
19813 win_T *wp;
19814 int winnr = 1;
19815 garray_T ga;
19816 char_u buf[50];
19817
19818 ga_init2(&ga, (int)sizeof(char), 70);
19819 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19820 {
19821 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19822 ga_concat(&ga, buf);
19823# ifdef FEAT_VERTSPLIT
19824 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19825 ga_concat(&ga, buf);
19826# endif
19827 ++winnr;
19828 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019829 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019833 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019835 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836}
19837
19838/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019839 * "winrestview()" function
19840 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019841 static void
19842f_winrestview(argvars, rettv)
19843 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019844 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019845{
19846 dict_T *dict;
19847
19848 if (argvars[0].v_type != VAR_DICT
19849 || (dict = argvars[0].vval.v_dict) == NULL)
19850 EMSG(_(e_invarg));
19851 else
19852 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019853 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19854 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19855 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19856 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019857#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019858 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19859 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019860#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019861 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19862 {
19863 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19864 curwin->w_set_curswant = FALSE;
19865 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019866
Bram Moolenaar82c25852014-05-28 16:47:16 +020019867 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19868 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019869#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019870 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19871 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019872#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019873 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19874 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19875 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19876 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019877
19878 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019879 win_new_height(curwin, curwin->w_height);
19880# ifdef FEAT_VERTSPLIT
19881 win_new_width(curwin, W_WIDTH(curwin));
19882# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019883 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019884
Bram Moolenaarb851a962014-10-31 15:45:52 +010019885 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019886 curwin->w_topline = 1;
19887 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19888 curwin->w_topline = curbuf->b_ml.ml_line_count;
19889#ifdef FEAT_DIFF
19890 check_topfill(curwin, TRUE);
19891#endif
19892 }
19893}
19894
19895/*
19896 * "winsaveview()" function
19897 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019898 static void
19899f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019900 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019901 typval_T *rettv;
19902{
19903 dict_T *dict;
19904
Bram Moolenaara800b422010-06-27 01:15:55 +020019905 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019906 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019907 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019908
19909 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19910 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19911#ifdef FEAT_VIRTUALEDIT
19912 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19913#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019914 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019915 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19916
19917 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19918#ifdef FEAT_DIFF
19919 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19920#endif
19921 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19922 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19923}
19924
19925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 * "winwidth(nr)" function
19927 */
19928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019929f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019930 typval_T *argvars;
19931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932{
19933 win_T *wp;
19934
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019935 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019937 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 else
19939#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019940 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019942 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943#endif
19944}
19945
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019947 * Write list of strings to file
19948 */
19949 static int
19950write_list(fd, list, binary)
19951 FILE *fd;
19952 list_T *list;
19953 int binary;
19954{
19955 listitem_T *li;
19956 int c;
19957 int ret = OK;
19958 char_u *s;
19959
19960 for (li = list->lv_first; li != NULL; li = li->li_next)
19961 {
19962 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19963 {
19964 if (*s == '\n')
19965 c = putc(NUL, fd);
19966 else
19967 c = putc(*s, fd);
19968 if (c == EOF)
19969 {
19970 ret = FAIL;
19971 break;
19972 }
19973 }
19974 if (!binary || li->li_next != NULL)
19975 if (putc('\n', fd) == EOF)
19976 {
19977 ret = FAIL;
19978 break;
19979 }
19980 if (ret == FAIL)
19981 {
19982 EMSG(_(e_write));
19983 break;
19984 }
19985 }
19986 return ret;
19987}
19988
19989/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019990 * "writefile()" function
19991 */
19992 static void
19993f_writefile(argvars, rettv)
19994 typval_T *argvars;
19995 typval_T *rettv;
19996{
19997 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019998 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019999 char_u *fname;
20000 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020001 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020002
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020003 if (check_restricted() || check_secure())
20004 return;
20005
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020006 if (argvars[0].v_type != VAR_LIST)
20007 {
20008 EMSG2(_(e_listarg), "writefile()");
20009 return;
20010 }
20011 if (argvars[0].vval.v_list == NULL)
20012 return;
20013
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020014 if (argvars[2].v_type != VAR_UNKNOWN)
20015 {
20016 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20017 binary = TRUE;
20018 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20019 append = TRUE;
20020 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020021
20022 /* Always open the file in binary mode, library functions have a mind of
20023 * their own about CR-LF conversion. */
20024 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020025 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20026 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020027 {
20028 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20029 ret = -1;
20030 }
20031 else
20032 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020033 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20034 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020035 fclose(fd);
20036 }
20037
20038 rettv->vval.v_number = ret;
20039}
20040
20041/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020042 * "xor(expr, expr)" function
20043 */
20044 static void
20045f_xor(argvars, rettv)
20046 typval_T *argvars;
20047 typval_T *rettv;
20048{
20049 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20050 ^ get_tv_number_chk(&argvars[1], NULL);
20051}
20052
20053
20054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020056 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 */
20058 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020059var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020060 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020061 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020062 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020064 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020066 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067
Bram Moolenaara5525202006-03-02 22:52:09 +000020068 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020069 if (varp->v_type == VAR_LIST)
20070 {
20071 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020072 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020073 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020074 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020075
20076 l = varp->vval.v_list;
20077 if (l == NULL)
20078 return NULL;
20079
20080 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020081 pos.lnum = list_find_nr(l, 0L, &error);
20082 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020083 return NULL; /* invalid line number */
20084
20085 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020086 pos.col = list_find_nr(l, 1L, &error);
20087 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020088 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020089 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020090
20091 /* We accept "$" for the column number: last column. */
20092 li = list_find(l, 1L);
20093 if (li != NULL && li->li_tv.v_type == VAR_STRING
20094 && li->li_tv.vval.v_string != NULL
20095 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20096 pos.col = len + 1;
20097
Bram Moolenaara5525202006-03-02 22:52:09 +000020098 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020099 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020100 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020101 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020102
Bram Moolenaara5525202006-03-02 22:52:09 +000020103#ifdef FEAT_VIRTUALEDIT
20104 /* Get the virtual offset. Defaults to zero. */
20105 pos.coladd = list_find_nr(l, 2L, &error);
20106 if (error)
20107 pos.coladd = 0;
20108#endif
20109
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020110 return &pos;
20111 }
20112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020113 name = get_tv_string_chk(varp);
20114 if (name == NULL)
20115 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020116 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020118 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20119 {
20120 if (VIsual_active)
20121 return &VIsual;
20122 return &curwin->w_cursor;
20123 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020124 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020126 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20128 return NULL;
20129 return pp;
20130 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020131
20132#ifdef FEAT_VIRTUALEDIT
20133 pos.coladd = 0;
20134#endif
20135
Bram Moolenaar477933c2007-07-17 14:32:23 +000020136 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020137 {
20138 pos.col = 0;
20139 if (name[1] == '0') /* "w0": first visible line */
20140 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020141 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020142 pos.lnum = curwin->w_topline;
20143 return &pos;
20144 }
20145 else if (name[1] == '$') /* "w$": last visible line */
20146 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020147 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020148 pos.lnum = curwin->w_botline - 1;
20149 return &pos;
20150 }
20151 }
20152 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020154 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155 {
20156 pos.lnum = curbuf->b_ml.ml_line_count;
20157 pos.col = 0;
20158 }
20159 else
20160 {
20161 pos.lnum = curwin->w_cursor.lnum;
20162 pos.col = (colnr_T)STRLEN(ml_get_curline());
20163 }
20164 return &pos;
20165 }
20166 return NULL;
20167}
20168
20169/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020170 * Convert list in "arg" into a position and optional file number.
20171 * When "fnump" is NULL there is no file number, only 3 items.
20172 * Note that the column is passed on as-is, the caller may want to decrement
20173 * it to use 1 for the first column.
20174 * Return FAIL when conversion is not possible, doesn't check the position for
20175 * validity.
20176 */
20177 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020178list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020179 typval_T *arg;
20180 pos_T *posp;
20181 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020182 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020183{
20184 list_T *l = arg->vval.v_list;
20185 long i = 0;
20186 long n;
20187
Bram Moolenaar493c1782014-05-28 14:34:46 +020020188 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20189 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020190 if (arg->v_type != VAR_LIST
20191 || l == NULL
20192 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020193 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020194 return FAIL;
20195
20196 if (fnump != NULL)
20197 {
20198 n = list_find_nr(l, i++, NULL); /* fnum */
20199 if (n < 0)
20200 return FAIL;
20201 if (n == 0)
20202 n = curbuf->b_fnum; /* current buffer */
20203 *fnump = n;
20204 }
20205
20206 n = list_find_nr(l, i++, NULL); /* lnum */
20207 if (n < 0)
20208 return FAIL;
20209 posp->lnum = n;
20210
20211 n = list_find_nr(l, i++, NULL); /* col */
20212 if (n < 0)
20213 return FAIL;
20214 posp->col = n;
20215
20216#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020217 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020218 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020219 posp->coladd = 0;
20220 else
20221 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020222#endif
20223
Bram Moolenaar493c1782014-05-28 14:34:46 +020020224 if (curswantp != NULL)
20225 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20226
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020227 return OK;
20228}
20229
20230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 * Get the length of an environment variable name.
20232 * Advance "arg" to the first character after the name.
20233 * Return 0 for error.
20234 */
20235 static int
20236get_env_len(arg)
20237 char_u **arg;
20238{
20239 char_u *p;
20240 int len;
20241
20242 for (p = *arg; vim_isIDc(*p); ++p)
20243 ;
20244 if (p == *arg) /* no name found */
20245 return 0;
20246
20247 len = (int)(p - *arg);
20248 *arg = p;
20249 return len;
20250}
20251
20252/*
20253 * Get the length of the name of a function or internal variable.
20254 * "arg" is advanced to the first non-white character after the name.
20255 * Return 0 if something is wrong.
20256 */
20257 static int
20258get_id_len(arg)
20259 char_u **arg;
20260{
20261 char_u *p;
20262 int len;
20263
20264 /* Find the end of the name. */
20265 for (p = *arg; eval_isnamec(*p); ++p)
20266 ;
20267 if (p == *arg) /* no name found */
20268 return 0;
20269
20270 len = (int)(p - *arg);
20271 *arg = skipwhite(p);
20272
20273 return len;
20274}
20275
20276/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020277 * Get the length of the name of a variable or function.
20278 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020280 * Return -1 if curly braces expansion failed.
20281 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 * If the name contains 'magic' {}'s, expand them and return the
20283 * expanded name in an allocated string via 'alias' - caller must free.
20284 */
20285 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020286get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 char_u **arg;
20288 char_u **alias;
20289 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020290 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291{
20292 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 char_u *p;
20294 char_u *expr_start;
20295 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296
20297 *alias = NULL; /* default to no alias */
20298
20299 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20300 && (*arg)[2] == (int)KE_SNR)
20301 {
20302 /* hard coded <SNR>, already translated */
20303 *arg += 3;
20304 return get_id_len(arg) + 3;
20305 }
20306 len = eval_fname_script(*arg);
20307 if (len > 0)
20308 {
20309 /* literal "<SID>", "s:" or "<SNR>" */
20310 *arg += len;
20311 }
20312
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020314 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020316 p = find_name_end(*arg, &expr_start, &expr_end,
20317 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 if (expr_start != NULL)
20319 {
20320 char_u *temp_string;
20321
20322 if (!evaluate)
20323 {
20324 len += (int)(p - *arg);
20325 *arg = skipwhite(p);
20326 return len;
20327 }
20328
20329 /*
20330 * Include any <SID> etc in the expanded string:
20331 * Thus the -len here.
20332 */
20333 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20334 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020335 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336 *alias = temp_string;
20337 *arg = skipwhite(p);
20338 return (int)STRLEN(temp_string);
20339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340
20341 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020342 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 EMSG2(_(e_invexpr2), *arg);
20344
20345 return len;
20346}
20347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020348/*
20349 * Find the end of a variable or function name, taking care of magic braces.
20350 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20351 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020352 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020353 * Return a pointer to just after the name. Equal to "arg" if there is no
20354 * valid name.
20355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020357find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 char_u *arg;
20359 char_u **expr_start;
20360 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020361 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020363 int mb_nest = 0;
20364 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 char_u *p;
20366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020367 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020369 *expr_start = NULL;
20370 *expr_end = NULL;
20371 }
20372
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020373 /* Quick check for valid starting character. */
20374 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20375 return arg;
20376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020377 for (p = arg; *p != NUL
20378 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020379 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020380 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020382 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020383 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020384 if (*p == '\'')
20385 {
20386 /* skip over 'string' to avoid counting [ and ] inside it. */
20387 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20388 ;
20389 if (*p == NUL)
20390 break;
20391 }
20392 else if (*p == '"')
20393 {
20394 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20395 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20396 if (*p == '\\' && p[1] != NUL)
20397 ++p;
20398 if (*p == NUL)
20399 break;
20400 }
20401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020402 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020404 if (*p == '[')
20405 ++br_nest;
20406 else if (*p == ']')
20407 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020410 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020412 if (*p == '{')
20413 {
20414 mb_nest++;
20415 if (expr_start != NULL && *expr_start == NULL)
20416 *expr_start = p;
20417 }
20418 else if (*p == '}')
20419 {
20420 mb_nest--;
20421 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20422 *expr_end = p;
20423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425 }
20426
20427 return p;
20428}
20429
20430/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020431 * Expands out the 'magic' {}'s in a variable/function name.
20432 * Note that this can call itself recursively, to deal with
20433 * constructs like foo{bar}{baz}{bam}
20434 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20435 * "in_start" ^
20436 * "expr_start" ^
20437 * "expr_end" ^
20438 * "in_end" ^
20439 *
20440 * Returns a new allocated string, which the caller must free.
20441 * Returns NULL for failure.
20442 */
20443 static char_u *
20444make_expanded_name(in_start, expr_start, expr_end, in_end)
20445 char_u *in_start;
20446 char_u *expr_start;
20447 char_u *expr_end;
20448 char_u *in_end;
20449{
20450 char_u c1;
20451 char_u *retval = NULL;
20452 char_u *temp_result;
20453 char_u *nextcmd = NULL;
20454
20455 if (expr_end == NULL || in_end == NULL)
20456 return NULL;
20457 *expr_start = NUL;
20458 *expr_end = NUL;
20459 c1 = *in_end;
20460 *in_end = NUL;
20461
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020462 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020463 if (temp_result != NULL && nextcmd == NULL)
20464 {
20465 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20466 + (in_end - expr_end) + 1));
20467 if (retval != NULL)
20468 {
20469 STRCPY(retval, in_start);
20470 STRCAT(retval, temp_result);
20471 STRCAT(retval, expr_end + 1);
20472 }
20473 }
20474 vim_free(temp_result);
20475
20476 *in_end = c1; /* put char back for error messages */
20477 *expr_start = '{';
20478 *expr_end = '}';
20479
20480 if (retval != NULL)
20481 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020482 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020483 if (expr_start != NULL)
20484 {
20485 /* Further expansion! */
20486 temp_result = make_expanded_name(retval, expr_start,
20487 expr_end, temp_result);
20488 vim_free(retval);
20489 retval = temp_result;
20490 }
20491 }
20492
20493 return retval;
20494}
20495
20496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020498 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 */
20500 static int
20501eval_isnamec(c)
20502 int c;
20503{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020504 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20505}
20506
20507/*
20508 * Return TRUE if character "c" can be used as the first character in a
20509 * variable or function name (excluding '{' and '}').
20510 */
20511 static int
20512eval_isnamec1(c)
20513 int c;
20514{
20515 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516}
20517
20518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519 * Set number v: variable to "val".
20520 */
20521 void
20522set_vim_var_nr(idx, val)
20523 int idx;
20524 long val;
20525{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020526 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527}
20528
20529/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020530 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 */
20532 long
20533get_vim_var_nr(idx)
20534 int idx;
20535{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020536 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537}
20538
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020539/*
20540 * Get string v: variable value. Uses a static buffer, can only be used once.
20541 */
20542 char_u *
20543get_vim_var_str(idx)
20544 int idx;
20545{
20546 return get_tv_string(&vimvars[idx].vv_tv);
20547}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020548
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020550 * Get List v: variable value. Caller must take care of reference count when
20551 * needed.
20552 */
20553 list_T *
20554get_vim_var_list(idx)
20555 int idx;
20556{
20557 return vimvars[idx].vv_list;
20558}
20559
20560/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020561 * Set v:char to character "c".
20562 */
20563 void
20564set_vim_var_char(c)
20565 int c;
20566{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020567 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020568
20569#ifdef FEAT_MBYTE
20570 if (has_mbyte)
20571 buf[(*mb_char2bytes)(c, buf)] = NUL;
20572 else
20573#endif
20574 {
20575 buf[0] = c;
20576 buf[1] = NUL;
20577 }
20578 set_vim_var_string(VV_CHAR, buf, -1);
20579}
20580
20581/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020582 * Set v:count to "count" and v:count1 to "count1".
20583 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 */
20585 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020586set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 long count;
20588 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020589 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020591 if (set_prevcount)
20592 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020593 vimvars[VV_COUNT].vv_nr = count;
20594 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595}
20596
20597/*
20598 * Set string v: variable to a copy of "val".
20599 */
20600 void
20601set_vim_var_string(idx, val, len)
20602 int idx;
20603 char_u *val;
20604 int len; /* length of "val" to use or -1 (whole string) */
20605{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020606 /* Need to do this (at least) once, since we can't initialize a union.
20607 * Will always be invoked when "v:progname" is set. */
20608 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20609
Bram Moolenaare9a41262005-01-15 22:18:47 +000020610 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020612 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020614 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020616 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617}
20618
20619/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020620 * Set List v: variable to "val".
20621 */
20622 void
20623set_vim_var_list(idx, val)
20624 int idx;
20625 list_T *val;
20626{
20627 list_unref(vimvars[idx].vv_list);
20628 vimvars[idx].vv_list = val;
20629 if (val != NULL)
20630 ++val->lv_refcount;
20631}
20632
20633/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020634 * Set Dictionary v: variable to "val".
20635 */
20636 void
20637set_vim_var_dict(idx, val)
20638 int idx;
20639 dict_T *val;
20640{
20641 int todo;
20642 hashitem_T *hi;
20643
20644 dict_unref(vimvars[idx].vv_dict);
20645 vimvars[idx].vv_dict = val;
20646 if (val != NULL)
20647 {
20648 ++val->dv_refcount;
20649
20650 /* Set readonly */
20651 todo = (int)val->dv_hashtab.ht_used;
20652 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20653 {
20654 if (HASHITEM_EMPTY(hi))
20655 continue;
20656 --todo;
20657 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20658 }
20659 }
20660}
20661
20662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 * Set v:register if needed.
20664 */
20665 void
20666set_reg_var(c)
20667 int c;
20668{
20669 char_u regname;
20670
20671 if (c == 0 || c == ' ')
20672 regname = '"';
20673 else
20674 regname = c;
20675 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020676 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677 set_vim_var_string(VV_REG, &regname, 1);
20678}
20679
20680/*
20681 * Get or set v:exception. If "oldval" == NULL, return the current value.
20682 * Otherwise, restore the value to "oldval" and return NULL.
20683 * Must always be called in pairs to save and restore v:exception! Does not
20684 * take care of memory allocations.
20685 */
20686 char_u *
20687v_exception(oldval)
20688 char_u *oldval;
20689{
20690 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020691 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692
Bram Moolenaare9a41262005-01-15 22:18:47 +000020693 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694 return NULL;
20695}
20696
20697/*
20698 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20699 * Otherwise, restore the value to "oldval" and return NULL.
20700 * Must always be called in pairs to save and restore v:throwpoint! Does not
20701 * take care of memory allocations.
20702 */
20703 char_u *
20704v_throwpoint(oldval)
20705 char_u *oldval;
20706{
20707 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020708 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709
Bram Moolenaare9a41262005-01-15 22:18:47 +000020710 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 return NULL;
20712}
20713
20714#if defined(FEAT_AUTOCMD) || defined(PROTO)
20715/*
20716 * Set v:cmdarg.
20717 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20718 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20719 * Must always be called in pairs!
20720 */
20721 char_u *
20722set_cmdarg(eap, oldarg)
20723 exarg_T *eap;
20724 char_u *oldarg;
20725{
20726 char_u *oldval;
20727 char_u *newval;
20728 unsigned len;
20729
Bram Moolenaare9a41262005-01-15 22:18:47 +000020730 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020731 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020733 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020734 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020735 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 }
20737
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020738 if (eap->force_bin == FORCE_BIN)
20739 len = 6;
20740 else if (eap->force_bin == FORCE_NOBIN)
20741 len = 8;
20742 else
20743 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020744
20745 if (eap->read_edit)
20746 len += 7;
20747
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020748 if (eap->force_ff != 0)
20749 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20750# ifdef FEAT_MBYTE
20751 if (eap->force_enc != 0)
20752 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020753 if (eap->bad_char != 0)
20754 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020755# endif
20756
20757 newval = alloc(len + 1);
20758 if (newval == NULL)
20759 return NULL;
20760
20761 if (eap->force_bin == FORCE_BIN)
20762 sprintf((char *)newval, " ++bin");
20763 else if (eap->force_bin == FORCE_NOBIN)
20764 sprintf((char *)newval, " ++nobin");
20765 else
20766 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020767
20768 if (eap->read_edit)
20769 STRCAT(newval, " ++edit");
20770
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020771 if (eap->force_ff != 0)
20772 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20773 eap->cmd + eap->force_ff);
20774# ifdef FEAT_MBYTE
20775 if (eap->force_enc != 0)
20776 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20777 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020778 if (eap->bad_char == BAD_KEEP)
20779 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20780 else if (eap->bad_char == BAD_DROP)
20781 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20782 else if (eap->bad_char != 0)
20783 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020784# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020785 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020786 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787}
20788#endif
20789
20790/*
20791 * Get the value of internal variable "name".
20792 * Return OK or FAIL.
20793 */
20794 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020795get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 char_u *name;
20797 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020798 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020799 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020800 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020801 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802{
20803 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020804 typval_T *tv = NULL;
20805 typval_T atv;
20806 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808
20809 /* truncate the name, so that we can use strcmp() */
20810 cc = name[len];
20811 name[len] = NUL;
20812
20813 /*
20814 * Check for "b:changedtick".
20815 */
20816 if (STRCMP(name, "b:changedtick") == 0)
20817 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020818 atv.v_type = VAR_NUMBER;
20819 atv.vval.v_number = curbuf->b_changedtick;
20820 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 }
20822
20823 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 * Check for user-defined variables.
20825 */
20826 else
20827 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020828 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020830 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020831 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020832 if (dip != NULL)
20833 *dip = v;
20834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 }
20836
Bram Moolenaare9a41262005-01-15 22:18:47 +000020837 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020839 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020840 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 ret = FAIL;
20842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020843 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020844 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845
20846 name[len] = cc;
20847
20848 return ret;
20849}
20850
20851/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020852 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20853 * Also handle function call with Funcref variable: func(expr)
20854 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20855 */
20856 static int
20857handle_subscript(arg, rettv, evaluate, verbose)
20858 char_u **arg;
20859 typval_T *rettv;
20860 int evaluate; /* do more than finding the end */
20861 int verbose; /* give error messages */
20862{
20863 int ret = OK;
20864 dict_T *selfdict = NULL;
20865 char_u *s;
20866 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020867 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020868
20869 while (ret == OK
20870 && (**arg == '['
20871 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020872 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020873 && !vim_iswhite(*(*arg - 1)))
20874 {
20875 if (**arg == '(')
20876 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020877 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020878 if (evaluate)
20879 {
20880 functv = *rettv;
20881 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020882
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020883 /* Invoke the function. Recursive! */
20884 s = functv.vval.v_string;
20885 }
20886 else
20887 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020888 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020889 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20890 &len, evaluate, selfdict);
20891
20892 /* Clear the funcref afterwards, so that deleting it while
20893 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020894 if (evaluate)
20895 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020896
20897 /* Stop the expression evaluation when immediately aborting on
20898 * error, or when an interrupt occurred or an exception was thrown
20899 * but not caught. */
20900 if (aborting())
20901 {
20902 if (ret == OK)
20903 clear_tv(rettv);
20904 ret = FAIL;
20905 }
20906 dict_unref(selfdict);
20907 selfdict = NULL;
20908 }
20909 else /* **arg == '[' || **arg == '.' */
20910 {
20911 dict_unref(selfdict);
20912 if (rettv->v_type == VAR_DICT)
20913 {
20914 selfdict = rettv->vval.v_dict;
20915 if (selfdict != NULL)
20916 ++selfdict->dv_refcount;
20917 }
20918 else
20919 selfdict = NULL;
20920 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20921 {
20922 clear_tv(rettv);
20923 ret = FAIL;
20924 }
20925 }
20926 }
20927 dict_unref(selfdict);
20928 return ret;
20929}
20930
20931/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020932 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020933 * value).
20934 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020935 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020936alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020937{
Bram Moolenaar33570922005-01-25 22:26:29 +000020938 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020939}
20940
20941/*
20942 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 * The string "s" must have been allocated, it is consumed.
20944 * Return NULL for out of memory, the variable otherwise.
20945 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020946 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020947alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 char_u *s;
20949{
Bram Moolenaar33570922005-01-25 22:26:29 +000020950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020952 rettv = alloc_tv();
20953 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020955 rettv->v_type = VAR_STRING;
20956 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 }
20958 else
20959 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020960 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961}
20962
20963/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020964 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020966 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020967free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020968 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969{
20970 if (varp != NULL)
20971 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020972 switch (varp->v_type)
20973 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020974 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020975 func_unref(varp->vval.v_string);
20976 /*FALLTHROUGH*/
20977 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020978 vim_free(varp->vval.v_string);
20979 break;
20980 case VAR_LIST:
20981 list_unref(varp->vval.v_list);
20982 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020983 case VAR_DICT:
20984 dict_unref(varp->vval.v_dict);
20985 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020986 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020987#ifdef FEAT_FLOAT
20988 case VAR_FLOAT:
20989#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020990 case VAR_UNKNOWN:
20991 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020992 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020993 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020994 break;
20995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 vim_free(varp);
20997 }
20998}
20999
21000/*
21001 * Free the memory for a variable value and set the value to NULL or 0.
21002 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021003 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021004clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021005 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006{
21007 if (varp != NULL)
21008 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021009 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021011 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021012 func_unref(varp->vval.v_string);
21013 /*FALLTHROUGH*/
21014 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021015 vim_free(varp->vval.v_string);
21016 varp->vval.v_string = NULL;
21017 break;
21018 case VAR_LIST:
21019 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021020 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021021 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021022 case VAR_DICT:
21023 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021024 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021025 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021026 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021027 varp->vval.v_number = 0;
21028 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021029#ifdef FEAT_FLOAT
21030 case VAR_FLOAT:
21031 varp->vval.v_float = 0.0;
21032 break;
21033#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021034 case VAR_UNKNOWN:
21035 break;
21036 default:
21037 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021039 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040 }
21041}
21042
21043/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021044 * Set the value of a variable to NULL without freeing items.
21045 */
21046 static void
21047init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021048 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021049{
21050 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021051 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021052}
21053
21054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 * Get the number value of a variable.
21056 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021057 * For incompatible types, return 0.
21058 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21059 * caller of incompatible types: it sets *denote to TRUE if "denote"
21060 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061 */
21062 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021063get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021064 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021066 int error = FALSE;
21067
21068 return get_tv_number_chk(varp, &error); /* return 0L on error */
21069}
21070
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021071 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021072get_tv_number_chk(varp, denote)
21073 typval_T *varp;
21074 int *denote;
21075{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021076 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021078 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021080 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021081 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021082#ifdef FEAT_FLOAT
21083 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021084 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021085 break;
21086#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021087 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021088 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021089 break;
21090 case VAR_STRING:
21091 if (varp->vval.v_string != NULL)
21092 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020021093 TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021094 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021095 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021096 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021097 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021098 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021099 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021100 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021101 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021102 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021103 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021105 if (denote == NULL) /* useful for values that must be unsigned */
21106 n = -1;
21107 else
21108 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021109 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110}
21111
21112/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021113 * Get the lnum from the first argument.
21114 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021115 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 */
21117 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021118get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021119 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120{
Bram Moolenaar33570922005-01-25 22:26:29 +000021121 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 linenr_T lnum;
21123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021124 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 if (lnum == 0) /* no valid number, try using line() */
21126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021127 rettv.v_type = VAR_NUMBER;
21128 f_line(argvars, &rettv);
21129 lnum = rettv.vval.v_number;
21130 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 }
21132 return lnum;
21133}
21134
21135/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021136 * Get the lnum from the first argument.
21137 * Also accepts "$", then "buf" is used.
21138 * Returns 0 on error.
21139 */
21140 static linenr_T
21141get_tv_lnum_buf(argvars, buf)
21142 typval_T *argvars;
21143 buf_T *buf;
21144{
21145 if (argvars[0].v_type == VAR_STRING
21146 && argvars[0].vval.v_string != NULL
21147 && argvars[0].vval.v_string[0] == '$'
21148 && buf != NULL)
21149 return buf->b_ml.ml_line_count;
21150 return get_tv_number_chk(&argvars[0], NULL);
21151}
21152
21153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 * Get the string value of a variable.
21155 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021156 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21157 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 * If the String variable has never been set, return an empty string.
21159 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021160 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21161 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 */
21163 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021164get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021165 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166{
21167 static char_u mybuf[NUMBUFLEN];
21168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021169 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170}
21171
21172 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021173get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021174 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021175 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021177 char_u *res = get_tv_string_buf_chk(varp, buf);
21178
21179 return res != NULL ? res : (char_u *)"";
21180}
21181
Bram Moolenaar7d647822014-04-05 21:28:56 +020021182/*
21183 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21184 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021185 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021186get_tv_string_chk(varp)
21187 typval_T *varp;
21188{
21189 static char_u mybuf[NUMBUFLEN];
21190
21191 return get_tv_string_buf_chk(varp, mybuf);
21192}
21193
21194 static char_u *
21195get_tv_string_buf_chk(varp, buf)
21196 typval_T *varp;
21197 char_u *buf;
21198{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021199 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021201 case VAR_NUMBER:
21202 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21203 return buf;
21204 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021205 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021206 break;
21207 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021208 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021209 break;
21210 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021211 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021212 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021213#ifdef FEAT_FLOAT
21214 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021215 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021216 break;
21217#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021218 case VAR_STRING:
21219 if (varp->vval.v_string != NULL)
21220 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021221 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021222 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021223 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021224 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021226 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227}
21228
21229/*
21230 * Find variable "name" in the list of variables.
21231 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021232 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021233 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021234 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021236 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021237find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021239 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021240 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021243 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244
Bram Moolenaara7043832005-01-21 11:56:39 +000021245 ht = find_var_ht(name, &varname);
21246 if (htp != NULL)
21247 *htp = ht;
21248 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021250 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251}
21252
21253/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021254 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021255 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021257 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021258find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021259 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021260 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021261 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021262 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021263{
Bram Moolenaar33570922005-01-25 22:26:29 +000021264 hashitem_T *hi;
21265
21266 if (*varname == NUL)
21267 {
21268 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021269 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021270 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021271 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021272 case 'g': return &globvars_var;
21273 case 'v': return &vimvars_var;
21274 case 'b': return &curbuf->b_bufvar;
21275 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021276#ifdef FEAT_WINDOWS
21277 case 't': return &curtab->tp_winvar;
21278#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021279 case 'l': return current_funccal == NULL
21280 ? NULL : &current_funccal->l_vars_var;
21281 case 'a': return current_funccal == NULL
21282 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021283 }
21284 return NULL;
21285 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021286
21287 hi = hash_find(ht, varname);
21288 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021289 {
21290 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021291 * worked find the variable again. Don't auto-load a script if it was
21292 * loaded already, otherwise it would be loaded every time when
21293 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021294 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021295 {
21296 /* Note: script_autoload() may make "hi" invalid. It must either
21297 * be obtained again or not used. */
21298 if (!script_autoload(varname, FALSE) || aborting())
21299 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021300 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021301 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021302 if (HASHITEM_EMPTY(hi))
21303 return NULL;
21304 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021305 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021306}
21307
21308/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021309 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021310 * Set "varname" to the start of name without ':'.
21311 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021312 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021313find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 char_u *name;
21315 char_u **varname;
21316{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021317 hashitem_T *hi;
21318
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 if (name[1] != ':')
21320 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021321 /* The name must not start with a colon or #. */
21322 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 return NULL;
21324 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021325
21326 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021327 hi = hash_find(&compat_hashtab, name);
21328 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021329 return &compat_hashtab;
21330
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021332 return &globvarht; /* global variable */
21333 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 }
21335 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021336 if (*name == 'g') /* global variable */
21337 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021338 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21339 */
21340 if (vim_strchr(name + 2, ':') != NULL
21341 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021342 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021344 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021346 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021347#ifdef FEAT_WINDOWS
21348 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021349 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021350#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021351 if (*name == 'v') /* v: variable */
21352 return &vimvarht;
21353 if (*name == 'a' && current_funccal != NULL) /* function argument */
21354 return &current_funccal->l_avars.dv_hashtab;
21355 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21356 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 if (*name == 's' /* script variable */
21358 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21359 return &SCRIPT_VARS(current_SID);
21360 return NULL;
21361}
21362
21363/*
21364 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021365 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 * Returns NULL when it doesn't exist.
21367 */
21368 char_u *
21369get_var_value(name)
21370 char_u *name;
21371{
Bram Moolenaar33570922005-01-25 22:26:29 +000021372 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021374 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375 if (v == NULL)
21376 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021377 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378}
21379
21380/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021381 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 * sourcing this script and when executing functions defined in the script.
21383 */
21384 void
21385new_script_vars(id)
21386 scid_T id;
21387{
Bram Moolenaara7043832005-01-21 11:56:39 +000021388 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021389 hashtab_T *ht;
21390 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021391
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21393 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021394 /* Re-allocating ga_data means that an ht_array pointing to
21395 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021396 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021397 for (i = 1; i <= ga_scripts.ga_len; ++i)
21398 {
21399 ht = &SCRIPT_VARS(i);
21400 if (ht->ht_mask == HT_INIT_SIZE - 1)
21401 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021402 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021403 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021404 }
21405
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 while (ga_scripts.ga_len < id)
21407 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021408 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021409 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021410 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 }
21413 }
21414}
21415
21416/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021417 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21418 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 */
21420 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021421init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021422 dict_T *dict;
21423 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021424 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425{
Bram Moolenaar33570922005-01-25 22:26:29 +000021426 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021427 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021428 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021429 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021430 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021431 dict_var->di_tv.vval.v_dict = dict;
21432 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021433 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021434 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21435 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021436}
21437
21438/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021439 * Unreference a dictionary initialized by init_var_dict().
21440 */
21441 void
21442unref_var_dict(dict)
21443 dict_T *dict;
21444{
21445 /* Now the dict needs to be freed if no one else is using it, go back to
21446 * normal reference counting. */
21447 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21448 dict_unref(dict);
21449}
21450
21451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021453 * Frees all allocated variables and the value they contain.
21454 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 */
21456 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021457vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021458 hashtab_T *ht;
21459{
21460 vars_clear_ext(ht, TRUE);
21461}
21462
21463/*
21464 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21465 */
21466 static void
21467vars_clear_ext(ht, free_val)
21468 hashtab_T *ht;
21469 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470{
Bram Moolenaara7043832005-01-21 11:56:39 +000021471 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021472 hashitem_T *hi;
21473 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474
Bram Moolenaar33570922005-01-25 22:26:29 +000021475 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021476 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021477 for (hi = ht->ht_array; todo > 0; ++hi)
21478 {
21479 if (!HASHITEM_EMPTY(hi))
21480 {
21481 --todo;
21482
Bram Moolenaar33570922005-01-25 22:26:29 +000021483 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021484 * ht_array might change then. hash_clear() takes care of it
21485 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021486 v = HI2DI(hi);
21487 if (free_val)
21488 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021489 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021490 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021491 }
21492 }
21493 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021494 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495}
21496
Bram Moolenaara7043832005-01-21 11:56:39 +000021497/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021498 * Delete a variable from hashtab "ht" at item "hi".
21499 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021502delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021503 hashtab_T *ht;
21504 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021505{
Bram Moolenaar33570922005-01-25 22:26:29 +000021506 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021507
21508 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021509 clear_tv(&di->di_tv);
21510 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511}
21512
21513/*
21514 * List the value of one internal variable.
21515 */
21516 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021517list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021518 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021520 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021522 char_u *tofree;
21523 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021524 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021525
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021526 current_copyID += COPYID_INC;
21527 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021528 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021529 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021530 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531}
21532
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021534list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 char_u *prefix;
21536 char_u *name;
21537 int type;
21538 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021539 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540{
Bram Moolenaar31859182007-08-14 20:41:13 +000021541 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21542 msg_start();
21543 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 if (name != NULL) /* "a:" vars don't have a name stored */
21545 msg_puts(name);
21546 msg_putchar(' ');
21547 msg_advance(22);
21548 if (type == VAR_NUMBER)
21549 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021550 else if (type == VAR_FUNC)
21551 msg_putchar('*');
21552 else if (type == VAR_LIST)
21553 {
21554 msg_putchar('[');
21555 if (*string == '[')
21556 ++string;
21557 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021558 else if (type == VAR_DICT)
21559 {
21560 msg_putchar('{');
21561 if (*string == '{')
21562 ++string;
21563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 else
21565 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021566
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021568
21569 if (type == VAR_FUNC)
21570 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021571 if (*first)
21572 {
21573 msg_clr_eos();
21574 *first = FALSE;
21575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576}
21577
21578/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021579 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580 * If the variable already exists, the value is updated.
21581 * Otherwise the variable is created.
21582 */
21583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021584set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021586 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021587 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588{
Bram Moolenaar33570922005-01-25 22:26:29 +000021589 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021591 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021593 ht = find_var_ht(name, &varname);
21594 if (ht == NULL || *varname == NUL)
21595 {
21596 EMSG2(_(e_illvar), name);
21597 return;
21598 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021599 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021600
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021601 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21602 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021603
Bram Moolenaar33570922005-01-25 22:26:29 +000021604 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021606 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021607 if (var_check_ro(v->di_flags, name, FALSE)
21608 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021609 return;
21610 if (v->di_tv.v_type != tv->v_type
21611 && !((v->di_tv.v_type == VAR_STRING
21612 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021613 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021614 || tv->v_type == VAR_NUMBER))
21615#ifdef FEAT_FLOAT
21616 && !((v->di_tv.v_type == VAR_NUMBER
21617 || v->di_tv.v_type == VAR_FLOAT)
21618 && (tv->v_type == VAR_NUMBER
21619 || tv->v_type == VAR_FLOAT))
21620#endif
21621 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021622 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021623 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021624 return;
21625 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021626
21627 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021628 * Handle setting internal v: variables separately where needed to
21629 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021630 */
21631 if (ht == &vimvarht)
21632 {
21633 if (v->di_tv.v_type == VAR_STRING)
21634 {
21635 vim_free(v->di_tv.vval.v_string);
21636 if (copy || tv->v_type != VAR_STRING)
21637 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21638 else
21639 {
21640 /* Take over the string to avoid an extra alloc/free. */
21641 v->di_tv.vval.v_string = tv->vval.v_string;
21642 tv->vval.v_string = NULL;
21643 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021644 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021645 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021646 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021647 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021648 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021649 if (STRCMP(varname, "searchforward") == 0)
21650 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021651#ifdef FEAT_SEARCH_EXTRA
21652 else if (STRCMP(varname, "hlsearch") == 0)
21653 {
21654 no_hlsearch = !v->di_tv.vval.v_number;
21655 redraw_all_later(SOME_VALID);
21656 }
21657#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021658 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021659 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021660 else if (v->di_tv.v_type != tv->v_type)
21661 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021662 }
21663
21664 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 }
21666 else /* add a new variable */
21667 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021668 /* Can't add "v:" variable. */
21669 if (ht == &vimvarht)
21670 {
21671 EMSG2(_(e_illvar), name);
21672 return;
21673 }
21674
Bram Moolenaar92124a32005-06-17 22:03:40 +000021675 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021676 if (!valid_varname(varname))
21677 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021678
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021679 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21680 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021681 if (v == NULL)
21682 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021683 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021684 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021686 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 return;
21688 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021689 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021691
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021692 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021693 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021694 else
21695 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021696 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021697 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021698 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700}
21701
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021702/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021703 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021704 * Also give an error message.
21705 */
21706 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021707var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021708 int flags;
21709 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021710 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021711{
21712 if (flags & DI_FLAGS_RO)
21713 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021714 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021715 return TRUE;
21716 }
21717 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21718 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021719 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021720 return TRUE;
21721 }
21722 return FALSE;
21723}
21724
21725/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021726 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21727 * Also give an error message.
21728 */
21729 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021730var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021731 int flags;
21732 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021733 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021734{
21735 if (flags & DI_FLAGS_FIX)
21736 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021737 EMSG2(_("E795: Cannot delete variable %s"),
21738 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021739 return TRUE;
21740 }
21741 return FALSE;
21742}
21743
21744/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021745 * Check if a funcref is assigned to a valid variable name.
21746 * Return TRUE and give an error if not.
21747 */
21748 static int
21749var_check_func_name(name, new_var)
21750 char_u *name; /* points to start of variable name */
21751 int new_var; /* TRUE when creating the variable */
21752{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021753 /* Allow for w: b: s: and t:. */
21754 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021755 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21756 ? name[2] : name[0]))
21757 {
21758 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21759 name);
21760 return TRUE;
21761 }
21762 /* Don't allow hiding a function. When "v" is not NULL we might be
21763 * assigning another function to the same var, the type is checked
21764 * below. */
21765 if (new_var && function_exists(name))
21766 {
21767 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21768 name);
21769 return TRUE;
21770 }
21771 return FALSE;
21772}
21773
21774/*
21775 * Check if a variable name is valid.
21776 * Return FALSE and give an error if not.
21777 */
21778 static int
21779valid_varname(varname)
21780 char_u *varname;
21781{
21782 char_u *p;
21783
21784 for (p = varname; *p != NUL; ++p)
21785 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21786 && *p != AUTOLOAD_CHAR)
21787 {
21788 EMSG2(_(e_illvar), varname);
21789 return FALSE;
21790 }
21791 return TRUE;
21792}
21793
21794/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021795 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021796 * Also give an error message, using "name" or _("name") when use_gettext is
21797 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021798 */
21799 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021800tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021801 int lock;
21802 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021803 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021804{
21805 if (lock & VAR_LOCKED)
21806 {
21807 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021808 name == NULL ? (char_u *)_("Unknown")
21809 : use_gettext ? (char_u *)_(name)
21810 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021811 return TRUE;
21812 }
21813 if (lock & VAR_FIXED)
21814 {
21815 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021816 name == NULL ? (char_u *)_("Unknown")
21817 : use_gettext ? (char_u *)_(name)
21818 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021819 return TRUE;
21820 }
21821 return FALSE;
21822}
21823
21824/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021825 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021826 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021827 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021828 * It is OK for "from" and "to" to point to the same item. This is used to
21829 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021830 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021831 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021832copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021833 typval_T *from;
21834 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021836 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021837 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021838 switch (from->v_type)
21839 {
21840 case VAR_NUMBER:
21841 to->vval.v_number = from->vval.v_number;
21842 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021843#ifdef FEAT_FLOAT
21844 case VAR_FLOAT:
21845 to->vval.v_float = from->vval.v_float;
21846 break;
21847#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021848 case VAR_STRING:
21849 case VAR_FUNC:
21850 if (from->vval.v_string == NULL)
21851 to->vval.v_string = NULL;
21852 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021853 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021854 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021855 if (from->v_type == VAR_FUNC)
21856 func_ref(to->vval.v_string);
21857 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021858 break;
21859 case VAR_LIST:
21860 if (from->vval.v_list == NULL)
21861 to->vval.v_list = NULL;
21862 else
21863 {
21864 to->vval.v_list = from->vval.v_list;
21865 ++to->vval.v_list->lv_refcount;
21866 }
21867 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021868 case VAR_DICT:
21869 if (from->vval.v_dict == NULL)
21870 to->vval.v_dict = NULL;
21871 else
21872 {
21873 to->vval.v_dict = from->vval.v_dict;
21874 ++to->vval.v_dict->dv_refcount;
21875 }
21876 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021877 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021878 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021879 break;
21880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881}
21882
21883/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021884 * Make a copy of an item.
21885 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021886 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21887 * reference to an already copied list/dict can be used.
21888 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021889 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021890 static int
21891item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021892 typval_T *from;
21893 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021894 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021895 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021896{
21897 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021898 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021899
Bram Moolenaar33570922005-01-25 22:26:29 +000021900 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021901 {
21902 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021903 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021904 }
21905 ++recurse;
21906
21907 switch (from->v_type)
21908 {
21909 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021910#ifdef FEAT_FLOAT
21911 case VAR_FLOAT:
21912#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021913 case VAR_STRING:
21914 case VAR_FUNC:
21915 copy_tv(from, to);
21916 break;
21917 case VAR_LIST:
21918 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021919 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021920 if (from->vval.v_list == NULL)
21921 to->vval.v_list = NULL;
21922 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21923 {
21924 /* use the copy made earlier */
21925 to->vval.v_list = from->vval.v_list->lv_copylist;
21926 ++to->vval.v_list->lv_refcount;
21927 }
21928 else
21929 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21930 if (to->vval.v_list == NULL)
21931 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021932 break;
21933 case VAR_DICT:
21934 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021935 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021936 if (from->vval.v_dict == NULL)
21937 to->vval.v_dict = NULL;
21938 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21939 {
21940 /* use the copy made earlier */
21941 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21942 ++to->vval.v_dict->dv_refcount;
21943 }
21944 else
21945 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21946 if (to->vval.v_dict == NULL)
21947 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021948 break;
21949 default:
21950 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021951 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021952 }
21953 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021954 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021955}
21956
21957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 * ":echo expr1 ..." print each argument separated with a space, add a
21959 * newline at the end.
21960 * ":echon expr1 ..." print each argument plain.
21961 */
21962 void
21963ex_echo(eap)
21964 exarg_T *eap;
21965{
21966 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021967 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021968 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969 char_u *p;
21970 int needclr = TRUE;
21971 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021972 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973
21974 if (eap->skip)
21975 ++emsg_skip;
21976 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21977 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021978 /* If eval1() causes an error message the text from the command may
21979 * still need to be cleared. E.g., "echo 22,44". */
21980 need_clr_eos = needclr;
21981
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021983 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 {
21985 /*
21986 * Report the invalid expression unless the expression evaluation
21987 * has been cancelled due to an aborting error, an interrupt, or an
21988 * exception.
21989 */
21990 if (!aborting())
21991 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021992 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 break;
21994 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021995 need_clr_eos = FALSE;
21996
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 if (!eap->skip)
21998 {
21999 if (atstart)
22000 {
22001 atstart = FALSE;
22002 /* Call msg_start() after eval1(), evaluating the expression
22003 * may cause a message to appear. */
22004 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022005 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022006 /* Mark the saved text as finishing the line, so that what
22007 * follows is displayed on a new line when scrolling back
22008 * at the more prompt. */
22009 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 }
22013 else if (eap->cmdidx == CMD_echo)
22014 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022015 current_copyID += COPYID_INC;
22016 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022017 if (p != NULL)
22018 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022020 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022022 if (*p != TAB && needclr)
22023 {
22024 /* remove any text still there from the command */
22025 msg_clr_eos();
22026 needclr = FALSE;
22027 }
22028 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 }
22030 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022031 {
22032#ifdef FEAT_MBYTE
22033 if (has_mbyte)
22034 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022035 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022036
22037 (void)msg_outtrans_len_attr(p, i, echo_attr);
22038 p += i - 1;
22039 }
22040 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022042 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022045 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022047 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 arg = skipwhite(arg);
22049 }
22050 eap->nextcmd = check_nextcmd(arg);
22051
22052 if (eap->skip)
22053 --emsg_skip;
22054 else
22055 {
22056 /* remove text that may still be there from the command */
22057 if (needclr)
22058 msg_clr_eos();
22059 if (eap->cmdidx == CMD_echo)
22060 msg_end();
22061 }
22062}
22063
22064/*
22065 * ":echohl {name}".
22066 */
22067 void
22068ex_echohl(eap)
22069 exarg_T *eap;
22070{
22071 int id;
22072
22073 id = syn_name2id(eap->arg);
22074 if (id == 0)
22075 echo_attr = 0;
22076 else
22077 echo_attr = syn_id2attr(id);
22078}
22079
22080/*
22081 * ":execute expr1 ..." execute the result of an expression.
22082 * ":echomsg expr1 ..." Print a message
22083 * ":echoerr expr1 ..." Print an error
22084 * Each gets spaces around each argument and a newline at the end for
22085 * echo commands
22086 */
22087 void
22088ex_execute(eap)
22089 exarg_T *eap;
22090{
22091 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022092 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093 int ret = OK;
22094 char_u *p;
22095 garray_T ga;
22096 int len;
22097 int save_did_emsg;
22098
22099 ga_init2(&ga, 1, 80);
22100
22101 if (eap->skip)
22102 ++emsg_skip;
22103 while (*arg != NUL && *arg != '|' && *arg != '\n')
22104 {
22105 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022106 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 {
22108 /*
22109 * Report the invalid expression unless the expression evaluation
22110 * has been cancelled due to an aborting error, an interrupt, or an
22111 * exception.
22112 */
22113 if (!aborting())
22114 EMSG2(_(e_invexpr2), p);
22115 ret = FAIL;
22116 break;
22117 }
22118
22119 if (!eap->skip)
22120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022121 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122 len = (int)STRLEN(p);
22123 if (ga_grow(&ga, len + 2) == FAIL)
22124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022125 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126 ret = FAIL;
22127 break;
22128 }
22129 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022130 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 ga.ga_len += len;
22133 }
22134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022135 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136 arg = skipwhite(arg);
22137 }
22138
22139 if (ret != FAIL && ga.ga_data != NULL)
22140 {
22141 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022144 out_flush();
22145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 else if (eap->cmdidx == CMD_echoerr)
22147 {
22148 /* We don't want to abort following commands, restore did_emsg. */
22149 save_did_emsg = did_emsg;
22150 EMSG((char_u *)ga.ga_data);
22151 if (!force_abort)
22152 did_emsg = save_did_emsg;
22153 }
22154 else if (eap->cmdidx == CMD_execute)
22155 do_cmdline((char_u *)ga.ga_data,
22156 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22157 }
22158
22159 ga_clear(&ga);
22160
22161 if (eap->skip)
22162 --emsg_skip;
22163
22164 eap->nextcmd = check_nextcmd(arg);
22165}
22166
22167/*
22168 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22169 * "arg" points to the "&" or '+' when called, to "option" when returning.
22170 * Returns NULL when no option name found. Otherwise pointer to the char
22171 * after the option name.
22172 */
22173 static char_u *
22174find_option_end(arg, opt_flags)
22175 char_u **arg;
22176 int *opt_flags;
22177{
22178 char_u *p = *arg;
22179
22180 ++p;
22181 if (*p == 'g' && p[1] == ':')
22182 {
22183 *opt_flags = OPT_GLOBAL;
22184 p += 2;
22185 }
22186 else if (*p == 'l' && p[1] == ':')
22187 {
22188 *opt_flags = OPT_LOCAL;
22189 p += 2;
22190 }
22191 else
22192 *opt_flags = 0;
22193
22194 if (!ASCII_ISALPHA(*p))
22195 return NULL;
22196 *arg = p;
22197
22198 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22199 p += 4; /* termcap option */
22200 else
22201 while (ASCII_ISALPHA(*p))
22202 ++p;
22203 return p;
22204}
22205
22206/*
22207 * ":function"
22208 */
22209 void
22210ex_function(eap)
22211 exarg_T *eap;
22212{
22213 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022214 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215 int j;
22216 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022218 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 char_u *name = NULL;
22220 char_u *p;
22221 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022222 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223 garray_T newargs;
22224 garray_T newlines;
22225 int varargs = FALSE;
22226 int mustend = FALSE;
22227 int flags = 0;
22228 ufunc_T *fp;
22229 int indent;
22230 int nesting;
22231 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022232 dictitem_T *v;
22233 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022234 static int func_nr = 0; /* number for nameless function */
22235 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022236 hashtab_T *ht;
22237 int todo;
22238 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022239 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240
22241 /*
22242 * ":function" without argument: list functions.
22243 */
22244 if (ends_excmd(*eap->arg))
22245 {
22246 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022247 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022248 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022249 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022250 {
22251 if (!HASHITEM_EMPTY(hi))
22252 {
22253 --todo;
22254 fp = HI2UF(hi);
22255 if (!isdigit(*fp->uf_name))
22256 list_func_head(fp, FALSE);
22257 }
22258 }
22259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 eap->nextcmd = check_nextcmd(eap->arg);
22261 return;
22262 }
22263
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022264 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022265 * ":function /pat": list functions matching pattern.
22266 */
22267 if (*eap->arg == '/')
22268 {
22269 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22270 if (!eap->skip)
22271 {
22272 regmatch_T regmatch;
22273
22274 c = *p;
22275 *p = NUL;
22276 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22277 *p = c;
22278 if (regmatch.regprog != NULL)
22279 {
22280 regmatch.rm_ic = p_ic;
22281
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022282 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022283 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22284 {
22285 if (!HASHITEM_EMPTY(hi))
22286 {
22287 --todo;
22288 fp = HI2UF(hi);
22289 if (!isdigit(*fp->uf_name)
22290 && vim_regexec(&regmatch, fp->uf_name, 0))
22291 list_func_head(fp, FALSE);
22292 }
22293 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022294 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022295 }
22296 }
22297 if (*p == '/')
22298 ++p;
22299 eap->nextcmd = check_nextcmd(p);
22300 return;
22301 }
22302
22303 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022304 * Get the function name. There are these situations:
22305 * func normal function name
22306 * "name" == func, "fudi.fd_dict" == NULL
22307 * dict.func new dictionary entry
22308 * "name" == NULL, "fudi.fd_dict" set,
22309 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22310 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022311 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022312 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22313 * dict.func existing dict entry that's not a Funcref
22314 * "name" == NULL, "fudi.fd_dict" set,
22315 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022316 * s:func script-local function name
22317 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022320 name = trans_function_name(&p, eap->skip, 0, &fudi);
22321 paren = (vim_strchr(p, '(') != NULL);
22322 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323 {
22324 /*
22325 * Return on an invalid expression in braces, unless the expression
22326 * evaluation has been cancelled due to an aborting error, an
22327 * interrupt, or an exception.
22328 */
22329 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022331 if (!eap->skip && fudi.fd_newkey != NULL)
22332 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022333 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336 else
22337 eap->skip = TRUE;
22338 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022339
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 /* An error in a function call during evaluation of an expression in magic
22341 * braces should not cause the function not to be defined. */
22342 saved_did_emsg = did_emsg;
22343 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344
22345 /*
22346 * ":function func" with only function name: list function.
22347 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022348 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 {
22350 if (!ends_excmd(*skipwhite(p)))
22351 {
22352 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022353 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 }
22355 eap->nextcmd = check_nextcmd(p);
22356 if (eap->nextcmd != NULL)
22357 *p = NUL;
22358 if (!eap->skip && !got_int)
22359 {
22360 fp = find_func(name);
22361 if (fp != NULL)
22362 {
22363 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022364 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022366 if (FUNCLINE(fp, j) == NULL)
22367 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 msg_putchar('\n');
22369 msg_outnum((long)(j + 1));
22370 if (j < 9)
22371 msg_putchar(' ');
22372 if (j < 99)
22373 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022374 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 out_flush(); /* show a line at a time */
22376 ui_breakcheck();
22377 }
22378 if (!got_int)
22379 {
22380 msg_putchar('\n');
22381 msg_puts((char_u *)" endfunction");
22382 }
22383 }
22384 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022385 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022387 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 }
22389
22390 /*
22391 * ":function name(arg1, arg2)" Define function.
22392 */
22393 p = skipwhite(p);
22394 if (*p != '(')
22395 {
22396 if (!eap->skip)
22397 {
22398 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022399 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 }
22401 /* attempt to continue by skipping some text */
22402 if (vim_strchr(p, '(') != NULL)
22403 p = vim_strchr(p, '(');
22404 }
22405 p = skipwhite(p + 1);
22406
22407 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22408 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22409
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022410 if (!eap->skip)
22411 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022412 /* Check the name of the function. Unless it's a dictionary function
22413 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022414 if (name != NULL)
22415 arg = name;
22416 else
22417 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022418 if (arg != NULL && (fudi.fd_di == NULL
22419 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022420 {
22421 if (*arg == K_SPECIAL)
22422 j = 3;
22423 else
22424 j = 0;
22425 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22426 : eval_isnamec(arg[j])))
22427 ++j;
22428 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022429 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022430 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022431 /* Disallow using the g: dict. */
22432 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22433 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022434 }
22435
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 /*
22437 * Isolate the arguments: "arg1, arg2, ...)"
22438 */
22439 while (*p != ')')
22440 {
22441 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22442 {
22443 varargs = TRUE;
22444 p += 3;
22445 mustend = TRUE;
22446 }
22447 else
22448 {
22449 arg = p;
22450 while (ASCII_ISALNUM(*p) || *p == '_')
22451 ++p;
22452 if (arg == p || isdigit(*arg)
22453 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22454 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22455 {
22456 if (!eap->skip)
22457 EMSG2(_("E125: Illegal argument: %s"), arg);
22458 break;
22459 }
22460 if (ga_grow(&newargs, 1) == FAIL)
22461 goto erret;
22462 c = *p;
22463 *p = NUL;
22464 arg = vim_strsave(arg);
22465 if (arg == NULL)
22466 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022467
22468 /* Check for duplicate argument name. */
22469 for (i = 0; i < newargs.ga_len; ++i)
22470 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22471 {
22472 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022473 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022474 goto erret;
22475 }
22476
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22478 *p = c;
22479 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480 if (*p == ',')
22481 ++p;
22482 else
22483 mustend = TRUE;
22484 }
22485 p = skipwhite(p);
22486 if (mustend && *p != ')')
22487 {
22488 if (!eap->skip)
22489 EMSG2(_(e_invarg2), eap->arg);
22490 break;
22491 }
22492 }
22493 ++p; /* skip the ')' */
22494
Bram Moolenaare9a41262005-01-15 22:18:47 +000022495 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 for (;;)
22497 {
22498 p = skipwhite(p);
22499 if (STRNCMP(p, "range", 5) == 0)
22500 {
22501 flags |= FC_RANGE;
22502 p += 5;
22503 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022504 else if (STRNCMP(p, "dict", 4) == 0)
22505 {
22506 flags |= FC_DICT;
22507 p += 4;
22508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 else if (STRNCMP(p, "abort", 5) == 0)
22510 {
22511 flags |= FC_ABORT;
22512 p += 5;
22513 }
22514 else
22515 break;
22516 }
22517
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022518 /* When there is a line break use what follows for the function body.
22519 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22520 if (*p == '\n')
22521 line_arg = p + 1;
22522 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523 EMSG(_(e_trailing));
22524
22525 /*
22526 * Read the body of the function, until ":endfunction" is found.
22527 */
22528 if (KeyTyped)
22529 {
22530 /* Check if the function already exists, don't let the user type the
22531 * whole function before telling him it doesn't work! For a script we
22532 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022533 if (!eap->skip && !eap->forceit)
22534 {
22535 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22536 EMSG(_(e_funcdict));
22537 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022538 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022541 if (!eap->skip && did_emsg)
22542 goto erret;
22543
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 msg_putchar('\n'); /* don't overwrite the function name */
22545 cmdline_row = msg_row;
22546 }
22547
22548 indent = 2;
22549 nesting = 0;
22550 for (;;)
22551 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022552 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022553 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022554 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022555 saved_wait_return = FALSE;
22556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022558 sourcing_lnum_off = sourcing_lnum;
22559
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022560 if (line_arg != NULL)
22561 {
22562 /* Use eap->arg, split up in parts by line breaks. */
22563 theline = line_arg;
22564 p = vim_strchr(theline, '\n');
22565 if (p == NULL)
22566 line_arg += STRLEN(line_arg);
22567 else
22568 {
22569 *p = NUL;
22570 line_arg = p + 1;
22571 }
22572 }
22573 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 theline = getcmdline(':', 0L, indent);
22575 else
22576 theline = eap->getline(':', eap->cookie, indent);
22577 if (KeyTyped)
22578 lines_left = Rows - 1;
22579 if (theline == NULL)
22580 {
22581 EMSG(_("E126: Missing :endfunction"));
22582 goto erret;
22583 }
22584
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022585 /* Detect line continuation: sourcing_lnum increased more than one. */
22586 if (sourcing_lnum > sourcing_lnum_off + 1)
22587 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22588 else
22589 sourcing_lnum_off = 0;
22590
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 if (skip_until != NULL)
22592 {
22593 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22594 * don't check for ":endfunc". */
22595 if (STRCMP(theline, skip_until) == 0)
22596 {
22597 vim_free(skip_until);
22598 skip_until = NULL;
22599 }
22600 }
22601 else
22602 {
22603 /* skip ':' and blanks*/
22604 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22605 ;
22606
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022607 /* Check for "endfunction". */
22608 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022610 if (line_arg == NULL)
22611 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 break;
22613 }
22614
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022615 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 * at "end". */
22617 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22618 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022619 else if (STRNCMP(p, "if", 2) == 0
22620 || STRNCMP(p, "wh", 2) == 0
22621 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 || STRNCMP(p, "try", 3) == 0)
22623 indent += 2;
22624
22625 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022626 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022628 if (*p == '!')
22629 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022631 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22632 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022634 ++nesting;
22635 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 }
22637 }
22638
22639 /* Check for ":append" or ":insert". */
22640 p = skip_range(p, NULL);
22641 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22642 || (p[0] == 'i'
22643 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22644 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22645 skip_until = vim_strsave((char_u *)".");
22646
22647 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22648 arg = skipwhite(skiptowhite(p));
22649 if (arg[0] == '<' && arg[1] =='<'
22650 && ((p[0] == 'p' && p[1] == 'y'
22651 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22652 || (p[0] == 'p' && p[1] == 'e'
22653 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22654 || (p[0] == 't' && p[1] == 'c'
22655 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022656 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22657 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22659 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022660 || (p[0] == 'm' && p[1] == 'z'
22661 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 ))
22663 {
22664 /* ":python <<" continues until a dot, like ":append" */
22665 p = skipwhite(arg + 2);
22666 if (*p == NUL)
22667 skip_until = vim_strsave((char_u *)".");
22668 else
22669 skip_until = vim_strsave(p);
22670 }
22671 }
22672
22673 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022674 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022675 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022676 if (line_arg == NULL)
22677 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022679 }
22680
22681 /* Copy the line to newly allocated memory. get_one_sourceline()
22682 * allocates 250 bytes per line, this saves 80% on average. The cost
22683 * is an extra alloc/free. */
22684 p = vim_strsave(theline);
22685 if (p != NULL)
22686 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022687 if (line_arg == NULL)
22688 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022689 theline = p;
22690 }
22691
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022692 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22693
22694 /* Add NULL lines for continuation lines, so that the line count is
22695 * equal to the index in the growarray. */
22696 while (sourcing_lnum_off-- > 0)
22697 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022698
22699 /* Check for end of eap->arg. */
22700 if (line_arg != NULL && *line_arg == NUL)
22701 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 }
22703
22704 /* Don't define the function when skipping commands or when an error was
22705 * detected. */
22706 if (eap->skip || did_emsg)
22707 goto erret;
22708
22709 /*
22710 * If there are no errors, add the function
22711 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022712 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022713 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022714 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022715 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022716 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022717 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022718 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022719 goto erret;
22720 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022721
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022722 fp = find_func(name);
22723 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022725 if (!eap->forceit)
22726 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022727 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022728 goto erret;
22729 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022730 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022731 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022732 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022733 name);
22734 goto erret;
22735 }
22736 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022737 ga_clear_strings(&(fp->uf_args));
22738 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022739 vim_free(name);
22740 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 }
22743 else
22744 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022745 char numbuf[20];
22746
22747 fp = NULL;
22748 if (fudi.fd_newkey == NULL && !eap->forceit)
22749 {
22750 EMSG(_(e_funcdict));
22751 goto erret;
22752 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022753 if (fudi.fd_di == NULL)
22754 {
22755 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022756 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022757 goto erret;
22758 }
22759 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022760 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022761 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022762
22763 /* Give the function a sequential number. Can only be used with a
22764 * Funcref! */
22765 vim_free(name);
22766 sprintf(numbuf, "%d", ++func_nr);
22767 name = vim_strsave((char_u *)numbuf);
22768 if (name == NULL)
22769 goto erret;
22770 }
22771
22772 if (fp == NULL)
22773 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022774 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022775 {
22776 int slen, plen;
22777 char_u *scriptname;
22778
22779 /* Check that the autoload name matches the script name. */
22780 j = FAIL;
22781 if (sourcing_name != NULL)
22782 {
22783 scriptname = autoload_name(name);
22784 if (scriptname != NULL)
22785 {
22786 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022787 plen = (int)STRLEN(p);
22788 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022789 if (slen > plen && fnamecmp(p,
22790 sourcing_name + slen - plen) == 0)
22791 j = OK;
22792 vim_free(scriptname);
22793 }
22794 }
22795 if (j == FAIL)
22796 {
22797 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22798 goto erret;
22799 }
22800 }
22801
22802 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 if (fp == NULL)
22804 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022805
22806 if (fudi.fd_dict != NULL)
22807 {
22808 if (fudi.fd_di == NULL)
22809 {
22810 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022811 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022812 if (fudi.fd_di == NULL)
22813 {
22814 vim_free(fp);
22815 goto erret;
22816 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022817 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22818 {
22819 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022820 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022821 goto erret;
22822 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022823 }
22824 else
22825 /* overwrite existing dict entry */
22826 clear_tv(&fudi.fd_di->di_tv);
22827 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022828 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022829 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022830 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022831
22832 /* behave like "dict" was used */
22833 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022834 }
22835
Bram Moolenaar071d4272004-06-13 20:20:40 +000022836 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022837 STRCPY(fp->uf_name, name);
22838 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022840 fp->uf_args = newargs;
22841 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022842#ifdef FEAT_PROFILE
22843 fp->uf_tml_count = NULL;
22844 fp->uf_tml_total = NULL;
22845 fp->uf_tml_self = NULL;
22846 fp->uf_profiling = FALSE;
22847 if (prof_def_func())
22848 func_do_profile(fp);
22849#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022850 fp->uf_varargs = varargs;
22851 fp->uf_flags = flags;
22852 fp->uf_calls = 0;
22853 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022854 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855
22856erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 ga_clear_strings(&newargs);
22858 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022859ret_free:
22860 vim_free(skip_until);
22861 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022864 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865}
22866
22867/*
22868 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022869 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022871 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022872 * TFN_INT: internal function name OK
22873 * TFN_QUIET: be quiet
22874 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 * Advances "pp" to just after the function name (if no error).
22876 */
22877 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022878trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 char_u **pp;
22880 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022881 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022882 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022884 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 char_u *start;
22886 char_u *end;
22887 int lead;
22888 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022890 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022891
22892 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022893 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022895
22896 /* Check for hard coded <SNR>: already translated function ID (from a user
22897 * command). */
22898 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22899 && (*pp)[2] == (int)KE_SNR)
22900 {
22901 *pp += 3;
22902 len = get_id_len(pp) + 3;
22903 return vim_strnsave(start, len);
22904 }
22905
22906 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22907 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022909 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022911
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022912 /* Note that TFN_ flags use the same values as GLV_ flags. */
22913 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022914 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022915 if (end == start)
22916 {
22917 if (!skip)
22918 EMSG(_("E129: Function name required"));
22919 goto theend;
22920 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022921 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022922 {
22923 /*
22924 * Report an invalid expression in braces, unless the expression
22925 * evaluation has been cancelled due to an aborting error, an
22926 * interrupt, or an exception.
22927 */
22928 if (!aborting())
22929 {
22930 if (end != NULL)
22931 EMSG2(_(e_invarg2), start);
22932 }
22933 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022934 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022935 goto theend;
22936 }
22937
22938 if (lv.ll_tv != NULL)
22939 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022940 if (fdp != NULL)
22941 {
22942 fdp->fd_dict = lv.ll_dict;
22943 fdp->fd_newkey = lv.ll_newkey;
22944 lv.ll_newkey = NULL;
22945 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022946 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022947 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22948 {
22949 name = vim_strsave(lv.ll_tv->vval.v_string);
22950 *pp = end;
22951 }
22952 else
22953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022954 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22955 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022956 EMSG(_(e_funcref));
22957 else
22958 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022959 name = NULL;
22960 }
22961 goto theend;
22962 }
22963
22964 if (lv.ll_name == NULL)
22965 {
22966 /* Error found, but continue after the function name. */
22967 *pp = end;
22968 goto theend;
22969 }
22970
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022971 /* Check if the name is a Funcref. If so, use the value. */
22972 if (lv.ll_exp_name != NULL)
22973 {
22974 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022975 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022976 if (name == lv.ll_exp_name)
22977 name = NULL;
22978 }
22979 else
22980 {
22981 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022982 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022983 if (name == *pp)
22984 name = NULL;
22985 }
22986 if (name != NULL)
22987 {
22988 name = vim_strsave(name);
22989 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022990 if (STRNCMP(name, "<SNR>", 5) == 0)
22991 {
22992 /* Change "<SNR>" to the byte sequence. */
22993 name[0] = K_SPECIAL;
22994 name[1] = KS_EXTRA;
22995 name[2] = (int)KE_SNR;
22996 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22997 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022998 goto theend;
22999 }
23000
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023001 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023002 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023003 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023004 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23005 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23006 {
23007 /* When there was "s:" already or the name expanded to get a
23008 * leading "s:" then remove it. */
23009 lv.ll_name += 2;
23010 len -= 2;
23011 lead = 2;
23012 }
23013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023014 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023015 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023016 /* skip over "s:" and "g:" */
23017 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023018 lv.ll_name += 2;
23019 len = (int)(end - lv.ll_name);
23020 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023021
23022 /*
23023 * Copy the function name to allocated memory.
23024 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23025 * Accept <SNR>123_name() outside a script.
23026 */
23027 if (skip)
23028 lead = 0; /* do nothing */
23029 else if (lead > 0)
23030 {
23031 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023032 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23033 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023034 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023035 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023036 if (current_SID <= 0)
23037 {
23038 EMSG(_(e_usingsid));
23039 goto theend;
23040 }
23041 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23042 lead += (int)STRLEN(sid_buf);
23043 }
23044 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023045 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023046 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023047 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023048 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023049 goto theend;
23050 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023051 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023052 {
23053 char_u *cp = vim_strchr(lv.ll_name, ':');
23054
23055 if (cp != NULL && cp < end)
23056 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023057 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023058 goto theend;
23059 }
23060 }
23061
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023062 name = alloc((unsigned)(len + lead + 1));
23063 if (name != NULL)
23064 {
23065 if (lead > 0)
23066 {
23067 name[0] = K_SPECIAL;
23068 name[1] = KS_EXTRA;
23069 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023070 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023071 STRCPY(name + 3, sid_buf);
23072 }
23073 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023074 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023075 }
23076 *pp = end;
23077
23078theend:
23079 clear_lval(&lv);
23080 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081}
23082
23083/*
23084 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23085 * Return 2 if "p" starts with "s:".
23086 * Return 0 otherwise.
23087 */
23088 static int
23089eval_fname_script(p)
23090 char_u *p;
23091{
23092 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23093 || STRNICMP(p + 1, "SNR>", 4) == 0))
23094 return 5;
23095 if (p[0] == 's' && p[1] == ':')
23096 return 2;
23097 return 0;
23098}
23099
23100/*
23101 * Return TRUE if "p" starts with "<SID>" or "s:".
23102 * Only works if eval_fname_script() returned non-zero for "p"!
23103 */
23104 static int
23105eval_fname_sid(p)
23106 char_u *p;
23107{
23108 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23109}
23110
23111/*
23112 * List the head of the function: "name(arg1, arg2)".
23113 */
23114 static void
23115list_func_head(fp, indent)
23116 ufunc_T *fp;
23117 int indent;
23118{
23119 int j;
23120
23121 msg_start();
23122 if (indent)
23123 MSG_PUTS(" ");
23124 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023125 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 {
23127 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023128 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129 }
23130 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023131 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023133 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 {
23135 if (j)
23136 MSG_PUTS(", ");
23137 msg_puts(FUNCARG(fp, j));
23138 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023139 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 {
23141 if (j)
23142 MSG_PUTS(", ");
23143 MSG_PUTS("...");
23144 }
23145 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023146 if (fp->uf_flags & FC_ABORT)
23147 MSG_PUTS(" abort");
23148 if (fp->uf_flags & FC_RANGE)
23149 MSG_PUTS(" range");
23150 if (fp->uf_flags & FC_DICT)
23151 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023152 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023153 if (p_verbose > 0)
23154 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155}
23156
23157/*
23158 * Find a function by name, return pointer to it in ufuncs.
23159 * Return NULL for unknown function.
23160 */
23161 static ufunc_T *
23162find_func(name)
23163 char_u *name;
23164{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023165 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023167 hi = hash_find(&func_hashtab, name);
23168 if (!HASHITEM_EMPTY(hi))
23169 return HI2UF(hi);
23170 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171}
23172
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023173#if defined(EXITFREE) || defined(PROTO)
23174 void
23175free_all_functions()
23176{
23177 hashitem_T *hi;
23178
23179 /* Need to start all over every time, because func_free() may change the
23180 * hash table. */
23181 while (func_hashtab.ht_used > 0)
23182 for (hi = func_hashtab.ht_array; ; ++hi)
23183 if (!HASHITEM_EMPTY(hi))
23184 {
23185 func_free(HI2UF(hi));
23186 break;
23187 }
23188}
23189#endif
23190
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023191 int
23192translated_function_exists(name)
23193 char_u *name;
23194{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023195 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023196 return find_internal_func(name) >= 0;
23197 return find_func(name) != NULL;
23198}
23199
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023200/*
23201 * Return TRUE if a function "name" exists.
23202 */
23203 static int
23204function_exists(name)
23205 char_u *name;
23206{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023207 char_u *nm = name;
23208 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023209 int n = FALSE;
23210
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023211 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23212 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023213 nm = skipwhite(nm);
23214
23215 /* Only accept "funcname", "funcname ", "funcname (..." and
23216 * "funcname(...", not "funcname!...". */
23217 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023218 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023219 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023220 return n;
23221}
23222
Bram Moolenaara1544c02013-05-30 12:35:52 +020023223 char_u *
23224get_expanded_name(name, check)
23225 char_u *name;
23226 int check;
23227{
23228 char_u *nm = name;
23229 char_u *p;
23230
23231 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23232
23233 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023234 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023235 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023236
Bram Moolenaara1544c02013-05-30 12:35:52 +020023237 vim_free(p);
23238 return NULL;
23239}
23240
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023241/*
23242 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023243 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23244 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023245 */
23246 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023247builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023248 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023249 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023250{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023251 char_u *p;
23252
23253 if (!ASCII_ISLOWER(name[0]))
23254 return FALSE;
23255 p = vim_strchr(name, AUTOLOAD_CHAR);
23256 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023257}
23258
Bram Moolenaar05159a02005-02-26 23:04:13 +000023259#if defined(FEAT_PROFILE) || defined(PROTO)
23260/*
23261 * Start profiling function "fp".
23262 */
23263 static void
23264func_do_profile(fp)
23265 ufunc_T *fp;
23266{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023267 int len = fp->uf_lines.ga_len;
23268
23269 if (len == 0)
23270 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023271 fp->uf_tm_count = 0;
23272 profile_zero(&fp->uf_tm_self);
23273 profile_zero(&fp->uf_tm_total);
23274 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023275 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023276 if (fp->uf_tml_total == NULL)
23277 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023278 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023279 if (fp->uf_tml_self == NULL)
23280 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023281 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023282 fp->uf_tml_idx = -1;
23283 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23284 || fp->uf_tml_self == NULL)
23285 return; /* out of memory */
23286
23287 fp->uf_profiling = TRUE;
23288}
23289
23290/*
23291 * Dump the profiling results for all functions in file "fd".
23292 */
23293 void
23294func_dump_profile(fd)
23295 FILE *fd;
23296{
23297 hashitem_T *hi;
23298 int todo;
23299 ufunc_T *fp;
23300 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023301 ufunc_T **sorttab;
23302 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023303
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023304 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023305 if (todo == 0)
23306 return; /* nothing to dump */
23307
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023308 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023309
Bram Moolenaar05159a02005-02-26 23:04:13 +000023310 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23311 {
23312 if (!HASHITEM_EMPTY(hi))
23313 {
23314 --todo;
23315 fp = HI2UF(hi);
23316 if (fp->uf_profiling)
23317 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023318 if (sorttab != NULL)
23319 sorttab[st_len++] = fp;
23320
Bram Moolenaar05159a02005-02-26 23:04:13 +000023321 if (fp->uf_name[0] == K_SPECIAL)
23322 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23323 else
23324 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23325 if (fp->uf_tm_count == 1)
23326 fprintf(fd, "Called 1 time\n");
23327 else
23328 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23329 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23330 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23331 fprintf(fd, "\n");
23332 fprintf(fd, "count total (s) self (s)\n");
23333
23334 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23335 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023336 if (FUNCLINE(fp, i) == NULL)
23337 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023338 prof_func_line(fd, fp->uf_tml_count[i],
23339 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023340 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23341 }
23342 fprintf(fd, "\n");
23343 }
23344 }
23345 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023346
23347 if (sorttab != NULL && st_len > 0)
23348 {
23349 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23350 prof_total_cmp);
23351 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23352 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23353 prof_self_cmp);
23354 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23355 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023356
23357 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023358}
Bram Moolenaar73830342005-02-28 22:48:19 +000023359
23360 static void
23361prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23362 FILE *fd;
23363 ufunc_T **sorttab;
23364 int st_len;
23365 char *title;
23366 int prefer_self; /* when equal print only self time */
23367{
23368 int i;
23369 ufunc_T *fp;
23370
23371 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23372 fprintf(fd, "count total (s) self (s) function\n");
23373 for (i = 0; i < 20 && i < st_len; ++i)
23374 {
23375 fp = sorttab[i];
23376 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23377 prefer_self);
23378 if (fp->uf_name[0] == K_SPECIAL)
23379 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23380 else
23381 fprintf(fd, " %s()\n", fp->uf_name);
23382 }
23383 fprintf(fd, "\n");
23384}
23385
23386/*
23387 * Print the count and times for one function or function line.
23388 */
23389 static void
23390prof_func_line(fd, count, total, self, prefer_self)
23391 FILE *fd;
23392 int count;
23393 proftime_T *total;
23394 proftime_T *self;
23395 int prefer_self; /* when equal print only self time */
23396{
23397 if (count > 0)
23398 {
23399 fprintf(fd, "%5d ", count);
23400 if (prefer_self && profile_equal(total, self))
23401 fprintf(fd, " ");
23402 else
23403 fprintf(fd, "%s ", profile_msg(total));
23404 if (!prefer_self && profile_equal(total, self))
23405 fprintf(fd, " ");
23406 else
23407 fprintf(fd, "%s ", profile_msg(self));
23408 }
23409 else
23410 fprintf(fd, " ");
23411}
23412
23413/*
23414 * Compare function for total time sorting.
23415 */
23416 static int
23417#ifdef __BORLANDC__
23418_RTLENTRYF
23419#endif
23420prof_total_cmp(s1, s2)
23421 const void *s1;
23422 const void *s2;
23423{
23424 ufunc_T *p1, *p2;
23425
23426 p1 = *(ufunc_T **)s1;
23427 p2 = *(ufunc_T **)s2;
23428 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23429}
23430
23431/*
23432 * Compare function for self time sorting.
23433 */
23434 static int
23435#ifdef __BORLANDC__
23436_RTLENTRYF
23437#endif
23438prof_self_cmp(s1, s2)
23439 const void *s1;
23440 const void *s2;
23441{
23442 ufunc_T *p1, *p2;
23443
23444 p1 = *(ufunc_T **)s1;
23445 p2 = *(ufunc_T **)s2;
23446 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23447}
23448
Bram Moolenaar05159a02005-02-26 23:04:13 +000023449#endif
23450
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023451/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023452 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023453 * Return TRUE if a package was loaded.
23454 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023455 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023456script_autoload(name, reload)
23457 char_u *name;
23458 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023459{
23460 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023461 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023462 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023463 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023464
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023465 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023466 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023467 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023468 return FALSE;
23469
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023470 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023471
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023472 /* Find the name in the list of previously loaded package names. Skip
23473 * "autoload/", it's always the same. */
23474 for (i = 0; i < ga_loaded.ga_len; ++i)
23475 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23476 break;
23477 if (!reload && i < ga_loaded.ga_len)
23478 ret = FALSE; /* was loaded already */
23479 else
23480 {
23481 /* Remember the name if it wasn't loaded already. */
23482 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23483 {
23484 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23485 tofree = NULL;
23486 }
23487
23488 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023489 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023490 ret = TRUE;
23491 }
23492
23493 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023494 return ret;
23495}
23496
23497/*
23498 * Return the autoload script name for a function or variable name.
23499 * Returns NULL when out of memory.
23500 */
23501 static char_u *
23502autoload_name(name)
23503 char_u *name;
23504{
23505 char_u *p;
23506 char_u *scriptname;
23507
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023508 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023509 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23510 if (scriptname == NULL)
23511 return FALSE;
23512 STRCPY(scriptname, "autoload/");
23513 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023514 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023515 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023516 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023517 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023518 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023519}
23520
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23522
23523/*
23524 * Function given to ExpandGeneric() to obtain the list of user defined
23525 * function names.
23526 */
23527 char_u *
23528get_user_func_name(xp, idx)
23529 expand_T *xp;
23530 int idx;
23531{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023532 static long_u done;
23533 static hashitem_T *hi;
23534 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535
23536 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023538 done = 0;
23539 hi = func_hashtab.ht_array;
23540 }
23541 if (done < func_hashtab.ht_used)
23542 {
23543 if (done++ > 0)
23544 ++hi;
23545 while (HASHITEM_EMPTY(hi))
23546 ++hi;
23547 fp = HI2UF(hi);
23548
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023549 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023550 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023551
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023552 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23553 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554
23555 cat_func_name(IObuff, fp);
23556 if (xp->xp_context != EXPAND_USER_FUNC)
23557 {
23558 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023559 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023560 STRCAT(IObuff, ")");
23561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 return IObuff;
23563 }
23564 return NULL;
23565}
23566
23567#endif /* FEAT_CMDL_COMPL */
23568
23569/*
23570 * Copy the function name of "fp" to buffer "buf".
23571 * "buf" must be able to hold the function name plus three bytes.
23572 * Takes care of script-local function names.
23573 */
23574 static void
23575cat_func_name(buf, fp)
23576 char_u *buf;
23577 ufunc_T *fp;
23578{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023579 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580 {
23581 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023582 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 }
23584 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023585 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586}
23587
23588/*
23589 * ":delfunction {name}"
23590 */
23591 void
23592ex_delfunction(eap)
23593 exarg_T *eap;
23594{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023595 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596 char_u *p;
23597 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023598 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599
23600 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023601 name = trans_function_name(&p, eap->skip, 0, &fudi);
23602 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023604 {
23605 if (fudi.fd_dict != NULL && !eap->skip)
23606 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023609 if (!ends_excmd(*skipwhite(p)))
23610 {
23611 vim_free(name);
23612 EMSG(_(e_trailing));
23613 return;
23614 }
23615 eap->nextcmd = check_nextcmd(p);
23616 if (eap->nextcmd != NULL)
23617 *p = NUL;
23618
23619 if (!eap->skip)
23620 fp = find_func(name);
23621 vim_free(name);
23622
23623 if (!eap->skip)
23624 {
23625 if (fp == NULL)
23626 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023627 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 return;
23629 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023630 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631 {
23632 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23633 return;
23634 }
23635
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023636 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023638 /* Delete the dict item that refers to the function, it will
23639 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023640 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023642 else
23643 func_free(fp);
23644 }
23645}
23646
23647/*
23648 * Free a function and remove it from the list of functions.
23649 */
23650 static void
23651func_free(fp)
23652 ufunc_T *fp;
23653{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023654 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023655
23656 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023657 ga_clear_strings(&(fp->uf_args));
23658 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023659#ifdef FEAT_PROFILE
23660 vim_free(fp->uf_tml_count);
23661 vim_free(fp->uf_tml_total);
23662 vim_free(fp->uf_tml_self);
23663#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023664
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023665 /* remove the function from the function hashtable */
23666 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23667 if (HASHITEM_EMPTY(hi))
23668 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023669 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023670 hash_remove(&func_hashtab, hi);
23671
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023672 vim_free(fp);
23673}
23674
23675/*
23676 * Unreference a Function: decrement the reference count and free it when it
23677 * becomes zero. Only for numbered functions.
23678 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023679 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023680func_unref(name)
23681 char_u *name;
23682{
23683 ufunc_T *fp;
23684
23685 if (name != NULL && isdigit(*name))
23686 {
23687 fp = find_func(name);
23688 if (fp == NULL)
23689 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023690 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023691 {
23692 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023693 * when "uf_calls" becomes zero. */
23694 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023695 func_free(fp);
23696 }
23697 }
23698}
23699
23700/*
23701 * Count a reference to a Function.
23702 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023703 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023704func_ref(name)
23705 char_u *name;
23706{
23707 ufunc_T *fp;
23708
23709 if (name != NULL && isdigit(*name))
23710 {
23711 fp = find_func(name);
23712 if (fp == NULL)
23713 EMSG2(_(e_intern2), "func_ref()");
23714 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023715 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 }
23717}
23718
23719/*
23720 * Call a user function.
23721 */
23722 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023723call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023724 ufunc_T *fp; /* pointer to function */
23725 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023726 typval_T *argvars; /* arguments */
23727 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 linenr_T firstline; /* first line of range */
23729 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023730 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023731{
Bram Moolenaar33570922005-01-25 22:26:29 +000023732 char_u *save_sourcing_name;
23733 linenr_T save_sourcing_lnum;
23734 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023735 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023736 int save_did_emsg;
23737 static int depth = 0;
23738 dictitem_T *v;
23739 int fixvar_idx = 0; /* index in fixvar[] */
23740 int i;
23741 int ai;
23742 char_u numbuf[NUMBUFLEN];
23743 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023744#ifdef FEAT_PROFILE
23745 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023746 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748
23749 /* If depth of calling is getting too high, don't execute the function */
23750 if (depth >= p_mfd)
23751 {
23752 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023753 rettv->v_type = VAR_NUMBER;
23754 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 return;
23756 }
23757 ++depth;
23758
23759 line_breakcheck(); /* check for CTRL-C hit */
23760
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023761 fc = (funccall_T *)alloc(sizeof(funccall_T));
23762 fc->caller = current_funccal;
23763 current_funccal = fc;
23764 fc->func = fp;
23765 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023766 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023767 fc->linenr = 0;
23768 fc->returned = FALSE;
23769 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023771 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23772 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773
Bram Moolenaar33570922005-01-25 22:26:29 +000023774 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023775 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023776 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23777 * each argument variable and saves a lot of time.
23778 */
23779 /*
23780 * Init l: variables.
23781 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023782 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023783 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023784 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023785 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23786 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023787 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023788 name = v->di_key;
23789 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023790 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023791 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023792 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023793 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023794 v->di_tv.vval.v_dict = selfdict;
23795 ++selfdict->dv_refcount;
23796 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023797
Bram Moolenaar33570922005-01-25 22:26:29 +000023798 /*
23799 * Init a: variables.
23800 * Set a:0 to "argcount".
23801 * Set a:000 to a list with room for the "..." arguments.
23802 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023803 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023804 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023805 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023806 /* Use "name" to avoid a warning from some compiler that checks the
23807 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023808 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023809 name = v->di_key;
23810 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023811 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023812 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023813 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023814 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023815 v->di_tv.vval.v_list = &fc->l_varlist;
23816 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23817 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23818 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023819
23820 /*
23821 * Set a:firstline to "firstline" and a:lastline to "lastline".
23822 * Set a:name to named arguments.
23823 * Set a:N to the "..." arguments.
23824 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023825 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023826 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023827 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023828 (varnumber_T)lastline);
23829 for (i = 0; i < argcount; ++i)
23830 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023831 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023832 if (ai < 0)
23833 /* named argument a:name */
23834 name = FUNCARG(fp, i);
23835 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023836 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023837 /* "..." argument a:1, a:2, etc. */
23838 sprintf((char *)numbuf, "%d", ai + 1);
23839 name = numbuf;
23840 }
23841 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23842 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023843 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023844 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23845 }
23846 else
23847 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023848 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23849 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023850 if (v == NULL)
23851 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023852 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023853 }
23854 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023855 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023856
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023857 /* Note: the values are copied directly to avoid alloc/free.
23858 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023859 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023860 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023861
23862 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23863 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023864 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23865 fc->l_listitems[ai].li_tv = argvars[i];
23866 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023867 }
23868 }
23869
Bram Moolenaar071d4272004-06-13 20:20:40 +000023870 /* Don't redraw while executing the function. */
23871 ++RedrawingDisabled;
23872 save_sourcing_name = sourcing_name;
23873 save_sourcing_lnum = sourcing_lnum;
23874 sourcing_lnum = 1;
23875 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023876 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877 if (sourcing_name != NULL)
23878 {
23879 if (save_sourcing_name != NULL
23880 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23881 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23882 else
23883 STRCPY(sourcing_name, "function ");
23884 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23885
23886 if (p_verbose >= 12)
23887 {
23888 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023889 verbose_enter_scroll();
23890
Bram Moolenaar555b2802005-05-19 21:08:39 +000023891 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 if (p_verbose >= 14)
23893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023895 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023896 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023897 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898
23899 msg_puts((char_u *)"(");
23900 for (i = 0; i < argcount; ++i)
23901 {
23902 if (i > 0)
23903 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023904 if (argvars[i].v_type == VAR_NUMBER)
23905 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 else
23907 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023908 /* Do not want errors such as E724 here. */
23909 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023910 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023911 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023912 if (s != NULL)
23913 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023914 if (vim_strsize(s) > MSG_BUF_CLEN)
23915 {
23916 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23917 s = buf;
23918 }
23919 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023920 vim_free(tofree);
23921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 }
23923 }
23924 msg_puts((char_u *)")");
23925 }
23926 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023927
23928 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929 --no_wait_return;
23930 }
23931 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023932#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023933 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023934 {
23935 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23936 func_do_profile(fp);
23937 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023938 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023939 {
23940 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023941 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023942 profile_zero(&fp->uf_tm_children);
23943 }
23944 script_prof_save(&wait_start);
23945 }
23946#endif
23947
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023949 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023950 save_did_emsg = did_emsg;
23951 did_emsg = FALSE;
23952
23953 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023954 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23956
23957 --RedrawingDisabled;
23958
23959 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023960 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023962 clear_tv(rettv);
23963 rettv->v_type = VAR_NUMBER;
23964 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023965 }
23966
Bram Moolenaar05159a02005-02-26 23:04:13 +000023967#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023968 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023969 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023970 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023971 profile_end(&call_start);
23972 profile_sub_wait(&wait_start, &call_start);
23973 profile_add(&fp->uf_tm_total, &call_start);
23974 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023975 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023976 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023977 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23978 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023979 }
23980 }
23981#endif
23982
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983 /* when being verbose, mention the return value */
23984 if (p_verbose >= 12)
23985 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023987 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988
Bram Moolenaar071d4272004-06-13 20:20:40 +000023989 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023990 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023991 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023992 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023993 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023996 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023997 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023998 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023999 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024000
Bram Moolenaar555b2802005-05-19 21:08:39 +000024001 /* The value may be very long. Skip the middle part, so that we
24002 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024003 * truncate it at the end. Don't want errors such as E724 here. */
24004 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024005 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024006 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024007 if (s != NULL)
24008 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024009 if (vim_strsize(s) > MSG_BUF_CLEN)
24010 {
24011 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24012 s = buf;
24013 }
24014 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024015 vim_free(tofree);
24016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024017 }
24018 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024019
24020 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024021 --no_wait_return;
24022 }
24023
24024 vim_free(sourcing_name);
24025 sourcing_name = save_sourcing_name;
24026 sourcing_lnum = save_sourcing_lnum;
24027 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024028#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024029 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024030 script_prof_restore(&wait_start);
24031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032
24033 if (p_verbose >= 12 && sourcing_name != NULL)
24034 {
24035 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024036 verbose_enter_scroll();
24037
Bram Moolenaar555b2802005-05-19 21:08:39 +000024038 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024040
24041 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024042 --no_wait_return;
24043 }
24044
24045 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024046 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024048
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024049 /* If the a:000 list and the l: and a: dicts are not referenced we can
24050 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024051 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24052 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24053 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24054 {
24055 free_funccal(fc, FALSE);
24056 }
24057 else
24058 {
24059 hashitem_T *hi;
24060 listitem_T *li;
24061 int todo;
24062
24063 /* "fc" is still in use. This can happen when returning "a:000" or
24064 * assigning "l:" to a global variable.
24065 * Link "fc" in the list for garbage collection later. */
24066 fc->caller = previous_funccal;
24067 previous_funccal = fc;
24068
24069 /* Make a copy of the a: variables, since we didn't do that above. */
24070 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24071 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24072 {
24073 if (!HASHITEM_EMPTY(hi))
24074 {
24075 --todo;
24076 v = HI2DI(hi);
24077 copy_tv(&v->di_tv, &v->di_tv);
24078 }
24079 }
24080
24081 /* Make a copy of the a:000 items, since we didn't do that above. */
24082 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24083 copy_tv(&li->li_tv, &li->li_tv);
24084 }
24085}
24086
24087/*
24088 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024089 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024090 */
24091 static int
24092can_free_funccal(fc, copyID)
24093 funccall_T *fc;
24094 int copyID;
24095{
24096 return (fc->l_varlist.lv_copyID != copyID
24097 && fc->l_vars.dv_copyID != copyID
24098 && fc->l_avars.dv_copyID != copyID);
24099}
24100
24101/*
24102 * Free "fc" and what it contains.
24103 */
24104 static void
24105free_funccal(fc, free_val)
24106 funccall_T *fc;
24107 int free_val; /* a: vars were allocated */
24108{
24109 listitem_T *li;
24110
24111 /* The a: variables typevals may not have been allocated, only free the
24112 * allocated variables. */
24113 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24114
24115 /* free all l: variables */
24116 vars_clear(&fc->l_vars.dv_hashtab);
24117
24118 /* Free the a:000 variables if they were allocated. */
24119 if (free_val)
24120 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24121 clear_tv(&li->li_tv);
24122
24123 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124}
24125
24126/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024127 * Add a number variable "name" to dict "dp" with value "nr".
24128 */
24129 static void
24130add_nr_var(dp, v, name, nr)
24131 dict_T *dp;
24132 dictitem_T *v;
24133 char *name;
24134 varnumber_T nr;
24135{
24136 STRCPY(v->di_key, name);
24137 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24138 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24139 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024140 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024141 v->di_tv.vval.v_number = nr;
24142}
24143
24144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145 * ":return [expr]"
24146 */
24147 void
24148ex_return(eap)
24149 exarg_T *eap;
24150{
24151 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024152 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 int returning = FALSE;
24154
24155 if (current_funccal == NULL)
24156 {
24157 EMSG(_("E133: :return not inside a function"));
24158 return;
24159 }
24160
24161 if (eap->skip)
24162 ++emsg_skip;
24163
24164 eap->nextcmd = NULL;
24165 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024166 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 {
24168 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024169 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024171 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024172 }
24173 /* It's safer to return also on error. */
24174 else if (!eap->skip)
24175 {
24176 /*
24177 * Return unless the expression evaluation has been cancelled due to an
24178 * aborting error, an interrupt, or an exception.
24179 */
24180 if (!aborting())
24181 returning = do_return(eap, FALSE, TRUE, NULL);
24182 }
24183
24184 /* When skipping or the return gets pending, advance to the next command
24185 * in this line (!returning). Otherwise, ignore the rest of the line.
24186 * Following lines will be ignored by get_func_line(). */
24187 if (returning)
24188 eap->nextcmd = NULL;
24189 else if (eap->nextcmd == NULL) /* no argument */
24190 eap->nextcmd = check_nextcmd(arg);
24191
24192 if (eap->skip)
24193 --emsg_skip;
24194}
24195
24196/*
24197 * Return from a function. Possibly makes the return pending. Also called
24198 * for a pending return at the ":endtry" or after returning from an extra
24199 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024200 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024201 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024202 * FALSE when the return gets pending.
24203 */
24204 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024205do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206 exarg_T *eap;
24207 int reanimate;
24208 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024209 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024210{
24211 int idx;
24212 struct condstack *cstack = eap->cstack;
24213
24214 if (reanimate)
24215 /* Undo the return. */
24216 current_funccal->returned = FALSE;
24217
24218 /*
24219 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24220 * not in its finally clause (which then is to be executed next) is found.
24221 * In this case, make the ":return" pending for execution at the ":endtry".
24222 * Otherwise, return normally.
24223 */
24224 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24225 if (idx >= 0)
24226 {
24227 cstack->cs_pending[idx] = CSTP_RETURN;
24228
24229 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024230 /* A pending return again gets pending. "rettv" points to an
24231 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024233 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 else
24235 {
24236 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024237 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024239 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024241 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 {
24243 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024244 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024245 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246 else
24247 EMSG(_(e_outofmem));
24248 }
24249 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024250 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251
24252 if (reanimate)
24253 {
24254 /* The pending return value could be overwritten by a ":return"
24255 * without argument in a finally clause; reset the default
24256 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024257 current_funccal->rettv->v_type = VAR_NUMBER;
24258 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259 }
24260 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024261 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 }
24263 else
24264 {
24265 current_funccal->returned = TRUE;
24266
24267 /* If the return is carried out now, store the return value. For
24268 * a return immediately after reanimation, the value is already
24269 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024270 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024272 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024273 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024275 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024276 }
24277 }
24278
24279 return idx < 0;
24280}
24281
24282/*
24283 * Free the variable with a pending return value.
24284 */
24285 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024286discard_pending_return(rettv)
24287 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288{
Bram Moolenaar33570922005-01-25 22:26:29 +000024289 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024290}
24291
24292/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024293 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294 * is an allocated string. Used by report_pending() for verbose messages.
24295 */
24296 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024297get_return_cmd(rettv)
24298 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024300 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024301 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024302 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024304 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024305 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024306 if (s == NULL)
24307 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024308
24309 STRCPY(IObuff, ":return ");
24310 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24311 if (STRLEN(s) + 8 >= IOSIZE)
24312 STRCPY(IObuff + IOSIZE - 4, "...");
24313 vim_free(tofree);
24314 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315}
24316
24317/*
24318 * Get next function line.
24319 * Called by do_cmdline() to get the next line.
24320 * Returns allocated string, or NULL for end of function.
24321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322 char_u *
24323get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024324 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024326 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024327{
Bram Moolenaar33570922005-01-25 22:26:29 +000024328 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024329 ufunc_T *fp = fcp->func;
24330 char_u *retval;
24331 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332
24333 /* If breakpoints have been added/deleted need to check for it. */
24334 if (fcp->dbg_tick != debug_tick)
24335 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024336 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024337 sourcing_lnum);
24338 fcp->dbg_tick = debug_tick;
24339 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024340#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024341 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024342 func_line_end(cookie);
24343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344
Bram Moolenaar05159a02005-02-26 23:04:13 +000024345 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024346 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24347 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024348 retval = NULL;
24349 else
24350 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024351 /* Skip NULL lines (continuation lines). */
24352 while (fcp->linenr < gap->ga_len
24353 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24354 ++fcp->linenr;
24355 if (fcp->linenr >= gap->ga_len)
24356 retval = NULL;
24357 else
24358 {
24359 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24360 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024361#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024362 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024363 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024364#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366 }
24367
24368 /* Did we encounter a breakpoint? */
24369 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24370 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024371 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024373 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374 sourcing_lnum);
24375 fcp->dbg_tick = debug_tick;
24376 }
24377
24378 return retval;
24379}
24380
Bram Moolenaar05159a02005-02-26 23:04:13 +000024381#if defined(FEAT_PROFILE) || defined(PROTO)
24382/*
24383 * Called when starting to read a function line.
24384 * "sourcing_lnum" must be correct!
24385 * When skipping lines it may not actually be executed, but we won't find out
24386 * until later and we need to store the time now.
24387 */
24388 void
24389func_line_start(cookie)
24390 void *cookie;
24391{
24392 funccall_T *fcp = (funccall_T *)cookie;
24393 ufunc_T *fp = fcp->func;
24394
24395 if (fp->uf_profiling && sourcing_lnum >= 1
24396 && sourcing_lnum <= fp->uf_lines.ga_len)
24397 {
24398 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024399 /* Skip continuation lines. */
24400 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24401 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024402 fp->uf_tml_execed = FALSE;
24403 profile_start(&fp->uf_tml_start);
24404 profile_zero(&fp->uf_tml_children);
24405 profile_get_wait(&fp->uf_tml_wait);
24406 }
24407}
24408
24409/*
24410 * Called when actually executing a function line.
24411 */
24412 void
24413func_line_exec(cookie)
24414 void *cookie;
24415{
24416 funccall_T *fcp = (funccall_T *)cookie;
24417 ufunc_T *fp = fcp->func;
24418
24419 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24420 fp->uf_tml_execed = TRUE;
24421}
24422
24423/*
24424 * Called when done with a function line.
24425 */
24426 void
24427func_line_end(cookie)
24428 void *cookie;
24429{
24430 funccall_T *fcp = (funccall_T *)cookie;
24431 ufunc_T *fp = fcp->func;
24432
24433 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24434 {
24435 if (fp->uf_tml_execed)
24436 {
24437 ++fp->uf_tml_count[fp->uf_tml_idx];
24438 profile_end(&fp->uf_tml_start);
24439 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024440 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024441 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24442 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024443 }
24444 fp->uf_tml_idx = -1;
24445 }
24446}
24447#endif
24448
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449/*
24450 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024451 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024452 */
24453 int
24454func_has_ended(cookie)
24455 void *cookie;
24456{
Bram Moolenaar33570922005-01-25 22:26:29 +000024457 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458
24459 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24460 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024461 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462 || fcp->returned);
24463}
24464
24465/*
24466 * return TRUE if cookie indicates a function which "abort"s on errors.
24467 */
24468 int
24469func_has_abort(cookie)
24470 void *cookie;
24471{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024472 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473}
24474
24475#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24476typedef enum
24477{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024478 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24479 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24480 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481} var_flavour_T;
24482
24483static var_flavour_T var_flavour __ARGS((char_u *varname));
24484
24485 static var_flavour_T
24486var_flavour(varname)
24487 char_u *varname;
24488{
24489 char_u *p = varname;
24490
24491 if (ASCII_ISUPPER(*p))
24492 {
24493 while (*(++p))
24494 if (ASCII_ISLOWER(*p))
24495 return VAR_FLAVOUR_SESSION;
24496 return VAR_FLAVOUR_VIMINFO;
24497 }
24498 else
24499 return VAR_FLAVOUR_DEFAULT;
24500}
24501#endif
24502
24503#if defined(FEAT_VIMINFO) || defined(PROTO)
24504/*
24505 * Restore global vars that start with a capital from the viminfo file
24506 */
24507 int
24508read_viminfo_varlist(virp, writing)
24509 vir_T *virp;
24510 int writing;
24511{
24512 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024513 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024514 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515
24516 if (!writing && (find_viminfo_parameter('!') != NULL))
24517 {
24518 tab = vim_strchr(virp->vir_line + 1, '\t');
24519 if (tab != NULL)
24520 {
24521 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024522 switch (*tab)
24523 {
24524 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024525#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024526 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024527#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024528 case 'D': type = VAR_DICT; break;
24529 case 'L': type = VAR_LIST; break;
24530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531
24532 tab = vim_strchr(tab, '\t');
24533 if (tab != NULL)
24534 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024535 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024536 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024537 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024539#ifdef FEAT_FLOAT
24540 else if (type == VAR_FLOAT)
24541 (void)string2float(tab + 1, &tv.vval.v_float);
24542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024543 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024544 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024545 if (type == VAR_DICT || type == VAR_LIST)
24546 {
24547 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24548
24549 if (etv == NULL)
24550 /* Failed to parse back the dict or list, use it as a
24551 * string. */
24552 tv.v_type = VAR_STRING;
24553 else
24554 {
24555 vim_free(tv.vval.v_string);
24556 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024557 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024558 }
24559 }
24560
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024561 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024562
24563 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024564 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024565 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24566 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 }
24568 }
24569 }
24570
24571 return viminfo_readline(virp);
24572}
24573
24574/*
24575 * Write global vars that start with a capital to the viminfo file
24576 */
24577 void
24578write_viminfo_varlist(fp)
24579 FILE *fp;
24580{
Bram Moolenaar33570922005-01-25 22:26:29 +000024581 hashitem_T *hi;
24582 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024583 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024584 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024585 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024586 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024587 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588
24589 if (find_viminfo_parameter('!') == NULL)
24590 return;
24591
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024592 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024593
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024594 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024595 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024597 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024598 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024599 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024600 this_var = HI2DI(hi);
24601 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024602 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024603 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024604 {
24605 case VAR_STRING: s = "STR"; break;
24606 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024607#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024608 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024609#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024610 case VAR_DICT: s = "DIC"; break;
24611 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024612 default: continue;
24613 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024614 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024615 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024616 if (p != NULL)
24617 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024618 vim_free(tofree);
24619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 }
24621 }
24622}
24623#endif
24624
24625#if defined(FEAT_SESSION) || defined(PROTO)
24626 int
24627store_session_globals(fd)
24628 FILE *fd;
24629{
Bram Moolenaar33570922005-01-25 22:26:29 +000024630 hashitem_T *hi;
24631 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024632 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633 char_u *p, *t;
24634
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024635 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024636 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024638 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024640 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024641 this_var = HI2DI(hi);
24642 if ((this_var->di_tv.v_type == VAR_NUMBER
24643 || this_var->di_tv.v_type == VAR_STRING)
24644 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024645 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024646 /* Escape special characters with a backslash. Turn a LF and
24647 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024648 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024649 (char_u *)"\\\"\n\r");
24650 if (p == NULL) /* out of memory */
24651 break;
24652 for (t = p; *t != NUL; ++t)
24653 if (*t == '\n')
24654 *t = 'n';
24655 else if (*t == '\r')
24656 *t = 'r';
24657 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024658 this_var->di_key,
24659 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24660 : ' ',
24661 p,
24662 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24663 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024664 || put_eol(fd) == FAIL)
24665 {
24666 vim_free(p);
24667 return FAIL;
24668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024669 vim_free(p);
24670 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024671#ifdef FEAT_FLOAT
24672 else if (this_var->di_tv.v_type == VAR_FLOAT
24673 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24674 {
24675 float_T f = this_var->di_tv.vval.v_float;
24676 int sign = ' ';
24677
24678 if (f < 0)
24679 {
24680 f = -f;
24681 sign = '-';
24682 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024683 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024684 this_var->di_key, sign, f) < 0)
24685 || put_eol(fd) == FAIL)
24686 return FAIL;
24687 }
24688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024689 }
24690 }
24691 return OK;
24692}
24693#endif
24694
Bram Moolenaar661b1822005-07-28 22:36:45 +000024695/*
24696 * Display script name where an item was last set.
24697 * Should only be invoked when 'verbose' is non-zero.
24698 */
24699 void
24700last_set_msg(scriptID)
24701 scid_T scriptID;
24702{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024703 char_u *p;
24704
Bram Moolenaar661b1822005-07-28 22:36:45 +000024705 if (scriptID != 0)
24706 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024707 p = home_replace_save(NULL, get_scriptname(scriptID));
24708 if (p != NULL)
24709 {
24710 verbose_enter();
24711 MSG_PUTS(_("\n\tLast set from "));
24712 MSG_PUTS(p);
24713 vim_free(p);
24714 verbose_leave();
24715 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024716 }
24717}
24718
Bram Moolenaard812df62008-11-09 12:46:09 +000024719/*
24720 * List v:oldfiles in a nice way.
24721 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024722 void
24723ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024724 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024725{
24726 list_T *l = vimvars[VV_OLDFILES].vv_list;
24727 listitem_T *li;
24728 int nr = 0;
24729
24730 if (l == NULL)
24731 msg((char_u *)_("No old files"));
24732 else
24733 {
24734 msg_start();
24735 msg_scroll = TRUE;
24736 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24737 {
24738 msg_outnum((long)++nr);
24739 MSG_PUTS(": ");
24740 msg_outtrans(get_tv_string(&li->li_tv));
24741 msg_putchar('\n');
24742 out_flush(); /* output one line at a time */
24743 ui_breakcheck();
24744 }
24745 /* Assume "got_int" was set to truncate the listing. */
24746 got_int = FALSE;
24747
24748#ifdef FEAT_BROWSE_CMD
24749 if (cmdmod.browse)
24750 {
24751 quit_more = FALSE;
24752 nr = prompt_for_number(FALSE);
24753 msg_starthere();
24754 if (nr > 0)
24755 {
24756 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24757 (long)nr);
24758
24759 if (p != NULL)
24760 {
24761 p = expand_env_save(p);
24762 eap->arg = p;
24763 eap->cmdidx = CMD_edit;
24764 cmdmod.browse = FALSE;
24765 do_exedit(eap, NULL);
24766 vim_free(p);
24767 }
24768 }
24769 }
24770#endif
24771 }
24772}
24773
Bram Moolenaar53744302015-07-17 17:38:22 +020024774/* reset v:option_new, v:option_old and v:option_type */
24775 void
24776reset_v_option_vars()
24777{
24778 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24779 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24780 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24781}
24782
24783
Bram Moolenaar071d4272004-06-13 20:20:40 +000024784#endif /* FEAT_EVAL */
24785
Bram Moolenaar071d4272004-06-13 20:20:40 +000024786
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024787#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788
24789#ifdef WIN3264
24790/*
24791 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24792 */
24793static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24794static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24795static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24796
24797/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024798 * Get the short path (8.3) for the filename in "fnamep".
24799 * Only works for a valid file name.
24800 * When the path gets longer "fnamep" is changed and the allocated buffer
24801 * is put in "bufp".
24802 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24803 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 */
24805 static int
24806get_short_pathname(fnamep, bufp, fnamelen)
24807 char_u **fnamep;
24808 char_u **bufp;
24809 int *fnamelen;
24810{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024811 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812 char_u *newbuf;
24813
24814 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815 l = GetShortPathName(*fnamep, *fnamep, len);
24816 if (l > len - 1)
24817 {
24818 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024819 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820 newbuf = vim_strnsave(*fnamep, l);
24821 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024822 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823
24824 vim_free(*bufp);
24825 *fnamep = *bufp = newbuf;
24826
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024827 /* Really should always succeed, as the buffer is big enough. */
24828 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024829 }
24830
24831 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024832 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024833}
24834
24835/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024836 * Get the short path (8.3) for the filename in "fname". The converted
24837 * path is returned in "bufp".
24838 *
24839 * Some of the directories specified in "fname" may not exist. This function
24840 * will shorten the existing directories at the beginning of the path and then
24841 * append the remaining non-existing path.
24842 *
24843 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024844 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024845 * bufp - Pointer to an allocated buffer for the filename.
24846 * fnamelen - Length of the filename pointed to by fname
24847 *
24848 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849 */
24850 static int
24851shortpath_for_invalid_fname(fname, bufp, fnamelen)
24852 char_u **fname;
24853 char_u **bufp;
24854 int *fnamelen;
24855{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024856 char_u *short_fname, *save_fname, *pbuf_unused;
24857 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024858 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024859 int old_len, len;
24860 int new_len, sfx_len;
24861 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862
24863 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024864 old_len = *fnamelen;
24865 save_fname = vim_strnsave(*fname, old_len);
24866 pbuf_unused = NULL;
24867 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024868
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024869 endp = save_fname + old_len - 1; /* Find the end of the copy */
24870 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024872 /*
24873 * Try shortening the supplied path till it succeeds by removing one
24874 * directory at a time from the tail of the path.
24875 */
24876 len = 0;
24877 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024879 /* go back one path-separator */
24880 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24881 --endp;
24882 if (endp <= save_fname)
24883 break; /* processed the complete path */
24884
24885 /*
24886 * Replace the path separator with a NUL and try to shorten the
24887 * resulting path.
24888 */
24889 ch = *endp;
24890 *endp = 0;
24891 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024892 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024893 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24894 {
24895 retval = FAIL;
24896 goto theend;
24897 }
24898 *endp = ch; /* preserve the string */
24899
24900 if (len > 0)
24901 break; /* successfully shortened the path */
24902
24903 /* failed to shorten the path. Skip the path separator */
24904 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905 }
24906
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024907 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024909 /*
24910 * Succeeded in shortening the path. Now concatenate the shortened
24911 * path with the remaining path at the tail.
24912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024914 /* Compute the length of the new path. */
24915 sfx_len = (int)(save_endp - endp) + 1;
24916 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024918 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024920 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024921 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024922 /* There is not enough space in the currently allocated string,
24923 * copy it to a buffer big enough. */
24924 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024926 {
24927 retval = FAIL;
24928 goto theend;
24929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930 }
24931 else
24932 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024933 /* Transfer short_fname to the main buffer (it's big enough),
24934 * unless get_short_pathname() did its work in-place. */
24935 *fname = *bufp = save_fname;
24936 if (short_fname != save_fname)
24937 vim_strncpy(save_fname, short_fname, len);
24938 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024940
24941 /* concat the not-shortened part of the path */
24942 vim_strncpy(*fname + len, endp, sfx_len);
24943 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024945
24946theend:
24947 vim_free(pbuf_unused);
24948 vim_free(save_fname);
24949
24950 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951}
24952
24953/*
24954 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024955 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024956 */
24957 static int
24958shortpath_for_partial(fnamep, bufp, fnamelen)
24959 char_u **fnamep;
24960 char_u **bufp;
24961 int *fnamelen;
24962{
24963 int sepcount, len, tflen;
24964 char_u *p;
24965 char_u *pbuf, *tfname;
24966 int hasTilde;
24967
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024968 /* Count up the path separators from the RHS.. so we know which part
24969 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024971 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024972 if (vim_ispathsep(*p))
24973 ++sepcount;
24974
24975 /* Need full path first (use expand_env() to remove a "~/") */
24976 hasTilde = (**fnamep == '~');
24977 if (hasTilde)
24978 pbuf = tfname = expand_env_save(*fnamep);
24979 else
24980 pbuf = tfname = FullName_save(*fnamep, FALSE);
24981
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024982 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024983
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024984 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24985 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024986
24987 if (len == 0)
24988 {
24989 /* Don't have a valid filename, so shorten the rest of the
24990 * path if we can. This CAN give us invalid 8.3 filenames, but
24991 * there's not a lot of point in guessing what it might be.
24992 */
24993 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024994 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024996 }
24997
24998 /* Count the paths backward to find the beginning of the desired string. */
24999 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025000 {
25001#ifdef FEAT_MBYTE
25002 if (has_mbyte)
25003 p -= mb_head_off(tfname, p);
25004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025005 if (vim_ispathsep(*p))
25006 {
25007 if (sepcount == 0 || (hasTilde && sepcount == 1))
25008 break;
25009 else
25010 sepcount --;
25011 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 if (hasTilde)
25014 {
25015 --p;
25016 if (p >= tfname)
25017 *p = '~';
25018 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025019 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025020 }
25021 else
25022 ++p;
25023
25024 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25025 vim_free(*bufp);
25026 *fnamelen = (int)STRLEN(p);
25027 *bufp = pbuf;
25028 *fnamep = p;
25029
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025030 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031}
25032#endif /* WIN3264 */
25033
25034/*
25035 * Adjust a filename, according to a string of modifiers.
25036 * *fnamep must be NUL terminated when called. When returning, the length is
25037 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025038 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039 * When there is an error, *fnamep is set to NULL.
25040 */
25041 int
25042modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25043 char_u *src; /* string with modifiers */
25044 int *usedlen; /* characters after src that are used */
25045 char_u **fnamep; /* file name so far */
25046 char_u **bufp; /* buffer for allocated file name or NULL */
25047 int *fnamelen; /* length of fnamep */
25048{
25049 int valid = 0;
25050 char_u *tail;
25051 char_u *s, *p, *pbuf;
25052 char_u dirname[MAXPATHL];
25053 int c;
25054 int has_fullname = 0;
25055#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025056 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025057 int has_shortname = 0;
25058#endif
25059
25060repeat:
25061 /* ":p" - full path/file_name */
25062 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25063 {
25064 has_fullname = 1;
25065
25066 valid |= VALID_PATH;
25067 *usedlen += 2;
25068
25069 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25070 if ((*fnamep)[0] == '~'
25071#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25072 && ((*fnamep)[1] == '/'
25073# ifdef BACKSLASH_IN_FILENAME
25074 || (*fnamep)[1] == '\\'
25075# endif
25076 || (*fnamep)[1] == NUL)
25077
25078#endif
25079 )
25080 {
25081 *fnamep = expand_env_save(*fnamep);
25082 vim_free(*bufp); /* free any allocated file name */
25083 *bufp = *fnamep;
25084 if (*fnamep == NULL)
25085 return -1;
25086 }
25087
25088 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025089 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025090 {
25091 if (vim_ispathsep(*p)
25092 && p[1] == '.'
25093 && (p[2] == NUL
25094 || vim_ispathsep(p[2])
25095 || (p[2] == '.'
25096 && (p[3] == NUL || vim_ispathsep(p[3])))))
25097 break;
25098 }
25099
25100 /* FullName_save() is slow, don't use it when not needed. */
25101 if (*p != NUL || !vim_isAbsName(*fnamep))
25102 {
25103 *fnamep = FullName_save(*fnamep, *p != NUL);
25104 vim_free(*bufp); /* free any allocated file name */
25105 *bufp = *fnamep;
25106 if (*fnamep == NULL)
25107 return -1;
25108 }
25109
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025110#ifdef WIN3264
25111# if _WIN32_WINNT >= 0x0500
25112 if (vim_strchr(*fnamep, '~') != NULL)
25113 {
25114 /* Expand 8.3 filename to full path. Needed to make sure the same
25115 * file does not have two different names.
25116 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25117 p = alloc(_MAX_PATH + 1);
25118 if (p != NULL)
25119 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025120 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025121 {
25122 vim_free(*bufp);
25123 *bufp = *fnamep = p;
25124 }
25125 else
25126 vim_free(p);
25127 }
25128 }
25129# endif
25130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025131 /* Append a path separator to a directory. */
25132 if (mch_isdir(*fnamep))
25133 {
25134 /* Make room for one or two extra characters. */
25135 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25136 vim_free(*bufp); /* free any allocated file name */
25137 *bufp = *fnamep;
25138 if (*fnamep == NULL)
25139 return -1;
25140 add_pathsep(*fnamep);
25141 }
25142 }
25143
25144 /* ":." - path relative to the current directory */
25145 /* ":~" - path relative to the home directory */
25146 /* ":8" - shortname path - postponed till after */
25147 while (src[*usedlen] == ':'
25148 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25149 {
25150 *usedlen += 2;
25151 if (c == '8')
25152 {
25153#ifdef WIN3264
25154 has_shortname = 1; /* Postpone this. */
25155#endif
25156 continue;
25157 }
25158 pbuf = NULL;
25159 /* Need full path first (use expand_env() to remove a "~/") */
25160 if (!has_fullname)
25161 {
25162 if (c == '.' && **fnamep == '~')
25163 p = pbuf = expand_env_save(*fnamep);
25164 else
25165 p = pbuf = FullName_save(*fnamep, FALSE);
25166 }
25167 else
25168 p = *fnamep;
25169
25170 has_fullname = 0;
25171
25172 if (p != NULL)
25173 {
25174 if (c == '.')
25175 {
25176 mch_dirname(dirname, MAXPATHL);
25177 s = shorten_fname(p, dirname);
25178 if (s != NULL)
25179 {
25180 *fnamep = s;
25181 if (pbuf != NULL)
25182 {
25183 vim_free(*bufp); /* free any allocated file name */
25184 *bufp = pbuf;
25185 pbuf = NULL;
25186 }
25187 }
25188 }
25189 else
25190 {
25191 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25192 /* Only replace it when it starts with '~' */
25193 if (*dirname == '~')
25194 {
25195 s = vim_strsave(dirname);
25196 if (s != NULL)
25197 {
25198 *fnamep = s;
25199 vim_free(*bufp);
25200 *bufp = s;
25201 }
25202 }
25203 }
25204 vim_free(pbuf);
25205 }
25206 }
25207
25208 tail = gettail(*fnamep);
25209 *fnamelen = (int)STRLEN(*fnamep);
25210
25211 /* ":h" - head, remove "/file_name", can be repeated */
25212 /* Don't remove the first "/" or "c:\" */
25213 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25214 {
25215 valid |= VALID_HEAD;
25216 *usedlen += 2;
25217 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025218 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025219 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220 *fnamelen = (int)(tail - *fnamep);
25221#ifdef VMS
25222 if (*fnamelen > 0)
25223 *fnamelen += 1; /* the path separator is part of the path */
25224#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025225 if (*fnamelen == 0)
25226 {
25227 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25228 p = vim_strsave((char_u *)".");
25229 if (p == NULL)
25230 return -1;
25231 vim_free(*bufp);
25232 *bufp = *fnamep = tail = p;
25233 *fnamelen = 1;
25234 }
25235 else
25236 {
25237 while (tail > s && !after_pathsep(s, tail))
25238 mb_ptr_back(*fnamep, tail);
25239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025240 }
25241
25242 /* ":8" - shortname */
25243 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25244 {
25245 *usedlen += 2;
25246#ifdef WIN3264
25247 has_shortname = 1;
25248#endif
25249 }
25250
25251#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025252 /*
25253 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025254 */
25255 if (has_shortname)
25256 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025257 /* Copy the string if it is shortened by :h and when it wasn't copied
25258 * yet, because we are going to change it in place. Avoids changing
25259 * the buffer name for "%:8". */
25260 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261 {
25262 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025263 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 return -1;
25265 vim_free(*bufp);
25266 *bufp = *fnamep = p;
25267 }
25268
25269 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025270 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271 if (!has_fullname && !vim_isAbsName(*fnamep))
25272 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025273 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274 return -1;
25275 }
25276 else
25277 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025278 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279
Bram Moolenaardc935552011-08-17 15:23:23 +020025280 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025282 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283 return -1;
25284
25285 if (l == 0)
25286 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025287 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025289 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290 return -1;
25291 }
25292 *fnamelen = l;
25293 }
25294 }
25295#endif /* WIN3264 */
25296
25297 /* ":t" - tail, just the basename */
25298 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25299 {
25300 *usedlen += 2;
25301 *fnamelen -= (int)(tail - *fnamep);
25302 *fnamep = tail;
25303 }
25304
25305 /* ":e" - extension, can be repeated */
25306 /* ":r" - root, without extension, can be repeated */
25307 while (src[*usedlen] == ':'
25308 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25309 {
25310 /* find a '.' in the tail:
25311 * - for second :e: before the current fname
25312 * - otherwise: The last '.'
25313 */
25314 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25315 s = *fnamep - 2;
25316 else
25317 s = *fnamep + *fnamelen - 1;
25318 for ( ; s > tail; --s)
25319 if (s[0] == '.')
25320 break;
25321 if (src[*usedlen + 1] == 'e') /* :e */
25322 {
25323 if (s > tail)
25324 {
25325 *fnamelen += (int)(*fnamep - (s + 1));
25326 *fnamep = s + 1;
25327#ifdef VMS
25328 /* cut version from the extension */
25329 s = *fnamep + *fnamelen - 1;
25330 for ( ; s > *fnamep; --s)
25331 if (s[0] == ';')
25332 break;
25333 if (s > *fnamep)
25334 *fnamelen = s - *fnamep;
25335#endif
25336 }
25337 else if (*fnamep <= tail)
25338 *fnamelen = 0;
25339 }
25340 else /* :r */
25341 {
25342 if (s > tail) /* remove one extension */
25343 *fnamelen = (int)(s - *fnamep);
25344 }
25345 *usedlen += 2;
25346 }
25347
25348 /* ":s?pat?foo?" - substitute */
25349 /* ":gs?pat?foo?" - global substitute */
25350 if (src[*usedlen] == ':'
25351 && (src[*usedlen + 1] == 's'
25352 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25353 {
25354 char_u *str;
25355 char_u *pat;
25356 char_u *sub;
25357 int sep;
25358 char_u *flags;
25359 int didit = FALSE;
25360
25361 flags = (char_u *)"";
25362 s = src + *usedlen + 2;
25363 if (src[*usedlen + 1] == 'g')
25364 {
25365 flags = (char_u *)"g";
25366 ++s;
25367 }
25368
25369 sep = *s++;
25370 if (sep)
25371 {
25372 /* find end of pattern */
25373 p = vim_strchr(s, sep);
25374 if (p != NULL)
25375 {
25376 pat = vim_strnsave(s, (int)(p - s));
25377 if (pat != NULL)
25378 {
25379 s = p + 1;
25380 /* find end of substitution */
25381 p = vim_strchr(s, sep);
25382 if (p != NULL)
25383 {
25384 sub = vim_strnsave(s, (int)(p - s));
25385 str = vim_strnsave(*fnamep, *fnamelen);
25386 if (sub != NULL && str != NULL)
25387 {
25388 *usedlen = (int)(p + 1 - src);
25389 s = do_string_sub(str, pat, sub, flags);
25390 if (s != NULL)
25391 {
25392 *fnamep = s;
25393 *fnamelen = (int)STRLEN(s);
25394 vim_free(*bufp);
25395 *bufp = s;
25396 didit = TRUE;
25397 }
25398 }
25399 vim_free(sub);
25400 vim_free(str);
25401 }
25402 vim_free(pat);
25403 }
25404 }
25405 /* after using ":s", repeat all the modifiers */
25406 if (didit)
25407 goto repeat;
25408 }
25409 }
25410
Bram Moolenaar26df0922014-02-23 23:39:13 +010025411 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25412 {
25413 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25414 if (p == NULL)
25415 return -1;
25416 vim_free(*bufp);
25417 *bufp = *fnamep = p;
25418 *fnamelen = (int)STRLEN(p);
25419 *usedlen += 2;
25420 }
25421
Bram Moolenaar071d4272004-06-13 20:20:40 +000025422 return valid;
25423}
25424
25425/*
25426 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25427 * "flags" can be "g" to do a global substitute.
25428 * Returns an allocated string, NULL for error.
25429 */
25430 char_u *
25431do_string_sub(str, pat, sub, flags)
25432 char_u *str;
25433 char_u *pat;
25434 char_u *sub;
25435 char_u *flags;
25436{
25437 int sublen;
25438 regmatch_T regmatch;
25439 int i;
25440 int do_all;
25441 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025442 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443 garray_T ga;
25444 char_u *ret;
25445 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025446 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447
25448 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25449 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025450 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451
25452 ga_init2(&ga, 1, 200);
25453
25454 do_all = (flags[0] == 'g');
25455
25456 regmatch.rm_ic = p_ic;
25457 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25458 if (regmatch.regprog != NULL)
25459 {
25460 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025461 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025462 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25463 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025464 /* Skip empty match except for first match. */
25465 if (regmatch.startp[0] == regmatch.endp[0])
25466 {
25467 if (zero_width == regmatch.startp[0])
25468 {
25469 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025470 i = MB_PTR2LEN(tail);
25471 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25472 (size_t)i);
25473 ga.ga_len += i;
25474 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025475 continue;
25476 }
25477 zero_width = regmatch.startp[0];
25478 }
25479
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480 /*
25481 * Get some space for a temporary buffer to do the substitution
25482 * into. It will contain:
25483 * - The text up to where the match is.
25484 * - The substituted text.
25485 * - The text after the match.
25486 */
25487 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025488 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025489 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25490 {
25491 ga_clear(&ga);
25492 break;
25493 }
25494
25495 /* copy the text up to where the match is */
25496 i = (int)(regmatch.startp[0] - tail);
25497 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25498 /* add the substituted text */
25499 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25500 + ga.ga_len + i, TRUE, TRUE, FALSE);
25501 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025502 tail = regmatch.endp[0];
25503 if (*tail == NUL)
25504 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025505 if (!do_all)
25506 break;
25507 }
25508
25509 if (ga.ga_data != NULL)
25510 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25511
Bram Moolenaar473de612013-06-08 18:19:48 +020025512 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 }
25514
25515 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25516 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025517 if (p_cpo == empty_option)
25518 p_cpo = save_cpo;
25519 else
25520 /* Darn, evaluating {sub} expression changed the value. */
25521 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522
25523 return ret;
25524}
25525
25526#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */