blob: 860bc358e7da0daf92d247f8d5854673a813b0ff [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 Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
454static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
455static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
456static char_u *string_quote(char_u *str, int function);
457static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
458static int find_internal_func(char_u *name);
459static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
460static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
461static int call_func(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);
462static void emsg_funcname(char *ermsg, char_u *name);
463static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100466static void f_abs(typval_T *argvars, typval_T *rettv);
467static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100469static void f_add(typval_T *argvars, typval_T *rettv);
470static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
471static void f_and(typval_T *argvars, typval_T *rettv);
472static void f_append(typval_T *argvars, typval_T *rettv);
473static void f_argc(typval_T *argvars, typval_T *rettv);
474static void f_argidx(typval_T *argvars, typval_T *rettv);
475static void f_arglistid(typval_T *argvars, typval_T *rettv);
476static void f_argv(typval_T *argvars, typval_T *rettv);
477static void f_assert_equal(typval_T *argvars, typval_T *rettv);
478static void f_assert_exception(typval_T *argvars, typval_T *rettv);
479static void f_assert_fails(typval_T *argvars, typval_T *rettv);
480static void f_assert_false(typval_T *argvars, typval_T *rettv);
481static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100483static void f_asin(typval_T *argvars, typval_T *rettv);
484static void f_atan(typval_T *argvars, typval_T *rettv);
485static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000486#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100487static void f_browse(typval_T *argvars, typval_T *rettv);
488static void f_browsedir(typval_T *argvars, typval_T *rettv);
489static void f_bufexists(typval_T *argvars, typval_T *rettv);
490static void f_buflisted(typval_T *argvars, typval_T *rettv);
491static void f_bufloaded(typval_T *argvars, typval_T *rettv);
492static void f_bufname(typval_T *argvars, typval_T *rettv);
493static void f_bufnr(typval_T *argvars, typval_T *rettv);
494static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
495static void f_byte2line(typval_T *argvars, typval_T *rettv);
496static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
497static void f_byteidx(typval_T *argvars, typval_T *rettv);
498static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
499static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100501static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100503static void f_changenr(typval_T *argvars, typval_T *rettv);
504static void f_char2nr(typval_T *argvars, typval_T *rettv);
505static void f_cindent(typval_T *argvars, typval_T *rettv);
506static void f_clearmatches(typval_T *argvars, typval_T *rettv);
507static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000508#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100509static void f_complete(typval_T *argvars, typval_T *rettv);
510static void f_complete_add(typval_T *argvars, typval_T *rettv);
511static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000512#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100513static void f_confirm(typval_T *argvars, typval_T *rettv);
514static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100516static void f_cos(typval_T *argvars, typval_T *rettv);
517static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000518#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100519static void f_count(typval_T *argvars, typval_T *rettv);
520static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
521static void f_cursor(typval_T *argsvars, typval_T *rettv);
522static void f_deepcopy(typval_T *argvars, typval_T *rettv);
523static void f_delete(typval_T *argvars, typval_T *rettv);
524static void f_did_filetype(typval_T *argvars, typval_T *rettv);
525static void f_diff_filler(typval_T *argvars, typval_T *rettv);
526static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
527static void f_empty(typval_T *argvars, typval_T *rettv);
528static void f_escape(typval_T *argvars, typval_T *rettv);
529static void f_eval(typval_T *argvars, typval_T *rettv);
530static void f_eventhandler(typval_T *argvars, typval_T *rettv);
531static void f_executable(typval_T *argvars, typval_T *rettv);
532static void f_exepath(typval_T *argvars, typval_T *rettv);
533static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100535static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200536#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100537static void f_expand(typval_T *argvars, typval_T *rettv);
538static void f_extend(typval_T *argvars, typval_T *rettv);
539static void f_feedkeys(typval_T *argvars, typval_T *rettv);
540static void f_filereadable(typval_T *argvars, typval_T *rettv);
541static void f_filewritable(typval_T *argvars, typval_T *rettv);
542static void f_filter(typval_T *argvars, typval_T *rettv);
543static void f_finddir(typval_T *argvars, typval_T *rettv);
544static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100546static void f_float2nr(typval_T *argvars, typval_T *rettv);
547static void f_floor(typval_T *argvars, typval_T *rettv);
548static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000549#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_fnameescape(typval_T *argvars, typval_T *rettv);
551static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
552static void f_foldclosed(typval_T *argvars, typval_T *rettv);
553static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
554static void f_foldlevel(typval_T *argvars, typval_T *rettv);
555static void f_foldtext(typval_T *argvars, typval_T *rettv);
556static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
557static void f_foreground(typval_T *argvars, typval_T *rettv);
558static void f_function(typval_T *argvars, typval_T *rettv);
559static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
560static void f_get(typval_T *argvars, typval_T *rettv);
561static void f_getbufline(typval_T *argvars, typval_T *rettv);
562static void f_getbufvar(typval_T *argvars, typval_T *rettv);
563static void f_getchar(typval_T *argvars, typval_T *rettv);
564static void f_getcharmod(typval_T *argvars, typval_T *rettv);
565static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
566static void f_getcmdline(typval_T *argvars, typval_T *rettv);
567static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
568static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
569static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
570static void f_getcwd(typval_T *argvars, typval_T *rettv);
571static void f_getfontname(typval_T *argvars, typval_T *rettv);
572static void f_getfperm(typval_T *argvars, typval_T *rettv);
573static void f_getfsize(typval_T *argvars, typval_T *rettv);
574static void f_getftime(typval_T *argvars, typval_T *rettv);
575static void f_getftype(typval_T *argvars, typval_T *rettv);
576static void f_getline(typval_T *argvars, typval_T *rettv);
577static void f_getmatches(typval_T *argvars, typval_T *rettv);
578static void f_getpid(typval_T *argvars, typval_T *rettv);
579static void f_getcurpos(typval_T *argvars, typval_T *rettv);
580static void f_getpos(typval_T *argvars, typval_T *rettv);
581static void f_getqflist(typval_T *argvars, typval_T *rettv);
582static void f_getreg(typval_T *argvars, typval_T *rettv);
583static void f_getregtype(typval_T *argvars, typval_T *rettv);
584static void f_gettabvar(typval_T *argvars, typval_T *rettv);
585static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
586static void f_getwinposx(typval_T *argvars, typval_T *rettv);
587static void f_getwinposy(typval_T *argvars, typval_T *rettv);
588static void f_getwinvar(typval_T *argvars, typval_T *rettv);
589static void f_glob(typval_T *argvars, typval_T *rettv);
590static void f_globpath(typval_T *argvars, typval_T *rettv);
591static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
592static void f_has(typval_T *argvars, typval_T *rettv);
593static void f_has_key(typval_T *argvars, typval_T *rettv);
594static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
595static void f_hasmapto(typval_T *argvars, typval_T *rettv);
596static void f_histadd(typval_T *argvars, typval_T *rettv);
597static void f_histdel(typval_T *argvars, typval_T *rettv);
598static void f_histget(typval_T *argvars, typval_T *rettv);
599static void f_histnr(typval_T *argvars, typval_T *rettv);
600static void f_hlID(typval_T *argvars, typval_T *rettv);
601static void f_hlexists(typval_T *argvars, typval_T *rettv);
602static void f_hostname(typval_T *argvars, typval_T *rettv);
603static void f_iconv(typval_T *argvars, typval_T *rettv);
604static void f_indent(typval_T *argvars, typval_T *rettv);
605static void f_index(typval_T *argvars, typval_T *rettv);
606static void f_input(typval_T *argvars, typval_T *rettv);
607static void f_inputdialog(typval_T *argvars, typval_T *rettv);
608static void f_inputlist(typval_T *argvars, typval_T *rettv);
609static void f_inputrestore(typval_T *argvars, typval_T *rettv);
610static void f_inputsave(typval_T *argvars, typval_T *rettv);
611static void f_inputsecret(typval_T *argvars, typval_T *rettv);
612static void f_insert(typval_T *argvars, typval_T *rettv);
613static void f_invert(typval_T *argvars, typval_T *rettv);
614static void f_isdirectory(typval_T *argvars, typval_T *rettv);
615static void f_islocked(typval_T *argvars, typval_T *rettv);
616static void f_items(typval_T *argvars, typval_T *rettv);
617static void f_join(typval_T *argvars, typval_T *rettv);
618static void f_jsondecode(typval_T *argvars, typval_T *rettv);
619static void f_jsonencode(typval_T *argvars, typval_T *rettv);
620static void f_keys(typval_T *argvars, typval_T *rettv);
621static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
622static void f_len(typval_T *argvars, typval_T *rettv);
623static void f_libcall(typval_T *argvars, typval_T *rettv);
624static void f_libcallnr(typval_T *argvars, typval_T *rettv);
625static void f_line(typval_T *argvars, typval_T *rettv);
626static void f_line2byte(typval_T *argvars, typval_T *rettv);
627static void f_lispindent(typval_T *argvars, typval_T *rettv);
628static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000629#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100630static void f_log(typval_T *argvars, typval_T *rettv);
631static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000632#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200633#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100634static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200635#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100636static void f_map(typval_T *argvars, typval_T *rettv);
637static void f_maparg(typval_T *argvars, typval_T *rettv);
638static void f_mapcheck(typval_T *argvars, typval_T *rettv);
639static void f_match(typval_T *argvars, typval_T *rettv);
640static void f_matchadd(typval_T *argvars, typval_T *rettv);
641static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
642static void f_matcharg(typval_T *argvars, typval_T *rettv);
643static void f_matchdelete(typval_T *argvars, typval_T *rettv);
644static void f_matchend(typval_T *argvars, typval_T *rettv);
645static void f_matchlist(typval_T *argvars, typval_T *rettv);
646static void f_matchstr(typval_T *argvars, typval_T *rettv);
647static void f_max(typval_T *argvars, typval_T *rettv);
648static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000649#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100650static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000651#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100652static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100653#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100654static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100655#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
657static void f_nr2char(typval_T *argvars, typval_T *rettv);
658static void f_or(typval_T *argvars, typval_T *rettv);
659static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100660#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100662#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000663#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100664static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000665#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100666static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
667static void f_printf(typval_T *argvars, typval_T *rettv);
668static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200669#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100670static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200671#endif
672#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100673static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200674#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_range(typval_T *argvars, typval_T *rettv);
676static void f_readfile(typval_T *argvars, typval_T *rettv);
677static void f_reltime(typval_T *argvars, typval_T *rettv);
678static void f_reltimestr(typval_T *argvars, typval_T *rettv);
679static void f_remote_expr(typval_T *argvars, typval_T *rettv);
680static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
681static void f_remote_peek(typval_T *argvars, typval_T *rettv);
682static void f_remote_read(typval_T *argvars, typval_T *rettv);
683static void f_remote_send(typval_T *argvars, typval_T *rettv);
684static void f_remove(typval_T *argvars, typval_T *rettv);
685static void f_rename(typval_T *argvars, typval_T *rettv);
686static void f_repeat(typval_T *argvars, typval_T *rettv);
687static void f_resolve(typval_T *argvars, typval_T *rettv);
688static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_screenattr(typval_T *argvars, typval_T *rettv);
693static void f_screenchar(typval_T *argvars, typval_T *rettv);
694static void f_screencol(typval_T *argvars, typval_T *rettv);
695static void f_screenrow(typval_T *argvars, typval_T *rettv);
696static void f_search(typval_T *argvars, typval_T *rettv);
697static void f_searchdecl(typval_T *argvars, typval_T *rettv);
698static void f_searchpair(typval_T *argvars, typval_T *rettv);
699static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
700static void f_searchpos(typval_T *argvars, typval_T *rettv);
701static void f_server2client(typval_T *argvars, typval_T *rettv);
702static void f_serverlist(typval_T *argvars, typval_T *rettv);
703static void f_setbufvar(typval_T *argvars, typval_T *rettv);
704static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
705static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
706static void f_setline(typval_T *argvars, typval_T *rettv);
707static void f_setloclist(typval_T *argvars, typval_T *rettv);
708static void f_setmatches(typval_T *argvars, typval_T *rettv);
709static void f_setpos(typval_T *argvars, typval_T *rettv);
710static void f_setqflist(typval_T *argvars, typval_T *rettv);
711static void f_setreg(typval_T *argvars, typval_T *rettv);
712static void f_settabvar(typval_T *argvars, typval_T *rettv);
713static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
714static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100715#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100717#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_shellescape(typval_T *argvars, typval_T *rettv);
719static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
720static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_sin(typval_T *argvars, typval_T *rettv);
723static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000724#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100725static void f_sort(typval_T *argvars, typval_T *rettv);
726static void f_soundfold(typval_T *argvars, typval_T *rettv);
727static void f_spellbadword(typval_T *argvars, typval_T *rettv);
728static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
729static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100731static void f_sqrt(typval_T *argvars, typval_T *rettv);
732static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_str2nr(typval_T *argvars, typval_T *rettv);
735static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000736#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100737static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000738#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_stridx(typval_T *argvars, typval_T *rettv);
740static void f_string(typval_T *argvars, typval_T *rettv);
741static void f_strlen(typval_T *argvars, typval_T *rettv);
742static void f_strpart(typval_T *argvars, typval_T *rettv);
743static void f_strridx(typval_T *argvars, typval_T *rettv);
744static void f_strtrans(typval_T *argvars, typval_T *rettv);
745static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
746static void f_strwidth(typval_T *argvars, typval_T *rettv);
747static void f_submatch(typval_T *argvars, typval_T *rettv);
748static void f_substitute(typval_T *argvars, typval_T *rettv);
749static void f_synID(typval_T *argvars, typval_T *rettv);
750static void f_synIDattr(typval_T *argvars, typval_T *rettv);
751static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
752static void f_synstack(typval_T *argvars, typval_T *rettv);
753static void f_synconcealed(typval_T *argvars, typval_T *rettv);
754static void f_system(typval_T *argvars, typval_T *rettv);
755static void f_systemlist(typval_T *argvars, typval_T *rettv);
756static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
757static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
758static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
759static void f_taglist(typval_T *argvars, typval_T *rettv);
760static void f_tagfiles(typval_T *argvars, typval_T *rettv);
761static void f_tempname(typval_T *argvars, typval_T *rettv);
762static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200763#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_tan(typval_T *argvars, typval_T *rettv);
765static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200766#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_tolower(typval_T *argvars, typval_T *rettv);
768static void f_toupper(typval_T *argvars, typval_T *rettv);
769static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000770#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100771static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000772#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100773static void f_type(typval_T *argvars, typval_T *rettv);
774static void f_undofile(typval_T *argvars, typval_T *rettv);
775static void f_undotree(typval_T *argvars, typval_T *rettv);
776static void f_uniq(typval_T *argvars, typval_T *rettv);
777static void f_values(typval_T *argvars, typval_T *rettv);
778static void f_virtcol(typval_T *argvars, typval_T *rettv);
779static void f_visualmode(typval_T *argvars, typval_T *rettv);
780static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
781static void f_winbufnr(typval_T *argvars, typval_T *rettv);
782static void f_wincol(typval_T *argvars, typval_T *rettv);
783static void f_winheight(typval_T *argvars, typval_T *rettv);
784static void f_winline(typval_T *argvars, typval_T *rettv);
785static void f_winnr(typval_T *argvars, typval_T *rettv);
786static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
787static void f_winrestview(typval_T *argvars, typval_T *rettv);
788static void f_winsaveview(typval_T *argvars, typval_T *rettv);
789static void f_winwidth(typval_T *argvars, typval_T *rettv);
790static void f_writefile(typval_T *argvars, typval_T *rettv);
791static void f_wordcount(typval_T *argvars, typval_T *rettv);
792static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000793
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
795static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
796static int get_env_len(char_u **arg);
797static int get_id_len(char_u **arg);
798static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
799static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000800#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
801#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
802 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
804static int eval_isnamec(int c);
805static int eval_isnamec1(int c);
806static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
807static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
808static typval_T *alloc_tv(void);
809static typval_T *alloc_string_tv(char_u *string);
810static void init_tv(typval_T *varp);
811static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100812#ifdef FEAT_FLOAT
813static float_T get_tv_float(typval_T *varp);
814#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100815static linenr_T get_tv_lnum(typval_T *argvars);
816static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
817static char_u *get_tv_string(typval_T *varp);
818static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
819static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
820static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
821static hashtab_T *find_var_ht(char_u *name, char_u **varname);
822static funccall_T *get_funccal(void);
823static void vars_clear_ext(hashtab_T *ht, int free_val);
824static void delete_var(hashtab_T *ht, hashitem_T *hi);
825static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
826static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
827static void set_var(char_u *name, typval_T *varp, int copy);
828static int var_check_ro(int flags, char_u *name, int use_gettext);
829static int var_check_fixed(int flags, char_u *name, int use_gettext);
830static int var_check_func_name(char_u *name, int new_var);
831static int valid_varname(char_u *varname);
832static int tv_check_lock(int lock, char_u *name, int use_gettext);
833static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
834static char_u *find_option_end(char_u **arg, int *opt_flags);
835static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
836static int eval_fname_script(char_u *p);
837static int eval_fname_sid(char_u *p);
838static void list_func_head(ufunc_T *fp, int indent);
839static ufunc_T *find_func(char_u *name);
840static int function_exists(char_u *name);
841static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100843static void func_do_profile(ufunc_T *fp);
844static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
845static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000846static int
847# ifdef __BORLANDC__
848 _RTLENTRYF
849# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100850 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000851static int
852# ifdef __BORLANDC__
853 _RTLENTRYF
854# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100855 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100857static int script_autoload(char_u *name, int reload);
858static char_u *autoload_name(char_u *name);
859static void cat_func_name(char_u *buf, ufunc_T *fp);
860static void func_free(ufunc_T *fp);
861static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
862static int can_free_funccal(funccall_T *fc, int copyID) ;
863static void free_funccal(funccall_T *fc, int free_val);
864static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
865static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
866static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
867static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
868static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
869static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
870static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
871static int write_list(FILE *fd, list_T *list, int binary);
872static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000873
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200874
875#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100876static int compare_func_name(const void *s1, const void *s2);
877static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200878#endif
879
Bram Moolenaar33570922005-01-25 22:26:29 +0000880/*
881 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000882 */
883 void
884eval_init()
885{
Bram Moolenaar33570922005-01-25 22:26:29 +0000886 int i;
887 struct vimvar *p;
888
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200889 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
890 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200891 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000892 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000893 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 STRCPY(p->vv_di.di_key, p->vv_name);
899 if (p->vv_flags & VV_RO)
900 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
901 else if (p->vv_flags & VV_RO_SBX)
902 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
903 else
904 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000905
906 /* add to v: scope dict, unless the value is not always available */
907 if (p->vv_type != VAR_UNKNOWN)
908 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000909 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000910 /* add to compat scope dict */
911 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000912 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000913 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100914 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200915 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100916 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100917
918 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
919 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
920 set_vim_var_nr(VV_NONE, VVAL_NONE);
921 set_vim_var_nr(VV_NULL, VVAL_NULL);
922
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200923 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200924
925#ifdef EBCDIC
926 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100927 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200928 */
929 sortFunctions();
930#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000931}
932
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000933#if defined(EXITFREE) || defined(PROTO)
934 void
935eval_clear()
936{
937 int i;
938 struct vimvar *p;
939
940 for (i = 0; i < VV_LEN; ++i)
941 {
942 p = &vimvars[i];
943 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000944 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000945 vim_free(p->vv_str);
946 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000947 }
948 else if (p->vv_di.di_tv.v_type == VAR_LIST)
949 {
950 list_unref(p->vv_list);
951 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000952 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000953 }
954 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000955 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000956 hash_clear(&compat_hashtab);
957
Bram Moolenaard9fba312005-06-26 22:34:35 +0000958 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100959# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200960 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100961# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962
963 /* global variables */
964 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000965
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000966 /* autoloaded script names */
967 ga_clear_strings(&ga_loaded);
968
Bram Moolenaarcca74132013-09-25 21:00:28 +0200969 /* Script-local variables. First clear all the variables and in a second
970 * loop free the scriptvar_T, because a variable in one script might hold
971 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200972 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200973 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200974 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200975 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200976 ga_clear(&ga_scripts);
977
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000978 /* unreferenced lists and dicts */
979 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000980
981 /* functions */
982 free_all_functions();
983 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000984}
985#endif
986
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 * Return the name of the executed function.
989 */
990 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100991func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000993 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994}
995
996/*
997 * Return the address holding the next breakpoint line for a funccall cookie.
998 */
999 linenr_T *
1000func_breakpoint(cookie)
1001 void *cookie;
1002{
Bram Moolenaar33570922005-01-25 22:26:29 +00001003 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004}
1005
1006/*
1007 * Return the address holding the debug tick for a funccall cookie.
1008 */
1009 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001010func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
Bram Moolenaar33570922005-01-25 22:26:29 +00001012 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013}
1014
1015/*
1016 * Return the nesting level for a funccall cookie.
1017 */
1018 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001019func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar33570922005-01-25 22:26:29 +00001021 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022}
1023
1024/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001025funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001027/* pointer to list of previously used funccal, still around because some
1028 * item in it is still being used. */
1029funccall_T *previous_funccal = NULL;
1030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031/*
1032 * Return TRUE when a function was ended by a ":return" command.
1033 */
1034 int
1035current_func_returned()
1036{
1037 return current_funccal->returned;
1038}
1039
1040
1041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 * Set an internal variable to a string value. Creates the variable if it does
1043 * not already exist.
1044 */
1045 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001046set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001048 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001049 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
1051 val = vim_strsave(value);
1052 if (val != NULL)
1053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001054 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001055 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001057 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001058 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
1060 }
1061}
1062
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001064static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065static char_u *redir_endp = NULL;
1066static char_u *redir_varname = NULL;
1067
1068/*
1069 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001070 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 * Returns OK if successfully completed the setup. FAIL otherwise.
1072 */
1073 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001074var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075{
1076 int save_emsg;
1077 int err;
1078 typval_T tv;
1079
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001080 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001081 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 {
1083 EMSG(_(e_invarg));
1084 return FAIL;
1085 }
1086
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 redir_varname = vim_strsave(name);
1089 if (redir_varname == NULL)
1090 return FAIL;
1091
1092 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1093 if (redir_lval == NULL)
1094 {
1095 var_redir_stop();
1096 return FAIL;
1097 }
1098
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099 /* The output is stored in growarray "redir_ga" until redirection ends. */
1100 ga_init2(&redir_ga, (int)sizeof(char), 500);
1101
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001103 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001104 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1106 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001107 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 if (redir_endp != NULL && *redir_endp != NUL)
1109 /* Trailing characters are present after the variable name */
1110 EMSG(_(e_trailing));
1111 else
1112 EMSG(_(e_invarg));
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 }
1117
1118 /* check if we can write to the variable: set it to or append an empty
1119 * string */
1120 save_emsg = did_emsg;
1121 did_emsg = FALSE;
1122 tv.v_type = VAR_STRING;
1123 tv.vval.v_string = (char_u *)"";
1124 if (append)
1125 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1126 else
1127 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001128 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001130 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 if (err)
1132 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001133 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 var_redir_stop();
1135 return FAIL;
1136 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137
1138 return OK;
1139}
1140
1141/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 * Append "value[value_len]" to the variable set by var_redir_start().
1143 * The actual appending is postponed until redirection ends, because the value
1144 * appended may in fact be the string we write to, changing it may cause freed
1145 * memory to be used:
1146 * :redir => foo
1147 * :let foo
1148 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 */
1150 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001151var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001153 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154
1155 if (redir_lval == NULL)
1156 return;
1157
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001158 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001159 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001161 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001163 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164 {
1165 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001166 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 }
1168 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170}
1171
1172/*
1173 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001174 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175 */
1176 void
1177var_redir_stop()
1178{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001179 typval_T tv;
1180
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 if (redir_lval != NULL)
1182 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001183 /* If there was no error: assign the text to the variable. */
1184 if (redir_endp != NULL)
1185 {
1186 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1187 tv.v_type = VAR_STRING;
1188 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001189 /* Call get_lval() again, if it's inside a Dict or List it may
1190 * have changed. */
1191 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001192 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001193 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1194 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1195 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001196 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001197
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001198 /* free the collected output */
1199 vim_free(redir_ga.ga_data);
1200 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001201
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 vim_free(redir_lval);
1203 redir_lval = NULL;
1204 }
1205 vim_free(redir_varname);
1206 redir_varname = NULL;
1207}
1208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209# if defined(FEAT_MBYTE) || defined(PROTO)
1210 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001211eval_charconvert(
1212 char_u *enc_from,
1213 char_u *enc_to,
1214 char_u *fname_from,
1215 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
1217 int err = FALSE;
1218
1219 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1220 set_vim_var_string(VV_CC_TO, enc_to, -1);
1221 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1222 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1223 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1224 err = TRUE;
1225 set_vim_var_string(VV_CC_FROM, NULL, -1);
1226 set_vim_var_string(VV_CC_TO, NULL, -1);
1227 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1228 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1229
1230 if (err)
1231 return FAIL;
1232 return OK;
1233}
1234# endif
1235
1236# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1237 int
1238eval_printexpr(fname, args)
1239 char_u *fname;
1240 char_u *args;
1241{
1242 int err = FALSE;
1243
1244 set_vim_var_string(VV_FNAME_IN, fname, -1);
1245 set_vim_var_string(VV_CMDARG, args, -1);
1246 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1247 err = TRUE;
1248 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1249 set_vim_var_string(VV_CMDARG, NULL, -1);
1250
1251 if (err)
1252 {
1253 mch_remove(fname);
1254 return FAIL;
1255 }
1256 return OK;
1257}
1258# endif
1259
1260# if defined(FEAT_DIFF) || defined(PROTO)
1261 void
1262eval_diff(origfile, newfile, outfile)
1263 char_u *origfile;
1264 char_u *newfile;
1265 char_u *outfile;
1266{
1267 int err = FALSE;
1268
1269 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1270 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1271 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1272 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1273 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1274 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1275 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1276}
1277
1278 void
1279eval_patch(origfile, difffile, outfile)
1280 char_u *origfile;
1281 char_u *difffile;
1282 char_u *outfile;
1283{
1284 int err;
1285
1286 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1287 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1288 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1289 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1290 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1291 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1292 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1293}
1294# endif
1295
1296/*
1297 * Top level evaluation function, returning a boolean.
1298 * Sets "error" to TRUE if there was an error.
1299 * Return TRUE or FALSE.
1300 */
1301 int
1302eval_to_bool(arg, error, nextcmd, skip)
1303 char_u *arg;
1304 int *error;
1305 char_u **nextcmd;
1306 int skip; /* only parse, don't execute */
1307{
Bram Moolenaar33570922005-01-25 22:26:29 +00001308 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 int retval = FALSE;
1310
1311 if (skip)
1312 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 else
1316 {
1317 *error = FALSE;
1318 if (!skip)
1319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001320 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001321 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 }
1324 if (skip)
1325 --emsg_skip;
1326
1327 return retval;
1328}
1329
1330/*
1331 * Top level evaluation function, returning a string. If "skip" is TRUE,
1332 * only parsing to "nextcmd" is done, without reporting errors. Return
1333 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1334 */
1335 char_u *
1336eval_to_string_skip(arg, nextcmd, skip)
1337 char_u *arg;
1338 char_u **nextcmd;
1339 int skip; /* only parse, don't execute */
1340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *retval;
1343
1344 if (skip)
1345 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 retval = NULL;
1348 else
1349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001350 retval = vim_strsave(get_tv_string(&tv));
1351 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 if (skip)
1354 --emsg_skip;
1355
1356 return retval;
1357}
1358
1359/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001360 * Skip over an expression at "*pp".
1361 * Return FAIL for an error, OK otherwise.
1362 */
1363 int
1364skip_expr(pp)
1365 char_u **pp;
1366{
Bram Moolenaar33570922005-01-25 22:26:29 +00001367 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001368
1369 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001370 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001371}
1372
1373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001375 * When "convert" is TRUE convert a List into a sequence of lines and convert
1376 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 * Return pointer to allocated memory, or NULL for failure.
1378 */
1379 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001380eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 char_u *arg;
1382 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001383 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
Bram Moolenaar33570922005-01-25 22:26:29 +00001385 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001387 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001388#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001389 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 retval = NULL;
1394 else
1395 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001396 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 {
1398 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001399 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001400 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001401 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001402 if (tv.vval.v_list->lv_len > 0)
1403 ga_append(&ga, NL);
1404 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001405 ga_append(&ga, NUL);
1406 retval = (char_u *)ga.ga_data;
1407 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001408#ifdef FEAT_FLOAT
1409 else if (convert && tv.v_type == VAR_FLOAT)
1410 {
1411 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1412 retval = vim_strsave(numbuf);
1413 }
1414#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001415 else
1416 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419
1420 return retval;
1421}
1422
1423/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001424 * Call eval_to_string() without using current local variables and using
1425 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 */
1427 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001428eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 char_u *arg;
1430 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001431 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 char_u *retval;
1434 void *save_funccalp;
1435
1436 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001437 if (use_sandbox)
1438 ++sandbox;
1439 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001440 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001441 if (use_sandbox)
1442 --sandbox;
1443 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 restore_funccal(save_funccalp);
1445 return retval;
1446}
1447
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448/*
1449 * Top level evaluation function, returning a number.
1450 * Evaluates "expr" silently.
1451 * Returns -1 for an error.
1452 */
1453 int
1454eval_to_number(expr)
1455 char_u *expr;
1456{
Bram Moolenaar33570922005-01-25 22:26:29 +00001457 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001459 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
1461 ++emsg_off;
1462
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001463 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 retval = -1;
1465 else
1466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001467 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001468 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470 --emsg_off;
1471
1472 return retval;
1473}
1474
Bram Moolenaara40058a2005-07-11 22:42:07 +00001475/*
1476 * Prepare v: variable "idx" to be used.
1477 * Save the current typeval in "save_tv".
1478 * When not used yet add the variable to the v: hashtable.
1479 */
1480 static void
1481prepare_vimvar(idx, save_tv)
1482 int idx;
1483 typval_T *save_tv;
1484{
1485 *save_tv = vimvars[idx].vv_tv;
1486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1487 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1488}
1489
1490/*
1491 * Restore v: variable "idx" to typeval "save_tv".
1492 * When no longer defined, remove the variable from the v: hashtable.
1493 */
1494 static void
1495restore_vimvar(idx, save_tv)
1496 int idx;
1497 typval_T *save_tv;
1498{
1499 hashitem_T *hi;
1500
Bram Moolenaara40058a2005-07-11 22:42:07 +00001501 vimvars[idx].vv_tv = *save_tv;
1502 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1503 {
1504 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1505 if (HASHITEM_EMPTY(hi))
1506 EMSG2(_(e_intern2), "restore_vimvar()");
1507 else
1508 hash_remove(&vimvarht, hi);
1509 }
1510}
1511
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001512#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001513/*
1514 * Evaluate an expression to a list with suggestions.
1515 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001516 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001517 */
1518 list_T *
1519eval_spell_expr(badword, expr)
1520 char_u *badword;
1521 char_u *expr;
1522{
1523 typval_T save_val;
1524 typval_T rettv;
1525 list_T *list = NULL;
1526 char_u *p = skipwhite(expr);
1527
1528 /* Set "v:val" to the bad word. */
1529 prepare_vimvar(VV_VAL, &save_val);
1530 vimvars[VV_VAL].vv_type = VAR_STRING;
1531 vimvars[VV_VAL].vv_str = badword;
1532 if (p_verbose == 0)
1533 ++emsg_off;
1534
1535 if (eval1(&p, &rettv, TRUE) == OK)
1536 {
1537 if (rettv.v_type != VAR_LIST)
1538 clear_tv(&rettv);
1539 else
1540 list = rettv.vval.v_list;
1541 }
1542
1543 if (p_verbose == 0)
1544 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001545 restore_vimvar(VV_VAL, &save_val);
1546
1547 return list;
1548}
1549
1550/*
1551 * "list" is supposed to contain two items: a word and a number. Return the
1552 * word in "pp" and the number as the return value.
1553 * Return -1 if anything isn't right.
1554 * Used to get the good word and score from the eval_spell_expr() result.
1555 */
1556 int
1557get_spellword(list, pp)
1558 list_T *list;
1559 char_u **pp;
1560{
1561 listitem_T *li;
1562
1563 li = list->lv_first;
1564 if (li == NULL)
1565 return -1;
1566 *pp = get_tv_string(&li->li_tv);
1567
1568 li = li->li_next;
1569 if (li == NULL)
1570 return -1;
1571 return get_tv_number(&li->li_tv);
1572}
1573#endif
1574
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001575/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001576 * Top level evaluation function.
1577 * Returns an allocated typval_T with the result.
1578 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001579 */
1580 typval_T *
1581eval_expr(arg, nextcmd)
1582 char_u *arg;
1583 char_u **nextcmd;
1584{
1585 typval_T *tv;
1586
1587 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001588 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001589 {
1590 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001591 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001592 }
1593
1594 return tv;
1595}
1596
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600 * Uses argv[argc] for the function arguments. Only Number and String
1601 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001602 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001604 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001605call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 char_u *func;
1607 int argc;
1608 char_u **argv;
1609 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001610 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
Bram Moolenaar33570922005-01-25 22:26:29 +00001613 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 long n;
1615 int len;
1616 int i;
1617 int doesrange;
1618 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001621 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
1625 for (i = 0; i < argc; i++)
1626 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001627 /* Pass a NULL or empty argument as an empty string */
1628 if (argv[i] == NULL || *argv[i] == NUL)
1629 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001630 argvars[i].v_type = VAR_STRING;
1631 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001632 continue;
1633 }
1634
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001635 if (str_arg_only)
1636 len = 0;
1637 else
1638 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001639 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (len != 0 && len == (int)STRLEN(argv[i]))
1641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001642 argvars[i].v_type = VAR_NUMBER;
1643 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645 else
1646 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001647 argvars[i].v_type = VAR_STRING;
1648 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 }
1650 }
1651
1652 if (safe)
1653 {
1654 save_funccalp = save_funccal();
1655 ++sandbox;
1656 }
1657
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1659 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001661 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (safe)
1663 {
1664 --sandbox;
1665 restore_funccal(save_funccalp);
1666 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667 vim_free(argvars);
1668
1669 if (ret == FAIL)
1670 clear_tv(rettv);
1671
1672 return ret;
1673}
1674
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001675/*
1676 * Call vimL function "func" and return the result as a number.
1677 * Returns -1 when calling the function fails.
1678 * Uses argv[argc] for the function arguments.
1679 */
1680 long
1681call_func_retnr(func, argc, argv, safe)
1682 char_u *func;
1683 int argc;
1684 char_u **argv;
1685 int safe; /* use the sandbox */
1686{
1687 typval_T rettv;
1688 long retval;
1689
1690 /* All arguments are passed as strings, no conversion to number. */
1691 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1692 return -1;
1693
1694 retval = get_tv_number_chk(&rettv, NULL);
1695 clear_tv(&rettv);
1696 return retval;
1697}
1698
1699#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1700 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1701
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704 * Call vimL function "func" and return the result as a string.
1705 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 * Uses argv[argc] for the function arguments.
1707 */
1708 void *
1709call_func_retstr(func, argc, argv, safe)
1710 char_u *func;
1711 int argc;
1712 char_u **argv;
1713 int safe; /* use the sandbox */
1714{
1715 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001716 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001717
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001718 /* All arguments are passed as strings, no conversion to number. */
1719 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 return NULL;
1721
1722 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 return retval;
1725}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001726# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001728/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001729 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001731 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732 */
1733 void *
1734call_func_retlist(func, argc, argv, safe)
1735 char_u *func;
1736 int argc;
1737 char_u **argv;
1738 int safe; /* use the sandbox */
1739{
1740 typval_T rettv;
1741
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001742 /* All arguments are passed as strings, no conversion to number. */
1743 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744 return NULL;
1745
1746 if (rettv.v_type != VAR_LIST)
1747 {
1748 clear_tv(&rettv);
1749 return NULL;
1750 }
1751
1752 return rettv.vval.v_list;
1753}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754#endif
1755
1756/*
1757 * Save the current function call pointer, and set it to NULL.
1758 * Used when executing autocommands and for ":source".
1759 */
1760 void *
1761save_funccal()
1762{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001763 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 current_funccal = NULL;
1766 return (void *)fc;
1767}
1768
1769 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001770restore_funccal(vfc)
1771 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001773 funccall_T *fc = (funccall_T *)vfc;
1774
1775 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776}
1777
Bram Moolenaar05159a02005-02-26 23:04:13 +00001778#if defined(FEAT_PROFILE) || defined(PROTO)
1779/*
1780 * Prepare profiling for entering a child or something else that is not
1781 * counted for the script/function itself.
1782 * Should always be called in pair with prof_child_exit().
1783 */
1784 void
1785prof_child_enter(tm)
1786 proftime_T *tm; /* place to store waittime */
1787{
1788 funccall_T *fc = current_funccal;
1789
1790 if (fc != NULL && fc->func->uf_profiling)
1791 profile_start(&fc->prof_child);
1792 script_prof_save(tm);
1793}
1794
1795/*
1796 * Take care of time spent in a child.
1797 * Should always be called after prof_child_enter().
1798 */
1799 void
1800prof_child_exit(tm)
1801 proftime_T *tm; /* where waittime was stored */
1802{
1803 funccall_T *fc = current_funccal;
1804
1805 if (fc != NULL && fc->func->uf_profiling)
1806 {
1807 profile_end(&fc->prof_child);
1808 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1809 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1810 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1811 }
1812 script_prof_restore(tm);
1813}
1814#endif
1815
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#ifdef FEAT_FOLDING
1818/*
1819 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1820 * it in "*cp". Doesn't give error messages.
1821 */
1822 int
1823eval_foldexpr(arg, cp)
1824 char_u *arg;
1825 int *cp;
1826{
Bram Moolenaar33570922005-01-25 22:26:29 +00001827 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 int retval;
1829 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001830 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1831 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001834 if (use_sandbox)
1835 ++sandbox;
1836 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 retval = 0;
1840 else
1841 {
1842 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 if (tv.v_type == VAR_NUMBER)
1844 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001845 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 retval = 0;
1847 else
1848 {
1849 /* If the result is a string, check if there is a non-digit before
1850 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (!VIM_ISDIGIT(*s) && *s != '-')
1853 *cp = *s++;
1854 retval = atol((char *)s);
1855 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001859 if (use_sandbox)
1860 --sandbox;
1861 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
1863 return retval;
1864}
1865#endif
1866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 * ":let" list all variable values
1869 * ":let var1 var2" list variable values
1870 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001871 * ":let var += expr" assignment command.
1872 * ":let var -= expr" assignment command.
1873 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 */
1876 void
1877ex_let(eap)
1878 exarg_T *eap;
1879{
1880 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 int var_count = 0;
1885 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001887 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001888 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
Bram Moolenaardb552d602006-03-23 22:59:57 +00001890 argend = skip_var_list(arg, &var_count, &semicolon);
1891 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001893 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1894 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001895 expr = skipwhite(argend);
1896 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1897 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001899 /*
1900 * ":let" without "=": list variables
1901 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 if (*arg == '[')
1903 EMSG(_(e_invarg));
1904 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001910 list_glob_vars(&first);
1911 list_buf_vars(&first);
1912 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001913#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001914 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001915#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001916 list_script_vars(&first);
1917 list_func_vars(&first);
1918 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 eap->nextcmd = check_nextcmd(arg);
1921 }
1922 else
1923 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 op[0] = '=';
1925 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001926 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001928 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1929 op[0] = *expr; /* +=, -= or .= */
1930 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001932 else
1933 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (eap->skip)
1936 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (eap->skip)
1939 {
1940 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 --emsg_skip;
1943 }
1944 else if (i != FAIL)
1945 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950 }
1951}
1952
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953/*
1954 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1955 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 * When "nextchars" is not NULL it points to a string with characters that
1957 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1958 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 * Returns OK or FAIL;
1960 */
1961 static int
1962ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1963 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001964 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 int copy; /* copy values from "tv", don't move */
1966 int semicolon; /* from skip_var_list() */
1967 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969{
1970 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001971 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001973 listitem_T *item;
1974 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975
1976 if (*arg != '[')
1977 {
1978 /*
1979 * ":let var = expr" or ":for var in list"
1980 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 return FAIL;
1983 return OK;
1984 }
1985
1986 /*
1987 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1988 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001989 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 {
1991 EMSG(_(e_listreq));
1992 return FAIL;
1993 }
1994
1995 i = list_len(l);
1996 if (semicolon == 0 && var_count < i)
1997 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001998 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 return FAIL;
2000 }
2001 if (var_count - semicolon > i)
2002 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002003 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 return FAIL;
2005 }
2006
2007 item = l->lv_first;
2008 while (*arg != ']')
2009 {
2010 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 item = item->li_next;
2013 if (arg == NULL)
2014 return FAIL;
2015
2016 arg = skipwhite(arg);
2017 if (*arg == ';')
2018 {
2019 /* Put the rest of the list (may be empty) in the var after ';'.
2020 * Create a new list for this. */
2021 l = list_alloc();
2022 if (l == NULL)
2023 return FAIL;
2024 while (item != NULL)
2025 {
2026 list_append_tv(l, &item->li_tv);
2027 item = item->li_next;
2028 }
2029
2030 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002031 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 ltv.vval.v_list = l;
2033 l->lv_refcount = 1;
2034
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2036 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037 clear_tv(&ltv);
2038 if (arg == NULL)
2039 return FAIL;
2040 break;
2041 }
2042 else if (*arg != ',' && *arg != ']')
2043 {
2044 EMSG2(_(e_intern2), "ex_let_vars()");
2045 return FAIL;
2046 }
2047 }
2048
2049 return OK;
2050}
2051
2052/*
2053 * Skip over assignable variable "var" or list of variables "[var, var]".
2054 * Used for ":let varvar = expr" and ":for varvar in expr".
2055 * For "[var, var]" increment "*var_count" for each variable.
2056 * for "[var, var; var]" set "semicolon".
2057 * Return NULL for an error.
2058 */
2059 static char_u *
2060skip_var_list(arg, var_count, semicolon)
2061 char_u *arg;
2062 int *var_count;
2063 int *semicolon;
2064{
2065 char_u *p, *s;
2066
2067 if (*arg == '[')
2068 {
2069 /* "[var, var]": find the matching ']'. */
2070 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002071 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072 {
2073 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2074 s = skip_var_one(p);
2075 if (s == p)
2076 {
2077 EMSG2(_(e_invarg2), p);
2078 return NULL;
2079 }
2080 ++*var_count;
2081
2082 p = skipwhite(s);
2083 if (*p == ']')
2084 break;
2085 else if (*p == ';')
2086 {
2087 if (*semicolon == 1)
2088 {
2089 EMSG(_("Double ; in list of variables"));
2090 return NULL;
2091 }
2092 *semicolon = 1;
2093 }
2094 else if (*p != ',')
2095 {
2096 EMSG2(_(e_invarg2), p);
2097 return NULL;
2098 }
2099 }
2100 return p + 1;
2101 }
2102 else
2103 return skip_var_one(arg);
2104}
2105
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002107 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002108 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002109 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002110 static char_u *
2111skip_var_one(arg)
2112 char_u *arg;
2113{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002114 if (*arg == '@' && arg[1] != NUL)
2115 return arg + 2;
2116 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2117 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002118}
2119
Bram Moolenaara7043832005-01-21 11:56:39 +00002120/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 * List variables for hashtab "ht" with prefix "prefix".
2122 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002123 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002126 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002127 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002128 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar33570922005-01-25 22:26:29 +00002131 hashitem_T *hi;
2132 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002133 int todo;
2134
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002135 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002136 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2137 {
2138 if (!HASHITEM_EMPTY(hi))
2139 {
2140 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002141 di = HI2DI(hi);
2142 if (empty || di->di_tv.v_type != VAR_STRING
2143 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145 }
2146 }
2147}
2148
2149/*
2150 * List global variables.
2151 */
2152 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153list_glob_vars(first)
2154 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002155{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002157}
2158
2159/*
2160 * List buffer variables.
2161 */
2162 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163list_buf_vars(first)
2164 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002165{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002166 char_u numbuf[NUMBUFLEN];
2167
Bram Moolenaar429fa852013-04-15 12:27:36 +02002168 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002170
2171 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2173 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002174}
2175
2176/*
2177 * List window variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_win_vars(first)
2181 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002182{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002183 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002185}
2186
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002187#ifdef FEAT_WINDOWS
2188/*
2189 * List tab page variables.
2190 */
2191 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_tab_vars(first)
2193 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002194{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002195 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002197}
2198#endif
2199
Bram Moolenaara7043832005-01-21 11:56:39 +00002200/*
2201 * List Vim variables.
2202 */
2203 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204list_vim_vars(first)
2205 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208}
2209
2210/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002211 * List script-local variables, if there is a script.
2212 */
2213 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_script_vars(first)
2215 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216{
2217 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2219 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220}
2221
2222/*
2223 * List function variables, if there is a function.
2224 */
2225 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002226list_func_vars(first)
2227 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002228{
2229 if (current_funccal != NULL)
2230 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002232}
2233
2234/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 * List variables in "arg".
2236 */
2237 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002238list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 exarg_T *eap;
2240 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242{
2243 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 char_u *name_start;
2247 char_u *arg_subsc;
2248 char_u *tofree;
2249 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250
2251 while (!ends_excmd(*arg) && !got_int)
2252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002255 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2257 {
2258 emsg_severe = TRUE;
2259 EMSG(_(e_trailing));
2260 break;
2261 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 /* get_name_len() takes care of expanding curly braces */
2266 name_start = name = arg;
2267 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2268 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 /* This is mainly to keep test 49 working: when expanding
2271 * curly braces fails overrule the exception error message. */
2272 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 emsg_severe = TRUE;
2275 EMSG2(_(e_invarg2), arg);
2276 break;
2277 }
2278 error = TRUE;
2279 }
2280 else
2281 {
2282 if (tofree != NULL)
2283 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002284 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 else
2287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 /* handle d.key, l[idx], f(expr) */
2289 arg_subsc = arg;
2290 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002298 case 'g': list_glob_vars(first); break;
2299 case 'b': list_buf_vars(first); break;
2300 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002301#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002303#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002304 case 'v': list_vim_vars(first); break;
2305 case 's': list_script_vars(first); break;
2306 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 default:
2308 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002310 }
2311 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 {
2313 char_u numbuf[NUMBUFLEN];
2314 char_u *tf;
2315 int c;
2316 char_u *s;
2317
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002318 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 c = *arg;
2320 *arg = NUL;
2321 list_one_var_a((char_u *)"",
2322 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002323 tv.v_type,
2324 s == NULL ? (char_u *)"" : s,
2325 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 *arg = c;
2327 vim_free(tf);
2328 }
2329 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
2332 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333
2334 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336
2337 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
2339
2340 return arg;
2341}
2342
2343/*
2344 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2345 * Returns a pointer to the char just after the var name.
2346 * Returns NULL if there is an error.
2347 */
2348 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002351 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002353 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355{
2356 int c1;
2357 char_u *name;
2358 char_u *p;
2359 char_u *arg_end = NULL;
2360 int len;
2361 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363
2364 /*
2365 * ":let $VAR = expr": Set environment variable.
2366 */
2367 if (*arg == '$')
2368 {
2369 /* Find the end of the name. */
2370 ++arg;
2371 name = arg;
2372 len = get_env_len(&arg);
2373 if (len == 0)
2374 EMSG2(_(e_invarg2), name - 1);
2375 else
2376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 if (op != NULL && (*op == '+' || *op == '-'))
2378 EMSG2(_(e_letwrong), op);
2379 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002380 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002382 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 {
2384 c1 = name[len];
2385 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 p = get_tv_string_chk(tv);
2387 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 {
2389 int mustfree = FALSE;
2390 char_u *s = vim_getenv(name, &mustfree);
2391
2392 if (s != NULL)
2393 {
2394 p = tofree = concat_str(s, p);
2395 if (mustfree)
2396 vim_free(s);
2397 }
2398 }
2399 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002402 if (STRICMP(name, "HOME") == 0)
2403 init_homedir();
2404 else if (didset_vim && STRICMP(name, "VIM") == 0)
2405 didset_vim = FALSE;
2406 else if (didset_vimruntime
2407 && STRICMP(name, "VIMRUNTIME") == 0)
2408 didset_vimruntime = FALSE;
2409 arg_end = arg;
2410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002411 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 }
2414 }
2415 }
2416
2417 /*
2418 * ":let &option = expr": Set option value.
2419 * ":let &l:option = expr": Set local option value.
2420 * ":let &g:option = expr": Set global option value.
2421 */
2422 else if (*arg == '&')
2423 {
2424 /* Find the end of the name. */
2425 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002426 if (p == NULL || (endchars != NULL
2427 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 EMSG(_(e_letunexp));
2429 else
2430 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 long n;
2432 int opt_type;
2433 long numval;
2434 char_u *stringval = NULL;
2435 char_u *s;
2436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 c1 = *p;
2438 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439
2440 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 s = get_tv_string_chk(tv); /* != NULL if number or string */
2442 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 {
2444 opt_type = get_option_value(arg, &numval,
2445 &stringval, opt_flags);
2446 if ((opt_type == 1 && *op == '.')
2447 || (opt_type == 0 && *op != '.'))
2448 EMSG2(_(e_letwrong), op);
2449 else
2450 {
2451 if (opt_type == 1) /* number */
2452 {
2453 if (*op == '+')
2454 n = numval + n;
2455 else
2456 n = numval - n;
2457 }
2458 else if (opt_type == 0 && stringval != NULL) /* string */
2459 {
2460 s = concat_str(stringval, s);
2461 vim_free(stringval);
2462 stringval = s;
2463 }
2464 }
2465 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 if (s != NULL)
2467 {
2468 set_option_value(arg, n, s, opt_flags);
2469 arg_end = p;
2470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
2474 }
2475
2476 /*
2477 * ":let @r = expr": Set register contents.
2478 */
2479 else if (*arg == '@')
2480 {
2481 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 if (op != NULL && (*op == '+' || *op == '-'))
2483 EMSG2(_(e_letwrong), op);
2484 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002485 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 EMSG(_(e_letunexp));
2487 else
2488 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002489 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 char_u *s;
2491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 p = get_tv_string_chk(tv);
2493 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002495 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 if (s != NULL)
2497 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002498 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 vim_free(s);
2500 }
2501 }
2502 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002504 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 arg_end = arg + 1;
2506 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 }
2509 }
2510
2511 /*
2512 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002515 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002519 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2523 EMSG(_(e_letunexp));
2524 else
2525 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002526 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 arg_end = p;
2528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 }
2532
2533 else
2534 EMSG2(_(e_invarg2), arg);
2535
2536 return arg_end;
2537}
2538
2539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2541 */
2542 static int
2543check_changedtick(arg)
2544 char_u *arg;
2545{
2546 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2547 {
2548 EMSG2(_(e_readonlyvar), arg);
2549 return TRUE;
2550 }
2551 return FALSE;
2552}
2553
2554/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 * Get an lval: variable, Dict item or List item that can be assigned a value
2556 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2557 * "name.key", "name.key[expr]" etc.
2558 * Indexing only works if "name" is an existing List or Dictionary.
2559 * "name" points to the start of the name.
2560 * If "rettv" is not NULL it points to the value to be assigned.
2561 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2562 * wrong; must end in space or cmd separator.
2563 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002564 * flags:
2565 * GLV_QUIET: do not give error messages
2566 * GLV_NO_AUTOLOAD: do not use script autoloading
2567 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002569 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 */
2572 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002573get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002575 typval_T *rettv;
2576 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 int unlet;
2578 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002579 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002580 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 char_u *expr_start, *expr_end;
2584 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 dictitem_T *v;
2586 typval_T var1;
2587 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002593 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002596 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597
2598 if (skip)
2599 {
2600 /* When skipping just find the end of the name. */
2601 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002602 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 }
2604
2605 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002606 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (expr_start != NULL)
2608 {
2609 /* Don't expand the name when we already know there is an error. */
2610 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2611 && *p != '[' && *p != '.')
2612 {
2613 EMSG(_(e_trailing));
2614 return NULL;
2615 }
2616
2617 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2618 if (lp->ll_exp_name == NULL)
2619 {
2620 /* Report an invalid expression in braces, unless the
2621 * expression evaluation has been cancelled due to an
2622 * aborting error, an interrupt, or an exception. */
2623 if (!aborting() && !quiet)
2624 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002625 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 EMSG2(_(e_invarg2), name);
2627 return NULL;
2628 }
2629 }
2630 lp->ll_name = lp->ll_exp_name;
2631 }
2632 else
2633 lp->ll_name = name;
2634
2635 /* Without [idx] or .key we are done. */
2636 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2637 return p;
2638
2639 cc = *p;
2640 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002641 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (v == NULL && !quiet)
2643 EMSG2(_(e_undefvar), lp->ll_name);
2644 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 if (v == NULL)
2646 return NULL;
2647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 /*
2649 * Loop until no more [idx] or .key is following.
2650 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002651 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2655 && !(lp->ll_tv->v_type == VAR_DICT
2656 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
2659 EMSG(_("E689: Can only index a List or Dictionary"));
2660 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E708: [:] must come last"));
2666 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 len = -1;
2670 if (*p == '.')
2671 {
2672 key = p + 1;
2673 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2674 ;
2675 if (len == 0)
2676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (!quiet)
2678 EMSG(_(e_emptykey));
2679 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 }
2681 p = key + len;
2682 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 else
2684 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (*p == ':')
2688 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689 else
2690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 empty1 = FALSE;
2692 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 if (get_tv_string_chk(&var1) == NULL)
2695 {
2696 /* not a number or string */
2697 clear_tv(&var1);
2698 return NULL;
2699 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701
2702 /* Optionally get the second index [ :expr]. */
2703 if (*p == ':')
2704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002708 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 if (!empty1)
2710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (rettv != NULL && (rettv->v_type != VAR_LIST
2714 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (!quiet)
2717 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
2722 p = skipwhite(p + 1);
2723 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 else
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (!empty1)
2731 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002734 if (get_tv_string_chk(&var2) == NULL)
2735 {
2736 /* not a number or string */
2737 if (!empty1)
2738 clear_tv(&var1);
2739 clear_tv(&var2);
2740 return NULL;
2741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (!quiet)
2751 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 if (!empty1)
2753 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 }
2758
2759 /* Skip to past ']'. */
2760 ++p;
2761 }
2762
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 {
2765 if (len == -1)
2766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002768 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (*key == NUL)
2770 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (!quiet)
2772 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002777 lp->ll_list = NULL;
2778 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002779 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002780
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 /* When assigning to a scope dictionary check that a function and
2782 * variable name is valid (only variable name unless it is l: or
2783 * g: dictionary). Disallow overwriting a builtin function. */
2784 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002785 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002786 int prevval;
2787 int wrong;
2788
2789 if (len != -1)
2790 {
2791 prevval = key[len];
2792 key[len] = NUL;
2793 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002794 else
2795 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002796 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2797 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002798 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002799 || !valid_varname(key);
2800 if (len != -1)
2801 key[len] = prevval;
2802 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 return NULL;
2804 }
2805
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002808 /* Can't add "v:" variable. */
2809 if (lp->ll_dict == &vimvardict)
2810 {
2811 EMSG2(_(e_illvar), name);
2812 return NULL;
2813 }
2814
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002815 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002819 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (len == -1)
2821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 if (len == -1)
2829 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 p = NULL;
2832 break;
2833 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002834 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002835 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002836 return NULL;
2837
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 if (len == -1)
2839 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 }
2842 else
2843 {
2844 /*
2845 * Get the number and item for the only or first index of the List.
2846 */
2847 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 else
2850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var1);
2853 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002854 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_list = lp->ll_tv->vval.v_list;
2856 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2857 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002859 if (lp->ll_n1 < 0)
2860 {
2861 lp->ll_n1 = 0;
2862 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2863 }
2864 }
2865 if (lp->ll_li == NULL)
2866 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002869 if (!quiet)
2870 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 }
2873
2874 /*
2875 * May need to find the item or absolute index for the second
2876 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 * When no index given: "lp->ll_empty2" is TRUE.
2878 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002882 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 {
2889 if (!quiet)
2890 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 }
2895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2897 if (lp->ll_n1 < 0)
2898 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2899 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002900 {
2901 if (!quiet)
2902 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002904 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002905 }
2906
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 }
2910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 return p;
2912}
2913
2914/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 */
2917 static void
2918clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002919 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920{
2921 vim_free(lp->ll_exp_name);
2922 vim_free(lp->ll_newkey);
2923}
2924
2925/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 */
2930 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002932 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937{
2938 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 listitem_T *ri;
2940 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941
2942 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 cc = *endp;
2947 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 if (op != NULL && *op != '=')
2949 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002952 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002953 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002954 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002955 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002957 if ((di == NULL
2958 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2959 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2960 FALSE)))
2961 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 set_var(lp->ll_name, &tv, FALSE);
2963 clear_tv(&tv);
2964 }
2965 }
2966 else
2967 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002969 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002971 else if (tv_check_lock(lp->ll_newkey == NULL
2972 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002973 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002974 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 else if (lp->ll_range)
2976 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002977 listitem_T *ll_li = lp->ll_li;
2978 int ll_n1 = lp->ll_n1;
2979
2980 /*
2981 * Check whether any of the list items is locked
2982 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002983 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002984 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002985 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002986 return;
2987 ri = ri->li_next;
2988 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2989 break;
2990 ll_li = ll_li->li_next;
2991 ++ll_n1;
2992 }
2993
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 /*
2995 * Assign the List values to the list items.
2996 */
2997 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 if (op != NULL && *op != '=')
3000 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3001 else
3002 {
3003 clear_tv(&lp->ll_li->li_tv);
3004 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3005 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 ri = ri->li_next;
3007 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3008 break;
3009 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003012 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003013 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 ri = NULL;
3015 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003016 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 lp->ll_li = lp->ll_li->li_next;
3019 ++lp->ll_n1;
3020 }
3021 if (ri != NULL)
3022 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 else if (lp->ll_empty2
3024 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 : lp->ll_n1 != lp->ll_n2)
3026 EMSG(_("E711: List value has not enough items"));
3027 }
3028 else
3029 {
3030 /*
3031 * Assign to a List or Dictionary item.
3032 */
3033 if (lp->ll_newkey != NULL)
3034 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 if (op != NULL && *op != '=')
3036 {
3037 EMSG2(_(e_letwrong), op);
3038 return;
3039 }
3040
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003042 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 if (di == NULL)
3044 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003045 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3046 {
3047 vim_free(di);
3048 return;
3049 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 else if (op != NULL && *op != '=')
3053 {
3054 tv_op(lp->ll_tv, rettv, op);
3055 return;
3056 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003057 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003059
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 /*
3061 * Assign the value to the variable or list item.
3062 */
3063 if (copy)
3064 copy_tv(rettv, lp->ll_tv);
3065 else
3066 {
3067 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003068 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003069 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003070 }
3071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072}
3073
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003074/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3076 * Returns OK or FAIL.
3077 */
3078 static int
3079tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003080 typval_T *tv1;
3081 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 char_u *op;
3083{
3084 long n;
3085 char_u numbuf[NUMBUFLEN];
3086 char_u *s;
3087
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003088 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3089 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3090 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 {
3092 switch (tv1->v_type)
3093 {
3094 case VAR_DICT:
3095 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003096 case VAR_SPECIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 break;
3098
3099 case VAR_LIST:
3100 if (*op != '+' || tv2->v_type != VAR_LIST)
3101 break;
3102 /* List += List */
3103 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3104 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3105 return OK;
3106
3107 case VAR_NUMBER:
3108 case VAR_STRING:
3109 if (tv2->v_type == VAR_LIST)
3110 break;
3111 if (*op == '+' || *op == '-')
3112 {
3113 /* nr += nr or nr -= nr*/
3114 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115#ifdef FEAT_FLOAT
3116 if (tv2->v_type == VAR_FLOAT)
3117 {
3118 float_T f = n;
3119
3120 if (*op == '+')
3121 f += tv2->vval.v_float;
3122 else
3123 f -= tv2->vval.v_float;
3124 clear_tv(tv1);
3125 tv1->v_type = VAR_FLOAT;
3126 tv1->vval.v_float = f;
3127 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003129#endif
3130 {
3131 if (*op == '+')
3132 n += get_tv_number(tv2);
3133 else
3134 n -= get_tv_number(tv2);
3135 clear_tv(tv1);
3136 tv1->v_type = VAR_NUMBER;
3137 tv1->vval.v_number = n;
3138 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 }
3140 else
3141 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003142 if (tv2->v_type == VAR_FLOAT)
3143 break;
3144
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 /* str .= str */
3146 s = get_tv_string(tv1);
3147 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3148 clear_tv(tv1);
3149 tv1->v_type = VAR_STRING;
3150 tv1->vval.v_string = s;
3151 }
3152 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003153
3154#ifdef FEAT_FLOAT
3155 case VAR_FLOAT:
3156 {
3157 float_T f;
3158
3159 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3160 && tv2->v_type != VAR_NUMBER
3161 && tv2->v_type != VAR_STRING))
3162 break;
3163 if (tv2->v_type == VAR_FLOAT)
3164 f = tv2->vval.v_float;
3165 else
3166 f = get_tv_number(tv2);
3167 if (*op == '+')
3168 tv1->vval.v_float += f;
3169 else
3170 tv1->vval.v_float -= f;
3171 }
3172 return OK;
3173#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003174 }
3175 }
3176
3177 EMSG2(_(e_letwrong), op);
3178 return FAIL;
3179}
3180
3181/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 * Add a watcher to a list.
3183 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003184 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003186 list_T *l;
3187 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188{
3189 lw->lw_next = l->lv_watch;
3190 l->lv_watch = lw;
3191}
3192
3193/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003194 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 * No warning when it isn't found...
3196 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003197 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 list_T *l;
3200 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201{
Bram Moolenaar33570922005-01-25 22:26:29 +00003202 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203
3204 lwp = &l->lv_watch;
3205 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3206 {
3207 if (lw == lwrem)
3208 {
3209 *lwp = lw->lw_next;
3210 break;
3211 }
3212 lwp = &lw->lw_next;
3213 }
3214}
3215
3216/*
3217 * Just before removing an item from a list: advance watchers to the next
3218 * item.
3219 */
3220 static void
3221list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 list_T *l;
3223 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224{
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226
3227 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3228 if (lw->lw_item == item)
3229 lw->lw_item = item->li_next;
3230}
3231
3232/*
3233 * Evaluate the expression used in a ":for var in expr" command.
3234 * "arg" points to "var".
3235 * Set "*errp" to TRUE for an error, FALSE otherwise;
3236 * Return a pointer that holds the info. Null when there is an error.
3237 */
3238 void *
3239eval_for_line(arg, errp, nextcmdp, skip)
3240 char_u *arg;
3241 int *errp;
3242 char_u **nextcmdp;
3243 int skip;
3244{
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 typval_T tv;
3248 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249
3250 *errp = TRUE; /* default: there is an error */
3251
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 if (fi == NULL)
3254 return NULL;
3255
3256 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3257 if (expr == NULL)
3258 return fi;
3259
3260 expr = skipwhite(expr);
3261 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3262 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003263 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 return fi;
3265 }
3266
3267 if (skip)
3268 ++emsg_skip;
3269 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3270 {
3271 *errp = FALSE;
3272 if (!skip)
3273 {
3274 l = tv.vval.v_list;
3275 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003276 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 clear_tv(&tv);
3279 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 else
3281 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003282 /* No need to increment the refcount, it's already set for the
3283 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 fi->fi_list = l;
3285 list_add_watch(l, &fi->fi_lw);
3286 fi->fi_lw.lw_item = l->lv_first;
3287 }
3288 }
3289 }
3290 if (skip)
3291 --emsg_skip;
3292
3293 return fi;
3294}
3295
3296/*
3297 * Use the first item in a ":for" list. Advance to the next.
3298 * Assign the values to the variable (list). "arg" points to the first one.
3299 * Return TRUE when a valid item was found, FALSE when at end of list or
3300 * something wrong.
3301 */
3302 int
3303next_for_item(fi_void, arg)
3304 void *fi_void;
3305 char_u *arg;
3306{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003307 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003309 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003310
3311 item = fi->fi_lw.lw_item;
3312 if (item == NULL)
3313 result = FALSE;
3314 else
3315 {
3316 fi->fi_lw.lw_item = item->li_next;
3317 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3318 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3319 }
3320 return result;
3321}
3322
3323/*
3324 * Free the structure used to store info used by ":for".
3325 */
3326 void
3327free_for_info(fi_void)
3328 void *fi_void;
3329{
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003332 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003333 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003335 list_unref(fi->fi_list);
3336 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 vim_free(fi);
3338}
3339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3341
3342 void
3343set_context_for_expression(xp, arg, cmdidx)
3344 expand_T *xp;
3345 char_u *arg;
3346 cmdidx_T cmdidx;
3347{
3348 int got_eq = FALSE;
3349 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 if (cmdidx == CMD_let)
3353 {
3354 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003355 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 {
3357 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003358 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 {
3360 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362 if (vim_iswhite(*p))
3363 break;
3364 }
3365 return;
3366 }
3367 }
3368 else
3369 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3370 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 while ((xp->xp_pattern = vim_strpbrk(arg,
3372 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3373 {
3374 c = *xp->xp_pattern;
3375 if (c == '&')
3376 {
3377 c = xp->xp_pattern[1];
3378 if (c == '&')
3379 {
3380 ++xp->xp_pattern;
3381 xp->xp_context = cmdidx != CMD_let || got_eq
3382 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3383 }
3384 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003387 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3388 xp->xp_pattern += 2;
3389
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392 else if (c == '$')
3393 {
3394 /* environment variable */
3395 xp->xp_context = EXPAND_ENV_VARS;
3396 }
3397 else if (c == '=')
3398 {
3399 got_eq = TRUE;
3400 xp->xp_context = EXPAND_EXPRESSION;
3401 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003402 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 && xp->xp_context == EXPAND_FUNCTIONS
3404 && vim_strchr(xp->xp_pattern, '(') == NULL)
3405 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003406 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 break;
3408 }
3409 else if (cmdidx != CMD_let || got_eq)
3410 {
3411 if (c == '"') /* string */
3412 {
3413 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3414 if (c == '\\' && xp->xp_pattern[1] != NUL)
3415 ++xp->xp_pattern;
3416 xp->xp_context = EXPAND_NOTHING;
3417 }
3418 else if (c == '\'') /* literal string */
3419 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003420 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3422 /* skip */ ;
3423 xp->xp_context = EXPAND_NOTHING;
3424 }
3425 else if (c == '|')
3426 {
3427 if (xp->xp_pattern[1] == '|')
3428 {
3429 ++xp->xp_pattern;
3430 xp->xp_context = EXPAND_EXPRESSION;
3431 }
3432 else
3433 xp->xp_context = EXPAND_COMMANDS;
3434 }
3435 else
3436 xp->xp_context = EXPAND_EXPRESSION;
3437 }
3438 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003439 /* Doesn't look like something valid, expand as an expression
3440 * anyway. */
3441 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 arg = xp->xp_pattern;
3443 if (*arg != NUL)
3444 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3445 /* skip */ ;
3446 }
3447 xp->xp_pattern = arg;
3448}
3449
3450#endif /* FEAT_CMDL_COMPL */
3451
3452/*
3453 * ":1,25call func(arg1, arg2)" function call.
3454 */
3455 void
3456ex_call(eap)
3457 exarg_T *eap;
3458{
3459 char_u *arg = eap->arg;
3460 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003462 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003464 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 linenr_T lnum;
3466 int doesrange;
3467 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003468 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003470 if (eap->skip)
3471 {
3472 /* trans_function_name() doesn't work well when skipping, use eval0()
3473 * instead to skip to any following command, e.g. for:
3474 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003475 ++emsg_skip;
3476 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3477 clear_tv(&rettv);
3478 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003479 return;
3480 }
3481
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003483 if (fudi.fd_newkey != NULL)
3484 {
3485 /* Still need to give an error message for missing key. */
3486 EMSG2(_(e_dictkey), fudi.fd_newkey);
3487 vim_free(fudi.fd_newkey);
3488 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003489 if (tofree == NULL)
3490 return;
3491
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003492 /* Increase refcount on dictionary, it could get deleted when evaluating
3493 * the arguments. */
3494 if (fudi.fd_dict != NULL)
3495 ++fudi.fd_dict->dv_refcount;
3496
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003497 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003498 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003499 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
Bram Moolenaar532c7802005-01-27 14:44:31 +00003501 /* Skip white space to allow ":call func ()". Not good, but required for
3502 * backward compatibility. */
3503 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003504 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
3506 if (*startarg != '(')
3507 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003508 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 goto end;
3510 }
3511
3512 /*
3513 * When skipping, evaluate the function once, to find the end of the
3514 * arguments.
3515 * When the function takes a range, this is discovered after the first
3516 * call, and the loop is broken.
3517 */
3518 if (eap->skip)
3519 {
3520 ++emsg_skip;
3521 lnum = eap->line2; /* do it once, also with an invalid range */
3522 }
3523 else
3524 lnum = eap->line1;
3525 for ( ; lnum <= eap->line2; ++lnum)
3526 {
3527 if (!eap->skip && eap->addr_count > 0)
3528 {
3529 curwin->w_cursor.lnum = lnum;
3530 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003531#ifdef FEAT_VIRTUALEDIT
3532 curwin->w_cursor.coladd = 0;
3533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003536 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003537 eap->line1, eap->line2, &doesrange,
3538 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
3540 failed = TRUE;
3541 break;
3542 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003543
3544 /* Handle a function returning a Funcref, Dictionary or List. */
3545 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3546 {
3547 failed = TRUE;
3548 break;
3549 }
3550
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003551 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (doesrange || eap->skip)
3553 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003556 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003557 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003558 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 if (aborting())
3560 break;
3561 }
3562 if (eap->skip)
3563 --emsg_skip;
3564
3565 if (!failed)
3566 {
3567 /* Check for trailing illegal characters and a following command. */
3568 if (!ends_excmd(*arg))
3569 {
3570 emsg_severe = TRUE;
3571 EMSG(_(e_trailing));
3572 }
3573 else
3574 eap->nextcmd = check_nextcmd(arg);
3575 }
3576
3577end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003578 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003579 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580}
3581
3582/*
3583 * ":unlet[!] var1 ... " command.
3584 */
3585 void
3586ex_unlet(eap)
3587 exarg_T *eap;
3588{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 ex_unletlock(eap, eap->arg, 0);
3590}
3591
3592/*
3593 * ":lockvar" and ":unlockvar" commands
3594 */
3595 void
3596ex_lockvar(eap)
3597 exarg_T *eap;
3598{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003600 int deep = 2;
3601
3602 if (eap->forceit)
3603 deep = -1;
3604 else if (vim_isdigit(*arg))
3605 {
3606 deep = getdigits(&arg);
3607 arg = skipwhite(arg);
3608 }
3609
3610 ex_unletlock(eap, arg, deep);
3611}
3612
3613/*
3614 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3615 */
3616 static void
3617ex_unletlock(eap, argstart, deep)
3618 exarg_T *eap;
3619 char_u *argstart;
3620 int deep;
3621{
3622 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003625 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
3627 do
3628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003630 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003631 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 if (lv.ll_name == NULL)
3633 error = TRUE; /* error but continue parsing */
3634 if (name_end == NULL || (!vim_iswhite(*name_end)
3635 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 if (name_end != NULL)
3638 {
3639 emsg_severe = TRUE;
3640 EMSG(_(e_trailing));
3641 }
3642 if (!(eap->skip || error))
3643 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 break;
3645 }
3646
3647 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003648 {
3649 if (eap->cmdidx == CMD_unlet)
3650 {
3651 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3652 error = TRUE;
3653 }
3654 else
3655 {
3656 if (do_lock_var(&lv, name_end, deep,
3657 eap->cmdidx == CMD_lockvar) == FAIL)
3658 error = TRUE;
3659 }
3660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 if (!eap->skip)
3663 clear_lval(&lv);
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 arg = skipwhite(name_end);
3666 } while (!ends_excmd(*arg));
3667
3668 eap->nextcmd = check_nextcmd(arg);
3669}
3670
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003675 int forceit;
3676{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 int ret = OK;
3678 int cc;
3679
3680 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 cc = *name_end;
3683 *name_end = NUL;
3684
3685 /* Normal name or expanded name. */
3686 if (check_changedtick(lp->ll_name))
3687 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003688 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003692 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003693 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003694 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003695 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003696 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 else if (lp->ll_range)
3698 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003699 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003700 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003701 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003702
3703 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3704 {
3705 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003706 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003707 return FAIL;
3708 ll_li = li;
3709 ++ll_n1;
3710 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711
3712 /* Delete a range of List items. */
3713 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3714 {
3715 li = lp->ll_li->li_next;
3716 listitem_remove(lp->ll_list, lp->ll_li);
3717 lp->ll_li = li;
3718 ++lp->ll_n1;
3719 }
3720 }
3721 else
3722 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003723 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 /* unlet a List item. */
3725 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003728 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003729 }
3730
3731 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003732}
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734/*
3735 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003736 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 */
3738 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003739do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003741 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
Bram Moolenaar33570922005-01-25 22:26:29 +00003743 hashtab_T *ht;
3744 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003745 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003746 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003747 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
Bram Moolenaar33570922005-01-25 22:26:29 +00003749 ht = find_var_ht(name, &varname);
3750 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003752 if (ht == &globvarht)
3753 d = &globvardict;
3754 else if (current_funccal != NULL
3755 && ht == &current_funccal->l_vars.dv_hashtab)
3756 d = &current_funccal->l_vars;
3757 else if (ht == &compat_hashtab)
3758 d = &vimvardict;
3759 else
3760 {
3761 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3762 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3763 }
3764 if (d == NULL)
3765 {
3766 EMSG2(_(e_intern2), "do_unlet()");
3767 return FAIL;
3768 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003769 hi = hash_find(ht, varname);
3770 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003771 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003772 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003773 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003774 || var_check_ro(di->di_flags, name, FALSE)
3775 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003776 return FAIL;
3777
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003778 delete_var(ht, hi);
3779 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003782 if (forceit)
3783 return OK;
3784 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 return FAIL;
3786}
3787
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003788/*
3789 * Lock or unlock variable indicated by "lp".
3790 * "deep" is the levels to go (-1 for unlimited);
3791 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3792 */
3793 static int
3794do_lock_var(lp, name_end, deep, lock)
3795 lval_T *lp;
3796 char_u *name_end;
3797 int deep;
3798 int lock;
3799{
3800 int ret = OK;
3801 int cc;
3802 dictitem_T *di;
3803
3804 if (deep == 0) /* nothing to do */
3805 return OK;
3806
3807 if (lp->ll_tv == NULL)
3808 {
3809 cc = *name_end;
3810 *name_end = NUL;
3811
3812 /* Normal name or expanded name. */
3813 if (check_changedtick(lp->ll_name))
3814 ret = FAIL;
3815 else
3816 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003817 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818 if (di == NULL)
3819 ret = FAIL;
3820 else
3821 {
3822 if (lock)
3823 di->di_flags |= DI_FLAGS_LOCK;
3824 else
3825 di->di_flags &= ~DI_FLAGS_LOCK;
3826 item_lock(&di->di_tv, deep, lock);
3827 }
3828 }
3829 *name_end = cc;
3830 }
3831 else if (lp->ll_range)
3832 {
3833 listitem_T *li = lp->ll_li;
3834
3835 /* (un)lock a range of List items. */
3836 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3837 {
3838 item_lock(&li->li_tv, deep, lock);
3839 li = li->li_next;
3840 ++lp->ll_n1;
3841 }
3842 }
3843 else if (lp->ll_list != NULL)
3844 /* (un)lock a List item. */
3845 item_lock(&lp->ll_li->li_tv, deep, lock);
3846 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003847 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003848 item_lock(&lp->ll_di->di_tv, deep, lock);
3849
3850 return ret;
3851}
3852
3853/*
3854 * Lock or unlock an item. "deep" is nr of levels to go.
3855 */
3856 static void
3857item_lock(tv, deep, lock)
3858 typval_T *tv;
3859 int deep;
3860 int lock;
3861{
3862 static int recurse = 0;
3863 list_T *l;
3864 listitem_T *li;
3865 dict_T *d;
3866 hashitem_T *hi;
3867 int todo;
3868
3869 if (recurse >= DICT_MAXNEST)
3870 {
3871 EMSG(_("E743: variable nested too deep for (un)lock"));
3872 return;
3873 }
3874 if (deep == 0)
3875 return;
3876 ++recurse;
3877
3878 /* lock/unlock the item itself */
3879 if (lock)
3880 tv->v_lock |= VAR_LOCKED;
3881 else
3882 tv->v_lock &= ~VAR_LOCKED;
3883
3884 switch (tv->v_type)
3885 {
3886 case VAR_LIST:
3887 if ((l = tv->vval.v_list) != NULL)
3888 {
3889 if (lock)
3890 l->lv_lock |= VAR_LOCKED;
3891 else
3892 l->lv_lock &= ~VAR_LOCKED;
3893 if (deep < 0 || deep > 1)
3894 /* recursive: lock/unlock the items the List contains */
3895 for (li = l->lv_first; li != NULL; li = li->li_next)
3896 item_lock(&li->li_tv, deep - 1, lock);
3897 }
3898 break;
3899 case VAR_DICT:
3900 if ((d = tv->vval.v_dict) != NULL)
3901 {
3902 if (lock)
3903 d->dv_lock |= VAR_LOCKED;
3904 else
3905 d->dv_lock &= ~VAR_LOCKED;
3906 if (deep < 0 || deep > 1)
3907 {
3908 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003909 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003910 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3911 {
3912 if (!HASHITEM_EMPTY(hi))
3913 {
3914 --todo;
3915 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3916 }
3917 }
3918 }
3919 }
3920 }
3921 --recurse;
3922}
3923
Bram Moolenaara40058a2005-07-11 22:42:07 +00003924/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003925 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3926 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003927 */
3928 static int
3929tv_islocked(tv)
3930 typval_T *tv;
3931{
3932 return (tv->v_lock & VAR_LOCKED)
3933 || (tv->v_type == VAR_LIST
3934 && tv->vval.v_list != NULL
3935 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3936 || (tv->v_type == VAR_DICT
3937 && tv->vval.v_dict != NULL
3938 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3939}
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3942/*
3943 * Delete all "menutrans_" variables.
3944 */
3945 void
3946del_menutrans_vars()
3947{
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003952 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 {
3955 if (!HASHITEM_EMPTY(hi))
3956 {
3957 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003958 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3959 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003960 }
3961 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963}
3964#endif
3965
3966#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3967
3968/*
3969 * Local string buffer for the next two functions to store a variable name
3970 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3971 * get_user_var_name().
3972 */
3973
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003974static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976static char_u *varnamebuf = NULL;
3977static int varnamebuflen = 0;
3978
3979/*
3980 * Function to concatenate a prefix and a variable name.
3981 */
3982 static char_u *
3983cat_prefix_varname(prefix, name)
3984 int prefix;
3985 char_u *name;
3986{
3987 int len;
3988
3989 len = (int)STRLEN(name) + 3;
3990 if (len > varnamebuflen)
3991 {
3992 vim_free(varnamebuf);
3993 len += 10; /* some additional space */
3994 varnamebuf = alloc(len);
3995 if (varnamebuf == NULL)
3996 {
3997 varnamebuflen = 0;
3998 return NULL;
3999 }
4000 varnamebuflen = len;
4001 }
4002 *varnamebuf = prefix;
4003 varnamebuf[1] = ':';
4004 STRCPY(varnamebuf + 2, name);
4005 return varnamebuf;
4006}
4007
4008/*
4009 * Function given to ExpandGeneric() to obtain the list of user defined
4010 * (global/buffer/window/built-in) variable names.
4011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 char_u *
4013get_user_var_name(xp, idx)
4014 expand_T *xp;
4015 int idx;
4016{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004017 static long_u gdone;
4018 static long_u bdone;
4019 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_WINDOWS
4021 static long_u tdone;
4022#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004023 static int vidx;
4024 static hashitem_T *hi;
4025 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004028 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004030#ifdef FEAT_WINDOWS
4031 tdone = 0;
4032#endif
4033 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004034
4035 /* Global variables */
4036 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004038 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004039 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004040 else
4041 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 while (HASHITEM_EMPTY(hi))
4043 ++hi;
4044 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4045 return cat_prefix_varname('g', hi->hi_key);
4046 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048
4049 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004050 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004054 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004055 else
4056 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004057 while (HASHITEM_EMPTY(hi))
4058 ++hi;
4059 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004063 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 return (char_u *)"b:changedtick";
4065 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004066
4067 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004068 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004071 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004073 else
4074 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004075 while (HASHITEM_EMPTY(hi))
4076 ++hi;
4077 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004079
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004080#ifdef FEAT_WINDOWS
4081 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004082 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004083 if (tdone < ht->ht_used)
4084 {
4085 if (tdone++ == 0)
4086 hi = ht->ht_array;
4087 else
4088 ++hi;
4089 while (HASHITEM_EMPTY(hi))
4090 ++hi;
4091 return cat_prefix_varname('t', hi->hi_key);
4092 }
4093#endif
4094
Bram Moolenaar33570922005-01-25 22:26:29 +00004095 /* v: variables */
4096 if (vidx < VV_LEN)
4097 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
4099 vim_free(varnamebuf);
4100 varnamebuf = NULL;
4101 varnamebuflen = 0;
4102 return NULL;
4103}
4104
4105#endif /* FEAT_CMDL_COMPL */
4106
4107/*
4108 * types for expressions.
4109 */
4110typedef enum
4111{
4112 TYPE_UNKNOWN = 0
4113 , TYPE_EQUAL /* == */
4114 , TYPE_NEQUAL /* != */
4115 , TYPE_GREATER /* > */
4116 , TYPE_GEQUAL /* >= */
4117 , TYPE_SMALLER /* < */
4118 , TYPE_SEQUAL /* <= */
4119 , TYPE_MATCH /* =~ */
4120 , TYPE_NOMATCH /* !~ */
4121} exptype_T;
4122
4123/*
4124 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4127 */
4128
4129/*
4130 * Handle zero level expression.
4131 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004133 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 * Return OK or FAIL.
4135 */
4136 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 char_u **nextcmd;
4141 int evaluate;
4142{
4143 int ret;
4144 char_u *p;
4145
4146 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 if (ret == FAIL || !ends_excmd(*p))
4149 {
4150 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 /*
4153 * Report the invalid expression unless the expression evaluation has
4154 * been cancelled due to an aborting error, an interrupt, or an
4155 * exception.
4156 */
4157 if (!aborting())
4158 EMSG2(_(e_invexpr2), arg);
4159 ret = FAIL;
4160 }
4161 if (nextcmd != NULL)
4162 *nextcmd = check_nextcmd(p);
4163
4164 return ret;
4165}
4166
4167/*
4168 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004169 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 *
4171 * "arg" must point to the first non-white of the expression.
4172 * "arg" is advanced to the next non-white after the recognized expression.
4173 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004174 * Note: "rettv.v_lock" is not set.
4175 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 * Return OK or FAIL.
4177 */
4178 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 int evaluate;
4183{
4184 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004185 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
4187 /*
4188 * Get the first variable.
4189 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192
4193 if ((*arg)[0] == '?')
4194 {
4195 result = FALSE;
4196 if (evaluate)
4197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 int error = FALSE;
4199
4200 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004203 if (error)
4204 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206
4207 /*
4208 * Get the second variable.
4209 */
4210 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213
4214 /*
4215 * Check for the ":".
4216 */
4217 if ((*arg)[0] != ':')
4218 {
4219 EMSG(_("E109: Missing ':' after '?'"));
4220 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 return FAIL;
4223 }
4224
4225 /*
4226 * Get the third variable.
4227 */
4228 *arg = skipwhite(*arg + 1);
4229 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4230 {
4231 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return FAIL;
4234 }
4235 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 }
4238
4239 return OK;
4240}
4241
4242/*
4243 * Handle first level expression:
4244 * expr2 || expr2 || expr2 logical OR
4245 *
4246 * "arg" must point to the first non-white of the expression.
4247 * "arg" is advanced to the next non-white after the recognized expression.
4248 *
4249 * Return OK or FAIL.
4250 */
4251 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 int evaluate;
4256{
Bram Moolenaar33570922005-01-25 22:26:29 +00004257 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 long result;
4259 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
4262 /*
4263 * Get the first variable.
4264 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 return FAIL;
4267
4268 /*
4269 * Repeat until there is no following "||".
4270 */
4271 first = TRUE;
4272 result = FALSE;
4273 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4274 {
4275 if (evaluate && first)
4276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (error)
4281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 first = FALSE;
4283 }
4284
4285 /*
4286 * Get the second variable.
4287 */
4288 *arg = skipwhite(*arg + 2);
4289 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4290 return FAIL;
4291
4292 /*
4293 * Compute the result.
4294 */
4295 if (evaluate && !result)
4296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004300 if (error)
4301 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 if (evaluate)
4304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305 rettv->v_type = VAR_NUMBER;
4306 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308 }
4309
4310 return OK;
4311}
4312
4313/*
4314 * Handle second level expression:
4315 * expr3 && expr3 && expr3 logical AND
4316 *
4317 * "arg" must point to the first non-white of the expression.
4318 * "arg" is advanced to the next non-white after the recognized expression.
4319 *
4320 * Return OK or FAIL.
4321 */
4322 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 int evaluate;
4327{
Bram Moolenaar33570922005-01-25 22:26:29 +00004328 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 long result;
4330 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332
4333 /*
4334 * Get the first variable.
4335 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 return FAIL;
4338
4339 /*
4340 * Repeat until there is no following "&&".
4341 */
4342 first = TRUE;
4343 result = TRUE;
4344 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4345 {
4346 if (evaluate && first)
4347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (error)
4352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 first = FALSE;
4354 }
4355
4356 /*
4357 * Get the second variable.
4358 */
4359 *arg = skipwhite(*arg + 2);
4360 if (eval4(arg, &var2, evaluate && result) == FAIL)
4361 return FAIL;
4362
4363 /*
4364 * Compute the result.
4365 */
4366 if (evaluate && result)
4367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004368 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004371 if (error)
4372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374 if (evaluate)
4375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004376 rettv->v_type = VAR_NUMBER;
4377 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379 }
4380
4381 return OK;
4382}
4383
4384/*
4385 * Handle third level expression:
4386 * var1 == var2
4387 * var1 =~ var2
4388 * var1 != var2
4389 * var1 !~ var2
4390 * var1 > var2
4391 * var1 >= var2
4392 * var1 < var2
4393 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004394 * var1 is var2
4395 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 *
4397 * "arg" must point to the first non-white of the expression.
4398 * "arg" is advanced to the next non-white after the recognized expression.
4399 *
4400 * Return OK or FAIL.
4401 */
4402 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 int evaluate;
4407{
Bram Moolenaar33570922005-01-25 22:26:29 +00004408 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 char_u *p;
4410 int i;
4411 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004412 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 int len = 2;
4414 long n1, n2;
4415 char_u *s1, *s2;
4416 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4417 regmatch_T regmatch;
4418 int ic;
4419 char_u *save_cpo;
4420
4421 /*
4422 * Get the first variable.
4423 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 return FAIL;
4426
4427 p = *arg;
4428 switch (p[0])
4429 {
4430 case '=': if (p[1] == '=')
4431 type = TYPE_EQUAL;
4432 else if (p[1] == '~')
4433 type = TYPE_MATCH;
4434 break;
4435 case '!': if (p[1] == '=')
4436 type = TYPE_NEQUAL;
4437 else if (p[1] == '~')
4438 type = TYPE_NOMATCH;
4439 break;
4440 case '>': if (p[1] != '=')
4441 {
4442 type = TYPE_GREATER;
4443 len = 1;
4444 }
4445 else
4446 type = TYPE_GEQUAL;
4447 break;
4448 case '<': if (p[1] != '=')
4449 {
4450 type = TYPE_SMALLER;
4451 len = 1;
4452 }
4453 else
4454 type = TYPE_SEQUAL;
4455 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004456 case 'i': if (p[1] == 's')
4457 {
4458 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4459 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004460 i = p[len];
4461 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 {
4463 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4464 type_is = TRUE;
4465 }
4466 }
4467 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469
4470 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 */
4473 if (type != TYPE_UNKNOWN)
4474 {
4475 /* extra question mark appended: ignore case */
4476 if (p[len] == '?')
4477 {
4478 ic = TRUE;
4479 ++len;
4480 }
4481 /* extra '#' appended: match case */
4482 else if (p[len] == '#')
4483 {
4484 ic = FALSE;
4485 ++len;
4486 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004487 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 else
4489 ic = p_ic;
4490
4491 /*
4492 * Get the second variable.
4493 */
4494 *arg = skipwhite(p + len);
4495 if (eval5(arg, &var2, evaluate) == FAIL)
4496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004497 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 return FAIL;
4499 }
4500
4501 if (evaluate)
4502 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004503 if (type_is && rettv->v_type != var2.v_type)
4504 {
4505 /* For "is" a different type always means FALSE, for "notis"
4506 * it means TRUE. */
4507 n1 = (type == TYPE_NEQUAL);
4508 }
4509 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4510 {
4511 if (type_is)
4512 {
4513 n1 = (rettv->v_type == var2.v_type
4514 && rettv->vval.v_list == var2.vval.v_list);
4515 if (type == TYPE_NEQUAL)
4516 n1 = !n1;
4517 }
4518 else if (rettv->v_type != var2.v_type
4519 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4520 {
4521 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004522 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004523 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004524 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004525 clear_tv(rettv);
4526 clear_tv(&var2);
4527 return FAIL;
4528 }
4529 else
4530 {
4531 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004532 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4533 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 if (type == TYPE_NEQUAL)
4535 n1 = !n1;
4536 }
4537 }
4538
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004539 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4540 {
4541 if (type_is)
4542 {
4543 n1 = (rettv->v_type == var2.v_type
4544 && rettv->vval.v_dict == var2.vval.v_dict);
4545 if (type == TYPE_NEQUAL)
4546 n1 = !n1;
4547 }
4548 else if (rettv->v_type != var2.v_type
4549 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4550 {
4551 if (rettv->v_type != var2.v_type)
4552 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4553 else
4554 EMSG(_("E736: Invalid operation for Dictionary"));
4555 clear_tv(rettv);
4556 clear_tv(&var2);
4557 return FAIL;
4558 }
4559 else
4560 {
4561 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004562 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4563 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004564 if (type == TYPE_NEQUAL)
4565 n1 = !n1;
4566 }
4567 }
4568
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004569 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4570 {
4571 if (rettv->v_type != var2.v_type
4572 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4573 {
4574 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004575 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004576 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004577 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004578 clear_tv(rettv);
4579 clear_tv(&var2);
4580 return FAIL;
4581 }
4582 else
4583 {
4584 /* Compare two Funcrefs for being equal or unequal. */
4585 if (rettv->vval.v_string == NULL
4586 || var2.vval.v_string == NULL)
4587 n1 = FALSE;
4588 else
4589 n1 = STRCMP(rettv->vval.v_string,
4590 var2.vval.v_string) == 0;
4591 if (type == TYPE_NEQUAL)
4592 n1 = !n1;
4593 }
4594 }
4595
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004596#ifdef FEAT_FLOAT
4597 /*
4598 * If one of the two variables is a float, compare as a float.
4599 * When using "=~" or "!~", always compare as string.
4600 */
4601 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4602 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4603 {
4604 float_T f1, f2;
4605
4606 if (rettv->v_type == VAR_FLOAT)
4607 f1 = rettv->vval.v_float;
4608 else
4609 f1 = get_tv_number(rettv);
4610 if (var2.v_type == VAR_FLOAT)
4611 f2 = var2.vval.v_float;
4612 else
4613 f2 = get_tv_number(&var2);
4614 n1 = FALSE;
4615 switch (type)
4616 {
4617 case TYPE_EQUAL: n1 = (f1 == f2); break;
4618 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4619 case TYPE_GREATER: n1 = (f1 > f2); break;
4620 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4621 case TYPE_SMALLER: n1 = (f1 < f2); break;
4622 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4623 case TYPE_UNKNOWN:
4624 case TYPE_MATCH:
4625 case TYPE_NOMATCH: break; /* avoid gcc warning */
4626 }
4627 }
4628#endif
4629
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 /*
4631 * If one of the two variables is a number, compare as a number.
4632 * When using "=~" or "!~", always compare as string.
4633 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004634 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 n1 = get_tv_number(rettv);
4638 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 switch (type)
4640 {
4641 case TYPE_EQUAL: n1 = (n1 == n2); break;
4642 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4643 case TYPE_GREATER: n1 = (n1 > n2); break;
4644 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4645 case TYPE_SMALLER: n1 = (n1 < n2); break;
4646 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4647 case TYPE_UNKNOWN:
4648 case TYPE_MATCH:
4649 case TYPE_NOMATCH: break; /* avoid gcc warning */
4650 }
4651 }
4652 else
4653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004654 s1 = get_tv_string_buf(rettv, buf1);
4655 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4657 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4658 else
4659 i = 0;
4660 n1 = FALSE;
4661 switch (type)
4662 {
4663 case TYPE_EQUAL: n1 = (i == 0); break;
4664 case TYPE_NEQUAL: n1 = (i != 0); break;
4665 case TYPE_GREATER: n1 = (i > 0); break;
4666 case TYPE_GEQUAL: n1 = (i >= 0); break;
4667 case TYPE_SMALLER: n1 = (i < 0); break;
4668 case TYPE_SEQUAL: n1 = (i <= 0); break;
4669
4670 case TYPE_MATCH:
4671 case TYPE_NOMATCH:
4672 /* avoid 'l' flag in 'cpoptions' */
4673 save_cpo = p_cpo;
4674 p_cpo = (char_u *)"";
4675 regmatch.regprog = vim_regcomp(s2,
4676 RE_MAGIC + RE_STRING);
4677 regmatch.rm_ic = ic;
4678 if (regmatch.regprog != NULL)
4679 {
4680 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004681 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 if (type == TYPE_NOMATCH)
4683 n1 = !n1;
4684 }
4685 p_cpo = save_cpo;
4686 break;
4687
4688 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4689 }
4690 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 clear_tv(rettv);
4692 clear_tv(&var2);
4693 rettv->v_type = VAR_NUMBER;
4694 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696 }
4697
4698 return OK;
4699}
4700
4701/*
4702 * Handle fourth level expression:
4703 * + number addition
4704 * - number subtraction
4705 * . string concatenation
4706 *
4707 * "arg" must point to the first non-white of the expression.
4708 * "arg" is advanced to the next non-white after the recognized expression.
4709 *
4710 * Return OK or FAIL.
4711 */
4712 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004713eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 int evaluate;
4717{
Bram Moolenaar33570922005-01-25 22:26:29 +00004718 typval_T var2;
4719 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 int op;
4721 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722#ifdef FEAT_FLOAT
4723 float_T f1 = 0, f2 = 0;
4724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 char_u *s1, *s2;
4726 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4727 char_u *p;
4728
4729 /*
4730 * Get the first variable.
4731 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004732 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 return FAIL;
4734
4735 /*
4736 * Repeat computing, until no '+', '-' or '.' is following.
4737 */
4738 for (;;)
4739 {
4740 op = **arg;
4741 if (op != '+' && op != '-' && op != '.')
4742 break;
4743
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004744 if ((op != '+' || rettv->v_type != VAR_LIST)
4745#ifdef FEAT_FLOAT
4746 && (op == '.' || rettv->v_type != VAR_FLOAT)
4747#endif
4748 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004749 {
4750 /* For "list + ...", an illegal use of the first operand as
4751 * a number cannot be determined before evaluating the 2nd
4752 * operand: if this is also a list, all is ok.
4753 * For "something . ...", "something - ..." or "non-list + ...",
4754 * we know that the first operand needs to be a string or number
4755 * without evaluating the 2nd operand. So check before to avoid
4756 * side effects after an error. */
4757 if (evaluate && get_tv_string_chk(rettv) == NULL)
4758 {
4759 clear_tv(rettv);
4760 return FAIL;
4761 }
4762 }
4763
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 /*
4765 * Get the second variable.
4766 */
4767 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004768 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 return FAIL;
4772 }
4773
4774 if (evaluate)
4775 {
4776 /*
4777 * Compute the result.
4778 */
4779 if (op == '.')
4780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4782 s2 = get_tv_string_buf_chk(&var2, buf2);
4783 if (s2 == NULL) /* type error ? */
4784 {
4785 clear_tv(rettv);
4786 clear_tv(&var2);
4787 return FAIL;
4788 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004789 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 clear_tv(rettv);
4791 rettv->v_type = VAR_STRING;
4792 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004794 else if (op == '+' && rettv->v_type == VAR_LIST
4795 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004796 {
4797 /* concatenate Lists */
4798 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4799 &var3) == FAIL)
4800 {
4801 clear_tv(rettv);
4802 clear_tv(&var2);
4803 return FAIL;
4804 }
4805 clear_tv(rettv);
4806 *rettv = var3;
4807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 else
4809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 int error = FALSE;
4811
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004812#ifdef FEAT_FLOAT
4813 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004814 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004815 f1 = rettv->vval.v_float;
4816 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004817 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004818 else
4819#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004820 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004821 n1 = get_tv_number_chk(rettv, &error);
4822 if (error)
4823 {
4824 /* This can only happen for "list + non-list". For
4825 * "non-list + ..." or "something - ...", we returned
4826 * before evaluating the 2nd operand. */
4827 clear_tv(rettv);
4828 return FAIL;
4829 }
4830#ifdef FEAT_FLOAT
4831 if (var2.v_type == VAR_FLOAT)
4832 f1 = n1;
4833#endif
4834 }
4835#ifdef FEAT_FLOAT
4836 if (var2.v_type == VAR_FLOAT)
4837 {
4838 f2 = var2.vval.v_float;
4839 n2 = 0;
4840 }
4841 else
4842#endif
4843 {
4844 n2 = get_tv_number_chk(&var2, &error);
4845 if (error)
4846 {
4847 clear_tv(rettv);
4848 clear_tv(&var2);
4849 return FAIL;
4850 }
4851#ifdef FEAT_FLOAT
4852 if (rettv->v_type == VAR_FLOAT)
4853 f2 = n2;
4854#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004855 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857
4858#ifdef FEAT_FLOAT
4859 /* If there is a float on either side the result is a float. */
4860 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4861 {
4862 if (op == '+')
4863 f1 = f1 + f2;
4864 else
4865 f1 = f1 - f2;
4866 rettv->v_type = VAR_FLOAT;
4867 rettv->vval.v_float = f1;
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004870#endif
4871 {
4872 if (op == '+')
4873 n1 = n1 + n2;
4874 else
4875 n1 = n1 - n2;
4876 rettv->v_type = VAR_NUMBER;
4877 rettv->vval.v_number = n1;
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 }
4883 return OK;
4884}
4885
4886/*
4887 * Handle fifth level expression:
4888 * * number multiplication
4889 * / number division
4890 * % number modulo
4891 *
4892 * "arg" must point to the first non-white of the expression.
4893 * "arg" is advanced to the next non-white after the recognized expression.
4894 *
4895 * Return OK or FAIL.
4896 */
4897 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004898eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004902 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903{
Bram Moolenaar33570922005-01-25 22:26:29 +00004904 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 int op;
4906 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907#ifdef FEAT_FLOAT
4908 int use_float = FALSE;
4909 float_T f1 = 0, f2;
4910#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004911 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
4913 /*
4914 * Get the first variable.
4915 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004916 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 return FAIL;
4918
4919 /*
4920 * Repeat computing, until no '*', '/' or '%' is following.
4921 */
4922 for (;;)
4923 {
4924 op = **arg;
4925 if (op != '*' && op != '/' && op != '%')
4926 break;
4927
4928 if (evaluate)
4929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930#ifdef FEAT_FLOAT
4931 if (rettv->v_type == VAR_FLOAT)
4932 {
4933 f1 = rettv->vval.v_float;
4934 use_float = TRUE;
4935 n1 = 0;
4936 }
4937 else
4938#endif
4939 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004940 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004941 if (error)
4942 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944 else
4945 n1 = 0;
4946
4947 /*
4948 * Get the second variable.
4949 */
4950 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004951 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 return FAIL;
4953
4954 if (evaluate)
4955 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956#ifdef FEAT_FLOAT
4957 if (var2.v_type == VAR_FLOAT)
4958 {
4959 if (!use_float)
4960 {
4961 f1 = n1;
4962 use_float = TRUE;
4963 }
4964 f2 = var2.vval.v_float;
4965 n2 = 0;
4966 }
4967 else
4968#endif
4969 {
4970 n2 = get_tv_number_chk(&var2, &error);
4971 clear_tv(&var2);
4972 if (error)
4973 return FAIL;
4974#ifdef FEAT_FLOAT
4975 if (use_float)
4976 f2 = n2;
4977#endif
4978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
4980 /*
4981 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984#ifdef FEAT_FLOAT
4985 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 if (op == '*')
4988 f1 = f1 * f2;
4989 else if (op == '/')
4990 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004991# ifdef VMS
4992 /* VMS crashes on divide by zero, work around it */
4993 if (f2 == 0.0)
4994 {
4995 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004996 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004997 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004998 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004999 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005000 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005001 }
5002 else
5003 f1 = f1 / f2;
5004# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005005 /* We rely on the floating point library to handle divide
5006 * by zero to result in "inf" and not a crash. */
5007 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005008# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005012 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005013 return FAIL;
5014 }
5015 rettv->v_type = VAR_FLOAT;
5016 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005021 if (op == '*')
5022 n1 = n1 * n2;
5023 else if (op == '/')
5024 {
5025 if (n2 == 0) /* give an error message? */
5026 {
5027 if (n1 == 0)
5028 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5029 else if (n1 < 0)
5030 n1 = -0x7fffffffL;
5031 else
5032 n1 = 0x7fffffffL;
5033 }
5034 else
5035 n1 = n1 / n2;
5036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005038 {
5039 if (n2 == 0) /* give an error message? */
5040 n1 = 0;
5041 else
5042 n1 = n1 % n2;
5043 }
5044 rettv->v_type = VAR_NUMBER;
5045 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
5048 }
5049
5050 return OK;
5051}
5052
5053/*
5054 * Handle sixth level expression:
5055 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005056 * "string" string constant
5057 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 * &option-name option value
5059 * @r register contents
5060 * identifier variable value
5061 * function() function call
5062 * $VAR environment variable
5063 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005064 * [expr, expr] List
5065 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 *
5067 * Also handle:
5068 * ! in front logical NOT
5069 * - in front unary minus
5070 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 * trailing [] subscript in String or List
5072 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 *
5074 * "arg" must point to the first non-white of the expression.
5075 * "arg" is advanced to the next non-white after the recognized expression.
5076 *
5077 * Return OK or FAIL.
5078 */
5079 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005080eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005084 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 long n;
5087 int len;
5088 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 char_u *start_leader, *end_leader;
5090 int ret = OK;
5091 char_u *alias;
5092
5093 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005095 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098
5099 /*
5100 * Skip '!' and '-' characters. They are handled later.
5101 */
5102 start_leader = *arg;
5103 while (**arg == '!' || **arg == '-' || **arg == '+')
5104 *arg = skipwhite(*arg + 1);
5105 end_leader = *arg;
5106
5107 switch (**arg)
5108 {
5109 /*
5110 * Number constant.
5111 */
5112 case '0':
5113 case '1':
5114 case '2':
5115 case '3':
5116 case '4':
5117 case '5':
5118 case '6':
5119 case '7':
5120 case '8':
5121 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005122 {
5123#ifdef FEAT_FLOAT
5124 char_u *p = skipdigits(*arg + 1);
5125 int get_float = FALSE;
5126
5127 /* We accept a float when the format matches
5128 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005129 * strict to avoid backwards compatibility problems.
5130 * Don't look for a float after the "." operator, so that
5131 * ":let vers = 1.2.3" doesn't fail. */
5132 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005134 get_float = TRUE;
5135 p = skipdigits(p + 2);
5136 if (*p == 'e' || *p == 'E')
5137 {
5138 ++p;
5139 if (*p == '-' || *p == '+')
5140 ++p;
5141 if (!vim_isdigit(*p))
5142 get_float = FALSE;
5143 else
5144 p = skipdigits(p + 1);
5145 }
5146 if (ASCII_ISALPHA(*p) || *p == '.')
5147 get_float = FALSE;
5148 }
5149 if (get_float)
5150 {
5151 float_T f;
5152
5153 *arg += string2float(*arg, &f);
5154 if (evaluate)
5155 {
5156 rettv->v_type = VAR_FLOAT;
5157 rettv->vval.v_float = f;
5158 }
5159 }
5160 else
5161#endif
5162 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005163 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005164 *arg += len;
5165 if (evaluate)
5166 {
5167 rettv->v_type = VAR_NUMBER;
5168 rettv->vval.v_number = n;
5169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
5174 /*
5175 * String constant: "string".
5176 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 break;
5179
5180 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005184 break;
5185
5186 /*
5187 * List: [expr, expr]
5188 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 break;
5191
5192 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 * Dictionary: {key: val, key: val}
5194 */
5195 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5196 break;
5197
5198 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005199 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005201 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 break;
5203
5204 /*
5205 * Environment variable: $VAR.
5206 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 break;
5209
5210 /*
5211 * Register contents: @r.
5212 */
5213 case '@': ++*arg;
5214 if (evaluate)
5215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005216 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005217 rettv->vval.v_string = get_reg_contents(**arg,
5218 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
5220 if (**arg != NUL)
5221 ++*arg;
5222 break;
5223
5224 /*
5225 * nested expression: (expression).
5226 */
5227 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005228 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 if (**arg == ')')
5230 ++*arg;
5231 else if (ret == OK)
5232 {
5233 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005234 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 ret = FAIL;
5236 }
5237 break;
5238
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 break;
5241 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005242
5243 if (ret == NOTDONE)
5244 {
5245 /*
5246 * Must be a variable or function name.
5247 * Can also be a curly-braces kind of name: {expr}.
5248 */
5249 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005250 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 if (alias != NULL)
5252 s = alias;
5253
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005254 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255 ret = FAIL;
5256 else
5257 {
5258 if (**arg == '(') /* recursive! */
5259 {
5260 /* If "s" is the name of a variable of type VAR_FUNC
5261 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005262 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263
5264 /* Invoke the function. */
5265 ret = get_func_tv(s, len, rettv, arg,
5266 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005267 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005268
5269 /* If evaluate is FALSE rettv->v_type was not set in
5270 * get_func_tv, but it's needed in handle_subscript() to parse
5271 * what follows. So set it here. */
5272 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5273 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005274 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005275 rettv->v_type = VAR_FUNC;
5276 }
5277
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 /* Stop the expression evaluation when immediately
5279 * aborting on error, or when an interrupt occurred or
5280 * an exception was thrown but not caught. */
5281 if (aborting())
5282 {
5283 if (ret == OK)
5284 clear_tv(rettv);
5285 ret = FAIL;
5286 }
5287 }
5288 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005289 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005290 else
5291 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005293 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 }
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 *arg = skipwhite(*arg);
5297
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5299 * expr(expr). */
5300 if (ret == OK)
5301 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
5303 /*
5304 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5305 */
5306 if (ret == OK && evaluate && end_leader > start_leader)
5307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005309 int val = 0;
5310#ifdef FEAT_FLOAT
5311 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005312
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005313 if (rettv->v_type == VAR_FLOAT)
5314 f = rettv->vval.v_float;
5315 else
5316#endif
5317 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 clear_tv(rettv);
5321 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005323 else
5324 {
5325 while (end_leader > start_leader)
5326 {
5327 --end_leader;
5328 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005329 {
5330#ifdef FEAT_FLOAT
5331 if (rettv->v_type == VAR_FLOAT)
5332 f = !f;
5333 else
5334#endif
5335 val = !val;
5336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005337 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005338 {
5339#ifdef FEAT_FLOAT
5340 if (rettv->v_type == VAR_FLOAT)
5341 f = -f;
5342 else
5343#endif
5344 val = -val;
5345 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005347#ifdef FEAT_FLOAT
5348 if (rettv->v_type == VAR_FLOAT)
5349 {
5350 clear_tv(rettv);
5351 rettv->vval.v_float = f;
5352 }
5353 else
5354#endif
5355 {
5356 clear_tv(rettv);
5357 rettv->v_type = VAR_NUMBER;
5358 rettv->vval.v_number = val;
5359 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
5362
5363 return ret;
5364}
5365
5366/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005367 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5368 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5370 */
5371 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005372eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005374 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005376 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377{
5378 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005379 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005381 long len = -1;
5382 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005384 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005386 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005388 if (verbose)
5389 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 return FAIL;
5391 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005392#ifdef FEAT_FLOAT
5393 else if (rettv->v_type == VAR_FLOAT)
5394 {
5395 if (verbose)
5396 EMSG(_(e_float_as_string));
5397 return FAIL;
5398 }
5399#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005400 else if (rettv->v_type == VAR_SPECIAL)
5401 {
5402 return FAIL;
5403 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005405 init_tv(&var1);
5406 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005409 /*
5410 * dict.name
5411 */
5412 key = *arg + 1;
5413 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5414 ;
5415 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 }
5419 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005421 /*
5422 * something[idx]
5423 *
5424 * Get the (first) variable from inside the [].
5425 */
5426 *arg = skipwhite(*arg + 1);
5427 if (**arg == ':')
5428 empty1 = TRUE;
5429 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5430 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005431 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5432 {
5433 /* not a number or string */
5434 clear_tv(&var1);
5435 return FAIL;
5436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437
5438 /*
5439 * Get the second variable from inside the [:].
5440 */
5441 if (**arg == ':')
5442 {
5443 range = TRUE;
5444 *arg = skipwhite(*arg + 1);
5445 if (**arg == ']')
5446 empty2 = TRUE;
5447 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005449 if (!empty1)
5450 clear_tv(&var1);
5451 return FAIL;
5452 }
5453 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5454 {
5455 /* not a number or string */
5456 if (!empty1)
5457 clear_tv(&var1);
5458 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 return FAIL;
5460 }
5461 }
5462
5463 /* Check for the ']'. */
5464 if (**arg != ']')
5465 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005466 if (verbose)
5467 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005468 clear_tv(&var1);
5469 if (range)
5470 clear_tv(&var2);
5471 return FAIL;
5472 }
5473 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
5475
5476 if (evaluate)
5477 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 n1 = 0;
5479 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 n1 = get_tv_number(&var1);
5482 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 }
5484 if (range)
5485 {
5486 if (empty2)
5487 n2 = -1;
5488 else
5489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490 n2 = get_tv_number(&var2);
5491 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 }
5493 }
5494
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005495 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 {
5497 case VAR_NUMBER:
5498 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 len = (long)STRLEN(s);
5501 if (range)
5502 {
5503 /* The resulting variable is a substring. If the indexes
5504 * are out of range the result is empty. */
5505 if (n1 < 0)
5506 {
5507 n1 = len + n1;
5508 if (n1 < 0)
5509 n1 = 0;
5510 }
5511 if (n2 < 0)
5512 n2 = len + n2;
5513 else if (n2 >= len)
5514 n2 = len;
5515 if (n1 >= len || n2 < 0 || n1 > n2)
5516 s = NULL;
5517 else
5518 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5519 }
5520 else
5521 {
5522 /* The resulting variable is a string of a single
5523 * character. If the index is too big or negative the
5524 * result is empty. */
5525 if (n1 >= len || n1 < 0)
5526 s = NULL;
5527 else
5528 s = vim_strnsave(s + n1, 1);
5529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 clear_tv(rettv);
5531 rettv->v_type = VAR_STRING;
5532 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 break;
5534
5535 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005536 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 if (n1 < 0)
5538 n1 = len + n1;
5539 if (!empty1 && (n1 < 0 || n1 >= len))
5540 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005541 /* For a range we allow invalid values and return an empty
5542 * list. A list index out of range is an error. */
5543 if (!range)
5544 {
5545 if (verbose)
5546 EMSGN(_(e_listidx), n1);
5547 return FAIL;
5548 }
5549 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 }
5551 if (range)
5552 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005553 list_T *l;
5554 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555
5556 if (n2 < 0)
5557 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005558 else if (n2 >= len)
5559 n2 = len - 1;
5560 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005561 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 l = list_alloc();
5563 if (l == NULL)
5564 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 n1 <= n2; ++n1)
5567 {
5568 if (list_append_tv(l, &item->li_tv) == FAIL)
5569 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005570 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 return FAIL;
5572 }
5573 item = item->li_next;
5574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 clear_tv(rettv);
5576 rettv->v_type = VAR_LIST;
5577 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005578 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579 }
5580 else
5581 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005582 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 clear_tv(rettv);
5584 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 }
5586 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587
5588 case VAR_DICT:
5589 if (range)
5590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005591 if (verbose)
5592 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 if (len == -1)
5594 clear_tv(&var1);
5595 return FAIL;
5596 }
5597 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005598 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599
5600 if (len == -1)
5601 {
5602 key = get_tv_string(&var1);
5603 if (*key == NUL)
5604 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005605 if (verbose)
5606 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 clear_tv(&var1);
5608 return FAIL;
5609 }
5610 }
5611
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005612 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005613
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005614 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005615 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 if (len == -1)
5617 clear_tv(&var1);
5618 if (item == NULL)
5619 return FAIL;
5620
5621 copy_tv(&item->di_tv, &var1);
5622 clear_tv(rettv);
5623 *rettv = var1;
5624 }
5625 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 }
5627 }
5628
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005629 return OK;
5630}
5631
5632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 * Get an option value.
5634 * "arg" points to the '&' or '+' before the option name.
5635 * "arg" is advanced to character after the option name.
5636 * Return OK or FAIL.
5637 */
5638 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005641 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 int evaluate;
5643{
5644 char_u *option_end;
5645 long numval;
5646 char_u *stringval;
5647 int opt_type;
5648 int c;
5649 int working = (**arg == '+'); /* has("+option") */
5650 int ret = OK;
5651 int opt_flags;
5652
5653 /*
5654 * Isolate the option name and find its value.
5655 */
5656 option_end = find_option_end(arg, &opt_flags);
5657 if (option_end == NULL)
5658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 EMSG2(_("E112: Option name missing: %s"), *arg);
5661 return FAIL;
5662 }
5663
5664 if (!evaluate)
5665 {
5666 *arg = option_end;
5667 return OK;
5668 }
5669
5670 c = *option_end;
5671 *option_end = NUL;
5672 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
5675 if (opt_type == -3) /* invalid name */
5676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 EMSG2(_("E113: Unknown option: %s"), *arg);
5679 ret = FAIL;
5680 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 {
5683 if (opt_type == -2) /* hidden string option */
5684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005685 rettv->v_type = VAR_STRING;
5686 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 else if (opt_type == -1) /* hidden number option */
5689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 rettv->v_type = VAR_NUMBER;
5691 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693 else if (opt_type == 1) /* number option */
5694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005695 rettv->v_type = VAR_NUMBER;
5696 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
5698 else /* string option */
5699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700 rettv->v_type = VAR_STRING;
5701 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 }
5703 }
5704 else if (working && (opt_type == -2 || opt_type == -1))
5705 ret = FAIL;
5706
5707 *option_end = c; /* put back for error messages */
5708 *arg = option_end;
5709
5710 return ret;
5711}
5712
5713/*
5714 * Allocate a variable for a string constant.
5715 * Return OK or FAIL.
5716 */
5717 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005718get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 int evaluate;
5722{
5723 char_u *p;
5724 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 int extra = 0;
5726
5727 /*
5728 * Find the end of the string, skipping backslashed characters.
5729 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005730 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {
5732 if (*p == '\\' && p[1] != NUL)
5733 {
5734 ++p;
5735 /* A "\<x>" form occupies at least 4 characters, and produces up
5736 * to 6 characters: reserve space for 2 extra */
5737 if (*p == '<')
5738 extra += 2;
5739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 }
5741
5742 if (*p != '"')
5743 {
5744 EMSG2(_("E114: Missing quote: %s"), *arg);
5745 return FAIL;
5746 }
5747
5748 /* If only parsing, set *arg and return here */
5749 if (!evaluate)
5750 {
5751 *arg = p + 1;
5752 return OK;
5753 }
5754
5755 /*
5756 * Copy the string into allocated memory, handling backslashed
5757 * characters.
5758 */
5759 name = alloc((unsigned)(p - *arg + extra));
5760 if (name == NULL)
5761 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 rettv->v_type = VAR_STRING;
5763 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
5767 if (*p == '\\')
5768 {
5769 switch (*++p)
5770 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 case 'b': *name++ = BS; ++p; break;
5772 case 'e': *name++ = ESC; ++p; break;
5773 case 'f': *name++ = FF; ++p; break;
5774 case 'n': *name++ = NL; ++p; break;
5775 case 'r': *name++ = CAR; ++p; break;
5776 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
5778 case 'X': /* hex: "\x1", "\x12" */
5779 case 'x':
5780 case 'u': /* Unicode: "\u0023" */
5781 case 'U':
5782 if (vim_isxdigit(p[1]))
5783 {
5784 int n, nr;
5785 int c = toupper(*p);
5786
5787 if (c == 'X')
5788 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005789 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005791 else
5792 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 nr = 0;
5794 while (--n >= 0 && vim_isxdigit(p[1]))
5795 {
5796 ++p;
5797 nr = (nr << 4) + hex2nr(*p);
5798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800#ifdef FEAT_MBYTE
5801 /* For "\u" store the number according to
5802 * 'encoding'. */
5803 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 else
5806#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 break;
5810
5811 /* octal: "\1", "\12", "\123" */
5812 case '0':
5813 case '1':
5814 case '2':
5815 case '3':
5816 case '4':
5817 case '5':
5818 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 case '7': *name = *p++ - '0';
5820 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 *name = (*name << 3) + *p++ - '0';
5823 if (*p >= '0' && *p <= '7')
5824 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 break;
5828
5829 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 if (extra != 0)
5832 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 break;
5835 }
5836 /* FALLTHROUGH */
5837
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 break;
5840 }
5841 }
5842 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 *arg = p + 1;
5848
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 return OK;
5850}
5851
5852/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 * Return OK or FAIL.
5855 */
5856 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005857get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 int evaluate;
5861{
5862 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 int reduce = 0;
5865
5866 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 */
5869 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 break;
5875 ++reduce;
5876 ++p;
5877 }
5878 }
5879
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 return FAIL;
5884 }
5885
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 if (!evaluate)
5888 {
5889 *arg = p + 1;
5890 return OK;
5891 }
5892
5893 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 */
5896 str = alloc((unsigned)((p - *arg) - reduce));
5897 if (str == NULL)
5898 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 rettv->v_type = VAR_STRING;
5900 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901
Bram Moolenaar8c711452005-01-14 21:53:12 +00005902 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005905 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 break;
5908 ++p;
5909 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 *arg = p + 1;
5914
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005915 return OK;
5916}
5917
5918/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 * Allocate a variable for a List and fill it from "*arg".
5920 * Return OK or FAIL.
5921 */
5922 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005923get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 int evaluate;
5927{
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 list_T *l = NULL;
5929 typval_T tv;
5930 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931
5932 if (evaluate)
5933 {
5934 l = list_alloc();
5935 if (l == NULL)
5936 return FAIL;
5937 }
5938
5939 *arg = skipwhite(*arg + 1);
5940 while (**arg != ']' && **arg != NUL)
5941 {
5942 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5943 goto failret;
5944 if (evaluate)
5945 {
5946 item = listitem_alloc();
5947 if (item != NULL)
5948 {
5949 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005950 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 list_append(l, item);
5952 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005953 else
5954 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 }
5956
5957 if (**arg == ']')
5958 break;
5959 if (**arg != ',')
5960 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005961 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 goto failret;
5963 }
5964 *arg = skipwhite(*arg + 1);
5965 }
5966
5967 if (**arg != ']')
5968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005969 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970failret:
5971 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005972 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 return FAIL;
5974 }
5975
5976 *arg = skipwhite(*arg + 1);
5977 if (evaluate)
5978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 rettv->v_type = VAR_LIST;
5980 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 ++l->lv_refcount;
5982 }
5983
5984 return OK;
5985}
5986
5987/*
5988 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005989 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005991 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992list_alloc()
5993{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005994 list_T *l;
5995
5996 l = (list_T *)alloc_clear(sizeof(list_T));
5997 if (l != NULL)
5998 {
5999 /* Prepend the list to the list of lists for garbage collection. */
6000 if (first_list != NULL)
6001 first_list->lv_used_prev = l;
6002 l->lv_used_prev = NULL;
6003 l->lv_used_next = first_list;
6004 first_list = l;
6005 }
6006 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007}
6008
6009/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006010 * Allocate an empty list for a return value.
6011 * Returns OK or FAIL.
6012 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006013 int
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006014rettv_list_alloc(rettv)
6015 typval_T *rettv;
6016{
6017 list_T *l = list_alloc();
6018
6019 if (l == NULL)
6020 return FAIL;
6021
6022 rettv->vval.v_list = l;
6023 rettv->v_type = VAR_LIST;
6024 ++l->lv_refcount;
6025 return OK;
6026}
6027
6028/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029 * Unreference a list: decrement the reference count and free it when it
6030 * becomes zero.
6031 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006032 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006036 if (l != NULL && --l->lv_refcount <= 0)
6037 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038}
6039
6040/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006041 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 * Ignores the reference count.
6043 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006044 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006045list_free(l, recurse)
6046 list_T *l;
6047 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048{
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006051 /* Remove the list from the list of lists for garbage collection. */
6052 if (l->lv_used_prev == NULL)
6053 first_list = l->lv_used_next;
6054 else
6055 l->lv_used_prev->lv_used_next = l->lv_used_next;
6056 if (l->lv_used_next != NULL)
6057 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6058
Bram Moolenaard9fba312005-06-26 22:34:35 +00006059 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006061 /* Remove the item before deleting it. */
6062 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006063 if (recurse || (item->li_tv.v_type != VAR_LIST
6064 && item->li_tv.v_type != VAR_DICT))
6065 clear_tv(&item->li_tv);
6066 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067 }
6068 vim_free(l);
6069}
6070
6071/*
6072 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006073 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006075 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076listitem_alloc()
6077{
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079}
6080
6081/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006082 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006084 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006088 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089 vim_free(item);
6090}
6091
6092/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006093 * Remove a list item from a List and free it. Also clears the value.
6094 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006095 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006096listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 list_T *l;
6098 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006099{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006100 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006101 listitem_free(item);
6102}
6103
6104/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006105 * Get the number of items in a list.
6106 */
6107 static long
6108list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006109 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006110{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111 if (l == NULL)
6112 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006113 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114}
6115
6116/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 * Return TRUE when two lists have exactly the same values.
6118 */
6119 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006120list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 list_T *l1;
6122 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125{
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006127
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006128 if (l1 == NULL || l2 == NULL)
6129 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006130 if (l1 == l2)
6131 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132 if (list_len(l1) != list_len(l2))
6133 return FALSE;
6134
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 for (item1 = l1->lv_first, item2 = l2->lv_first;
6136 item1 != NULL && item2 != NULL;
6137 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006138 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006139 return FALSE;
6140 return item1 == NULL && item2 == NULL;
6141}
6142
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006143/*
6144 * Return the dictitem that an entry in a hashtable points to.
6145 */
6146 dictitem_T *
6147dict_lookup(hi)
6148 hashitem_T *hi;
6149{
6150 return HI2DI(hi);
6151}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006152
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006153/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 * Return TRUE when two dictionaries have exactly the same key/values.
6155 */
6156 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006157dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 dict_T *d1;
6159 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006160 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006161 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006162{
Bram Moolenaar33570922005-01-25 22:26:29 +00006163 hashitem_T *hi;
6164 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006165 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006166
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006167 if (d1 == NULL || d2 == NULL)
6168 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006169 if (d1 == d2)
6170 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006171 if (dict_len(d1) != dict_len(d2))
6172 return FALSE;
6173
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006174 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006176 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006177 if (!HASHITEM_EMPTY(hi))
6178 {
6179 item2 = dict_find(d2, hi->hi_key, -1);
6180 if (item2 == NULL)
6181 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006182 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006183 return FALSE;
6184 --todo;
6185 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006186 }
6187 return TRUE;
6188}
6189
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190static int tv_equal_recurse_limit;
6191
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006192/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006194 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006195 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006196 */
6197 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006198tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006199 typval_T *tv1;
6200 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201 int ic; /* ignore case */
6202 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006203{
6204 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006205 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006207 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006208
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006209 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006210 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006211
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006212 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006213 * recursiveness to a limit. We guess they are equal then.
6214 * A fixed limit has the problem of still taking an awful long time.
6215 * Reduce the limit every time running into it. That should work fine for
6216 * deeply linked structures that are not recursively linked and catch
6217 * recursiveness quickly. */
6218 if (!recursive)
6219 tv_equal_recurse_limit = 1000;
6220 if (recursive_cnt >= tv_equal_recurse_limit)
6221 {
6222 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006223 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006224 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006225
6226 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006229 ++recursive_cnt;
6230 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6231 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006232 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006233
6234 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006235 ++recursive_cnt;
6236 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6237 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006238 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006239
6240 case VAR_FUNC:
6241 return (tv1->vval.v_string != NULL
6242 && tv2->vval.v_string != NULL
6243 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6244
6245 case VAR_NUMBER:
6246 return tv1->vval.v_number == tv2->vval.v_number;
6247
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006248#ifdef FEAT_FLOAT
6249 case VAR_FLOAT:
6250 return tv1->vval.v_float == tv2->vval.v_float;
6251#endif
6252
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006253 case VAR_STRING:
6254 s1 = get_tv_string_buf(tv1, buf1);
6255 s2 = get_tv_string_buf(tv2, buf2);
6256 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006257
6258 case VAR_SPECIAL:
6259 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006260 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006261
6262 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006263 return TRUE;
6264}
6265
6266/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267 * Locate item with index "n" in list "l" and return it.
6268 * A negative index is counted from the end; -1 is the last item.
6269 * Returns NULL when "n" is out of range.
6270 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006271 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006273 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006274 long n;
6275{
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 long idx;
6278
6279 if (l == NULL)
6280 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006281
6282 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006284 n = l->lv_len + n;
6285
6286 /* Check for index out of range. */
6287 if (n < 0 || n >= l->lv_len)
6288 return NULL;
6289
6290 /* When there is a cached index may start search from there. */
6291 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006293 if (n < l->lv_idx / 2)
6294 {
6295 /* closest to the start of the list */
6296 item = l->lv_first;
6297 idx = 0;
6298 }
6299 else if (n > (l->lv_idx + l->lv_len) / 2)
6300 {
6301 /* closest to the end of the list */
6302 item = l->lv_last;
6303 idx = l->lv_len - 1;
6304 }
6305 else
6306 {
6307 /* closest to the cached index */
6308 item = l->lv_idx_item;
6309 idx = l->lv_idx;
6310 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 }
6312 else
6313 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006314 if (n < l->lv_len / 2)
6315 {
6316 /* closest to the start of the list */
6317 item = l->lv_first;
6318 idx = 0;
6319 }
6320 else
6321 {
6322 /* closest to the end of the list */
6323 item = l->lv_last;
6324 idx = l->lv_len - 1;
6325 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006327
6328 while (n > idx)
6329 {
6330 /* search forward */
6331 item = item->li_next;
6332 ++idx;
6333 }
6334 while (n < idx)
6335 {
6336 /* search backward */
6337 item = item->li_prev;
6338 --idx;
6339 }
6340
6341 /* cache the used index */
6342 l->lv_idx = idx;
6343 l->lv_idx_item = item;
6344
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 return item;
6346}
6347
6348/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006349 * Get list item "l[idx]" as a number.
6350 */
6351 static long
6352list_find_nr(l, idx, errorp)
6353 list_T *l;
6354 long idx;
6355 int *errorp; /* set to TRUE when something wrong */
6356{
6357 listitem_T *li;
6358
6359 li = list_find(l, idx);
6360 if (li == NULL)
6361 {
6362 if (errorp != NULL)
6363 *errorp = TRUE;
6364 return -1L;
6365 }
6366 return get_tv_number_chk(&li->li_tv, errorp);
6367}
6368
6369/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006370 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6371 */
6372 char_u *
6373list_find_str(l, idx)
6374 list_T *l;
6375 long idx;
6376{
6377 listitem_T *li;
6378
6379 li = list_find(l, idx - 1);
6380 if (li == NULL)
6381 {
6382 EMSGN(_(e_listidx), idx);
6383 return NULL;
6384 }
6385 return get_tv_string(&li->li_tv);
6386}
6387
6388/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006389 * Locate "item" list "l" and return its index.
6390 * Returns -1 when "item" is not in the list.
6391 */
6392 static long
6393list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 list_T *l;
6395 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006396{
6397 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006399
6400 if (l == NULL)
6401 return -1;
6402 idx = 0;
6403 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6404 ++idx;
6405 if (li == NULL)
6406 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006407 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006408}
6409
6410/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 * Append item "item" to the end of list "l".
6412 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006413 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 list_T *l;
6416 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417{
6418 if (l->lv_last == NULL)
6419 {
6420 /* empty list */
6421 l->lv_first = item;
6422 l->lv_last = item;
6423 item->li_prev = NULL;
6424 }
6425 else
6426 {
6427 l->lv_last->li_next = item;
6428 item->li_prev = l->lv_last;
6429 l->lv_last = item;
6430 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006431 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432 item->li_next = NULL;
6433}
6434
6435/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006439 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006440list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006441 list_T *l;
6442 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006444 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445
Bram Moolenaar05159a02005-02-26 23:04:13 +00006446 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006448 copy_tv(tv, &li->li_tv);
6449 list_append(l, li);
6450 return OK;
6451}
6452
6453/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006454 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006455 * Return FAIL when out of memory.
6456 */
6457 int
6458list_append_dict(list, dict)
6459 list_T *list;
6460 dict_T *dict;
6461{
6462 listitem_T *li = listitem_alloc();
6463
6464 if (li == NULL)
6465 return FAIL;
6466 li->li_tv.v_type = VAR_DICT;
6467 li->li_tv.v_lock = 0;
6468 li->li_tv.vval.v_dict = dict;
6469 list_append(list, li);
6470 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006471 return OK;
6472}
6473
6474/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006475 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006476 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006477 * Returns FAIL when out of memory.
6478 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006479 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006480list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006481 list_T *l;
6482 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006483 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006484{
6485 listitem_T *li = listitem_alloc();
6486
6487 if (li == NULL)
6488 return FAIL;
6489 list_append(l, li);
6490 li->li_tv.v_type = VAR_STRING;
6491 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006492 if (str == NULL)
6493 li->li_tv.vval.v_string = NULL;
6494 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006495 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006496 return FAIL;
6497 return OK;
6498}
6499
6500/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006501 * Append "n" to list "l".
6502 * Returns FAIL when out of memory.
6503 */
6504 static int
6505list_append_number(l, n)
6506 list_T *l;
6507 varnumber_T n;
6508{
6509 listitem_T *li;
6510
6511 li = listitem_alloc();
6512 if (li == NULL)
6513 return FAIL;
6514 li->li_tv.v_type = VAR_NUMBER;
6515 li->li_tv.v_lock = 0;
6516 li->li_tv.vval.v_number = n;
6517 list_append(l, li);
6518 return OK;
6519}
6520
6521/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006522 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 * If "item" is NULL append at the end.
6524 * Return FAIL when out of memory.
6525 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006526 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006528 list_T *l;
6529 typval_T *tv;
6530 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531{
Bram Moolenaar33570922005-01-25 22:26:29 +00006532 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533
6534 if (ni == NULL)
6535 return FAIL;
6536 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006537 list_insert(l, ni, item);
6538 return OK;
6539}
6540
6541 void
6542list_insert(l, ni, item)
6543 list_T *l;
6544 listitem_T *ni;
6545 listitem_T *item;
6546{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 if (item == NULL)
6548 /* Append new item at end of list. */
6549 list_append(l, ni);
6550 else
6551 {
6552 /* Insert new item before existing item. */
6553 ni->li_prev = item->li_prev;
6554 ni->li_next = item;
6555 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006556 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006558 ++l->lv_idx;
6559 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006561 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006563 l->lv_idx_item = NULL;
6564 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006566 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568}
6569
6570/*
6571 * Extend "l1" with "l2".
6572 * If "bef" is NULL append at the end, otherwise insert before this item.
6573 * Returns FAIL when out of memory.
6574 */
6575 static int
6576list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 list_T *l1;
6578 list_T *l2;
6579 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580{
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006582 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006584 /* We also quit the loop when we have inserted the original item count of
6585 * the list, avoid a hang when we extend a list with itself. */
6586 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6588 return FAIL;
6589 return OK;
6590}
6591
6592/*
6593 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6594 * Return FAIL when out of memory.
6595 */
6596 static int
6597list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006598 list_T *l1;
6599 list_T *l2;
6600 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006601{
Bram Moolenaar33570922005-01-25 22:26:29 +00006602 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006603
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006604 if (l1 == NULL || l2 == NULL)
6605 return FAIL;
6606
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006607 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006608 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006609 if (l == NULL)
6610 return FAIL;
6611 tv->v_type = VAR_LIST;
6612 tv->vval.v_list = l;
6613
6614 /* append all items from the second list */
6615 return list_extend(l, l2, NULL);
6616}
6617
6618/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006619 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006621 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 * Returns NULL when out of memory.
6623 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006625list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006626 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006628 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629{
Bram Moolenaar33570922005-01-25 22:26:29 +00006630 list_T *copy;
6631 listitem_T *item;
6632 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633
6634 if (orig == NULL)
6635 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636
6637 copy = list_alloc();
6638 if (copy != NULL)
6639 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006640 if (copyID != 0)
6641 {
6642 /* Do this before adding the items, because one of the items may
6643 * refer back to this list. */
6644 orig->lv_copyID = copyID;
6645 orig->lv_copylist = copy;
6646 }
6647 for (item = orig->lv_first; item != NULL && !got_int;
6648 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 {
6650 ni = listitem_alloc();
6651 if (ni == NULL)
6652 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006653 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006654 {
6655 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6656 {
6657 vim_free(ni);
6658 break;
6659 }
6660 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006662 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663 list_append(copy, ni);
6664 }
6665 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006666 if (item != NULL)
6667 {
6668 list_unref(copy);
6669 copy = NULL;
6670 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 }
6672
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673 return copy;
6674}
6675
6676/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006677 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006678 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006679 * This used to be called list_remove, but that conflicts with a Sun header
6680 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006681 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006682 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006683vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 list_T *l;
6685 listitem_T *item;
6686 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006687{
Bram Moolenaar33570922005-01-25 22:26:29 +00006688 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006689
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006690 /* notify watchers */
6691 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006692 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006693 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006694 list_fix_watch(l, ip);
6695 if (ip == item2)
6696 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006698
6699 if (item2->li_next == NULL)
6700 l->lv_last = item->li_prev;
6701 else
6702 item2->li_next->li_prev = item->li_prev;
6703 if (item->li_prev == NULL)
6704 l->lv_first = item2->li_next;
6705 else
6706 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006707 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006708}
6709
6710/*
6711 * Return an allocated string with the string representation of a list.
6712 * May return NULL.
6713 */
6714 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006715list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006716 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006717 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006718{
6719 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006720
6721 if (tv->vval.v_list == NULL)
6722 return NULL;
6723 ga_init2(&ga, (int)sizeof(char), 80);
6724 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006725 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006726 {
6727 vim_free(ga.ga_data);
6728 return NULL;
6729 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006730 ga_append(&ga, ']');
6731 ga_append(&ga, NUL);
6732 return (char_u *)ga.ga_data;
6733}
6734
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006735typedef struct join_S {
6736 char_u *s;
6737 char_u *tofree;
6738} join_T;
6739
6740 static int
6741list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6742 garray_T *gap; /* to store the result in */
6743 list_T *l;
6744 char_u *sep;
6745 int echo_style;
6746 int copyID;
6747 garray_T *join_gap; /* to keep each list item string */
6748{
6749 int i;
6750 join_T *p;
6751 int len;
6752 int sumlen = 0;
6753 int first = TRUE;
6754 char_u *tofree;
6755 char_u numbuf[NUMBUFLEN];
6756 listitem_T *item;
6757 char_u *s;
6758
6759 /* Stringify each item in the list. */
6760 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6761 {
6762 if (echo_style)
6763 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6764 else
6765 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6766 if (s == NULL)
6767 return FAIL;
6768
6769 len = (int)STRLEN(s);
6770 sumlen += len;
6771
Bram Moolenaarcde88542015-08-11 19:14:00 +02006772 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006773 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6774 if (tofree != NULL || s != numbuf)
6775 {
6776 p->s = s;
6777 p->tofree = tofree;
6778 }
6779 else
6780 {
6781 p->s = vim_strnsave(s, len);
6782 p->tofree = p->s;
6783 }
6784
6785 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006786 if (did_echo_string_emsg) /* recursion error, bail out */
6787 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006788 }
6789
6790 /* Allocate result buffer with its total size, avoid re-allocation and
6791 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6792 if (join_gap->ga_len >= 2)
6793 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6794 if (ga_grow(gap, sumlen + 2) == FAIL)
6795 return FAIL;
6796
6797 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6798 {
6799 if (first)
6800 first = FALSE;
6801 else
6802 ga_concat(gap, sep);
6803 p = ((join_T *)join_gap->ga_data) + i;
6804
6805 if (p->s != NULL)
6806 ga_concat(gap, p->s);
6807 line_breakcheck();
6808 }
6809
6810 return OK;
6811}
6812
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006813/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006815 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006816 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006817 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006818 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006819list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006820 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006821 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006822 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006823 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006824 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006825{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006826 garray_T join_ga;
6827 int retval;
6828 join_T *p;
6829 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006830
Bram Moolenaard39a7512015-04-16 22:51:22 +02006831 if (l->lv_len < 1)
6832 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006833 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6834 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6835
6836 /* Dispose each item in join_ga. */
6837 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006838 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006839 p = (join_T *)join_ga.ga_data;
6840 for (i = 0; i < join_ga.ga_len; ++i)
6841 {
6842 vim_free(p->tofree);
6843 ++p;
6844 }
6845 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006846 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006847
6848 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006849}
6850
6851/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006852 * Return the next (unique) copy ID.
6853 * Used for serializing nested structures.
6854 */
6855 int
6856get_copyID()
6857{
6858 current_copyID += COPYID_INC;
6859 return current_copyID;
6860}
6861
6862/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006863 * Garbage collection for lists and dictionaries.
6864 *
6865 * We use reference counts to be able to free most items right away when they
6866 * are no longer used. But for composite items it's possible that it becomes
6867 * unused while the reference count is > 0: When there is a recursive
6868 * reference. Example:
6869 * :let l = [1, 2, 3]
6870 * :let d = {9: l}
6871 * :let l[1] = d
6872 *
6873 * Since this is quite unusual we handle this with garbage collection: every
6874 * once in a while find out which lists and dicts are not referenced from any
6875 * variable.
6876 *
6877 * Here is a good reference text about garbage collection (refers to Python
6878 * but it applies to all reference-counting mechanisms):
6879 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006880 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006881
6882/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883 * Do garbage collection for lists and dicts.
6884 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006885 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 int
6887garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006888{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 buf_T *buf;
6892 win_T *wp;
6893 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006894 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006895 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006896 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006897#ifdef FEAT_WINDOWS
6898 tabpage_T *tp;
6899#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006901 /* Only do this once. */
6902 want_garbage_collect = FALSE;
6903 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006904 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006905
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006906 /* We advance by two because we add one for items referenced through
6907 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006908 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910 /*
6911 * 1. Go through all accessible variables and mark all lists and dicts
6912 * with copyID.
6913 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006914
6915 /* Don't free variables in the previous_funccal list unless they are only
6916 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006917 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006918 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6919 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6921 NULL);
6922 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6923 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006924 }
6925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926 /* script-local variables */
6927 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929
6930 /* buffer-local variables */
6931 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6933 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006934
6935 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006936 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6938 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006939#ifdef FEAT_AUTOCMD
6940 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006941 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6942 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006943#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006945#ifdef FEAT_WINDOWS
6946 /* tabpage-local variables */
6947 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6949 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006950#endif
6951
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954
6955 /* function-local variables */
6956 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6957 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6959 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 }
6961
Bram Moolenaard812df62008-11-09 12:46:09 +00006962 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006963 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006964
Bram Moolenaar1dced572012-04-05 16:54:08 +02006965#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006966 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006967#endif
6968
Bram Moolenaardb913952012-06-29 12:54:53 +02006969#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006970 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006971#endif
6972
6973#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006974 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006975#endif
6976
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006979 /*
6980 * 2. Free lists and dictionaries that are not referenced.
6981 */
6982 did_free = free_unref_items(copyID);
6983
6984 /*
6985 * 3. Check if any funccal can be freed now.
6986 */
6987 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006988 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006989 if (can_free_funccal(*pfc, copyID))
6990 {
6991 fc = *pfc;
6992 *pfc = fc->caller;
6993 free_funccal(fc, TRUE);
6994 did_free = TRUE;
6995 did_free_funccal = TRUE;
6996 }
6997 else
6998 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006999 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007000 if (did_free_funccal)
7001 /* When a funccal was freed some more items might be garbage
7002 * collected, so run again. */
7003 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007004 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007005 else if (p_verbose > 0)
7006 {
7007 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7008 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007009
7010 return did_free;
7011}
7012
7013/*
7014 * Free lists and dictionaries that are no longer referenced.
7015 */
7016 static int
7017free_unref_items(copyID)
7018 int copyID;
7019{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007020 dict_T *dd, *dd_next;
7021 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007022 int did_free = FALSE;
7023
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007025 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 */
7027 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007028 {
7029 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007030 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007032 /* Free the Dictionary and ordinary items it contains, but don't
7033 * recurse into Lists and Dictionaries, they will be in the list
7034 * of dicts or list of lists. */
7035 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007038 dd = dd_next;
7039 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040
7041 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007042 * Go through the list of lists and free items without the copyID.
7043 * But don't free a list that has a watcher (used in a for loop), these
7044 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 */
7046 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007047 {
7048 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007049 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7050 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007052 /* Free the List and ordinary items it contains, but don't recurse
7053 * into Lists and Dictionaries, they will be in the list of dicts
7054 * or list of lists. */
7055 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007058 ll = ll_next;
7059 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060 return did_free;
7061}
7062
7063/*
7064 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065 * "list_stack" is used to add lists to be marked. Can be NULL.
7066 *
7067 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007069 int
7070set_ref_in_ht(ht, copyID, list_stack)
7071 hashtab_T *ht;
7072 int copyID;
7073 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074{
7075 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007078 hashtab_T *cur_ht;
7079 ht_stack_T *ht_stack = NULL;
7080 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007082 cur_ht = ht;
7083 for (;;)
7084 {
7085 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007087 /* Mark each item in the hashtab. If the item contains a hashtab
7088 * it is added to ht_stack, if it contains a list it is added to
7089 * list_stack. */
7090 todo = (int)cur_ht->ht_used;
7091 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7092 if (!HASHITEM_EMPTY(hi))
7093 {
7094 --todo;
7095 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7096 &ht_stack, list_stack);
7097 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007098 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007099
7100 if (ht_stack == NULL)
7101 break;
7102
7103 /* take an item from the stack */
7104 cur_ht = ht_stack->ht;
7105 tempitem = ht_stack;
7106 ht_stack = ht_stack->prev;
7107 free(tempitem);
7108 }
7109
7110 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007111}
7112
7113/*
7114 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007115 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7116 *
7117 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007118 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007119 int
7120set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007121 list_T *l;
7122 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007123 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007125 listitem_T *li;
7126 int abort = FALSE;
7127 list_T *cur_l;
7128 list_stack_T *list_stack = NULL;
7129 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007130
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 cur_l = l;
7132 for (;;)
7133 {
7134 if (!abort)
7135 /* Mark each item in the list. If the item contains a hashtab
7136 * it is added to ht_stack, if it contains a list it is added to
7137 * list_stack. */
7138 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7139 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7140 ht_stack, &list_stack);
7141 if (list_stack == NULL)
7142 break;
7143
7144 /* take an item from the stack */
7145 cur_l = list_stack->list;
7146 tempitem = list_stack;
7147 list_stack = list_stack->prev;
7148 free(tempitem);
7149 }
7150
7151 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007152}
7153
7154/*
7155 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 * "list_stack" is used to add lists to be marked. Can be NULL.
7157 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7158 *
7159 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007160 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 int
7162set_ref_in_item(tv, copyID, ht_stack, list_stack)
7163 typval_T *tv;
7164 int copyID;
7165 ht_stack_T **ht_stack;
7166 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007167{
7168 dict_T *dd;
7169 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007170 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007171
7172 switch (tv->v_type)
7173 {
7174 case VAR_DICT:
7175 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007176 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007177 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007178 /* Didn't see this dict yet. */
7179 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007180 if (ht_stack == NULL)
7181 {
7182 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7183 }
7184 else
7185 {
7186 ht_stack_T *newitem = (ht_stack_T*)malloc(
7187 sizeof(ht_stack_T));
7188 if (newitem == NULL)
7189 abort = TRUE;
7190 else
7191 {
7192 newitem->ht = &dd->dv_hashtab;
7193 newitem->prev = *ht_stack;
7194 *ht_stack = newitem;
7195 }
7196 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007197 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007198 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007199
7200 case VAR_LIST:
7201 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007202 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007203 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007204 /* Didn't see this list yet. */
7205 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007206 if (list_stack == NULL)
7207 {
7208 abort = set_ref_in_list(ll, copyID, ht_stack);
7209 }
7210 else
7211 {
7212 list_stack_T *newitem = (list_stack_T*)malloc(
7213 sizeof(list_stack_T));
7214 if (newitem == NULL)
7215 abort = TRUE;
7216 else
7217 {
7218 newitem->list = ll;
7219 newitem->prev = *list_stack;
7220 *list_stack = newitem;
7221 }
7222 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007223 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007224 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007225 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007226 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007227}
7228
7229/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230 * Allocate an empty header for a dictionary.
7231 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007232 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233dict_alloc()
7234{
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007236
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007238 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007239 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007240 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007241 if (first_dict != NULL)
7242 first_dict->dv_used_prev = d;
7243 d->dv_used_next = first_dict;
7244 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007245 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007246
Bram Moolenaar33570922005-01-25 22:26:29 +00007247 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007248 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007249 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007250 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007251 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007252 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007253 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254}
7255
7256/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007257 * Allocate an empty dict for a return value.
7258 * Returns OK or FAIL.
7259 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007260 int
Bram Moolenaara800b422010-06-27 01:15:55 +02007261rettv_dict_alloc(rettv)
7262 typval_T *rettv;
7263{
7264 dict_T *d = dict_alloc();
7265
7266 if (d == NULL)
7267 return FAIL;
7268
7269 rettv->vval.v_dict = d;
7270 rettv->v_type = VAR_DICT;
7271 ++d->dv_refcount;
7272 return OK;
7273}
7274
7275
7276/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277 * Unreference a Dictionary: decrement the reference count and free it when it
7278 * becomes zero.
7279 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007280 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007284 if (d != NULL && --d->dv_refcount <= 0)
7285 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286}
7287
7288/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007289 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290 * Ignores the reference count.
7291 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007292 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007293dict_free(d, recurse)
7294 dict_T *d;
7295 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007299 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007301 /* Remove the dict from the list of dicts for garbage collection. */
7302 if (d->dv_used_prev == NULL)
7303 first_dict = d->dv_used_next;
7304 else
7305 d->dv_used_prev->dv_used_next = d->dv_used_next;
7306 if (d->dv_used_next != NULL)
7307 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7308
7309 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007310 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007311 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007312 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007313 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 if (!HASHITEM_EMPTY(hi))
7315 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007316 /* Remove the item before deleting it, just in case there is
7317 * something recursive causing trouble. */
7318 di = HI2DI(hi);
7319 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007320 if (recurse || (di->di_tv.v_type != VAR_LIST
7321 && di->di_tv.v_type != VAR_DICT))
7322 clear_tv(&di->di_tv);
7323 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 --todo;
7325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328 vim_free(d);
7329}
7330
7331/*
7332 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 * The "key" is copied to the new item.
7334 * Note that the value of the item "di_tv" still needs to be initialized!
7335 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007337 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338dictitem_alloc(key)
7339 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340{
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007343 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007345 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007346 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007347 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007348 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007350}
7351
7352/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353 * Make a copy of a Dictionary item.
7354 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007356dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358{
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007361 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7362 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 if (di != NULL)
7364 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007365 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007366 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367 copy_tv(&org->di_tv, &di->di_tv);
7368 }
7369 return di;
7370}
7371
7372/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007373 * Remove item "item" from Dictionary "dict" and free it.
7374 */
7375 static void
7376dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 dict_T *dict;
7378 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007379{
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007381
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007383 if (HASHITEM_EMPTY(hi))
7384 EMSG2(_(e_intern2), "dictitem_remove()");
7385 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387 dictitem_free(item);
7388}
7389
7390/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391 * Free a dict item. Also clears the value.
7392 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007393 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007398 if (item->di_flags & DI_FLAGS_ALLOC)
7399 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400}
7401
7402/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007403 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7404 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007405 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007406 * Returns NULL when out of memory.
7407 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007409dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007412 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413{
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 dict_T *copy;
7415 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007416 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007418
7419 if (orig == NULL)
7420 return NULL;
7421
7422 copy = dict_alloc();
7423 if (copy != NULL)
7424 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007425 if (copyID != 0)
7426 {
7427 orig->dv_copyID = copyID;
7428 orig->dv_copydict = copy;
7429 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007430 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007431 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007433 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007435 --todo;
7436
7437 di = dictitem_alloc(hi->hi_key);
7438 if (di == NULL)
7439 break;
7440 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007441 {
7442 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7443 copyID) == FAIL)
7444 {
7445 vim_free(di);
7446 break;
7447 }
7448 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007449 else
7450 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7451 if (dict_add(copy, di) == FAIL)
7452 {
7453 dictitem_free(di);
7454 break;
7455 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007458
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007460 if (todo > 0)
7461 {
7462 dict_unref(copy);
7463 copy = NULL;
7464 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007465 }
7466
7467 return copy;
7468}
7469
7470/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007471 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007472 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007474 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007476 dict_T *d;
7477 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478{
Bram Moolenaar33570922005-01-25 22:26:29 +00007479 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480}
7481
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007483 * Add a number or string entry to dictionary "d".
7484 * When "str" is NULL use number "nr", otherwise use "str".
7485 * Returns FAIL when out of memory and when key already exists.
7486 */
7487 int
7488dict_add_nr_str(d, key, nr, str)
7489 dict_T *d;
7490 char *key;
7491 long nr;
7492 char_u *str;
7493{
7494 dictitem_T *item;
7495
7496 item = dictitem_alloc((char_u *)key);
7497 if (item == NULL)
7498 return FAIL;
7499 item->di_tv.v_lock = 0;
7500 if (str == NULL)
7501 {
7502 item->di_tv.v_type = VAR_NUMBER;
7503 item->di_tv.vval.v_number = nr;
7504 }
7505 else
7506 {
7507 item->di_tv.v_type = VAR_STRING;
7508 item->di_tv.vval.v_string = vim_strsave(str);
7509 }
7510 if (dict_add(d, item) == FAIL)
7511 {
7512 dictitem_free(item);
7513 return FAIL;
7514 }
7515 return OK;
7516}
7517
7518/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007519 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007520 * Returns FAIL when out of memory and when key already exists.
7521 */
7522 int
7523dict_add_list(d, key, list)
7524 dict_T *d;
7525 char *key;
7526 list_T *list;
7527{
7528 dictitem_T *item;
7529
7530 item = dictitem_alloc((char_u *)key);
7531 if (item == NULL)
7532 return FAIL;
7533 item->di_tv.v_lock = 0;
7534 item->di_tv.v_type = VAR_LIST;
7535 item->di_tv.vval.v_list = list;
7536 if (dict_add(d, item) == FAIL)
7537 {
7538 dictitem_free(item);
7539 return FAIL;
7540 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007541 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007542 return OK;
7543}
7544
7545/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007546 * Get the number of items in a Dictionary.
7547 */
7548 static long
7549dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007551{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 if (d == NULL)
7553 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007554 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007555}
7556
7557/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558 * Find item "key[len]" in Dictionary "d".
7559 * If "len" is negative use strlen(key).
7560 * Returns NULL when not found.
7561 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007562 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 char_u *key;
7566 int len;
7567{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007568#define AKEYLEN 200
7569 char_u buf[AKEYLEN];
7570 char_u *akey;
7571 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007574 if (len < 0)
7575 akey = key;
7576 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007577 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007578 tofree = akey = vim_strnsave(key, len);
7579 if (akey == NULL)
7580 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007581 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 else
7583 {
7584 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007585 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586 akey = buf;
7587 }
7588
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 vim_free(tofree);
7591 if (HASHITEM_EMPTY(hi))
7592 return NULL;
7593 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594}
7595
7596/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007597 * Get a string item from a dictionary.
7598 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007599 * Returns NULL if the entry doesn't exist or out of memory.
7600 */
7601 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007602get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007603 dict_T *d;
7604 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007605 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007606{
7607 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007608 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007609
7610 di = dict_find(d, key, -1);
7611 if (di == NULL)
7612 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007613 s = get_tv_string(&di->di_tv);
7614 if (save && s != NULL)
7615 s = vim_strsave(s);
7616 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007617}
7618
7619/*
7620 * Get a number item from a dictionary.
7621 * Returns 0 if the entry doesn't exist or out of memory.
7622 */
7623 long
7624get_dict_number(d, key)
7625 dict_T *d;
7626 char_u *key;
7627{
7628 dictitem_T *di;
7629
7630 di = dict_find(d, key, -1);
7631 if (di == NULL)
7632 return 0;
7633 return get_tv_number(&di->di_tv);
7634}
7635
7636/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 * Return an allocated string with the string representation of a Dictionary.
7638 * May return NULL.
7639 */
7640 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007641dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007643 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007644{
7645 garray_T ga;
7646 int first = TRUE;
7647 char_u *tofree;
7648 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007651 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007652 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007654 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 return NULL;
7656 ga_init2(&ga, (int)sizeof(char), 80);
7657 ga_append(&ga, '{');
7658
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007659 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007660 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007662 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007664 --todo;
7665
7666 if (first)
7667 first = FALSE;
7668 else
7669 ga_concat(&ga, (char_u *)", ");
7670
7671 tofree = string_quote(hi->hi_key, FALSE);
7672 if (tofree != NULL)
7673 {
7674 ga_concat(&ga, tofree);
7675 vim_free(tofree);
7676 }
7677 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007678 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007679 if (s != NULL)
7680 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007682 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007683 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007684 line_breakcheck();
7685
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007688 if (todo > 0)
7689 {
7690 vim_free(ga.ga_data);
7691 return NULL;
7692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693
7694 ga_append(&ga, '}');
7695 ga_append(&ga, NUL);
7696 return (char_u *)ga.ga_data;
7697}
7698
7699/*
7700 * Allocate a variable for a Dictionary and fill it from "*arg".
7701 * Return OK or FAIL. Returns NOTDONE for {expr}.
7702 */
7703 static int
7704get_dict_tv(arg, rettv, evaluate)
7705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007706 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707 int evaluate;
7708{
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 dict_T *d = NULL;
7710 typval_T tvkey;
7711 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007712 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007715 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716
7717 /*
7718 * First check if it's not a curly-braces thing: {expr}.
7719 * Must do this without evaluating, otherwise a function may be called
7720 * twice. Unfortunately this means we need to call eval1() twice for the
7721 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007724 if (*start != '}')
7725 {
7726 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7727 return FAIL;
7728 if (*start == '}')
7729 return NOTDONE;
7730 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007731
7732 if (evaluate)
7733 {
7734 d = dict_alloc();
7735 if (d == NULL)
7736 return FAIL;
7737 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007738 tvkey.v_type = VAR_UNKNOWN;
7739 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740
7741 *arg = skipwhite(*arg + 1);
7742 while (**arg != '}' && **arg != NUL)
7743 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007744 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 goto failret;
7746 if (**arg != ':')
7747 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007748 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007749 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 goto failret;
7751 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007752 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007754 key = get_tv_string_buf_chk(&tvkey, buf);
7755 if (key == NULL || *key == NUL)
7756 {
7757 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7758 if (key != NULL)
7759 EMSG(_(e_emptykey));
7760 clear_tv(&tvkey);
7761 goto failret;
7762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764
7765 *arg = skipwhite(*arg + 1);
7766 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7767 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007768 if (evaluate)
7769 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770 goto failret;
7771 }
7772 if (evaluate)
7773 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007774 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775 if (item != NULL)
7776 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007777 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007778 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779 clear_tv(&tv);
7780 goto failret;
7781 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007782 item = dictitem_alloc(key);
7783 clear_tv(&tvkey);
7784 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007786 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007787 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007788 if (dict_add(d, item) == FAIL)
7789 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007790 }
7791 }
7792
7793 if (**arg == '}')
7794 break;
7795 if (**arg != ',')
7796 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007797 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007798 goto failret;
7799 }
7800 *arg = skipwhite(*arg + 1);
7801 }
7802
7803 if (**arg != '}')
7804 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007805 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007806failret:
7807 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007808 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007809 return FAIL;
7810 }
7811
7812 *arg = skipwhite(*arg + 1);
7813 if (evaluate)
7814 {
7815 rettv->v_type = VAR_DICT;
7816 rettv->vval.v_dict = d;
7817 ++d->dv_refcount;
7818 }
7819
7820 return OK;
7821}
7822
Bram Moolenaar17a13432016-01-24 14:22:10 +01007823 static char *
7824get_var_special_name(int nr)
7825{
7826 switch (nr)
7827 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007828 case VVAL_FALSE: return "v:false";
7829 case VVAL_TRUE: return "v:true";
7830 case VVAL_NONE: return "v:none";
7831 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007832 }
7833 EMSG2(_(e_intern2), "get_var_special_name()");
7834 return "42";
7835}
7836
Bram Moolenaar8c711452005-01-14 21:53:12 +00007837/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007838 * Return a string with the string representation of a variable.
7839 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007840 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007841 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007842 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007843 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007844 */
7845 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007846echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007848 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007849 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007850 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007851{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007852 static int recurse = 0;
7853 char_u *r = NULL;
7854
Bram Moolenaar33570922005-01-25 22:26:29 +00007855 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007857 if (!did_echo_string_emsg)
7858 {
7859 /* Only give this message once for a recursive call to avoid
7860 * flooding the user with errors. And stop iterating over lists
7861 * and dicts. */
7862 did_echo_string_emsg = TRUE;
7863 EMSG(_("E724: variable nested too deep for displaying"));
7864 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007865 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007866 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007867 }
7868 ++recurse;
7869
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007870 switch (tv->v_type)
7871 {
7872 case VAR_FUNC:
7873 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874 r = tv->vval.v_string;
7875 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007876
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007877 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007878 if (tv->vval.v_list == NULL)
7879 {
7880 *tofree = NULL;
7881 r = NULL;
7882 }
7883 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7884 {
7885 *tofree = NULL;
7886 r = (char_u *)"[...]";
7887 }
7888 else
7889 {
7890 tv->vval.v_list->lv_copyID = copyID;
7891 *tofree = list2string(tv, copyID);
7892 r = *tofree;
7893 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007894 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007895
Bram Moolenaar8c711452005-01-14 21:53:12 +00007896 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007897 if (tv->vval.v_dict == NULL)
7898 {
7899 *tofree = NULL;
7900 r = NULL;
7901 }
7902 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7903 {
7904 *tofree = NULL;
7905 r = (char_u *)"{...}";
7906 }
7907 else
7908 {
7909 tv->vval.v_dict->dv_copyID = copyID;
7910 *tofree = dict2string(tv, copyID);
7911 r = *tofree;
7912 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007913 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007914
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007915 case VAR_STRING:
7916 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 *tofree = NULL;
7918 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007920
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007921#ifdef FEAT_FLOAT
7922 case VAR_FLOAT:
7923 *tofree = NULL;
7924 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7925 r = numbuf;
7926 break;
7927#endif
7928
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007929 case VAR_SPECIAL:
7930 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007931 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007932 break;
7933
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007935 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007936 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007937 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007938
Bram Moolenaar8502c702014-06-17 12:51:16 +02007939 if (--recurse == 0)
7940 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007941 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942}
7943
7944/*
7945 * Return a string with the string representation of a variable.
7946 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7947 * "numbuf" is used for a number.
7948 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007949 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950 */
7951 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007952tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007953 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007954 char_u **tofree;
7955 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007956 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007957{
7958 switch (tv->v_type)
7959 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 case VAR_FUNC:
7961 *tofree = string_quote(tv->vval.v_string, TRUE);
7962 return *tofree;
7963 case VAR_STRING:
7964 *tofree = string_quote(tv->vval.v_string, FALSE);
7965 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007966#ifdef FEAT_FLOAT
7967 case VAR_FLOAT:
7968 *tofree = NULL;
7969 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7970 return numbuf;
7971#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007972 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007973 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007974 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007975 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007976 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007977 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007978 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007979 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007980 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007981}
7982
7983/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007984 * Return string "str" in ' quotes, doubling ' characters.
7985 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007986 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007987 */
7988 static char_u *
7989string_quote(str, function)
7990 char_u *str;
7991 int function;
7992{
Bram Moolenaar33570922005-01-25 22:26:29 +00007993 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007994 char_u *p, *r, *s;
7995
Bram Moolenaar33570922005-01-25 22:26:29 +00007996 len = (function ? 13 : 3);
7997 if (str != NULL)
7998 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007999 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008000 for (p = str; *p != NUL; mb_ptr_adv(p))
8001 if (*p == '\'')
8002 ++len;
8003 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008004 s = r = alloc(len);
8005 if (r != NULL)
8006 {
8007 if (function)
8008 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008009 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008010 r += 10;
8011 }
8012 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008013 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008014 if (str != NULL)
8015 for (p = str; *p != NUL; )
8016 {
8017 if (*p == '\'')
8018 *r++ = '\'';
8019 MB_COPY_CHAR(p, r);
8020 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008021 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008022 if (function)
8023 *r++ = ')';
8024 *r++ = NUL;
8025 }
8026 return s;
8027}
8028
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008029#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008030/*
8031 * Convert the string "text" to a floating point number.
8032 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8033 * this always uses a decimal point.
8034 * Returns the length of the text that was consumed.
8035 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008036 int
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008037string2float(text, value)
8038 char_u *text;
8039 float_T *value; /* result stored here */
8040{
8041 char *s = (char *)text;
8042 float_T f;
8043
8044 f = strtod(s, &s);
8045 *value = f;
8046 return (int)((char_u *)s - text);
8047}
8048#endif
8049
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 * Get the value of an environment variable.
8052 * "arg" is pointing to the '$'. It is advanced to after the name.
8053 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008054 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 */
8056 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008057get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 int evaluate;
8061{
8062 char_u *string = NULL;
8063 int len;
8064 int cc;
8065 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008066 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067
8068 ++*arg;
8069 name = *arg;
8070 len = get_env_len(arg);
8071 if (evaluate)
8072 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008073 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008074 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008075
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008076 cc = name[len];
8077 name[len] = NUL;
8078 /* first try vim_getenv(), fast for normal environment vars */
8079 string = vim_getenv(name, &mustfree);
8080 if (string != NULL && *string != NUL)
8081 {
8082 if (!mustfree)
8083 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008085 else
8086 {
8087 if (mustfree)
8088 vim_free(string);
8089
8090 /* next try expanding things like $VIM and ${HOME} */
8091 string = expand_env_save(name - 1);
8092 if (string != NULL && *string == '$')
8093 {
8094 vim_free(string);
8095 string = NULL;
8096 }
8097 }
8098 name[len] = cc;
8099
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008100 rettv->v_type = VAR_STRING;
8101 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
8103
8104 return OK;
8105}
8106
8107/*
8108 * Array with names and number of arguments of all internal functions
8109 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8110 */
8111static struct fst
8112{
8113 char *f_name; /* function name */
8114 char f_min_argc; /* minimal number of arguments */
8115 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008116 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008117 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118} functions[] =
8119{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008120#ifdef FEAT_FLOAT
8121 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008122 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008123#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008124 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008125 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008126 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"append", 2, 2, f_append},
8128 {"argc", 0, 0, f_argc},
8129 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008130 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008131 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008132#ifdef FEAT_FLOAT
8133 {"asin", 1, 1, f_asin}, /* WJMc */
8134#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008135 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008136 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008137 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008138 {"assert_false", 1, 2, f_assert_false},
8139 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008140#ifdef FEAT_FLOAT
8141 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008142 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008145 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"bufexists", 1, 1, f_bufexists},
8147 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8148 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8149 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8150 {"buflisted", 1, 1, f_buflisted},
8151 {"bufloaded", 1, 1, f_bufloaded},
8152 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008153 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"bufwinnr", 1, 1, f_bufwinnr},
8155 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008156 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008157 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008158 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008159#ifdef FEAT_FLOAT
8160 {"ceil", 1, 1, f_ceil},
8161#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008162 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008163 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008165 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008167#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008168 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008169 {"complete_add", 1, 1, f_complete_add},
8170 {"complete_check", 0, 0, f_complete_check},
8171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008173 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008174#ifdef FEAT_FLOAT
8175 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008176 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008177#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008178 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008180 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008181 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008182 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008184 {"diff_filler", 1, 1, f_diff_filler},
8185 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008186 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008188 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"eventhandler", 0, 0, f_eventhandler},
8190 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008191 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008193#ifdef FEAT_FLOAT
8194 {"exp", 1, 1, f_exp},
8195#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008196 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008197 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008198 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8200 {"filereadable", 1, 1, f_filereadable},
8201 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008202 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008203 {"finddir", 1, 3, f_finddir},
8204 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008205#ifdef FEAT_FLOAT
8206 {"float2nr", 1, 1, f_float2nr},
8207 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008208 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008209#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008210 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {"fnamemodify", 2, 2, f_fnamemodify},
8212 {"foldclosed", 1, 1, f_foldclosed},
8213 {"foldclosedend", 1, 1, f_foldclosedend},
8214 {"foldlevel", 1, 1, f_foldlevel},
8215 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008216 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008218 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008219 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008220 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008221 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008222 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"getchar", 0, 1, f_getchar},
8224 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008225 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"getcmdline", 0, 0, f_getcmdline},
8227 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008228 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008229 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008230 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008231 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008232 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008233 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"getfsize", 1, 1, f_getfsize},
8235 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008236 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008237 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008238 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008239 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008240 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008241 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008242 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008243 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008245 {"gettabvar", 2, 3, f_gettabvar},
8246 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {"getwinposx", 0, 0, f_getwinposx},
8248 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008249 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008250 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008251 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008252 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008254 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008255 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008256 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8258 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8259 {"histadd", 2, 2, f_histadd},
8260 {"histdel", 1, 2, f_histdel},
8261 {"histget", 1, 2, f_histget},
8262 {"histnr", 1, 1, f_histnr},
8263 {"hlID", 1, 1, f_hlID},
8264 {"hlexists", 1, 1, f_hlexists},
8265 {"hostname", 0, 0, f_hostname},
8266 {"iconv", 3, 3, f_iconv},
8267 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008268 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008269 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008271 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"inputrestore", 0, 0, f_inputrestore},
8273 {"inputsave", 0, 0, f_inputsave},
8274 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008276 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008278 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008279 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008280 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008281 {"jsondecode", 1, 1, f_jsondecode},
8282 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008283 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"libcall", 3, 3, f_libcall},
8287 {"libcallnr", 3, 3, f_libcallnr},
8288 {"line", 1, 1, f_line},
8289 {"line2byte", 1, 1, f_line2byte},
8290 {"lispindent", 1, 1, f_lispindent},
8291 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008292#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008293 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008294 {"log10", 1, 1, f_log10},
8295#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008296#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008297 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008298#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008299 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008300 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008301 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008302 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008303 {"matchadd", 2, 5, f_matchadd},
8304 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008305 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008306 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008307 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008308 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008309 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008310 {"max", 1, 1, f_max},
8311 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008312#ifdef vim_mkdir
8313 {"mkdir", 1, 3, f_mkdir},
8314#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008315 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008316#ifdef FEAT_MZSCHEME
8317 {"mzeval", 1, 1, f_mzeval},
8318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008320 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008321 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008322 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008323#ifdef FEAT_PERL
8324 {"perleval", 1, 1, f_perleval},
8325#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008326#ifdef FEAT_FLOAT
8327 {"pow", 2, 2, f_pow},
8328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008330 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008331 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008332#ifdef FEAT_PYTHON3
8333 {"py3eval", 1, 1, f_py3eval},
8334#endif
8335#ifdef FEAT_PYTHON
8336 {"pyeval", 1, 1, f_pyeval},
8337#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008338 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008339 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008340 {"reltime", 0, 2, f_reltime},
8341 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {"remote_expr", 2, 3, f_remote_expr},
8343 {"remote_foreground", 1, 1, f_remote_foreground},
8344 {"remote_peek", 1, 2, f_remote_peek},
8345 {"remote_read", 1, 1, f_remote_read},
8346 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008347 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008349 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008351 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008352#ifdef FEAT_FLOAT
8353 {"round", 1, 1, f_round},
8354#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008355 {"screenattr", 2, 2, f_screenattr},
8356 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008357 {"screencol", 0, 0, f_screencol},
8358 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008359 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008360 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008361 {"searchpair", 3, 7, f_searchpair},
8362 {"searchpairpos", 3, 7, f_searchpairpos},
8363 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"server2client", 2, 2, f_server2client},
8365 {"serverlist", 0, 0, f_serverlist},
8366 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008367 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {"setcmdpos", 1, 1, f_setcmdpos},
8369 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008370 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008371 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008372 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008373 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008375 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008376 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008378#ifdef FEAT_CRYPT
8379 {"sha256", 1, 1, f_sha256},
8380#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008381 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008382 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008384#ifdef FEAT_FLOAT
8385 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008386 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008387#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008388 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008389 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008390 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008391 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008392 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008393#ifdef FEAT_FLOAT
8394 {"sqrt", 1, 1, f_sqrt},
8395 {"str2float", 1, 1, f_str2float},
8396#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008397 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008398 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008399 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400#ifdef HAVE_STRFTIME
8401 {"strftime", 1, 2, f_strftime},
8402#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008403 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008404 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {"strlen", 1, 1, f_strlen},
8406 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008407 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008409 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008410 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 {"substitute", 4, 4, f_substitute},
8412 {"synID", 3, 3, f_synID},
8413 {"synIDattr", 2, 3, f_synIDattr},
8414 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008415 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008416 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008417 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008418 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008419 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008420 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008421 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008422 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008423 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008424#ifdef FEAT_FLOAT
8425 {"tan", 1, 1, f_tan},
8426 {"tanh", 1, 1, f_tanh},
8427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008429 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"tolower", 1, 1, f_tolower},
8431 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008432 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008433#ifdef FEAT_FLOAT
8434 {"trunc", 1, 1, f_trunc},
8435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008437 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008438 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008439 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008440 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {"virtcol", 1, 1, f_virtcol},
8442 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008443 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {"winbufnr", 1, 1, f_winbufnr},
8445 {"wincol", 0, 0, f_wincol},
8446 {"winheight", 1, 1, f_winheight},
8447 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008448 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008450 {"winrestview", 1, 1, f_winrestview},
8451 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008453 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008454 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008455 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456};
8457
8458#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8459
8460/*
8461 * Function given to ExpandGeneric() to obtain the list of internal
8462 * or user defined function names.
8463 */
8464 char_u *
8465get_function_name(xp, idx)
8466 expand_T *xp;
8467 int idx;
8468{
8469 static int intidx = -1;
8470 char_u *name;
8471
8472 if (idx == 0)
8473 intidx = -1;
8474 if (intidx < 0)
8475 {
8476 name = get_user_func_name(xp, idx);
8477 if (name != NULL)
8478 return name;
8479 }
8480 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8481 {
8482 STRCPY(IObuff, functions[intidx].f_name);
8483 STRCAT(IObuff, "(");
8484 if (functions[intidx].f_max_argc == 0)
8485 STRCAT(IObuff, ")");
8486 return IObuff;
8487 }
8488
8489 return NULL;
8490}
8491
8492/*
8493 * Function given to ExpandGeneric() to obtain the list of internal or
8494 * user defined variable or function names.
8495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 char_u *
8497get_expr_name(xp, idx)
8498 expand_T *xp;
8499 int idx;
8500{
8501 static int intidx = -1;
8502 char_u *name;
8503
8504 if (idx == 0)
8505 intidx = -1;
8506 if (intidx < 0)
8507 {
8508 name = get_function_name(xp, idx);
8509 if (name != NULL)
8510 return name;
8511 }
8512 return get_user_var_name(xp, ++intidx);
8513}
8514
8515#endif /* FEAT_CMDL_COMPL */
8516
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008517#if defined(EBCDIC) || defined(PROTO)
8518/*
8519 * Compare struct fst by function name.
8520 */
8521 static int
8522compare_func_name(s1, s2)
8523 const void *s1;
8524 const void *s2;
8525{
8526 struct fst *p1 = (struct fst *)s1;
8527 struct fst *p2 = (struct fst *)s2;
8528
8529 return STRCMP(p1->f_name, p2->f_name);
8530}
8531
8532/*
8533 * Sort the function table by function name.
8534 * The sorting of the table above is ASCII dependant.
8535 * On machines using EBCDIC we have to sort it.
8536 */
8537 static void
8538sortFunctions()
8539{
8540 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8541
8542 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8543}
8544#endif
8545
8546
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547/*
8548 * Find internal function in table above.
8549 * Return index, or -1 if not found
8550 */
8551 static int
8552find_internal_func(name)
8553 char_u *name; /* name of the function */
8554{
8555 int first = 0;
8556 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8557 int cmp;
8558 int x;
8559
8560 /*
8561 * Find the function name in the table. Binary search.
8562 */
8563 while (first <= last)
8564 {
8565 x = first + ((unsigned)(last - first) >> 1);
8566 cmp = STRCMP(name, functions[x].f_name);
8567 if (cmp < 0)
8568 last = x - 1;
8569 else if (cmp > 0)
8570 first = x + 1;
8571 else
8572 return x;
8573 }
8574 return -1;
8575}
8576
8577/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008578 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8579 * name it contains, otherwise return "name".
8580 */
8581 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008582deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008583 char_u *name;
8584 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008585 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008586{
Bram Moolenaar33570922005-01-25 22:26:29 +00008587 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008588 int cc;
8589
8590 cc = name[*lenp];
8591 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008592 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008593 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008594 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008595 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008596 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008597 {
8598 *lenp = 0;
8599 return (char_u *)""; /* just in case */
8600 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008601 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008602 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008603 }
8604
8605 return name;
8606}
8607
8608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 * Allocate a variable for the result of a function.
8610 * Return OK or FAIL.
8611 */
8612 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008613get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8614 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 char_u *name; /* name of the function */
8616 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 char_u **arg; /* argument, pointing to the '(' */
8619 linenr_T firstline; /* first line of range */
8620 linenr_T lastline; /* last line of range */
8621 int *doesrange; /* return: function handled range */
8622 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624{
8625 char_u *argp;
8626 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008627 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 int argcount = 0; /* number of arguments found */
8629
8630 /*
8631 * Get the arguments.
8632 */
8633 argp = *arg;
8634 while (argcount < MAX_FUNC_ARGS)
8635 {
8636 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8637 if (*argp == ')' || *argp == ',' || *argp == NUL)
8638 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8640 {
8641 ret = FAIL;
8642 break;
8643 }
8644 ++argcount;
8645 if (*argp != ',')
8646 break;
8647 }
8648 if (*argp == ')')
8649 ++argp;
8650 else
8651 ret = FAIL;
8652
8653 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008654 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008655 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008657 {
8658 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008659 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008661 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663
8664 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
8667 *arg = skipwhite(argp);
8668 return ret;
8669}
8670
8671
8672/*
8673 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008674 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008675 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 */
8677 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008678call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008679 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008680 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008682 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008684 typval_T *argvars; /* vars for arguments, must have "argcount"
8685 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 linenr_T firstline; /* first line of range */
8687 linenr_T lastline; /* last line of range */
8688 int *doesrange; /* return: function handled range */
8689 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008690 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691{
8692 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693#define ERROR_UNKNOWN 0
8694#define ERROR_TOOMANY 1
8695#define ERROR_TOOFEW 2
8696#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008697#define ERROR_DICT 4
8698#define ERROR_NONE 5
8699#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 int error = ERROR_NONE;
8701 int i;
8702 int llen;
8703 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704#define FLEN_FIXED 40
8705 char_u fname_buf[FLEN_FIXED + 1];
8706 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008707 char_u *name;
8708
8709 /* Make a copy of the name, if it comes from a funcref variable it could
8710 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008711 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008712 if (name == NULL)
8713 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714
8715 /*
8716 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8717 * Change <SNR>123_name() to K_SNR 123_name().
8718 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 llen = eval_fname_script(name);
8721 if (llen > 0)
8722 {
8723 fname_buf[0] = K_SPECIAL;
8724 fname_buf[1] = KS_EXTRA;
8725 fname_buf[2] = (int)KE_SNR;
8726 i = 3;
8727 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8728 {
8729 if (current_SID <= 0)
8730 error = ERROR_SCRIPT;
8731 else
8732 {
8733 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8734 i = (int)STRLEN(fname_buf);
8735 }
8736 }
8737 if (i + STRLEN(name + llen) < FLEN_FIXED)
8738 {
8739 STRCPY(fname_buf + i, name + llen);
8740 fname = fname_buf;
8741 }
8742 else
8743 {
8744 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8745 if (fname == NULL)
8746 error = ERROR_OTHER;
8747 else
8748 {
8749 mch_memmove(fname, fname_buf, (size_t)i);
8750 STRCPY(fname + i, name + llen);
8751 }
8752 }
8753 }
8754 else
8755 fname = name;
8756
8757 *doesrange = FALSE;
8758
8759
8760 /* execute the function if no errors detected and executing */
8761 if (evaluate && error == ERROR_NONE)
8762 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008763 char_u *rfname = fname;
8764
8765 /* Ignore "g:" before a function name. */
8766 if (fname[0] == 'g' && fname[1] == ':')
8767 rfname = fname + 2;
8768
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008769 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8770 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 error = ERROR_UNKNOWN;
8772
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008773 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 {
8775 /*
8776 * User defined function.
8777 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008778 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008779
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008781 /* Trigger FuncUndefined event, may load the function. */
8782 if (fp == NULL
8783 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008784 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008785 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008787 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008788 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 }
8790#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008791 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008792 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008793 {
8794 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008795 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008796 }
8797
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 if (fp != NULL)
8799 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008800 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008802 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008804 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008806 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008807 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 else
8809 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008810 int did_save_redo = FALSE;
8811
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 /*
8813 * Call the user function.
8814 * Save and restore search patterns, script variables and
8815 * redo buffer.
8816 */
8817 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008818#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008819 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008820#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008821 {
8822 saveRedobuff();
8823 did_save_redo = TRUE;
8824 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008825 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008826 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008827 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008828 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8829 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8830 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008831 /* Function was unreferenced while being used, free it
8832 * now. */
8833 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008834 if (did_save_redo)
8835 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 restore_search_patterns();
8837 error = ERROR_NONE;
8838 }
8839 }
8840 }
8841 else
8842 {
8843 /*
8844 * Find the function name in the table, call its implementation.
8845 */
8846 i = find_internal_func(fname);
8847 if (i >= 0)
8848 {
8849 if (argcount < functions[i].f_min_argc)
8850 error = ERROR_TOOFEW;
8851 else if (argcount > functions[i].f_max_argc)
8852 error = ERROR_TOOMANY;
8853 else
8854 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008855 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 error = ERROR_NONE;
8858 }
8859 }
8860 }
8861 /*
8862 * The function call (or "FuncUndefined" autocommand sequence) might
8863 * have been aborted by an error, an interrupt, or an explicitly thrown
8864 * exception that has not been caught so far. This situation can be
8865 * tested for by calling aborting(). For an error in an internal
8866 * function or for the "E132" error in call_user_func(), however, the
8867 * throw point at which the "force_abort" flag (temporarily reset by
8868 * emsg()) is normally updated has not been reached yet. We need to
8869 * update that flag first to make aborting() reliable.
8870 */
8871 update_force_abort();
8872 }
8873 if (error == ERROR_NONE)
8874 ret = OK;
8875
8876 /*
8877 * Report an error unless the argument evaluation or function call has been
8878 * cancelled due to an aborting error, an interrupt, or an exception.
8879 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008880 if (!aborting())
8881 {
8882 switch (error)
8883 {
8884 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008885 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008886 break;
8887 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008888 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008889 break;
8890 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008891 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008892 name);
8893 break;
8894 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008895 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008896 name);
8897 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008898 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008899 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008900 name);
8901 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008902 }
8903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 if (fname != name && fname != fname_buf)
8906 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008907 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
8909 return ret;
8910}
8911
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008912/*
8913 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008914 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008915 */
8916 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008917emsg_funcname(ermsg, name)
8918 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008919 char_u *name;
8920{
8921 char_u *p;
8922
8923 if (*name == K_SPECIAL)
8924 p = concat_str((char_u *)"<SNR>", name + 3);
8925 else
8926 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008927 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008928 if (p != name)
8929 vim_free(p);
8930}
8931
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008932/*
8933 * Return TRUE for a non-zero Number and a non-empty String.
8934 */
8935 static int
8936non_zero_arg(argvars)
8937 typval_T *argvars;
8938{
8939 return ((argvars[0].v_type == VAR_NUMBER
8940 && argvars[0].vval.v_number != 0)
8941 || (argvars[0].v_type == VAR_STRING
8942 && argvars[0].vval.v_string != NULL
8943 && *argvars[0].vval.v_string != NUL));
8944}
8945
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946/*********************************************
8947 * Implementation of the built-in functions
8948 */
8949
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008950#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008951static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008952
8953/*
8954 * Get the float value of "argvars[0]" into "f".
8955 * Returns FAIL when the argument is not a Number or Float.
8956 */
8957 static int
8958get_float_arg(argvars, f)
8959 typval_T *argvars;
8960 float_T *f;
8961{
8962 if (argvars[0].v_type == VAR_FLOAT)
8963 {
8964 *f = argvars[0].vval.v_float;
8965 return OK;
8966 }
8967 if (argvars[0].v_type == VAR_NUMBER)
8968 {
8969 *f = (float_T)argvars[0].vval.v_number;
8970 return OK;
8971 }
8972 EMSG(_("E808: Number or Float required"));
8973 return FAIL;
8974}
8975
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008976/*
8977 * "abs(expr)" function
8978 */
8979 static void
8980f_abs(argvars, rettv)
8981 typval_T *argvars;
8982 typval_T *rettv;
8983{
8984 if (argvars[0].v_type == VAR_FLOAT)
8985 {
8986 rettv->v_type = VAR_FLOAT;
8987 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8988 }
8989 else
8990 {
8991 varnumber_T n;
8992 int error = FALSE;
8993
8994 n = get_tv_number_chk(&argvars[0], &error);
8995 if (error)
8996 rettv->vval.v_number = -1;
8997 else if (n > 0)
8998 rettv->vval.v_number = n;
8999 else
9000 rettv->vval.v_number = -n;
9001 }
9002}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009003
9004/*
9005 * "acos()" function
9006 */
9007 static void
9008f_acos(argvars, rettv)
9009 typval_T *argvars;
9010 typval_T *rettv;
9011{
9012 float_T f;
9013
9014 rettv->v_type = VAR_FLOAT;
9015 if (get_float_arg(argvars, &f) == OK)
9016 rettv->vval.v_float = acos(f);
9017 else
9018 rettv->vval.v_float = 0.0;
9019}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009020#endif
9021
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009023 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 */
9025 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009026f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 typval_T *argvars;
9028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029{
Bram Moolenaar33570922005-01-25 22:26:29 +00009030 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009033 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009035 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009036 && !tv_check_lock(l->lv_lock,
9037 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009038 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009040 }
9041 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009042 EMSG(_(e_listreq));
9043}
9044
9045/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009046 * "alloc_fail(id, countdown, repeat)" function
9047 */
9048 static void
9049f_alloc_fail(argvars, rettv)
9050 typval_T *argvars;
9051 typval_T *rettv UNUSED;
9052{
9053 if (argvars[0].v_type != VAR_NUMBER
9054 || argvars[0].vval.v_number <= 0
9055 || argvars[1].v_type != VAR_NUMBER
9056 || argvars[1].vval.v_number < 0
9057 || argvars[2].v_type != VAR_NUMBER)
9058 EMSG(_(e_invarg));
9059 else
9060 {
9061 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009062 if (alloc_fail_id >= aid_last)
9063 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009064 alloc_fail_countdown = argvars[1].vval.v_number;
9065 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009066 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009067 }
9068}
9069
9070/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009071 * "and(expr, expr)" function
9072 */
9073 static void
9074f_and(argvars, rettv)
9075 typval_T *argvars;
9076 typval_T *rettv;
9077{
9078 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9079 & get_tv_number_chk(&argvars[1], NULL);
9080}
9081
9082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009083 * "append(lnum, string/list)" function
9084 */
9085 static void
9086f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009087 typval_T *argvars;
9088 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009089{
9090 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009091 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009092 list_T *l = NULL;
9093 listitem_T *li = NULL;
9094 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009095 long added = 0;
9096
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009097 /* When coming here from Insert mode, sync undo, so that this can be
9098 * undone separately from what was previously inserted. */
9099 if (u_sync_once == 2)
9100 {
9101 u_sync_once = 1; /* notify that u_sync() was called */
9102 u_sync(TRUE);
9103 }
9104
Bram Moolenaar0d660222005-01-07 21:51:51 +00009105 lnum = get_tv_lnum(argvars);
9106 if (lnum >= 0
9107 && lnum <= curbuf->b_ml.ml_line_count
9108 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009109 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009110 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009111 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009112 l = argvars[1].vval.v_list;
9113 if (l == NULL)
9114 return;
9115 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009116 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009117 for (;;)
9118 {
9119 if (l == NULL)
9120 tv = &argvars[1]; /* append a string */
9121 else if (li == NULL)
9122 break; /* end of list */
9123 else
9124 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009125 line = get_tv_string_chk(tv);
9126 if (line == NULL) /* type error */
9127 {
9128 rettv->vval.v_number = 1; /* Failed */
9129 break;
9130 }
9131 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009132 ++added;
9133 if (l == NULL)
9134 break;
9135 li = li->li_next;
9136 }
9137
9138 appended_lines_mark(lnum, added);
9139 if (curwin->w_cursor.lnum > lnum)
9140 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009142 else
9143 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144}
9145
9146/*
9147 * "argc()" function
9148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009150f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009151 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155}
9156
9157/*
9158 * "argidx()" function
9159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166}
9167
9168/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009169 * "arglistid()" function
9170 */
9171 static void
9172f_arglistid(argvars, rettv)
9173 typval_T *argvars UNUSED;
9174 typval_T *rettv;
9175{
9176 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009177
9178 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009179 wp = find_tabwin(&argvars[0], &argvars[1]);
9180 if (wp != NULL)
9181 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009182}
9183
9184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 * "argv(nr)" function
9186 */
9187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009189 typval_T *argvars;
9190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
9192 int idx;
9193
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009194 if (argvars[0].v_type != VAR_UNKNOWN)
9195 {
9196 idx = get_tv_number_chk(&argvars[0], NULL);
9197 if (idx >= 0 && idx < ARGCOUNT)
9198 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9199 else
9200 rettv->vval.v_string = NULL;
9201 rettv->v_type = VAR_STRING;
9202 }
9203 else if (rettv_list_alloc(rettv) == OK)
9204 for (idx = 0; idx < ARGCOUNT; ++idx)
9205 list_append_string(rettv->vval.v_list,
9206 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207}
9208
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009209static void prepare_assert_error(garray_T*gap);
9210static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9211static void assert_error(garray_T *gap);
9212static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009213
9214/*
9215 * Prepare "gap" for an assert error and add the sourcing position.
9216 */
9217 static void
9218prepare_assert_error(gap)
9219 garray_T *gap;
9220{
9221 char buf[NUMBUFLEN];
9222
9223 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009224 if (sourcing_name != NULL)
9225 {
9226 ga_concat(gap, sourcing_name);
9227 if (sourcing_lnum > 0)
9228 ga_concat(gap, (char_u *)" ");
9229 }
9230 if (sourcing_lnum > 0)
9231 {
9232 sprintf(buf, "line %ld", (long)sourcing_lnum);
9233 ga_concat(gap, (char_u *)buf);
9234 }
9235 if (sourcing_name != NULL || sourcing_lnum > 0)
9236 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009237}
9238
9239/*
9240 * Fill "gap" with information about an assert error.
9241 */
9242 static void
9243fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9244 garray_T *gap;
9245 typval_T *opt_msg_tv;
9246 char_u *exp_str;
9247 typval_T *exp_tv;
9248 typval_T *got_tv;
9249{
9250 char_u numbuf[NUMBUFLEN];
9251 char_u *tofree;
9252
9253 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9254 {
9255 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9256 vim_free(tofree);
9257 }
9258 else
9259 {
9260 ga_concat(gap, (char_u *)"Expected ");
9261 if (exp_str == NULL)
9262 {
9263 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9264 vim_free(tofree);
9265 }
9266 else
9267 ga_concat(gap, exp_str);
9268 ga_concat(gap, (char_u *)" but got ");
9269 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9270 vim_free(tofree);
9271 }
9272}
Bram Moolenaar43345542015-11-29 17:35:35 +01009273
9274/*
9275 * Add an assert error to v:errors.
9276 */
9277 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009278assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009279 garray_T *gap;
9280{
9281 struct vimvar *vp = &vimvars[VV_ERRORS];
9282
9283 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9284 /* Make sure v:errors is a list. */
9285 set_vim_var_list(VV_ERRORS, list_alloc());
9286 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9287}
9288
Bram Moolenaar43345542015-11-29 17:35:35 +01009289/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009290 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009291 */
9292 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009293f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009294 typval_T *argvars;
9295 typval_T *rettv UNUSED;
9296{
9297 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009298
9299 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9300 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009301 prepare_assert_error(&ga);
9302 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9303 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009304 ga_clear(&ga);
9305 }
9306}
9307
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009308/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009309 * "assert_exception(string[, msg])" function
9310 */
9311 static void
9312f_assert_exception(argvars, rettv)
9313 typval_T *argvars;
9314 typval_T *rettv UNUSED;
9315{
9316 garray_T ga;
9317 char *error;
9318
9319 error = (char *)get_tv_string_chk(&argvars[0]);
9320 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9321 {
9322 prepare_assert_error(&ga);
9323 ga_concat(&ga, (char_u *)"v:exception is not set");
9324 assert_error(&ga);
9325 ga_clear(&ga);
9326 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009327 else if (error != NULL
9328 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009329 {
9330 prepare_assert_error(&ga);
9331 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9332 &vimvars[VV_EXCEPTION].vv_tv);
9333 assert_error(&ga);
9334 ga_clear(&ga);
9335 }
9336}
9337
9338/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009339 * "assert_fails(cmd [, error])" function
9340 */
9341 static void
9342f_assert_fails(argvars, rettv)
9343 typval_T *argvars;
9344 typval_T *rettv UNUSED;
9345{
9346 char_u *cmd = get_tv_string_chk(&argvars[0]);
9347 garray_T ga;
9348
9349 called_emsg = FALSE;
9350 suppress_errthrow = TRUE;
9351 emsg_silent = TRUE;
9352 do_cmdline_cmd(cmd);
9353 if (!called_emsg)
9354 {
9355 prepare_assert_error(&ga);
9356 ga_concat(&ga, (char_u *)"command did not fail: ");
9357 ga_concat(&ga, cmd);
9358 assert_error(&ga);
9359 ga_clear(&ga);
9360 }
9361 else if (argvars[1].v_type != VAR_UNKNOWN)
9362 {
9363 char_u buf[NUMBUFLEN];
9364 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9365
9366 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9367 {
9368 prepare_assert_error(&ga);
9369 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9370 &vimvars[VV_ERRMSG].vv_tv);
9371 assert_error(&ga);
9372 ga_clear(&ga);
9373 }
9374 }
9375
9376 called_emsg = FALSE;
9377 suppress_errthrow = FALSE;
9378 emsg_silent = FALSE;
9379 emsg_on_display = FALSE;
9380 set_vim_var_string(VV_ERRMSG, NULL, 0);
9381}
9382
9383/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009384 * Common for assert_true() and assert_false().
9385 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009386 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009387assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009388 typval_T *argvars;
9389 int isTrue;
9390{
9391 int error = FALSE;
9392 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009393
9394 if (argvars[0].v_type != VAR_NUMBER
9395 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9396 || error)
9397 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009398 prepare_assert_error(&ga);
9399 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009400 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009401 NULL, &argvars[0]);
9402 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009403 ga_clear(&ga);
9404 }
9405}
9406
9407/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009408 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009409 */
9410 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009411f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009412 typval_T *argvars;
9413 typval_T *rettv UNUSED;
9414{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009415 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009416}
9417
9418/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009419 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009420 */
9421 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009422f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009423 typval_T *argvars;
9424 typval_T *rettv UNUSED;
9425{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009426 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009427}
9428
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009429#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009430/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009431 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009432 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009433 static void
9434f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009435 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009436 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009437{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009438 float_T f;
9439
9440 rettv->v_type = VAR_FLOAT;
9441 if (get_float_arg(argvars, &f) == OK)
9442 rettv->vval.v_float = asin(f);
9443 else
9444 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009445}
9446
9447/*
9448 * "atan()" function
9449 */
9450 static void
9451f_atan(argvars, rettv)
9452 typval_T *argvars;
9453 typval_T *rettv;
9454{
9455 float_T f;
9456
9457 rettv->v_type = VAR_FLOAT;
9458 if (get_float_arg(argvars, &f) == OK)
9459 rettv->vval.v_float = atan(f);
9460 else
9461 rettv->vval.v_float = 0.0;
9462}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009463
9464/*
9465 * "atan2()" function
9466 */
9467 static void
9468f_atan2(argvars, rettv)
9469 typval_T *argvars;
9470 typval_T *rettv;
9471{
9472 float_T fx, fy;
9473
9474 rettv->v_type = VAR_FLOAT;
9475 if (get_float_arg(argvars, &fx) == OK
9476 && get_float_arg(&argvars[1], &fy) == OK)
9477 rettv->vval.v_float = atan2(fx, fy);
9478 else
9479 rettv->vval.v_float = 0.0;
9480}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009481#endif
9482
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483/*
9484 * "browse(save, title, initdir, default)" function
9485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009488 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490{
9491#ifdef FEAT_BROWSE
9492 int save;
9493 char_u *title;
9494 char_u *initdir;
9495 char_u *defname;
9496 char_u buf[NUMBUFLEN];
9497 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009498 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009500 save = get_tv_number_chk(&argvars[0], &error);
9501 title = get_tv_string_chk(&argvars[1]);
9502 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9503 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009505 if (error || title == NULL || initdir == NULL || defname == NULL)
9506 rettv->vval.v_string = NULL;
9507 else
9508 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009509 do_browse(save ? BROWSE_SAVE : 0,
9510 title, defname, NULL, initdir, NULL, curbuf);
9511#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009513#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009515}
9516
9517/*
9518 * "browsedir(title, initdir)" function
9519 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009522 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009523 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009524{
9525#ifdef FEAT_BROWSE
9526 char_u *title;
9527 char_u *initdir;
9528 char_u buf[NUMBUFLEN];
9529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 title = get_tv_string_chk(&argvars[0]);
9531 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 if (title == NULL || initdir == NULL)
9534 rettv->vval.v_string = NULL;
9535 else
9536 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009537 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542}
9543
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009544static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009545
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546/*
9547 * Find a buffer by number or exact name.
9548 */
9549 static buf_T *
9550find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009551 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009555 if (avar->v_type == VAR_NUMBER)
9556 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009557 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009559 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009560 if (buf == NULL)
9561 {
9562 /* No full path name match, try a match with a URL or a "nofile"
9563 * buffer, these don't use the full path. */
9564 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9565 if (buf->b_fname != NULL
9566 && (path_with_url(buf->b_fname)
9567#ifdef FEAT_QUICKFIX
9568 || bt_nofile(buf)
9569#endif
9570 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009571 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009572 break;
9573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 }
9575 return buf;
9576}
9577
9578/*
9579 * "bufexists(expr)" function
9580 */
9581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009583 typval_T *argvars;
9584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587}
9588
9589/*
9590 * "buflisted(expr)" function
9591 */
9592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009594 typval_T *argvars;
9595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597 buf_T *buf;
9598
9599 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601}
9602
9603/*
9604 * "bufloaded(expr)" function
9605 */
9606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009607f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009608 typval_T *argvars;
9609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611 buf_T *buf;
9612
9613 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615}
9616
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009617static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009618
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619/*
9620 * Get buffer by number or pattern.
9621 */
9622 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009623get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009624 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009625 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 int save_magic;
9629 char_u *save_cpo;
9630 buf_T *buf;
9631
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632 if (tv->v_type == VAR_NUMBER)
9633 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009634 if (tv->v_type != VAR_STRING)
9635 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 if (name == NULL || *name == NUL)
9637 return curbuf;
9638 if (name[0] == '$' && name[1] == NUL)
9639 return lastbuf;
9640
9641 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9642 save_magic = p_magic;
9643 p_magic = TRUE;
9644 save_cpo = p_cpo;
9645 p_cpo = (char_u *)"";
9646
9647 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009648 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649
9650 p_magic = save_magic;
9651 p_cpo = save_cpo;
9652
9653 /* If not found, try expanding the name, like done for bufexists(). */
9654 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656
9657 return buf;
9658}
9659
9660/*
9661 * "bufname(expr)" function
9662 */
9663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009665 typval_T *argvars;
9666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667{
9668 buf_T *buf;
9669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009670 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009672 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 --emsg_off;
9679}
9680
9681/*
9682 * "bufnr(expr)" function
9683 */
9684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009686 typval_T *argvars;
9687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688{
9689 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009690 int error = FALSE;
9691 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009695 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009696 --emsg_off;
9697
9698 /* If the buffer isn't found and the second argument is not zero create a
9699 * new buffer. */
9700 if (buf == NULL
9701 && argvars[1].v_type != VAR_UNKNOWN
9702 && get_tv_number_chk(&argvars[1], &error) != 0
9703 && !error
9704 && (name = get_tv_string_chk(&argvars[0])) != NULL
9705 && !error)
9706 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9707
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712}
9713
9714/*
9715 * "bufwinnr(nr)" function
9716 */
9717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 typval_T *argvars;
9720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722#ifdef FEAT_WINDOWS
9723 win_T *wp;
9724 int winnr = 0;
9725#endif
9726 buf_T *buf;
9727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009728 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009730 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731#ifdef FEAT_WINDOWS
9732 for (wp = firstwin; wp; wp = wp->w_next)
9733 {
9734 ++winnr;
9735 if (wp->w_buffer == buf)
9736 break;
9737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741#endif
9742 --emsg_off;
9743}
9744
9745/*
9746 * "byte2line(byte)" function
9747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752{
9753#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755#else
9756 long boff = 0;
9757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009758 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 (linenr_T)0, &boff);
9764#endif
9765}
9766
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009767 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009768byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009769 typval_T *argvars;
9770 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009771 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009772{
9773#ifdef FEAT_MBYTE
9774 char_u *t;
9775#endif
9776 char_u *str;
9777 long idx;
9778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009779 str = get_tv_string_chk(&argvars[0]);
9780 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009782 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009783 return;
9784
9785#ifdef FEAT_MBYTE
9786 t = str;
9787 for ( ; idx > 0; idx--)
9788 {
9789 if (*t == NUL) /* EOL reached */
9790 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009791 if (enc_utf8 && comp)
9792 t += utf_ptr2len(t);
9793 else
9794 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009795 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009796 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009797#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009798 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009800#endif
9801}
9802
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009803/*
9804 * "byteidx()" function
9805 */
9806 static void
9807f_byteidx(argvars, rettv)
9808 typval_T *argvars;
9809 typval_T *rettv;
9810{
9811 byteidx(argvars, rettv, FALSE);
9812}
9813
9814/*
9815 * "byteidxcomp()" function
9816 */
9817 static void
9818f_byteidxcomp(argvars, rettv)
9819 typval_T *argvars;
9820 typval_T *rettv;
9821{
9822 byteidx(argvars, rettv, TRUE);
9823}
9824
Bram Moolenaardb913952012-06-29 12:54:53 +02009825 int
9826func_call(name, args, selfdict, rettv)
9827 char_u *name;
9828 typval_T *args;
9829 dict_T *selfdict;
9830 typval_T *rettv;
9831{
9832 listitem_T *item;
9833 typval_T argv[MAX_FUNC_ARGS + 1];
9834 int argc = 0;
9835 int dummy;
9836 int r = 0;
9837
9838 for (item = args->vval.v_list->lv_first; item != NULL;
9839 item = item->li_next)
9840 {
9841 if (argc == MAX_FUNC_ARGS)
9842 {
9843 EMSG(_("E699: Too many arguments"));
9844 break;
9845 }
9846 /* Make a copy of each argument. This is needed to be able to set
9847 * v_lock to VAR_FIXED in the copy without changing the original list.
9848 */
9849 copy_tv(&item->li_tv, &argv[argc++]);
9850 }
9851
9852 if (item == NULL)
9853 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9854 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9855 &dummy, TRUE, selfdict);
9856
9857 /* Free the arguments. */
9858 while (argc > 0)
9859 clear_tv(&argv[--argc]);
9860
9861 return r;
9862}
9863
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009864/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009865 * "call(func, arglist)" function
9866 */
9867 static void
9868f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009869 typval_T *argvars;
9870 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009871{
9872 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009873 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009874
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009875 if (argvars[1].v_type != VAR_LIST)
9876 {
9877 EMSG(_(e_listreq));
9878 return;
9879 }
9880 if (argvars[1].vval.v_list == NULL)
9881 return;
9882
9883 if (argvars[0].v_type == VAR_FUNC)
9884 func = argvars[0].vval.v_string;
9885 else
9886 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009887 if (*func == NUL)
9888 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009889
Bram Moolenaare9a41262005-01-15 22:18:47 +00009890 if (argvars[2].v_type != VAR_UNKNOWN)
9891 {
9892 if (argvars[2].v_type != VAR_DICT)
9893 {
9894 EMSG(_(e_dictreq));
9895 return;
9896 }
9897 selfdict = argvars[2].vval.v_dict;
9898 }
9899
Bram Moolenaardb913952012-06-29 12:54:53 +02009900 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009901}
9902
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009903#ifdef FEAT_FLOAT
9904/*
9905 * "ceil({float})" function
9906 */
9907 static void
9908f_ceil(argvars, rettv)
9909 typval_T *argvars;
9910 typval_T *rettv;
9911{
9912 float_T f;
9913
9914 rettv->v_type = VAR_FLOAT;
9915 if (get_float_arg(argvars, &f) == OK)
9916 rettv->vval.v_float = ceil(f);
9917 else
9918 rettv->vval.v_float = 0.0;
9919}
9920#endif
9921
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009922/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009923 * "changenr()" function
9924 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009925 static void
9926f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009927 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009928 typval_T *rettv;
9929{
9930 rettv->vval.v_number = curbuf->b_u_seq_cur;
9931}
9932
9933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 * "char2nr(string)" function
9935 */
9936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009938 typval_T *argvars;
9939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940{
9941#ifdef FEAT_MBYTE
9942 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009943 {
9944 int utf8 = 0;
9945
9946 if (argvars[1].v_type != VAR_UNKNOWN)
9947 utf8 = get_tv_number_chk(&argvars[1], NULL);
9948
9949 if (utf8)
9950 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9951 else
9952 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 else
9955#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957}
9958
9959/*
9960 * "cindent(lnum)" function
9961 */
9962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009963f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009964 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966{
9967#ifdef FEAT_CINDENT
9968 pos_T pos;
9969 linenr_T lnum;
9970
9971 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9974 {
9975 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 curwin->w_cursor = pos;
9978 }
9979 else
9980#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009981 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982}
9983
9984/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009985 * "clearmatches()" function
9986 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009987 static void
9988f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009989 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009990 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009991{
9992#ifdef FEAT_SEARCH_EXTRA
9993 clear_matches(curwin);
9994#endif
9995}
9996
9997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 * "col(string)" function
9999 */
10000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010002 typval_T *argvars;
10003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004{
10005 colnr_T col = 0;
10006 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010007 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010009 fp = var2fpos(&argvars[0], FALSE, &fnum);
10010 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 {
10012 if (fp->col == MAXCOL)
10013 {
10014 /* '> can be MAXCOL, get the length of the line then */
10015 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010016 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 else
10018 col = MAXCOL;
10019 }
10020 else
10021 {
10022 col = fp->col + 1;
10023#ifdef FEAT_VIRTUALEDIT
10024 /* col(".") when the cursor is on the NUL at the end of the line
10025 * because of "coladd" can be seen as an extra column. */
10026 if (virtual_active() && fp == &curwin->w_cursor)
10027 {
10028 char_u *p = ml_get_cursor();
10029
10030 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10031 curwin->w_virtcol - curwin->w_cursor.coladd))
10032 {
10033# ifdef FEAT_MBYTE
10034 int l;
10035
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010036 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 col += l;
10038# else
10039 if (*p != NUL && p[1] == NUL)
10040 ++col;
10041# endif
10042 }
10043 }
10044#endif
10045 }
10046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048}
10049
Bram Moolenaar572cb562005-08-05 21:35:02 +000010050#if defined(FEAT_INS_EXPAND)
10051/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010052 * "complete()" function
10053 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010054 static void
10055f_complete(argvars, rettv)
10056 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010057 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +000010058{
10059 int startcol;
10060
10061 if ((State & INSERT) == 0)
10062 {
10063 EMSG(_("E785: complete() can only be used in Insert mode"));
10064 return;
10065 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010066
10067 /* Check for undo allowed here, because if something was already inserted
10068 * the line was already saved for undo and this check isn't done. */
10069 if (!undo_allowed())
10070 return;
10071
Bram Moolenaarade00832006-03-10 21:46:58 +000010072 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10073 {
10074 EMSG(_(e_invarg));
10075 return;
10076 }
10077
10078 startcol = get_tv_number_chk(&argvars[0], NULL);
10079 if (startcol <= 0)
10080 return;
10081
10082 set_completion(startcol - 1, argvars[1].vval.v_list);
10083}
10084
10085/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010086 * "complete_add()" function
10087 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010088 static void
10089f_complete_add(argvars, rettv)
10090 typval_T *argvars;
10091 typval_T *rettv;
10092{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010093 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010094}
10095
10096/*
10097 * "complete_check()" function
10098 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010099 static void
10100f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010101 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +000010102 typval_T *rettv;
10103{
10104 int saved = RedrawingDisabled;
10105
10106 RedrawingDisabled = 0;
10107 ins_compl_check_keys(0);
10108 rettv->vval.v_number = compl_interrupted;
10109 RedrawingDisabled = saved;
10110}
10111#endif
10112
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113/*
10114 * "confirm(message, buttons[, default [, type]])" function
10115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010118 typval_T *argvars UNUSED;
10119 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120{
10121#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10122 char_u *message;
10123 char_u *buttons = NULL;
10124 char_u buf[NUMBUFLEN];
10125 char_u buf2[NUMBUFLEN];
10126 int def = 1;
10127 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010128 char_u *typestr;
10129 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 message = get_tv_string_chk(&argvars[0]);
10132 if (message == NULL)
10133 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010134 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010136 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10137 if (buttons == NULL)
10138 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010139 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010141 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010142 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010144 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10145 if (typestr == NULL)
10146 error = TRUE;
10147 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010149 switch (TOUPPER_ASC(*typestr))
10150 {
10151 case 'E': type = VIM_ERROR; break;
10152 case 'Q': type = VIM_QUESTION; break;
10153 case 'I': type = VIM_INFO; break;
10154 case 'W': type = VIM_WARNING; break;
10155 case 'G': type = VIM_GENERIC; break;
10156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 }
10158 }
10159 }
10160 }
10161
10162 if (buttons == NULL || *buttons == NUL)
10163 buttons = (char_u *)_("&Ok");
10164
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010165 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010166 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010167 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168#endif
10169}
10170
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010171/*
10172 * "copy()" function
10173 */
10174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010175f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010176 typval_T *argvars;
10177 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010178{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010179 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010180}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010182#ifdef FEAT_FLOAT
10183/*
10184 * "cos()" function
10185 */
10186 static void
10187f_cos(argvars, rettv)
10188 typval_T *argvars;
10189 typval_T *rettv;
10190{
10191 float_T f;
10192
10193 rettv->v_type = VAR_FLOAT;
10194 if (get_float_arg(argvars, &f) == OK)
10195 rettv->vval.v_float = cos(f);
10196 else
10197 rettv->vval.v_float = 0.0;
10198}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010199
10200/*
10201 * "cosh()" function
10202 */
10203 static void
10204f_cosh(argvars, rettv)
10205 typval_T *argvars;
10206 typval_T *rettv;
10207{
10208 float_T f;
10209
10210 rettv->v_type = VAR_FLOAT;
10211 if (get_float_arg(argvars, &f) == OK)
10212 rettv->vval.v_float = cosh(f);
10213 else
10214 rettv->vval.v_float = 0.0;
10215}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010216#endif
10217
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010219 * "count()" function
10220 */
10221 static void
10222f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010223 typval_T *argvars;
10224 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010225{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010226 long n = 0;
10227 int ic = FALSE;
10228
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010230 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010231 listitem_T *li;
10232 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010233 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010234
Bram Moolenaare9a41262005-01-15 22:18:47 +000010235 if ((l = argvars[0].vval.v_list) != NULL)
10236 {
10237 li = l->lv_first;
10238 if (argvars[2].v_type != VAR_UNKNOWN)
10239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 int error = FALSE;
10241
10242 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 if (argvars[3].v_type != VAR_UNKNOWN)
10244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010245 idx = get_tv_number_chk(&argvars[3], &error);
10246 if (!error)
10247 {
10248 li = list_find(l, idx);
10249 if (li == NULL)
10250 EMSGN(_(e_listidx), idx);
10251 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010252 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 if (error)
10254 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010255 }
10256
10257 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010258 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010259 ++n;
10260 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010261 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262 else if (argvars[0].v_type == VAR_DICT)
10263 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010264 int todo;
10265 dict_T *d;
10266 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010267
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010268 if ((d = argvars[0].vval.v_dict) != NULL)
10269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010270 int error = FALSE;
10271
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272 if (argvars[2].v_type != VAR_UNKNOWN)
10273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010275 if (argvars[3].v_type != VAR_UNKNOWN)
10276 EMSG(_(e_invarg));
10277 }
10278
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010279 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010280 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010281 {
10282 if (!HASHITEM_EMPTY(hi))
10283 {
10284 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010285 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010286 ++n;
10287 }
10288 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010289 }
10290 }
10291 else
10292 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010293 rettv->vval.v_number = n;
10294}
10295
10296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10298 *
10299 * Checks the existence of a cscope connection.
10300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010303 typval_T *argvars UNUSED;
10304 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305{
10306#ifdef FEAT_CSCOPE
10307 int num = 0;
10308 char_u *dbpath = NULL;
10309 char_u *prepend = NULL;
10310 char_u buf[NUMBUFLEN];
10311
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010312 if (argvars[0].v_type != VAR_UNKNOWN
10313 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010315 num = (int)get_tv_number(&argvars[0]);
10316 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010317 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 }
10320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010321 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322#endif
10323}
10324
10325/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010326 * "cursor(lnum, col)" function, or
10327 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010329 * Moves the cursor to the specified line and column.
10330 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010333f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010334 typval_T *argvars;
10335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336{
10337 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010338#ifdef FEAT_VIRTUALEDIT
10339 long coladd = 0;
10340#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010341 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010343 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010344 if (argvars[1].v_type == VAR_UNKNOWN)
10345 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010346 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010347 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010348
Bram Moolenaar493c1782014-05-28 14:34:46 +020010349 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010350 {
10351 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010352 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010353 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010354 line = pos.lnum;
10355 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010356#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010357 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010358#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010359 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010360 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010361 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010362 set_curswant = FALSE;
10363 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010364 }
10365 else
10366 {
10367 line = get_tv_lnum(argvars);
10368 col = get_tv_number_chk(&argvars[1], NULL);
10369#ifdef FEAT_VIRTUALEDIT
10370 if (argvars[2].v_type != VAR_UNKNOWN)
10371 coladd = get_tv_number_chk(&argvars[2], NULL);
10372#endif
10373 }
10374 if (line < 0 || col < 0
10375#ifdef FEAT_VIRTUALEDIT
10376 || coladd < 0
10377#endif
10378 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010379 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 if (line > 0)
10381 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 if (col > 0)
10383 curwin->w_cursor.col = col - 1;
10384#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010385 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386#endif
10387
10388 /* Make sure the cursor is in a valid position. */
10389 check_cursor();
10390#ifdef FEAT_MBYTE
10391 /* Correct cursor for multi-byte character. */
10392 if (has_mbyte)
10393 mb_adjust_cursor();
10394#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010395
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010396 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010397 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398}
10399
10400/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010401 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 */
10403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010404f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010405 typval_T *argvars;
10406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010408 int noref = 0;
10409
10410 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010411 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010412 if (noref < 0 || noref > 1)
10413 EMSG(_(e_invarg));
10414 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010415 {
10416 current_copyID += COPYID_INC;
10417 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419}
10420
10421/*
10422 * "delete()" function
10423 */
10424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010425f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010426 typval_T *argvars;
10427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010429 char_u nbuf[NUMBUFLEN];
10430 char_u *name;
10431 char_u *flags;
10432
10433 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010435 return;
10436
10437 name = get_tv_string(&argvars[0]);
10438 if (name == NULL || *name == NUL)
10439 {
10440 EMSG(_(e_invarg));
10441 return;
10442 }
10443
10444 if (argvars[1].v_type != VAR_UNKNOWN)
10445 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010447 flags = (char_u *)"";
10448
10449 if (*flags == NUL)
10450 /* delete a file */
10451 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10452 else if (STRCMP(flags, "d") == 0)
10453 /* delete an empty directory */
10454 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10455 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010456 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010457 rettv->vval.v_number = delete_recursive(name);
10458 else
10459 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460}
10461
10462/*
10463 * "did_filetype()" function
10464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010466f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010467 typval_T *argvars UNUSED;
10468 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469{
10470#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010471 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472#endif
10473}
10474
10475/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010476 * "diff_filler()" function
10477 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010479f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010480 typval_T *argvars UNUSED;
10481 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010482{
10483#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010485#endif
10486}
10487
10488/*
10489 * "diff_hlID()" function
10490 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010492f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010493 typval_T *argvars UNUSED;
10494 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010495{
10496#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010497 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010498 static linenr_T prev_lnum = 0;
10499 static int changedtick = 0;
10500 static int fnum = 0;
10501 static int change_start = 0;
10502 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010503 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010504 int filler_lines;
10505 int col;
10506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 if (lnum < 0) /* ignore type error in {lnum} arg */
10508 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010509 if (lnum != prev_lnum
10510 || changedtick != curbuf->b_changedtick
10511 || fnum != curbuf->b_fnum)
10512 {
10513 /* New line, buffer, change: need to get the values. */
10514 filler_lines = diff_check(curwin, lnum);
10515 if (filler_lines < 0)
10516 {
10517 if (filler_lines == -1)
10518 {
10519 change_start = MAXCOL;
10520 change_end = -1;
10521 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10522 hlID = HLF_ADD; /* added line */
10523 else
10524 hlID = HLF_CHD; /* changed line */
10525 }
10526 else
10527 hlID = HLF_ADD; /* added line */
10528 }
10529 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010530 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010531 prev_lnum = lnum;
10532 changedtick = curbuf->b_changedtick;
10533 fnum = curbuf->b_fnum;
10534 }
10535
10536 if (hlID == HLF_CHD || hlID == HLF_TXD)
10537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010539 if (col >= change_start && col <= change_end)
10540 hlID = HLF_TXD; /* changed text */
10541 else
10542 hlID = HLF_CHD; /* changed line */
10543 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010544 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010545#endif
10546}
10547
10548/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010549 * "empty({expr})" function
10550 */
10551 static void
10552f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010553 typval_T *argvars;
10554 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010555{
10556 int n;
10557
10558 switch (argvars[0].v_type)
10559 {
10560 case VAR_STRING:
10561 case VAR_FUNC:
10562 n = argvars[0].vval.v_string == NULL
10563 || *argvars[0].vval.v_string == NUL;
10564 break;
10565 case VAR_NUMBER:
10566 n = argvars[0].vval.v_number == 0;
10567 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010568#ifdef FEAT_FLOAT
10569 case VAR_FLOAT:
10570 n = argvars[0].vval.v_float == 0.0;
10571 break;
10572#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010573 case VAR_LIST:
10574 n = argvars[0].vval.v_list == NULL
10575 || argvars[0].vval.v_list->lv_first == NULL;
10576 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010577 case VAR_DICT:
10578 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010579 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010581 default:
10582 EMSG2(_(e_intern2), "f_empty()");
10583 n = 0;
10584 }
10585
10586 rettv->vval.v_number = n;
10587}
10588
10589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 * "escape({string}, {chars})" function
10591 */
10592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010594 typval_T *argvars;
10595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596{
10597 char_u buf[NUMBUFLEN];
10598
Bram Moolenaar758711c2005-02-02 23:11:38 +000010599 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10600 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602}
10603
10604/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010605 * "eval()" function
10606 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010607 static void
10608f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010609 typval_T *argvars;
10610 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010611{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010612 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 s = get_tv_string_chk(&argvars[0]);
10615 if (s != NULL)
10616 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010617
Bram Moolenaar615b9972015-01-14 17:15:05 +010010618 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010619 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10620 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010621 if (p != NULL && !aborting())
10622 EMSG2(_(e_invexpr2), p);
10623 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010624 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010625 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010626 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010627 else if (*s != NUL)
10628 EMSG(_(e_trailing));
10629}
10630
10631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 * "eventhandler()" function
10633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010636 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640}
10641
10642/*
10643 * "executable()" function
10644 */
10645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010646f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010647 typval_T *argvars;
10648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010650 char_u *name = get_tv_string(&argvars[0]);
10651
10652 /* Check in $PATH and also check directly if there is a directory name. */
10653 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10654 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010655}
10656
10657/*
10658 * "exepath()" function
10659 */
10660 static void
10661f_exepath(argvars, rettv)
10662 typval_T *argvars;
10663 typval_T *rettv;
10664{
10665 char_u *p = NULL;
10666
Bram Moolenaarb5971142015-03-21 17:32:19 +010010667 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010668 rettv->v_type = VAR_STRING;
10669 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670}
10671
10672/*
10673 * "exists()" function
10674 */
10675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010677 typval_T *argvars;
10678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679{
10680 char_u *p;
10681 char_u *name;
10682 int n = FALSE;
10683 int len = 0;
10684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 if (*p == '$') /* environment variable */
10687 {
10688 /* first try "normal" environment variables (fast) */
10689 if (mch_getenv(p + 1) != NULL)
10690 n = TRUE;
10691 else
10692 {
10693 /* try expanding things like $VIM and ${HOME} */
10694 p = expand_env_save(p);
10695 if (p != NULL && *p != '$')
10696 n = TRUE;
10697 vim_free(p);
10698 }
10699 }
10700 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010702 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010703 if (*skipwhite(p) != NUL)
10704 n = FALSE; /* trailing garbage */
10705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 else if (*p == '*') /* internal or user defined function */
10707 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010708 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 }
10710 else if (*p == ':')
10711 {
10712 n = cmd_exists(p + 1);
10713 }
10714 else if (*p == '#')
10715 {
10716#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010717 if (p[1] == '#')
10718 n = autocmd_supported(p + 2);
10719 else
10720 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721#endif
10722 }
10723 else /* internal variable */
10724 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010725 char_u *tofree;
10726 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010728 /* get_name_len() takes care of expanding curly braces */
10729 name = p;
10730 len = get_name_len(&p, &tofree, TRUE, FALSE);
10731 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010733 if (tofree != NULL)
10734 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010735 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010736 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010738 /* handle d.key, l[idx], f(expr) */
10739 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10740 if (n)
10741 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 }
10743 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010744 if (*p != NUL)
10745 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010747 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 }
10749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751}
10752
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010753#ifdef FEAT_FLOAT
10754/*
10755 * "exp()" function
10756 */
10757 static void
10758f_exp(argvars, rettv)
10759 typval_T *argvars;
10760 typval_T *rettv;
10761{
10762 float_T f;
10763
10764 rettv->v_type = VAR_FLOAT;
10765 if (get_float_arg(argvars, &f) == OK)
10766 rettv->vval.v_float = exp(f);
10767 else
10768 rettv->vval.v_float = 0.0;
10769}
10770#endif
10771
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772/*
10773 * "expand()" function
10774 */
10775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010776f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010777 typval_T *argvars;
10778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779{
10780 char_u *s;
10781 int len;
10782 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010783 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010785 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010786 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010789 if (argvars[1].v_type != VAR_UNKNOWN
10790 && argvars[2].v_type != VAR_UNKNOWN
10791 && get_tv_number_chk(&argvars[2], &error)
10792 && !error)
10793 {
10794 rettv->v_type = VAR_LIST;
10795 rettv->vval.v_list = NULL;
10796 }
10797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010798 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 if (*s == '%' || *s == '#' || *s == '<')
10800 {
10801 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010802 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010804 if (rettv->v_type == VAR_LIST)
10805 {
10806 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10807 list_append_string(rettv->vval.v_list, result, -1);
10808 else
10809 vim_free(result);
10810 }
10811 else
10812 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 }
10814 else
10815 {
10816 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010817 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010818 if (argvars[1].v_type != VAR_UNKNOWN
10819 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010820 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010821 if (!error)
10822 {
10823 ExpandInit(&xpc);
10824 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010825 if (p_wic)
10826 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010827 if (rettv->v_type == VAR_STRING)
10828 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10829 options, WILD_ALL);
10830 else if (rettv_list_alloc(rettv) != FAIL)
10831 {
10832 int i;
10833
10834 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10835 for (i = 0; i < xpc.xp_numfiles; i++)
10836 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10837 ExpandCleanup(&xpc);
10838 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010839 }
10840 else
10841 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 }
10843}
10844
10845/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010846 * Go over all entries in "d2" and add them to "d1".
10847 * When "action" is "error" then a duplicate key is an error.
10848 * When "action" is "force" then a duplicate key is overwritten.
10849 * Otherwise duplicate keys are ignored ("action" is "keep").
10850 */
10851 void
10852dict_extend(d1, d2, action)
10853 dict_T *d1;
10854 dict_T *d2;
10855 char_u *action;
10856{
10857 dictitem_T *di1;
10858 hashitem_T *hi2;
10859 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010860 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010861
10862 todo = (int)d2->dv_hashtab.ht_used;
10863 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10864 {
10865 if (!HASHITEM_EMPTY(hi2))
10866 {
10867 --todo;
10868 di1 = dict_find(d1, hi2->hi_key, -1);
10869 if (d1->dv_scope != 0)
10870 {
10871 /* Disallow replacing a builtin function in l: and g:.
10872 * Check the key to be valid when adding to any
10873 * scope. */
10874 if (d1->dv_scope == VAR_DEF_SCOPE
10875 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10876 && var_check_func_name(hi2->hi_key,
10877 di1 == NULL))
10878 break;
10879 if (!valid_varname(hi2->hi_key))
10880 break;
10881 }
10882 if (di1 == NULL)
10883 {
10884 di1 = dictitem_copy(HI2DI(hi2));
10885 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10886 dictitem_free(di1);
10887 }
10888 else if (*action == 'e')
10889 {
10890 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10891 break;
10892 }
10893 else if (*action == 'f' && HI2DI(hi2) != di1)
10894 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010895 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10896 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010897 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010898 clear_tv(&di1->di_tv);
10899 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10900 }
10901 }
10902 }
10903}
10904
10905/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010906 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010907 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010908 */
10909 static void
10910f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010911 typval_T *argvars;
10912 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010913{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010914 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010915
Bram Moolenaare9a41262005-01-15 22:18:47 +000010916 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010917 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010918 list_T *l1, *l2;
10919 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010920 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010921 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010922
Bram Moolenaare9a41262005-01-15 22:18:47 +000010923 l1 = argvars[0].vval.v_list;
10924 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010925 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010926 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010927 {
10928 if (argvars[2].v_type != VAR_UNKNOWN)
10929 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 before = get_tv_number_chk(&argvars[2], &error);
10931 if (error)
10932 return; /* type error; errmsg already given */
10933
Bram Moolenaar758711c2005-02-02 23:11:38 +000010934 if (before == l1->lv_len)
10935 item = NULL;
10936 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010938 item = list_find(l1, before);
10939 if (item == NULL)
10940 {
10941 EMSGN(_(e_listidx), before);
10942 return;
10943 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010944 }
10945 }
10946 else
10947 item = NULL;
10948 list_extend(l1, l2, item);
10949
Bram Moolenaare9a41262005-01-15 22:18:47 +000010950 copy_tv(&argvars[0], rettv);
10951 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010952 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010953 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10954 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010955 dict_T *d1, *d2;
10956 char_u *action;
10957 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010958
10959 d1 = argvars[0].vval.v_dict;
10960 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010961 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010962 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010963 {
10964 /* Check the third argument. */
10965 if (argvars[2].v_type != VAR_UNKNOWN)
10966 {
10967 static char *(av[]) = {"keep", "force", "error"};
10968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010969 action = get_tv_string_chk(&argvars[2]);
10970 if (action == NULL)
10971 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010972 for (i = 0; i < 3; ++i)
10973 if (STRCMP(action, av[i]) == 0)
10974 break;
10975 if (i == 3)
10976 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010977 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 return;
10979 }
10980 }
10981 else
10982 action = (char_u *)"force";
10983
Bram Moolenaara9922d62013-05-30 13:01:18 +020010984 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010985
Bram Moolenaare9a41262005-01-15 22:18:47 +000010986 copy_tv(&argvars[0], rettv);
10987 }
10988 }
10989 else
10990 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010991}
10992
10993/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010994 * "feedkeys()" function
10995 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010996 static void
10997f_feedkeys(argvars, rettv)
10998 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010999 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011000{
11001 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011002 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011003 char_u *keys, *flags;
11004 char_u nbuf[NUMBUFLEN];
11005 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011006 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011007 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011008
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011009 /* This is not allowed in the sandbox. If the commands would still be
11010 * executed in the sandbox it would be OK, but it probably happens later,
11011 * when "sandbox" is no longer set. */
11012 if (check_secure())
11013 return;
11014
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011015 keys = get_tv_string(&argvars[0]);
11016 if (*keys != NUL)
11017 {
11018 if (argvars[1].v_type != VAR_UNKNOWN)
11019 {
11020 flags = get_tv_string_buf(&argvars[1], nbuf);
11021 for ( ; *flags != NUL; ++flags)
11022 {
11023 switch (*flags)
11024 {
11025 case 'n': remap = FALSE; break;
11026 case 'm': remap = TRUE; break;
11027 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011028 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011029 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011030 }
11031 }
11032 }
11033
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011034 /* Need to escape K_SPECIAL and CSI before putting the string in the
11035 * typeahead buffer. */
11036 keys_esc = vim_strsave_escape_csi(keys);
11037 if (keys_esc != NULL)
11038 {
11039 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011040 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011041 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011042 if (vgetc_busy)
11043 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011044 if (execute)
11045 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011046 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011047 }
11048}
11049
11050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 * "filereadable()" function
11052 */
11053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 typval_T *argvars;
11056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011058 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 char_u *p;
11060 int n;
11061
Bram Moolenaarc236c162008-07-13 17:41:49 +000011062#ifndef O_NONBLOCK
11063# define O_NONBLOCK 0
11064#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011066 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11067 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 {
11069 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011070 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 }
11072 else
11073 n = FALSE;
11074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076}
11077
11078/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011079 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 * rights to write into.
11081 */
11082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011084 typval_T *argvars;
11085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011087 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088}
11089
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011090static void findfilendir(typval_T *argvars, typval_T *rettv, int find_what);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011091
11092 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011093findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011094 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011095 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011096 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011097{
11098#ifdef FEAT_SEARCHPATH
11099 char_u *fname;
11100 char_u *fresult = NULL;
11101 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11102 char_u *p;
11103 char_u pathbuf[NUMBUFLEN];
11104 int count = 1;
11105 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011106 int error = FALSE;
11107#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011108
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011109 rettv->vval.v_string = NULL;
11110 rettv->v_type = VAR_STRING;
11111
11112#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011114
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011115 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011117 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11118 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011119 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011120 else
11121 {
11122 if (*p != NUL)
11123 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011125 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011126 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011127 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011128 }
11129
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011130 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11131 error = TRUE;
11132
11133 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135 do
11136 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011137 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011138 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011139 fresult = find_file_in_path_option(first ? fname : NULL,
11140 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011141 0, first, path,
11142 find_what,
11143 curbuf->b_ffname,
11144 find_what == FINDFILE_DIR
11145 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011146 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011147
11148 if (fresult != NULL && rettv->v_type == VAR_LIST)
11149 list_append_string(rettv->vval.v_list, fresult, -1);
11150
11151 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011152 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011153
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011154 if (rettv->v_type == VAR_STRING)
11155 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011156#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011157}
11158
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011159static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11160static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011161
11162/*
11163 * Implementation of map() and filter().
11164 */
11165 static void
11166filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011167 typval_T *argvars;
11168 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011169 int map;
11170{
11171 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011172 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011173 listitem_T *li, *nli;
11174 list_T *l = NULL;
11175 dictitem_T *di;
11176 hashtab_T *ht;
11177 hashitem_T *hi;
11178 dict_T *d = NULL;
11179 typval_T save_val;
11180 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011181 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011182 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011183 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011184 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011185 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011186 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011187 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011188
Bram Moolenaare9a41262005-01-15 22:18:47 +000011189 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011190 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011191 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011192 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011193 return;
11194 }
11195 else if (argvars[0].v_type == VAR_DICT)
11196 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011197 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011198 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011199 return;
11200 }
11201 else
11202 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011203 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011204 return;
11205 }
11206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011207 expr = get_tv_string_buf_chk(&argvars[1], buf);
11208 /* On type errors, the preceding call has already displayed an error
11209 * message. Avoid a misleading error message for an empty string that
11210 * was not passed as argument. */
11211 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011213 prepare_vimvar(VV_VAL, &save_val);
11214 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011215
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011216 /* We reset "did_emsg" to be able to detect whether an error
11217 * occurred during evaluation of the expression. */
11218 save_did_emsg = did_emsg;
11219 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011220
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011221 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011224 vimvars[VV_KEY].vv_type = VAR_STRING;
11225
11226 ht = &d->dv_hashtab;
11227 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011228 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011229 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011231 if (!HASHITEM_EMPTY(hi))
11232 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011233 int r;
11234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 --todo;
11236 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011237 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011238 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11239 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011240 break;
11241 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011242 r = filter_map_one(&di->di_tv, expr, map, &rem);
11243 clear_tv(&vimvars[VV_KEY].vv_tv);
11244 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011245 break;
11246 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011247 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011248 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11249 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011250 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011252 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011253 }
11254 }
11255 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011256 }
11257 else
11258 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011259 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011261 for (li = l->lv_first; li != NULL; li = nli)
11262 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011263 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011264 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011265 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011266 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011267 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011268 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011269 break;
11270 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011272 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011273 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011274 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011275
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011276 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011277 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011278
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011279 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011281
11282 copy_tv(&argvars[0], rettv);
11283}
11284
11285 static int
11286filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011287 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011288 char_u *expr;
11289 int map;
11290 int *remp;
11291{
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011293 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011294 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011295
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011297 s = expr;
11298 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011299 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011300 if (*s != NUL) /* check for trailing chars after expr */
11301 {
11302 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011303 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011304 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011305 }
11306 if (map)
11307 {
11308 /* map(): replace the list item value */
11309 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011310 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011311 *tv = rettv;
11312 }
11313 else
11314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011315 int error = FALSE;
11316
Bram Moolenaare9a41262005-01-15 22:18:47 +000011317 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011319 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 /* On type error, nothing has been removed; return FAIL to stop the
11321 * loop. The error message was given by get_tv_number_chk(). */
11322 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011323 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011324 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011325 retval = OK;
11326theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011327 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011328 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011329}
11330
11331/*
11332 * "filter()" function
11333 */
11334 static void
11335f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011336 typval_T *argvars;
11337 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011338{
11339 filter_map(argvars, rettv, FALSE);
11340}
11341
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011342/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011343 * "finddir({fname}[, {path}[, {count}]])" function
11344 */
11345 static void
11346f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011347 typval_T *argvars;
11348 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011349{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011350 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011351}
11352
11353/*
11354 * "findfile({fname}[, {path}[, {count}]])" function
11355 */
11356 static void
11357f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011358 typval_T *argvars;
11359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011360{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011361 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011362}
11363
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011364#ifdef FEAT_FLOAT
11365/*
11366 * "float2nr({float})" function
11367 */
11368 static void
11369f_float2nr(argvars, rettv)
11370 typval_T *argvars;
11371 typval_T *rettv;
11372{
11373 float_T f;
11374
11375 if (get_float_arg(argvars, &f) == OK)
11376 {
11377 if (f < -0x7fffffff)
11378 rettv->vval.v_number = -0x7fffffff;
11379 else if (f > 0x7fffffff)
11380 rettv->vval.v_number = 0x7fffffff;
11381 else
11382 rettv->vval.v_number = (varnumber_T)f;
11383 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011384}
11385
11386/*
11387 * "floor({float})" function
11388 */
11389 static void
11390f_floor(argvars, rettv)
11391 typval_T *argvars;
11392 typval_T *rettv;
11393{
11394 float_T f;
11395
11396 rettv->v_type = VAR_FLOAT;
11397 if (get_float_arg(argvars, &f) == OK)
11398 rettv->vval.v_float = floor(f);
11399 else
11400 rettv->vval.v_float = 0.0;
11401}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011402
11403/*
11404 * "fmod()" function
11405 */
11406 static void
11407f_fmod(argvars, rettv)
11408 typval_T *argvars;
11409 typval_T *rettv;
11410{
11411 float_T fx, fy;
11412
11413 rettv->v_type = VAR_FLOAT;
11414 if (get_float_arg(argvars, &fx) == OK
11415 && get_float_arg(&argvars[1], &fy) == OK)
11416 rettv->vval.v_float = fmod(fx, fy);
11417 else
11418 rettv->vval.v_float = 0.0;
11419}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011420#endif
11421
Bram Moolenaar0d660222005-01-07 21:51:51 +000011422/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011423 * "fnameescape({string})" function
11424 */
11425 static void
11426f_fnameescape(argvars, rettv)
11427 typval_T *argvars;
11428 typval_T *rettv;
11429{
11430 rettv->vval.v_string = vim_strsave_fnameescape(
11431 get_tv_string(&argvars[0]), FALSE);
11432 rettv->v_type = VAR_STRING;
11433}
11434
11435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 * "fnamemodify({fname}, {mods})" function
11437 */
11438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *argvars;
11441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442{
11443 char_u *fname;
11444 char_u *mods;
11445 int usedlen = 0;
11446 int len;
11447 char_u *fbuf = NULL;
11448 char_u buf[NUMBUFLEN];
11449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011450 fname = get_tv_string_chk(&argvars[0]);
11451 mods = get_tv_string_buf_chk(&argvars[1], buf);
11452 if (fname == NULL || mods == NULL)
11453 fname = NULL;
11454 else
11455 {
11456 len = (int)STRLEN(fname);
11457 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 vim_free(fbuf);
11466}
11467
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011468static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469
11470/*
11471 * "foldclosed()" function
11472 */
11473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011475 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011476 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011477 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478{
11479#ifdef FEAT_FOLDING
11480 linenr_T lnum;
11481 linenr_T first, last;
11482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11485 {
11486 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11487 {
11488 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 return;
11493 }
11494 }
11495#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497}
11498
11499/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500 * "foldclosed()" function
11501 */
11502 static void
11503f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011504 typval_T *argvars;
11505 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011506{
11507 foldclosed_both(argvars, rettv, FALSE);
11508}
11509
11510/*
11511 * "foldclosedend()" function
11512 */
11513 static void
11514f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011515 typval_T *argvars;
11516 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011517{
11518 foldclosed_both(argvars, rettv, TRUE);
11519}
11520
11521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 * "foldlevel()" function
11523 */
11524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011525f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011526 typval_T *argvars UNUSED;
11527 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528{
11529#ifdef FEAT_FOLDING
11530 linenr_T lnum;
11531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011532 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536}
11537
11538/*
11539 * "foldtext()" function
11540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011543 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545{
11546#ifdef FEAT_FOLDING
11547 linenr_T lnum;
11548 char_u *s;
11549 char_u *r;
11550 int len;
11551 char *txt;
11552#endif
11553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 rettv->v_type = VAR_STRING;
11555 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011557 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11558 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11559 <= curbuf->b_ml.ml_line_count
11560 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 {
11562 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11564 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 {
11566 if (!linewhite(lnum))
11567 break;
11568 ++lnum;
11569 }
11570
11571 /* Find interesting text in this line. */
11572 s = skipwhite(ml_get(lnum));
11573 /* skip C comment-start */
11574 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011577 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011578 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011579 {
11580 s = skipwhite(ml_get(lnum + 1));
11581 if (*s == '*')
11582 s = skipwhite(s + 1);
11583 }
11584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 txt = _("+-%s%3ld lines: ");
11586 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011587 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 + 20 /* for %3ld */
11589 + STRLEN(s))); /* concatenated */
11590 if (r != NULL)
11591 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011592 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11593 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11594 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 len = (int)STRLEN(r);
11596 STRCAT(r, s);
11597 /* remove 'foldmarker' and 'commentstring' */
11598 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 }
11601 }
11602#endif
11603}
11604
11605/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011606 * "foldtextresult(lnum)" function
11607 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011610 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011611 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011612{
11613#ifdef FEAT_FOLDING
11614 linenr_T lnum;
11615 char_u *text;
11616 char_u buf[51];
11617 foldinfo_T foldinfo;
11618 int fold_count;
11619#endif
11620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->v_type = VAR_STRING;
11622 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011623#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011625 /* treat illegal types and illegal string values for {lnum} the same */
11626 if (lnum < 0)
11627 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011628 fold_count = foldedCount(curwin, lnum, &foldinfo);
11629 if (fold_count > 0)
11630 {
11631 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11632 &foldinfo, buf);
11633 if (text == buf)
11634 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011636 }
11637#endif
11638}
11639
11640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 * "foreground()" function
11642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011645 typval_T *argvars UNUSED;
11646 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648#ifdef FEAT_GUI
11649 if (gui.in_use)
11650 gui_mch_set_foreground();
11651#else
11652# ifdef WIN32
11653 win32_set_foreground();
11654# endif
11655#endif
11656}
11657
11658/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011659 * "function()" function
11660 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011662f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011663 typval_T *argvars;
11664 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011665{
11666 char_u *s;
11667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011669 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011670 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011671 /* Don't check an autoload name for existence here. */
11672 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011673 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011674 else
11675 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011676 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011677 {
11678 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011679 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011680
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011681 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11682 * also be called from another script. Using trans_function_name()
11683 * would also work, but some plugins depend on the name being
11684 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011685 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011686 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011687 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011688 if (rettv->vval.v_string != NULL)
11689 {
11690 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011691 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011692 }
11693 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011694 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011695 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011697 }
11698}
11699
11700/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011701 * "garbagecollect()" function
11702 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011703 static void
11704f_garbagecollect(argvars, rettv)
11705 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011706 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011707{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011708 /* This is postponed until we are back at the toplevel, because we may be
11709 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11710 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011711
11712 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11713 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011714}
11715
11716/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011717 * "get()" function
11718 */
11719 static void
11720f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011721 typval_T *argvars;
11722 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011723{
Bram Moolenaar33570922005-01-25 22:26:29 +000011724 listitem_T *li;
11725 list_T *l;
11726 dictitem_T *di;
11727 dict_T *d;
11728 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011729
Bram Moolenaare9a41262005-01-15 22:18:47 +000011730 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011731 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011732 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011734 int error = FALSE;
11735
11736 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11737 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011738 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011739 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011740 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011741 else if (argvars[0].v_type == VAR_DICT)
11742 {
11743 if ((d = argvars[0].vval.v_dict) != NULL)
11744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011745 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 if (di != NULL)
11747 tv = &di->di_tv;
11748 }
11749 }
11750 else
11751 EMSG2(_(e_listdictarg), "get()");
11752
11753 if (tv == NULL)
11754 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011755 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011756 copy_tv(&argvars[2], rettv);
11757 }
11758 else
11759 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011760}
11761
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011762static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011763
11764/*
11765 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011766 * Return a range (from start to end) of lines in rettv from the specified
11767 * buffer.
11768 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011769 */
11770 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011771get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011772 buf_T *buf;
11773 linenr_T start;
11774 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011775 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011776 typval_T *rettv;
11777{
11778 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011779
Bram Moolenaar959a1432013-12-14 12:17:38 +010011780 rettv->v_type = VAR_STRING;
11781 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011782 if (retlist && rettv_list_alloc(rettv) == FAIL)
11783 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011784
11785 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11786 return;
11787
11788 if (!retlist)
11789 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011790 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11791 p = ml_get_buf(buf, start, FALSE);
11792 else
11793 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011794 rettv->vval.v_string = vim_strsave(p);
11795 }
11796 else
11797 {
11798 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011799 return;
11800
11801 if (start < 1)
11802 start = 1;
11803 if (end > buf->b_ml.ml_line_count)
11804 end = buf->b_ml.ml_line_count;
11805 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011806 if (list_append_string(rettv->vval.v_list,
11807 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011808 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011809 }
11810}
11811
11812/*
11813 * "getbufline()" function
11814 */
11815 static void
11816f_getbufline(argvars, rettv)
11817 typval_T *argvars;
11818 typval_T *rettv;
11819{
11820 linenr_T lnum;
11821 linenr_T end;
11822 buf_T *buf;
11823
11824 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11825 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011826 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011827 --emsg_off;
11828
Bram Moolenaar661b1822005-07-28 22:36:45 +000011829 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011830 if (argvars[2].v_type == VAR_UNKNOWN)
11831 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011832 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011833 end = get_tv_lnum_buf(&argvars[2], buf);
11834
Bram Moolenaar342337a2005-07-21 21:11:17 +000011835 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011836}
11837
Bram Moolenaar0d660222005-01-07 21:51:51 +000011838/*
11839 * "getbufvar()" function
11840 */
11841 static void
11842f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011843 typval_T *argvars;
11844 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011845{
11846 buf_T *buf;
11847 buf_T *save_curbuf;
11848 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011849 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011850 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011852 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11853 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011854 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011855 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011856
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011857 rettv->v_type = VAR_STRING;
11858 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011859
11860 if (buf != NULL && varname != NULL)
11861 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011862 /* set curbuf to be our buf, temporarily */
11863 save_curbuf = curbuf;
11864 curbuf = buf;
11865
Bram Moolenaar0d660222005-01-07 21:51:51 +000011866 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011867 {
11868 if (get_option_tv(&varname, rettv, TRUE) == OK)
11869 done = TRUE;
11870 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011871 else if (STRCMP(varname, "changedtick") == 0)
11872 {
11873 rettv->v_type = VAR_NUMBER;
11874 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011875 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011876 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011877 else
11878 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011879 /* Look up the variable. */
11880 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11881 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11882 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011883 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011884 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011885 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011886 done = TRUE;
11887 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011888 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011889
11890 /* restore previous notion of curbuf */
11891 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892 }
11893
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011894 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11895 /* use the default value */
11896 copy_tv(&argvars[2], rettv);
11897
Bram Moolenaar0d660222005-01-07 21:51:51 +000011898 --emsg_off;
11899}
11900
11901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 * "getchar()" function
11903 */
11904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011905f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011906 typval_T *argvars;
11907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908{
11909 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011910 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011912 /* Position the cursor. Needed after a message that ends in a space. */
11913 windgoto(msg_row, msg_col);
11914
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 ++no_mapping;
11916 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011917 for (;;)
11918 {
11919 if (argvars[0].v_type == VAR_UNKNOWN)
11920 /* getchar(): blocking wait. */
11921 n = safe_vgetc();
11922 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11923 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011924 n = vpeekc_any();
11925 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011926 /* illegal argument or getchar(0) and no char avail: return zero */
11927 n = 0;
11928 else
11929 /* getchar(0) and char avail: return char */
11930 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011931
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011932 if (n == K_IGNORE)
11933 continue;
11934 break;
11935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 --no_mapping;
11937 --allow_keys;
11938
Bram Moolenaar219b8702006-11-01 14:32:36 +000011939 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11940 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11941 vimvars[VV_MOUSE_COL].vv_nr = 0;
11942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 if (IS_SPECIAL(n) || mod_mask != 0)
11945 {
11946 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11947 int i = 0;
11948
11949 /* Turn a special key into three bytes, plus modifier. */
11950 if (mod_mask != 0)
11951 {
11952 temp[i++] = K_SPECIAL;
11953 temp[i++] = KS_MODIFIER;
11954 temp[i++] = mod_mask;
11955 }
11956 if (IS_SPECIAL(n))
11957 {
11958 temp[i++] = K_SPECIAL;
11959 temp[i++] = K_SECOND(n);
11960 temp[i++] = K_THIRD(n);
11961 }
11962#ifdef FEAT_MBYTE
11963 else if (has_mbyte)
11964 i += (*mb_char2bytes)(n, temp + i);
11965#endif
11966 else
11967 temp[i++] = n;
11968 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011969 rettv->v_type = VAR_STRING;
11970 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011971
11972#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011973 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011974 {
11975 int row = mouse_row;
11976 int col = mouse_col;
11977 win_T *win;
11978 linenr_T lnum;
11979# ifdef FEAT_WINDOWS
11980 win_T *wp;
11981# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011982 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011983
11984 if (row >= 0 && col >= 0)
11985 {
11986 /* Find the window at the mouse coordinates and compute the
11987 * text position. */
11988 win = mouse_find_win(&row, &col);
11989 (void)mouse_comp_pos(win, &row, &col, &lnum);
11990# ifdef FEAT_WINDOWS
11991 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011992 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011993# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011994 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011995 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11996 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11997 }
11998 }
11999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 }
12001}
12002
12003/*
12004 * "getcharmod()" function
12005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012008 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012}
12013
12014/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012015 * "getcharsearch()" function
12016 */
12017 static void
12018f_getcharsearch(argvars, rettv)
12019 typval_T *argvars UNUSED;
12020 typval_T *rettv;
12021{
12022 if (rettv_dict_alloc(rettv) != FAIL)
12023 {
12024 dict_T *dict = rettv->vval.v_dict;
12025
12026 dict_add_nr_str(dict, "char", 0L, last_csearch());
12027 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12028 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12029 }
12030}
12031
12032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 * "getcmdline()" function
12034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012037 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040 rettv->v_type = VAR_STRING;
12041 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042}
12043
12044/*
12045 * "getcmdpos()" function
12046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012049 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053}
12054
12055/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012056 * "getcmdtype()" function
12057 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012058 static void
12059f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012060 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012061 typval_T *rettv;
12062{
12063 rettv->v_type = VAR_STRING;
12064 rettv->vval.v_string = alloc(2);
12065 if (rettv->vval.v_string != NULL)
12066 {
12067 rettv->vval.v_string[0] = get_cmdline_type();
12068 rettv->vval.v_string[1] = NUL;
12069 }
12070}
12071
12072/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012073 * "getcmdwintype()" function
12074 */
12075 static void
12076f_getcmdwintype(argvars, rettv)
12077 typval_T *argvars UNUSED;
12078 typval_T *rettv;
12079{
12080 rettv->v_type = VAR_STRING;
12081 rettv->vval.v_string = NULL;
12082#ifdef FEAT_CMDWIN
12083 rettv->vval.v_string = alloc(2);
12084 if (rettv->vval.v_string != NULL)
12085 {
12086 rettv->vval.v_string[0] = cmdwin_type;
12087 rettv->vval.v_string[1] = NUL;
12088 }
12089#endif
12090}
12091
12092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093 * "getcwd()" function
12094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096f_getcwd(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010012097 typval_T *argvars;
Bram Moolenaar33570922005-01-25 22:26:29 +000012098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012100 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012101 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012104 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012105
12106 wp = find_tabwin(&argvars[0], &argvars[1]);
12107 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012109 if (wp->w_localdir != NULL)
12110 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12111 else if(globaldir != NULL)
12112 rettv->vval.v_string = vim_strsave(globaldir);
12113 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012114 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012115 cwd = alloc(MAXPATHL);
12116 if (cwd != NULL)
12117 {
12118 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12119 rettv->vval.v_string = vim_strsave(cwd);
12120 vim_free(cwd);
12121 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012122 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012123#ifdef BACKSLASH_IN_FILENAME
12124 if (rettv->vval.v_string != NULL)
12125 slash_adjust(rettv->vval.v_string);
12126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127 }
12128}
12129
12130/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012131 * "getfontname()" function
12132 */
12133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012135 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012136 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012137{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138 rettv->v_type = VAR_STRING;
12139 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012140#ifdef FEAT_GUI
12141 if (gui.in_use)
12142 {
12143 GuiFont font;
12144 char_u *name = NULL;
12145
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012146 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012147 {
12148 /* Get the "Normal" font. Either the name saved by
12149 * hl_set_font_name() or from the font ID. */
12150 font = gui.norm_font;
12151 name = hl_get_font_name();
12152 }
12153 else
12154 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012156 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12157 return;
12158 font = gui_mch_get_font(name, FALSE);
12159 if (font == NOFONT)
12160 return; /* Invalid font name, return empty string. */
12161 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012162 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012163 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012164 gui_mch_free_font(font);
12165 }
12166#endif
12167}
12168
12169/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012170 * "getfperm({fname})" function
12171 */
12172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012173f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012174 typval_T *argvars;
12175 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012176{
12177 char_u *fname;
12178 struct stat st;
12179 char_u *perm = NULL;
12180 char_u flags[] = "rwx";
12181 int i;
12182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012185 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012186 if (mch_stat((char *)fname, &st) >= 0)
12187 {
12188 perm = vim_strsave((char_u *)"---------");
12189 if (perm != NULL)
12190 {
12191 for (i = 0; i < 9; i++)
12192 {
12193 if (st.st_mode & (1 << (8 - i)))
12194 perm[i] = flags[i % 3];
12195 }
12196 }
12197 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012198 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012199}
12200
12201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 * "getfsize({fname})" function
12203 */
12204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012206 typval_T *argvars;
12207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208{
12209 char_u *fname;
12210 struct stat st;
12211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012212 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215
12216 if (mch_stat((char *)fname, &st) >= 0)
12217 {
12218 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012221 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012222 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012223
12224 /* non-perfect check for overflow */
12225 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12226 rettv->vval.v_number = -2;
12227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 }
12229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012230 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231}
12232
12233/*
12234 * "getftime({fname})" function
12235 */
12236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012237f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012238 typval_T *argvars;
12239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240{
12241 char_u *fname;
12242 struct stat st;
12243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245
12246 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012247 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250}
12251
12252/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012253 * "getftype({fname})" function
12254 */
12255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012256f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012257 typval_T *argvars;
12258 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012259{
12260 char_u *fname;
12261 struct stat st;
12262 char_u *type = NULL;
12263 char *t;
12264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012265 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012268 if (mch_lstat((char *)fname, &st) >= 0)
12269 {
12270#ifdef S_ISREG
12271 if (S_ISREG(st.st_mode))
12272 t = "file";
12273 else if (S_ISDIR(st.st_mode))
12274 t = "dir";
12275# ifdef S_ISLNK
12276 else if (S_ISLNK(st.st_mode))
12277 t = "link";
12278# endif
12279# ifdef S_ISBLK
12280 else if (S_ISBLK(st.st_mode))
12281 t = "bdev";
12282# endif
12283# ifdef S_ISCHR
12284 else if (S_ISCHR(st.st_mode))
12285 t = "cdev";
12286# endif
12287# ifdef S_ISFIFO
12288 else if (S_ISFIFO(st.st_mode))
12289 t = "fifo";
12290# endif
12291# ifdef S_ISSOCK
12292 else if (S_ISSOCK(st.st_mode))
12293 t = "fifo";
12294# endif
12295 else
12296 t = "other";
12297#else
12298# ifdef S_IFMT
12299 switch (st.st_mode & S_IFMT)
12300 {
12301 case S_IFREG: t = "file"; break;
12302 case S_IFDIR: t = "dir"; break;
12303# ifdef S_IFLNK
12304 case S_IFLNK: t = "link"; break;
12305# endif
12306# ifdef S_IFBLK
12307 case S_IFBLK: t = "bdev"; break;
12308# endif
12309# ifdef S_IFCHR
12310 case S_IFCHR: t = "cdev"; break;
12311# endif
12312# ifdef S_IFIFO
12313 case S_IFIFO: t = "fifo"; break;
12314# endif
12315# ifdef S_IFSOCK
12316 case S_IFSOCK: t = "socket"; break;
12317# endif
12318 default: t = "other";
12319 }
12320# else
12321 if (mch_isdir(fname))
12322 t = "dir";
12323 else
12324 t = "file";
12325# endif
12326#endif
12327 type = vim_strsave((char_u *)t);
12328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012330}
12331
12332/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012333 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012334 */
12335 static void
12336f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012337 typval_T *argvars;
12338 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012339{
12340 linenr_T lnum;
12341 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012342 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012343
12344 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012345 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012346 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012347 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012348 retlist = FALSE;
12349 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012350 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012351 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012352 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012353 retlist = TRUE;
12354 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012355
Bram Moolenaar342337a2005-07-21 21:11:17 +000012356 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012357}
12358
12359/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012360 * "getmatches()" function
12361 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012362 static void
12363f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012364 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012365 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012366{
12367#ifdef FEAT_SEARCH_EXTRA
12368 dict_T *dict;
12369 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012370 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012371
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012372 if (rettv_list_alloc(rettv) == OK)
12373 {
12374 while (cur != NULL)
12375 {
12376 dict = dict_alloc();
12377 if (dict == NULL)
12378 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012379 if (cur->match.regprog == NULL)
12380 {
12381 /* match added with matchaddpos() */
12382 for (i = 0; i < MAXPOSMATCH; ++i)
12383 {
12384 llpos_T *llpos;
12385 char buf[6];
12386 list_T *l;
12387
12388 llpos = &cur->pos.pos[i];
12389 if (llpos->lnum == 0)
12390 break;
12391 l = list_alloc();
12392 if (l == NULL)
12393 break;
12394 list_append_number(l, (varnumber_T)llpos->lnum);
12395 if (llpos->col > 0)
12396 {
12397 list_append_number(l, (varnumber_T)llpos->col);
12398 list_append_number(l, (varnumber_T)llpos->len);
12399 }
12400 sprintf(buf, "pos%d", i + 1);
12401 dict_add_list(dict, buf, l);
12402 }
12403 }
12404 else
12405 {
12406 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12407 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012408 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012409 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12410 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012411# ifdef FEAT_CONCEAL
12412 if (cur->conceal_char)
12413 {
12414 char_u buf[MB_MAXBYTES + 1];
12415
12416 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12417 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12418 }
12419# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012420 list_append_dict(rettv->vval.v_list, dict);
12421 cur = cur->next;
12422 }
12423 }
12424#endif
12425}
12426
12427/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012428 * "getpid()" function
12429 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012430 static void
12431f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012432 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012433 typval_T *rettv;
12434{
12435 rettv->vval.v_number = mch_get_pid();
12436}
12437
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012438static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012439
12440/*
12441 * "getcurpos()" function
12442 */
12443 static void
12444f_getcurpos(argvars, rettv)
12445 typval_T *argvars;
12446 typval_T *rettv;
12447{
12448 getpos_both(argvars, rettv, TRUE);
12449}
12450
Bram Moolenaar18081e32008-02-20 19:11:07 +000012451/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012452 * "getpos(string)" function
12453 */
12454 static void
12455f_getpos(argvars, rettv)
12456 typval_T *argvars;
12457 typval_T *rettv;
12458{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012459 getpos_both(argvars, rettv, FALSE);
12460}
12461
12462 static void
12463getpos_both(argvars, rettv, getcurpos)
12464 typval_T *argvars;
12465 typval_T *rettv;
12466 int getcurpos;
12467{
Bram Moolenaara5525202006-03-02 22:52:09 +000012468 pos_T *fp;
12469 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012470 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012471
12472 if (rettv_list_alloc(rettv) == OK)
12473 {
12474 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012475 if (getcurpos)
12476 fp = &curwin->w_cursor;
12477 else
12478 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012479 if (fnum != -1)
12480 list_append_number(l, (varnumber_T)fnum);
12481 else
12482 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012483 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12484 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012485 list_append_number(l, (fp != NULL)
12486 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012487 : (varnumber_T)0);
12488 list_append_number(l,
12489#ifdef FEAT_VIRTUALEDIT
12490 (fp != NULL) ? (varnumber_T)fp->coladd :
12491#endif
12492 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012493 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012494 list_append_number(l, curwin->w_curswant == MAXCOL ?
12495 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012496 }
12497 else
12498 rettv->vval.v_number = FALSE;
12499}
12500
12501/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012502 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012503 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012504 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012505f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012506 typval_T *argvars UNUSED;
12507 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012508{
12509#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012510 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012511#endif
12512
Bram Moolenaar2641f772005-03-25 21:58:17 +000012513#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012514 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012515 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012516 wp = NULL;
12517 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12518 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012519 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012520 if (wp == NULL)
12521 return;
12522 }
12523
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012524 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012525 }
12526#endif
12527}
12528
12529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530 * "getreg()" function
12531 */
12532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012533f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 typval_T *argvars;
12535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536{
12537 char_u *strregname;
12538 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012539 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012540 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012541 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012543 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012545 strregname = get_tv_string_chk(&argvars[0]);
12546 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012547 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012548 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012549 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012550 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12551 return_list = get_tv_number_chk(&argvars[2], &error);
12552 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012555 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012556
12557 if (error)
12558 return;
12559
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560 regname = (strregname == NULL ? '"' : *strregname);
12561 if (regname == 0)
12562 regname = '"';
12563
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012564 if (return_list)
12565 {
12566 rettv->v_type = VAR_LIST;
12567 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12568 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012569 if (rettv->vval.v_list != NULL)
12570 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012571 }
12572 else
12573 {
12574 rettv->v_type = VAR_STRING;
12575 rettv->vval.v_string = get_reg_contents(regname,
12576 arg2 ? GREG_EXPR_SRC : 0);
12577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578}
12579
12580/*
12581 * "getregtype()" function
12582 */
12583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012585 typval_T *argvars;
12586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587{
12588 char_u *strregname;
12589 int regname;
12590 char_u buf[NUMBUFLEN + 2];
12591 long reglen = 0;
12592
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012593 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012594 {
12595 strregname = get_tv_string_chk(&argvars[0]);
12596 if (strregname == NULL) /* type error; errmsg already given */
12597 {
12598 rettv->v_type = VAR_STRING;
12599 rettv->vval.v_string = NULL;
12600 return;
12601 }
12602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603 else
12604 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012605 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606
12607 regname = (strregname == NULL ? '"' : *strregname);
12608 if (regname == 0)
12609 regname = '"';
12610
12611 buf[0] = NUL;
12612 buf[1] = NUL;
12613 switch (get_reg_type(regname, &reglen))
12614 {
12615 case MLINE: buf[0] = 'V'; break;
12616 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617 case MBLOCK:
12618 buf[0] = Ctrl_V;
12619 sprintf((char *)buf + 1, "%ld", reglen + 1);
12620 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012622 rettv->v_type = VAR_STRING;
12623 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624}
12625
12626/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012627 * "gettabvar()" function
12628 */
12629 static void
12630f_gettabvar(argvars, rettv)
12631 typval_T *argvars;
12632 typval_T *rettv;
12633{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012634 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012635 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012636 dictitem_T *v;
12637 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012638 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012639
12640 rettv->v_type = VAR_STRING;
12641 rettv->vval.v_string = NULL;
12642
12643 varname = get_tv_string_chk(&argvars[1]);
12644 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12645 if (tp != NULL && varname != NULL)
12646 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012647 /* Set tp to be our tabpage, temporarily. Also set the window to the
12648 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012649 if (switch_win(&oldcurwin, &oldtabpage,
12650 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012651 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012652 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012653 /* look up the variable */
12654 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12655 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12656 if (v != NULL)
12657 {
12658 copy_tv(&v->di_tv, rettv);
12659 done = TRUE;
12660 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012661 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012662
12663 /* restore previous notion of curwin */
12664 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012665 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012666
12667 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012668 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012669}
12670
12671/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012672 * "gettabwinvar()" function
12673 */
12674 static void
12675f_gettabwinvar(argvars, rettv)
12676 typval_T *argvars;
12677 typval_T *rettv;
12678{
12679 getwinvar(argvars, rettv, 1);
12680}
12681
12682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 * "getwinposx()" function
12684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012687 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012690 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691#ifdef FEAT_GUI
12692 if (gui.in_use)
12693 {
12694 int x, y;
12695
12696 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 }
12699#endif
12700}
12701
12702/*
12703 * "getwinposy()" function
12704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012706f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012707 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012710 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711#ifdef FEAT_GUI
12712 if (gui.in_use)
12713 {
12714 int x, y;
12715
12716 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 }
12719#endif
12720}
12721
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012722/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012723 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012724 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012725 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012726find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012727 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012728 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012729{
12730#ifdef FEAT_WINDOWS
12731 win_T *wp;
12732#endif
12733 int nr;
12734
12735 nr = get_tv_number_chk(vp, NULL);
12736
12737#ifdef FEAT_WINDOWS
12738 if (nr < 0)
12739 return NULL;
12740 if (nr == 0)
12741 return curwin;
12742
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012743 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12744 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012745 if (--nr <= 0)
12746 break;
12747 return wp;
12748#else
12749 if (nr == 0 || nr == 1)
12750 return curwin;
12751 return NULL;
12752#endif
12753}
12754
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012756 * Find window specified by "wvp" in tabpage "tvp".
12757 */
12758 static win_T *
12759find_tabwin(wvp, tvp)
12760 typval_T *wvp; /* VAR_UNKNOWN for current window */
12761 typval_T *tvp; /* VAR_UNKNOWN for current tab page */
12762{
12763 win_T *wp = NULL;
12764 tabpage_T *tp = NULL;
12765 long n;
12766
12767 if (wvp->v_type != VAR_UNKNOWN)
12768 {
12769 if (tvp->v_type != VAR_UNKNOWN)
12770 {
12771 n = get_tv_number(tvp);
12772 if (n >= 0)
12773 tp = find_tabpage(n);
12774 }
12775 else
12776 tp = curtab;
12777
12778 if (tp != NULL)
12779 wp = find_win_by_nr(wvp, tp);
12780 }
12781 else
12782 wp = curwin;
12783
12784 return wp;
12785}
12786
12787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 * "getwinvar()" function
12789 */
12790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012792 typval_T *argvars;
12793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012795 getwinvar(argvars, rettv, 0);
12796}
12797
12798/*
12799 * getwinvar() and gettabwinvar()
12800 */
12801 static void
12802getwinvar(argvars, rettv, off)
12803 typval_T *argvars;
12804 typval_T *rettv;
12805 int off; /* 1 for gettabwinvar() */
12806{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012807 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012809 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012810 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012811 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012812#ifdef FEAT_WINDOWS
12813 win_T *oldcurwin;
12814 tabpage_T *oldtabpage;
12815 int need_switch_win;
12816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012818#ifdef FEAT_WINDOWS
12819 if (off == 1)
12820 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12821 else
12822 tp = curtab;
12823#endif
12824 win = find_win_by_nr(&argvars[off], tp);
12825 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012828 rettv->v_type = VAR_STRING;
12829 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830
12831 if (win != NULL && varname != NULL)
12832 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012833#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012834 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012835 * otherwise the window is not valid. Only do this when needed,
12836 * autocommands get blocked. */
12837 need_switch_win = !(tp == curtab && win == curwin);
12838 if (!need_switch_win
12839 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12840#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012841 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012842 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012843 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012844 if (get_option_tv(&varname, rettv, 1) == OK)
12845 done = TRUE;
12846 }
12847 else
12848 {
12849 /* Look up the variable. */
12850 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12851 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12852 varname, FALSE);
12853 if (v != NULL)
12854 {
12855 copy_tv(&v->di_tv, rettv);
12856 done = TRUE;
12857 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012860
Bram Moolenaarba117c22015-09-29 16:53:22 +020012861#ifdef FEAT_WINDOWS
12862 if (need_switch_win)
12863 /* restore previous notion of curwin */
12864 restore_win(oldcurwin, oldtabpage, TRUE);
12865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866 }
12867
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012868 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12869 /* use the default return value */
12870 copy_tv(&argvars[off + 2], rettv);
12871
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 --emsg_off;
12873}
12874
12875/*
12876 * "glob()" function
12877 */
12878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012880 typval_T *argvars;
12881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012883 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012887 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012888 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012890 if (argvars[1].v_type != VAR_UNKNOWN)
12891 {
12892 if (get_tv_number_chk(&argvars[1], &error))
12893 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012894 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012895 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012896 if (get_tv_number_chk(&argvars[2], &error))
12897 {
12898 rettv->v_type = VAR_LIST;
12899 rettv->vval.v_list = NULL;
12900 }
12901 if (argvars[3].v_type != VAR_UNKNOWN
12902 && get_tv_number_chk(&argvars[3], &error))
12903 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012904 }
12905 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012906 if (!error)
12907 {
12908 ExpandInit(&xpc);
12909 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012910 if (p_wic)
12911 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012912 if (rettv->v_type == VAR_STRING)
12913 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012914 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012915 else if (rettv_list_alloc(rettv) != FAIL)
12916 {
12917 int i;
12918
12919 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12920 NULL, options, WILD_ALL_KEEP);
12921 for (i = 0; i < xpc.xp_numfiles; i++)
12922 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12923
12924 ExpandCleanup(&xpc);
12925 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012926 }
12927 else
12928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929}
12930
12931/*
12932 * "globpath()" function
12933 */
12934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012935f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012936 typval_T *argvars;
12937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012939 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012941 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012942 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012943 garray_T ga;
12944 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012946 /* When the optional second argument is non-zero, don't remove matches
12947 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012949 if (argvars[2].v_type != VAR_UNKNOWN)
12950 {
12951 if (get_tv_number_chk(&argvars[2], &error))
12952 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012953 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012954 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012955 if (get_tv_number_chk(&argvars[3], &error))
12956 {
12957 rettv->v_type = VAR_LIST;
12958 rettv->vval.v_list = NULL;
12959 }
12960 if (argvars[4].v_type != VAR_UNKNOWN
12961 && get_tv_number_chk(&argvars[4], &error))
12962 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012963 }
12964 }
12965 if (file != NULL && !error)
12966 {
12967 ga_init2(&ga, (int)sizeof(char_u *), 10);
12968 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12969 if (rettv->v_type == VAR_STRING)
12970 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12971 else if (rettv_list_alloc(rettv) != FAIL)
12972 for (i = 0; i < ga.ga_len; ++i)
12973 list_append_string(rettv->vval.v_list,
12974 ((char_u **)(ga.ga_data))[i], -1);
12975 ga_clear_strings(&ga);
12976 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012977 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012978 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979}
12980
12981/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012982 * "glob2regpat()" function
12983 */
12984 static void
12985f_glob2regpat(argvars, rettv)
12986 typval_T *argvars;
12987 typval_T *rettv;
12988{
12989 char_u *pat = get_tv_string_chk(&argvars[0]);
12990
12991 rettv->v_type = VAR_STRING;
12992 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12993}
12994
12995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996 * "has()" function
12997 */
12998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013000 typval_T *argvars;
13001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002{
13003 int i;
13004 char_u *name;
13005 int n = FALSE;
13006 static char *(has_list[]) =
13007 {
13008#ifdef AMIGA
13009 "amiga",
13010# ifdef FEAT_ARP
13011 "arp",
13012# endif
13013#endif
13014#ifdef __BEOS__
13015 "beos",
13016#endif
13017#ifdef MSDOS
13018# ifdef DJGPP
13019 "dos32",
13020# else
13021 "dos16",
13022# endif
13023#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013024#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 "mac",
13026#endif
13027#if defined(MACOS_X_UNIX)
13028 "macunix",
13029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030#ifdef __QNX__
13031 "qnx",
13032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033#ifdef UNIX
13034 "unix",
13035#endif
13036#ifdef VMS
13037 "vms",
13038#endif
13039#ifdef WIN16
13040 "win16",
13041#endif
13042#ifdef WIN32
13043 "win32",
13044#endif
13045#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13046 "win32unix",
13047#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013048#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 "win64",
13050#endif
13051#ifdef EBCDIC
13052 "ebcdic",
13053#endif
13054#ifndef CASE_INSENSITIVE_FILENAME
13055 "fname_case",
13056#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013057#ifdef HAVE_ACL
13058 "acl",
13059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060#ifdef FEAT_ARABIC
13061 "arabic",
13062#endif
13063#ifdef FEAT_AUTOCMD
13064 "autocmd",
13065#endif
13066#ifdef FEAT_BEVAL
13067 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013068# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13069 "balloon_multiline",
13070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071#endif
13072#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13073 "builtin_terms",
13074# ifdef ALL_BUILTIN_TCAPS
13075 "all_builtin_terms",
13076# endif
13077#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013078#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13079 || defined(FEAT_GUI_W32) \
13080 || defined(FEAT_GUI_MOTIF))
13081 "browsefilter",
13082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083#ifdef FEAT_BYTEOFF
13084 "byte_offset",
13085#endif
13086#ifdef FEAT_CINDENT
13087 "cindent",
13088#endif
13089#ifdef FEAT_CLIENTSERVER
13090 "clientserver",
13091#endif
13092#ifdef FEAT_CLIPBOARD
13093 "clipboard",
13094#endif
13095#ifdef FEAT_CMDL_COMPL
13096 "cmdline_compl",
13097#endif
13098#ifdef FEAT_CMDHIST
13099 "cmdline_hist",
13100#endif
13101#ifdef FEAT_COMMENTS
13102 "comments",
13103#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013104#ifdef FEAT_CONCEAL
13105 "conceal",
13106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107#ifdef FEAT_CRYPT
13108 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013109 "crypt-blowfish",
13110 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111#endif
13112#ifdef FEAT_CSCOPE
13113 "cscope",
13114#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013115#ifdef FEAT_CURSORBIND
13116 "cursorbind",
13117#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013118#ifdef CURSOR_SHAPE
13119 "cursorshape",
13120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121#ifdef DEBUG
13122 "debug",
13123#endif
13124#ifdef FEAT_CON_DIALOG
13125 "dialog_con",
13126#endif
13127#ifdef FEAT_GUI_DIALOG
13128 "dialog_gui",
13129#endif
13130#ifdef FEAT_DIFF
13131 "diff",
13132#endif
13133#ifdef FEAT_DIGRAPHS
13134 "digraphs",
13135#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013136#ifdef FEAT_DIRECTX
13137 "directx",
13138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139#ifdef FEAT_DND
13140 "dnd",
13141#endif
13142#ifdef FEAT_EMACS_TAGS
13143 "emacs_tags",
13144#endif
13145 "eval", /* always present, of course! */
13146#ifdef FEAT_EX_EXTRA
13147 "ex_extra",
13148#endif
13149#ifdef FEAT_SEARCH_EXTRA
13150 "extra_search",
13151#endif
13152#ifdef FEAT_FKMAP
13153 "farsi",
13154#endif
13155#ifdef FEAT_SEARCHPATH
13156 "file_in_path",
13157#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013158#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013159 "filterpipe",
13160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161#ifdef FEAT_FIND_ID
13162 "find_in_path",
13163#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013164#ifdef FEAT_FLOAT
13165 "float",
13166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167#ifdef FEAT_FOLDING
13168 "folding",
13169#endif
13170#ifdef FEAT_FOOTER
13171 "footer",
13172#endif
13173#if !defined(USE_SYSTEM) && defined(UNIX)
13174 "fork",
13175#endif
13176#ifdef FEAT_GETTEXT
13177 "gettext",
13178#endif
13179#ifdef FEAT_GUI
13180 "gui",
13181#endif
13182#ifdef FEAT_GUI_ATHENA
13183# ifdef FEAT_GUI_NEXTAW
13184 "gui_neXtaw",
13185# else
13186 "gui_athena",
13187# endif
13188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189#ifdef FEAT_GUI_GTK
13190 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013193#ifdef FEAT_GUI_GNOME
13194 "gui_gnome",
13195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196#ifdef FEAT_GUI_MAC
13197 "gui_mac",
13198#endif
13199#ifdef FEAT_GUI_MOTIF
13200 "gui_motif",
13201#endif
13202#ifdef FEAT_GUI_PHOTON
13203 "gui_photon",
13204#endif
13205#ifdef FEAT_GUI_W16
13206 "gui_win16",
13207#endif
13208#ifdef FEAT_GUI_W32
13209 "gui_win32",
13210#endif
13211#ifdef FEAT_HANGULIN
13212 "hangul_input",
13213#endif
13214#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13215 "iconv",
13216#endif
13217#ifdef FEAT_INS_EXPAND
13218 "insert_expand",
13219#endif
13220#ifdef FEAT_JUMPLIST
13221 "jumplist",
13222#endif
13223#ifdef FEAT_KEYMAP
13224 "keymap",
13225#endif
13226#ifdef FEAT_LANGMAP
13227 "langmap",
13228#endif
13229#ifdef FEAT_LIBCALL
13230 "libcall",
13231#endif
13232#ifdef FEAT_LINEBREAK
13233 "linebreak",
13234#endif
13235#ifdef FEAT_LISP
13236 "lispindent",
13237#endif
13238#ifdef FEAT_LISTCMDS
13239 "listcmds",
13240#endif
13241#ifdef FEAT_LOCALMAP
13242 "localmap",
13243#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013244#ifdef FEAT_LUA
13245# ifndef DYNAMIC_LUA
13246 "lua",
13247# endif
13248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249#ifdef FEAT_MENU
13250 "menu",
13251#endif
13252#ifdef FEAT_SESSION
13253 "mksession",
13254#endif
13255#ifdef FEAT_MODIFY_FNAME
13256 "modify_fname",
13257#endif
13258#ifdef FEAT_MOUSE
13259 "mouse",
13260#endif
13261#ifdef FEAT_MOUSESHAPE
13262 "mouseshape",
13263#endif
13264#if defined(UNIX) || defined(VMS)
13265# ifdef FEAT_MOUSE_DEC
13266 "mouse_dec",
13267# endif
13268# ifdef FEAT_MOUSE_GPM
13269 "mouse_gpm",
13270# endif
13271# ifdef FEAT_MOUSE_JSB
13272 "mouse_jsbterm",
13273# endif
13274# ifdef FEAT_MOUSE_NET
13275 "mouse_netterm",
13276# endif
13277# ifdef FEAT_MOUSE_PTERM
13278 "mouse_pterm",
13279# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013280# ifdef FEAT_MOUSE_SGR
13281 "mouse_sgr",
13282# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013283# ifdef FEAT_SYSMOUSE
13284 "mouse_sysmouse",
13285# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013286# ifdef FEAT_MOUSE_URXVT
13287 "mouse_urxvt",
13288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289# ifdef FEAT_MOUSE_XTERM
13290 "mouse_xterm",
13291# endif
13292#endif
13293#ifdef FEAT_MBYTE
13294 "multi_byte",
13295#endif
13296#ifdef FEAT_MBYTE_IME
13297 "multi_byte_ime",
13298#endif
13299#ifdef FEAT_MULTI_LANG
13300 "multi_lang",
13301#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013302#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013303#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013304 "mzscheme",
13305#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307#ifdef FEAT_OLE
13308 "ole",
13309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310#ifdef FEAT_PATH_EXTRA
13311 "path_extra",
13312#endif
13313#ifdef FEAT_PERL
13314#ifndef DYNAMIC_PERL
13315 "perl",
13316#endif
13317#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013318#ifdef FEAT_PERSISTENT_UNDO
13319 "persistent_undo",
13320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321#ifdef FEAT_PYTHON
13322#ifndef DYNAMIC_PYTHON
13323 "python",
13324#endif
13325#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013326#ifdef FEAT_PYTHON3
13327#ifndef DYNAMIC_PYTHON3
13328 "python3",
13329#endif
13330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331#ifdef FEAT_POSTSCRIPT
13332 "postscript",
13333#endif
13334#ifdef FEAT_PRINTER
13335 "printer",
13336#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013337#ifdef FEAT_PROFILE
13338 "profile",
13339#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013340#ifdef FEAT_RELTIME
13341 "reltime",
13342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343#ifdef FEAT_QUICKFIX
13344 "quickfix",
13345#endif
13346#ifdef FEAT_RIGHTLEFT
13347 "rightleft",
13348#endif
13349#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13350 "ruby",
13351#endif
13352#ifdef FEAT_SCROLLBIND
13353 "scrollbind",
13354#endif
13355#ifdef FEAT_CMDL_INFO
13356 "showcmd",
13357 "cmdline_info",
13358#endif
13359#ifdef FEAT_SIGNS
13360 "signs",
13361#endif
13362#ifdef FEAT_SMARTINDENT
13363 "smartindent",
13364#endif
13365#ifdef FEAT_SNIFF
13366 "sniff",
13367#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013368#ifdef STARTUPTIME
13369 "startuptime",
13370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371#ifdef FEAT_STL_OPT
13372 "statusline",
13373#endif
13374#ifdef FEAT_SUN_WORKSHOP
13375 "sun_workshop",
13376#endif
13377#ifdef FEAT_NETBEANS_INTG
13378 "netbeans_intg",
13379#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013380#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013381 "spell",
13382#endif
13383#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 "syntax",
13385#endif
13386#if defined(USE_SYSTEM) || !defined(UNIX)
13387 "system",
13388#endif
13389#ifdef FEAT_TAG_BINS
13390 "tag_binary",
13391#endif
13392#ifdef FEAT_TAG_OLDSTATIC
13393 "tag_old_static",
13394#endif
13395#ifdef FEAT_TAG_ANYWHITE
13396 "tag_any_white",
13397#endif
13398#ifdef FEAT_TCL
13399# ifndef DYNAMIC_TCL
13400 "tcl",
13401# endif
13402#endif
13403#ifdef TERMINFO
13404 "terminfo",
13405#endif
13406#ifdef FEAT_TERMRESPONSE
13407 "termresponse",
13408#endif
13409#ifdef FEAT_TEXTOBJ
13410 "textobjects",
13411#endif
13412#ifdef HAVE_TGETENT
13413 "tgetent",
13414#endif
13415#ifdef FEAT_TITLE
13416 "title",
13417#endif
13418#ifdef FEAT_TOOLBAR
13419 "toolbar",
13420#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013421#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13422 "unnamedplus",
13423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424#ifdef FEAT_USR_CMDS
13425 "user-commands", /* was accidentally included in 5.4 */
13426 "user_commands",
13427#endif
13428#ifdef FEAT_VIMINFO
13429 "viminfo",
13430#endif
13431#ifdef FEAT_VERTSPLIT
13432 "vertsplit",
13433#endif
13434#ifdef FEAT_VIRTUALEDIT
13435 "virtualedit",
13436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438#ifdef FEAT_VISUALEXTRA
13439 "visualextra",
13440#endif
13441#ifdef FEAT_VREPLACE
13442 "vreplace",
13443#endif
13444#ifdef FEAT_WILDIGN
13445 "wildignore",
13446#endif
13447#ifdef FEAT_WILDMENU
13448 "wildmenu",
13449#endif
13450#ifdef FEAT_WINDOWS
13451 "windows",
13452#endif
13453#ifdef FEAT_WAK
13454 "winaltkeys",
13455#endif
13456#ifdef FEAT_WRITEBACKUP
13457 "writebackup",
13458#endif
13459#ifdef FEAT_XIM
13460 "xim",
13461#endif
13462#ifdef FEAT_XFONTSET
13463 "xfontset",
13464#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013465#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013466 "xpm",
13467 "xpm_w32", /* for backward compatibility */
13468#else
13469# if defined(HAVE_XPM)
13470 "xpm",
13471# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473#ifdef USE_XSMP
13474 "xsmp",
13475#endif
13476#ifdef USE_XSMP_INTERACT
13477 "xsmp_interact",
13478#endif
13479#ifdef FEAT_XCLIPBOARD
13480 "xterm_clipboard",
13481#endif
13482#ifdef FEAT_XTERM_SAVE
13483 "xterm_save",
13484#endif
13485#if defined(UNIX) && defined(FEAT_X11)
13486 "X11",
13487#endif
13488 NULL
13489 };
13490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 for (i = 0; has_list[i] != NULL; ++i)
13493 if (STRICMP(name, has_list[i]) == 0)
13494 {
13495 n = TRUE;
13496 break;
13497 }
13498
13499 if (n == FALSE)
13500 {
13501 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013502 {
13503 if (name[5] == '-'
13504 && STRLEN(name) > 11
13505 && vim_isdigit(name[6])
13506 && vim_isdigit(name[8])
13507 && vim_isdigit(name[10]))
13508 {
13509 int major = atoi((char *)name + 6);
13510 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013511
13512 /* Expect "patch-9.9.01234". */
13513 n = (major < VIM_VERSION_MAJOR
13514 || (major == VIM_VERSION_MAJOR
13515 && (minor < VIM_VERSION_MINOR
13516 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013517 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013518 }
13519 else
13520 n = has_patch(atoi((char *)name + 5));
13521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522 else if (STRICMP(name, "vim_starting") == 0)
13523 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013524#ifdef FEAT_MBYTE
13525 else if (STRICMP(name, "multi_byte_encoding") == 0)
13526 n = has_mbyte;
13527#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013528#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13529 else if (STRICMP(name, "balloon_multiline") == 0)
13530 n = multiline_balloon_available();
13531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532#ifdef DYNAMIC_TCL
13533 else if (STRICMP(name, "tcl") == 0)
13534 n = tcl_enabled(FALSE);
13535#endif
13536#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13537 else if (STRICMP(name, "iconv") == 0)
13538 n = iconv_enabled(FALSE);
13539#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013540#ifdef DYNAMIC_LUA
13541 else if (STRICMP(name, "lua") == 0)
13542 n = lua_enabled(FALSE);
13543#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013544#ifdef DYNAMIC_MZSCHEME
13545 else if (STRICMP(name, "mzscheme") == 0)
13546 n = mzscheme_enabled(FALSE);
13547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548#ifdef DYNAMIC_RUBY
13549 else if (STRICMP(name, "ruby") == 0)
13550 n = ruby_enabled(FALSE);
13551#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013552#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553#ifdef DYNAMIC_PYTHON
13554 else if (STRICMP(name, "python") == 0)
13555 n = python_enabled(FALSE);
13556#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013557#endif
13558#ifdef FEAT_PYTHON3
13559#ifdef DYNAMIC_PYTHON3
13560 else if (STRICMP(name, "python3") == 0)
13561 n = python3_enabled(FALSE);
13562#endif
13563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564#ifdef DYNAMIC_PERL
13565 else if (STRICMP(name, "perl") == 0)
13566 n = perl_enabled(FALSE);
13567#endif
13568#ifdef FEAT_GUI
13569 else if (STRICMP(name, "gui_running") == 0)
13570 n = (gui.in_use || gui.starting);
13571# ifdef FEAT_GUI_W32
13572 else if (STRICMP(name, "gui_win32s") == 0)
13573 n = gui_is_win32s();
13574# endif
13575# ifdef FEAT_BROWSE
13576 else if (STRICMP(name, "browse") == 0)
13577 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13578# endif
13579#endif
13580#ifdef FEAT_SYN_HL
13581 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013582 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583#endif
13584#if defined(WIN3264)
13585 else if (STRICMP(name, "win95") == 0)
13586 n = mch_windows95();
13587#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013588#ifdef FEAT_NETBEANS_INTG
13589 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013590 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592 }
13593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595}
13596
13597/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013598 * "has_key()" function
13599 */
13600 static void
13601f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013602 typval_T *argvars;
13603 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013604{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013605 if (argvars[0].v_type != VAR_DICT)
13606 {
13607 EMSG(_(e_dictreq));
13608 return;
13609 }
13610 if (argvars[0].vval.v_dict == NULL)
13611 return;
13612
13613 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013614 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013615}
13616
13617/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013618 * "haslocaldir()" function
13619 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013620 static void
13621f_haslocaldir(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010013622 typval_T *argvars;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013623 typval_T *rettv;
13624{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013625 win_T *wp = NULL;
13626
13627 wp = find_tabwin(&argvars[0], &argvars[1]);
13628 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013629}
13630
13631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 * "hasmapto()" function
13633 */
13634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013636 typval_T *argvars;
13637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638{
13639 char_u *name;
13640 char_u *mode;
13641 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013642 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013645 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 mode = (char_u *)"nvo";
13647 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013650 if (argvars[2].v_type != VAR_UNKNOWN)
13651 abbr = get_tv_number(&argvars[2]);
13652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653
Bram Moolenaar2c932302006-03-18 21:42:09 +000013654 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013657 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658}
13659
13660/*
13661 * "histadd()" function
13662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013665 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667{
13668#ifdef FEAT_CMDHIST
13669 int histype;
13670 char_u *str;
13671 char_u buf[NUMBUFLEN];
13672#endif
13673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 if (check_restricted() || check_secure())
13676 return;
13677#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013678 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13679 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 if (histype >= 0)
13681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 if (*str != NUL)
13684 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013685 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013687 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 return;
13689 }
13690 }
13691#endif
13692}
13693
13694/*
13695 * "histdel()" function
13696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013698f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013699 typval_T *argvars UNUSED;
13700 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701{
13702#ifdef FEAT_CMDHIST
13703 int n;
13704 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013705 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13708 if (str == NULL)
13709 n = 0;
13710 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013712 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013713 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013715 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013716 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 else
13718 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013720 get_tv_string_buf(&argvars[1], buf));
13721 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722#endif
13723}
13724
13725/*
13726 * "histget()" function
13727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013730 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732{
13733#ifdef FEAT_CMDHIST
13734 int type;
13735 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013736 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13739 if (str == NULL)
13740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013742 {
13743 type = get_histtype(str);
13744 if (argvars[1].v_type == VAR_UNKNOWN)
13745 idx = get_history_idx(type);
13746 else
13747 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13748 /* -1 on type error */
13749 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013754 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755}
13756
13757/*
13758 * "histnr()" function
13759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013761f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013762 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764{
13765 int i;
13766
13767#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013768 char_u *history = get_tv_string_chk(&argvars[0]);
13769
13770 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 if (i >= HIST_CMD && i < HIST_COUNT)
13772 i = get_history_idx(i);
13773 else
13774#endif
13775 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777}
13778
13779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780 * "highlightID(name)" function
13781 */
13782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013784 typval_T *argvars;
13785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013787 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788}
13789
13790/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013791 * "highlight_exists()" function
13792 */
13793 static void
13794f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013795 typval_T *argvars;
13796 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013797{
13798 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13799}
13800
13801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802 * "hostname()" function
13803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013805f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013806 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808{
13809 char_u hostname[256];
13810
13811 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812 rettv->v_type = VAR_STRING;
13813 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814}
13815
13816/*
13817 * iconv() function
13818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013821 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823{
13824#ifdef FEAT_MBYTE
13825 char_u buf1[NUMBUFLEN];
13826 char_u buf2[NUMBUFLEN];
13827 char_u *from, *to, *str;
13828 vimconv_T vimconv;
13829#endif
13830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013831 rettv->v_type = VAR_STRING;
13832 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833
13834#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013835 str = get_tv_string(&argvars[0]);
13836 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13837 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838 vimconv.vc_type = CONV_NONE;
13839 convert_setup(&vimconv, from, to);
13840
13841 /* If the encodings are equal, no conversion needed. */
13842 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013843 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013845 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846
13847 convert_setup(&vimconv, NULL, NULL);
13848 vim_free(from);
13849 vim_free(to);
13850#endif
13851}
13852
13853/*
13854 * "indent()" function
13855 */
13856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013857f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013858 typval_T *argvars;
13859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860{
13861 linenr_T lnum;
13862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013863 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013865 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013867 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868}
13869
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013870/*
13871 * "index()" function
13872 */
13873 static void
13874f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013875 typval_T *argvars;
13876 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013877{
Bram Moolenaar33570922005-01-25 22:26:29 +000013878 list_T *l;
13879 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013880 long idx = 0;
13881 int ic = FALSE;
13882
13883 rettv->vval.v_number = -1;
13884 if (argvars[0].v_type != VAR_LIST)
13885 {
13886 EMSG(_(e_listreq));
13887 return;
13888 }
13889 l = argvars[0].vval.v_list;
13890 if (l != NULL)
13891 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013892 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013893 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013895 int error = FALSE;
13896
Bram Moolenaar758711c2005-02-02 23:11:38 +000013897 /* Start at specified item. Use the cached index that list_find()
13898 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013899 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013900 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013901 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013902 ic = get_tv_number_chk(&argvars[3], &error);
13903 if (error)
13904 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013905 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013906
Bram Moolenaar758711c2005-02-02 23:11:38 +000013907 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013908 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013909 {
13910 rettv->vval.v_number = idx;
13911 break;
13912 }
13913 }
13914}
13915
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916static int inputsecret_flag = 0;
13917
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013918static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013919
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013921 * This function is used by f_input() and f_inputdialog() functions. The third
13922 * argument to f_input() specifies the type of completion to use at the
13923 * prompt. The third argument to f_inputdialog() specifies the value to return
13924 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 */
13926 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013927get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013928 typval_T *argvars;
13929 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013930 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013932 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 char_u *p = NULL;
13934 int c;
13935 char_u buf[NUMBUFLEN];
13936 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013937 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013938 int xp_type = EXPAND_NOTHING;
13939 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013941 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013942 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943
13944#ifdef NO_CONSOLE_INPUT
13945 /* While starting up, there is no place to enter text. */
13946 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948#endif
13949
13950 cmd_silent = FALSE; /* Want to see the prompt. */
13951 if (prompt != NULL)
13952 {
13953 /* Only the part of the message after the last NL is considered as
13954 * prompt for the command line */
13955 p = vim_strrchr(prompt, '\n');
13956 if (p == NULL)
13957 p = prompt;
13958 else
13959 {
13960 ++p;
13961 c = *p;
13962 *p = NUL;
13963 msg_start();
13964 msg_clr_eos();
13965 msg_puts_attr(prompt, echo_attr);
13966 msg_didout = FALSE;
13967 msg_starthere();
13968 *p = c;
13969 }
13970 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013972 if (argvars[1].v_type != VAR_UNKNOWN)
13973 {
13974 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13975 if (defstr != NULL)
13976 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013978 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013979 {
13980 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013981 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013982 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013983
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013984 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013985 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013986
Bram Moolenaar4463f292005-09-25 22:20:24 +000013987 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13988 if (xp_name == NULL)
13989 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013990
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013991 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013992
Bram Moolenaar4463f292005-09-25 22:20:24 +000013993 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13994 &xp_arg) == FAIL)
13995 return;
13996 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013997 }
13998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013999 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014000 {
14001# ifdef FEAT_EX_EXTRA
14002 int save_ex_normal_busy = ex_normal_busy;
14003 ex_normal_busy = 0;
14004# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014005 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014006 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14007 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014008# ifdef FEAT_EX_EXTRA
14009 ex_normal_busy = save_ex_normal_busy;
14010# endif
14011 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014012 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014013 && argvars[1].v_type != VAR_UNKNOWN
14014 && argvars[2].v_type != VAR_UNKNOWN)
14015 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14016 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014017
14018 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014020 /* since the user typed this, no need to wait for return */
14021 need_wait_return = FALSE;
14022 msg_didout = FALSE;
14023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 cmd_silent = cmd_silent_save;
14025}
14026
14027/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014028 * "input()" function
14029 * Also handles inputsecret() when inputsecret is set.
14030 */
14031 static void
14032f_input(argvars, rettv)
14033 typval_T *argvars;
14034 typval_T *rettv;
14035{
14036 get_user_input(argvars, rettv, FALSE);
14037}
14038
14039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 * "inputdialog()" function
14041 */
14042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014044 typval_T *argvars;
14045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046{
14047#if defined(FEAT_GUI_TEXTDIALOG)
14048 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14049 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14050 {
14051 char_u *message;
14052 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014053 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014055 message = get_tv_string_chk(&argvars[0]);
14056 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014057 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014058 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 else
14060 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014061 if (message != NULL && defstr != NULL
14062 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014063 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014064 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 else
14066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014067 if (message != NULL && defstr != NULL
14068 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014069 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014070 rettv->vval.v_string = vim_strsave(
14071 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014075 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 }
14077 else
14078#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014079 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080}
14081
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014082/*
14083 * "inputlist()" function
14084 */
14085 static void
14086f_inputlist(argvars, rettv)
14087 typval_T *argvars;
14088 typval_T *rettv;
14089{
14090 listitem_T *li;
14091 int selected;
14092 int mouse_used;
14093
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014094#ifdef NO_CONSOLE_INPUT
14095 /* While starting up, there is no place to enter text. */
14096 if (no_console_input())
14097 return;
14098#endif
14099 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14100 {
14101 EMSG2(_(e_listarg), "inputlist()");
14102 return;
14103 }
14104
14105 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014106 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014107 lines_left = Rows; /* avoid more prompt */
14108 msg_scroll = TRUE;
14109 msg_clr_eos();
14110
14111 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14112 {
14113 msg_puts(get_tv_string(&li->li_tv));
14114 msg_putchar('\n');
14115 }
14116
14117 /* Ask for choice. */
14118 selected = prompt_for_number(&mouse_used);
14119 if (mouse_used)
14120 selected -= lines_left;
14121
14122 rettv->vval.v_number = selected;
14123}
14124
14125
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14127
14128/*
14129 * "inputrestore()" function
14130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014133 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135{
14136 if (ga_userinput.ga_len > 0)
14137 {
14138 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14140 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014141 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142 }
14143 else if (p_verbose > 1)
14144 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014145 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014146 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 }
14148}
14149
14150/*
14151 * "inputsave()" function
14152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014154f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014155 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014158 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159 if (ga_grow(&ga_userinput, 1) == OK)
14160 {
14161 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14162 + ga_userinput.ga_len);
14163 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014164 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165 }
14166 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014167 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168}
14169
14170/*
14171 * "inputsecret()" function
14172 */
14173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014174f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014175 typval_T *argvars;
14176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177{
14178 ++cmdline_star;
14179 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014180 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181 --cmdline_star;
14182 --inputsecret_flag;
14183}
14184
14185/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014186 * "insert()" function
14187 */
14188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014189f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014190 typval_T *argvars;
14191 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014192{
14193 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014194 listitem_T *item;
14195 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014197
14198 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014199 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014200 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014201 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014202 {
14203 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204 before = get_tv_number_chk(&argvars[2], &error);
14205 if (error)
14206 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014207
Bram Moolenaar758711c2005-02-02 23:11:38 +000014208 if (before == l->lv_len)
14209 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014210 else
14211 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014212 item = list_find(l, before);
14213 if (item == NULL)
14214 {
14215 EMSGN(_(e_listidx), before);
14216 l = NULL;
14217 }
14218 }
14219 if (l != NULL)
14220 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014221 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014222 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014223 }
14224 }
14225}
14226
14227/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014228 * "invert(expr)" function
14229 */
14230 static void
14231f_invert(argvars, rettv)
14232 typval_T *argvars;
14233 typval_T *rettv;
14234{
14235 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14236}
14237
14238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 * "isdirectory()" function
14240 */
14241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014243 typval_T *argvars;
14244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014246 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247}
14248
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014249/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014250 * "islocked()" function
14251 */
14252 static void
14253f_islocked(argvars, rettv)
14254 typval_T *argvars;
14255 typval_T *rettv;
14256{
14257 lval_T lv;
14258 char_u *end;
14259 dictitem_T *di;
14260
14261 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014262 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14263 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014264 if (end != NULL && lv.ll_name != NULL)
14265 {
14266 if (*end != NUL)
14267 EMSG(_(e_trailing));
14268 else
14269 {
14270 if (lv.ll_tv == NULL)
14271 {
14272 if (check_changedtick(lv.ll_name))
14273 rettv->vval.v_number = 1; /* always locked */
14274 else
14275 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014276 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014277 if (di != NULL)
14278 {
14279 /* Consider a variable locked when:
14280 * 1. the variable itself is locked
14281 * 2. the value of the variable is locked.
14282 * 3. the List or Dict value is locked.
14283 */
14284 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14285 || tv_islocked(&di->di_tv));
14286 }
14287 }
14288 }
14289 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014290 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014291 else if (lv.ll_newkey != NULL)
14292 EMSG2(_(e_dictkey), lv.ll_newkey);
14293 else if (lv.ll_list != NULL)
14294 /* List item. */
14295 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14296 else
14297 /* Dictionary item. */
14298 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14299 }
14300 }
14301
14302 clear_lval(&lv);
14303}
14304
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014305static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014306
14307/*
14308 * Turn a dict into a list:
14309 * "what" == 0: list of keys
14310 * "what" == 1: list of values
14311 * "what" == 2: list of items
14312 */
14313 static void
14314dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014315 typval_T *argvars;
14316 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014317 int what;
14318{
Bram Moolenaar33570922005-01-25 22:26:29 +000014319 list_T *l2;
14320 dictitem_T *di;
14321 hashitem_T *hi;
14322 listitem_T *li;
14323 listitem_T *li2;
14324 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014325 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014326
Bram Moolenaar8c711452005-01-14 21:53:12 +000014327 if (argvars[0].v_type != VAR_DICT)
14328 {
14329 EMSG(_(e_dictreq));
14330 return;
14331 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014332 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014333 return;
14334
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014335 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014336 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014337
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014338 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014339 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014340 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014341 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014342 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014343 --todo;
14344 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014345
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014346 li = listitem_alloc();
14347 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014348 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014349 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014350
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014351 if (what == 0)
14352 {
14353 /* keys() */
14354 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014355 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014356 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14357 }
14358 else if (what == 1)
14359 {
14360 /* values() */
14361 copy_tv(&di->di_tv, &li->li_tv);
14362 }
14363 else
14364 {
14365 /* items() */
14366 l2 = list_alloc();
14367 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014368 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014369 li->li_tv.vval.v_list = l2;
14370 if (l2 == NULL)
14371 break;
14372 ++l2->lv_refcount;
14373
14374 li2 = listitem_alloc();
14375 if (li2 == NULL)
14376 break;
14377 list_append(l2, li2);
14378 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014379 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014380 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14381
14382 li2 = listitem_alloc();
14383 if (li2 == NULL)
14384 break;
14385 list_append(l2, li2);
14386 copy_tv(&di->di_tv, &li2->li_tv);
14387 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014388 }
14389 }
14390}
14391
14392/*
14393 * "items(dict)" function
14394 */
14395 static void
14396f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 typval_T *argvars;
14398 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014399{
14400 dict_list(argvars, rettv, 2);
14401}
14402
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014404 * "join()" function
14405 */
14406 static void
14407f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014408 typval_T *argvars;
14409 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014410{
14411 garray_T ga;
14412 char_u *sep;
14413
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014414 if (argvars[0].v_type != VAR_LIST)
14415 {
14416 EMSG(_(e_listreq));
14417 return;
14418 }
14419 if (argvars[0].vval.v_list == NULL)
14420 return;
14421 if (argvars[1].v_type == VAR_UNKNOWN)
14422 sep = (char_u *)" ";
14423 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014424 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014425
14426 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014427
14428 if (sep != NULL)
14429 {
14430 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014431 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014432 ga_append(&ga, NUL);
14433 rettv->vval.v_string = (char_u *)ga.ga_data;
14434 }
14435 else
14436 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014437}
14438
14439/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014440 * "jsondecode()" function
14441 */
14442 static void
14443f_jsondecode(argvars, rettv)
14444 typval_T *argvars;
14445 typval_T *rettv;
14446{
14447 js_read_T reader;
14448
14449 reader.js_buf = get_tv_string(&argvars[0]);
14450 reader.js_eof = TRUE;
14451 reader.js_used = 0;
14452 json_decode(&reader, rettv);
14453}
14454
14455/*
14456 * "jsonencode()" function
14457 */
14458 static void
14459f_jsonencode(argvars, rettv)
14460 typval_T *argvars;
14461 typval_T *rettv;
14462{
14463 rettv->v_type = VAR_STRING;
14464 rettv->vval.v_string = json_encode(&argvars[0]);
14465}
14466
14467/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014468 * "keys()" function
14469 */
14470 static void
14471f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014472 typval_T *argvars;
14473 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014474{
14475 dict_list(argvars, rettv, 0);
14476}
14477
14478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 * "last_buffer_nr()" function.
14480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014482f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014483 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485{
14486 int n = 0;
14487 buf_T *buf;
14488
14489 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14490 if (n < buf->b_fnum)
14491 n = buf->b_fnum;
14492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014493 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014494}
14495
14496/*
14497 * "len()" function
14498 */
14499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014501 typval_T *argvars;
14502 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014503{
14504 switch (argvars[0].v_type)
14505 {
14506 case VAR_STRING:
14507 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 rettv->vval.v_number = (varnumber_T)STRLEN(
14509 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014510 break;
14511 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014512 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014513 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014514 case VAR_DICT:
14515 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14516 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014517 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014518 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014519 break;
14520 }
14521}
14522
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014523static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014524
14525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014527 typval_T *argvars;
14528 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014529 int type;
14530{
14531#ifdef FEAT_LIBCALL
14532 char_u *string_in;
14533 char_u **string_result;
14534 int nr_result;
14535#endif
14536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014538 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014540
14541 if (check_restricted() || check_secure())
14542 return;
14543
14544#ifdef FEAT_LIBCALL
14545 /* The first two args must be strings, otherwise its meaningless */
14546 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14547 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014548 string_in = NULL;
14549 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014550 string_in = argvars[2].vval.v_string;
14551 if (type == VAR_NUMBER)
14552 string_result = NULL;
14553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014554 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014555 if (mch_libcall(argvars[0].vval.v_string,
14556 argvars[1].vval.v_string,
14557 string_in,
14558 argvars[2].vval.v_number,
14559 string_result,
14560 &nr_result) == OK
14561 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014562 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014563 }
14564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565}
14566
14567/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014568 * "libcall()" function
14569 */
14570 static void
14571f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014572 typval_T *argvars;
14573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014574{
14575 libcall_common(argvars, rettv, VAR_STRING);
14576}
14577
14578/*
14579 * "libcallnr()" function
14580 */
14581 static void
14582f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 typval_T *argvars;
14584 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014585{
14586 libcall_common(argvars, rettv, VAR_NUMBER);
14587}
14588
14589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 * "line(string)" function
14591 */
14592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014593f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014594 typval_T *argvars;
14595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596{
14597 linenr_T lnum = 0;
14598 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014599 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014601 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 if (fp != NULL)
14603 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014604 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605}
14606
14607/*
14608 * "line2byte(lnum)" function
14609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014611f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014612 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614{
14615#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617#else
14618 linenr_T lnum;
14619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014620 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14625 if (rettv->vval.v_number >= 0)
14626 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627#endif
14628}
14629
14630/*
14631 * "lispindent(lnum)" function
14632 */
14633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014634f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637{
14638#ifdef FEAT_LISP
14639 pos_T pos;
14640 linenr_T lnum;
14641
14642 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14645 {
14646 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 curwin->w_cursor = pos;
14649 }
14650 else
14651#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014652 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653}
14654
14655/*
14656 * "localtime()" function
14657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014659f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014660 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014663 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664}
14665
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014666static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667
14668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014669get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014670 typval_T *argvars;
14671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 int exact;
14673{
14674 char_u *keys;
14675 char_u *which;
14676 char_u buf[NUMBUFLEN];
14677 char_u *keys_buf = NULL;
14678 char_u *rhs;
14679 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014680 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014681 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014682 mapblock_T *mp;
14683 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684
14685 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686 rettv->v_type = VAR_STRING;
14687 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014689 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 if (*keys == NUL)
14691 return;
14692
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014693 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014695 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014696 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014697 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014698 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014699 if (argvars[3].v_type != VAR_UNKNOWN)
14700 get_dict = get_tv_number(&argvars[3]);
14701 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 else
14704 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014705 if (which == NULL)
14706 return;
14707
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708 mode = get_map_mode(&which, 0);
14709
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014710 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014711 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014713
14714 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014716 /* Return a string. */
14717 if (rhs != NULL)
14718 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719
Bram Moolenaarbd743252010-10-20 21:23:33 +020014720 }
14721 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14722 {
14723 /* Return a dictionary. */
14724 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14725 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14726 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727
Bram Moolenaarbd743252010-10-20 21:23:33 +020014728 dict_add_nr_str(dict, "lhs", 0L, lhs);
14729 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14730 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14731 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14732 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14733 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14734 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014735 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014736 dict_add_nr_str(dict, "mode", 0L, mapmode);
14737
14738 vim_free(lhs);
14739 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740 }
14741}
14742
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014743#ifdef FEAT_FLOAT
14744/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014745 * "log()" function
14746 */
14747 static void
14748f_log(argvars, rettv)
14749 typval_T *argvars;
14750 typval_T *rettv;
14751{
14752 float_T f;
14753
14754 rettv->v_type = VAR_FLOAT;
14755 if (get_float_arg(argvars, &f) == OK)
14756 rettv->vval.v_float = log(f);
14757 else
14758 rettv->vval.v_float = 0.0;
14759}
14760
14761/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014762 * "log10()" function
14763 */
14764 static void
14765f_log10(argvars, rettv)
14766 typval_T *argvars;
14767 typval_T *rettv;
14768{
14769 float_T f;
14770
14771 rettv->v_type = VAR_FLOAT;
14772 if (get_float_arg(argvars, &f) == OK)
14773 rettv->vval.v_float = log10(f);
14774 else
14775 rettv->vval.v_float = 0.0;
14776}
14777#endif
14778
Bram Moolenaar1dced572012-04-05 16:54:08 +020014779#ifdef FEAT_LUA
14780/*
14781 * "luaeval()" function
14782 */
14783 static void
14784f_luaeval(argvars, rettv)
14785 typval_T *argvars;
14786 typval_T *rettv;
14787{
14788 char_u *str;
14789 char_u buf[NUMBUFLEN];
14790
14791 str = get_tv_string_buf(&argvars[0], buf);
14792 do_luaeval(str, argvars + 1, rettv);
14793}
14794#endif
14795
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014797 * "map()" function
14798 */
14799 static void
14800f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014801 typval_T *argvars;
14802 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014803{
14804 filter_map(argvars, rettv, TRUE);
14805}
14806
14807/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014808 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 */
14810 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014811f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014812 typval_T *argvars;
14813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014814{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014815 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816}
14817
14818/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014819 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 */
14821 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014822f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014823 typval_T *argvars;
14824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014826 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827}
14828
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014829static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830
14831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014832find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014833 typval_T *argvars;
14834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 int type;
14836{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014837 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014838 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014839 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840 char_u *pat;
14841 regmatch_T regmatch;
14842 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014843 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 char_u *save_cpo;
14845 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014846 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014847 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014848 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014849 list_T *l = NULL;
14850 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014851 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014852 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853
14854 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14855 save_cpo = p_cpo;
14856 p_cpo = (char_u *)"";
14857
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014858 rettv->vval.v_number = -1;
14859 if (type == 3)
14860 {
14861 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014862 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014863 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014864 }
14865 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014867 rettv->v_type = VAR_STRING;
14868 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014871 if (argvars[0].v_type == VAR_LIST)
14872 {
14873 if ((l = argvars[0].vval.v_list) == NULL)
14874 goto theend;
14875 li = l->lv_first;
14876 }
14877 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014878 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014879 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014880 len = (long)STRLEN(str);
14881 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014883 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14884 if (pat == NULL)
14885 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014886
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014887 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014889 int error = FALSE;
14890
14891 start = get_tv_number_chk(&argvars[2], &error);
14892 if (error)
14893 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014894 if (l != NULL)
14895 {
14896 li = list_find(l, start);
14897 if (li == NULL)
14898 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014899 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014900 }
14901 else
14902 {
14903 if (start < 0)
14904 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014905 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014906 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014907 /* When "count" argument is there ignore matches before "start",
14908 * otherwise skip part of the string. Differs when pattern is "^"
14909 * or "\<". */
14910 if (argvars[3].v_type != VAR_UNKNOWN)
14911 startcol = start;
14912 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014913 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014914 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014915 len -= start;
14916 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014917 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014918
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014919 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014920 nth = get_tv_number_chk(&argvars[3], &error);
14921 if (error)
14922 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923 }
14924
14925 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14926 if (regmatch.regprog != NULL)
14927 {
14928 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014929
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014930 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014931 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014932 if (l != NULL)
14933 {
14934 if (li == NULL)
14935 {
14936 match = FALSE;
14937 break;
14938 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014939 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014940 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014941 if (str == NULL)
14942 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014943 }
14944
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014945 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014946
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014947 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014948 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014949 if (l == NULL && !match)
14950 break;
14951
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014952 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014953 if (l != NULL)
14954 {
14955 li = li->li_next;
14956 ++idx;
14957 }
14958 else
14959 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014960#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014961 startcol = (colnr_T)(regmatch.startp[0]
14962 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014963#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014964 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014965#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014966 if (startcol > (colnr_T)len
14967 || str + startcol <= regmatch.startp[0])
14968 {
14969 match = FALSE;
14970 break;
14971 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014972 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014973 }
14974
14975 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014977 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014978 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014979 int i;
14980
14981 /* return list with matched string and submatches */
14982 for (i = 0; i < NSUBEXP; ++i)
14983 {
14984 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014985 {
14986 if (list_append_string(rettv->vval.v_list,
14987 (char_u *)"", 0) == FAIL)
14988 break;
14989 }
14990 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014991 regmatch.startp[i],
14992 (int)(regmatch.endp[i] - regmatch.startp[i]))
14993 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014994 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014995 }
14996 }
14997 else if (type == 2)
14998 {
14999 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015000 if (l != NULL)
15001 copy_tv(&li->li_tv, rettv);
15002 else
15003 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015005 }
15006 else if (l != NULL)
15007 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 else
15009 {
15010 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015011 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012 (varnumber_T)(regmatch.startp[0] - str);
15013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015014 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015016 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 }
15018 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015019 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 }
15021
15022theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015023 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 p_cpo = save_cpo;
15025}
15026
15027/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015028 * "match()" function
15029 */
15030 static void
15031f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 typval_T *argvars;
15033 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015034{
15035 find_some_match(argvars, rettv, 1);
15036}
15037
15038/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015039 * "matchadd()" function
15040 */
15041 static void
15042f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015043 typval_T *argvars UNUSED;
15044 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015045{
15046#ifdef FEAT_SEARCH_EXTRA
15047 char_u buf[NUMBUFLEN];
15048 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15049 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15050 int prio = 10; /* default priority */
15051 int id = -1;
15052 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015053 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015054
15055 rettv->vval.v_number = -1;
15056
15057 if (grp == NULL || pat == NULL)
15058 return;
15059 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015060 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015061 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015062 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015063 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015064 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015065 if (argvars[4].v_type != VAR_UNKNOWN)
15066 {
15067 if (argvars[4].v_type != VAR_DICT)
15068 {
15069 EMSG(_(e_dictreq));
15070 return;
15071 }
15072 if (dict_find(argvars[4].vval.v_dict,
15073 (char_u *)"conceal", -1) != NULL)
15074 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15075 (char_u *)"conceal", FALSE);
15076 }
15077 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015078 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015079 if (error == TRUE)
15080 return;
15081 if (id >= 1 && id <= 3)
15082 {
15083 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15084 return;
15085 }
15086
Bram Moolenaar6561d522015-07-21 15:48:27 +020015087 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15088 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015089#endif
15090}
15091
15092/*
15093 * "matchaddpos()" function
15094 */
15095 static void
15096f_matchaddpos(argvars, rettv)
15097 typval_T *argvars UNUSED;
15098 typval_T *rettv UNUSED;
15099{
15100#ifdef FEAT_SEARCH_EXTRA
15101 char_u buf[NUMBUFLEN];
15102 char_u *group;
15103 int prio = 10;
15104 int id = -1;
15105 int error = FALSE;
15106 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015107 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015108
15109 rettv->vval.v_number = -1;
15110
15111 group = get_tv_string_buf_chk(&argvars[0], buf);
15112 if (group == NULL)
15113 return;
15114
15115 if (argvars[1].v_type != VAR_LIST)
15116 {
15117 EMSG2(_(e_listarg), "matchaddpos()");
15118 return;
15119 }
15120 l = argvars[1].vval.v_list;
15121 if (l == NULL)
15122 return;
15123
15124 if (argvars[2].v_type != VAR_UNKNOWN)
15125 {
15126 prio = get_tv_number_chk(&argvars[2], &error);
15127 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015128 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015129 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015130 if (argvars[4].v_type != VAR_UNKNOWN)
15131 {
15132 if (argvars[4].v_type != VAR_DICT)
15133 {
15134 EMSG(_(e_dictreq));
15135 return;
15136 }
15137 if (dict_find(argvars[4].vval.v_dict,
15138 (char_u *)"conceal", -1) != NULL)
15139 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15140 (char_u *)"conceal", FALSE);
15141 }
15142 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015143 }
15144 if (error == TRUE)
15145 return;
15146
15147 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15148 if (id == 1 || id == 2)
15149 {
15150 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15151 return;
15152 }
15153
Bram Moolenaar6561d522015-07-21 15:48:27 +020015154 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15155 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015156#endif
15157}
15158
15159/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015160 * "matcharg()" function
15161 */
15162 static void
15163f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015164 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015165 typval_T *rettv;
15166{
15167 if (rettv_list_alloc(rettv) == OK)
15168 {
15169#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015170 int id = get_tv_number(&argvars[0]);
15171 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015172
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015173 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015174 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015175 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15176 {
15177 list_append_string(rettv->vval.v_list,
15178 syn_id2name(m->hlg_id), -1);
15179 list_append_string(rettv->vval.v_list, m->pattern, -1);
15180 }
15181 else
15182 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015183 list_append_string(rettv->vval.v_list, NULL, -1);
15184 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015185 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015186 }
15187#endif
15188 }
15189}
15190
15191/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015192 * "matchdelete()" function
15193 */
15194 static void
15195f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015196 typval_T *argvars UNUSED;
15197 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015198{
15199#ifdef FEAT_SEARCH_EXTRA
15200 rettv->vval.v_number = match_delete(curwin,
15201 (int)get_tv_number(&argvars[0]), TRUE);
15202#endif
15203}
15204
15205/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015206 * "matchend()" function
15207 */
15208 static void
15209f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 typval_T *argvars;
15211 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015212{
15213 find_some_match(argvars, rettv, 0);
15214}
15215
15216/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015217 * "matchlist()" function
15218 */
15219 static void
15220f_matchlist(argvars, rettv)
15221 typval_T *argvars;
15222 typval_T *rettv;
15223{
15224 find_some_match(argvars, rettv, 3);
15225}
15226
15227/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015228 * "matchstr()" function
15229 */
15230 static void
15231f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015232 typval_T *argvars;
15233 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015234{
15235 find_some_match(argvars, rettv, 2);
15236}
15237
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015238static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015239
15240 static void
15241max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015242 typval_T *argvars;
15243 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015244 int domax;
15245{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015246 long n = 0;
15247 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015248 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015249
15250 if (argvars[0].v_type == VAR_LIST)
15251 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015252 list_T *l;
15253 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015254
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015255 l = argvars[0].vval.v_list;
15256 if (l != NULL)
15257 {
15258 li = l->lv_first;
15259 if (li != NULL)
15260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015261 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015262 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015263 {
15264 li = li->li_next;
15265 if (li == NULL)
15266 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015267 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015268 if (domax ? i > n : i < n)
15269 n = i;
15270 }
15271 }
15272 }
15273 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015274 else if (argvars[0].v_type == VAR_DICT)
15275 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015276 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015277 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015278 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015279 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015280
15281 d = argvars[0].vval.v_dict;
15282 if (d != NULL)
15283 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015284 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015285 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015286 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015287 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015288 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015289 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015290 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015291 if (first)
15292 {
15293 n = i;
15294 first = FALSE;
15295 }
15296 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015297 n = i;
15298 }
15299 }
15300 }
15301 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015302 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015303 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015304 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015305}
15306
15307/*
15308 * "max()" function
15309 */
15310 static void
15311f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015312 typval_T *argvars;
15313 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015314{
15315 max_min(argvars, rettv, TRUE);
15316}
15317
15318/*
15319 * "min()" function
15320 */
15321 static void
15322f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015323 typval_T *argvars;
15324 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015325{
15326 max_min(argvars, rettv, FALSE);
15327}
15328
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015329static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015330
15331/*
15332 * Create the directory in which "dir" is located, and higher levels when
15333 * needed.
15334 */
15335 static int
15336mkdir_recurse(dir, prot)
15337 char_u *dir;
15338 int prot;
15339{
15340 char_u *p;
15341 char_u *updir;
15342 int r = FAIL;
15343
15344 /* Get end of directory name in "dir".
15345 * We're done when it's "/" or "c:/". */
15346 p = gettail_sep(dir);
15347 if (p <= get_past_head(dir))
15348 return OK;
15349
15350 /* If the directory exists we're done. Otherwise: create it.*/
15351 updir = vim_strnsave(dir, (int)(p - dir));
15352 if (updir == NULL)
15353 return FAIL;
15354 if (mch_isdir(updir))
15355 r = OK;
15356 else if (mkdir_recurse(updir, prot) == OK)
15357 r = vim_mkdir_emsg(updir, prot);
15358 vim_free(updir);
15359 return r;
15360}
15361
15362#ifdef vim_mkdir
15363/*
15364 * "mkdir()" function
15365 */
15366 static void
15367f_mkdir(argvars, rettv)
15368 typval_T *argvars;
15369 typval_T *rettv;
15370{
15371 char_u *dir;
15372 char_u buf[NUMBUFLEN];
15373 int prot = 0755;
15374
15375 rettv->vval.v_number = FAIL;
15376 if (check_restricted() || check_secure())
15377 return;
15378
15379 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015380 if (*dir == NUL)
15381 rettv->vval.v_number = FAIL;
15382 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015383 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015384 if (*gettail(dir) == NUL)
15385 /* remove trailing slashes */
15386 *gettail_sep(dir) = NUL;
15387
15388 if (argvars[1].v_type != VAR_UNKNOWN)
15389 {
15390 if (argvars[2].v_type != VAR_UNKNOWN)
15391 prot = get_tv_number_chk(&argvars[2], NULL);
15392 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15393 mkdir_recurse(dir, prot);
15394 }
15395 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015396 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015397}
15398#endif
15399
Bram Moolenaar0d660222005-01-07 21:51:51 +000015400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 * "mode()" function
15402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015405 typval_T *argvars;
15406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015408 char_u buf[3];
15409
15410 buf[1] = NUL;
15411 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 if (VIsual_active)
15414 {
15415 if (VIsual_select)
15416 buf[0] = VIsual_mode + 's' - 'v';
15417 else
15418 buf[0] = VIsual_mode;
15419 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015420 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015421 || State == CONFIRM)
15422 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015424 if (State == ASKMORE)
15425 buf[1] = 'm';
15426 else if (State == CONFIRM)
15427 buf[1] = '?';
15428 }
15429 else if (State == EXTERNCMD)
15430 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431 else if (State & INSERT)
15432 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015433#ifdef FEAT_VREPLACE
15434 if (State & VREPLACE_FLAG)
15435 {
15436 buf[0] = 'R';
15437 buf[1] = 'v';
15438 }
15439 else
15440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 if (State & REPLACE_FLAG)
15442 buf[0] = 'R';
15443 else
15444 buf[0] = 'i';
15445 }
15446 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015449 if (exmode_active)
15450 buf[1] = 'v';
15451 }
15452 else if (exmode_active)
15453 {
15454 buf[0] = 'c';
15455 buf[1] = 'e';
15456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015458 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015460 if (finish_op)
15461 buf[1] = 'o';
15462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015464 /* Clear out the minor mode when the argument is not a non-zero number or
15465 * non-empty string. */
15466 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015467 buf[1] = NUL;
15468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015469 rettv->vval.v_string = vim_strsave(buf);
15470 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471}
15472
Bram Moolenaar429fa852013-04-15 12:27:36 +020015473#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015474/*
15475 * "mzeval()" function
15476 */
15477 static void
15478f_mzeval(argvars, rettv)
15479 typval_T *argvars;
15480 typval_T *rettv;
15481{
15482 char_u *str;
15483 char_u buf[NUMBUFLEN];
15484
15485 str = get_tv_string_buf(&argvars[0], buf);
15486 do_mzeval(str, rettv);
15487}
Bram Moolenaar75676462013-01-30 14:55:42 +010015488
15489 void
15490mzscheme_call_vim(name, args, rettv)
15491 char_u *name;
15492 typval_T *args;
15493 typval_T *rettv;
15494{
15495 typval_T argvars[3];
15496
15497 argvars[0].v_type = VAR_STRING;
15498 argvars[0].vval.v_string = name;
15499 copy_tv(args, &argvars[1]);
15500 argvars[2].v_type = VAR_UNKNOWN;
15501 f_call(argvars, rettv);
15502 clear_tv(&argvars[1]);
15503}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015504#endif
15505
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015507 * "nextnonblank()" function
15508 */
15509 static void
15510f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015511 typval_T *argvars;
15512 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015513{
15514 linenr_T lnum;
15515
15516 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015518 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015519 {
15520 lnum = 0;
15521 break;
15522 }
15523 if (*skipwhite(ml_get(lnum)) != NUL)
15524 break;
15525 }
15526 rettv->vval.v_number = lnum;
15527}
15528
15529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 * "nr2char()" function
15531 */
15532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015533f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015534 typval_T *argvars;
15535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536{
15537 char_u buf[NUMBUFLEN];
15538
15539#ifdef FEAT_MBYTE
15540 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015541 {
15542 int utf8 = 0;
15543
15544 if (argvars[1].v_type != VAR_UNKNOWN)
15545 utf8 = get_tv_number_chk(&argvars[1], NULL);
15546 if (utf8)
15547 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15548 else
15549 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 else
15552#endif
15553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 buf[1] = NUL;
15556 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557 rettv->v_type = VAR_STRING;
15558 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015559}
15560
15561/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015562 * "or(expr, expr)" function
15563 */
15564 static void
15565f_or(argvars, rettv)
15566 typval_T *argvars;
15567 typval_T *rettv;
15568{
15569 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15570 | get_tv_number_chk(&argvars[1], NULL);
15571}
15572
15573/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015574 * "pathshorten()" function
15575 */
15576 static void
15577f_pathshorten(argvars, rettv)
15578 typval_T *argvars;
15579 typval_T *rettv;
15580{
15581 char_u *p;
15582
15583 rettv->v_type = VAR_STRING;
15584 p = get_tv_string_chk(&argvars[0]);
15585 if (p == NULL)
15586 rettv->vval.v_string = NULL;
15587 else
15588 {
15589 p = vim_strsave(p);
15590 rettv->vval.v_string = p;
15591 if (p != NULL)
15592 shorten_dir(p);
15593 }
15594}
15595
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015596#ifdef FEAT_PERL
15597/*
15598 * "perleval()" function
15599 */
15600 static void
15601f_perleval(argvars, rettv)
15602 typval_T *argvars;
15603 typval_T *rettv;
15604{
15605 char_u *str;
15606 char_u buf[NUMBUFLEN];
15607
15608 str = get_tv_string_buf(&argvars[0], buf);
15609 do_perleval(str, rettv);
15610}
15611#endif
15612
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015613#ifdef FEAT_FLOAT
15614/*
15615 * "pow()" function
15616 */
15617 static void
15618f_pow(argvars, rettv)
15619 typval_T *argvars;
15620 typval_T *rettv;
15621{
15622 float_T fx, fy;
15623
15624 rettv->v_type = VAR_FLOAT;
15625 if (get_float_arg(argvars, &fx) == OK
15626 && get_float_arg(&argvars[1], &fy) == OK)
15627 rettv->vval.v_float = pow(fx, fy);
15628 else
15629 rettv->vval.v_float = 0.0;
15630}
15631#endif
15632
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015633/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015634 * "prevnonblank()" function
15635 */
15636 static void
15637f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015638 typval_T *argvars;
15639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640{
15641 linenr_T lnum;
15642
15643 lnum = get_tv_lnum(argvars);
15644 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15645 lnum = 0;
15646 else
15647 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15648 --lnum;
15649 rettv->vval.v_number = lnum;
15650}
15651
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015652#ifdef HAVE_STDARG_H
15653/* This dummy va_list is here because:
15654 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15655 * - locally in the function results in a "used before set" warning
15656 * - using va_start() to initialize it gives "function with fixed args" error */
15657static va_list ap;
15658#endif
15659
Bram Moolenaar8c711452005-01-14 21:53:12 +000015660/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015661 * "printf()" function
15662 */
15663 static void
15664f_printf(argvars, rettv)
15665 typval_T *argvars;
15666 typval_T *rettv;
15667{
15668 rettv->v_type = VAR_STRING;
15669 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015670#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015671 {
15672 char_u buf[NUMBUFLEN];
15673 int len;
15674 char_u *s;
15675 int saved_did_emsg = did_emsg;
15676 char *fmt;
15677
15678 /* Get the required length, allocate the buffer and do it for real. */
15679 did_emsg = FALSE;
15680 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015681 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015682 if (!did_emsg)
15683 {
15684 s = alloc(len + 1);
15685 if (s != NULL)
15686 {
15687 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015688 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015689 }
15690 }
15691 did_emsg |= saved_did_emsg;
15692 }
15693#endif
15694}
15695
15696/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015697 * "pumvisible()" function
15698 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015699 static void
15700f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015701 typval_T *argvars UNUSED;
15702 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015703{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015704#ifdef FEAT_INS_EXPAND
15705 if (pum_visible())
15706 rettv->vval.v_number = 1;
15707#endif
15708}
15709
Bram Moolenaardb913952012-06-29 12:54:53 +020015710#ifdef FEAT_PYTHON3
15711/*
15712 * "py3eval()" function
15713 */
15714 static void
15715f_py3eval(argvars, rettv)
15716 typval_T *argvars;
15717 typval_T *rettv;
15718{
15719 char_u *str;
15720 char_u buf[NUMBUFLEN];
15721
15722 str = get_tv_string_buf(&argvars[0], buf);
15723 do_py3eval(str, rettv);
15724}
15725#endif
15726
15727#ifdef FEAT_PYTHON
15728/*
15729 * "pyeval()" function
15730 */
15731 static void
15732f_pyeval(argvars, rettv)
15733 typval_T *argvars;
15734 typval_T *rettv;
15735{
15736 char_u *str;
15737 char_u buf[NUMBUFLEN];
15738
15739 str = get_tv_string_buf(&argvars[0], buf);
15740 do_pyeval(str, rettv);
15741}
15742#endif
15743
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015744/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015745 * "range()" function
15746 */
15747 static void
15748f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015749 typval_T *argvars;
15750 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015751{
15752 long start;
15753 long end;
15754 long stride = 1;
15755 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015756 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015758 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015759 if (argvars[1].v_type == VAR_UNKNOWN)
15760 {
15761 end = start - 1;
15762 start = 0;
15763 }
15764 else
15765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015766 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015767 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015768 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015769 }
15770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015771 if (error)
15772 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015773 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015774 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015775 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015776 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015777 else
15778 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015779 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015780 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015781 if (list_append_number(rettv->vval.v_list,
15782 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015783 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015784 }
15785}
15786
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015787/*
15788 * "readfile()" function
15789 */
15790 static void
15791f_readfile(argvars, rettv)
15792 typval_T *argvars;
15793 typval_T *rettv;
15794{
15795 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015796 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015797 char_u *fname;
15798 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015799 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15800 int io_size = sizeof(buf);
15801 int readlen; /* size of last fread() */
15802 char_u *prev = NULL; /* previously read bytes, if any */
15803 long prevlen = 0; /* length of data in prev */
15804 long prevsize = 0; /* size of prev buffer */
15805 long maxline = MAXLNUM;
15806 long cnt = 0;
15807 char_u *p; /* position in buf */
15808 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015809
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015810 if (argvars[1].v_type != VAR_UNKNOWN)
15811 {
15812 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15813 binary = TRUE;
15814 if (argvars[2].v_type != VAR_UNKNOWN)
15815 maxline = get_tv_number(&argvars[2]);
15816 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015817
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015818 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015819 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015820
15821 /* Always open the file in binary mode, library functions have a mind of
15822 * their own about CR-LF conversion. */
15823 fname = get_tv_string(&argvars[0]);
15824 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15825 {
15826 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15827 return;
15828 }
15829
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015830 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015831 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015832 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015833
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015834 /* This for loop processes what was read, but is also entered at end
15835 * of file so that either:
15836 * - an incomplete line gets written
15837 * - a "binary" file gets an empty line at the end if it ends in a
15838 * newline. */
15839 for (p = buf, start = buf;
15840 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15841 ++p)
15842 {
15843 if (*p == '\n' || readlen <= 0)
15844 {
15845 listitem_T *li;
15846 char_u *s = NULL;
15847 long_u len = p - start;
15848
15849 /* Finished a line. Remove CRs before NL. */
15850 if (readlen > 0 && !binary)
15851 {
15852 while (len > 0 && start[len - 1] == '\r')
15853 --len;
15854 /* removal may cross back to the "prev" string */
15855 if (len == 0)
15856 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15857 --prevlen;
15858 }
15859 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015860 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015861 else
15862 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015863 /* Change "prev" buffer to be the right size. This way
15864 * the bytes are only copied once, and very long lines are
15865 * allocated only once. */
15866 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015867 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015868 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015869 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015870 prev = NULL; /* the list will own the string */
15871 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015872 }
15873 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015874 if (s == NULL)
15875 {
15876 do_outofmem_msg((long_u) prevlen + len + 1);
15877 failed = TRUE;
15878 break;
15879 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015880
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015881 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015882 {
15883 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015884 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015885 break;
15886 }
15887 li->li_tv.v_type = VAR_STRING;
15888 li->li_tv.v_lock = 0;
15889 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015890 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015891
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015892 start = p + 1; /* step over newline */
15893 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015894 break;
15895 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015896 else if (*p == NUL)
15897 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015898#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015899 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15900 * when finding the BF and check the previous two bytes. */
15901 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015902 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015903 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15904 * + 1, these may be in the "prev" string. */
15905 char_u back1 = p >= buf + 1 ? p[-1]
15906 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15907 char_u back2 = p >= buf + 2 ? p[-2]
15908 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15909 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015910
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015911 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015912 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015913 char_u *dest = p - 2;
15914
15915 /* Usually a BOM is at the beginning of a file, and so at
15916 * the beginning of a line; then we can just step over it.
15917 */
15918 if (start == dest)
15919 start = p + 1;
15920 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015921 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015922 /* have to shuffle buf to close gap */
15923 int adjust_prevlen = 0;
15924
15925 if (dest < buf)
15926 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015927 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015928 dest = buf;
15929 }
15930 if (readlen > p - buf + 1)
15931 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15932 readlen -= 3 - adjust_prevlen;
15933 prevlen -= adjust_prevlen;
15934 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015935 }
15936 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015937 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015938#endif
15939 } /* for */
15940
15941 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15942 break;
15943 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015944 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015945 /* There's part of a line in buf, store it in "prev". */
15946 if (p - start + prevlen >= prevsize)
15947 {
15948 /* need bigger "prev" buffer */
15949 char_u *newprev;
15950
15951 /* A common use case is ordinary text files and "prev" gets a
15952 * fragment of a line, so the first allocation is made
15953 * small, to avoid repeatedly 'allocing' large and
15954 * 'reallocing' small. */
15955 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015956 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015957 else
15958 {
15959 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015960 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015961 prevsize = grow50pc > growmin ? grow50pc : growmin;
15962 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015963 newprev = prev == NULL ? alloc(prevsize)
15964 : vim_realloc(prev, prevsize);
15965 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015966 {
15967 do_outofmem_msg((long_u)prevsize);
15968 failed = TRUE;
15969 break;
15970 }
15971 prev = newprev;
15972 }
15973 /* Add the line part to end of "prev". */
15974 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015975 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015976 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015977 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015978
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015979 /*
15980 * For a negative line count use only the lines at the end of the file,
15981 * free the rest.
15982 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015983 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015984 while (cnt > -maxline)
15985 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015986 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015987 --cnt;
15988 }
15989
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015990 if (failed)
15991 {
15992 list_free(rettv->vval.v_list, TRUE);
15993 /* readfile doc says an empty list is returned on error */
15994 rettv->vval.v_list = list_alloc();
15995 }
15996
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015997 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015998 fclose(fd);
15999}
16000
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016001#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016002static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016003
16004/*
16005 * Convert a List to proftime_T.
16006 * Return FAIL when there is something wrong.
16007 */
16008 static int
16009list2proftime(arg, tm)
16010 typval_T *arg;
16011 proftime_T *tm;
16012{
16013 long n1, n2;
16014 int error = FALSE;
16015
16016 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16017 || arg->vval.v_list->lv_len != 2)
16018 return FAIL;
16019 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16020 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16021# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016022 tm->HighPart = n1;
16023 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016024# else
16025 tm->tv_sec = n1;
16026 tm->tv_usec = n2;
16027# endif
16028 return error ? FAIL : OK;
16029}
16030#endif /* FEAT_RELTIME */
16031
16032/*
16033 * "reltime()" function
16034 */
16035 static void
16036f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016037 typval_T *argvars UNUSED;
16038 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016039{
16040#ifdef FEAT_RELTIME
16041 proftime_T res;
16042 proftime_T start;
16043
16044 if (argvars[0].v_type == VAR_UNKNOWN)
16045 {
16046 /* No arguments: get current time. */
16047 profile_start(&res);
16048 }
16049 else if (argvars[1].v_type == VAR_UNKNOWN)
16050 {
16051 if (list2proftime(&argvars[0], &res) == FAIL)
16052 return;
16053 profile_end(&res);
16054 }
16055 else
16056 {
16057 /* Two arguments: compute the difference. */
16058 if (list2proftime(&argvars[0], &start) == FAIL
16059 || list2proftime(&argvars[1], &res) == FAIL)
16060 return;
16061 profile_sub(&res, &start);
16062 }
16063
16064 if (rettv_list_alloc(rettv) == OK)
16065 {
16066 long n1, n2;
16067
16068# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016069 n1 = res.HighPart;
16070 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016071# else
16072 n1 = res.tv_sec;
16073 n2 = res.tv_usec;
16074# endif
16075 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16076 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16077 }
16078#endif
16079}
16080
16081/*
16082 * "reltimestr()" function
16083 */
16084 static void
16085f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016086 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016087 typval_T *rettv;
16088{
16089#ifdef FEAT_RELTIME
16090 proftime_T tm;
16091#endif
16092
16093 rettv->v_type = VAR_STRING;
16094 rettv->vval.v_string = NULL;
16095#ifdef FEAT_RELTIME
16096 if (list2proftime(&argvars[0], &tm) == OK)
16097 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16098#endif
16099}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016100
Bram Moolenaar0d660222005-01-07 21:51:51 +000016101#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016102static void make_connection(void);
16103static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016104
16105 static void
16106make_connection()
16107{
16108 if (X_DISPLAY == NULL
16109# ifdef FEAT_GUI
16110 && !gui.in_use
16111# endif
16112 )
16113 {
16114 x_force_connect = TRUE;
16115 setup_term_clip();
16116 x_force_connect = FALSE;
16117 }
16118}
16119
16120 static int
16121check_connection()
16122{
16123 make_connection();
16124 if (X_DISPLAY == NULL)
16125 {
16126 EMSG(_("E240: No connection to Vim server"));
16127 return FAIL;
16128 }
16129 return OK;
16130}
16131#endif
16132
16133#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016134static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016135
16136 static void
16137remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000016138 typval_T *argvars;
16139 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016140 int expr;
16141{
16142 char_u *server_name;
16143 char_u *keys;
16144 char_u *r = NULL;
16145 char_u buf[NUMBUFLEN];
16146# ifdef WIN32
16147 HWND w;
16148# else
16149 Window w;
16150# endif
16151
16152 if (check_restricted() || check_secure())
16153 return;
16154
16155# ifdef FEAT_X11
16156 if (check_connection() == FAIL)
16157 return;
16158# endif
16159
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016160 server_name = get_tv_string_chk(&argvars[0]);
16161 if (server_name == NULL)
16162 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016163 keys = get_tv_string_buf(&argvars[1], buf);
16164# ifdef WIN32
16165 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16166# else
16167 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16168 < 0)
16169# endif
16170 {
16171 if (r != NULL)
16172 EMSG(r); /* sending worked but evaluation failed */
16173 else
16174 EMSG2(_("E241: Unable to send to %s"), server_name);
16175 return;
16176 }
16177
16178 rettv->vval.v_string = r;
16179
16180 if (argvars[2].v_type != VAR_UNKNOWN)
16181 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016182 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016183 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016184 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016186 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016187 v.di_tv.v_type = VAR_STRING;
16188 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016189 idvar = get_tv_string_chk(&argvars[2]);
16190 if (idvar != NULL)
16191 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016192 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016193 }
16194}
16195#endif
16196
16197/*
16198 * "remote_expr()" function
16199 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200 static void
16201f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016202 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016203 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016204{
16205 rettv->v_type = VAR_STRING;
16206 rettv->vval.v_string = NULL;
16207#ifdef FEAT_CLIENTSERVER
16208 remote_common(argvars, rettv, TRUE);
16209#endif
16210}
16211
16212/*
16213 * "remote_foreground()" function
16214 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016215 static void
16216f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016217 typval_T *argvars UNUSED;
16218 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016220#ifdef FEAT_CLIENTSERVER
16221# ifdef WIN32
16222 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016223 {
16224 char_u *server_name = get_tv_string_chk(&argvars[0]);
16225
16226 if (server_name != NULL)
16227 serverForeground(server_name);
16228 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229# else
16230 /* Send a foreground() expression to the server. */
16231 argvars[1].v_type = VAR_STRING;
16232 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16233 argvars[2].v_type = VAR_UNKNOWN;
16234 remote_common(argvars, rettv, TRUE);
16235 vim_free(argvars[1].vval.v_string);
16236# endif
16237#endif
16238}
16239
Bram Moolenaar0d660222005-01-07 21:51:51 +000016240 static void
16241f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016242 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016243 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244{
16245#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016246 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016247 char_u *s = NULL;
16248# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016249 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016250# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016251 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252
16253 if (check_restricted() || check_secure())
16254 {
16255 rettv->vval.v_number = -1;
16256 return;
16257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016258 serverid = get_tv_string_chk(&argvars[0]);
16259 if (serverid == NULL)
16260 {
16261 rettv->vval.v_number = -1;
16262 return; /* type error; errmsg already given */
16263 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016264# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016265 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016266 if (n == 0)
16267 rettv->vval.v_number = -1;
16268 else
16269 {
16270 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16271 rettv->vval.v_number = (s != NULL);
16272 }
16273# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016274 if (check_connection() == FAIL)
16275 return;
16276
16277 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016278 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016279# endif
16280
16281 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016283 char_u *retvar;
16284
Bram Moolenaar33570922005-01-25 22:26:29 +000016285 v.di_tv.v_type = VAR_STRING;
16286 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016287 retvar = get_tv_string_chk(&argvars[1]);
16288 if (retvar != NULL)
16289 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016290 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016291 }
16292#else
16293 rettv->vval.v_number = -1;
16294#endif
16295}
16296
Bram Moolenaar0d660222005-01-07 21:51:51 +000016297 static void
16298f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016299 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016300 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016301{
16302 char_u *r = NULL;
16303
16304#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016305 char_u *serverid = get_tv_string_chk(&argvars[0]);
16306
16307 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308 {
16309# ifdef WIN32
16310 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016311 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016313 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016314 if (n != 0)
16315 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16316 if (r == NULL)
16317# else
16318 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016319 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016320# endif
16321 EMSG(_("E277: Unable to read a server reply"));
16322 }
16323#endif
16324 rettv->v_type = VAR_STRING;
16325 rettv->vval.v_string = r;
16326}
16327
16328/*
16329 * "remote_send()" function
16330 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016331 static void
16332f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016333 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016334 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016335{
16336 rettv->v_type = VAR_STRING;
16337 rettv->vval.v_string = NULL;
16338#ifdef FEAT_CLIENTSERVER
16339 remote_common(argvars, rettv, FALSE);
16340#endif
16341}
16342
16343/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016344 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016345 */
16346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016347f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016348 typval_T *argvars;
16349 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016350{
Bram Moolenaar33570922005-01-25 22:26:29 +000016351 list_T *l;
16352 listitem_T *item, *item2;
16353 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016354 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016355 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016356 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016357 dict_T *d;
16358 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016359 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016360
Bram Moolenaar8c711452005-01-14 21:53:12 +000016361 if (argvars[0].v_type == VAR_DICT)
16362 {
16363 if (argvars[2].v_type != VAR_UNKNOWN)
16364 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016365 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016366 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016368 key = get_tv_string_chk(&argvars[1]);
16369 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016371 di = dict_find(d, key, -1);
16372 if (di == NULL)
16373 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016374 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16375 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016376 {
16377 *rettv = di->di_tv;
16378 init_tv(&di->di_tv);
16379 dictitem_remove(d, di);
16380 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016381 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016382 }
16383 }
16384 else if (argvars[0].v_type != VAR_LIST)
16385 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016386 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016387 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016389 int error = FALSE;
16390
16391 idx = get_tv_number_chk(&argvars[1], &error);
16392 if (error)
16393 ; /* type error: do nothing, errmsg already given */
16394 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016395 EMSGN(_(e_listidx), idx);
16396 else
16397 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016398 if (argvars[2].v_type == VAR_UNKNOWN)
16399 {
16400 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016401 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016402 *rettv = item->li_tv;
16403 vim_free(item);
16404 }
16405 else
16406 {
16407 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016408 end = get_tv_number_chk(&argvars[2], &error);
16409 if (error)
16410 ; /* type error: do nothing */
16411 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016412 EMSGN(_(e_listidx), end);
16413 else
16414 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016415 int cnt = 0;
16416
16417 for (li = item; li != NULL; li = li->li_next)
16418 {
16419 ++cnt;
16420 if (li == item2)
16421 break;
16422 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016423 if (li == NULL) /* didn't find "item2" after "item" */
16424 EMSG(_(e_invrange));
16425 else
16426 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016427 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016428 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016429 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016430 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016431 l->lv_first = item;
16432 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016433 item->li_prev = NULL;
16434 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016435 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016436 }
16437 }
16438 }
16439 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016440 }
16441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442}
16443
16444/*
16445 * "rename({from}, {to})" function
16446 */
16447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016448f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 typval_T *argvars;
16450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451{
16452 char_u buf[NUMBUFLEN];
16453
16454 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016455 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016457 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16458 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459}
16460
16461/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016462 * "repeat()" function
16463 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016464 static void
16465f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016466 typval_T *argvars;
16467 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016468{
16469 char_u *p;
16470 int n;
16471 int slen;
16472 int len;
16473 char_u *r;
16474 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016475
16476 n = get_tv_number(&argvars[1]);
16477 if (argvars[0].v_type == VAR_LIST)
16478 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016479 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016480 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016481 if (list_extend(rettv->vval.v_list,
16482 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016483 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016484 }
16485 else
16486 {
16487 p = get_tv_string(&argvars[0]);
16488 rettv->v_type = VAR_STRING;
16489 rettv->vval.v_string = NULL;
16490
16491 slen = (int)STRLEN(p);
16492 len = slen * n;
16493 if (len <= 0)
16494 return;
16495
16496 r = alloc(len + 1);
16497 if (r != NULL)
16498 {
16499 for (i = 0; i < n; i++)
16500 mch_memmove(r + i * slen, p, (size_t)slen);
16501 r[len] = NUL;
16502 }
16503
16504 rettv->vval.v_string = r;
16505 }
16506}
16507
16508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509 * "resolve()" function
16510 */
16511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016512f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 typval_T *argvars;
16514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515{
16516 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016517#ifdef HAVE_READLINK
16518 char_u *buf = NULL;
16519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016521 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522#ifdef FEAT_SHORTCUT
16523 {
16524 char_u *v = NULL;
16525
16526 v = mch_resolve_shortcut(p);
16527 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016528 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016530 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 }
16532#else
16533# ifdef HAVE_READLINK
16534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 char_u *cpy;
16536 int len;
16537 char_u *remain = NULL;
16538 char_u *q;
16539 int is_relative_to_current = FALSE;
16540 int has_trailing_pathsep = FALSE;
16541 int limit = 100;
16542
16543 p = vim_strsave(p);
16544
16545 if (p[0] == '.' && (vim_ispathsep(p[1])
16546 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16547 is_relative_to_current = TRUE;
16548
16549 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016550 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016553 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555
16556 q = getnextcomp(p);
16557 if (*q != NUL)
16558 {
16559 /* Separate the first path component in "p", and keep the
16560 * remainder (beginning with the path separator). */
16561 remain = vim_strsave(q - 1);
16562 q[-1] = NUL;
16563 }
16564
Bram Moolenaard9462e32011-04-11 21:35:11 +020016565 buf = alloc(MAXPATHL + 1);
16566 if (buf == NULL)
16567 goto fail;
16568
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 for (;;)
16570 {
16571 for (;;)
16572 {
16573 len = readlink((char *)p, (char *)buf, MAXPATHL);
16574 if (len <= 0)
16575 break;
16576 buf[len] = NUL;
16577
16578 if (limit-- == 0)
16579 {
16580 vim_free(p);
16581 vim_free(remain);
16582 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016583 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 goto fail;
16585 }
16586
16587 /* Ensure that the result will have a trailing path separator
16588 * if the argument has one. */
16589 if (remain == NULL && has_trailing_pathsep)
16590 add_pathsep(buf);
16591
16592 /* Separate the first path component in the link value and
16593 * concatenate the remainders. */
16594 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16595 if (*q != NUL)
16596 {
16597 if (remain == NULL)
16598 remain = vim_strsave(q - 1);
16599 else
16600 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016601 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602 if (cpy != NULL)
16603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 vim_free(remain);
16605 remain = cpy;
16606 }
16607 }
16608 q[-1] = NUL;
16609 }
16610
16611 q = gettail(p);
16612 if (q > p && *q == NUL)
16613 {
16614 /* Ignore trailing path separator. */
16615 q[-1] = NUL;
16616 q = gettail(p);
16617 }
16618 if (q > p && !mch_isFullName(buf))
16619 {
16620 /* symlink is relative to directory of argument */
16621 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16622 if (cpy != NULL)
16623 {
16624 STRCPY(cpy, p);
16625 STRCPY(gettail(cpy), buf);
16626 vim_free(p);
16627 p = cpy;
16628 }
16629 }
16630 else
16631 {
16632 vim_free(p);
16633 p = vim_strsave(buf);
16634 }
16635 }
16636
16637 if (remain == NULL)
16638 break;
16639
16640 /* Append the first path component of "remain" to "p". */
16641 q = getnextcomp(remain + 1);
16642 len = q - remain - (*q != NUL);
16643 cpy = vim_strnsave(p, STRLEN(p) + len);
16644 if (cpy != NULL)
16645 {
16646 STRNCAT(cpy, remain, len);
16647 vim_free(p);
16648 p = cpy;
16649 }
16650 /* Shorten "remain". */
16651 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016652 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 else
16654 {
16655 vim_free(remain);
16656 remain = NULL;
16657 }
16658 }
16659
16660 /* If the result is a relative path name, make it explicitly relative to
16661 * the current directory if and only if the argument had this form. */
16662 if (!vim_ispathsep(*p))
16663 {
16664 if (is_relative_to_current
16665 && *p != NUL
16666 && !(p[0] == '.'
16667 && (p[1] == NUL
16668 || vim_ispathsep(p[1])
16669 || (p[1] == '.'
16670 && (p[2] == NUL
16671 || vim_ispathsep(p[2]))))))
16672 {
16673 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016674 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 if (cpy != NULL)
16676 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 vim_free(p);
16678 p = cpy;
16679 }
16680 }
16681 else if (!is_relative_to_current)
16682 {
16683 /* Strip leading "./". */
16684 q = p;
16685 while (q[0] == '.' && vim_ispathsep(q[1]))
16686 q += 2;
16687 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016688 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689 }
16690 }
16691
16692 /* Ensure that the result will have no trailing path separator
16693 * if the argument had none. But keep "/" or "//". */
16694 if (!has_trailing_pathsep)
16695 {
16696 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016697 if (after_pathsep(p, q))
16698 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 }
16700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016701 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 }
16703# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016704 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705# endif
16706#endif
16707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016708 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709
16710#ifdef HAVE_READLINK
16711fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016712 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016714 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715}
16716
16717/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016718 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 */
16720 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016721f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016722 typval_T *argvars;
16723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724{
Bram Moolenaar33570922005-01-25 22:26:29 +000016725 list_T *l;
16726 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727
Bram Moolenaar0d660222005-01-07 21:51:51 +000016728 if (argvars[0].v_type != VAR_LIST)
16729 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016730 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016731 && !tv_check_lock(l->lv_lock,
16732 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016733 {
16734 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016735 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016736 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016737 while (li != NULL)
16738 {
16739 ni = li->li_prev;
16740 list_append(l, li);
16741 li = ni;
16742 }
16743 rettv->vval.v_list = l;
16744 rettv->v_type = VAR_LIST;
16745 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016746 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748}
16749
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016750#define SP_NOMOVE 0x01 /* don't move cursor */
16751#define SP_REPEAT 0x02 /* repeat to find outer pair */
16752#define SP_RETCOUNT 0x04 /* return matchcount */
16753#define SP_SETPCMARK 0x08 /* set previous context mark */
16754#define SP_START 0x10 /* accept match at start position */
16755#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16756#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016757#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016758
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016759static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016760
16761/*
16762 * Get flags for a search function.
16763 * Possibly sets "p_ws".
16764 * Returns BACKWARD, FORWARD or zero (for an error).
16765 */
16766 static int
16767get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016768 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016769 int *flagsp;
16770{
16771 int dir = FORWARD;
16772 char_u *flags;
16773 char_u nbuf[NUMBUFLEN];
16774 int mask;
16775
16776 if (varp->v_type != VAR_UNKNOWN)
16777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016778 flags = get_tv_string_buf_chk(varp, nbuf);
16779 if (flags == NULL)
16780 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016781 while (*flags != NUL)
16782 {
16783 switch (*flags)
16784 {
16785 case 'b': dir = BACKWARD; break;
16786 case 'w': p_ws = TRUE; break;
16787 case 'W': p_ws = FALSE; break;
16788 default: mask = 0;
16789 if (flagsp != NULL)
16790 switch (*flags)
16791 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016792 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016793 case 'e': mask = SP_END; break;
16794 case 'm': mask = SP_RETCOUNT; break;
16795 case 'n': mask = SP_NOMOVE; break;
16796 case 'p': mask = SP_SUBPAT; break;
16797 case 'r': mask = SP_REPEAT; break;
16798 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016799 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016800 }
16801 if (mask == 0)
16802 {
16803 EMSG2(_(e_invarg2), flags);
16804 dir = 0;
16805 }
16806 else
16807 *flagsp |= mask;
16808 }
16809 if (dir == 0)
16810 break;
16811 ++flags;
16812 }
16813 }
16814 return dir;
16815}
16816
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016818 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016820 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016821search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016822 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016823 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016824 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016826 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 char_u *pat;
16828 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016829 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 int save_p_ws = p_ws;
16831 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016832 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016833 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016834 proftime_T tm;
16835#ifdef FEAT_RELTIME
16836 long time_limit = 0;
16837#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016838 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016839 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016841 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016842 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016843 if (dir == 0)
16844 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016845 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016846 if (flags & SP_START)
16847 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016848 if (flags & SP_END)
16849 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016850 if (flags & SP_COLUMN)
16851 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016852
Bram Moolenaar76929292008-01-06 19:07:36 +000016853 /* Optional arguments: line number to stop searching and timeout. */
16854 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016855 {
16856 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16857 if (lnum_stop < 0)
16858 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016859#ifdef FEAT_RELTIME
16860 if (argvars[3].v_type != VAR_UNKNOWN)
16861 {
16862 time_limit = get_tv_number_chk(&argvars[3], NULL);
16863 if (time_limit < 0)
16864 goto theend;
16865 }
16866#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016867 }
16868
Bram Moolenaar76929292008-01-06 19:07:36 +000016869#ifdef FEAT_RELTIME
16870 /* Set the time limit, if there is one. */
16871 profile_setlimit(time_limit, &tm);
16872#endif
16873
Bram Moolenaar231334e2005-07-25 20:46:57 +000016874 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016875 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016876 * Check to make sure only those flags are set.
16877 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16878 * flags cannot be set. Check for that condition also.
16879 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016880 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016881 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016883 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016884 goto theend;
16885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016887 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016888 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016889 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016890 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016892 if (flags & SP_SUBPAT)
16893 retval = subpatnum;
16894 else
16895 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016896 if (flags & SP_SETPCMARK)
16897 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016899 if (match_pos != NULL)
16900 {
16901 /* Store the match cursor position */
16902 match_pos->lnum = pos.lnum;
16903 match_pos->col = pos.col + 1;
16904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 /* "/$" will put the cursor after the end of the line, may need to
16906 * correct that here */
16907 check_cursor();
16908 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016909
16910 /* If 'n' flag is used: restore cursor position. */
16911 if (flags & SP_NOMOVE)
16912 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016913 else
16914 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016915theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016917
16918 return retval;
16919}
16920
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016921#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016922
16923/*
16924 * round() is not in C90, use ceil() or floor() instead.
16925 */
16926 float_T
16927vim_round(f)
16928 float_T f;
16929{
16930 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16931}
16932
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016933/*
16934 * "round({float})" function
16935 */
16936 static void
16937f_round(argvars, rettv)
16938 typval_T *argvars;
16939 typval_T *rettv;
16940{
16941 float_T f;
16942
16943 rettv->v_type = VAR_FLOAT;
16944 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016945 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016946 else
16947 rettv->vval.v_float = 0.0;
16948}
16949#endif
16950
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016951/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016952 * "screenattr()" function
16953 */
16954 static void
16955f_screenattr(argvars, rettv)
16956 typval_T *argvars UNUSED;
16957 typval_T *rettv;
16958{
16959 int row;
16960 int col;
16961 int c;
16962
16963 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16964 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16965 if (row < 0 || row >= screen_Rows
16966 || col < 0 || col >= screen_Columns)
16967 c = -1;
16968 else
16969 c = ScreenAttrs[LineOffset[row] + col];
16970 rettv->vval.v_number = c;
16971}
16972
16973/*
16974 * "screenchar()" function
16975 */
16976 static void
16977f_screenchar(argvars, rettv)
16978 typval_T *argvars UNUSED;
16979 typval_T *rettv;
16980{
16981 int row;
16982 int col;
16983 int off;
16984 int c;
16985
16986 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16987 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16988 if (row < 0 || row >= screen_Rows
16989 || col < 0 || col >= screen_Columns)
16990 c = -1;
16991 else
16992 {
16993 off = LineOffset[row] + col;
16994#ifdef FEAT_MBYTE
16995 if (enc_utf8 && ScreenLinesUC[off] != 0)
16996 c = ScreenLinesUC[off];
16997 else
16998#endif
16999 c = ScreenLines[off];
17000 }
17001 rettv->vval.v_number = c;
17002}
17003
17004/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017005 * "screencol()" function
17006 *
17007 * First column is 1 to be consistent with virtcol().
17008 */
17009 static void
17010f_screencol(argvars, rettv)
17011 typval_T *argvars UNUSED;
17012 typval_T *rettv;
17013{
17014 rettv->vval.v_number = screen_screencol() + 1;
17015}
17016
17017/*
17018 * "screenrow()" function
17019 */
17020 static void
17021f_screenrow(argvars, rettv)
17022 typval_T *argvars UNUSED;
17023 typval_T *rettv;
17024{
17025 rettv->vval.v_number = screen_screenrow() + 1;
17026}
17027
17028/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017029 * "search()" function
17030 */
17031 static void
17032f_search(argvars, rettv)
17033 typval_T *argvars;
17034 typval_T *rettv;
17035{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017036 int flags = 0;
17037
17038 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039}
17040
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017042 * "searchdecl()" function
17043 */
17044 static void
17045f_searchdecl(argvars, rettv)
17046 typval_T *argvars;
17047 typval_T *rettv;
17048{
17049 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017050 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017051 int error = FALSE;
17052 char_u *name;
17053
17054 rettv->vval.v_number = 1; /* default: FAIL */
17055
17056 name = get_tv_string_chk(&argvars[0]);
17057 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017058 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017059 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017060 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17061 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17062 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017063 if (!error && name != NULL)
17064 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017065 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017066}
17067
17068/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017069 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017071 static int
17072searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000017073 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017074 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075{
17076 char_u *spat, *mpat, *epat;
17077 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 int dir;
17080 int flags = 0;
17081 char_u nbuf1[NUMBUFLEN];
17082 char_u nbuf2[NUMBUFLEN];
17083 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017084 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017085 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017086 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017089 spat = get_tv_string_chk(&argvars[0]);
17090 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17091 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17092 if (spat == NULL || mpat == NULL || epat == NULL)
17093 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095 /* Handle the optional fourth argument: flags */
17096 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017097 if (dir == 0)
17098 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017099
17100 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017101 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17102 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017103 if ((flags & (SP_END | SP_SUBPAT)) != 0
17104 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017105 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017106 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017107 goto theend;
17108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017110 /* Using 'r' implies 'W', otherwise it doesn't work. */
17111 if (flags & SP_REPEAT)
17112 p_ws = FALSE;
17113
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017114 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017115 if (argvars[3].v_type == VAR_UNKNOWN
17116 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 skip = (char_u *)"";
17118 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017121 if (argvars[5].v_type != VAR_UNKNOWN)
17122 {
17123 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17124 if (lnum_stop < 0)
17125 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017126#ifdef FEAT_RELTIME
17127 if (argvars[6].v_type != VAR_UNKNOWN)
17128 {
17129 time_limit = get_tv_number_chk(&argvars[6], NULL);
17130 if (time_limit < 0)
17131 goto theend;
17132 }
17133#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017134 }
17135 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 if (skip == NULL)
17137 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017139 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017140 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017141
17142theend:
17143 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017144
17145 return retval;
17146}
17147
17148/*
17149 * "searchpair()" function
17150 */
17151 static void
17152f_searchpair(argvars, rettv)
17153 typval_T *argvars;
17154 typval_T *rettv;
17155{
17156 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17157}
17158
17159/*
17160 * "searchpairpos()" function
17161 */
17162 static void
17163f_searchpairpos(argvars, rettv)
17164 typval_T *argvars;
17165 typval_T *rettv;
17166{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017167 pos_T match_pos;
17168 int lnum = 0;
17169 int col = 0;
17170
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017171 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017172 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017173
17174 if (searchpair_cmn(argvars, &match_pos) > 0)
17175 {
17176 lnum = match_pos.lnum;
17177 col = match_pos.col;
17178 }
17179
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017180 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17181 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017182}
17183
17184/*
17185 * Search for a start/middle/end thing.
17186 * Used by searchpair(), see its documentation for the details.
17187 * Returns 0 or -1 for no match,
17188 */
17189 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017190do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17191 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017192 char_u *spat; /* start pattern */
17193 char_u *mpat; /* middle pattern */
17194 char_u *epat; /* end pattern */
17195 int dir; /* BACKWARD or FORWARD */
17196 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017197 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017198 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017199 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017200 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017201{
17202 char_u *save_cpo;
17203 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17204 long retval = 0;
17205 pos_T pos;
17206 pos_T firstpos;
17207 pos_T foundpos;
17208 pos_T save_cursor;
17209 pos_T save_pos;
17210 int n;
17211 int r;
17212 int nest = 1;
17213 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017214 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017215 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017216
17217 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17218 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017219 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017220
Bram Moolenaar76929292008-01-06 19:07:36 +000017221#ifdef FEAT_RELTIME
17222 /* Set the time limit, if there is one. */
17223 profile_setlimit(time_limit, &tm);
17224#endif
17225
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017226 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17227 * start/middle/end (pat3, for the top pair). */
17228 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17229 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17230 if (pat2 == NULL || pat3 == NULL)
17231 goto theend;
17232 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17233 if (*mpat == NUL)
17234 STRCPY(pat3, pat2);
17235 else
17236 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17237 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017238 if (flags & SP_START)
17239 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017240
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 save_cursor = curwin->w_cursor;
17242 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017243 clearpos(&firstpos);
17244 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 pat = pat3;
17246 for (;;)
17247 {
17248 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017249 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17251 /* didn't find it or found the first match again: FAIL */
17252 break;
17253
17254 if (firstpos.lnum == 0)
17255 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017256 if (equalpos(pos, foundpos))
17257 {
17258 /* Found the same position again. Can happen with a pattern that
17259 * has "\zs" at the end and searching backwards. Advance one
17260 * character and try again. */
17261 if (dir == BACKWARD)
17262 decl(&pos);
17263 else
17264 incl(&pos);
17265 }
17266 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017268 /* clear the start flag to avoid getting stuck here */
17269 options &= ~SEARCH_START;
17270
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 /* If the skip pattern matches, ignore this match. */
17272 if (*skip != NUL)
17273 {
17274 save_pos = curwin->w_cursor;
17275 curwin->w_cursor = pos;
17276 r = eval_to_bool(skip, &err, NULL, FALSE);
17277 curwin->w_cursor = save_pos;
17278 if (err)
17279 {
17280 /* Evaluating {skip} caused an error, break here. */
17281 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017282 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 break;
17284 }
17285 if (r)
17286 continue;
17287 }
17288
17289 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17290 {
17291 /* Found end when searching backwards or start when searching
17292 * forward: nested pair. */
17293 ++nest;
17294 pat = pat2; /* nested, don't search for middle */
17295 }
17296 else
17297 {
17298 /* Found end when searching forward or start when searching
17299 * backward: end of (nested) pair; or found middle in outer pair. */
17300 if (--nest == 1)
17301 pat = pat3; /* outer level, search for middle */
17302 }
17303
17304 if (nest == 0)
17305 {
17306 /* Found the match: return matchcount or line number. */
17307 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017308 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017310 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017311 if (flags & SP_SETPCMARK)
17312 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313 curwin->w_cursor = pos;
17314 if (!(flags & SP_REPEAT))
17315 break;
17316 nest = 1; /* search for next unmatched */
17317 }
17318 }
17319
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017320 if (match_pos != NULL)
17321 {
17322 /* Store the match cursor position */
17323 match_pos->lnum = curwin->w_cursor.lnum;
17324 match_pos->col = curwin->w_cursor.col + 1;
17325 }
17326
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017328 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 curwin->w_cursor = save_cursor;
17330
17331theend:
17332 vim_free(pat2);
17333 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017334 if (p_cpo == empty_option)
17335 p_cpo = save_cpo;
17336 else
17337 /* Darn, evaluating the {skip} expression changed the value. */
17338 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017339
17340 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341}
17342
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017343/*
17344 * "searchpos()" function
17345 */
17346 static void
17347f_searchpos(argvars, rettv)
17348 typval_T *argvars;
17349 typval_T *rettv;
17350{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017351 pos_T match_pos;
17352 int lnum = 0;
17353 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017354 int n;
17355 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017356
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017357 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017358 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017359
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017360 n = search_cmn(argvars, &match_pos, &flags);
17361 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017362 {
17363 lnum = match_pos.lnum;
17364 col = match_pos.col;
17365 }
17366
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017367 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17368 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017369 if (flags & SP_SUBPAT)
17370 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017371}
17372
17373
Bram Moolenaar0d660222005-01-07 21:51:51 +000017374 static void
17375f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017376 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017379#ifdef FEAT_CLIENTSERVER
17380 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017381 char_u *server = get_tv_string_chk(&argvars[0]);
17382 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383
Bram Moolenaar0d660222005-01-07 21:51:51 +000017384 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017385 if (server == NULL || reply == NULL)
17386 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017387 if (check_restricted() || check_secure())
17388 return;
17389# ifdef FEAT_X11
17390 if (check_connection() == FAIL)
17391 return;
17392# endif
17393
17394 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017396 EMSG(_("E258: Unable to send to client"));
17397 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017399 rettv->vval.v_number = 0;
17400#else
17401 rettv->vval.v_number = -1;
17402#endif
17403}
17404
Bram Moolenaar0d660222005-01-07 21:51:51 +000017405 static void
17406f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017407 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017408 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017409{
17410 char_u *r = NULL;
17411
17412#ifdef FEAT_CLIENTSERVER
17413# ifdef WIN32
17414 r = serverGetVimNames();
17415# else
17416 make_connection();
17417 if (X_DISPLAY != NULL)
17418 r = serverGetVimNames(X_DISPLAY);
17419# endif
17420#endif
17421 rettv->v_type = VAR_STRING;
17422 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423}
17424
17425/*
17426 * "setbufvar()" function
17427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017429f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017430 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017431 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432{
17433 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017436 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 char_u nbuf[NUMBUFLEN];
17438
17439 if (check_restricted() || check_secure())
17440 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017441 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17442 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017443 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444 varp = &argvars[2];
17445
17446 if (buf != NULL && varname != NULL && varp != NULL)
17447 {
17448 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450
17451 if (*varname == '&')
17452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017453 long numval;
17454 char_u *strval;
17455 int error = FALSE;
17456
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017458 numval = get_tv_number_chk(varp, &error);
17459 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017460 if (!error && strval != NULL)
17461 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462 }
17463 else
17464 {
17465 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17466 if (bufvarname != NULL)
17467 {
17468 STRCPY(bufvarname, "b:");
17469 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017470 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 vim_free(bufvarname);
17472 }
17473 }
17474
17475 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478}
17479
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017480 static void
17481f_setcharsearch(argvars, rettv)
17482 typval_T *argvars;
17483 typval_T *rettv UNUSED;
17484{
17485 dict_T *d;
17486 dictitem_T *di;
17487 char_u *csearch;
17488
17489 if (argvars[0].v_type != VAR_DICT)
17490 {
17491 EMSG(_(e_dictreq));
17492 return;
17493 }
17494
17495 if ((d = argvars[0].vval.v_dict) != NULL)
17496 {
17497 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17498 if (csearch != NULL)
17499 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017500#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017501 if (enc_utf8)
17502 {
17503 int pcc[MAX_MCO];
17504 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017505
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017506 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17507 }
17508 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017509#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017510 set_last_csearch(PTR2CHAR(csearch),
17511 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017512 }
17513
17514 di = dict_find(d, (char_u *)"forward", -1);
17515 if (di != NULL)
17516 set_csearch_direction(get_tv_number(&di->di_tv)
17517 ? FORWARD : BACKWARD);
17518
17519 di = dict_find(d, (char_u *)"until", -1);
17520 if (di != NULL)
17521 set_csearch_until(!!get_tv_number(&di->di_tv));
17522 }
17523}
17524
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525/*
17526 * "setcmdpos()" function
17527 */
17528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017529f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017530 typval_T *argvars;
17531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017533 int pos = (int)get_tv_number(&argvars[0]) - 1;
17534
17535 if (pos >= 0)
17536 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537}
17538
17539/*
17540 * "setline()" function
17541 */
17542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017543f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017544 typval_T *argvars;
17545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546{
17547 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017548 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017549 list_T *l = NULL;
17550 listitem_T *li = NULL;
17551 long added = 0;
17552 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017554 lnum = get_tv_lnum(&argvars[0]);
17555 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017557 l = argvars[1].vval.v_list;
17558 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017560 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017561 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017562
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017563 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017564 for (;;)
17565 {
17566 if (l != NULL)
17567 {
17568 /* list argument, get next string */
17569 if (li == NULL)
17570 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017571 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017572 li = li->li_next;
17573 }
17574
17575 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017576 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017577 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017578
17579 /* When coming here from Insert mode, sync undo, so that this can be
17580 * undone separately from what was previously inserted. */
17581 if (u_sync_once == 2)
17582 {
17583 u_sync_once = 1; /* notify that u_sync() was called */
17584 u_sync(TRUE);
17585 }
17586
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017587 if (lnum <= curbuf->b_ml.ml_line_count)
17588 {
17589 /* existing line, replace it */
17590 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17591 {
17592 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017593 if (lnum == curwin->w_cursor.lnum)
17594 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017595 rettv->vval.v_number = 0; /* OK */
17596 }
17597 }
17598 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17599 {
17600 /* lnum is one past the last line, append the line */
17601 ++added;
17602 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17603 rettv->vval.v_number = 0; /* OK */
17604 }
17605
17606 if (l == NULL) /* only one string argument */
17607 break;
17608 ++lnum;
17609 }
17610
17611 if (added > 0)
17612 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613}
17614
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017615static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv);
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017616
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017618 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017619 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017620 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017621set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017622 win_T *wp UNUSED;
17623 typval_T *list_arg UNUSED;
17624 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017625 typval_T *rettv;
17626{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017627#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017628 char_u *act;
17629 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017630#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017631
Bram Moolenaar2641f772005-03-25 21:58:17 +000017632 rettv->vval.v_number = -1;
17633
17634#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017635 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017636 EMSG(_(e_listreq));
17637 else
17638 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017639 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017640
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017641 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017642 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017643 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017644 if (act == NULL)
17645 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017646 if (*act == 'a' || *act == 'r')
17647 action = *act;
17648 }
17649
Bram Moolenaar81484f42012-12-05 15:16:47 +010017650 if (l != NULL && set_errorlist(wp, l, action,
17651 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017652 rettv->vval.v_number = 0;
17653 }
17654#endif
17655}
17656
17657/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017658 * "setloclist()" function
17659 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017660 static void
17661f_setloclist(argvars, rettv)
17662 typval_T *argvars;
17663 typval_T *rettv;
17664{
17665 win_T *win;
17666
17667 rettv->vval.v_number = -1;
17668
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017669 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017670 if (win != NULL)
17671 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17672}
17673
17674/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017675 * "setmatches()" function
17676 */
17677 static void
17678f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017679 typval_T *argvars UNUSED;
17680 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017681{
17682#ifdef FEAT_SEARCH_EXTRA
17683 list_T *l;
17684 listitem_T *li;
17685 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017686 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017687
17688 rettv->vval.v_number = -1;
17689 if (argvars[0].v_type != VAR_LIST)
17690 {
17691 EMSG(_(e_listreq));
17692 return;
17693 }
17694 if ((l = argvars[0].vval.v_list) != NULL)
17695 {
17696
17697 /* To some extent make sure that we are dealing with a list from
17698 * "getmatches()". */
17699 li = l->lv_first;
17700 while (li != NULL)
17701 {
17702 if (li->li_tv.v_type != VAR_DICT
17703 || (d = li->li_tv.vval.v_dict) == NULL)
17704 {
17705 EMSG(_(e_invarg));
17706 return;
17707 }
17708 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017709 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17710 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017711 && dict_find(d, (char_u *)"priority", -1) != NULL
17712 && dict_find(d, (char_u *)"id", -1) != NULL))
17713 {
17714 EMSG(_(e_invarg));
17715 return;
17716 }
17717 li = li->li_next;
17718 }
17719
17720 clear_matches(curwin);
17721 li = l->lv_first;
17722 while (li != NULL)
17723 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017724 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017725 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017726 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017727 char_u *group;
17728 int priority;
17729 int id;
17730 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017731
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017732 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017733 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17734 {
17735 if (s == NULL)
17736 {
17737 s = list_alloc();
17738 if (s == NULL)
17739 return;
17740 }
17741
17742 /* match from matchaddpos() */
17743 for (i = 1; i < 9; i++)
17744 {
17745 sprintf((char *)buf, (char *)"pos%d", i);
17746 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17747 {
17748 if (di->di_tv.v_type != VAR_LIST)
17749 return;
17750
17751 list_append_tv(s, &di->di_tv);
17752 s->lv_refcount++;
17753 }
17754 else
17755 break;
17756 }
17757 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017758
17759 group = get_dict_string(d, (char_u *)"group", FALSE);
17760 priority = (int)get_dict_number(d, (char_u *)"priority");
17761 id = (int)get_dict_number(d, (char_u *)"id");
17762 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17763 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17764 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017765 if (i == 0)
17766 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017767 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017768 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017769 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017770 }
17771 else
17772 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017773 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017774 list_unref(s);
17775 s = NULL;
17776 }
17777
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017778 li = li->li_next;
17779 }
17780 rettv->vval.v_number = 0;
17781 }
17782#endif
17783}
17784
17785/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017786 * "setpos()" function
17787 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017788 static void
17789f_setpos(argvars, rettv)
17790 typval_T *argvars;
17791 typval_T *rettv;
17792{
17793 pos_T pos;
17794 int fnum;
17795 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017796 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017797
Bram Moolenaar08250432008-02-13 11:42:46 +000017798 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017799 name = get_tv_string_chk(argvars);
17800 if (name != NULL)
17801 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017802 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017803 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017804 if (--pos.col < 0)
17805 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017806 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017807 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017808 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017809 if (fnum == curbuf->b_fnum)
17810 {
17811 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017812 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017813 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017814 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017815 curwin->w_set_curswant = FALSE;
17816 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017817 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017818 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017819 }
17820 else
17821 EMSG(_(e_invarg));
17822 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017823 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17824 {
17825 /* set mark */
17826 if (setmark_pos(name[1], &pos, fnum) == OK)
17827 rettv->vval.v_number = 0;
17828 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017829 else
17830 EMSG(_(e_invarg));
17831 }
17832 }
17833}
17834
17835/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017836 * "setqflist()" function
17837 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017838 static void
17839f_setqflist(argvars, rettv)
17840 typval_T *argvars;
17841 typval_T *rettv;
17842{
17843 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17844}
17845
17846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 * "setreg()" function
17848 */
17849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017850f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017851 typval_T *argvars;
17852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853{
17854 int regname;
17855 char_u *strregname;
17856 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017857 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 int append;
17859 char_u yank_type;
17860 long block_len;
17861
17862 block_len = -1;
17863 yank_type = MAUTO;
17864 append = FALSE;
17865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017866 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017867 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017869 if (strregname == NULL)
17870 return; /* type error; errmsg already given */
17871 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 if (regname == 0 || regname == '@')
17873 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017875 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017877 stropt = get_tv_string_chk(&argvars[2]);
17878 if (stropt == NULL)
17879 return; /* type error */
17880 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881 switch (*stropt)
17882 {
17883 case 'a': case 'A': /* append */
17884 append = TRUE;
17885 break;
17886 case 'v': case 'c': /* character-wise selection */
17887 yank_type = MCHAR;
17888 break;
17889 case 'V': case 'l': /* line-wise selection */
17890 yank_type = MLINE;
17891 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 case 'b': case Ctrl_V: /* block-wise selection */
17893 yank_type = MBLOCK;
17894 if (VIM_ISDIGIT(stropt[1]))
17895 {
17896 ++stropt;
17897 block_len = getdigits(&stropt) - 1;
17898 --stropt;
17899 }
17900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 }
17902 }
17903
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017904 if (argvars[1].v_type == VAR_LIST)
17905 {
17906 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017907 char_u **allocval;
17908 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017909 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017910 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017911 int len = argvars[1].vval.v_list->lv_len;
17912 listitem_T *li;
17913
Bram Moolenaar7d647822014-04-05 21:28:56 +020017914 /* First half: use for pointers to result lines; second half: use for
17915 * pointers to allocated copies. */
17916 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017917 if (lstval == NULL)
17918 return;
17919 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017920 allocval = lstval + len + 2;
17921 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017922
17923 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17924 li = li->li_next)
17925 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017926 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017927 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017928 goto free_lstval;
17929 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017930 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017931 /* Need to make a copy, next get_tv_string_buf_chk() will
17932 * overwrite the string. */
17933 strval = vim_strsave(buf);
17934 if (strval == NULL)
17935 goto free_lstval;
17936 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017937 }
17938 *curval++ = strval;
17939 }
17940 *curval++ = NULL;
17941
17942 write_reg_contents_lst(regname, lstval, -1,
17943 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017944free_lstval:
17945 while (curallocval > allocval)
17946 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017947 vim_free(lstval);
17948 }
17949 else
17950 {
17951 strval = get_tv_string_chk(&argvars[1]);
17952 if (strval == NULL)
17953 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017954 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017957 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958}
17959
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017960/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017961 * "settabvar()" function
17962 */
17963 static void
17964f_settabvar(argvars, rettv)
17965 typval_T *argvars;
17966 typval_T *rettv;
17967{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017968#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017969 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017970 tabpage_T *tp;
17971#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017972 char_u *varname, *tabvarname;
17973 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017974
17975 rettv->vval.v_number = 0;
17976
17977 if (check_restricted() || check_secure())
17978 return;
17979
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017980#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017981 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017982#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017983 varname = get_tv_string_chk(&argvars[1]);
17984 varp = &argvars[2];
17985
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017986 if (varname != NULL && varp != NULL
17987#ifdef FEAT_WINDOWS
17988 && tp != NULL
17989#endif
17990 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017991 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017992#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017993 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017994 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017995#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017996
17997 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17998 if (tabvarname != NULL)
17999 {
18000 STRCPY(tabvarname, "t:");
18001 STRCPY(tabvarname + 2, varname);
18002 set_var(tabvarname, varp, TRUE);
18003 vim_free(tabvarname);
18004 }
18005
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018006#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018007 /* Restore current tabpage */
18008 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018009 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018010#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018011 }
18012}
18013
18014/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018015 * "settabwinvar()" function
18016 */
18017 static void
18018f_settabwinvar(argvars, rettv)
18019 typval_T *argvars;
18020 typval_T *rettv;
18021{
18022 setwinvar(argvars, rettv, 1);
18023}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024
18025/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018026 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018029f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018030 typval_T *argvars;
18031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018033 setwinvar(argvars, rettv, 0);
18034}
18035
18036/*
18037 * "setwinvar()" and "settabwinvar()" functions
18038 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018039
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018040 static void
18041setwinvar(argvars, rettv, off)
18042 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018043 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018044 int off;
18045{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 win_T *win;
18047#ifdef FEAT_WINDOWS
18048 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018049 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018050 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051#endif
18052 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018053 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018055 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056
18057 if (check_restricted() || check_secure())
18058 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018059
18060#ifdef FEAT_WINDOWS
18061 if (off == 1)
18062 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18063 else
18064 tp = curtab;
18065#endif
18066 win = find_win_by_nr(&argvars[off], tp);
18067 varname = get_tv_string_chk(&argvars[off + 1]);
18068 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069
18070 if (win != NULL && varname != NULL && varp != NULL)
18071 {
18072#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018073 need_switch_win = !(tp == curtab && win == curwin);
18074 if (!need_switch_win
18075 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018078 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018080 long numval;
18081 char_u *strval;
18082 int error = FALSE;
18083
18084 ++varname;
18085 numval = get_tv_number_chk(varp, &error);
18086 strval = get_tv_string_buf_chk(varp, nbuf);
18087 if (!error && strval != NULL)
18088 set_option_value(varname, numval, strval, OPT_LOCAL);
18089 }
18090 else
18091 {
18092 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18093 if (winvarname != NULL)
18094 {
18095 STRCPY(winvarname, "w:");
18096 STRCPY(winvarname + 2, varname);
18097 set_var(winvarname, varp, TRUE);
18098 vim_free(winvarname);
18099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 }
18101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018103 if (need_switch_win)
18104 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105#endif
18106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107}
18108
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018109#ifdef FEAT_CRYPT
18110/*
18111 * "sha256({string})" function
18112 */
18113 static void
18114f_sha256(argvars, rettv)
18115 typval_T *argvars;
18116 typval_T *rettv;
18117{
18118 char_u *p;
18119
18120 p = get_tv_string(&argvars[0]);
18121 rettv->vval.v_string = vim_strsave(
18122 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18123 rettv->v_type = VAR_STRING;
18124}
18125#endif /* FEAT_CRYPT */
18126
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018128 * "shellescape({string})" function
18129 */
18130 static void
18131f_shellescape(argvars, rettv)
18132 typval_T *argvars;
18133 typval_T *rettv;
18134{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018135 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018136 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018137 rettv->v_type = VAR_STRING;
18138}
18139
18140/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018141 * shiftwidth() function
18142 */
18143 static void
18144f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020018145 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018146 typval_T *rettv;
18147{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018148 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018149}
18150
18151/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018152 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 */
18154 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000018155f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018156 typval_T *argvars;
18157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018159 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160
Bram Moolenaar0d660222005-01-07 21:51:51 +000018161 p = get_tv_string(&argvars[0]);
18162 rettv->vval.v_string = vim_strsave(p);
18163 simplify_filename(rettv->vval.v_string); /* simplify in place */
18164 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165}
18166
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018167#ifdef FEAT_FLOAT
18168/*
18169 * "sin()" function
18170 */
18171 static void
18172f_sin(argvars, rettv)
18173 typval_T *argvars;
18174 typval_T *rettv;
18175{
18176 float_T f;
18177
18178 rettv->v_type = VAR_FLOAT;
18179 if (get_float_arg(argvars, &f) == OK)
18180 rettv->vval.v_float = sin(f);
18181 else
18182 rettv->vval.v_float = 0.0;
18183}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018184
18185/*
18186 * "sinh()" function
18187 */
18188 static void
18189f_sinh(argvars, rettv)
18190 typval_T *argvars;
18191 typval_T *rettv;
18192{
18193 float_T f;
18194
18195 rettv->v_type = VAR_FLOAT;
18196 if (get_float_arg(argvars, &f) == OK)
18197 rettv->vval.v_float = sinh(f);
18198 else
18199 rettv->vval.v_float = 0.0;
18200}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018201#endif
18202
Bram Moolenaar0d660222005-01-07 21:51:51 +000018203static int
18204#ifdef __BORLANDC__
18205 _RTLENTRYF
18206#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018207 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018208static int
18209#ifdef __BORLANDC__
18210 _RTLENTRYF
18211#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018212 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018213
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018214/* struct used in the array that's given to qsort() */
18215typedef struct
18216{
18217 listitem_T *item;
18218 int idx;
18219} sortItem_T;
18220
Bram Moolenaar0d660222005-01-07 21:51:51 +000018221static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018222static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018223static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018224#ifdef FEAT_FLOAT
18225static int item_compare_float;
18226#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018227static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018228static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018229static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018230static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018231static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018232#define ITEM_COMPARE_FAIL 999
18233
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018235 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237 static int
18238#ifdef __BORLANDC__
18239_RTLENTRYF
18240#endif
18241item_compare(s1, s2)
18242 const void *s1;
18243 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018245 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018246 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018247 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018248 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018249 int res;
18250 char_u numbuf1[NUMBUFLEN];
18251 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018253 si1 = (sortItem_T *)s1;
18254 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018255 tv1 = &si1->item->li_tv;
18256 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018257
18258 if (item_compare_numbers)
18259 {
18260 long v1 = get_tv_number(tv1);
18261 long v2 = get_tv_number(tv2);
18262
18263 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18264 }
18265
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018266#ifdef FEAT_FLOAT
18267 if (item_compare_float)
18268 {
18269 float_T v1 = get_tv_float(tv1);
18270 float_T v2 = get_tv_float(tv2);
18271
18272 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18273 }
18274#endif
18275
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018276 /* tv2string() puts quotes around a string and allocates memory. Don't do
18277 * that for string variables. Use a single quote when comparing with a
18278 * non-string to do what the docs promise. */
18279 if (tv1->v_type == VAR_STRING)
18280 {
18281 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18282 p1 = (char_u *)"'";
18283 else
18284 p1 = tv1->vval.v_string;
18285 }
18286 else
18287 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18288 if (tv2->v_type == VAR_STRING)
18289 {
18290 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18291 p2 = (char_u *)"'";
18292 else
18293 p2 = tv2->vval.v_string;
18294 }
18295 else
18296 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018297 if (p1 == NULL)
18298 p1 = (char_u *)"";
18299 if (p2 == NULL)
18300 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018301 if (!item_compare_numeric)
18302 {
18303 if (item_compare_ic)
18304 res = STRICMP(p1, p2);
18305 else
18306 res = STRCMP(p1, p2);
18307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018309 {
18310 double n1, n2;
18311 n1 = strtod((char *)p1, (char **)&p1);
18312 n2 = strtod((char *)p2, (char **)&p2);
18313 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18314 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018315
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018316 /* When the result would be zero, compare the item indexes. Makes the
18317 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018318 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018319 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018320
Bram Moolenaar0d660222005-01-07 21:51:51 +000018321 vim_free(tofree1);
18322 vim_free(tofree2);
18323 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324}
18325
18326 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018327#ifdef __BORLANDC__
18328_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018330item_compare2(s1, s2)
18331 const void *s1;
18332 const void *s2;
18333{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018334 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018335 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018336 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018337 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018338 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018340 /* shortcut after failure in previous call; compare all items equal */
18341 if (item_compare_func_err)
18342 return 0;
18343
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018344 si1 = (sortItem_T *)s1;
18345 si2 = (sortItem_T *)s2;
18346
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018347 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018348 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018349 copy_tv(&si1->item->li_tv, &argv[0]);
18350 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018351
18352 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018353 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018354 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18355 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018356 clear_tv(&argv[0]);
18357 clear_tv(&argv[1]);
18358
18359 if (res == FAIL)
18360 res = ITEM_COMPARE_FAIL;
18361 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018362 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18363 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018364 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018365 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018366
18367 /* When the result would be zero, compare the pointers themselves. Makes
18368 * the sort stable. */
18369 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018370 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018371
Bram Moolenaar0d660222005-01-07 21:51:51 +000018372 return res;
18373}
18374
18375/*
18376 * "sort({list})" function
18377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018379do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018380 typval_T *argvars;
18381 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018382 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383{
Bram Moolenaar33570922005-01-25 22:26:29 +000018384 list_T *l;
18385 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018386 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018387 long len;
18388 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389
Bram Moolenaar0d660222005-01-07 21:51:51 +000018390 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018391 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 else
18393 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018394 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018395 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018396 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18397 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018398 return;
18399 rettv->vval.v_list = l;
18400 rettv->v_type = VAR_LIST;
18401 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402
Bram Moolenaar0d660222005-01-07 21:51:51 +000018403 len = list_len(l);
18404 if (len <= 1)
18405 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406
Bram Moolenaar0d660222005-01-07 21:51:51 +000018407 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018408 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018409 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018410#ifdef FEAT_FLOAT
18411 item_compare_float = FALSE;
18412#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018414 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018415 if (argvars[1].v_type != VAR_UNKNOWN)
18416 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018417 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018418 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018419 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018420 else
18421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018422 int error = FALSE;
18423
18424 i = get_tv_number_chk(&argvars[1], &error);
18425 if (error)
18426 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018427 if (i == 1)
18428 item_compare_ic = TRUE;
18429 else
18430 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018431 if (item_compare_func != NULL)
18432 {
18433 if (STRCMP(item_compare_func, "n") == 0)
18434 {
18435 item_compare_func = NULL;
18436 item_compare_numeric = TRUE;
18437 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018438 else if (STRCMP(item_compare_func, "N") == 0)
18439 {
18440 item_compare_func = NULL;
18441 item_compare_numbers = TRUE;
18442 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018443#ifdef FEAT_FLOAT
18444 else if (STRCMP(item_compare_func, "f") == 0)
18445 {
18446 item_compare_func = NULL;
18447 item_compare_float = TRUE;
18448 }
18449#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018450 else if (STRCMP(item_compare_func, "i") == 0)
18451 {
18452 item_compare_func = NULL;
18453 item_compare_ic = TRUE;
18454 }
18455 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018456 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018457
18458 if (argvars[2].v_type != VAR_UNKNOWN)
18459 {
18460 /* optional third argument: {dict} */
18461 if (argvars[2].v_type != VAR_DICT)
18462 {
18463 EMSG(_(e_dictreq));
18464 return;
18465 }
18466 item_compare_selfdict = argvars[2].vval.v_dict;
18467 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469
Bram Moolenaar0d660222005-01-07 21:51:51 +000018470 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018471 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018472 if (ptrs == NULL)
18473 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474
Bram Moolenaar327aa022014-03-25 18:24:23 +010018475 i = 0;
18476 if (sort)
18477 {
18478 /* sort(): ptrs will be the list to sort */
18479 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018480 {
18481 ptrs[i].item = li;
18482 ptrs[i].idx = i;
18483 ++i;
18484 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018485
18486 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018487 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018488 /* test the compare function */
18489 if (item_compare_func != NULL
18490 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018491 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018492 EMSG(_("E702: Sort compare function failed"));
18493 else
18494 {
18495 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018496 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018497 item_compare_func == NULL ? item_compare : item_compare2);
18498
18499 if (!item_compare_func_err)
18500 {
18501 /* Clear the List and append the items in sorted order. */
18502 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18503 l->lv_len = 0;
18504 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018505 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018506 }
18507 }
18508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018510 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018511 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018512
18513 /* f_uniq(): ptrs will be a stack of items to remove */
18514 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018515 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018516 item_compare_func_ptr = item_compare_func
18517 ? item_compare2 : item_compare;
18518
18519 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18520 li = li->li_next)
18521 {
18522 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18523 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018524 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018525 if (item_compare_func_err)
18526 {
18527 EMSG(_("E882: Uniq compare function failed"));
18528 break;
18529 }
18530 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018532 if (!item_compare_func_err)
18533 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018534 while (--i >= 0)
18535 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018536 li = ptrs[i].item->li_next;
18537 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018538 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018539 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018540 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018541 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018542 list_fix_watch(l, li);
18543 listitem_free(li);
18544 l->lv_len--;
18545 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018546 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018547 }
18548
18549 vim_free(ptrs);
18550 }
18551}
18552
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018553/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018554 * "sort({list})" function
18555 */
18556 static void
18557f_sort(argvars, rettv)
18558 typval_T *argvars;
18559 typval_T *rettv;
18560{
18561 do_sort_uniq(argvars, rettv, TRUE);
18562}
18563
18564/*
18565 * "uniq({list})" function
18566 */
18567 static void
18568f_uniq(argvars, rettv)
18569 typval_T *argvars;
18570 typval_T *rettv;
18571{
18572 do_sort_uniq(argvars, rettv, FALSE);
18573}
18574
18575/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018576 * "soundfold({word})" function
18577 */
18578 static void
18579f_soundfold(argvars, rettv)
18580 typval_T *argvars;
18581 typval_T *rettv;
18582{
18583 char_u *s;
18584
18585 rettv->v_type = VAR_STRING;
18586 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018587#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018588 rettv->vval.v_string = eval_soundfold(s);
18589#else
18590 rettv->vval.v_string = vim_strsave(s);
18591#endif
18592}
18593
18594/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018595 * "spellbadword()" function
18596 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018597 static void
18598f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018599 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018600 typval_T *rettv;
18601{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018602 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018603 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018604 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018605
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018606 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018607 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018608
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018609#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018610 if (argvars[0].v_type == VAR_UNKNOWN)
18611 {
18612 /* Find the start and length of the badly spelled word. */
18613 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18614 if (len != 0)
18615 word = ml_get_cursor();
18616 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018617 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018618 {
18619 char_u *str = get_tv_string_chk(&argvars[0]);
18620 int capcol = -1;
18621
18622 if (str != NULL)
18623 {
18624 /* Check the argument for spelling. */
18625 while (*str != NUL)
18626 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018627 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018628 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018629 {
18630 word = str;
18631 break;
18632 }
18633 str += len;
18634 }
18635 }
18636 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018637#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018638
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018639 list_append_string(rettv->vval.v_list, word, len);
18640 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018641 attr == HLF_SPB ? "bad" :
18642 attr == HLF_SPR ? "rare" :
18643 attr == HLF_SPL ? "local" :
18644 attr == HLF_SPC ? "caps" :
18645 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018646}
18647
18648/*
18649 * "spellsuggest()" function
18650 */
18651 static void
18652f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018653 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018654 typval_T *rettv;
18655{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018656#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018657 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018658 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018659 int maxcount;
18660 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018661 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018662 listitem_T *li;
18663 int need_capital = FALSE;
18664#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018665
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018666 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018667 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018668
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018669#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018670 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018671 {
18672 str = get_tv_string(&argvars[0]);
18673 if (argvars[1].v_type != VAR_UNKNOWN)
18674 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018675 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018676 if (maxcount <= 0)
18677 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018678 if (argvars[2].v_type != VAR_UNKNOWN)
18679 {
18680 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18681 if (typeerr)
18682 return;
18683 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018684 }
18685 else
18686 maxcount = 25;
18687
Bram Moolenaar4770d092006-01-12 23:22:24 +000018688 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018689
18690 for (i = 0; i < ga.ga_len; ++i)
18691 {
18692 str = ((char_u **)ga.ga_data)[i];
18693
18694 li = listitem_alloc();
18695 if (li == NULL)
18696 vim_free(str);
18697 else
18698 {
18699 li->li_tv.v_type = VAR_STRING;
18700 li->li_tv.v_lock = 0;
18701 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018702 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018703 }
18704 }
18705 ga_clear(&ga);
18706 }
18707#endif
18708}
18709
Bram Moolenaar0d660222005-01-07 21:51:51 +000018710 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018711f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018712 typval_T *argvars;
18713 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018714{
18715 char_u *str;
18716 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018717 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018718 regmatch_T regmatch;
18719 char_u patbuf[NUMBUFLEN];
18720 char_u *save_cpo;
18721 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018722 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018723 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018724 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018725
18726 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18727 save_cpo = p_cpo;
18728 p_cpo = (char_u *)"";
18729
18730 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018731 if (argvars[1].v_type != VAR_UNKNOWN)
18732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018733 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18734 if (pat == NULL)
18735 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018736 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018737 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018738 }
18739 if (pat == NULL || *pat == NUL)
18740 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018741
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018742 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018744 if (typeerr)
18745 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746
Bram Moolenaar0d660222005-01-07 21:51:51 +000018747 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18748 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018750 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018751 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018752 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018753 if (*str == NUL)
18754 match = FALSE; /* empty item at the end */
18755 else
18756 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018757 if (match)
18758 end = regmatch.startp[0];
18759 else
18760 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018761 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18762 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018763 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018764 if (list_append_string(rettv->vval.v_list, str,
18765 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018766 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018767 }
18768 if (!match)
18769 break;
18770 /* Advance to just after the match. */
18771 if (regmatch.endp[0] > str)
18772 col = 0;
18773 else
18774 {
18775 /* Don't get stuck at the same match. */
18776#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018777 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018778#else
18779 col = 1;
18780#endif
18781 }
18782 str = regmatch.endp[0];
18783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784
Bram Moolenaar473de612013-06-08 18:19:48 +020018785 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787
Bram Moolenaar0d660222005-01-07 21:51:51 +000018788 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789}
18790
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018791#ifdef FEAT_FLOAT
18792/*
18793 * "sqrt()" function
18794 */
18795 static void
18796f_sqrt(argvars, rettv)
18797 typval_T *argvars;
18798 typval_T *rettv;
18799{
18800 float_T f;
18801
18802 rettv->v_type = VAR_FLOAT;
18803 if (get_float_arg(argvars, &f) == OK)
18804 rettv->vval.v_float = sqrt(f);
18805 else
18806 rettv->vval.v_float = 0.0;
18807}
18808
18809/*
18810 * "str2float()" function
18811 */
18812 static void
18813f_str2float(argvars, rettv)
18814 typval_T *argvars;
18815 typval_T *rettv;
18816{
18817 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18818
18819 if (*p == '+')
18820 p = skipwhite(p + 1);
18821 (void)string2float(p, &rettv->vval.v_float);
18822 rettv->v_type = VAR_FLOAT;
18823}
18824#endif
18825
Bram Moolenaar2c932302006-03-18 21:42:09 +000018826/*
18827 * "str2nr()" function
18828 */
18829 static void
18830f_str2nr(argvars, rettv)
18831 typval_T *argvars;
18832 typval_T *rettv;
18833{
18834 int base = 10;
18835 char_u *p;
18836 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018837 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018838
18839 if (argvars[1].v_type != VAR_UNKNOWN)
18840 {
18841 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018842 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018843 {
18844 EMSG(_(e_invarg));
18845 return;
18846 }
18847 }
18848
18849 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018850 if (*p == '+')
18851 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018852 switch (base)
18853 {
18854 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18855 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18856 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18857 default: what = 0;
18858 }
18859 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018860 rettv->vval.v_number = n;
18861}
18862
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863#ifdef HAVE_STRFTIME
18864/*
18865 * "strftime({format}[, {time}])" function
18866 */
18867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018868f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018869 typval_T *argvars;
18870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871{
18872 char_u result_buf[256];
18873 struct tm *curtime;
18874 time_t seconds;
18875 char_u *p;
18876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018877 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018879 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018880 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 seconds = time(NULL);
18882 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018883 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 curtime = localtime(&seconds);
18885 /* MSVC returns NULL for an invalid value of seconds. */
18886 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018887 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 else
18889 {
18890# ifdef FEAT_MBYTE
18891 vimconv_T conv;
18892 char_u *enc;
18893
18894 conv.vc_type = CONV_NONE;
18895 enc = enc_locale();
18896 convert_setup(&conv, p_enc, enc);
18897 if (conv.vc_type != CONV_NONE)
18898 p = string_convert(&conv, p, NULL);
18899# endif
18900 if (p != NULL)
18901 (void)strftime((char *)result_buf, sizeof(result_buf),
18902 (char *)p, curtime);
18903 else
18904 result_buf[0] = NUL;
18905
18906# ifdef FEAT_MBYTE
18907 if (conv.vc_type != CONV_NONE)
18908 vim_free(p);
18909 convert_setup(&conv, enc, p_enc);
18910 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018911 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 else
18913# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018914 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915
18916# ifdef FEAT_MBYTE
18917 /* Release conversion descriptors */
18918 convert_setup(&conv, NULL, NULL);
18919 vim_free(enc);
18920# endif
18921 }
18922}
18923#endif
18924
18925/*
18926 * "stridx()" function
18927 */
18928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018929f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018930 typval_T *argvars;
18931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932{
18933 char_u buf[NUMBUFLEN];
18934 char_u *needle;
18935 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018936 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018938 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018940 needle = get_tv_string_chk(&argvars[1]);
18941 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018942 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018943 if (needle == NULL || haystack == NULL)
18944 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945
Bram Moolenaar33570922005-01-25 22:26:29 +000018946 if (argvars[2].v_type != VAR_UNKNOWN)
18947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018948 int error = FALSE;
18949
18950 start_idx = get_tv_number_chk(&argvars[2], &error);
18951 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018952 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018953 if (start_idx >= 0)
18954 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018955 }
18956
18957 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18958 if (pos != NULL)
18959 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960}
18961
18962/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018963 * "string()" function
18964 */
18965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018966f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018967 typval_T *argvars;
18968 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018969{
18970 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018971 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018973 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018974 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018975 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018976 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018977 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978}
18979
18980/*
18981 * "strlen()" function
18982 */
18983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018984f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018985 typval_T *argvars;
18986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018988 rettv->vval.v_number = (varnumber_T)(STRLEN(
18989 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990}
18991
18992/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018993 * "strchars()" function
18994 */
18995 static void
18996f_strchars(argvars, rettv)
18997 typval_T *argvars;
18998 typval_T *rettv;
18999{
19000 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019001 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019002#ifdef FEAT_MBYTE
19003 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019004 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019005#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019006
19007 if (argvars[1].v_type != VAR_UNKNOWN)
19008 skipcc = get_tv_number_chk(&argvars[1], NULL);
19009 if (skipcc < 0 || skipcc > 1)
19010 EMSG(_(e_invarg));
19011 else
19012 {
19013#ifdef FEAT_MBYTE
19014 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19015 while (*s != NUL)
19016 {
19017 func_mb_ptr2char_adv(&s);
19018 ++len;
19019 }
19020 rettv->vval.v_number = len;
19021#else
19022 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19023#endif
19024 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019025}
19026
19027/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019028 * "strdisplaywidth()" function
19029 */
19030 static void
19031f_strdisplaywidth(argvars, rettv)
19032 typval_T *argvars;
19033 typval_T *rettv;
19034{
19035 char_u *s = get_tv_string(&argvars[0]);
19036 int col = 0;
19037
19038 if (argvars[1].v_type != VAR_UNKNOWN)
19039 col = get_tv_number(&argvars[1]);
19040
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019041 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019042}
19043
19044/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019045 * "strwidth()" function
19046 */
19047 static void
19048f_strwidth(argvars, rettv)
19049 typval_T *argvars;
19050 typval_T *rettv;
19051{
19052 char_u *s = get_tv_string(&argvars[0]);
19053
19054 rettv->vval.v_number = (varnumber_T)(
19055#ifdef FEAT_MBYTE
19056 mb_string2cells(s, -1)
19057#else
19058 STRLEN(s)
19059#endif
19060 );
19061}
19062
19063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 * "strpart()" function
19065 */
19066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019067f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019068 typval_T *argvars;
19069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070{
19071 char_u *p;
19072 int n;
19073 int len;
19074 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019075 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019077 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 slen = (int)STRLEN(p);
19079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019080 n = get_tv_number_chk(&argvars[1], &error);
19081 if (error)
19082 len = 0;
19083 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019084 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 else
19086 len = slen - n; /* default len: all bytes that are available. */
19087
19088 /*
19089 * Only return the overlap between the specified part and the actual
19090 * string.
19091 */
19092 if (n < 0)
19093 {
19094 len += n;
19095 n = 0;
19096 }
19097 else if (n > slen)
19098 n = slen;
19099 if (len < 0)
19100 len = 0;
19101 else if (n + len > slen)
19102 len = slen - n;
19103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019104 rettv->v_type = VAR_STRING;
19105 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106}
19107
19108/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019109 * "strridx()" function
19110 */
19111 static void
19112f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019113 typval_T *argvars;
19114 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019115{
19116 char_u buf[NUMBUFLEN];
19117 char_u *needle;
19118 char_u *haystack;
19119 char_u *rest;
19120 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019121 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019123 needle = get_tv_string_chk(&argvars[1]);
19124 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019125
19126 rettv->vval.v_number = -1;
19127 if (needle == NULL || haystack == NULL)
19128 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019129
19130 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019131 if (argvars[2].v_type != VAR_UNKNOWN)
19132 {
19133 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019134 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019135 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019136 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019137 }
19138 else
19139 end_idx = haystack_len;
19140
Bram Moolenaar0d660222005-01-07 21:51:51 +000019141 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019142 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019143 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019144 lastmatch = haystack + end_idx;
19145 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019146 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019147 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019148 for (rest = haystack; *rest != '\0'; ++rest)
19149 {
19150 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019151 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019152 break;
19153 lastmatch = rest;
19154 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019155 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019156
19157 if (lastmatch == NULL)
19158 rettv->vval.v_number = -1;
19159 else
19160 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19161}
19162
19163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 * "strtrans()" function
19165 */
19166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019167f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019168 typval_T *argvars;
19169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019171 rettv->v_type = VAR_STRING;
19172 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173}
19174
19175/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019176 * "submatch()" function
19177 */
19178 static void
19179f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019180 typval_T *argvars;
19181 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019182{
Bram Moolenaar41571762014-04-02 19:00:58 +020019183 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019184 int no;
19185 int retList = 0;
19186
19187 no = (int)get_tv_number_chk(&argvars[0], &error);
19188 if (error)
19189 return;
19190 error = FALSE;
19191 if (argvars[1].v_type != VAR_UNKNOWN)
19192 retList = get_tv_number_chk(&argvars[1], &error);
19193 if (error)
19194 return;
19195
19196 if (retList == 0)
19197 {
19198 rettv->v_type = VAR_STRING;
19199 rettv->vval.v_string = reg_submatch(no);
19200 }
19201 else
19202 {
19203 rettv->v_type = VAR_LIST;
19204 rettv->vval.v_list = reg_submatch_list(no);
19205 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019206}
19207
19208/*
19209 * "substitute()" function
19210 */
19211 static void
19212f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019213 typval_T *argvars;
19214 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019215{
19216 char_u patbuf[NUMBUFLEN];
19217 char_u subbuf[NUMBUFLEN];
19218 char_u flagsbuf[NUMBUFLEN];
19219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019220 char_u *str = get_tv_string_chk(&argvars[0]);
19221 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19222 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19223 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19224
Bram Moolenaar0d660222005-01-07 21:51:51 +000019225 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019226 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19227 rettv->vval.v_string = NULL;
19228 else
19229 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019230}
19231
19232/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019233 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019236f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019237 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239{
19240 int id = 0;
19241#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019242 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243 long col;
19244 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019245 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019247 lnum = get_tv_lnum(argvars); /* -1 on type error */
19248 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19249 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019251 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019252 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019253 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254#endif
19255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019256 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257}
19258
19259/*
19260 * "synIDattr(id, what [, mode])" function
19261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019263f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019264 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266{
19267 char_u *p = NULL;
19268#ifdef FEAT_SYN_HL
19269 int id;
19270 char_u *what;
19271 char_u *mode;
19272 char_u modebuf[NUMBUFLEN];
19273 int modec;
19274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019275 id = get_tv_number(&argvars[0]);
19276 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019277 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019279 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019281 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282 modec = 0; /* replace invalid with current */
19283 }
19284 else
19285 {
19286#ifdef FEAT_GUI
19287 if (gui.in_use)
19288 modec = 'g';
19289 else
19290#endif
19291 if (t_colors > 1)
19292 modec = 'c';
19293 else
19294 modec = 't';
19295 }
19296
19297
19298 switch (TOLOWER_ASC(what[0]))
19299 {
19300 case 'b':
19301 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19302 p = highlight_color(id, what, modec);
19303 else /* bold */
19304 p = highlight_has_attr(id, HL_BOLD, modec);
19305 break;
19306
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019307 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019308 p = highlight_color(id, what, modec);
19309 break;
19310
19311 case 'i':
19312 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19313 p = highlight_has_attr(id, HL_INVERSE, modec);
19314 else /* italic */
19315 p = highlight_has_attr(id, HL_ITALIC, modec);
19316 break;
19317
19318 case 'n': /* name */
19319 p = get_highlight_name(NULL, id - 1);
19320 break;
19321
19322 case 'r': /* reverse */
19323 p = highlight_has_attr(id, HL_INVERSE, modec);
19324 break;
19325
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019326 case 's':
19327 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19328 p = highlight_color(id, what, modec);
19329 else /* standout */
19330 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331 break;
19332
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019333 case 'u':
19334 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19335 /* underline */
19336 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19337 else
19338 /* undercurl */
19339 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340 break;
19341 }
19342
19343 if (p != NULL)
19344 p = vim_strsave(p);
19345#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019346 rettv->v_type = VAR_STRING;
19347 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348}
19349
19350/*
19351 * "synIDtrans(id)" function
19352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019354f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019355 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357{
19358 int id;
19359
19360#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019361 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362
19363 if (id > 0)
19364 id = syn_get_final_id(id);
19365 else
19366#endif
19367 id = 0;
19368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019369 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370}
19371
19372/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019373 * "synconcealed(lnum, col)" function
19374 */
19375 static void
19376f_synconcealed(argvars, rettv)
19377 typval_T *argvars UNUSED;
19378 typval_T *rettv;
19379{
19380#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19381 long lnum;
19382 long col;
19383 int syntax_flags = 0;
19384 int cchar;
19385 int matchid = 0;
19386 char_u str[NUMBUFLEN];
19387#endif
19388
19389 rettv->v_type = VAR_LIST;
19390 rettv->vval.v_list = NULL;
19391
19392#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19393 lnum = get_tv_lnum(argvars); /* -1 on type error */
19394 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19395
19396 vim_memset(str, NUL, sizeof(str));
19397
19398 if (rettv_list_alloc(rettv) != FAIL)
19399 {
19400 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19401 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19402 && curwin->w_p_cole > 0)
19403 {
19404 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19405 syntax_flags = get_syntax_info(&matchid);
19406
19407 /* get the conceal character */
19408 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19409 {
19410 cchar = syn_get_sub_char();
19411 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19412 cchar = lcs_conceal;
19413 if (cchar != NUL)
19414 {
19415# ifdef FEAT_MBYTE
19416 if (has_mbyte)
19417 (*mb_char2bytes)(cchar, str);
19418 else
19419# endif
19420 str[0] = cchar;
19421 }
19422 }
19423 }
19424
19425 list_append_number(rettv->vval.v_list,
19426 (syntax_flags & HL_CONCEAL) != 0);
19427 /* -1 to auto-determine strlen */
19428 list_append_string(rettv->vval.v_list, str, -1);
19429 list_append_number(rettv->vval.v_list, matchid);
19430 }
19431#endif
19432}
19433
19434/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019435 * "synstack(lnum, col)" function
19436 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019437 static void
19438f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019439 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019440 typval_T *rettv;
19441{
19442#ifdef FEAT_SYN_HL
19443 long lnum;
19444 long col;
19445 int i;
19446 int id;
19447#endif
19448
19449 rettv->v_type = VAR_LIST;
19450 rettv->vval.v_list = NULL;
19451
19452#ifdef FEAT_SYN_HL
19453 lnum = get_tv_lnum(argvars); /* -1 on type error */
19454 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19455
19456 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019457 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019458 && rettv_list_alloc(rettv) != FAIL)
19459 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019460 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019461 for (i = 0; ; ++i)
19462 {
19463 id = syn_get_stack_item(i);
19464 if (id < 0)
19465 break;
19466 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19467 break;
19468 }
19469 }
19470#endif
19471}
19472
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019474get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019475 typval_T *argvars;
19476 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019477 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019479 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019481 char_u *infile = NULL;
19482 char_u buf[NUMBUFLEN];
19483 int err = FALSE;
19484 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019485 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019486 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019488 rettv->v_type = VAR_STRING;
19489 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019490 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019491 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019492
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019493 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019494 {
19495 /*
19496 * Write the string to a temp file, to be used for input of the shell
19497 * command.
19498 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019499 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019500 {
19501 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019502 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019503 }
19504
19505 fd = mch_fopen((char *)infile, WRITEBIN);
19506 if (fd == NULL)
19507 {
19508 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019509 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019510 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019511 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019512 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019513 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19514 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019515 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019516 else
19517 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019518 size_t len;
19519
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019520 p = get_tv_string_buf_chk(&argvars[1], buf);
19521 if (p == NULL)
19522 {
19523 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019524 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019525 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019526 len = STRLEN(p);
19527 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019528 err = TRUE;
19529 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019530 if (fclose(fd) != 0)
19531 err = TRUE;
19532 if (err)
19533 {
19534 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019535 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019536 }
19537 }
19538
Bram Moolenaar52a72462014-08-29 15:53:52 +020019539 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19540 * echoes typeahead, that messes up the display. */
19541 if (!msg_silent)
19542 flags += SHELL_COOKED;
19543
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019544 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019546 int len;
19547 listitem_T *li;
19548 char_u *s = NULL;
19549 char_u *start;
19550 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019551 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552
Bram Moolenaar52a72462014-08-29 15:53:52 +020019553 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019554 if (res == NULL)
19555 goto errret;
19556
19557 list = list_alloc();
19558 if (list == NULL)
19559 goto errret;
19560
19561 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019563 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019564 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019565 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019566 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019567
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019568 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019569 if (s == NULL)
19570 goto errret;
19571
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019572 for (p = s; start < end; ++p, ++start)
19573 *p = *start == NUL ? NL : *start;
19574 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019575
19576 li = listitem_alloc();
19577 if (li == NULL)
19578 {
19579 vim_free(s);
19580 goto errret;
19581 }
19582 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019583 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019584 li->li_tv.vval.v_string = s;
19585 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019587
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019588 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019589 rettv->v_type = VAR_LIST;
19590 rettv->vval.v_list = list;
19591 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019593 else
19594 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019595 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019596#ifdef USE_CR
19597 /* translate <CR> into <NL> */
19598 if (res != NULL)
19599 {
19600 char_u *s;
19601
19602 for (s = res; *s; ++s)
19603 {
19604 if (*s == CAR)
19605 *s = NL;
19606 }
19607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608#else
19609# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019610 /* translate <CR><NL> into <NL> */
19611 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019613 char_u *s, *d;
19614
19615 d = res;
19616 for (s = res; *s; ++s)
19617 {
19618 if (s[0] == CAR && s[1] == NL)
19619 ++s;
19620 *d++ = *s;
19621 }
19622 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624# endif
19625#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019626 rettv->vval.v_string = res;
19627 res = NULL;
19628 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019629
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019630errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019631 if (infile != NULL)
19632 {
19633 mch_remove(infile);
19634 vim_free(infile);
19635 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019636 if (res != NULL)
19637 vim_free(res);
19638 if (list != NULL)
19639 list_free(list, TRUE);
19640}
19641
19642/*
19643 * "system()" function
19644 */
19645 static void
19646f_system(argvars, rettv)
19647 typval_T *argvars;
19648 typval_T *rettv;
19649{
19650 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19651}
19652
19653/*
19654 * "systemlist()" function
19655 */
19656 static void
19657f_systemlist(argvars, rettv)
19658 typval_T *argvars;
19659 typval_T *rettv;
19660{
19661 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662}
19663
19664/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019665 * "tabpagebuflist()" function
19666 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019667 static void
19668f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019669 typval_T *argvars UNUSED;
19670 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019671{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019672#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019673 tabpage_T *tp;
19674 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019675
19676 if (argvars[0].v_type == VAR_UNKNOWN)
19677 wp = firstwin;
19678 else
19679 {
19680 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19681 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019682 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019683 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019684 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019685 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019686 for (; wp != NULL; wp = wp->w_next)
19687 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019688 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019689 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019690 }
19691#endif
19692}
19693
19694
19695/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019696 * "tabpagenr()" function
19697 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019698 static void
19699f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019700 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019701 typval_T *rettv;
19702{
19703 int nr = 1;
19704#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019705 char_u *arg;
19706
19707 if (argvars[0].v_type != VAR_UNKNOWN)
19708 {
19709 arg = get_tv_string_chk(&argvars[0]);
19710 nr = 0;
19711 if (arg != NULL)
19712 {
19713 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019714 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019715 else
19716 EMSG2(_(e_invexpr2), arg);
19717 }
19718 }
19719 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019720 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019721#endif
19722 rettv->vval.v_number = nr;
19723}
19724
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019725
19726#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019727static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019728
19729/*
19730 * Common code for tabpagewinnr() and winnr().
19731 */
19732 static int
19733get_winnr(tp, argvar)
19734 tabpage_T *tp;
19735 typval_T *argvar;
19736{
19737 win_T *twin;
19738 int nr = 1;
19739 win_T *wp;
19740 char_u *arg;
19741
19742 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19743 if (argvar->v_type != VAR_UNKNOWN)
19744 {
19745 arg = get_tv_string_chk(argvar);
19746 if (arg == NULL)
19747 nr = 0; /* type error; errmsg already given */
19748 else if (STRCMP(arg, "$") == 0)
19749 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19750 else if (STRCMP(arg, "#") == 0)
19751 {
19752 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19753 if (twin == NULL)
19754 nr = 0;
19755 }
19756 else
19757 {
19758 EMSG2(_(e_invexpr2), arg);
19759 nr = 0;
19760 }
19761 }
19762
19763 if (nr > 0)
19764 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19765 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019766 {
19767 if (wp == NULL)
19768 {
19769 /* didn't find it in this tabpage */
19770 nr = 0;
19771 break;
19772 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019773 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019774 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019775 return nr;
19776}
19777#endif
19778
19779/*
19780 * "tabpagewinnr()" function
19781 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019782 static void
19783f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019784 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019785 typval_T *rettv;
19786{
19787 int nr = 1;
19788#ifdef FEAT_WINDOWS
19789 tabpage_T *tp;
19790
19791 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19792 if (tp == NULL)
19793 nr = 0;
19794 else
19795 nr = get_winnr(tp, &argvars[1]);
19796#endif
19797 rettv->vval.v_number = nr;
19798}
19799
19800
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019801/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019802 * "tagfiles()" function
19803 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019804 static void
19805f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019806 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019807 typval_T *rettv;
19808{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019809 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019810 tagname_T tn;
19811 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019812
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019813 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019814 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019815 fname = alloc(MAXPATHL);
19816 if (fname == NULL)
19817 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019818
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019819 for (first = TRUE; ; first = FALSE)
19820 if (get_tagfname(&tn, first, fname) == FAIL
19821 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019822 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019823 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019824 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019825}
19826
19827/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019828 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019829 */
19830 static void
19831f_taglist(argvars, rettv)
19832 typval_T *argvars;
19833 typval_T *rettv;
19834{
19835 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019836
19837 tag_pattern = get_tv_string(&argvars[0]);
19838
19839 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019840 if (*tag_pattern == NUL)
19841 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019842
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019843 if (rettv_list_alloc(rettv) == OK)
19844 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019845}
19846
19847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 * "tempname()" function
19849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019851f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019852 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854{
19855 static int x = 'A';
19856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019857 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019858 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859
19860 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19861 * names. Skip 'I' and 'O', they are used for shell redirection. */
19862 do
19863 {
19864 if (x == 'Z')
19865 x = '0';
19866 else if (x == '9')
19867 x = 'A';
19868 else
19869 {
19870#ifdef EBCDIC
19871 if (x == 'I')
19872 x = 'J';
19873 else if (x == 'R')
19874 x = 'S';
19875 else
19876#endif
19877 ++x;
19878 }
19879 } while (x == 'I' || x == 'O');
19880}
19881
19882/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019883 * "test(list)" function: Just checking the walls...
19884 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019885 static void
19886f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019887 typval_T *argvars UNUSED;
19888 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019889{
19890 /* Used for unit testing. Change the code below to your liking. */
19891#if 0
19892 listitem_T *li;
19893 list_T *l;
19894 char_u *bad, *good;
19895
19896 if (argvars[0].v_type != VAR_LIST)
19897 return;
19898 l = argvars[0].vval.v_list;
19899 if (l == NULL)
19900 return;
19901 li = l->lv_first;
19902 if (li == NULL)
19903 return;
19904 bad = get_tv_string(&li->li_tv);
19905 li = li->li_next;
19906 if (li == NULL)
19907 return;
19908 good = get_tv_string(&li->li_tv);
19909 rettv->vval.v_number = test_edit_score(bad, good);
19910#endif
19911}
19912
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019913#ifdef FEAT_FLOAT
19914/*
19915 * "tan()" function
19916 */
19917 static void
19918f_tan(argvars, rettv)
19919 typval_T *argvars;
19920 typval_T *rettv;
19921{
19922 float_T f;
19923
19924 rettv->v_type = VAR_FLOAT;
19925 if (get_float_arg(argvars, &f) == OK)
19926 rettv->vval.v_float = tan(f);
19927 else
19928 rettv->vval.v_float = 0.0;
19929}
19930
19931/*
19932 * "tanh()" function
19933 */
19934 static void
19935f_tanh(argvars, rettv)
19936 typval_T *argvars;
19937 typval_T *rettv;
19938{
19939 float_T f;
19940
19941 rettv->v_type = VAR_FLOAT;
19942 if (get_float_arg(argvars, &f) == OK)
19943 rettv->vval.v_float = tanh(f);
19944 else
19945 rettv->vval.v_float = 0.0;
19946}
19947#endif
19948
Bram Moolenaard52d9742005-08-21 22:20:28 +000019949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 * "tolower(string)" function
19951 */
19952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019953f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019954 typval_T *argvars;
19955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956{
19957 char_u *p;
19958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019959 p = vim_strsave(get_tv_string(&argvars[0]));
19960 rettv->v_type = VAR_STRING;
19961 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962
19963 if (p != NULL)
19964 while (*p != NUL)
19965 {
19966#ifdef FEAT_MBYTE
19967 int l;
19968
19969 if (enc_utf8)
19970 {
19971 int c, lc;
19972
19973 c = utf_ptr2char(p);
19974 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019975 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 /* TODO: reallocate string when byte count changes. */
19977 if (utf_char2len(lc) == l)
19978 utf_char2bytes(lc, p);
19979 p += l;
19980 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019981 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982 p += l; /* skip multi-byte character */
19983 else
19984#endif
19985 {
19986 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19987 ++p;
19988 }
19989 }
19990}
19991
19992/*
19993 * "toupper(string)" function
19994 */
19995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019997 typval_T *argvars;
19998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020000 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020001 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002}
20003
20004/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020005 * "tr(string, fromstr, tostr)" function
20006 */
20007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020008f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020009 typval_T *argvars;
20010 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020011{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020012 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020013 char_u *fromstr;
20014 char_u *tostr;
20015 char_u *p;
20016#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020017 int inlen;
20018 int fromlen;
20019 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020020 int idx;
20021 char_u *cpstr;
20022 int cplen;
20023 int first = TRUE;
20024#endif
20025 char_u buf[NUMBUFLEN];
20026 char_u buf2[NUMBUFLEN];
20027 garray_T ga;
20028
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020029 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020030 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20031 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020032
20033 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020034 rettv->v_type = VAR_STRING;
20035 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020036 if (fromstr == NULL || tostr == NULL)
20037 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020038 ga_init2(&ga, (int)sizeof(char), 80);
20039
20040#ifdef FEAT_MBYTE
20041 if (!has_mbyte)
20042#endif
20043 /* not multi-byte: fromstr and tostr must be the same length */
20044 if (STRLEN(fromstr) != STRLEN(tostr))
20045 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020046#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020047error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020048#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020049 EMSG2(_(e_invarg2), fromstr);
20050 ga_clear(&ga);
20051 return;
20052 }
20053
20054 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020055 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020056 {
20057#ifdef FEAT_MBYTE
20058 if (has_mbyte)
20059 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020060 inlen = (*mb_ptr2len)(in_str);
20061 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020062 cplen = inlen;
20063 idx = 0;
20064 for (p = fromstr; *p != NUL; p += fromlen)
20065 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020066 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020067 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020068 {
20069 for (p = tostr; *p != NUL; p += tolen)
20070 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020071 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020072 if (idx-- == 0)
20073 {
20074 cplen = tolen;
20075 cpstr = p;
20076 break;
20077 }
20078 }
20079 if (*p == NUL) /* tostr is shorter than fromstr */
20080 goto error;
20081 break;
20082 }
20083 ++idx;
20084 }
20085
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020086 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020087 {
20088 /* Check that fromstr and tostr have the same number of
20089 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020090 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020091 first = FALSE;
20092 for (p = tostr; *p != NUL; p += tolen)
20093 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020094 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020095 --idx;
20096 }
20097 if (idx != 0)
20098 goto error;
20099 }
20100
Bram Moolenaarcde88542015-08-11 19:14:00 +020020101 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020102 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020103 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020104
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020105 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020106 }
20107 else
20108#endif
20109 {
20110 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020111 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020112 if (p != NULL)
20113 ga_append(&ga, tostr[p - fromstr]);
20114 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020115 ga_append(&ga, *in_str);
20116 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020117 }
20118 }
20119
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020120 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020121 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020122 ga_append(&ga, NUL);
20123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020124 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020125}
20126
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020127#ifdef FEAT_FLOAT
20128/*
20129 * "trunc({float})" function
20130 */
20131 static void
20132f_trunc(argvars, rettv)
20133 typval_T *argvars;
20134 typval_T *rettv;
20135{
20136 float_T f;
20137
20138 rettv->v_type = VAR_FLOAT;
20139 if (get_float_arg(argvars, &f) == OK)
20140 /* trunc() is not in C90, use floor() or ceil() instead. */
20141 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20142 else
20143 rettv->vval.v_float = 0.0;
20144}
20145#endif
20146
Bram Moolenaar8299df92004-07-10 09:47:34 +000020147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 * "type(expr)" function
20149 */
20150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020151f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020152 typval_T *argvars;
20153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020155 int n;
20156
20157 switch (argvars[0].v_type)
20158 {
20159 case VAR_NUMBER: n = 0; break;
20160 case VAR_STRING: n = 1; break;
20161 case VAR_FUNC: n = 2; break;
20162 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020163 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020164#ifdef FEAT_FLOAT
20165 case VAR_FLOAT: n = 5; break;
20166#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020167 case VAR_SPECIAL:
20168 if (argvars[0].vval.v_number == VVAL_FALSE
20169 || argvars[0].vval.v_number == VVAL_TRUE)
20170 n = 6;
20171 else
20172 n = 7;
20173 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020174 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
20175 }
20176 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177}
20178
20179/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020180 * "undofile(name)" function
20181 */
20182 static void
20183f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010020184 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020185 typval_T *rettv;
20186{
20187 rettv->v_type = VAR_STRING;
20188#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020189 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020190 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020191
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020192 if (*fname == NUL)
20193 {
20194 /* If there is no file name there will be no undo file. */
20195 rettv->vval.v_string = NULL;
20196 }
20197 else
20198 {
20199 char_u *ffname = FullName_save(fname, FALSE);
20200
20201 if (ffname != NULL)
20202 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20203 vim_free(ffname);
20204 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020205 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020206#else
20207 rettv->vval.v_string = NULL;
20208#endif
20209}
20210
20211/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020212 * "undotree()" function
20213 */
20214 static void
20215f_undotree(argvars, rettv)
20216 typval_T *argvars UNUSED;
20217 typval_T *rettv;
20218{
20219 if (rettv_dict_alloc(rettv) == OK)
20220 {
20221 dict_T *dict = rettv->vval.v_dict;
20222 list_T *list;
20223
Bram Moolenaar730cde92010-06-27 05:18:54 +020020224 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020225 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020226 dict_add_nr_str(dict, "save_last",
20227 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020228 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20229 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020230 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020231
20232 list = list_alloc();
20233 if (list != NULL)
20234 {
20235 u_eval_tree(curbuf->b_u_oldhead, list);
20236 dict_add_list(dict, "entries", list);
20237 }
20238 }
20239}
20240
20241/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020242 * "values(dict)" function
20243 */
20244 static void
20245f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020246 typval_T *argvars;
20247 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020248{
20249 dict_list(argvars, rettv, 1);
20250}
20251
20252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 * "virtcol(string)" function
20254 */
20255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020256f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020257 typval_T *argvars;
20258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259{
20260 colnr_T vcol = 0;
20261 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020262 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020264 fp = var2fpos(&argvars[0], FALSE, &fnum);
20265 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20266 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 {
20268 getvvcol(curwin, fp, NULL, NULL, &vcol);
20269 ++vcol;
20270 }
20271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020272 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273}
20274
20275/*
20276 * "visualmode()" function
20277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020279f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020280 typval_T *argvars UNUSED;
20281 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 char_u str[2];
20284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020285 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 str[0] = curbuf->b_visual_mode_eval;
20287 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020288 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289
20290 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020291 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293}
20294
20295/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020296 * "wildmenumode()" function
20297 */
20298 static void
20299f_wildmenumode(argvars, rettv)
20300 typval_T *argvars UNUSED;
20301 typval_T *rettv UNUSED;
20302{
20303#ifdef FEAT_WILDMENU
20304 if (wild_menu_showing)
20305 rettv->vval.v_number = 1;
20306#endif
20307}
20308
20309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 * "winbufnr(nr)" function
20311 */
20312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020313f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020314 typval_T *argvars;
20315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316{
20317 win_T *wp;
20318
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020319 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020321 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020323 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324}
20325
20326/*
20327 * "wincol()" function
20328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020330f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020331 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333{
20334 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020335 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336}
20337
20338/*
20339 * "winheight(nr)" function
20340 */
20341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020342f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020343 typval_T *argvars;
20344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345{
20346 win_T *wp;
20347
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020348 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020350 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020352 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353}
20354
20355/*
20356 * "winline()" function
20357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020359f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362{
20363 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020364 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365}
20366
20367/*
20368 * "winnr()" function
20369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020371f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020372 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374{
20375 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020376
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020378 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020380 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381}
20382
20383/*
20384 * "winrestcmd()" function
20385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020387f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020388 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390{
20391#ifdef FEAT_WINDOWS
20392 win_T *wp;
20393 int winnr = 1;
20394 garray_T ga;
20395 char_u buf[50];
20396
20397 ga_init2(&ga, (int)sizeof(char), 70);
20398 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20399 {
20400 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20401 ga_concat(&ga, buf);
20402# ifdef FEAT_VERTSPLIT
20403 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20404 ga_concat(&ga, buf);
20405# endif
20406 ++winnr;
20407 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020408 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020410 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020412 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020414 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415}
20416
20417/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020418 * "winrestview()" function
20419 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020420 static void
20421f_winrestview(argvars, rettv)
20422 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020423 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020424{
20425 dict_T *dict;
20426
20427 if (argvars[0].v_type != VAR_DICT
20428 || (dict = argvars[0].vval.v_dict) == NULL)
20429 EMSG(_(e_invarg));
20430 else
20431 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020432 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20433 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20434 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20435 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020436#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020437 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20438 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020439#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020440 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20441 {
20442 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20443 curwin->w_set_curswant = FALSE;
20444 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020445
Bram Moolenaar82c25852014-05-28 16:47:16 +020020446 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20447 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020448#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020449 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20450 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020451#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020452 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20453 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20454 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20455 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020456
20457 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020458 win_new_height(curwin, curwin->w_height);
20459# ifdef FEAT_VERTSPLIT
20460 win_new_width(curwin, W_WIDTH(curwin));
20461# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020462 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020463
Bram Moolenaarb851a962014-10-31 15:45:52 +010020464 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020465 curwin->w_topline = 1;
20466 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20467 curwin->w_topline = curbuf->b_ml.ml_line_count;
20468#ifdef FEAT_DIFF
20469 check_topfill(curwin, TRUE);
20470#endif
20471 }
20472}
20473
20474/*
20475 * "winsaveview()" function
20476 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020477 static void
20478f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020479 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020480 typval_T *rettv;
20481{
20482 dict_T *dict;
20483
Bram Moolenaara800b422010-06-27 01:15:55 +020020484 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020485 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020486 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020487
20488 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20489 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20490#ifdef FEAT_VIRTUALEDIT
20491 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20492#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020493 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020494 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20495
20496 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20497#ifdef FEAT_DIFF
20498 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20499#endif
20500 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20501 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20502}
20503
20504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 * "winwidth(nr)" function
20506 */
20507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020508f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020509 typval_T *argvars;
20510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511{
20512 win_T *wp;
20513
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020514 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020516 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 else
20518#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020519 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020521 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522#endif
20523}
20524
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020526 * "wordcount()" function
20527 */
20528 static void
20529f_wordcount(argvars, rettv)
20530 typval_T *argvars UNUSED;
20531 typval_T *rettv;
20532{
20533 if (rettv_dict_alloc(rettv) == FAIL)
20534 return;
20535 cursor_pos_info(rettv->vval.v_dict);
20536}
20537
20538/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020539 * Write list of strings to file
20540 */
20541 static int
20542write_list(fd, list, binary)
20543 FILE *fd;
20544 list_T *list;
20545 int binary;
20546{
20547 listitem_T *li;
20548 int c;
20549 int ret = OK;
20550 char_u *s;
20551
20552 for (li = list->lv_first; li != NULL; li = li->li_next)
20553 {
20554 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20555 {
20556 if (*s == '\n')
20557 c = putc(NUL, fd);
20558 else
20559 c = putc(*s, fd);
20560 if (c == EOF)
20561 {
20562 ret = FAIL;
20563 break;
20564 }
20565 }
20566 if (!binary || li->li_next != NULL)
20567 if (putc('\n', fd) == EOF)
20568 {
20569 ret = FAIL;
20570 break;
20571 }
20572 if (ret == FAIL)
20573 {
20574 EMSG(_(e_write));
20575 break;
20576 }
20577 }
20578 return ret;
20579}
20580
20581/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020582 * "writefile()" function
20583 */
20584 static void
20585f_writefile(argvars, rettv)
20586 typval_T *argvars;
20587 typval_T *rettv;
20588{
20589 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020590 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020591 char_u *fname;
20592 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020593 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020594
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020595 if (check_restricted() || check_secure())
20596 return;
20597
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020598 if (argvars[0].v_type != VAR_LIST)
20599 {
20600 EMSG2(_(e_listarg), "writefile()");
20601 return;
20602 }
20603 if (argvars[0].vval.v_list == NULL)
20604 return;
20605
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020606 if (argvars[2].v_type != VAR_UNKNOWN)
20607 {
20608 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20609 binary = TRUE;
20610 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20611 append = TRUE;
20612 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020613
20614 /* Always open the file in binary mode, library functions have a mind of
20615 * their own about CR-LF conversion. */
20616 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020617 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20618 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020619 {
20620 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20621 ret = -1;
20622 }
20623 else
20624 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020625 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20626 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020627 fclose(fd);
20628 }
20629
20630 rettv->vval.v_number = ret;
20631}
20632
20633/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020634 * "xor(expr, expr)" function
20635 */
20636 static void
20637f_xor(argvars, rettv)
20638 typval_T *argvars;
20639 typval_T *rettv;
20640{
20641 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20642 ^ get_tv_number_chk(&argvars[1], NULL);
20643}
20644
20645
20646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020648 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 */
20650 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020651var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020652 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020653 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020654 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020656 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020658 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659
Bram Moolenaara5525202006-03-02 22:52:09 +000020660 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020661 if (varp->v_type == VAR_LIST)
20662 {
20663 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020664 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020665 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020666 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020667
20668 l = varp->vval.v_list;
20669 if (l == NULL)
20670 return NULL;
20671
20672 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020673 pos.lnum = list_find_nr(l, 0L, &error);
20674 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020675 return NULL; /* invalid line number */
20676
20677 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020678 pos.col = list_find_nr(l, 1L, &error);
20679 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020680 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020681 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020682
20683 /* We accept "$" for the column number: last column. */
20684 li = list_find(l, 1L);
20685 if (li != NULL && li->li_tv.v_type == VAR_STRING
20686 && li->li_tv.vval.v_string != NULL
20687 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20688 pos.col = len + 1;
20689
Bram Moolenaara5525202006-03-02 22:52:09 +000020690 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020691 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020692 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020693 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020694
Bram Moolenaara5525202006-03-02 22:52:09 +000020695#ifdef FEAT_VIRTUALEDIT
20696 /* Get the virtual offset. Defaults to zero. */
20697 pos.coladd = list_find_nr(l, 2L, &error);
20698 if (error)
20699 pos.coladd = 0;
20700#endif
20701
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020702 return &pos;
20703 }
20704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020705 name = get_tv_string_chk(varp);
20706 if (name == NULL)
20707 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020708 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020710 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20711 {
20712 if (VIsual_active)
20713 return &VIsual;
20714 return &curwin->w_cursor;
20715 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020716 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020718 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20720 return NULL;
20721 return pp;
20722 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020723
20724#ifdef FEAT_VIRTUALEDIT
20725 pos.coladd = 0;
20726#endif
20727
Bram Moolenaar477933c2007-07-17 14:32:23 +000020728 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020729 {
20730 pos.col = 0;
20731 if (name[1] == '0') /* "w0": first visible line */
20732 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020733 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020734 pos.lnum = curwin->w_topline;
20735 return &pos;
20736 }
20737 else if (name[1] == '$') /* "w$": last visible line */
20738 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020739 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020740 pos.lnum = curwin->w_botline - 1;
20741 return &pos;
20742 }
20743 }
20744 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020746 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 {
20748 pos.lnum = curbuf->b_ml.ml_line_count;
20749 pos.col = 0;
20750 }
20751 else
20752 {
20753 pos.lnum = curwin->w_cursor.lnum;
20754 pos.col = (colnr_T)STRLEN(ml_get_curline());
20755 }
20756 return &pos;
20757 }
20758 return NULL;
20759}
20760
20761/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020762 * Convert list in "arg" into a position and optional file number.
20763 * When "fnump" is NULL there is no file number, only 3 items.
20764 * Note that the column is passed on as-is, the caller may want to decrement
20765 * it to use 1 for the first column.
20766 * Return FAIL when conversion is not possible, doesn't check the position for
20767 * validity.
20768 */
20769 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020770list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020771 typval_T *arg;
20772 pos_T *posp;
20773 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020774 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020775{
20776 list_T *l = arg->vval.v_list;
20777 long i = 0;
20778 long n;
20779
Bram Moolenaar493c1782014-05-28 14:34:46 +020020780 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20781 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020782 if (arg->v_type != VAR_LIST
20783 || l == NULL
20784 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020785 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020786 return FAIL;
20787
20788 if (fnump != NULL)
20789 {
20790 n = list_find_nr(l, i++, NULL); /* fnum */
20791 if (n < 0)
20792 return FAIL;
20793 if (n == 0)
20794 n = curbuf->b_fnum; /* current buffer */
20795 *fnump = n;
20796 }
20797
20798 n = list_find_nr(l, i++, NULL); /* lnum */
20799 if (n < 0)
20800 return FAIL;
20801 posp->lnum = n;
20802
20803 n = list_find_nr(l, i++, NULL); /* col */
20804 if (n < 0)
20805 return FAIL;
20806 posp->col = n;
20807
20808#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020809 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020810 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020811 posp->coladd = 0;
20812 else
20813 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020814#endif
20815
Bram Moolenaar493c1782014-05-28 14:34:46 +020020816 if (curswantp != NULL)
20817 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20818
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020819 return OK;
20820}
20821
20822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 * Get the length of an environment variable name.
20824 * Advance "arg" to the first character after the name.
20825 * Return 0 for error.
20826 */
20827 static int
20828get_env_len(arg)
20829 char_u **arg;
20830{
20831 char_u *p;
20832 int len;
20833
20834 for (p = *arg; vim_isIDc(*p); ++p)
20835 ;
20836 if (p == *arg) /* no name found */
20837 return 0;
20838
20839 len = (int)(p - *arg);
20840 *arg = p;
20841 return len;
20842}
20843
20844/*
20845 * Get the length of the name of a function or internal variable.
20846 * "arg" is advanced to the first non-white character after the name.
20847 * Return 0 if something is wrong.
20848 */
20849 static int
20850get_id_len(arg)
20851 char_u **arg;
20852{
20853 char_u *p;
20854 int len;
20855
20856 /* Find the end of the name. */
20857 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020858 {
20859 if (*p == ':')
20860 {
20861 /* "s:" is start of "s:var", but "n:" is not and can be used in
20862 * slice "[n:]". Also "xx:" is not a namespace. */
20863 len = (int)(p - *arg);
20864 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20865 || len > 1)
20866 break;
20867 }
20868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 if (p == *arg) /* no name found */
20870 return 0;
20871
20872 len = (int)(p - *arg);
20873 *arg = skipwhite(p);
20874
20875 return len;
20876}
20877
20878/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020879 * Get the length of the name of a variable or function.
20880 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020882 * Return -1 if curly braces expansion failed.
20883 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884 * If the name contains 'magic' {}'s, expand them and return the
20885 * expanded name in an allocated string via 'alias' - caller must free.
20886 */
20887 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020888get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 char_u **arg;
20890 char_u **alias;
20891 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020892 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893{
20894 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 char_u *p;
20896 char_u *expr_start;
20897 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898
20899 *alias = NULL; /* default to no alias */
20900
20901 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20902 && (*arg)[2] == (int)KE_SNR)
20903 {
20904 /* hard coded <SNR>, already translated */
20905 *arg += 3;
20906 return get_id_len(arg) + 3;
20907 }
20908 len = eval_fname_script(*arg);
20909 if (len > 0)
20910 {
20911 /* literal "<SID>", "s:" or "<SNR>" */
20912 *arg += len;
20913 }
20914
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020916 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020918 p = find_name_end(*arg, &expr_start, &expr_end,
20919 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 if (expr_start != NULL)
20921 {
20922 char_u *temp_string;
20923
20924 if (!evaluate)
20925 {
20926 len += (int)(p - *arg);
20927 *arg = skipwhite(p);
20928 return len;
20929 }
20930
20931 /*
20932 * Include any <SID> etc in the expanded string:
20933 * Thus the -len here.
20934 */
20935 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20936 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020937 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 *alias = temp_string;
20939 *arg = skipwhite(p);
20940 return (int)STRLEN(temp_string);
20941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942
20943 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020944 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 EMSG2(_(e_invexpr2), *arg);
20946
20947 return len;
20948}
20949
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020950/*
20951 * Find the end of a variable or function name, taking care of magic braces.
20952 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20953 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020954 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020955 * Return a pointer to just after the name. Equal to "arg" if there is no
20956 * valid name.
20957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020959find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 char_u *arg;
20961 char_u **expr_start;
20962 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020963 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020965 int mb_nest = 0;
20966 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020968 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020970 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020972 *expr_start = NULL;
20973 *expr_end = NULL;
20974 }
20975
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020976 /* Quick check for valid starting character. */
20977 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20978 return arg;
20979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020980 for (p = arg; *p != NUL
20981 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020982 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020983 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020984 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020985 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020986 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020987 if (*p == '\'')
20988 {
20989 /* skip over 'string' to avoid counting [ and ] inside it. */
20990 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20991 ;
20992 if (*p == NUL)
20993 break;
20994 }
20995 else if (*p == '"')
20996 {
20997 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20998 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20999 if (*p == '\\' && p[1] != NUL)
21000 ++p;
21001 if (*p == NUL)
21002 break;
21003 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021004 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21005 {
21006 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021007 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021008 len = (int)(p - arg);
21009 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021010 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021011 break;
21012 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021014 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021016 if (*p == '[')
21017 ++br_nest;
21018 else if (*p == ']')
21019 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021022 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021024 if (*p == '{')
21025 {
21026 mb_nest++;
21027 if (expr_start != NULL && *expr_start == NULL)
21028 *expr_start = p;
21029 }
21030 else if (*p == '}')
21031 {
21032 mb_nest--;
21033 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21034 *expr_end = p;
21035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037 }
21038
21039 return p;
21040}
21041
21042/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021043 * Expands out the 'magic' {}'s in a variable/function name.
21044 * Note that this can call itself recursively, to deal with
21045 * constructs like foo{bar}{baz}{bam}
21046 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21047 * "in_start" ^
21048 * "expr_start" ^
21049 * "expr_end" ^
21050 * "in_end" ^
21051 *
21052 * Returns a new allocated string, which the caller must free.
21053 * Returns NULL for failure.
21054 */
21055 static char_u *
21056make_expanded_name(in_start, expr_start, expr_end, in_end)
21057 char_u *in_start;
21058 char_u *expr_start;
21059 char_u *expr_end;
21060 char_u *in_end;
21061{
21062 char_u c1;
21063 char_u *retval = NULL;
21064 char_u *temp_result;
21065 char_u *nextcmd = NULL;
21066
21067 if (expr_end == NULL || in_end == NULL)
21068 return NULL;
21069 *expr_start = NUL;
21070 *expr_end = NUL;
21071 c1 = *in_end;
21072 *in_end = NUL;
21073
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021074 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021075 if (temp_result != NULL && nextcmd == NULL)
21076 {
21077 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21078 + (in_end - expr_end) + 1));
21079 if (retval != NULL)
21080 {
21081 STRCPY(retval, in_start);
21082 STRCAT(retval, temp_result);
21083 STRCAT(retval, expr_end + 1);
21084 }
21085 }
21086 vim_free(temp_result);
21087
21088 *in_end = c1; /* put char back for error messages */
21089 *expr_start = '{';
21090 *expr_end = '}';
21091
21092 if (retval != NULL)
21093 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021094 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021095 if (expr_start != NULL)
21096 {
21097 /* Further expansion! */
21098 temp_result = make_expanded_name(retval, expr_start,
21099 expr_end, temp_result);
21100 vim_free(retval);
21101 retval = temp_result;
21102 }
21103 }
21104
21105 return retval;
21106}
21107
21108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021110 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 */
21112 static int
21113eval_isnamec(c)
21114 int c;
21115{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021116 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21117}
21118
21119/*
21120 * Return TRUE if character "c" can be used as the first character in a
21121 * variable or function name (excluding '{' and '}').
21122 */
21123 static int
21124eval_isnamec1(c)
21125 int c;
21126{
21127 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128}
21129
21130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 * Set number v: variable to "val".
21132 */
21133 void
21134set_vim_var_nr(idx, val)
21135 int idx;
21136 long val;
21137{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021138 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139}
21140
21141/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021142 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 */
21144 long
21145get_vim_var_nr(idx)
21146 int idx;
21147{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021148 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149}
21150
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021151/*
21152 * Get string v: variable value. Uses a static buffer, can only be used once.
21153 */
21154 char_u *
21155get_vim_var_str(idx)
21156 int idx;
21157{
21158 return get_tv_string(&vimvars[idx].vv_tv);
21159}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021160
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021162 * Get List v: variable value. Caller must take care of reference count when
21163 * needed.
21164 */
21165 list_T *
21166get_vim_var_list(idx)
21167 int idx;
21168{
21169 return vimvars[idx].vv_list;
21170}
21171
21172/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021173 * Set v:char to character "c".
21174 */
21175 void
21176set_vim_var_char(c)
21177 int c;
21178{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021179 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021180
21181#ifdef FEAT_MBYTE
21182 if (has_mbyte)
21183 buf[(*mb_char2bytes)(c, buf)] = NUL;
21184 else
21185#endif
21186 {
21187 buf[0] = c;
21188 buf[1] = NUL;
21189 }
21190 set_vim_var_string(VV_CHAR, buf, -1);
21191}
21192
21193/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021194 * Set v:count to "count" and v:count1 to "count1".
21195 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 */
21197 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021198set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 long count;
21200 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021201 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021203 if (set_prevcount)
21204 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021205 vimvars[VV_COUNT].vv_nr = count;
21206 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207}
21208
21209/*
21210 * Set string v: variable to a copy of "val".
21211 */
21212 void
21213set_vim_var_string(idx, val, len)
21214 int idx;
21215 char_u *val;
21216 int len; /* length of "val" to use or -1 (whole string) */
21217{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021218 /* Need to do this (at least) once, since we can't initialize a union.
21219 * Will always be invoked when "v:progname" is set. */
21220 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21221
Bram Moolenaare9a41262005-01-15 22:18:47 +000021222 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021224 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021226 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021228 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229}
21230
21231/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021232 * Set List v: variable to "val".
21233 */
21234 void
21235set_vim_var_list(idx, val)
21236 int idx;
21237 list_T *val;
21238{
21239 list_unref(vimvars[idx].vv_list);
21240 vimvars[idx].vv_list = val;
21241 if (val != NULL)
21242 ++val->lv_refcount;
21243}
21244
21245/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021246 * Set Dictionary v: variable to "val".
21247 */
21248 void
21249set_vim_var_dict(idx, val)
21250 int idx;
21251 dict_T *val;
21252{
21253 int todo;
21254 hashitem_T *hi;
21255
21256 dict_unref(vimvars[idx].vv_dict);
21257 vimvars[idx].vv_dict = val;
21258 if (val != NULL)
21259 {
21260 ++val->dv_refcount;
21261
21262 /* Set readonly */
21263 todo = (int)val->dv_hashtab.ht_used;
21264 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21265 {
21266 if (HASHITEM_EMPTY(hi))
21267 continue;
21268 --todo;
21269 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21270 }
21271 }
21272}
21273
21274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275 * Set v:register if needed.
21276 */
21277 void
21278set_reg_var(c)
21279 int c;
21280{
21281 char_u regname;
21282
21283 if (c == 0 || c == ' ')
21284 regname = '"';
21285 else
21286 regname = c;
21287 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021288 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 set_vim_var_string(VV_REG, &regname, 1);
21290}
21291
21292/*
21293 * Get or set v:exception. If "oldval" == NULL, return the current value.
21294 * Otherwise, restore the value to "oldval" and return NULL.
21295 * Must always be called in pairs to save and restore v:exception! Does not
21296 * take care of memory allocations.
21297 */
21298 char_u *
21299v_exception(oldval)
21300 char_u *oldval;
21301{
21302 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021303 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304
Bram Moolenaare9a41262005-01-15 22:18:47 +000021305 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 return NULL;
21307}
21308
21309/*
21310 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21311 * Otherwise, restore the value to "oldval" and return NULL.
21312 * Must always be called in pairs to save and restore v:throwpoint! Does not
21313 * take care of memory allocations.
21314 */
21315 char_u *
21316v_throwpoint(oldval)
21317 char_u *oldval;
21318{
21319 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021320 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321
Bram Moolenaare9a41262005-01-15 22:18:47 +000021322 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 return NULL;
21324}
21325
21326#if defined(FEAT_AUTOCMD) || defined(PROTO)
21327/*
21328 * Set v:cmdarg.
21329 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21330 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21331 * Must always be called in pairs!
21332 */
21333 char_u *
21334set_cmdarg(eap, oldarg)
21335 exarg_T *eap;
21336 char_u *oldarg;
21337{
21338 char_u *oldval;
21339 char_u *newval;
21340 unsigned len;
21341
Bram Moolenaare9a41262005-01-15 22:18:47 +000021342 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021343 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021345 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021346 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021347 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348 }
21349
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021350 if (eap->force_bin == FORCE_BIN)
21351 len = 6;
21352 else if (eap->force_bin == FORCE_NOBIN)
21353 len = 8;
21354 else
21355 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021356
21357 if (eap->read_edit)
21358 len += 7;
21359
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021360 if (eap->force_ff != 0)
21361 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21362# ifdef FEAT_MBYTE
21363 if (eap->force_enc != 0)
21364 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021365 if (eap->bad_char != 0)
21366 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021367# endif
21368
21369 newval = alloc(len + 1);
21370 if (newval == NULL)
21371 return NULL;
21372
21373 if (eap->force_bin == FORCE_BIN)
21374 sprintf((char *)newval, " ++bin");
21375 else if (eap->force_bin == FORCE_NOBIN)
21376 sprintf((char *)newval, " ++nobin");
21377 else
21378 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021379
21380 if (eap->read_edit)
21381 STRCAT(newval, " ++edit");
21382
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021383 if (eap->force_ff != 0)
21384 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21385 eap->cmd + eap->force_ff);
21386# ifdef FEAT_MBYTE
21387 if (eap->force_enc != 0)
21388 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21389 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021390 if (eap->bad_char == BAD_KEEP)
21391 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21392 else if (eap->bad_char == BAD_DROP)
21393 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21394 else if (eap->bad_char != 0)
21395 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021396# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021397 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021398 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399}
21400#endif
21401
21402/*
21403 * Get the value of internal variable "name".
21404 * Return OK or FAIL.
21405 */
21406 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021407get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408 char_u *name;
21409 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021410 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021411 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021412 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021413 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414{
21415 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021416 typval_T *tv = NULL;
21417 typval_T atv;
21418 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420
21421 /* truncate the name, so that we can use strcmp() */
21422 cc = name[len];
21423 name[len] = NUL;
21424
21425 /*
21426 * Check for "b:changedtick".
21427 */
21428 if (STRCMP(name, "b:changedtick") == 0)
21429 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021430 atv.v_type = VAR_NUMBER;
21431 atv.vval.v_number = curbuf->b_changedtick;
21432 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 }
21434
21435 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021436 * Check for user-defined variables.
21437 */
21438 else
21439 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021440 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021442 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021443 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021444 if (dip != NULL)
21445 *dip = v;
21446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 }
21448
Bram Moolenaare9a41262005-01-15 22:18:47 +000021449 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021451 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021452 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 ret = FAIL;
21454 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021455 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021456 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457
21458 name[len] = cc;
21459
21460 return ret;
21461}
21462
21463/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021464 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21465 * Also handle function call with Funcref variable: func(expr)
21466 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21467 */
21468 static int
21469handle_subscript(arg, rettv, evaluate, verbose)
21470 char_u **arg;
21471 typval_T *rettv;
21472 int evaluate; /* do more than finding the end */
21473 int verbose; /* give error messages */
21474{
21475 int ret = OK;
21476 dict_T *selfdict = NULL;
21477 char_u *s;
21478 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021479 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021480
21481 while (ret == OK
21482 && (**arg == '['
21483 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021484 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021485 && !vim_iswhite(*(*arg - 1)))
21486 {
21487 if (**arg == '(')
21488 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021489 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021490 if (evaluate)
21491 {
21492 functv = *rettv;
21493 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021494
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021495 /* Invoke the function. Recursive! */
21496 s = functv.vval.v_string;
21497 }
21498 else
21499 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021500 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021501 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21502 &len, evaluate, selfdict);
21503
21504 /* Clear the funcref afterwards, so that deleting it while
21505 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021506 if (evaluate)
21507 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021508
21509 /* Stop the expression evaluation when immediately aborting on
21510 * error, or when an interrupt occurred or an exception was thrown
21511 * but not caught. */
21512 if (aborting())
21513 {
21514 if (ret == OK)
21515 clear_tv(rettv);
21516 ret = FAIL;
21517 }
21518 dict_unref(selfdict);
21519 selfdict = NULL;
21520 }
21521 else /* **arg == '[' || **arg == '.' */
21522 {
21523 dict_unref(selfdict);
21524 if (rettv->v_type == VAR_DICT)
21525 {
21526 selfdict = rettv->vval.v_dict;
21527 if (selfdict != NULL)
21528 ++selfdict->dv_refcount;
21529 }
21530 else
21531 selfdict = NULL;
21532 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21533 {
21534 clear_tv(rettv);
21535 ret = FAIL;
21536 }
21537 }
21538 }
21539 dict_unref(selfdict);
21540 return ret;
21541}
21542
21543/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021544 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021545 * value).
21546 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021547 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021548alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021549{
Bram Moolenaar33570922005-01-25 22:26:29 +000021550 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021551}
21552
21553/*
21554 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 * The string "s" must have been allocated, it is consumed.
21556 * Return NULL for out of memory, the variable otherwise.
21557 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021558 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021559alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 char_u *s;
21561{
Bram Moolenaar33570922005-01-25 22:26:29 +000021562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021564 rettv = alloc_tv();
21565 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021567 rettv->v_type = VAR_STRING;
21568 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569 }
21570 else
21571 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021572 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573}
21574
21575/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021576 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021578 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021579free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021580 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581{
21582 if (varp != NULL)
21583 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021584 switch (varp->v_type)
21585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021586 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021587 func_unref(varp->vval.v_string);
21588 /*FALLTHROUGH*/
21589 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021590 vim_free(varp->vval.v_string);
21591 break;
21592 case VAR_LIST:
21593 list_unref(varp->vval.v_list);
21594 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021595 case VAR_DICT:
21596 dict_unref(varp->vval.v_dict);
21597 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021598 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021599#ifdef FEAT_FLOAT
21600 case VAR_FLOAT:
21601#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021602 case VAR_UNKNOWN:
21603 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021604 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021605 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021606 break;
21607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 vim_free(varp);
21609 }
21610}
21611
21612/*
21613 * Free the memory for a variable value and set the value to NULL or 0.
21614 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021615 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021616clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021617 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618{
21619 if (varp != NULL)
21620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021621 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021623 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021624 func_unref(varp->vval.v_string);
21625 /*FALLTHROUGH*/
21626 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021627 vim_free(varp->vval.v_string);
21628 varp->vval.v_string = NULL;
21629 break;
21630 case VAR_LIST:
21631 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021632 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021633 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021634 case VAR_DICT:
21635 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021636 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021637 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021638 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021639 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021640 varp->vval.v_number = 0;
21641 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021642#ifdef FEAT_FLOAT
21643 case VAR_FLOAT:
21644 varp->vval.v_float = 0.0;
21645 break;
21646#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021647 case VAR_UNKNOWN:
21648 break;
21649 default:
21650 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021652 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 }
21654}
21655
21656/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021657 * Set the value of a variable to NULL without freeing items.
21658 */
21659 static void
21660init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021661 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021662{
21663 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021664 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021665}
21666
21667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 * Get the number value of a variable.
21669 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021670 * For incompatible types, return 0.
21671 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21672 * caller of incompatible types: it sets *denote to TRUE if "denote"
21673 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 */
21675 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021676get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021677 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021679 int error = FALSE;
21680
21681 return get_tv_number_chk(varp, &error); /* return 0L on error */
21682}
21683
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021684 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021685get_tv_number_chk(varp, denote)
21686 typval_T *varp;
21687 int *denote;
21688{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021689 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021691 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021692 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021693 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021694 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021695#ifdef FEAT_FLOAT
21696 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021697 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021698 break;
21699#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021700 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021701 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021702 break;
21703 case VAR_STRING:
21704 if (varp->vval.v_string != NULL)
21705 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021706 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021707 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021708 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021709 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021710 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021711 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021712 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021713 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021714 case VAR_SPECIAL:
21715 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21716 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021717 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021718 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021719 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021720 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021721 if (denote == NULL) /* useful for values that must be unsigned */
21722 n = -1;
21723 else
21724 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021725 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726}
21727
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021728#ifdef FEAT_FLOAT
21729 static float_T
21730get_tv_float(varp)
21731 typval_T *varp;
21732{
21733 switch (varp->v_type)
21734 {
21735 case VAR_NUMBER:
21736 return (float_T)(varp->vval.v_number);
21737#ifdef FEAT_FLOAT
21738 case VAR_FLOAT:
21739 return varp->vval.v_float;
21740 break;
21741#endif
21742 case VAR_FUNC:
21743 EMSG(_("E891: Using a Funcref as a Float"));
21744 break;
21745 case VAR_STRING:
21746 EMSG(_("E892: Using a String as a Float"));
21747 break;
21748 case VAR_LIST:
21749 EMSG(_("E893: Using a List as a Float"));
21750 break;
21751 case VAR_DICT:
21752 EMSG(_("E894: Using a Dictionary as a Float"));
21753 break;
21754 default:
21755 EMSG2(_(e_intern2), "get_tv_float()");
21756 break;
21757 }
21758 return 0;
21759}
21760#endif
21761
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021763 * Get the lnum from the first argument.
21764 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021765 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766 */
21767 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021768get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021769 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770{
Bram Moolenaar33570922005-01-25 22:26:29 +000021771 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772 linenr_T lnum;
21773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021774 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 if (lnum == 0) /* no valid number, try using line() */
21776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021777 rettv.v_type = VAR_NUMBER;
21778 f_line(argvars, &rettv);
21779 lnum = rettv.vval.v_number;
21780 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 }
21782 return lnum;
21783}
21784
21785/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021786 * Get the lnum from the first argument.
21787 * Also accepts "$", then "buf" is used.
21788 * Returns 0 on error.
21789 */
21790 static linenr_T
21791get_tv_lnum_buf(argvars, buf)
21792 typval_T *argvars;
21793 buf_T *buf;
21794{
21795 if (argvars[0].v_type == VAR_STRING
21796 && argvars[0].vval.v_string != NULL
21797 && argvars[0].vval.v_string[0] == '$'
21798 && buf != NULL)
21799 return buf->b_ml.ml_line_count;
21800 return get_tv_number_chk(&argvars[0], NULL);
21801}
21802
21803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 * Get the string value of a variable.
21805 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021806 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21807 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 * If the String variable has never been set, return an empty string.
21809 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021810 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21811 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812 */
21813 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021814get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021815 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816{
21817 static char_u mybuf[NUMBUFLEN];
21818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021819 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820}
21821
21822 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021823get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021824 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021825 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021827 char_u *res = get_tv_string_buf_chk(varp, buf);
21828
21829 return res != NULL ? res : (char_u *)"";
21830}
21831
Bram Moolenaar7d647822014-04-05 21:28:56 +020021832/*
21833 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21834 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021835 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021836get_tv_string_chk(varp)
21837 typval_T *varp;
21838{
21839 static char_u mybuf[NUMBUFLEN];
21840
21841 return get_tv_string_buf_chk(varp, mybuf);
21842}
21843
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021844 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021845get_tv_string_buf_chk(varp, buf)
21846 typval_T *varp;
21847 char_u *buf;
21848{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021849 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021851 case VAR_NUMBER:
21852 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21853 return buf;
21854 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021855 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021856 break;
21857 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021858 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021859 break;
21860 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021861 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021862 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021863#ifdef FEAT_FLOAT
21864 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021865 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021866 break;
21867#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021868 case VAR_STRING:
21869 if (varp->vval.v_string != NULL)
21870 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021871 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021872 case VAR_SPECIAL:
21873 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21874 return buf;
21875
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021876 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021877 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021878 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021880 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881}
21882
21883/*
21884 * Find variable "name" in the list of variables.
21885 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021886 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021887 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021888 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021890 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021891find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021893 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021894 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021897 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898
Bram Moolenaara7043832005-01-21 11:56:39 +000021899 ht = find_var_ht(name, &varname);
21900 if (htp != NULL)
21901 *htp = ht;
21902 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021904 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905}
21906
21907/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021908 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021909 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021911 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021912find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021913 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021914 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021915 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021916 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021917{
Bram Moolenaar33570922005-01-25 22:26:29 +000021918 hashitem_T *hi;
21919
21920 if (*varname == NUL)
21921 {
21922 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021923 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021924 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021925 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021926 case 'g': return &globvars_var;
21927 case 'v': return &vimvars_var;
21928 case 'b': return &curbuf->b_bufvar;
21929 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021930#ifdef FEAT_WINDOWS
21931 case 't': return &curtab->tp_winvar;
21932#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021933 case 'l': return current_funccal == NULL
21934 ? NULL : &current_funccal->l_vars_var;
21935 case 'a': return current_funccal == NULL
21936 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021937 }
21938 return NULL;
21939 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021940
21941 hi = hash_find(ht, varname);
21942 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021943 {
21944 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021945 * worked find the variable again. Don't auto-load a script if it was
21946 * loaded already, otherwise it would be loaded every time when
21947 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021948 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021949 {
21950 /* Note: script_autoload() may make "hi" invalid. It must either
21951 * be obtained again or not used. */
21952 if (!script_autoload(varname, FALSE) || aborting())
21953 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021954 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021955 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021956 if (HASHITEM_EMPTY(hi))
21957 return NULL;
21958 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021959 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021960}
21961
21962/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021964 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021965 * Set "varname" to the start of name without ':'.
21966 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021967 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021968find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969 char_u *name;
21970 char_u **varname;
21971{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021972 hashitem_T *hi;
21973
Bram Moolenaar73627d02015-08-11 15:46:09 +020021974 if (name[0] == NUL)
21975 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 if (name[1] != ':')
21977 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021978 /* The name must not start with a colon or #. */
21979 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980 return NULL;
21981 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021982
21983 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021984 hi = hash_find(&compat_hashtab, name);
21985 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021986 return &compat_hashtab;
21987
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021989 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021990 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 }
21992 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021993 if (*name == 'g') /* global variable */
21994 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021995 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21996 */
21997 if (vim_strchr(name + 2, ':') != NULL
21998 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021999 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022001 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022003 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022004#ifdef FEAT_WINDOWS
22005 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022006 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022007#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022008 if (*name == 'v') /* v: variable */
22009 return &vimvarht;
22010 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022011 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022012 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022013 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 if (*name == 's' /* script variable */
22015 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22016 return &SCRIPT_VARS(current_SID);
22017 return NULL;
22018}
22019
22020/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022021 * Get function call environment based on bactrace debug level
22022 */
22023 static funccall_T *
22024get_funccal()
22025{
22026 int i;
22027 funccall_T *funccal;
22028 funccall_T *temp_funccal;
22029
22030 funccal = current_funccal;
22031 if (debug_backtrace_level > 0)
22032 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022033 for (i = 0; i < debug_backtrace_level; i++)
22034 {
22035 temp_funccal = funccal->caller;
22036 if (temp_funccal)
22037 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022038 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022039 /* backtrace level overflow. reset to max */
22040 debug_backtrace_level = i;
22041 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022042 }
22043 return funccal;
22044}
22045
22046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022048 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 * Returns NULL when it doesn't exist.
22050 */
22051 char_u *
22052get_var_value(name)
22053 char_u *name;
22054{
Bram Moolenaar33570922005-01-25 22:26:29 +000022055 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022057 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058 if (v == NULL)
22059 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022060 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061}
22062
22063/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022064 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 * sourcing this script and when executing functions defined in the script.
22066 */
22067 void
22068new_script_vars(id)
22069 scid_T id;
22070{
Bram Moolenaara7043832005-01-21 11:56:39 +000022071 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022072 hashtab_T *ht;
22073 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022074
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22076 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022077 /* Re-allocating ga_data means that an ht_array pointing to
22078 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022079 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022080 for (i = 1; i <= ga_scripts.ga_len; ++i)
22081 {
22082 ht = &SCRIPT_VARS(i);
22083 if (ht->ht_mask == HT_INIT_SIZE - 1)
22084 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022085 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022086 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022087 }
22088
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 while (ga_scripts.ga_len < id)
22090 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022091 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022092 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022093 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 }
22096 }
22097}
22098
22099/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022100 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22101 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 */
22103 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022104init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000022105 dict_T *dict;
22106 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022107 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108{
Bram Moolenaar33570922005-01-25 22:26:29 +000022109 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022110 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022111 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022112 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022113 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022114 dict_var->di_tv.vval.v_dict = dict;
22115 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022116 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022117 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22118 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119}
22120
22121/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022122 * Unreference a dictionary initialized by init_var_dict().
22123 */
22124 void
22125unref_var_dict(dict)
22126 dict_T *dict;
22127{
22128 /* Now the dict needs to be freed if no one else is using it, go back to
22129 * normal reference counting. */
22130 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22131 dict_unref(dict);
22132}
22133
22134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022136 * Frees all allocated variables and the value they contain.
22137 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138 */
22139 void
Bram Moolenaara7043832005-01-21 11:56:39 +000022140vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022141 hashtab_T *ht;
22142{
22143 vars_clear_ext(ht, TRUE);
22144}
22145
22146/*
22147 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22148 */
22149 static void
22150vars_clear_ext(ht, free_val)
22151 hashtab_T *ht;
22152 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153{
Bram Moolenaara7043832005-01-21 11:56:39 +000022154 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022155 hashitem_T *hi;
22156 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157
Bram Moolenaar33570922005-01-25 22:26:29 +000022158 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022159 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022160 for (hi = ht->ht_array; todo > 0; ++hi)
22161 {
22162 if (!HASHITEM_EMPTY(hi))
22163 {
22164 --todo;
22165
Bram Moolenaar33570922005-01-25 22:26:29 +000022166 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022167 * ht_array might change then. hash_clear() takes care of it
22168 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022169 v = HI2DI(hi);
22170 if (free_val)
22171 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022172 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022173 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022174 }
22175 }
22176 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022177 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178}
22179
Bram Moolenaara7043832005-01-21 11:56:39 +000022180/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022181 * Delete a variable from hashtab "ht" at item "hi".
22182 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000022185delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000022186 hashtab_T *ht;
22187 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188{
Bram Moolenaar33570922005-01-25 22:26:29 +000022189 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022190
22191 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022192 clear_tv(&di->di_tv);
22193 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194}
22195
22196/*
22197 * List the value of one internal variable.
22198 */
22199 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022200list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000022201 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022203 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022205 char_u *tofree;
22206 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022207 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022208
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022209 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022210 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022211 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022212 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213}
22214
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022216list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 char_u *prefix;
22218 char_u *name;
22219 int type;
22220 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022221 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222{
Bram Moolenaar31859182007-08-14 20:41:13 +000022223 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22224 msg_start();
22225 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 if (name != NULL) /* "a:" vars don't have a name stored */
22227 msg_puts(name);
22228 msg_putchar(' ');
22229 msg_advance(22);
22230 if (type == VAR_NUMBER)
22231 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022232 else if (type == VAR_FUNC)
22233 msg_putchar('*');
22234 else if (type == VAR_LIST)
22235 {
22236 msg_putchar('[');
22237 if (*string == '[')
22238 ++string;
22239 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022240 else if (type == VAR_DICT)
22241 {
22242 msg_putchar('{');
22243 if (*string == '{')
22244 ++string;
22245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 else
22247 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022248
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022250
22251 if (type == VAR_FUNC)
22252 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022253 if (*first)
22254 {
22255 msg_clr_eos();
22256 *first = FALSE;
22257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258}
22259
22260/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022261 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 * If the variable already exists, the value is updated.
22263 * Otherwise the variable is created.
22264 */
22265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022266set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022268 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022269 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270{
Bram Moolenaar33570922005-01-25 22:26:29 +000022271 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022273 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022275 ht = find_var_ht(name, &varname);
22276 if (ht == NULL || *varname == NUL)
22277 {
22278 EMSG2(_(e_illvar), name);
22279 return;
22280 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022281 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022282
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022283 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22284 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022285
Bram Moolenaar33570922005-01-25 22:26:29 +000022286 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022288 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022289 if (var_check_ro(v->di_flags, name, FALSE)
22290 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022291 return;
22292 if (v->di_tv.v_type != tv->v_type
22293 && !((v->di_tv.v_type == VAR_STRING
22294 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022295 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022296 || tv->v_type == VAR_NUMBER))
22297#ifdef FEAT_FLOAT
22298 && !((v->di_tv.v_type == VAR_NUMBER
22299 || v->di_tv.v_type == VAR_FLOAT)
22300 && (tv->v_type == VAR_NUMBER
22301 || tv->v_type == VAR_FLOAT))
22302#endif
22303 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022304 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022305 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022306 return;
22307 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022308
22309 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022310 * Handle setting internal v: variables separately where needed to
22311 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022312 */
22313 if (ht == &vimvarht)
22314 {
22315 if (v->di_tv.v_type == VAR_STRING)
22316 {
22317 vim_free(v->di_tv.vval.v_string);
22318 if (copy || tv->v_type != VAR_STRING)
22319 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22320 else
22321 {
22322 /* Take over the string to avoid an extra alloc/free. */
22323 v->di_tv.vval.v_string = tv->vval.v_string;
22324 tv->vval.v_string = NULL;
22325 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022326 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022328 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022329 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022330 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022331 if (STRCMP(varname, "searchforward") == 0)
22332 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022333#ifdef FEAT_SEARCH_EXTRA
22334 else if (STRCMP(varname, "hlsearch") == 0)
22335 {
22336 no_hlsearch = !v->di_tv.vval.v_number;
22337 redraw_all_later(SOME_VALID);
22338 }
22339#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022340 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022341 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022342 else if (v->di_tv.v_type != tv->v_type)
22343 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022344 }
22345
22346 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347 }
22348 else /* add a new variable */
22349 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022350 /* Can't add "v:" variable. */
22351 if (ht == &vimvarht)
22352 {
22353 EMSG2(_(e_illvar), name);
22354 return;
22355 }
22356
Bram Moolenaar92124a32005-06-17 22:03:40 +000022357 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022358 if (!valid_varname(varname))
22359 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022360
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022361 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22362 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022363 if (v == NULL)
22364 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022365 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022366 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022368 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 return;
22370 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022371 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022373
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022374 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022375 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022376 else
22377 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022378 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022379 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022380 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382}
22383
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022384/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022385 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022386 * Also give an error message.
22387 */
22388 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022389var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022390 int flags;
22391 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022392 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022393{
22394 if (flags & DI_FLAGS_RO)
22395 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022396 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022397 return TRUE;
22398 }
22399 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22400 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022401 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022402 return TRUE;
22403 }
22404 return FALSE;
22405}
22406
22407/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022408 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22409 * Also give an error message.
22410 */
22411 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022412var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022413 int flags;
22414 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022415 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022416{
22417 if (flags & DI_FLAGS_FIX)
22418 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022419 EMSG2(_("E795: Cannot delete variable %s"),
22420 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022421 return TRUE;
22422 }
22423 return FALSE;
22424}
22425
22426/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022427 * Check if a funcref is assigned to a valid variable name.
22428 * Return TRUE and give an error if not.
22429 */
22430 static int
22431var_check_func_name(name, new_var)
22432 char_u *name; /* points to start of variable name */
22433 int new_var; /* TRUE when creating the variable */
22434{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022435 /* Allow for w: b: s: and t:. */
22436 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022437 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22438 ? name[2] : name[0]))
22439 {
22440 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22441 name);
22442 return TRUE;
22443 }
22444 /* Don't allow hiding a function. When "v" is not NULL we might be
22445 * assigning another function to the same var, the type is checked
22446 * below. */
22447 if (new_var && function_exists(name))
22448 {
22449 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22450 name);
22451 return TRUE;
22452 }
22453 return FALSE;
22454}
22455
22456/*
22457 * Check if a variable name is valid.
22458 * Return FALSE and give an error if not.
22459 */
22460 static int
22461valid_varname(varname)
22462 char_u *varname;
22463{
22464 char_u *p;
22465
22466 for (p = varname; *p != NUL; ++p)
22467 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22468 && *p != AUTOLOAD_CHAR)
22469 {
22470 EMSG2(_(e_illvar), varname);
22471 return FALSE;
22472 }
22473 return TRUE;
22474}
22475
22476/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022477 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022478 * Also give an error message, using "name" or _("name") when use_gettext is
22479 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022480 */
22481 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022482tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022483 int lock;
22484 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022485 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022486{
22487 if (lock & VAR_LOCKED)
22488 {
22489 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022490 name == NULL ? (char_u *)_("Unknown")
22491 : use_gettext ? (char_u *)_(name)
22492 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022493 return TRUE;
22494 }
22495 if (lock & VAR_FIXED)
22496 {
22497 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022498 name == NULL ? (char_u *)_("Unknown")
22499 : use_gettext ? (char_u *)_(name)
22500 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022501 return TRUE;
22502 }
22503 return FALSE;
22504}
22505
22506/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022507 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022508 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022509 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022510 * It is OK for "from" and "to" to point to the same item. This is used to
22511 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022512 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022513 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022514copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022515 typval_T *from;
22516 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022518 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022519 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022520 switch (from->v_type)
22521 {
22522 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022523 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022524 to->vval.v_number = from->vval.v_number;
22525 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022526#ifdef FEAT_FLOAT
22527 case VAR_FLOAT:
22528 to->vval.v_float = from->vval.v_float;
22529 break;
22530#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022531 case VAR_STRING:
22532 case VAR_FUNC:
22533 if (from->vval.v_string == NULL)
22534 to->vval.v_string = NULL;
22535 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022536 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022537 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022538 if (from->v_type == VAR_FUNC)
22539 func_ref(to->vval.v_string);
22540 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022541 break;
22542 case VAR_LIST:
22543 if (from->vval.v_list == NULL)
22544 to->vval.v_list = NULL;
22545 else
22546 {
22547 to->vval.v_list = from->vval.v_list;
22548 ++to->vval.v_list->lv_refcount;
22549 }
22550 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022551 case VAR_DICT:
22552 if (from->vval.v_dict == NULL)
22553 to->vval.v_dict = NULL;
22554 else
22555 {
22556 to->vval.v_dict = from->vval.v_dict;
22557 ++to->vval.v_dict->dv_refcount;
22558 }
22559 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022560 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022561 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022562 break;
22563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564}
22565
22566/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022567 * Make a copy of an item.
22568 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022569 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22570 * reference to an already copied list/dict can be used.
22571 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022572 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022573 static int
22574item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 typval_T *from;
22576 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022577 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022578 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022579{
22580 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022581 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022582
Bram Moolenaar33570922005-01-25 22:26:29 +000022583 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022584 {
22585 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022586 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022587 }
22588 ++recurse;
22589
22590 switch (from->v_type)
22591 {
22592 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022593#ifdef FEAT_FLOAT
22594 case VAR_FLOAT:
22595#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022596 case VAR_STRING:
22597 case VAR_FUNC:
22598 copy_tv(from, to);
22599 break;
22600 case VAR_LIST:
22601 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022602 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022603 if (from->vval.v_list == NULL)
22604 to->vval.v_list = NULL;
22605 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22606 {
22607 /* use the copy made earlier */
22608 to->vval.v_list = from->vval.v_list->lv_copylist;
22609 ++to->vval.v_list->lv_refcount;
22610 }
22611 else
22612 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22613 if (to->vval.v_list == NULL)
22614 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022615 break;
22616 case VAR_DICT:
22617 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022618 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022619 if (from->vval.v_dict == NULL)
22620 to->vval.v_dict = NULL;
22621 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22622 {
22623 /* use the copy made earlier */
22624 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22625 ++to->vval.v_dict->dv_refcount;
22626 }
22627 else
22628 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22629 if (to->vval.v_dict == NULL)
22630 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022631 break;
22632 default:
22633 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022634 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022635 }
22636 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022637 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022638}
22639
22640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 * ":echo expr1 ..." print each argument separated with a space, add a
22642 * newline at the end.
22643 * ":echon expr1 ..." print each argument plain.
22644 */
22645 void
22646ex_echo(eap)
22647 exarg_T *eap;
22648{
22649 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022650 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022651 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 char_u *p;
22653 int needclr = TRUE;
22654 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022655 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656
22657 if (eap->skip)
22658 ++emsg_skip;
22659 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22660 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022661 /* If eval1() causes an error message the text from the command may
22662 * still need to be cleared. E.g., "echo 22,44". */
22663 need_clr_eos = needclr;
22664
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022666 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 {
22668 /*
22669 * Report the invalid expression unless the expression evaluation
22670 * has been cancelled due to an aborting error, an interrupt, or an
22671 * exception.
22672 */
22673 if (!aborting())
22674 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022675 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 break;
22677 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022678 need_clr_eos = FALSE;
22679
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 if (!eap->skip)
22681 {
22682 if (atstart)
22683 {
22684 atstart = FALSE;
22685 /* Call msg_start() after eval1(), evaluating the expression
22686 * may cause a message to appear. */
22687 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022688 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022689 /* Mark the saved text as finishing the line, so that what
22690 * follows is displayed on a new line when scrolling back
22691 * at the more prompt. */
22692 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 }
22696 else if (eap->cmdidx == CMD_echo)
22697 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022698 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022699 if (p != NULL)
22700 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022702 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022704 if (*p != TAB && needclr)
22705 {
22706 /* remove any text still there from the command */
22707 msg_clr_eos();
22708 needclr = FALSE;
22709 }
22710 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 }
22712 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022713 {
22714#ifdef FEAT_MBYTE
22715 if (has_mbyte)
22716 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022717 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022718
22719 (void)msg_outtrans_len_attr(p, i, echo_attr);
22720 p += i - 1;
22721 }
22722 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022724 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022727 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022729 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 arg = skipwhite(arg);
22731 }
22732 eap->nextcmd = check_nextcmd(arg);
22733
22734 if (eap->skip)
22735 --emsg_skip;
22736 else
22737 {
22738 /* remove text that may still be there from the command */
22739 if (needclr)
22740 msg_clr_eos();
22741 if (eap->cmdidx == CMD_echo)
22742 msg_end();
22743 }
22744}
22745
22746/*
22747 * ":echohl {name}".
22748 */
22749 void
22750ex_echohl(eap)
22751 exarg_T *eap;
22752{
22753 int id;
22754
22755 id = syn_name2id(eap->arg);
22756 if (id == 0)
22757 echo_attr = 0;
22758 else
22759 echo_attr = syn_id2attr(id);
22760}
22761
22762/*
22763 * ":execute expr1 ..." execute the result of an expression.
22764 * ":echomsg expr1 ..." Print a message
22765 * ":echoerr expr1 ..." Print an error
22766 * Each gets spaces around each argument and a newline at the end for
22767 * echo commands
22768 */
22769 void
22770ex_execute(eap)
22771 exarg_T *eap;
22772{
22773 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022774 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 int ret = OK;
22776 char_u *p;
22777 garray_T ga;
22778 int len;
22779 int save_did_emsg;
22780
22781 ga_init2(&ga, 1, 80);
22782
22783 if (eap->skip)
22784 ++emsg_skip;
22785 while (*arg != NUL && *arg != '|' && *arg != '\n')
22786 {
22787 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022788 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 {
22790 /*
22791 * Report the invalid expression unless the expression evaluation
22792 * has been cancelled due to an aborting error, an interrupt, or an
22793 * exception.
22794 */
22795 if (!aborting())
22796 EMSG2(_(e_invexpr2), p);
22797 ret = FAIL;
22798 break;
22799 }
22800
22801 if (!eap->skip)
22802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022803 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 len = (int)STRLEN(p);
22805 if (ga_grow(&ga, len + 2) == FAIL)
22806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022807 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 ret = FAIL;
22809 break;
22810 }
22811 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 ga.ga_len += len;
22815 }
22816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022817 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818 arg = skipwhite(arg);
22819 }
22820
22821 if (ret != FAIL && ga.ga_data != NULL)
22822 {
22823 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022826 out_flush();
22827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 else if (eap->cmdidx == CMD_echoerr)
22829 {
22830 /* We don't want to abort following commands, restore did_emsg. */
22831 save_did_emsg = did_emsg;
22832 EMSG((char_u *)ga.ga_data);
22833 if (!force_abort)
22834 did_emsg = save_did_emsg;
22835 }
22836 else if (eap->cmdidx == CMD_execute)
22837 do_cmdline((char_u *)ga.ga_data,
22838 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22839 }
22840
22841 ga_clear(&ga);
22842
22843 if (eap->skip)
22844 --emsg_skip;
22845
22846 eap->nextcmd = check_nextcmd(arg);
22847}
22848
22849/*
22850 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22851 * "arg" points to the "&" or '+' when called, to "option" when returning.
22852 * Returns NULL when no option name found. Otherwise pointer to the char
22853 * after the option name.
22854 */
22855 static char_u *
22856find_option_end(arg, opt_flags)
22857 char_u **arg;
22858 int *opt_flags;
22859{
22860 char_u *p = *arg;
22861
22862 ++p;
22863 if (*p == 'g' && p[1] == ':')
22864 {
22865 *opt_flags = OPT_GLOBAL;
22866 p += 2;
22867 }
22868 else if (*p == 'l' && p[1] == ':')
22869 {
22870 *opt_flags = OPT_LOCAL;
22871 p += 2;
22872 }
22873 else
22874 *opt_flags = 0;
22875
22876 if (!ASCII_ISALPHA(*p))
22877 return NULL;
22878 *arg = p;
22879
22880 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22881 p += 4; /* termcap option */
22882 else
22883 while (ASCII_ISALPHA(*p))
22884 ++p;
22885 return p;
22886}
22887
22888/*
22889 * ":function"
22890 */
22891 void
22892ex_function(eap)
22893 exarg_T *eap;
22894{
22895 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022896 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 int j;
22898 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022900 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 char_u *name = NULL;
22902 char_u *p;
22903 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022904 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 garray_T newargs;
22906 garray_T newlines;
22907 int varargs = FALSE;
22908 int mustend = FALSE;
22909 int flags = 0;
22910 ufunc_T *fp;
22911 int indent;
22912 int nesting;
22913 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022914 dictitem_T *v;
22915 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022916 static int func_nr = 0; /* number for nameless function */
22917 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022918 hashtab_T *ht;
22919 int todo;
22920 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022921 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922
22923 /*
22924 * ":function" without argument: list functions.
22925 */
22926 if (ends_excmd(*eap->arg))
22927 {
22928 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022929 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022930 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022931 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022932 {
22933 if (!HASHITEM_EMPTY(hi))
22934 {
22935 --todo;
22936 fp = HI2UF(hi);
22937 if (!isdigit(*fp->uf_name))
22938 list_func_head(fp, FALSE);
22939 }
22940 }
22941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 eap->nextcmd = check_nextcmd(eap->arg);
22943 return;
22944 }
22945
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022946 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022947 * ":function /pat": list functions matching pattern.
22948 */
22949 if (*eap->arg == '/')
22950 {
22951 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22952 if (!eap->skip)
22953 {
22954 regmatch_T regmatch;
22955
22956 c = *p;
22957 *p = NUL;
22958 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22959 *p = c;
22960 if (regmatch.regprog != NULL)
22961 {
22962 regmatch.rm_ic = p_ic;
22963
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022964 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022965 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22966 {
22967 if (!HASHITEM_EMPTY(hi))
22968 {
22969 --todo;
22970 fp = HI2UF(hi);
22971 if (!isdigit(*fp->uf_name)
22972 && vim_regexec(&regmatch, fp->uf_name, 0))
22973 list_func_head(fp, FALSE);
22974 }
22975 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022976 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022977 }
22978 }
22979 if (*p == '/')
22980 ++p;
22981 eap->nextcmd = check_nextcmd(p);
22982 return;
22983 }
22984
22985 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022986 * Get the function name. There are these situations:
22987 * func normal function name
22988 * "name" == func, "fudi.fd_dict" == NULL
22989 * dict.func new dictionary entry
22990 * "name" == NULL, "fudi.fd_dict" set,
22991 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22992 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022993 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022994 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22995 * dict.func existing dict entry that's not a Funcref
22996 * "name" == NULL, "fudi.fd_dict" set,
22997 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022998 * s:func script-local function name
22999 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023002 name = trans_function_name(&p, eap->skip, 0, &fudi);
23003 paren = (vim_strchr(p, '(') != NULL);
23004 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 {
23006 /*
23007 * Return on an invalid expression in braces, unless the expression
23008 * evaluation has been cancelled due to an aborting error, an
23009 * interrupt, or an exception.
23010 */
23011 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023012 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023013 if (!eap->skip && fudi.fd_newkey != NULL)
23014 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023015 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 else
23019 eap->skip = TRUE;
23020 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023021
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022 /* An error in a function call during evaluation of an expression in magic
23023 * braces should not cause the function not to be defined. */
23024 saved_did_emsg = did_emsg;
23025 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026
23027 /*
23028 * ":function func" with only function name: list function.
23029 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023030 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031 {
23032 if (!ends_excmd(*skipwhite(p)))
23033 {
23034 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023035 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023036 }
23037 eap->nextcmd = check_nextcmd(p);
23038 if (eap->nextcmd != NULL)
23039 *p = NUL;
23040 if (!eap->skip && !got_int)
23041 {
23042 fp = find_func(name);
23043 if (fp != NULL)
23044 {
23045 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023046 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023048 if (FUNCLINE(fp, j) == NULL)
23049 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 msg_putchar('\n');
23051 msg_outnum((long)(j + 1));
23052 if (j < 9)
23053 msg_putchar(' ');
23054 if (j < 99)
23055 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023056 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 out_flush(); /* show a line at a time */
23058 ui_breakcheck();
23059 }
23060 if (!got_int)
23061 {
23062 msg_putchar('\n');
23063 msg_puts((char_u *)" endfunction");
23064 }
23065 }
23066 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023067 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023069 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 }
23071
23072 /*
23073 * ":function name(arg1, arg2)" Define function.
23074 */
23075 p = skipwhite(p);
23076 if (*p != '(')
23077 {
23078 if (!eap->skip)
23079 {
23080 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023081 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 }
23083 /* attempt to continue by skipping some text */
23084 if (vim_strchr(p, '(') != NULL)
23085 p = vim_strchr(p, '(');
23086 }
23087 p = skipwhite(p + 1);
23088
23089 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23090 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23091
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023092 if (!eap->skip)
23093 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023094 /* Check the name of the function. Unless it's a dictionary function
23095 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023096 if (name != NULL)
23097 arg = name;
23098 else
23099 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023100 if (arg != NULL && (fudi.fd_di == NULL
23101 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023102 {
23103 if (*arg == K_SPECIAL)
23104 j = 3;
23105 else
23106 j = 0;
23107 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23108 : eval_isnamec(arg[j])))
23109 ++j;
23110 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023111 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023112 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023113 /* Disallow using the g: dict. */
23114 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23115 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023116 }
23117
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 /*
23119 * Isolate the arguments: "arg1, arg2, ...)"
23120 */
23121 while (*p != ')')
23122 {
23123 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23124 {
23125 varargs = TRUE;
23126 p += 3;
23127 mustend = TRUE;
23128 }
23129 else
23130 {
23131 arg = p;
23132 while (ASCII_ISALNUM(*p) || *p == '_')
23133 ++p;
23134 if (arg == p || isdigit(*arg)
23135 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23136 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23137 {
23138 if (!eap->skip)
23139 EMSG2(_("E125: Illegal argument: %s"), arg);
23140 break;
23141 }
23142 if (ga_grow(&newargs, 1) == FAIL)
23143 goto erret;
23144 c = *p;
23145 *p = NUL;
23146 arg = vim_strsave(arg);
23147 if (arg == NULL)
23148 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023149
23150 /* Check for duplicate argument name. */
23151 for (i = 0; i < newargs.ga_len; ++i)
23152 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23153 {
23154 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023155 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023156 goto erret;
23157 }
23158
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23160 *p = c;
23161 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 if (*p == ',')
23163 ++p;
23164 else
23165 mustend = TRUE;
23166 }
23167 p = skipwhite(p);
23168 if (mustend && *p != ')')
23169 {
23170 if (!eap->skip)
23171 EMSG2(_(e_invarg2), eap->arg);
23172 break;
23173 }
23174 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023175 if (*p != ')')
23176 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177 ++p; /* skip the ')' */
23178
Bram Moolenaare9a41262005-01-15 22:18:47 +000023179 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 for (;;)
23181 {
23182 p = skipwhite(p);
23183 if (STRNCMP(p, "range", 5) == 0)
23184 {
23185 flags |= FC_RANGE;
23186 p += 5;
23187 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023188 else if (STRNCMP(p, "dict", 4) == 0)
23189 {
23190 flags |= FC_DICT;
23191 p += 4;
23192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 else if (STRNCMP(p, "abort", 5) == 0)
23194 {
23195 flags |= FC_ABORT;
23196 p += 5;
23197 }
23198 else
23199 break;
23200 }
23201
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023202 /* When there is a line break use what follows for the function body.
23203 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23204 if (*p == '\n')
23205 line_arg = p + 1;
23206 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 EMSG(_(e_trailing));
23208
23209 /*
23210 * Read the body of the function, until ":endfunction" is found.
23211 */
23212 if (KeyTyped)
23213 {
23214 /* Check if the function already exists, don't let the user type the
23215 * whole function before telling him it doesn't work! For a script we
23216 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023217 if (!eap->skip && !eap->forceit)
23218 {
23219 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23220 EMSG(_(e_funcdict));
23221 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023222 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023225 if (!eap->skip && did_emsg)
23226 goto erret;
23227
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228 msg_putchar('\n'); /* don't overwrite the function name */
23229 cmdline_row = msg_row;
23230 }
23231
23232 indent = 2;
23233 nesting = 0;
23234 for (;;)
23235 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023236 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023237 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023238 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023239 saved_wait_return = FALSE;
23240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023242 sourcing_lnum_off = sourcing_lnum;
23243
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023244 if (line_arg != NULL)
23245 {
23246 /* Use eap->arg, split up in parts by line breaks. */
23247 theline = line_arg;
23248 p = vim_strchr(theline, '\n');
23249 if (p == NULL)
23250 line_arg += STRLEN(line_arg);
23251 else
23252 {
23253 *p = NUL;
23254 line_arg = p + 1;
23255 }
23256 }
23257 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258 theline = getcmdline(':', 0L, indent);
23259 else
23260 theline = eap->getline(':', eap->cookie, indent);
23261 if (KeyTyped)
23262 lines_left = Rows - 1;
23263 if (theline == NULL)
23264 {
23265 EMSG(_("E126: Missing :endfunction"));
23266 goto erret;
23267 }
23268
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023269 /* Detect line continuation: sourcing_lnum increased more than one. */
23270 if (sourcing_lnum > sourcing_lnum_off + 1)
23271 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23272 else
23273 sourcing_lnum_off = 0;
23274
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 if (skip_until != NULL)
23276 {
23277 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23278 * don't check for ":endfunc". */
23279 if (STRCMP(theline, skip_until) == 0)
23280 {
23281 vim_free(skip_until);
23282 skip_until = NULL;
23283 }
23284 }
23285 else
23286 {
23287 /* skip ':' and blanks*/
23288 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23289 ;
23290
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023291 /* Check for "endfunction". */
23292 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023294 if (line_arg == NULL)
23295 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 break;
23297 }
23298
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023299 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300 * at "end". */
23301 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23302 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023303 else if (STRNCMP(p, "if", 2) == 0
23304 || STRNCMP(p, "wh", 2) == 0
23305 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306 || STRNCMP(p, "try", 3) == 0)
23307 indent += 2;
23308
23309 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023310 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023312 if (*p == '!')
23313 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023314 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023315 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23316 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023318 ++nesting;
23319 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023320 }
23321 }
23322
23323 /* Check for ":append" or ":insert". */
23324 p = skip_range(p, NULL);
23325 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23326 || (p[0] == 'i'
23327 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23328 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23329 skip_until = vim_strsave((char_u *)".");
23330
23331 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23332 arg = skipwhite(skiptowhite(p));
23333 if (arg[0] == '<' && arg[1] =='<'
23334 && ((p[0] == 'p' && p[1] == 'y'
23335 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23336 || (p[0] == 'p' && p[1] == 'e'
23337 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23338 || (p[0] == 't' && p[1] == 'c'
23339 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023340 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23341 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23343 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023344 || (p[0] == 'm' && p[1] == 'z'
23345 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 ))
23347 {
23348 /* ":python <<" continues until a dot, like ":append" */
23349 p = skipwhite(arg + 2);
23350 if (*p == NUL)
23351 skip_until = vim_strsave((char_u *)".");
23352 else
23353 skip_until = vim_strsave(p);
23354 }
23355 }
23356
23357 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023358 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023359 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023360 if (line_arg == NULL)
23361 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023363 }
23364
23365 /* Copy the line to newly allocated memory. get_one_sourceline()
23366 * allocates 250 bytes per line, this saves 80% on average. The cost
23367 * is an extra alloc/free. */
23368 p = vim_strsave(theline);
23369 if (p != NULL)
23370 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023371 if (line_arg == NULL)
23372 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023373 theline = p;
23374 }
23375
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023376 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23377
23378 /* Add NULL lines for continuation lines, so that the line count is
23379 * equal to the index in the growarray. */
23380 while (sourcing_lnum_off-- > 0)
23381 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023382
23383 /* Check for end of eap->arg. */
23384 if (line_arg != NULL && *line_arg == NUL)
23385 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023386 }
23387
23388 /* Don't define the function when skipping commands or when an error was
23389 * detected. */
23390 if (eap->skip || did_emsg)
23391 goto erret;
23392
23393 /*
23394 * If there are no errors, add the function
23395 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023396 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023397 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023398 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023399 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023400 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023401 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023402 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023403 goto erret;
23404 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023405
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023406 fp = find_func(name);
23407 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023409 if (!eap->forceit)
23410 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023411 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023412 goto erret;
23413 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023414 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023415 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023416 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023417 name);
23418 goto erret;
23419 }
23420 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023421 ga_clear_strings(&(fp->uf_args));
23422 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023423 vim_free(name);
23424 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 }
23427 else
23428 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023429 char numbuf[20];
23430
23431 fp = NULL;
23432 if (fudi.fd_newkey == NULL && !eap->forceit)
23433 {
23434 EMSG(_(e_funcdict));
23435 goto erret;
23436 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023437 if (fudi.fd_di == NULL)
23438 {
23439 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023440 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023441 goto erret;
23442 }
23443 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023444 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023445 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023446
23447 /* Give the function a sequential number. Can only be used with a
23448 * Funcref! */
23449 vim_free(name);
23450 sprintf(numbuf, "%d", ++func_nr);
23451 name = vim_strsave((char_u *)numbuf);
23452 if (name == NULL)
23453 goto erret;
23454 }
23455
23456 if (fp == NULL)
23457 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023458 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023459 {
23460 int slen, plen;
23461 char_u *scriptname;
23462
23463 /* Check that the autoload name matches the script name. */
23464 j = FAIL;
23465 if (sourcing_name != NULL)
23466 {
23467 scriptname = autoload_name(name);
23468 if (scriptname != NULL)
23469 {
23470 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023471 plen = (int)STRLEN(p);
23472 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023473 if (slen > plen && fnamecmp(p,
23474 sourcing_name + slen - plen) == 0)
23475 j = OK;
23476 vim_free(scriptname);
23477 }
23478 }
23479 if (j == FAIL)
23480 {
23481 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23482 goto erret;
23483 }
23484 }
23485
23486 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 if (fp == NULL)
23488 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023489
23490 if (fudi.fd_dict != NULL)
23491 {
23492 if (fudi.fd_di == NULL)
23493 {
23494 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023495 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023496 if (fudi.fd_di == NULL)
23497 {
23498 vim_free(fp);
23499 goto erret;
23500 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023501 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23502 {
23503 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023504 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023505 goto erret;
23506 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023507 }
23508 else
23509 /* overwrite existing dict entry */
23510 clear_tv(&fudi.fd_di->di_tv);
23511 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023512 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023513 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023514 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023515
23516 /* behave like "dict" was used */
23517 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023518 }
23519
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023521 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023522 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23523 {
23524 vim_free(fp);
23525 goto erret;
23526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023528 fp->uf_args = newargs;
23529 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023530#ifdef FEAT_PROFILE
23531 fp->uf_tml_count = NULL;
23532 fp->uf_tml_total = NULL;
23533 fp->uf_tml_self = NULL;
23534 fp->uf_profiling = FALSE;
23535 if (prof_def_func())
23536 func_do_profile(fp);
23537#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023538 fp->uf_varargs = varargs;
23539 fp->uf_flags = flags;
23540 fp->uf_calls = 0;
23541 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023542 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543
23544erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 ga_clear_strings(&newargs);
23546 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023547ret_free:
23548 vim_free(skip_until);
23549 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023552 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023553}
23554
23555/*
23556 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023557 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023559 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023560 * TFN_INT: internal function name OK
23561 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023562 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563 * Advances "pp" to just after the function name (if no error).
23564 */
23565 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023566trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 char_u **pp;
23568 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023569 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023570 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023572 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 char_u *start;
23574 char_u *end;
23575 int lead;
23576 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023578 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023579
23580 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023581 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023583
23584 /* Check for hard coded <SNR>: already translated function ID (from a user
23585 * command). */
23586 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23587 && (*pp)[2] == (int)KE_SNR)
23588 {
23589 *pp += 3;
23590 len = get_id_len(pp) + 3;
23591 return vim_strnsave(start, len);
23592 }
23593
23594 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23595 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023597 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023599
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023600 /* Note that TFN_ flags use the same values as GLV_ flags. */
23601 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023602 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023603 if (end == start)
23604 {
23605 if (!skip)
23606 EMSG(_("E129: Function name required"));
23607 goto theend;
23608 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023609 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023610 {
23611 /*
23612 * Report an invalid expression in braces, unless the expression
23613 * evaluation has been cancelled due to an aborting error, an
23614 * interrupt, or an exception.
23615 */
23616 if (!aborting())
23617 {
23618 if (end != NULL)
23619 EMSG2(_(e_invarg2), start);
23620 }
23621 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023622 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023623 goto theend;
23624 }
23625
23626 if (lv.ll_tv != NULL)
23627 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023628 if (fdp != NULL)
23629 {
23630 fdp->fd_dict = lv.ll_dict;
23631 fdp->fd_newkey = lv.ll_newkey;
23632 lv.ll_newkey = NULL;
23633 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023634 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023635 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23636 {
23637 name = vim_strsave(lv.ll_tv->vval.v_string);
23638 *pp = end;
23639 }
23640 else
23641 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023642 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23643 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023644 EMSG(_(e_funcref));
23645 else
23646 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023647 name = NULL;
23648 }
23649 goto theend;
23650 }
23651
23652 if (lv.ll_name == NULL)
23653 {
23654 /* Error found, but continue after the function name. */
23655 *pp = end;
23656 goto theend;
23657 }
23658
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023659 /* Check if the name is a Funcref. If so, use the value. */
23660 if (lv.ll_exp_name != NULL)
23661 {
23662 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023663 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023664 if (name == lv.ll_exp_name)
23665 name = NULL;
23666 }
23667 else
23668 {
23669 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023670 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023671 if (name == *pp)
23672 name = NULL;
23673 }
23674 if (name != NULL)
23675 {
23676 name = vim_strsave(name);
23677 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023678 if (STRNCMP(name, "<SNR>", 5) == 0)
23679 {
23680 /* Change "<SNR>" to the byte sequence. */
23681 name[0] = K_SPECIAL;
23682 name[1] = KS_EXTRA;
23683 name[2] = (int)KE_SNR;
23684 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23685 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023686 goto theend;
23687 }
23688
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023689 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023690 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023691 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023692 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23693 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23694 {
23695 /* When there was "s:" already or the name expanded to get a
23696 * leading "s:" then remove it. */
23697 lv.ll_name += 2;
23698 len -= 2;
23699 lead = 2;
23700 }
23701 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023702 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023703 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023704 /* skip over "s:" and "g:" */
23705 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023706 lv.ll_name += 2;
23707 len = (int)(end - lv.ll_name);
23708 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023709
23710 /*
23711 * Copy the function name to allocated memory.
23712 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23713 * Accept <SNR>123_name() outside a script.
23714 */
23715 if (skip)
23716 lead = 0; /* do nothing */
23717 else if (lead > 0)
23718 {
23719 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023720 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23721 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023722 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023723 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023724 if (current_SID <= 0)
23725 {
23726 EMSG(_(e_usingsid));
23727 goto theend;
23728 }
23729 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23730 lead += (int)STRLEN(sid_buf);
23731 }
23732 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023733 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023734 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023735 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023736 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023737 goto theend;
23738 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023739 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023740 {
23741 char_u *cp = vim_strchr(lv.ll_name, ':');
23742
23743 if (cp != NULL && cp < end)
23744 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023745 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023746 goto theend;
23747 }
23748 }
23749
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023750 name = alloc((unsigned)(len + lead + 1));
23751 if (name != NULL)
23752 {
23753 if (lead > 0)
23754 {
23755 name[0] = K_SPECIAL;
23756 name[1] = KS_EXTRA;
23757 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023758 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023759 STRCPY(name + 3, sid_buf);
23760 }
23761 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023762 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023763 }
23764 *pp = end;
23765
23766theend:
23767 clear_lval(&lv);
23768 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769}
23770
23771/*
23772 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23773 * Return 2 if "p" starts with "s:".
23774 * Return 0 otherwise.
23775 */
23776 static int
23777eval_fname_script(p)
23778 char_u *p;
23779{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023780 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23781 * the standard library function. */
23782 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23783 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 return 5;
23785 if (p[0] == 's' && p[1] == ':')
23786 return 2;
23787 return 0;
23788}
23789
23790/*
23791 * Return TRUE if "p" starts with "<SID>" or "s:".
23792 * Only works if eval_fname_script() returned non-zero for "p"!
23793 */
23794 static int
23795eval_fname_sid(p)
23796 char_u *p;
23797{
23798 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23799}
23800
23801/*
23802 * List the head of the function: "name(arg1, arg2)".
23803 */
23804 static void
23805list_func_head(fp, indent)
23806 ufunc_T *fp;
23807 int indent;
23808{
23809 int j;
23810
23811 msg_start();
23812 if (indent)
23813 MSG_PUTS(" ");
23814 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023815 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 {
23817 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023818 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 }
23820 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023821 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023823 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824 {
23825 if (j)
23826 MSG_PUTS(", ");
23827 msg_puts(FUNCARG(fp, j));
23828 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023829 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830 {
23831 if (j)
23832 MSG_PUTS(", ");
23833 MSG_PUTS("...");
23834 }
23835 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023836 if (fp->uf_flags & FC_ABORT)
23837 MSG_PUTS(" abort");
23838 if (fp->uf_flags & FC_RANGE)
23839 MSG_PUTS(" range");
23840 if (fp->uf_flags & FC_DICT)
23841 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023842 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023843 if (p_verbose > 0)
23844 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845}
23846
23847/*
23848 * Find a function by name, return pointer to it in ufuncs.
23849 * Return NULL for unknown function.
23850 */
23851 static ufunc_T *
23852find_func(name)
23853 char_u *name;
23854{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023855 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023857 hi = hash_find(&func_hashtab, name);
23858 if (!HASHITEM_EMPTY(hi))
23859 return HI2UF(hi);
23860 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861}
23862
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023863#if defined(EXITFREE) || defined(PROTO)
23864 void
23865free_all_functions()
23866{
23867 hashitem_T *hi;
23868
23869 /* Need to start all over every time, because func_free() may change the
23870 * hash table. */
23871 while (func_hashtab.ht_used > 0)
23872 for (hi = func_hashtab.ht_array; ; ++hi)
23873 if (!HASHITEM_EMPTY(hi))
23874 {
23875 func_free(HI2UF(hi));
23876 break;
23877 }
23878}
23879#endif
23880
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023881 int
23882translated_function_exists(name)
23883 char_u *name;
23884{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023885 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023886 return find_internal_func(name) >= 0;
23887 return find_func(name) != NULL;
23888}
23889
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023890/*
23891 * Return TRUE if a function "name" exists.
23892 */
23893 static int
23894function_exists(name)
23895 char_u *name;
23896{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023897 char_u *nm = name;
23898 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023899 int n = FALSE;
23900
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023901 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23902 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023903 nm = skipwhite(nm);
23904
23905 /* Only accept "funcname", "funcname ", "funcname (..." and
23906 * "funcname(...", not "funcname!...". */
23907 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023908 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023909 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023910 return n;
23911}
23912
Bram Moolenaara1544c02013-05-30 12:35:52 +020023913 char_u *
23914get_expanded_name(name, check)
23915 char_u *name;
23916 int check;
23917{
23918 char_u *nm = name;
23919 char_u *p;
23920
23921 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23922
23923 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023924 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023925 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023926
Bram Moolenaara1544c02013-05-30 12:35:52 +020023927 vim_free(p);
23928 return NULL;
23929}
23930
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023931/*
23932 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023933 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23934 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023935 */
23936 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023937builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023938 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023939 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023940{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023941 char_u *p;
23942
23943 if (!ASCII_ISLOWER(name[0]))
23944 return FALSE;
23945 p = vim_strchr(name, AUTOLOAD_CHAR);
23946 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023947}
23948
Bram Moolenaar05159a02005-02-26 23:04:13 +000023949#if defined(FEAT_PROFILE) || defined(PROTO)
23950/*
23951 * Start profiling function "fp".
23952 */
23953 static void
23954func_do_profile(fp)
23955 ufunc_T *fp;
23956{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023957 int len = fp->uf_lines.ga_len;
23958
23959 if (len == 0)
23960 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023961 fp->uf_tm_count = 0;
23962 profile_zero(&fp->uf_tm_self);
23963 profile_zero(&fp->uf_tm_total);
23964 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023965 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023966 if (fp->uf_tml_total == NULL)
23967 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023968 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023969 if (fp->uf_tml_self == NULL)
23970 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023971 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023972 fp->uf_tml_idx = -1;
23973 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23974 || fp->uf_tml_self == NULL)
23975 return; /* out of memory */
23976
23977 fp->uf_profiling = TRUE;
23978}
23979
23980/*
23981 * Dump the profiling results for all functions in file "fd".
23982 */
23983 void
23984func_dump_profile(fd)
23985 FILE *fd;
23986{
23987 hashitem_T *hi;
23988 int todo;
23989 ufunc_T *fp;
23990 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023991 ufunc_T **sorttab;
23992 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023993
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023994 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023995 if (todo == 0)
23996 return; /* nothing to dump */
23997
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023998 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023999
Bram Moolenaar05159a02005-02-26 23:04:13 +000024000 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24001 {
24002 if (!HASHITEM_EMPTY(hi))
24003 {
24004 --todo;
24005 fp = HI2UF(hi);
24006 if (fp->uf_profiling)
24007 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024008 if (sorttab != NULL)
24009 sorttab[st_len++] = fp;
24010
Bram Moolenaar05159a02005-02-26 23:04:13 +000024011 if (fp->uf_name[0] == K_SPECIAL)
24012 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24013 else
24014 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24015 if (fp->uf_tm_count == 1)
24016 fprintf(fd, "Called 1 time\n");
24017 else
24018 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24019 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24020 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24021 fprintf(fd, "\n");
24022 fprintf(fd, "count total (s) self (s)\n");
24023
24024 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24025 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024026 if (FUNCLINE(fp, i) == NULL)
24027 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024028 prof_func_line(fd, fp->uf_tml_count[i],
24029 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024030 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24031 }
24032 fprintf(fd, "\n");
24033 }
24034 }
24035 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024036
24037 if (sorttab != NULL && st_len > 0)
24038 {
24039 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24040 prof_total_cmp);
24041 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24042 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24043 prof_self_cmp);
24044 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24045 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024046
24047 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024048}
Bram Moolenaar73830342005-02-28 22:48:19 +000024049
24050 static void
24051prof_sort_list(fd, sorttab, st_len, title, prefer_self)
24052 FILE *fd;
24053 ufunc_T **sorttab;
24054 int st_len;
24055 char *title;
24056 int prefer_self; /* when equal print only self time */
24057{
24058 int i;
24059 ufunc_T *fp;
24060
24061 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24062 fprintf(fd, "count total (s) self (s) function\n");
24063 for (i = 0; i < 20 && i < st_len; ++i)
24064 {
24065 fp = sorttab[i];
24066 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24067 prefer_self);
24068 if (fp->uf_name[0] == K_SPECIAL)
24069 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24070 else
24071 fprintf(fd, " %s()\n", fp->uf_name);
24072 }
24073 fprintf(fd, "\n");
24074}
24075
24076/*
24077 * Print the count and times for one function or function line.
24078 */
24079 static void
24080prof_func_line(fd, count, total, self, prefer_self)
24081 FILE *fd;
24082 int count;
24083 proftime_T *total;
24084 proftime_T *self;
24085 int prefer_self; /* when equal print only self time */
24086{
24087 if (count > 0)
24088 {
24089 fprintf(fd, "%5d ", count);
24090 if (prefer_self && profile_equal(total, self))
24091 fprintf(fd, " ");
24092 else
24093 fprintf(fd, "%s ", profile_msg(total));
24094 if (!prefer_self && profile_equal(total, self))
24095 fprintf(fd, " ");
24096 else
24097 fprintf(fd, "%s ", profile_msg(self));
24098 }
24099 else
24100 fprintf(fd, " ");
24101}
24102
24103/*
24104 * Compare function for total time sorting.
24105 */
24106 static int
24107#ifdef __BORLANDC__
24108_RTLENTRYF
24109#endif
24110prof_total_cmp(s1, s2)
24111 const void *s1;
24112 const void *s2;
24113{
24114 ufunc_T *p1, *p2;
24115
24116 p1 = *(ufunc_T **)s1;
24117 p2 = *(ufunc_T **)s2;
24118 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24119}
24120
24121/*
24122 * Compare function for self time sorting.
24123 */
24124 static int
24125#ifdef __BORLANDC__
24126_RTLENTRYF
24127#endif
24128prof_self_cmp(s1, s2)
24129 const void *s1;
24130 const void *s2;
24131{
24132 ufunc_T *p1, *p2;
24133
24134 p1 = *(ufunc_T **)s1;
24135 p2 = *(ufunc_T **)s2;
24136 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24137}
24138
Bram Moolenaar05159a02005-02-26 23:04:13 +000024139#endif
24140
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024141/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024142 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024143 * Return TRUE if a package was loaded.
24144 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024145 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024146script_autoload(name, reload)
24147 char_u *name;
24148 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024149{
24150 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024151 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024152 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024153 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024154
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024155 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024156 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024157 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024158 return FALSE;
24159
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024160 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024161
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024162 /* Find the name in the list of previously loaded package names. Skip
24163 * "autoload/", it's always the same. */
24164 for (i = 0; i < ga_loaded.ga_len; ++i)
24165 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24166 break;
24167 if (!reload && i < ga_loaded.ga_len)
24168 ret = FALSE; /* was loaded already */
24169 else
24170 {
24171 /* Remember the name if it wasn't loaded already. */
24172 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24173 {
24174 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24175 tofree = NULL;
24176 }
24177
24178 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024179 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024180 ret = TRUE;
24181 }
24182
24183 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024184 return ret;
24185}
24186
24187/*
24188 * Return the autoload script name for a function or variable name.
24189 * Returns NULL when out of memory.
24190 */
24191 static char_u *
24192autoload_name(name)
24193 char_u *name;
24194{
24195 char_u *p;
24196 char_u *scriptname;
24197
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024198 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024199 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24200 if (scriptname == NULL)
24201 return FALSE;
24202 STRCPY(scriptname, "autoload/");
24203 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024204 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024205 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024206 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024207 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024208 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024209}
24210
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24212
24213/*
24214 * Function given to ExpandGeneric() to obtain the list of user defined
24215 * function names.
24216 */
24217 char_u *
24218get_user_func_name(xp, idx)
24219 expand_T *xp;
24220 int idx;
24221{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024222 static long_u done;
24223 static hashitem_T *hi;
24224 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225
24226 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024228 done = 0;
24229 hi = func_hashtab.ht_array;
24230 }
24231 if (done < func_hashtab.ht_used)
24232 {
24233 if (done++ > 0)
24234 ++hi;
24235 while (HASHITEM_EMPTY(hi))
24236 ++hi;
24237 fp = HI2UF(hi);
24238
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024239 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024240 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024241
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024242 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24243 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244
24245 cat_func_name(IObuff, fp);
24246 if (xp->xp_context != EXPAND_USER_FUNC)
24247 {
24248 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024249 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024250 STRCAT(IObuff, ")");
24251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 return IObuff;
24253 }
24254 return NULL;
24255}
24256
24257#endif /* FEAT_CMDL_COMPL */
24258
24259/*
24260 * Copy the function name of "fp" to buffer "buf".
24261 * "buf" must be able to hold the function name plus three bytes.
24262 * Takes care of script-local function names.
24263 */
24264 static void
24265cat_func_name(buf, fp)
24266 char_u *buf;
24267 ufunc_T *fp;
24268{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024269 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270 {
24271 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024272 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024273 }
24274 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024275 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024276}
24277
24278/*
24279 * ":delfunction {name}"
24280 */
24281 void
24282ex_delfunction(eap)
24283 exarg_T *eap;
24284{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024285 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 char_u *p;
24287 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024288 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289
24290 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024291 name = trans_function_name(&p, eap->skip, 0, &fudi);
24292 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024294 {
24295 if (fudi.fd_dict != NULL && !eap->skip)
24296 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299 if (!ends_excmd(*skipwhite(p)))
24300 {
24301 vim_free(name);
24302 EMSG(_(e_trailing));
24303 return;
24304 }
24305 eap->nextcmd = check_nextcmd(p);
24306 if (eap->nextcmd != NULL)
24307 *p = NUL;
24308
24309 if (!eap->skip)
24310 fp = find_func(name);
24311 vim_free(name);
24312
24313 if (!eap->skip)
24314 {
24315 if (fp == NULL)
24316 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024317 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318 return;
24319 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024320 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 {
24322 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24323 return;
24324 }
24325
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024326 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024327 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024328 /* Delete the dict item that refers to the function, it will
24329 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024330 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024332 else
24333 func_free(fp);
24334 }
24335}
24336
24337/*
24338 * Free a function and remove it from the list of functions.
24339 */
24340 static void
24341func_free(fp)
24342 ufunc_T *fp;
24343{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024344 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024345
24346 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024347 ga_clear_strings(&(fp->uf_args));
24348 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024349#ifdef FEAT_PROFILE
24350 vim_free(fp->uf_tml_count);
24351 vim_free(fp->uf_tml_total);
24352 vim_free(fp->uf_tml_self);
24353#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024354
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024355 /* remove the function from the function hashtable */
24356 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24357 if (HASHITEM_EMPTY(hi))
24358 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024359 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024360 hash_remove(&func_hashtab, hi);
24361
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024362 vim_free(fp);
24363}
24364
24365/*
24366 * Unreference a Function: decrement the reference count and free it when it
24367 * becomes zero. Only for numbered functions.
24368 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024369 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024370func_unref(name)
24371 char_u *name;
24372{
24373 ufunc_T *fp;
24374
24375 if (name != NULL && isdigit(*name))
24376 {
24377 fp = find_func(name);
24378 if (fp == NULL)
24379 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024380 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024381 {
24382 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024383 * when "uf_calls" becomes zero. */
24384 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024385 func_free(fp);
24386 }
24387 }
24388}
24389
24390/*
24391 * Count a reference to a Function.
24392 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024393 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024394func_ref(name)
24395 char_u *name;
24396{
24397 ufunc_T *fp;
24398
24399 if (name != NULL && isdigit(*name))
24400 {
24401 fp = find_func(name);
24402 if (fp == NULL)
24403 EMSG2(_(e_intern2), "func_ref()");
24404 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024405 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 }
24407}
24408
24409/*
24410 * Call a user function.
24411 */
24412 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024413call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 ufunc_T *fp; /* pointer to function */
24415 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024416 typval_T *argvars; /* arguments */
24417 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024418 linenr_T firstline; /* first line of range */
24419 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024420 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024421{
Bram Moolenaar33570922005-01-25 22:26:29 +000024422 char_u *save_sourcing_name;
24423 linenr_T save_sourcing_lnum;
24424 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024425 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024426 int save_did_emsg;
24427 static int depth = 0;
24428 dictitem_T *v;
24429 int fixvar_idx = 0; /* index in fixvar[] */
24430 int i;
24431 int ai;
24432 char_u numbuf[NUMBUFLEN];
24433 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024434 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024435#ifdef FEAT_PROFILE
24436 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024437 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439
24440 /* If depth of calling is getting too high, don't execute the function */
24441 if (depth >= p_mfd)
24442 {
24443 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024444 rettv->v_type = VAR_NUMBER;
24445 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 return;
24447 }
24448 ++depth;
24449
24450 line_breakcheck(); /* check for CTRL-C hit */
24451
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024452 fc = (funccall_T *)alloc(sizeof(funccall_T));
24453 fc->caller = current_funccal;
24454 current_funccal = fc;
24455 fc->func = fp;
24456 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024457 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024458 fc->linenr = 0;
24459 fc->returned = FALSE;
24460 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024461 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024462 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24463 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024464
Bram Moolenaar33570922005-01-25 22:26:29 +000024465 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024466 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024467 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24468 * each argument variable and saves a lot of time.
24469 */
24470 /*
24471 * Init l: variables.
24472 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024473 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024474 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024475 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024476 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24477 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024478 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024479 name = v->di_key;
24480 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024481 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024482 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024483 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024484 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024485 v->di_tv.vval.v_dict = selfdict;
24486 ++selfdict->dv_refcount;
24487 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024488
Bram Moolenaar33570922005-01-25 22:26:29 +000024489 /*
24490 * Init a: variables.
24491 * Set a:0 to "argcount".
24492 * Set a:000 to a list with room for the "..." arguments.
24493 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024494 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024495 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024496 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024497 /* Use "name" to avoid a warning from some compiler that checks the
24498 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024499 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024500 name = v->di_key;
24501 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024502 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024503 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024504 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024505 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024506 v->di_tv.vval.v_list = &fc->l_varlist;
24507 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24508 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24509 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024510
24511 /*
24512 * Set a:firstline to "firstline" and a:lastline to "lastline".
24513 * Set a:name to named arguments.
24514 * Set a:N to the "..." arguments.
24515 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024516 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024517 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024518 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024519 (varnumber_T)lastline);
24520 for (i = 0; i < argcount; ++i)
24521 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024522 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024523 if (ai < 0)
24524 /* named argument a:name */
24525 name = FUNCARG(fp, i);
24526 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024527 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024528 /* "..." argument a:1, a:2, etc. */
24529 sprintf((char *)numbuf, "%d", ai + 1);
24530 name = numbuf;
24531 }
24532 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24533 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024534 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024535 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24536 }
24537 else
24538 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024539 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24540 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024541 if (v == NULL)
24542 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024543 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024544 }
24545 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024546 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024547
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024548 /* Note: the values are copied directly to avoid alloc/free.
24549 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024550 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024551 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024552
24553 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24554 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024555 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24556 fc->l_listitems[ai].li_tv = argvars[i];
24557 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024558 }
24559 }
24560
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561 /* Don't redraw while executing the function. */
24562 ++RedrawingDisabled;
24563 save_sourcing_name = sourcing_name;
24564 save_sourcing_lnum = sourcing_lnum;
24565 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024566 /* need space for function name + ("function " + 3) or "[number]" */
24567 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24568 + STRLEN(fp->uf_name) + 20;
24569 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 if (sourcing_name != NULL)
24571 {
24572 if (save_sourcing_name != NULL
24573 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024574 sprintf((char *)sourcing_name, "%s[%d]..",
24575 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576 else
24577 STRCPY(sourcing_name, "function ");
24578 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24579
24580 if (p_verbose >= 12)
24581 {
24582 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024583 verbose_enter_scroll();
24584
Bram Moolenaar555b2802005-05-19 21:08:39 +000024585 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586 if (p_verbose >= 14)
24587 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024589 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024590 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024591 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024592
24593 msg_puts((char_u *)"(");
24594 for (i = 0; i < argcount; ++i)
24595 {
24596 if (i > 0)
24597 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024598 if (argvars[i].v_type == VAR_NUMBER)
24599 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600 else
24601 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024602 /* Do not want errors such as E724 here. */
24603 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024604 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024605 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024606 if (s != NULL)
24607 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024608 if (vim_strsize(s) > MSG_BUF_CLEN)
24609 {
24610 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24611 s = buf;
24612 }
24613 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024614 vim_free(tofree);
24615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616 }
24617 }
24618 msg_puts((char_u *)")");
24619 }
24620 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024621
24622 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024623 --no_wait_return;
24624 }
24625 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024626#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024627 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024628 {
24629 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24630 func_do_profile(fp);
24631 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024632 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024633 {
24634 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024635 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024636 profile_zero(&fp->uf_tm_children);
24637 }
24638 script_prof_save(&wait_start);
24639 }
24640#endif
24641
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024643 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024644 save_did_emsg = did_emsg;
24645 did_emsg = FALSE;
24646
24647 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024648 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024649 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24650
24651 --RedrawingDisabled;
24652
24653 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024654 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024656 clear_tv(rettv);
24657 rettv->v_type = VAR_NUMBER;
24658 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 }
24660
Bram Moolenaar05159a02005-02-26 23:04:13 +000024661#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024662 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024663 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024664 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024665 profile_end(&call_start);
24666 profile_sub_wait(&wait_start, &call_start);
24667 profile_add(&fp->uf_tm_total, &call_start);
24668 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024669 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024670 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024671 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24672 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024673 }
24674 }
24675#endif
24676
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677 /* when being verbose, mention the return value */
24678 if (p_verbose >= 12)
24679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024681 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682
Bram Moolenaar071d4272004-06-13 20:20:40 +000024683 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024684 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024685 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024686 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024687 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024688 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024689 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024690 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024691 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024692 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024693 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024694
Bram Moolenaar555b2802005-05-19 21:08:39 +000024695 /* The value may be very long. Skip the middle part, so that we
24696 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024697 * truncate it at the end. Don't want errors such as E724 here. */
24698 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024699 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024700 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024701 if (s != NULL)
24702 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024703 if (vim_strsize(s) > MSG_BUF_CLEN)
24704 {
24705 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24706 s = buf;
24707 }
24708 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024709 vim_free(tofree);
24710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024711 }
24712 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024713
24714 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715 --no_wait_return;
24716 }
24717
24718 vim_free(sourcing_name);
24719 sourcing_name = save_sourcing_name;
24720 sourcing_lnum = save_sourcing_lnum;
24721 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024722#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024723 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024724 script_prof_restore(&wait_start);
24725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024726
24727 if (p_verbose >= 12 && sourcing_name != NULL)
24728 {
24729 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024730 verbose_enter_scroll();
24731
Bram Moolenaar555b2802005-05-19 21:08:39 +000024732 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024734
24735 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024736 --no_wait_return;
24737 }
24738
24739 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024740 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024742
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024743 /* If the a:000 list and the l: and a: dicts are not referenced we can
24744 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024745 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24746 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24747 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24748 {
24749 free_funccal(fc, FALSE);
24750 }
24751 else
24752 {
24753 hashitem_T *hi;
24754 listitem_T *li;
24755 int todo;
24756
24757 /* "fc" is still in use. This can happen when returning "a:000" or
24758 * assigning "l:" to a global variable.
24759 * Link "fc" in the list for garbage collection later. */
24760 fc->caller = previous_funccal;
24761 previous_funccal = fc;
24762
24763 /* Make a copy of the a: variables, since we didn't do that above. */
24764 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24765 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24766 {
24767 if (!HASHITEM_EMPTY(hi))
24768 {
24769 --todo;
24770 v = HI2DI(hi);
24771 copy_tv(&v->di_tv, &v->di_tv);
24772 }
24773 }
24774
24775 /* Make a copy of the a:000 items, since we didn't do that above. */
24776 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24777 copy_tv(&li->li_tv, &li->li_tv);
24778 }
24779}
24780
24781/*
24782 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024783 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024784 */
24785 static int
24786can_free_funccal(fc, copyID)
24787 funccall_T *fc;
24788 int copyID;
24789{
24790 return (fc->l_varlist.lv_copyID != copyID
24791 && fc->l_vars.dv_copyID != copyID
24792 && fc->l_avars.dv_copyID != copyID);
24793}
24794
24795/*
24796 * Free "fc" and what it contains.
24797 */
24798 static void
24799free_funccal(fc, free_val)
24800 funccall_T *fc;
24801 int free_val; /* a: vars were allocated */
24802{
24803 listitem_T *li;
24804
24805 /* The a: variables typevals may not have been allocated, only free the
24806 * allocated variables. */
24807 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24808
24809 /* free all l: variables */
24810 vars_clear(&fc->l_vars.dv_hashtab);
24811
24812 /* Free the a:000 variables if they were allocated. */
24813 if (free_val)
24814 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24815 clear_tv(&li->li_tv);
24816
24817 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024818}
24819
24820/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024821 * Add a number variable "name" to dict "dp" with value "nr".
24822 */
24823 static void
24824add_nr_var(dp, v, name, nr)
24825 dict_T *dp;
24826 dictitem_T *v;
24827 char *name;
24828 varnumber_T nr;
24829{
24830 STRCPY(v->di_key, name);
24831 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24832 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24833 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024834 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024835 v->di_tv.vval.v_number = nr;
24836}
24837
24838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 * ":return [expr]"
24840 */
24841 void
24842ex_return(eap)
24843 exarg_T *eap;
24844{
24845 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024846 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847 int returning = FALSE;
24848
24849 if (current_funccal == NULL)
24850 {
24851 EMSG(_("E133: :return not inside a function"));
24852 return;
24853 }
24854
24855 if (eap->skip)
24856 ++emsg_skip;
24857
24858 eap->nextcmd = NULL;
24859 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024860 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024861 {
24862 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024863 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024865 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024866 }
24867 /* It's safer to return also on error. */
24868 else if (!eap->skip)
24869 {
24870 /*
24871 * Return unless the expression evaluation has been cancelled due to an
24872 * aborting error, an interrupt, or an exception.
24873 */
24874 if (!aborting())
24875 returning = do_return(eap, FALSE, TRUE, NULL);
24876 }
24877
24878 /* When skipping or the return gets pending, advance to the next command
24879 * in this line (!returning). Otherwise, ignore the rest of the line.
24880 * Following lines will be ignored by get_func_line(). */
24881 if (returning)
24882 eap->nextcmd = NULL;
24883 else if (eap->nextcmd == NULL) /* no argument */
24884 eap->nextcmd = check_nextcmd(arg);
24885
24886 if (eap->skip)
24887 --emsg_skip;
24888}
24889
24890/*
24891 * Return from a function. Possibly makes the return pending. Also called
24892 * for a pending return at the ":endtry" or after returning from an extra
24893 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024894 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024895 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896 * FALSE when the return gets pending.
24897 */
24898 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024899do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900 exarg_T *eap;
24901 int reanimate;
24902 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024903 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024904{
24905 int idx;
24906 struct condstack *cstack = eap->cstack;
24907
24908 if (reanimate)
24909 /* Undo the return. */
24910 current_funccal->returned = FALSE;
24911
24912 /*
24913 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24914 * not in its finally clause (which then is to be executed next) is found.
24915 * In this case, make the ":return" pending for execution at the ":endtry".
24916 * Otherwise, return normally.
24917 */
24918 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24919 if (idx >= 0)
24920 {
24921 cstack->cs_pending[idx] = CSTP_RETURN;
24922
24923 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024924 /* A pending return again gets pending. "rettv" points to an
24925 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024926 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024927 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928 else
24929 {
24930 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024931 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024932 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024933 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024935 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024936 {
24937 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024938 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024939 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940 else
24941 EMSG(_(e_outofmem));
24942 }
24943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024944 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024945
24946 if (reanimate)
24947 {
24948 /* The pending return value could be overwritten by a ":return"
24949 * without argument in a finally clause; reset the default
24950 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024951 current_funccal->rettv->v_type = VAR_NUMBER;
24952 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953 }
24954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024955 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024956 }
24957 else
24958 {
24959 current_funccal->returned = TRUE;
24960
24961 /* If the return is carried out now, store the return value. For
24962 * a return immediately after reanimation, the value is already
24963 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024964 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024966 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024967 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024968 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024969 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970 }
24971 }
24972
24973 return idx < 0;
24974}
24975
24976/*
24977 * Free the variable with a pending return value.
24978 */
24979 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024980discard_pending_return(rettv)
24981 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024982{
Bram Moolenaar33570922005-01-25 22:26:29 +000024983 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024984}
24985
24986/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024987 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 * is an allocated string. Used by report_pending() for verbose messages.
24989 */
24990 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024991get_return_cmd(rettv)
24992 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024993{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024994 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024995 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024996 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024997
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024998 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024999 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025000 if (s == NULL)
25001 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025002
25003 STRCPY(IObuff, ":return ");
25004 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25005 if (STRLEN(s) + 8 >= IOSIZE)
25006 STRCPY(IObuff + IOSIZE - 4, "...");
25007 vim_free(tofree);
25008 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025009}
25010
25011/*
25012 * Get next function line.
25013 * Called by do_cmdline() to get the next line.
25014 * Returns allocated string, or NULL for end of function.
25015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025016 char_u *
25017get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025018 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025020 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025021{
Bram Moolenaar33570922005-01-25 22:26:29 +000025022 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025023 ufunc_T *fp = fcp->func;
25024 char_u *retval;
25025 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026
25027 /* If breakpoints have been added/deleted need to check for it. */
25028 if (fcp->dbg_tick != debug_tick)
25029 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025030 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031 sourcing_lnum);
25032 fcp->dbg_tick = debug_tick;
25033 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025034#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025035 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025036 func_line_end(cookie);
25037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025038
Bram Moolenaar05159a02005-02-26 23:04:13 +000025039 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025040 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25041 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042 retval = NULL;
25043 else
25044 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025045 /* Skip NULL lines (continuation lines). */
25046 while (fcp->linenr < gap->ga_len
25047 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25048 ++fcp->linenr;
25049 if (fcp->linenr >= gap->ga_len)
25050 retval = NULL;
25051 else
25052 {
25053 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25054 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025055#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025056 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025057 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025058#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060 }
25061
25062 /* Did we encounter a breakpoint? */
25063 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25064 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025065 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025066 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025067 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 sourcing_lnum);
25069 fcp->dbg_tick = debug_tick;
25070 }
25071
25072 return retval;
25073}
25074
Bram Moolenaar05159a02005-02-26 23:04:13 +000025075#if defined(FEAT_PROFILE) || defined(PROTO)
25076/*
25077 * Called when starting to read a function line.
25078 * "sourcing_lnum" must be correct!
25079 * When skipping lines it may not actually be executed, but we won't find out
25080 * until later and we need to store the time now.
25081 */
25082 void
25083func_line_start(cookie)
25084 void *cookie;
25085{
25086 funccall_T *fcp = (funccall_T *)cookie;
25087 ufunc_T *fp = fcp->func;
25088
25089 if (fp->uf_profiling && sourcing_lnum >= 1
25090 && sourcing_lnum <= fp->uf_lines.ga_len)
25091 {
25092 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025093 /* Skip continuation lines. */
25094 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25095 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025096 fp->uf_tml_execed = FALSE;
25097 profile_start(&fp->uf_tml_start);
25098 profile_zero(&fp->uf_tml_children);
25099 profile_get_wait(&fp->uf_tml_wait);
25100 }
25101}
25102
25103/*
25104 * Called when actually executing a function line.
25105 */
25106 void
25107func_line_exec(cookie)
25108 void *cookie;
25109{
25110 funccall_T *fcp = (funccall_T *)cookie;
25111 ufunc_T *fp = fcp->func;
25112
25113 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25114 fp->uf_tml_execed = TRUE;
25115}
25116
25117/*
25118 * Called when done with a function line.
25119 */
25120 void
25121func_line_end(cookie)
25122 void *cookie;
25123{
25124 funccall_T *fcp = (funccall_T *)cookie;
25125 ufunc_T *fp = fcp->func;
25126
25127 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25128 {
25129 if (fp->uf_tml_execed)
25130 {
25131 ++fp->uf_tml_count[fp->uf_tml_idx];
25132 profile_end(&fp->uf_tml_start);
25133 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025134 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025135 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25136 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025137 }
25138 fp->uf_tml_idx = -1;
25139 }
25140}
25141#endif
25142
Bram Moolenaar071d4272004-06-13 20:20:40 +000025143/*
25144 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025145 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025146 */
25147 int
25148func_has_ended(cookie)
25149 void *cookie;
25150{
Bram Moolenaar33570922005-01-25 22:26:29 +000025151 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025152
25153 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25154 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025155 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156 || fcp->returned);
25157}
25158
25159/*
25160 * return TRUE if cookie indicates a function which "abort"s on errors.
25161 */
25162 int
25163func_has_abort(cookie)
25164 void *cookie;
25165{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025166 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025167}
25168
25169#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25170typedef enum
25171{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025172 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25173 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25174 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175} var_flavour_T;
25176
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025177static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178
25179 static var_flavour_T
25180var_flavour(varname)
25181 char_u *varname;
25182{
25183 char_u *p = varname;
25184
25185 if (ASCII_ISUPPER(*p))
25186 {
25187 while (*(++p))
25188 if (ASCII_ISLOWER(*p))
25189 return VAR_FLAVOUR_SESSION;
25190 return VAR_FLAVOUR_VIMINFO;
25191 }
25192 else
25193 return VAR_FLAVOUR_DEFAULT;
25194}
25195#endif
25196
25197#if defined(FEAT_VIMINFO) || defined(PROTO)
25198/*
25199 * Restore global vars that start with a capital from the viminfo file
25200 */
25201 int
25202read_viminfo_varlist(virp, writing)
25203 vir_T *virp;
25204 int writing;
25205{
25206 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025207 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025208 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025209 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210
25211 if (!writing && (find_viminfo_parameter('!') != NULL))
25212 {
25213 tab = vim_strchr(virp->vir_line + 1, '\t');
25214 if (tab != NULL)
25215 {
25216 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025217 switch (*tab)
25218 {
25219 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025220#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025221 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025222#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025223 case 'D': type = VAR_DICT; break;
25224 case 'L': type = VAR_LIST; break;
25225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025226
25227 tab = vim_strchr(tab, '\t');
25228 if (tab != NULL)
25229 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025230 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025231 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025232 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025234#ifdef FEAT_FLOAT
25235 else if (type == VAR_FLOAT)
25236 (void)string2float(tab + 1, &tv.vval.v_float);
25237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025238 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025239 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025240 if (type == VAR_DICT || type == VAR_LIST)
25241 {
25242 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25243
25244 if (etv == NULL)
25245 /* Failed to parse back the dict or list, use it as a
25246 * string. */
25247 tv.v_type = VAR_STRING;
25248 else
25249 {
25250 vim_free(tv.vval.v_string);
25251 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025252 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025253 }
25254 }
25255
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025256 /* when in a function use global variables */
25257 save_funccal = current_funccal;
25258 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025259 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025260 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025261
25262 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025263 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025264 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25265 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266 }
25267 }
25268 }
25269
25270 return viminfo_readline(virp);
25271}
25272
25273/*
25274 * Write global vars that start with a capital to the viminfo file
25275 */
25276 void
25277write_viminfo_varlist(fp)
25278 FILE *fp;
25279{
Bram Moolenaar33570922005-01-25 22:26:29 +000025280 hashitem_T *hi;
25281 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025282 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025283 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025284 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025285 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025286 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287
25288 if (find_viminfo_parameter('!') == NULL)
25289 return;
25290
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025291 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025292
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025293 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025294 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025296 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025297 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025298 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025299 this_var = HI2DI(hi);
25300 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025301 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025302 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025303 {
25304 case VAR_STRING: s = "STR"; break;
25305 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025306#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025307 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025308#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025309 case VAR_DICT: s = "DIC"; break;
25310 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025311 default: continue;
25312 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025313 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025314 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025315 if (p != NULL)
25316 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025317 vim_free(tofree);
25318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319 }
25320 }
25321}
25322#endif
25323
25324#if defined(FEAT_SESSION) || defined(PROTO)
25325 int
25326store_session_globals(fd)
25327 FILE *fd;
25328{
Bram Moolenaar33570922005-01-25 22:26:29 +000025329 hashitem_T *hi;
25330 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025331 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332 char_u *p, *t;
25333
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025334 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025335 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025337 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025339 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025340 this_var = HI2DI(hi);
25341 if ((this_var->di_tv.v_type == VAR_NUMBER
25342 || this_var->di_tv.v_type == VAR_STRING)
25343 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025344 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025345 /* Escape special characters with a backslash. Turn a LF and
25346 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025347 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025348 (char_u *)"\\\"\n\r");
25349 if (p == NULL) /* out of memory */
25350 break;
25351 for (t = p; *t != NUL; ++t)
25352 if (*t == '\n')
25353 *t = 'n';
25354 else if (*t == '\r')
25355 *t = 'r';
25356 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025357 this_var->di_key,
25358 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25359 : ' ',
25360 p,
25361 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25362 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025363 || put_eol(fd) == FAIL)
25364 {
25365 vim_free(p);
25366 return FAIL;
25367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025368 vim_free(p);
25369 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025370#ifdef FEAT_FLOAT
25371 else if (this_var->di_tv.v_type == VAR_FLOAT
25372 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25373 {
25374 float_T f = this_var->di_tv.vval.v_float;
25375 int sign = ' ';
25376
25377 if (f < 0)
25378 {
25379 f = -f;
25380 sign = '-';
25381 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025382 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025383 this_var->di_key, sign, f) < 0)
25384 || put_eol(fd) == FAIL)
25385 return FAIL;
25386 }
25387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388 }
25389 }
25390 return OK;
25391}
25392#endif
25393
Bram Moolenaar661b1822005-07-28 22:36:45 +000025394/*
25395 * Display script name where an item was last set.
25396 * Should only be invoked when 'verbose' is non-zero.
25397 */
25398 void
25399last_set_msg(scriptID)
25400 scid_T scriptID;
25401{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025402 char_u *p;
25403
Bram Moolenaar661b1822005-07-28 22:36:45 +000025404 if (scriptID != 0)
25405 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025406 p = home_replace_save(NULL, get_scriptname(scriptID));
25407 if (p != NULL)
25408 {
25409 verbose_enter();
25410 MSG_PUTS(_("\n\tLast set from "));
25411 MSG_PUTS(p);
25412 vim_free(p);
25413 verbose_leave();
25414 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025415 }
25416}
25417
Bram Moolenaard812df62008-11-09 12:46:09 +000025418/*
25419 * List v:oldfiles in a nice way.
25420 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025421 void
25422ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025423 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025424{
25425 list_T *l = vimvars[VV_OLDFILES].vv_list;
25426 listitem_T *li;
25427 int nr = 0;
25428
25429 if (l == NULL)
25430 msg((char_u *)_("No old files"));
25431 else
25432 {
25433 msg_start();
25434 msg_scroll = TRUE;
25435 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25436 {
25437 msg_outnum((long)++nr);
25438 MSG_PUTS(": ");
25439 msg_outtrans(get_tv_string(&li->li_tv));
25440 msg_putchar('\n');
25441 out_flush(); /* output one line at a time */
25442 ui_breakcheck();
25443 }
25444 /* Assume "got_int" was set to truncate the listing. */
25445 got_int = FALSE;
25446
25447#ifdef FEAT_BROWSE_CMD
25448 if (cmdmod.browse)
25449 {
25450 quit_more = FALSE;
25451 nr = prompt_for_number(FALSE);
25452 msg_starthere();
25453 if (nr > 0)
25454 {
25455 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25456 (long)nr);
25457
25458 if (p != NULL)
25459 {
25460 p = expand_env_save(p);
25461 eap->arg = p;
25462 eap->cmdidx = CMD_edit;
25463 cmdmod.browse = FALSE;
25464 do_exedit(eap, NULL);
25465 vim_free(p);
25466 }
25467 }
25468 }
25469#endif
25470 }
25471}
25472
Bram Moolenaar53744302015-07-17 17:38:22 +020025473/* reset v:option_new, v:option_old and v:option_type */
25474 void
25475reset_v_option_vars()
25476{
25477 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25478 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25479 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25480}
25481
25482
Bram Moolenaar071d4272004-06-13 20:20:40 +000025483#endif /* FEAT_EVAL */
25484
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025486#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025487
25488#ifdef WIN3264
25489/*
25490 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25491 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025492static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25493static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25494static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025495
25496/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025497 * Get the short path (8.3) for the filename in "fnamep".
25498 * Only works for a valid file name.
25499 * When the path gets longer "fnamep" is changed and the allocated buffer
25500 * is put in "bufp".
25501 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25502 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503 */
25504 static int
25505get_short_pathname(fnamep, bufp, fnamelen)
25506 char_u **fnamep;
25507 char_u **bufp;
25508 int *fnamelen;
25509{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025510 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511 char_u *newbuf;
25512
25513 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025514 l = GetShortPathName(*fnamep, *fnamep, len);
25515 if (l > len - 1)
25516 {
25517 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025518 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025519 newbuf = vim_strnsave(*fnamep, l);
25520 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025521 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522
25523 vim_free(*bufp);
25524 *fnamep = *bufp = newbuf;
25525
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025526 /* Really should always succeed, as the buffer is big enough. */
25527 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025528 }
25529
25530 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025531 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532}
25533
25534/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025535 * Get the short path (8.3) for the filename in "fname". The converted
25536 * path is returned in "bufp".
25537 *
25538 * Some of the directories specified in "fname" may not exist. This function
25539 * will shorten the existing directories at the beginning of the path and then
25540 * append the remaining non-existing path.
25541 *
25542 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025543 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025544 * bufp - Pointer to an allocated buffer for the filename.
25545 * fnamelen - Length of the filename pointed to by fname
25546 *
25547 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025548 */
25549 static int
25550shortpath_for_invalid_fname(fname, bufp, fnamelen)
25551 char_u **fname;
25552 char_u **bufp;
25553 int *fnamelen;
25554{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025555 char_u *short_fname, *save_fname, *pbuf_unused;
25556 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025558 int old_len, len;
25559 int new_len, sfx_len;
25560 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561
25562 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025563 old_len = *fnamelen;
25564 save_fname = vim_strnsave(*fname, old_len);
25565 pbuf_unused = NULL;
25566 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025568 endp = save_fname + old_len - 1; /* Find the end of the copy */
25569 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025571 /*
25572 * Try shortening the supplied path till it succeeds by removing one
25573 * directory at a time from the tail of the path.
25574 */
25575 len = 0;
25576 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025578 /* go back one path-separator */
25579 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25580 --endp;
25581 if (endp <= save_fname)
25582 break; /* processed the complete path */
25583
25584 /*
25585 * Replace the path separator with a NUL and try to shorten the
25586 * resulting path.
25587 */
25588 ch = *endp;
25589 *endp = 0;
25590 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025591 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025592 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25593 {
25594 retval = FAIL;
25595 goto theend;
25596 }
25597 *endp = ch; /* preserve the string */
25598
25599 if (len > 0)
25600 break; /* successfully shortened the path */
25601
25602 /* failed to shorten the path. Skip the path separator */
25603 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025604 }
25605
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025606 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025608 /*
25609 * Succeeded in shortening the path. Now concatenate the shortened
25610 * path with the remaining path at the tail.
25611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025613 /* Compute the length of the new path. */
25614 sfx_len = (int)(save_endp - endp) + 1;
25615 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025617 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025619 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025621 /* There is not enough space in the currently allocated string,
25622 * copy it to a buffer big enough. */
25623 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025624 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025625 {
25626 retval = FAIL;
25627 goto theend;
25628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025629 }
25630 else
25631 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025632 /* Transfer short_fname to the main buffer (it's big enough),
25633 * unless get_short_pathname() did its work in-place. */
25634 *fname = *bufp = save_fname;
25635 if (short_fname != save_fname)
25636 vim_strncpy(save_fname, short_fname, len);
25637 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025638 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025639
25640 /* concat the not-shortened part of the path */
25641 vim_strncpy(*fname + len, endp, sfx_len);
25642 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025643 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025644
25645theend:
25646 vim_free(pbuf_unused);
25647 vim_free(save_fname);
25648
25649 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650}
25651
25652/*
25653 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025654 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 */
25656 static int
25657shortpath_for_partial(fnamep, bufp, fnamelen)
25658 char_u **fnamep;
25659 char_u **bufp;
25660 int *fnamelen;
25661{
25662 int sepcount, len, tflen;
25663 char_u *p;
25664 char_u *pbuf, *tfname;
25665 int hasTilde;
25666
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025667 /* Count up the path separators from the RHS.. so we know which part
25668 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025669 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025670 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025671 if (vim_ispathsep(*p))
25672 ++sepcount;
25673
25674 /* Need full path first (use expand_env() to remove a "~/") */
25675 hasTilde = (**fnamep == '~');
25676 if (hasTilde)
25677 pbuf = tfname = expand_env_save(*fnamep);
25678 else
25679 pbuf = tfname = FullName_save(*fnamep, FALSE);
25680
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025681 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025683 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25684 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025685
25686 if (len == 0)
25687 {
25688 /* Don't have a valid filename, so shorten the rest of the
25689 * path if we can. This CAN give us invalid 8.3 filenames, but
25690 * there's not a lot of point in guessing what it might be.
25691 */
25692 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025693 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25694 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025695 }
25696
25697 /* Count the paths backward to find the beginning of the desired string. */
25698 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025699 {
25700#ifdef FEAT_MBYTE
25701 if (has_mbyte)
25702 p -= mb_head_off(tfname, p);
25703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704 if (vim_ispathsep(*p))
25705 {
25706 if (sepcount == 0 || (hasTilde && sepcount == 1))
25707 break;
25708 else
25709 sepcount --;
25710 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025712 if (hasTilde)
25713 {
25714 --p;
25715 if (p >= tfname)
25716 *p = '~';
25717 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025718 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025719 }
25720 else
25721 ++p;
25722
25723 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25724 vim_free(*bufp);
25725 *fnamelen = (int)STRLEN(p);
25726 *bufp = pbuf;
25727 *fnamep = p;
25728
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025729 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025730}
25731#endif /* WIN3264 */
25732
25733/*
25734 * Adjust a filename, according to a string of modifiers.
25735 * *fnamep must be NUL terminated when called. When returning, the length is
25736 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025737 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 * When there is an error, *fnamep is set to NULL.
25739 */
25740 int
25741modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25742 char_u *src; /* string with modifiers */
25743 int *usedlen; /* characters after src that are used */
25744 char_u **fnamep; /* file name so far */
25745 char_u **bufp; /* buffer for allocated file name or NULL */
25746 int *fnamelen; /* length of fnamep */
25747{
25748 int valid = 0;
25749 char_u *tail;
25750 char_u *s, *p, *pbuf;
25751 char_u dirname[MAXPATHL];
25752 int c;
25753 int has_fullname = 0;
25754#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025755 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025756 int has_shortname = 0;
25757#endif
25758
25759repeat:
25760 /* ":p" - full path/file_name */
25761 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25762 {
25763 has_fullname = 1;
25764
25765 valid |= VALID_PATH;
25766 *usedlen += 2;
25767
25768 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25769 if ((*fnamep)[0] == '~'
25770#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25771 && ((*fnamep)[1] == '/'
25772# ifdef BACKSLASH_IN_FILENAME
25773 || (*fnamep)[1] == '\\'
25774# endif
25775 || (*fnamep)[1] == NUL)
25776
25777#endif
25778 )
25779 {
25780 *fnamep = expand_env_save(*fnamep);
25781 vim_free(*bufp); /* free any allocated file name */
25782 *bufp = *fnamep;
25783 if (*fnamep == NULL)
25784 return -1;
25785 }
25786
25787 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025788 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025789 {
25790 if (vim_ispathsep(*p)
25791 && p[1] == '.'
25792 && (p[2] == NUL
25793 || vim_ispathsep(p[2])
25794 || (p[2] == '.'
25795 && (p[3] == NUL || vim_ispathsep(p[3])))))
25796 break;
25797 }
25798
25799 /* FullName_save() is slow, don't use it when not needed. */
25800 if (*p != NUL || !vim_isAbsName(*fnamep))
25801 {
25802 *fnamep = FullName_save(*fnamep, *p != NUL);
25803 vim_free(*bufp); /* free any allocated file name */
25804 *bufp = *fnamep;
25805 if (*fnamep == NULL)
25806 return -1;
25807 }
25808
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025809#ifdef WIN3264
25810# if _WIN32_WINNT >= 0x0500
25811 if (vim_strchr(*fnamep, '~') != NULL)
25812 {
25813 /* Expand 8.3 filename to full path. Needed to make sure the same
25814 * file does not have two different names.
25815 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25816 p = alloc(_MAX_PATH + 1);
25817 if (p != NULL)
25818 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025819 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025820 {
25821 vim_free(*bufp);
25822 *bufp = *fnamep = p;
25823 }
25824 else
25825 vim_free(p);
25826 }
25827 }
25828# endif
25829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 /* Append a path separator to a directory. */
25831 if (mch_isdir(*fnamep))
25832 {
25833 /* Make room for one or two extra characters. */
25834 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25835 vim_free(*bufp); /* free any allocated file name */
25836 *bufp = *fnamep;
25837 if (*fnamep == NULL)
25838 return -1;
25839 add_pathsep(*fnamep);
25840 }
25841 }
25842
25843 /* ":." - path relative to the current directory */
25844 /* ":~" - path relative to the home directory */
25845 /* ":8" - shortname path - postponed till after */
25846 while (src[*usedlen] == ':'
25847 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25848 {
25849 *usedlen += 2;
25850 if (c == '8')
25851 {
25852#ifdef WIN3264
25853 has_shortname = 1; /* Postpone this. */
25854#endif
25855 continue;
25856 }
25857 pbuf = NULL;
25858 /* Need full path first (use expand_env() to remove a "~/") */
25859 if (!has_fullname)
25860 {
25861 if (c == '.' && **fnamep == '~')
25862 p = pbuf = expand_env_save(*fnamep);
25863 else
25864 p = pbuf = FullName_save(*fnamep, FALSE);
25865 }
25866 else
25867 p = *fnamep;
25868
25869 has_fullname = 0;
25870
25871 if (p != NULL)
25872 {
25873 if (c == '.')
25874 {
25875 mch_dirname(dirname, MAXPATHL);
25876 s = shorten_fname(p, dirname);
25877 if (s != NULL)
25878 {
25879 *fnamep = s;
25880 if (pbuf != NULL)
25881 {
25882 vim_free(*bufp); /* free any allocated file name */
25883 *bufp = pbuf;
25884 pbuf = NULL;
25885 }
25886 }
25887 }
25888 else
25889 {
25890 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25891 /* Only replace it when it starts with '~' */
25892 if (*dirname == '~')
25893 {
25894 s = vim_strsave(dirname);
25895 if (s != NULL)
25896 {
25897 *fnamep = s;
25898 vim_free(*bufp);
25899 *bufp = s;
25900 }
25901 }
25902 }
25903 vim_free(pbuf);
25904 }
25905 }
25906
25907 tail = gettail(*fnamep);
25908 *fnamelen = (int)STRLEN(*fnamep);
25909
25910 /* ":h" - head, remove "/file_name", can be repeated */
25911 /* Don't remove the first "/" or "c:\" */
25912 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25913 {
25914 valid |= VALID_HEAD;
25915 *usedlen += 2;
25916 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025917 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025918 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025919 *fnamelen = (int)(tail - *fnamep);
25920#ifdef VMS
25921 if (*fnamelen > 0)
25922 *fnamelen += 1; /* the path separator is part of the path */
25923#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025924 if (*fnamelen == 0)
25925 {
25926 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25927 p = vim_strsave((char_u *)".");
25928 if (p == NULL)
25929 return -1;
25930 vim_free(*bufp);
25931 *bufp = *fnamep = tail = p;
25932 *fnamelen = 1;
25933 }
25934 else
25935 {
25936 while (tail > s && !after_pathsep(s, tail))
25937 mb_ptr_back(*fnamep, tail);
25938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025939 }
25940
25941 /* ":8" - shortname */
25942 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25943 {
25944 *usedlen += 2;
25945#ifdef WIN3264
25946 has_shortname = 1;
25947#endif
25948 }
25949
25950#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025951 /*
25952 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025953 */
25954 if (has_shortname)
25955 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025956 /* Copy the string if it is shortened by :h and when it wasn't copied
25957 * yet, because we are going to change it in place. Avoids changing
25958 * the buffer name for "%:8". */
25959 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025960 {
25961 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025962 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025963 return -1;
25964 vim_free(*bufp);
25965 *bufp = *fnamep = p;
25966 }
25967
25968 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025969 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970 if (!has_fullname && !vim_isAbsName(*fnamep))
25971 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025972 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025973 return -1;
25974 }
25975 else
25976 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025977 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025978
Bram Moolenaardc935552011-08-17 15:23:23 +020025979 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025981 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025982 return -1;
25983
25984 if (l == 0)
25985 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025986 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025988 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989 return -1;
25990 }
25991 *fnamelen = l;
25992 }
25993 }
25994#endif /* WIN3264 */
25995
25996 /* ":t" - tail, just the basename */
25997 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25998 {
25999 *usedlen += 2;
26000 *fnamelen -= (int)(tail - *fnamep);
26001 *fnamep = tail;
26002 }
26003
26004 /* ":e" - extension, can be repeated */
26005 /* ":r" - root, without extension, can be repeated */
26006 while (src[*usedlen] == ':'
26007 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26008 {
26009 /* find a '.' in the tail:
26010 * - for second :e: before the current fname
26011 * - otherwise: The last '.'
26012 */
26013 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26014 s = *fnamep - 2;
26015 else
26016 s = *fnamep + *fnamelen - 1;
26017 for ( ; s > tail; --s)
26018 if (s[0] == '.')
26019 break;
26020 if (src[*usedlen + 1] == 'e') /* :e */
26021 {
26022 if (s > tail)
26023 {
26024 *fnamelen += (int)(*fnamep - (s + 1));
26025 *fnamep = s + 1;
26026#ifdef VMS
26027 /* cut version from the extension */
26028 s = *fnamep + *fnamelen - 1;
26029 for ( ; s > *fnamep; --s)
26030 if (s[0] == ';')
26031 break;
26032 if (s > *fnamep)
26033 *fnamelen = s - *fnamep;
26034#endif
26035 }
26036 else if (*fnamep <= tail)
26037 *fnamelen = 0;
26038 }
26039 else /* :r */
26040 {
26041 if (s > tail) /* remove one extension */
26042 *fnamelen = (int)(s - *fnamep);
26043 }
26044 *usedlen += 2;
26045 }
26046
26047 /* ":s?pat?foo?" - substitute */
26048 /* ":gs?pat?foo?" - global substitute */
26049 if (src[*usedlen] == ':'
26050 && (src[*usedlen + 1] == 's'
26051 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26052 {
26053 char_u *str;
26054 char_u *pat;
26055 char_u *sub;
26056 int sep;
26057 char_u *flags;
26058 int didit = FALSE;
26059
26060 flags = (char_u *)"";
26061 s = src + *usedlen + 2;
26062 if (src[*usedlen + 1] == 'g')
26063 {
26064 flags = (char_u *)"g";
26065 ++s;
26066 }
26067
26068 sep = *s++;
26069 if (sep)
26070 {
26071 /* find end of pattern */
26072 p = vim_strchr(s, sep);
26073 if (p != NULL)
26074 {
26075 pat = vim_strnsave(s, (int)(p - s));
26076 if (pat != NULL)
26077 {
26078 s = p + 1;
26079 /* find end of substitution */
26080 p = vim_strchr(s, sep);
26081 if (p != NULL)
26082 {
26083 sub = vim_strnsave(s, (int)(p - s));
26084 str = vim_strnsave(*fnamep, *fnamelen);
26085 if (sub != NULL && str != NULL)
26086 {
26087 *usedlen = (int)(p + 1 - src);
26088 s = do_string_sub(str, pat, sub, flags);
26089 if (s != NULL)
26090 {
26091 *fnamep = s;
26092 *fnamelen = (int)STRLEN(s);
26093 vim_free(*bufp);
26094 *bufp = s;
26095 didit = TRUE;
26096 }
26097 }
26098 vim_free(sub);
26099 vim_free(str);
26100 }
26101 vim_free(pat);
26102 }
26103 }
26104 /* after using ":s", repeat all the modifiers */
26105 if (didit)
26106 goto repeat;
26107 }
26108 }
26109
Bram Moolenaar26df0922014-02-23 23:39:13 +010026110 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26111 {
26112 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26113 if (p == NULL)
26114 return -1;
26115 vim_free(*bufp);
26116 *bufp = *fnamep = p;
26117 *fnamelen = (int)STRLEN(p);
26118 *usedlen += 2;
26119 }
26120
Bram Moolenaar071d4272004-06-13 20:20:40 +000026121 return valid;
26122}
26123
26124/*
26125 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26126 * "flags" can be "g" to do a global substitute.
26127 * Returns an allocated string, NULL for error.
26128 */
26129 char_u *
26130do_string_sub(str, pat, sub, flags)
26131 char_u *str;
26132 char_u *pat;
26133 char_u *sub;
26134 char_u *flags;
26135{
26136 int sublen;
26137 regmatch_T regmatch;
26138 int i;
26139 int do_all;
26140 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026141 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026142 garray_T ga;
26143 char_u *ret;
26144 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026145 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026146
26147 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26148 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026149 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026150
26151 ga_init2(&ga, 1, 200);
26152
26153 do_all = (flags[0] == 'g');
26154
26155 regmatch.rm_ic = p_ic;
26156 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26157 if (regmatch.regprog != NULL)
26158 {
26159 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026160 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026161 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26162 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026163 /* Skip empty match except for first match. */
26164 if (regmatch.startp[0] == regmatch.endp[0])
26165 {
26166 if (zero_width == regmatch.startp[0])
26167 {
26168 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026169 i = MB_PTR2LEN(tail);
26170 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26171 (size_t)i);
26172 ga.ga_len += i;
26173 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026174 continue;
26175 }
26176 zero_width = regmatch.startp[0];
26177 }
26178
Bram Moolenaar071d4272004-06-13 20:20:40 +000026179 /*
26180 * Get some space for a temporary buffer to do the substitution
26181 * into. It will contain:
26182 * - The text up to where the match is.
26183 * - The substituted text.
26184 * - The text after the match.
26185 */
26186 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026187 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026188 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26189 {
26190 ga_clear(&ga);
26191 break;
26192 }
26193
26194 /* copy the text up to where the match is */
26195 i = (int)(regmatch.startp[0] - tail);
26196 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26197 /* add the substituted text */
26198 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26199 + ga.ga_len + i, TRUE, TRUE, FALSE);
26200 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026201 tail = regmatch.endp[0];
26202 if (*tail == NUL)
26203 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026204 if (!do_all)
26205 break;
26206 }
26207
26208 if (ga.ga_data != NULL)
26209 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26210
Bram Moolenaar473de612013-06-08 18:19:48 +020026211 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026212 }
26213
26214 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26215 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026216 if (p_cpo == empty_option)
26217 p_cpo = save_cpo;
26218 else
26219 /* Darn, evaluating {sub} expression changed the value. */
26220 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026221
26222 return ret;
26223}
26224
26225#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */