blob: b79bb02f9fc57b855985e21c43a9d358b0eaeee7 [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);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100454#ifdef FEAT_JOB
455static void job_free(job_T *job);
456#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
458static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
462static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
463static 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);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_abs(typval_T *argvars, typval_T *rettv);
469static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_add(typval_T *argvars, typval_T *rettv);
472static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
473static void f_and(typval_T *argvars, typval_T *rettv);
474static void f_append(typval_T *argvars, typval_T *rettv);
475static void f_argc(typval_T *argvars, typval_T *rettv);
476static void f_argidx(typval_T *argvars, typval_T *rettv);
477static void f_arglistid(typval_T *argvars, typval_T *rettv);
478static void f_argv(typval_T *argvars, typval_T *rettv);
479static void f_assert_equal(typval_T *argvars, typval_T *rettv);
480static void f_assert_exception(typval_T *argvars, typval_T *rettv);
481static void f_assert_fails(typval_T *argvars, typval_T *rettv);
482static void f_assert_false(typval_T *argvars, typval_T *rettv);
483static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100485static void f_asin(typval_T *argvars, typval_T *rettv);
486static void f_atan(typval_T *argvars, typval_T *rettv);
487static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_browse(typval_T *argvars, typval_T *rettv);
490static void f_browsedir(typval_T *argvars, typval_T *rettv);
491static void f_bufexists(typval_T *argvars, typval_T *rettv);
492static void f_buflisted(typval_T *argvars, typval_T *rettv);
493static void f_bufloaded(typval_T *argvars, typval_T *rettv);
494static void f_bufname(typval_T *argvars, typval_T *rettv);
495static void f_bufnr(typval_T *argvars, typval_T *rettv);
496static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
497static void f_byte2line(typval_T *argvars, typval_T *rettv);
498static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
499static void f_byteidx(typval_T *argvars, typval_T *rettv);
500static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
501static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100503static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100505#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100506static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100507static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
508static void f_ch_open(typval_T *argvars, typval_T *rettv);
509static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100512static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100513#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_changenr(typval_T *argvars, typval_T *rettv);
515static void f_char2nr(typval_T *argvars, typval_T *rettv);
516static void f_cindent(typval_T *argvars, typval_T *rettv);
517static void f_clearmatches(typval_T *argvars, typval_T *rettv);
518static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000519#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100520static void f_complete(typval_T *argvars, typval_T *rettv);
521static void f_complete_add(typval_T *argvars, typval_T *rettv);
522static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_confirm(typval_T *argvars, typval_T *rettv);
525static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_cos(typval_T *argvars, typval_T *rettv);
528static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_count(typval_T *argvars, typval_T *rettv);
531static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
532static void f_cursor(typval_T *argsvars, typval_T *rettv);
533static void f_deepcopy(typval_T *argvars, typval_T *rettv);
534static void f_delete(typval_T *argvars, typval_T *rettv);
535static void f_did_filetype(typval_T *argvars, typval_T *rettv);
536static void f_diff_filler(typval_T *argvars, typval_T *rettv);
537static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100538static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_empty(typval_T *argvars, typval_T *rettv);
540static void f_escape(typval_T *argvars, typval_T *rettv);
541static void f_eval(typval_T *argvars, typval_T *rettv);
542static void f_eventhandler(typval_T *argvars, typval_T *rettv);
543static void f_executable(typval_T *argvars, typval_T *rettv);
544static void f_exepath(typval_T *argvars, typval_T *rettv);
545static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_expand(typval_T *argvars, typval_T *rettv);
550static void f_extend(typval_T *argvars, typval_T *rettv);
551static void f_feedkeys(typval_T *argvars, typval_T *rettv);
552static void f_filereadable(typval_T *argvars, typval_T *rettv);
553static void f_filewritable(typval_T *argvars, typval_T *rettv);
554static void f_filter(typval_T *argvars, typval_T *rettv);
555static void f_finddir(typval_T *argvars, typval_T *rettv);
556static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000557#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_float2nr(typval_T *argvars, typval_T *rettv);
559static void f_floor(typval_T *argvars, typval_T *rettv);
560static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_fnameescape(typval_T *argvars, typval_T *rettv);
563static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
564static void f_foldclosed(typval_T *argvars, typval_T *rettv);
565static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
566static void f_foldlevel(typval_T *argvars, typval_T *rettv);
567static void f_foldtext(typval_T *argvars, typval_T *rettv);
568static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
569static void f_foreground(typval_T *argvars, typval_T *rettv);
570static void f_function(typval_T *argvars, typval_T *rettv);
571static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
572static void f_get(typval_T *argvars, typval_T *rettv);
573static void f_getbufline(typval_T *argvars, typval_T *rettv);
574static void f_getbufvar(typval_T *argvars, typval_T *rettv);
575static void f_getchar(typval_T *argvars, typval_T *rettv);
576static void f_getcharmod(typval_T *argvars, typval_T *rettv);
577static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
578static void f_getcmdline(typval_T *argvars, typval_T *rettv);
579static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
580static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
581static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
582static void f_getcwd(typval_T *argvars, typval_T *rettv);
583static void f_getfontname(typval_T *argvars, typval_T *rettv);
584static void f_getfperm(typval_T *argvars, typval_T *rettv);
585static void f_getfsize(typval_T *argvars, typval_T *rettv);
586static void f_getftime(typval_T *argvars, typval_T *rettv);
587static void f_getftype(typval_T *argvars, typval_T *rettv);
588static void f_getline(typval_T *argvars, typval_T *rettv);
589static void f_getmatches(typval_T *argvars, typval_T *rettv);
590static void f_getpid(typval_T *argvars, typval_T *rettv);
591static void f_getcurpos(typval_T *argvars, typval_T *rettv);
592static void f_getpos(typval_T *argvars, typval_T *rettv);
593static void f_getqflist(typval_T *argvars, typval_T *rettv);
594static void f_getreg(typval_T *argvars, typval_T *rettv);
595static void f_getregtype(typval_T *argvars, typval_T *rettv);
596static void f_gettabvar(typval_T *argvars, typval_T *rettv);
597static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
598static void f_getwinposx(typval_T *argvars, typval_T *rettv);
599static void f_getwinposy(typval_T *argvars, typval_T *rettv);
600static void f_getwinvar(typval_T *argvars, typval_T *rettv);
601static void f_glob(typval_T *argvars, typval_T *rettv);
602static void f_globpath(typval_T *argvars, typval_T *rettv);
603static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
604static void f_has(typval_T *argvars, typval_T *rettv);
605static void f_has_key(typval_T *argvars, typval_T *rettv);
606static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
607static void f_hasmapto(typval_T *argvars, typval_T *rettv);
608static void f_histadd(typval_T *argvars, typval_T *rettv);
609static void f_histdel(typval_T *argvars, typval_T *rettv);
610static void f_histget(typval_T *argvars, typval_T *rettv);
611static void f_histnr(typval_T *argvars, typval_T *rettv);
612static void f_hlID(typval_T *argvars, typval_T *rettv);
613static void f_hlexists(typval_T *argvars, typval_T *rettv);
614static void f_hostname(typval_T *argvars, typval_T *rettv);
615static void f_iconv(typval_T *argvars, typval_T *rettv);
616static void f_indent(typval_T *argvars, typval_T *rettv);
617static void f_index(typval_T *argvars, typval_T *rettv);
618static void f_input(typval_T *argvars, typval_T *rettv);
619static void f_inputdialog(typval_T *argvars, typval_T *rettv);
620static void f_inputlist(typval_T *argvars, typval_T *rettv);
621static void f_inputrestore(typval_T *argvars, typval_T *rettv);
622static void f_inputsave(typval_T *argvars, typval_T *rettv);
623static void f_inputsecret(typval_T *argvars, typval_T *rettv);
624static void f_insert(typval_T *argvars, typval_T *rettv);
625static void f_invert(typval_T *argvars, typval_T *rettv);
626static void f_isdirectory(typval_T *argvars, typval_T *rettv);
627static void f_islocked(typval_T *argvars, typval_T *rettv);
628static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100629#ifdef FEAT_JOB
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100630static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100631static void f_job_start(typval_T *argvars, typval_T *rettv);
632static void f_job_stop(typval_T *argvars, typval_T *rettv);
633static void f_job_status(typval_T *argvars, typval_T *rettv);
634#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100635static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100636static void f_js_decode(typval_T *argvars, typval_T *rettv);
637static void f_js_encode(typval_T *argvars, typval_T *rettv);
638static void f_json_decode(typval_T *argvars, typval_T *rettv);
639static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100640static void f_keys(typval_T *argvars, typval_T *rettv);
641static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
642static void f_len(typval_T *argvars, typval_T *rettv);
643static void f_libcall(typval_T *argvars, typval_T *rettv);
644static void f_libcallnr(typval_T *argvars, typval_T *rettv);
645static void f_line(typval_T *argvars, typval_T *rettv);
646static void f_line2byte(typval_T *argvars, typval_T *rettv);
647static void f_lispindent(typval_T *argvars, typval_T *rettv);
648static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000649#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100650static void f_log(typval_T *argvars, typval_T *rettv);
651static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000652#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200653#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100654static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200655#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_map(typval_T *argvars, typval_T *rettv);
657static void f_maparg(typval_T *argvars, typval_T *rettv);
658static void f_mapcheck(typval_T *argvars, typval_T *rettv);
659static void f_match(typval_T *argvars, typval_T *rettv);
660static void f_matchadd(typval_T *argvars, typval_T *rettv);
661static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
662static void f_matcharg(typval_T *argvars, typval_T *rettv);
663static void f_matchdelete(typval_T *argvars, typval_T *rettv);
664static void f_matchend(typval_T *argvars, typval_T *rettv);
665static void f_matchlist(typval_T *argvars, typval_T *rettv);
666static void f_matchstr(typval_T *argvars, typval_T *rettv);
667static void f_max(typval_T *argvars, typval_T *rettv);
668static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000669#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100670static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000671#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100673#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100675#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
677static void f_nr2char(typval_T *argvars, typval_T *rettv);
678static void f_or(typval_T *argvars, typval_T *rettv);
679static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100680#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100682#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000683#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100684static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000685#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
687static void f_printf(typval_T *argvars, typval_T *rettv);
688static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200689#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200691#endif
692#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_range(typval_T *argvars, typval_T *rettv);
696static void f_readfile(typval_T *argvars, typval_T *rettv);
697static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100698#ifdef FEAT_FLOAT
699static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
700#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_reltimestr(typval_T *argvars, typval_T *rettv);
702static void f_remote_expr(typval_T *argvars, typval_T *rettv);
703static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
704static void f_remote_peek(typval_T *argvars, typval_T *rettv);
705static void f_remote_read(typval_T *argvars, typval_T *rettv);
706static void f_remote_send(typval_T *argvars, typval_T *rettv);
707static void f_remove(typval_T *argvars, typval_T *rettv);
708static void f_rename(typval_T *argvars, typval_T *rettv);
709static void f_repeat(typval_T *argvars, typval_T *rettv);
710static void f_resolve(typval_T *argvars, typval_T *rettv);
711static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000712#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000714#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100715static void f_screenattr(typval_T *argvars, typval_T *rettv);
716static void f_screenchar(typval_T *argvars, typval_T *rettv);
717static void f_screencol(typval_T *argvars, typval_T *rettv);
718static void f_screenrow(typval_T *argvars, typval_T *rettv);
719static void f_search(typval_T *argvars, typval_T *rettv);
720static void f_searchdecl(typval_T *argvars, typval_T *rettv);
721static void f_searchpair(typval_T *argvars, typval_T *rettv);
722static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
723static void f_searchpos(typval_T *argvars, typval_T *rettv);
724static void f_server2client(typval_T *argvars, typval_T *rettv);
725static void f_serverlist(typval_T *argvars, typval_T *rettv);
726static void f_setbufvar(typval_T *argvars, typval_T *rettv);
727static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
728static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
729static void f_setline(typval_T *argvars, typval_T *rettv);
730static void f_setloclist(typval_T *argvars, typval_T *rettv);
731static void f_setmatches(typval_T *argvars, typval_T *rettv);
732static void f_setpos(typval_T *argvars, typval_T *rettv);
733static void f_setqflist(typval_T *argvars, typval_T *rettv);
734static void f_setreg(typval_T *argvars, typval_T *rettv);
735static void f_settabvar(typval_T *argvars, typval_T *rettv);
736static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
737static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100738#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100740#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100741static void f_shellescape(typval_T *argvars, typval_T *rettv);
742static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
743static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000744#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100745static void f_sin(typval_T *argvars, typval_T *rettv);
746static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000747#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_sort(typval_T *argvars, typval_T *rettv);
749static void f_soundfold(typval_T *argvars, typval_T *rettv);
750static void f_spellbadword(typval_T *argvars, typval_T *rettv);
751static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
752static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sqrt(typval_T *argvars, typval_T *rettv);
755static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100757static void f_str2nr(typval_T *argvars, typval_T *rettv);
758static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000759#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000761#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_stridx(typval_T *argvars, typval_T *rettv);
763static void f_string(typval_T *argvars, typval_T *rettv);
764static void f_strlen(typval_T *argvars, typval_T *rettv);
765static void f_strpart(typval_T *argvars, typval_T *rettv);
766static void f_strridx(typval_T *argvars, typval_T *rettv);
767static void f_strtrans(typval_T *argvars, typval_T *rettv);
768static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
769static void f_strwidth(typval_T *argvars, typval_T *rettv);
770static void f_submatch(typval_T *argvars, typval_T *rettv);
771static void f_substitute(typval_T *argvars, typval_T *rettv);
772static void f_synID(typval_T *argvars, typval_T *rettv);
773static void f_synIDattr(typval_T *argvars, typval_T *rettv);
774static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
775static void f_synstack(typval_T *argvars, typval_T *rettv);
776static void f_synconcealed(typval_T *argvars, typval_T *rettv);
777static void f_system(typval_T *argvars, typval_T *rettv);
778static void f_systemlist(typval_T *argvars, typval_T *rettv);
779static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
780static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
781static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
782static void f_taglist(typval_T *argvars, typval_T *rettv);
783static void f_tagfiles(typval_T *argvars, typval_T *rettv);
784static void f_tempname(typval_T *argvars, typval_T *rettv);
785static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200786#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_tan(typval_T *argvars, typval_T *rettv);
788static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200789#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100790static void f_tolower(typval_T *argvars, typval_T *rettv);
791static void f_toupper(typval_T *argvars, typval_T *rettv);
792static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000793#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000795#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_type(typval_T *argvars, typval_T *rettv);
797static void f_undofile(typval_T *argvars, typval_T *rettv);
798static void f_undotree(typval_T *argvars, typval_T *rettv);
799static void f_uniq(typval_T *argvars, typval_T *rettv);
800static void f_values(typval_T *argvars, typval_T *rettv);
801static void f_virtcol(typval_T *argvars, typval_T *rettv);
802static void f_visualmode(typval_T *argvars, typval_T *rettv);
803static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
804static void f_winbufnr(typval_T *argvars, typval_T *rettv);
805static void f_wincol(typval_T *argvars, typval_T *rettv);
806static void f_winheight(typval_T *argvars, typval_T *rettv);
807static void f_winline(typval_T *argvars, typval_T *rettv);
808static void f_winnr(typval_T *argvars, typval_T *rettv);
809static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
810static void f_winrestview(typval_T *argvars, typval_T *rettv);
811static void f_winsaveview(typval_T *argvars, typval_T *rettv);
812static void f_winwidth(typval_T *argvars, typval_T *rettv);
813static void f_writefile(typval_T *argvars, typval_T *rettv);
814static void f_wordcount(typval_T *argvars, typval_T *rettv);
815static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000816
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100817static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
818static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
819static int get_env_len(char_u **arg);
820static int get_id_len(char_u **arg);
821static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
822static 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 +0000823#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
824#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
825 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
827static int eval_isnamec(int c);
828static int eval_isnamec1(int c);
829static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
830static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100831static typval_T *alloc_string_tv(char_u *string);
832static void init_tv(typval_T *varp);
833static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100834#ifdef FEAT_FLOAT
835static float_T get_tv_float(typval_T *varp);
836#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static linenr_T get_tv_lnum(typval_T *argvars);
838static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
839static char_u *get_tv_string(typval_T *varp);
840static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
841static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
842static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
843static hashtab_T *find_var_ht(char_u *name, char_u **varname);
844static funccall_T *get_funccal(void);
845static void vars_clear_ext(hashtab_T *ht, int free_val);
846static void delete_var(hashtab_T *ht, hashitem_T *hi);
847static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
848static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
849static void set_var(char_u *name, typval_T *varp, int copy);
850static int var_check_ro(int flags, char_u *name, int use_gettext);
851static int var_check_fixed(int flags, char_u *name, int use_gettext);
852static int var_check_func_name(char_u *name, int new_var);
853static int valid_varname(char_u *varname);
854static int tv_check_lock(int lock, char_u *name, int use_gettext);
855static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
856static char_u *find_option_end(char_u **arg, int *opt_flags);
857static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
858static int eval_fname_script(char_u *p);
859static int eval_fname_sid(char_u *p);
860static void list_func_head(ufunc_T *fp, int indent);
861static ufunc_T *find_func(char_u *name);
862static int function_exists(char_u *name);
863static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000864#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100865static void func_do_profile(ufunc_T *fp);
866static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
867static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000868static int
869# ifdef __BORLANDC__
870 _RTLENTRYF
871# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100872 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000873static int
874# ifdef __BORLANDC__
875 _RTLENTRYF
876# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100877 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000878#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879static int script_autoload(char_u *name, int reload);
880static char_u *autoload_name(char_u *name);
881static void cat_func_name(char_u *buf, ufunc_T *fp);
882static void func_free(ufunc_T *fp);
883static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
884static int can_free_funccal(funccall_T *fc, int copyID) ;
885static void free_funccal(funccall_T *fc, int free_val);
886static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
887static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
888static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
889static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
890static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
891static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
892static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
893static int write_list(FILE *fd, list_T *list, int binary);
894static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000895
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200896
897#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100898static int compare_func_name(const void *s1, const void *s2);
899static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200900#endif
901
Bram Moolenaar33570922005-01-25 22:26:29 +0000902/*
903 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000904 */
905 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100906eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000907{
Bram Moolenaar33570922005-01-25 22:26:29 +0000908 int i;
909 struct vimvar *p;
910
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200911 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
912 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200913 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000914 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000915 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000916
917 for (i = 0; i < VV_LEN; ++i)
918 {
919 p = &vimvars[i];
920 STRCPY(p->vv_di.di_key, p->vv_name);
921 if (p->vv_flags & VV_RO)
922 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
923 else if (p->vv_flags & VV_RO_SBX)
924 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
925 else
926 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000927
928 /* add to v: scope dict, unless the value is not always available */
929 if (p->vv_type != VAR_UNKNOWN)
930 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000931 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000932 /* add to compat scope dict */
933 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000934 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100935 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
936
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000937 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100938 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200939 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100940 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100941
942 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
943 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
944 set_vim_var_nr(VV_NONE, VVAL_NONE);
945 set_vim_var_nr(VV_NULL, VVAL_NULL);
946
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200947 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200948
949#ifdef EBCDIC
950 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100951 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200952 */
953 sortFunctions();
954#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000955}
956
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000957#if defined(EXITFREE) || defined(PROTO)
958 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100959eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000960{
961 int i;
962 struct vimvar *p;
963
964 for (i = 0; i < VV_LEN; ++i)
965 {
966 p = &vimvars[i];
967 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000968 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000969 vim_free(p->vv_str);
970 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000971 }
972 else if (p->vv_di.di_tv.v_type == VAR_LIST)
973 {
974 list_unref(p->vv_list);
975 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000976 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000977 }
978 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000979 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000980 hash_clear(&compat_hashtab);
981
Bram Moolenaard9fba312005-06-26 22:34:35 +0000982 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100983# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200984 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100985# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000986
987 /* global variables */
988 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000989
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000990 /* autoloaded script names */
991 ga_clear_strings(&ga_loaded);
992
Bram Moolenaarcca74132013-09-25 21:00:28 +0200993 /* Script-local variables. First clear all the variables and in a second
994 * loop free the scriptvar_T, because a variable in one script might hold
995 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200996 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200997 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200998 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200999 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001000 ga_clear(&ga_scripts);
1001
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001002 /* unreferenced lists and dicts */
1003 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001004
1005 /* functions */
1006 free_all_functions();
1007 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001008}
1009#endif
1010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * Return the name of the executed function.
1013 */
1014 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001015func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018}
1019
1020/*
1021 * Return the address holding the next breakpoint line for a funccall cookie.
1022 */
1023 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001024func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar33570922005-01-25 22:26:29 +00001026 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/*
1030 * Return the address holding the debug tick for a funccall cookie.
1031 */
1032 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001033func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036}
1037
1038/*
1039 * Return the nesting level for a funccall cookie.
1040 */
1041 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001042func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
Bram Moolenaar33570922005-01-25 22:26:29 +00001044 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045}
1046
1047/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001048funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001050/* pointer to list of previously used funccal, still around because some
1051 * item in it is still being used. */
1052funccall_T *previous_funccal = NULL;
1053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054/*
1055 * Return TRUE when a function was ended by a ":return" command.
1056 */
1057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001058current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059{
1060 return current_funccal->returned;
1061}
1062
1063
1064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 * Set an internal variable to a string value. Creates the variable if it does
1066 * not already exist.
1067 */
1068 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001069set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001071 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001072 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
1074 val = vim_strsave(value);
1075 if (val != NULL)
1076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001077 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001078 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001080 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001081 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 }
1083 }
1084}
1085
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001087static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088static char_u *redir_endp = NULL;
1089static char_u *redir_varname = NULL;
1090
1091/*
1092 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001093 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 * Returns OK if successfully completed the setup. FAIL otherwise.
1095 */
1096 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001097var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098{
1099 int save_emsg;
1100 int err;
1101 typval_T tv;
1102
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001103 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001104 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 {
1106 EMSG(_(e_invarg));
1107 return FAIL;
1108 }
1109
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001110 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 redir_varname = vim_strsave(name);
1112 if (redir_varname == NULL)
1113 return FAIL;
1114
1115 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1116 if (redir_lval == NULL)
1117 {
1118 var_redir_stop();
1119 return FAIL;
1120 }
1121
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122 /* The output is stored in growarray "redir_ga" until redirection ends. */
1123 ga_init2(&redir_ga, (int)sizeof(char), 500);
1124
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001126 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001127 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1129 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001130 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 if (redir_endp != NULL && *redir_endp != NUL)
1132 /* Trailing characters are present after the variable name */
1133 EMSG(_(e_trailing));
1134 else
1135 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001136 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 var_redir_stop();
1138 return FAIL;
1139 }
1140
1141 /* check if we can write to the variable: set it to or append an empty
1142 * string */
1143 save_emsg = did_emsg;
1144 did_emsg = FALSE;
1145 tv.v_type = VAR_STRING;
1146 tv.vval.v_string = (char_u *)"";
1147 if (append)
1148 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1149 else
1150 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001151 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001153 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 if (err)
1155 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 var_redir_stop();
1158 return FAIL;
1159 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160
1161 return OK;
1162}
1163
1164/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001165 * Append "value[value_len]" to the variable set by var_redir_start().
1166 * The actual appending is postponed until redirection ends, because the value
1167 * appended may in fact be the string we write to, changing it may cause freed
1168 * memory to be used:
1169 * :redir => foo
1170 * :let foo
1171 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 */
1173 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001174var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001176 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177
1178 if (redir_lval == NULL)
1179 return;
1180
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001181 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001182 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001184 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001186 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001187 {
1188 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001189 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001190 }
1191 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193}
1194
1195/*
1196 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001197 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 */
1199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001200var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001202 typval_T tv;
1203
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 if (redir_lval != NULL)
1205 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001206 /* If there was no error: assign the text to the variable. */
1207 if (redir_endp != NULL)
1208 {
1209 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1210 tv.v_type = VAR_STRING;
1211 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001212 /* Call get_lval() again, if it's inside a Dict or List it may
1213 * have changed. */
1214 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001215 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001216 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1217 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1218 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001219 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001220
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001221 /* free the collected output */
1222 vim_free(redir_ga.ga_data);
1223 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001224
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001225 vim_free(redir_lval);
1226 redir_lval = NULL;
1227 }
1228 vim_free(redir_varname);
1229 redir_varname = NULL;
1230}
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232# if defined(FEAT_MBYTE) || defined(PROTO)
1233 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001234eval_charconvert(
1235 char_u *enc_from,
1236 char_u *enc_to,
1237 char_u *fname_from,
1238 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239{
1240 int err = FALSE;
1241
1242 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1243 set_vim_var_string(VV_CC_TO, enc_to, -1);
1244 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1245 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1246 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1247 err = TRUE;
1248 set_vim_var_string(VV_CC_FROM, NULL, -1);
1249 set_vim_var_string(VV_CC_TO, NULL, -1);
1250 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252
1253 if (err)
1254 return FAIL;
1255 return OK;
1256}
1257# endif
1258
1259# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1260 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001261eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262{
1263 int err = FALSE;
1264
1265 set_vim_var_string(VV_FNAME_IN, fname, -1);
1266 set_vim_var_string(VV_CMDARG, args, -1);
1267 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1268 err = TRUE;
1269 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1270 set_vim_var_string(VV_CMDARG, NULL, -1);
1271
1272 if (err)
1273 {
1274 mch_remove(fname);
1275 return FAIL;
1276 }
1277 return OK;
1278}
1279# endif
1280
1281# if defined(FEAT_DIFF) || defined(PROTO)
1282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001283eval_diff(
1284 char_u *origfile,
1285 char_u *newfile,
1286 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
1288 int err = FALSE;
1289
1290 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1291 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1292 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1293 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1294 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1295 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1296 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1297}
1298
1299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001300eval_patch(
1301 char_u *origfile,
1302 char_u *difffile,
1303 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304{
1305 int err;
1306
1307 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1308 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1309 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1310 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1311 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1312 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1313 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1314}
1315# endif
1316
1317/*
1318 * Top level evaluation function, returning a boolean.
1319 * Sets "error" to TRUE if there was an error.
1320 * Return TRUE or FALSE.
1321 */
1322 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001323eval_to_bool(
1324 char_u *arg,
1325 int *error,
1326 char_u **nextcmd,
1327 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
Bram Moolenaar33570922005-01-25 22:26:29 +00001329 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 int retval = FALSE;
1331
1332 if (skip)
1333 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 else
1337 {
1338 *error = FALSE;
1339 if (!skip)
1340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001341 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
1344 }
1345 if (skip)
1346 --emsg_skip;
1347
1348 return retval;
1349}
1350
1351/*
1352 * Top level evaluation function, returning a string. If "skip" is TRUE,
1353 * only parsing to "nextcmd" is done, without reporting errors. Return
1354 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1355 */
1356 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001357eval_to_string_skip(
1358 char_u *arg,
1359 char_u **nextcmd,
1360 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
Bram Moolenaar33570922005-01-25 22:26:29 +00001362 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 char_u *retval;
1364
1365 if (skip)
1366 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 retval = NULL;
1369 else
1370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 retval = vim_strsave(get_tv_string(&tv));
1372 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374 if (skip)
1375 --emsg_skip;
1376
1377 return retval;
1378}
1379
1380/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001381 * Skip over an expression at "*pp".
1382 * Return FAIL for an error, OK otherwise.
1383 */
1384 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001385skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001386{
Bram Moolenaar33570922005-01-25 22:26:29 +00001387 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001388
1389 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001391}
1392
1393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001395 * When "convert" is TRUE convert a List into a sequence of lines and convert
1396 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 * Return pointer to allocated memory, or NULL for failure.
1398 */
1399 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001400eval_to_string(
1401 char_u *arg,
1402 char_u **nextcmd,
1403 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
Bram Moolenaar33570922005-01-25 22:26:29 +00001405 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001407 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001408#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001409 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 retval = NULL;
1414 else
1415 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001416 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001417 {
1418 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001419 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001420 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001421 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001422 if (tv.vval.v_list->lv_len > 0)
1423 ga_append(&ga, NL);
1424 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001425 ga_append(&ga, NUL);
1426 retval = (char_u *)ga.ga_data;
1427 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001428#ifdef FEAT_FLOAT
1429 else if (convert && tv.v_type == VAR_FLOAT)
1430 {
1431 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1432 retval = vim_strsave(numbuf);
1433 }
1434#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001435 else
1436 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439
1440 return retval;
1441}
1442
1443/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001444 * Call eval_to_string() without using current local variables and using
1445 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 */
1447 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001448eval_to_string_safe(
1449 char_u *arg,
1450 char_u **nextcmd,
1451 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
1453 char_u *retval;
1454 void *save_funccalp;
1455
1456 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001457 if (use_sandbox)
1458 ++sandbox;
1459 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001460 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001461 if (use_sandbox)
1462 --sandbox;
1463 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 restore_funccal(save_funccalp);
1465 return retval;
1466}
1467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468/*
1469 * Top level evaluation function, returning a number.
1470 * Evaluates "expr" silently.
1471 * Returns -1 for an error.
1472 */
1473 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001474eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
Bram Moolenaar33570922005-01-25 22:26:29 +00001476 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001478 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
1480 ++emsg_off;
1481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 retval = -1;
1484 else
1485 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001486 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 }
1489 --emsg_off;
1490
1491 return retval;
1492}
1493
Bram Moolenaara40058a2005-07-11 22:42:07 +00001494/*
1495 * Prepare v: variable "idx" to be used.
1496 * Save the current typeval in "save_tv".
1497 * When not used yet add the variable to the v: hashtable.
1498 */
1499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001500prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001501{
1502 *save_tv = vimvars[idx].vv_tv;
1503 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1504 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1505}
1506
1507/*
1508 * Restore v: variable "idx" to typeval "save_tv".
1509 * When no longer defined, remove the variable from the v: hashtable.
1510 */
1511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001512restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001513{
1514 hashitem_T *hi;
1515
Bram Moolenaara40058a2005-07-11 22:42:07 +00001516 vimvars[idx].vv_tv = *save_tv;
1517 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1518 {
1519 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1520 if (HASHITEM_EMPTY(hi))
1521 EMSG2(_(e_intern2), "restore_vimvar()");
1522 else
1523 hash_remove(&vimvarht, hi);
1524 }
1525}
1526
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001527#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001528/*
1529 * Evaluate an expression to a list with suggestions.
1530 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001531 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001532 */
1533 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001534eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001535{
1536 typval_T save_val;
1537 typval_T rettv;
1538 list_T *list = NULL;
1539 char_u *p = skipwhite(expr);
1540
1541 /* Set "v:val" to the bad word. */
1542 prepare_vimvar(VV_VAL, &save_val);
1543 vimvars[VV_VAL].vv_type = VAR_STRING;
1544 vimvars[VV_VAL].vv_str = badword;
1545 if (p_verbose == 0)
1546 ++emsg_off;
1547
1548 if (eval1(&p, &rettv, TRUE) == OK)
1549 {
1550 if (rettv.v_type != VAR_LIST)
1551 clear_tv(&rettv);
1552 else
1553 list = rettv.vval.v_list;
1554 }
1555
1556 if (p_verbose == 0)
1557 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001558 restore_vimvar(VV_VAL, &save_val);
1559
1560 return list;
1561}
1562
1563/*
1564 * "list" is supposed to contain two items: a word and a number. Return the
1565 * word in "pp" and the number as the return value.
1566 * Return -1 if anything isn't right.
1567 * Used to get the good word and score from the eval_spell_expr() result.
1568 */
1569 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001570get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001571{
1572 listitem_T *li;
1573
1574 li = list->lv_first;
1575 if (li == NULL)
1576 return -1;
1577 *pp = get_tv_string(&li->li_tv);
1578
1579 li = li->li_next;
1580 if (li == NULL)
1581 return -1;
1582 return get_tv_number(&li->li_tv);
1583}
1584#endif
1585
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001586/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001587 * Top level evaluation function.
1588 * Returns an allocated typval_T with the result.
1589 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001590 */
1591 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001592eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001593{
1594 typval_T *tv;
1595
1596 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001597 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001598 {
1599 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001600 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001601 }
1602
1603 return tv;
1604}
1605
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001608 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609 * Uses argv[argc] for the function arguments. Only Number and String
1610 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001614call_vim_function(
1615 char_u *func,
1616 int argc,
1617 char_u **argv,
1618 int safe, /* use the sandbox */
1619 int str_arg_only, /* all arguments are strings */
1620 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
Bram Moolenaar33570922005-01-25 22:26:29 +00001622 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 long n;
1624 int len;
1625 int i;
1626 int doesrange;
1627 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001628 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001630 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
1634 for (i = 0; i < argc; i++)
1635 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001636 /* Pass a NULL or empty argument as an empty string */
1637 if (argv[i] == NULL || *argv[i] == NUL)
1638 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001639 argvars[i].v_type = VAR_STRING;
1640 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001641 continue;
1642 }
1643
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001644 if (str_arg_only)
1645 len = 0;
1646 else
1647 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001648 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 if (len != 0 && len == (int)STRLEN(argv[i]))
1650 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001651 argvars[i].v_type = VAR_NUMBER;
1652 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
1654 else
1655 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001656 argvars[i].v_type = VAR_STRING;
1657 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
1659 }
1660
1661 if (safe)
1662 {
1663 save_funccalp = save_funccal();
1664 ++sandbox;
1665 }
1666
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1668 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001670 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (safe)
1672 {
1673 --sandbox;
1674 restore_funccal(save_funccalp);
1675 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 vim_free(argvars);
1677
1678 if (ret == FAIL)
1679 clear_tv(rettv);
1680
1681 return ret;
1682}
1683
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001684/*
1685 * Call vimL function "func" and return the result as a number.
1686 * Returns -1 when calling the function fails.
1687 * Uses argv[argc] for the function arguments.
1688 */
1689 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001690call_func_retnr(
1691 char_u *func,
1692 int argc,
1693 char_u **argv,
1694 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001695{
1696 typval_T rettv;
1697 long retval;
1698
1699 /* All arguments are passed as strings, no conversion to number. */
1700 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1701 return -1;
1702
1703 retval = get_tv_number_chk(&rettv, NULL);
1704 clear_tv(&rettv);
1705 return retval;
1706}
1707
1708#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1709 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1710
Bram Moolenaar4f688582007-07-24 12:34:30 +00001711# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001713 * Call vimL function "func" and return the result as a string.
1714 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715 * Uses argv[argc] for the function arguments.
1716 */
1717 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001718call_func_retstr(
1719 char_u *func,
1720 int argc,
1721 char_u **argv,
1722 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723{
1724 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001725 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001726
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001727 /* All arguments are passed as strings, no conversion to number. */
1728 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001729 return NULL;
1730
1731 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 return retval;
1734}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001735# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001737/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001738 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001740 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 */
1742 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001743call_func_retlist(
1744 char_u *func,
1745 int argc,
1746 char_u **argv,
1747 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748{
1749 typval_T rettv;
1750
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001751 /* All arguments are passed as strings, no conversion to number. */
1752 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001753 return NULL;
1754
1755 if (rettv.v_type != VAR_LIST)
1756 {
1757 clear_tv(&rettv);
1758 return NULL;
1759 }
1760
1761 return rettv.vval.v_list;
1762}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#endif
1764
1765/*
1766 * Save the current function call pointer, and set it to NULL.
1767 * Used when executing autocommands and for ":source".
1768 */
1769 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001770save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001772 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 current_funccal = NULL;
1775 return (void *)fc;
1776}
1777
1778 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001779restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001781 funccall_T *fc = (funccall_T *)vfc;
1782
1783 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784}
1785
Bram Moolenaar05159a02005-02-26 23:04:13 +00001786#if defined(FEAT_PROFILE) || defined(PROTO)
1787/*
1788 * Prepare profiling for entering a child or something else that is not
1789 * counted for the script/function itself.
1790 * Should always be called in pair with prof_child_exit().
1791 */
1792 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001793prof_child_enter(
1794 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001795{
1796 funccall_T *fc = current_funccal;
1797
1798 if (fc != NULL && fc->func->uf_profiling)
1799 profile_start(&fc->prof_child);
1800 script_prof_save(tm);
1801}
1802
1803/*
1804 * Take care of time spent in a child.
1805 * Should always be called after prof_child_enter().
1806 */
1807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001808prof_child_exit(
1809 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001810{
1811 funccall_T *fc = current_funccal;
1812
1813 if (fc != NULL && fc->func->uf_profiling)
1814 {
1815 profile_end(&fc->prof_child);
1816 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1817 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1818 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1819 }
1820 script_prof_restore(tm);
1821}
1822#endif
1823
1824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825#ifdef FEAT_FOLDING
1826/*
1827 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1828 * it in "*cp". Doesn't give error messages.
1829 */
1830 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001831eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832{
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int retval;
1835 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001836 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1837 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001840 if (use_sandbox)
1841 ++sandbox;
1842 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 retval = 0;
1846 else
1847 {
1848 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 if (tv.v_type == VAR_NUMBER)
1850 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001851 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 retval = 0;
1853 else
1854 {
1855 /* If the result is a string, check if there is a non-digit before
1856 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 if (!VIM_ISDIGIT(*s) && *s != '-')
1859 *cp = *s++;
1860 retval = atol((char *)s);
1861 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 }
1864 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001865 if (use_sandbox)
1866 --sandbox;
1867 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 return retval;
1870}
1871#endif
1872
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 * ":let" list all variable values
1875 * ":let var1 var2" list variable values
1876 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 * ":let var += expr" assignment command.
1878 * ":let var -= expr" assignment command.
1879 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 */
1882 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001883ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884{
1885 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001887 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 int var_count = 0;
1890 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001892 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001893 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894
Bram Moolenaardb552d602006-03-23 22:59:57 +00001895 argend = skip_var_list(arg, &var_count, &semicolon);
1896 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001898 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1899 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001900 expr = skipwhite(argend);
1901 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1902 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001904 /*
1905 * ":let" without "=": list variables
1906 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 if (*arg == '[')
1908 EMSG(_(e_invarg));
1909 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001911 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001915 list_glob_vars(&first);
1916 list_buf_vars(&first);
1917 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001918#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001919 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001920#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001921 list_script_vars(&first);
1922 list_func_vars(&first);
1923 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 eap->nextcmd = check_nextcmd(arg);
1926 }
1927 else
1928 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 op[0] = '=';
1930 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001931 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001933 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1934 op[0] = *expr; /* +=, -= or .= */
1935 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001937 else
1938 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 if (eap->skip)
1941 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (eap->skip)
1944 {
1945 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 --emsg_skip;
1948 }
1949 else if (i != FAIL)
1950 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 }
1955 }
1956}
1957
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958/*
1959 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1960 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 * When "nextchars" is not NULL it points to a string with characters that
1962 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1963 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 * Returns OK or FAIL;
1965 */
1966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001967ex_let_vars(
1968 char_u *arg_start,
1969 typval_T *tv,
1970 int copy, /* copy values from "tv", don't move */
1971 int semicolon, /* from skip_var_list() */
1972 int var_count, /* from skip_var_list() */
1973 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974{
1975 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001976 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001978 listitem_T *item;
1979 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980
1981 if (*arg != '[')
1982 {
1983 /*
1984 * ":let var = expr" or ":for var in list"
1985 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 return FAIL;
1988 return OK;
1989 }
1990
1991 /*
1992 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1993 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001994 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 {
1996 EMSG(_(e_listreq));
1997 return FAIL;
1998 }
1999
2000 i = list_len(l);
2001 if (semicolon == 0 && var_count < i)
2002 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002003 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 return FAIL;
2005 }
2006 if (var_count - semicolon > i)
2007 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002008 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 return FAIL;
2010 }
2011
2012 item = l->lv_first;
2013 while (*arg != ']')
2014 {
2015 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002016 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017 item = item->li_next;
2018 if (arg == NULL)
2019 return FAIL;
2020
2021 arg = skipwhite(arg);
2022 if (*arg == ';')
2023 {
2024 /* Put the rest of the list (may be empty) in the var after ';'.
2025 * Create a new list for this. */
2026 l = list_alloc();
2027 if (l == NULL)
2028 return FAIL;
2029 while (item != NULL)
2030 {
2031 list_append_tv(l, &item->li_tv);
2032 item = item->li_next;
2033 }
2034
2035 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002036 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037 ltv.vval.v_list = l;
2038 l->lv_refcount = 1;
2039
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002040 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2041 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002042 clear_tv(&ltv);
2043 if (arg == NULL)
2044 return FAIL;
2045 break;
2046 }
2047 else if (*arg != ',' && *arg != ']')
2048 {
2049 EMSG2(_(e_intern2), "ex_let_vars()");
2050 return FAIL;
2051 }
2052 }
2053
2054 return OK;
2055}
2056
2057/*
2058 * Skip over assignable variable "var" or list of variables "[var, var]".
2059 * Used for ":let varvar = expr" and ":for varvar in expr".
2060 * For "[var, var]" increment "*var_count" for each variable.
2061 * for "[var, var; var]" set "semicolon".
2062 * Return NULL for an error.
2063 */
2064 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002065skip_var_list(
2066 char_u *arg,
2067 int *var_count,
2068 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002069{
2070 char_u *p, *s;
2071
2072 if (*arg == '[')
2073 {
2074 /* "[var, var]": find the matching ']'. */
2075 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002076 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077 {
2078 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2079 s = skip_var_one(p);
2080 if (s == p)
2081 {
2082 EMSG2(_(e_invarg2), p);
2083 return NULL;
2084 }
2085 ++*var_count;
2086
2087 p = skipwhite(s);
2088 if (*p == ']')
2089 break;
2090 else if (*p == ';')
2091 {
2092 if (*semicolon == 1)
2093 {
2094 EMSG(_("Double ; in list of variables"));
2095 return NULL;
2096 }
2097 *semicolon = 1;
2098 }
2099 else if (*p != ',')
2100 {
2101 EMSG2(_(e_invarg2), p);
2102 return NULL;
2103 }
2104 }
2105 return p + 1;
2106 }
2107 else
2108 return skip_var_one(arg);
2109}
2110
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002111/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002112 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002113 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002114 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002115 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002116skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002117{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002118 if (*arg == '@' && arg[1] != NUL)
2119 return arg + 2;
2120 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2121 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002122}
2123
Bram Moolenaara7043832005-01-21 11:56:39 +00002124/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 * List variables for hashtab "ht" with prefix "prefix".
2126 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002127 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002129list_hashtable_vars(
2130 hashtab_T *ht,
2131 char_u *prefix,
2132 int empty,
2133 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar33570922005-01-25 22:26:29 +00002135 hashitem_T *hi;
2136 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002137 int todo;
2138
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002139 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002140 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2141 {
2142 if (!HASHITEM_EMPTY(hi))
2143 {
2144 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002145 di = HI2DI(hi);
2146 if (empty || di->di_tv.v_type != VAR_STRING
2147 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 }
2150 }
2151}
2152
2153/*
2154 * List global variables.
2155 */
2156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002157list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002160}
2161
2162/*
2163 * List buffer variables.
2164 */
2165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002166list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002167{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002168 char_u numbuf[NUMBUFLEN];
2169
Bram Moolenaar429fa852013-04-15 12:27:36 +02002170 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002172
2173 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2175 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002176}
2177
2178/*
2179 * List window variables.
2180 */
2181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002182list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002183{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002184 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002186}
2187
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002188#ifdef FEAT_WINDOWS
2189/*
2190 * List tab page variables.
2191 */
2192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002193list_tab_vars(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 Moolenaar7454a062016-01-30 15:14:10 +01002204list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207}
2208
2209/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002210 * List script-local variables, if there is a script.
2211 */
2212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002213list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002214{
2215 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2217 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002218}
2219
2220/*
2221 * List function variables, if there is a function.
2222 */
2223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002224list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002225{
2226 if (current_funccal != NULL)
2227 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002228 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002229}
2230
2231/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 * List variables in "arg".
2233 */
2234 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002235list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236{
2237 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 char_u *name_start;
2241 char_u *arg_subsc;
2242 char_u *tofree;
2243 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244
2245 while (!ends_excmd(*arg) && !got_int)
2246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002249 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2251 {
2252 emsg_severe = TRUE;
2253 EMSG(_(e_trailing));
2254 break;
2255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 /* get_name_len() takes care of expanding curly braces */
2260 name_start = name = arg;
2261 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2262 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* This is mainly to keep test 49 working: when expanding
2265 * curly braces fails overrule the exception error message. */
2266 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 emsg_severe = TRUE;
2269 EMSG2(_(e_invarg2), arg);
2270 break;
2271 }
2272 error = TRUE;
2273 }
2274 else
2275 {
2276 if (tofree != NULL)
2277 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002278 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 else
2281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 /* handle d.key, l[idx], f(expr) */
2283 arg_subsc = arg;
2284 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002292 case 'g': list_glob_vars(first); break;
2293 case 'b': list_buf_vars(first); break;
2294 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002295#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002296 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002297#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002298 case 'v': list_vim_vars(first); break;
2299 case 's': list_script_vars(first); break;
2300 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 default:
2302 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002303 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002304 }
2305 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002306 {
2307 char_u numbuf[NUMBUFLEN];
2308 char_u *tf;
2309 int c;
2310 char_u *s;
2311
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002312 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 c = *arg;
2314 *arg = NUL;
2315 list_one_var_a((char_u *)"",
2316 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002317 tv.v_type,
2318 s == NULL ? (char_u *)"" : s,
2319 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320 *arg = c;
2321 vim_free(tf);
2322 }
2323 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002324 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 }
2326 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002327
2328 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002330
2331 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 }
2333
2334 return arg;
2335}
2336
2337/*
2338 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2339 * Returns a pointer to the char just after the var name.
2340 * Returns NULL if there is an error.
2341 */
2342 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002343ex_let_one(
2344 char_u *arg, /* points to variable name */
2345 typval_T *tv, /* value to assign to variable */
2346 int copy, /* copy value from "tv" */
2347 char_u *endchars, /* valid chars after variable name or NULL */
2348 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349{
2350 int c1;
2351 char_u *name;
2352 char_u *p;
2353 char_u *arg_end = NULL;
2354 int len;
2355 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357
2358 /*
2359 * ":let $VAR = expr": Set environment variable.
2360 */
2361 if (*arg == '$')
2362 {
2363 /* Find the end of the name. */
2364 ++arg;
2365 name = arg;
2366 len = get_env_len(&arg);
2367 if (len == 0)
2368 EMSG2(_(e_invarg2), name - 1);
2369 else
2370 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371 if (op != NULL && (*op == '+' || *op == '-'))
2372 EMSG2(_(e_letwrong), op);
2373 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002374 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002376 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 {
2378 c1 = name[len];
2379 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002380 p = get_tv_string_chk(tv);
2381 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 {
2383 int mustfree = FALSE;
2384 char_u *s = vim_getenv(name, &mustfree);
2385
2386 if (s != NULL)
2387 {
2388 p = tofree = concat_str(s, p);
2389 if (mustfree)
2390 vim_free(s);
2391 }
2392 }
2393 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002396 if (STRICMP(name, "HOME") == 0)
2397 init_homedir();
2398 else if (didset_vim && STRICMP(name, "VIM") == 0)
2399 didset_vim = FALSE;
2400 else if (didset_vimruntime
2401 && STRICMP(name, "VIMRUNTIME") == 0)
2402 didset_vimruntime = FALSE;
2403 arg_end = arg;
2404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002405 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 }
2408 }
2409 }
2410
2411 /*
2412 * ":let &option = expr": Set option value.
2413 * ":let &l:option = expr": Set local option value.
2414 * ":let &g:option = expr": Set global option value.
2415 */
2416 else if (*arg == '&')
2417 {
2418 /* Find the end of the name. */
2419 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002420 if (p == NULL || (endchars != NULL
2421 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 EMSG(_(e_letunexp));
2423 else
2424 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002425 long n;
2426 int opt_type;
2427 long numval;
2428 char_u *stringval = NULL;
2429 char_u *s;
2430
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 c1 = *p;
2432 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433
2434 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002435 s = get_tv_string_chk(tv); /* != NULL if number or string */
2436 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 {
2438 opt_type = get_option_value(arg, &numval,
2439 &stringval, opt_flags);
2440 if ((opt_type == 1 && *op == '.')
2441 || (opt_type == 0 && *op != '.'))
2442 EMSG2(_(e_letwrong), op);
2443 else
2444 {
2445 if (opt_type == 1) /* number */
2446 {
2447 if (*op == '+')
2448 n = numval + n;
2449 else
2450 n = numval - n;
2451 }
2452 else if (opt_type == 0 && stringval != NULL) /* string */
2453 {
2454 s = concat_str(stringval, s);
2455 vim_free(stringval);
2456 stringval = s;
2457 }
2458 }
2459 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002460 if (s != NULL)
2461 {
2462 set_option_value(arg, n, s, opt_flags);
2463 arg_end = p;
2464 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 }
2468 }
2469
2470 /*
2471 * ":let @r = expr": Set register contents.
2472 */
2473 else if (*arg == '@')
2474 {
2475 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 if (op != NULL && (*op == '+' || *op == '-'))
2477 EMSG2(_(e_letwrong), op);
2478 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002479 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 EMSG(_(e_letunexp));
2481 else
2482 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 char_u *s;
2485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002486 p = get_tv_string_chk(tv);
2487 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002489 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 if (s != NULL)
2491 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002492 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 vim_free(s);
2494 }
2495 }
2496 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002497 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002499 arg_end = arg + 1;
2500 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002501 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 }
2503 }
2504
2505 /*
2506 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002509 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002511 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002513 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2517 EMSG(_(e_letunexp));
2518 else
2519 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002520 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 arg_end = p;
2522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 }
2526
2527 else
2528 EMSG2(_(e_invarg2), arg);
2529
2530 return arg_end;
2531}
2532
2533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2535 */
2536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002537check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538{
2539 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2540 {
2541 EMSG2(_(e_readonlyvar), arg);
2542 return TRUE;
2543 }
2544 return FALSE;
2545}
2546
2547/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 * Get an lval: variable, Dict item or List item that can be assigned a value
2549 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2550 * "name.key", "name.key[expr]" etc.
2551 * Indexing only works if "name" is an existing List or Dictionary.
2552 * "name" points to the start of the name.
2553 * If "rettv" is not NULL it points to the value to be assigned.
2554 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2555 * wrong; must end in space or cmd separator.
2556 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002557 * flags:
2558 * GLV_QUIET: do not give error messages
2559 * GLV_NO_AUTOLOAD: do not use script autoloading
2560 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002562 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 */
2565 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002566get_lval(
2567 char_u *name,
2568 typval_T *rettv,
2569 lval_T *lp,
2570 int unlet,
2571 int skip,
2572 int flags, /* GLV_ values */
2573 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002575 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 char_u *expr_start, *expr_end;
2577 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002578 dictitem_T *v;
2579 typval_T var1;
2580 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002582 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002586 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590
2591 if (skip)
2592 {
2593 /* When skipping just find the end of the name. */
2594 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002595 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 }
2597
2598 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002599 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (expr_start != NULL)
2601 {
2602 /* Don't expand the name when we already know there is an error. */
2603 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2604 && *p != '[' && *p != '.')
2605 {
2606 EMSG(_(e_trailing));
2607 return NULL;
2608 }
2609
2610 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2611 if (lp->ll_exp_name == NULL)
2612 {
2613 /* Report an invalid expression in braces, unless the
2614 * expression evaluation has been cancelled due to an
2615 * aborting error, an interrupt, or an exception. */
2616 if (!aborting() && !quiet)
2617 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002618 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 EMSG2(_(e_invarg2), name);
2620 return NULL;
2621 }
2622 }
2623 lp->ll_name = lp->ll_exp_name;
2624 }
2625 else
2626 lp->ll_name = name;
2627
2628 /* Without [idx] or .key we are done. */
2629 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2630 return p;
2631
2632 cc = *p;
2633 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002634 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (v == NULL && !quiet)
2636 EMSG2(_(e_undefvar), lp->ll_name);
2637 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 if (v == NULL)
2639 return NULL;
2640
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 /*
2642 * Loop until no more [idx] or .key is following.
2643 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002644 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2648 && !(lp->ll_tv->v_type == VAR_DICT
2649 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (!quiet)
2652 EMSG(_("E689: Can only index a List or Dictionary"));
2653 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!quiet)
2658 EMSG(_("E708: [:] must come last"));
2659 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 len = -1;
2663 if (*p == '.')
2664 {
2665 key = p + 1;
2666 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2667 ;
2668 if (len == 0)
2669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (!quiet)
2671 EMSG(_(e_emptykey));
2672 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 }
2674 p = key + len;
2675 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676 else
2677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 if (*p == ':')
2681 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002682 else
2683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 empty1 = FALSE;
2685 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002687 if (get_tv_string_chk(&var1) == NULL)
2688 {
2689 /* not a number or string */
2690 clear_tv(&var1);
2691 return NULL;
2692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
2694
2695 /* Optionally get the second index [ :expr]. */
2696 if (*p == ':')
2697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002701 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002702 if (!empty1)
2703 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (rettv != NULL && (rettv->v_type != VAR_LIST
2707 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (!quiet)
2710 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 if (!empty1)
2712 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 }
2715 p = skipwhite(p + 1);
2716 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 else
2719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (!empty1)
2724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002727 if (get_tv_string_chk(&var2) == NULL)
2728 {
2729 /* not a number or string */
2730 if (!empty1)
2731 clear_tv(&var1);
2732 clear_tv(&var2);
2733 return NULL;
2734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002740
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (!quiet)
2744 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (!empty1)
2746 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751
2752 /* Skip to past ']'. */
2753 ++p;
2754 }
2755
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 {
2758 if (len == -1)
2759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002761 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 if (*key == NUL)
2763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (!quiet)
2765 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 }
2769 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002770 lp->ll_list = NULL;
2771 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002772 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 /* When assigning to a scope dictionary check that a function and
2775 * variable name is valid (only variable name unless it is l: or
2776 * g: dictionary). Disallow overwriting a builtin function. */
2777 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002778 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002779 int prevval;
2780 int wrong;
2781
2782 if (len != -1)
2783 {
2784 prevval = key[len];
2785 key[len] = NUL;
2786 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002787 else
2788 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002789 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2790 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002791 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002792 || !valid_varname(key);
2793 if (len != -1)
2794 key[len] = prevval;
2795 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002796 return NULL;
2797 }
2798
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002801 /* Can't add "v:" variable. */
2802 if (lp->ll_dict == &vimvardict)
2803 {
2804 EMSG2(_(e_illvar), name);
2805 return NULL;
2806 }
2807
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002808 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 if (len == -1)
2814 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 }
2817 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 if (len == -1)
2822 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 p = NULL;
2825 break;
2826 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002827 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002828 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002829 return NULL;
2830
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 if (len == -1)
2832 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 }
2835 else
2836 {
2837 /*
2838 * Get the number and item for the only or first index of the List.
2839 */
2840 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 else
2843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002844 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 clear_tv(&var1);
2846 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002847 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_list = lp->ll_tv->vval.v_list;
2849 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2850 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002852 if (lp->ll_n1 < 0)
2853 {
2854 lp->ll_n1 = 0;
2855 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2856 }
2857 }
2858 if (lp->ll_li == NULL)
2859 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002862 if (!quiet)
2863 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 }
2866
2867 /*
2868 * May need to find the item or absolute index for the second
2869 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 * When no index given: "lp->ll_empty2" is TRUE.
2871 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002875 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002881 {
2882 if (!quiet)
2883 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002885 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 }
2888
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2890 if (lp->ll_n1 < 0)
2891 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2892 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002893 {
2894 if (!quiet)
2895 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002897 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002898 }
2899
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002902 }
2903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 return p;
2905}
2906
2907/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 */
2910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002911clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912{
2913 vim_free(lp->ll_exp_name);
2914 vim_free(lp->ll_newkey);
2915}
2916
2917/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 */
2922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002923set_var_lval(
2924 lval_T *lp,
2925 char_u *endp,
2926 typval_T *rettv,
2927 int copy,
2928 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929{
2930 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002931 listitem_T *ri;
2932 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933
2934 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 cc = *endp;
2939 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 if (op != NULL && *op != '=')
2941 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002942 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002944 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002945 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002946 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002947 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002949 if ((di == NULL
2950 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2951 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2952 FALSE)))
2953 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 set_var(lp->ll_name, &tv, FALSE);
2955 clear_tv(&tv);
2956 }
2957 }
2958 else
2959 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002963 else if (tv_check_lock(lp->ll_newkey == NULL
2964 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002965 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 else if (lp->ll_range)
2968 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002969 listitem_T *ll_li = lp->ll_li;
2970 int ll_n1 = lp->ll_n1;
2971
2972 /*
2973 * Check whether any of the list items is locked
2974 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002975 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002976 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002977 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 return;
2979 ri = ri->li_next;
2980 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2981 break;
2982 ll_li = ll_li->li_next;
2983 ++ll_n1;
2984 }
2985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 /*
2987 * Assign the List values to the list items.
2988 */
2989 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002990 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002991 if (op != NULL && *op != '=')
2992 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2993 else
2994 {
2995 clear_tv(&lp->ll_li->li_tv);
2996 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2997 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 ri = ri->li_next;
2999 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3000 break;
3001 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003004 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 ri = NULL;
3007 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 lp->ll_li = lp->ll_li->li_next;
3011 ++lp->ll_n1;
3012 }
3013 if (ri != NULL)
3014 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 else if (lp->ll_empty2
3016 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 : lp->ll_n1 != lp->ll_n2)
3018 EMSG(_("E711: List value has not enough items"));
3019 }
3020 else
3021 {
3022 /*
3023 * Assign to a List or Dictionary item.
3024 */
3025 if (lp->ll_newkey != NULL)
3026 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 if (op != NULL && *op != '=')
3028 {
3029 EMSG2(_(e_letwrong), op);
3030 return;
3031 }
3032
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003033 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003034 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 if (di == NULL)
3036 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003037 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3038 {
3039 vim_free(di);
3040 return;
3041 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 else if (op != NULL && *op != '=')
3045 {
3046 tv_op(lp->ll_tv, rettv, op);
3047 return;
3048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003051
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 /*
3053 * Assign the value to the variable or list item.
3054 */
3055 if (copy)
3056 copy_tv(rettv, lp->ll_tv);
3057 else
3058 {
3059 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003060 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003061 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062 }
3063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064}
3065
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3068 * Returns OK or FAIL.
3069 */
3070 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003071tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072{
3073 long n;
3074 char_u numbuf[NUMBUFLEN];
3075 char_u *s;
3076
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003077 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3078 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3079 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 {
3081 switch (tv1->v_type)
3082 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003083 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 case VAR_DICT:
3085 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003086 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003087 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003088 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 break;
3090
3091 case VAR_LIST:
3092 if (*op != '+' || tv2->v_type != VAR_LIST)
3093 break;
3094 /* List += List */
3095 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3096 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3097 return OK;
3098
3099 case VAR_NUMBER:
3100 case VAR_STRING:
3101 if (tv2->v_type == VAR_LIST)
3102 break;
3103 if (*op == '+' || *op == '-')
3104 {
3105 /* nr += nr or nr -= nr*/
3106 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003107#ifdef FEAT_FLOAT
3108 if (tv2->v_type == VAR_FLOAT)
3109 {
3110 float_T f = n;
3111
3112 if (*op == '+')
3113 f += tv2->vval.v_float;
3114 else
3115 f -= tv2->vval.v_float;
3116 clear_tv(tv1);
3117 tv1->v_type = VAR_FLOAT;
3118 tv1->vval.v_float = f;
3119 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003120 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003121#endif
3122 {
3123 if (*op == '+')
3124 n += get_tv_number(tv2);
3125 else
3126 n -= get_tv_number(tv2);
3127 clear_tv(tv1);
3128 tv1->v_type = VAR_NUMBER;
3129 tv1->vval.v_number = n;
3130 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 }
3132 else
3133 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134 if (tv2->v_type == VAR_FLOAT)
3135 break;
3136
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 /* str .= str */
3138 s = get_tv_string(tv1);
3139 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3140 clear_tv(tv1);
3141 tv1->v_type = VAR_STRING;
3142 tv1->vval.v_string = s;
3143 }
3144 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003145
3146#ifdef FEAT_FLOAT
3147 case VAR_FLOAT:
3148 {
3149 float_T f;
3150
3151 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3152 && tv2->v_type != VAR_NUMBER
3153 && tv2->v_type != VAR_STRING))
3154 break;
3155 if (tv2->v_type == VAR_FLOAT)
3156 f = tv2->vval.v_float;
3157 else
3158 f = get_tv_number(tv2);
3159 if (*op == '+')
3160 tv1->vval.v_float += f;
3161 else
3162 tv1->vval.v_float -= f;
3163 }
3164 return OK;
3165#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003166 }
3167 }
3168
3169 EMSG2(_(e_letwrong), op);
3170 return FAIL;
3171}
3172
3173/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 * Add a watcher to a list.
3175 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003177list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178{
3179 lw->lw_next = l->lv_watch;
3180 l->lv_watch = lw;
3181}
3182
3183/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003184 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185 * No warning when it isn't found...
3186 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003188list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189{
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191
3192 lwp = &l->lv_watch;
3193 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3194 {
3195 if (lw == lwrem)
3196 {
3197 *lwp = lw->lw_next;
3198 break;
3199 }
3200 lwp = &lw->lw_next;
3201 }
3202}
3203
3204/*
3205 * Just before removing an item from a list: advance watchers to the next
3206 * item.
3207 */
3208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003209list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003210{
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212
3213 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3214 if (lw->lw_item == item)
3215 lw->lw_item = item->li_next;
3216}
3217
3218/*
3219 * Evaluate the expression used in a ":for var in expr" command.
3220 * "arg" points to "var".
3221 * Set "*errp" to TRUE for an error, FALSE otherwise;
3222 * Return a pointer that holds the info. Null when there is an error.
3223 */
3224 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003225eval_for_line(
3226 char_u *arg,
3227 int *errp,
3228 char_u **nextcmdp,
3229 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230{
Bram Moolenaar33570922005-01-25 22:26:29 +00003231 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 typval_T tv;
3234 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 *errp = TRUE; /* default: there is an error */
3237
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 if (fi == NULL)
3240 return NULL;
3241
3242 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3243 if (expr == NULL)
3244 return fi;
3245
3246 expr = skipwhite(expr);
3247 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3248 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003249 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250 return fi;
3251 }
3252
3253 if (skip)
3254 ++emsg_skip;
3255 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3256 {
3257 *errp = FALSE;
3258 if (!skip)
3259 {
3260 l = tv.vval.v_list;
3261 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003262 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 clear_tv(&tv);
3265 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 else
3267 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003268 /* No need to increment the refcount, it's already set for the
3269 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 fi->fi_list = l;
3271 list_add_watch(l, &fi->fi_lw);
3272 fi->fi_lw.lw_item = l->lv_first;
3273 }
3274 }
3275 }
3276 if (skip)
3277 --emsg_skip;
3278
3279 return fi;
3280}
3281
3282/*
3283 * Use the first item in a ":for" list. Advance to the next.
3284 * Assign the values to the variable (list). "arg" points to the first one.
3285 * Return TRUE when a valid item was found, FALSE when at end of list or
3286 * something wrong.
3287 */
3288 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003289next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003291 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003293 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294
3295 item = fi->fi_lw.lw_item;
3296 if (item == NULL)
3297 result = FALSE;
3298 else
3299 {
3300 fi->fi_lw.lw_item = item->li_next;
3301 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3302 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3303 }
3304 return result;
3305}
3306
3307/*
3308 * Free the structure used to store info used by ":for".
3309 */
3310 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003311free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003312{
Bram Moolenaar33570922005-01-25 22:26:29 +00003313 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003315 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003316 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003318 list_unref(fi->fi_list);
3319 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320 vim_free(fi);
3321}
3322
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3324
3325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003326set_context_for_expression(
3327 expand_T *xp,
3328 char_u *arg,
3329 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330{
3331 int got_eq = FALSE;
3332 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003335 if (cmdidx == CMD_let)
3336 {
3337 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003338 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 {
3340 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003341 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 {
3343 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003344 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 if (vim_iswhite(*p))
3346 break;
3347 }
3348 return;
3349 }
3350 }
3351 else
3352 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3353 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 while ((xp->xp_pattern = vim_strpbrk(arg,
3355 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3356 {
3357 c = *xp->xp_pattern;
3358 if (c == '&')
3359 {
3360 c = xp->xp_pattern[1];
3361 if (c == '&')
3362 {
3363 ++xp->xp_pattern;
3364 xp->xp_context = cmdidx != CMD_let || got_eq
3365 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3366 }
3367 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003370 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3371 xp->xp_pattern += 2;
3372
3373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375 else if (c == '$')
3376 {
3377 /* environment variable */
3378 xp->xp_context = EXPAND_ENV_VARS;
3379 }
3380 else if (c == '=')
3381 {
3382 got_eq = TRUE;
3383 xp->xp_context = EXPAND_EXPRESSION;
3384 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003385 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 && xp->xp_context == EXPAND_FUNCTIONS
3387 && vim_strchr(xp->xp_pattern, '(') == NULL)
3388 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003389 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 break;
3391 }
3392 else if (cmdidx != CMD_let || got_eq)
3393 {
3394 if (c == '"') /* string */
3395 {
3396 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3397 if (c == '\\' && xp->xp_pattern[1] != NUL)
3398 ++xp->xp_pattern;
3399 xp->xp_context = EXPAND_NOTHING;
3400 }
3401 else if (c == '\'') /* literal string */
3402 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003403 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3405 /* skip */ ;
3406 xp->xp_context = EXPAND_NOTHING;
3407 }
3408 else if (c == '|')
3409 {
3410 if (xp->xp_pattern[1] == '|')
3411 {
3412 ++xp->xp_pattern;
3413 xp->xp_context = EXPAND_EXPRESSION;
3414 }
3415 else
3416 xp->xp_context = EXPAND_COMMANDS;
3417 }
3418 else
3419 xp->xp_context = EXPAND_EXPRESSION;
3420 }
3421 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003422 /* Doesn't look like something valid, expand as an expression
3423 * anyway. */
3424 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 arg = xp->xp_pattern;
3426 if (*arg != NUL)
3427 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3428 /* skip */ ;
3429 }
3430 xp->xp_pattern = arg;
3431}
3432
3433#endif /* FEAT_CMDL_COMPL */
3434
3435/*
3436 * ":1,25call func(arg1, arg2)" function call.
3437 */
3438 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003439ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
3441 char_u *arg = eap->arg;
3442 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003444 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003446 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 linenr_T lnum;
3448 int doesrange;
3449 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003452 if (eap->skip)
3453 {
3454 /* trans_function_name() doesn't work well when skipping, use eval0()
3455 * instead to skip to any following command, e.g. for:
3456 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003457 ++emsg_skip;
3458 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3459 clear_tv(&rettv);
3460 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003461 return;
3462 }
3463
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003464 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003465 if (fudi.fd_newkey != NULL)
3466 {
3467 /* Still need to give an error message for missing key. */
3468 EMSG2(_(e_dictkey), fudi.fd_newkey);
3469 vim_free(fudi.fd_newkey);
3470 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003471 if (tofree == NULL)
3472 return;
3473
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003474 /* Increase refcount on dictionary, it could get deleted when evaluating
3475 * the arguments. */
3476 if (fudi.fd_dict != NULL)
3477 ++fudi.fd_dict->dv_refcount;
3478
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003479 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003480 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003481 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaar532c7802005-01-27 14:44:31 +00003483 /* Skip white space to allow ":call func ()". Not good, but required for
3484 * backward compatibility. */
3485 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 if (*startarg != '(')
3489 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003490 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 goto end;
3492 }
3493
3494 /*
3495 * When skipping, evaluate the function once, to find the end of the
3496 * arguments.
3497 * When the function takes a range, this is discovered after the first
3498 * call, and the loop is broken.
3499 */
3500 if (eap->skip)
3501 {
3502 ++emsg_skip;
3503 lnum = eap->line2; /* do it once, also with an invalid range */
3504 }
3505 else
3506 lnum = eap->line1;
3507 for ( ; lnum <= eap->line2; ++lnum)
3508 {
3509 if (!eap->skip && eap->addr_count > 0)
3510 {
3511 curwin->w_cursor.lnum = lnum;
3512 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003513#ifdef FEAT_VIRTUALEDIT
3514 curwin->w_cursor.coladd = 0;
3515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003518 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003519 eap->line1, eap->line2, &doesrange,
3520 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 failed = TRUE;
3523 break;
3524 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003525
3526 /* Handle a function returning a Funcref, Dictionary or List. */
3527 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3528 {
3529 failed = TRUE;
3530 break;
3531 }
3532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (doesrange || eap->skip)
3535 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003538 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003540 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (aborting())
3542 break;
3543 }
3544 if (eap->skip)
3545 --emsg_skip;
3546
3547 if (!failed)
3548 {
3549 /* Check for trailing illegal characters and a following command. */
3550 if (!ends_excmd(*arg))
3551 {
3552 emsg_severe = TRUE;
3553 EMSG(_(e_trailing));
3554 }
3555 else
3556 eap->nextcmd = check_nextcmd(arg);
3557 }
3558
3559end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003560 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003561 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562}
3563
3564/*
3565 * ":unlet[!] var1 ... " command.
3566 */
3567 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003568ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003570 ex_unletlock(eap, eap->arg, 0);
3571}
3572
3573/*
3574 * ":lockvar" and ":unlockvar" commands
3575 */
3576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003577ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003578{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580 int deep = 2;
3581
3582 if (eap->forceit)
3583 deep = -1;
3584 else if (vim_isdigit(*arg))
3585 {
3586 deep = getdigits(&arg);
3587 arg = skipwhite(arg);
3588 }
3589
3590 ex_unletlock(eap, arg, deep);
3591}
3592
3593/*
3594 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3595 */
3596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003597ex_unletlock(
3598 exarg_T *eap,
3599 char_u *argstart,
3600 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003601{
3602 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
3607 do
3608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003610 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003611 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 if (lv.ll_name == NULL)
3613 error = TRUE; /* error but continue parsing */
3614 if (name_end == NULL || (!vim_iswhite(*name_end)
3615 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 if (name_end != NULL)
3618 {
3619 emsg_severe = TRUE;
3620 EMSG(_(e_trailing));
3621 }
3622 if (!(eap->skip || error))
3623 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 break;
3625 }
3626
3627 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 {
3629 if (eap->cmdidx == CMD_unlet)
3630 {
3631 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3632 error = TRUE;
3633 }
3634 else
3635 {
3636 if (do_lock_var(&lv, name_end, deep,
3637 eap->cmdidx == CMD_lockvar) == FAIL)
3638 error = TRUE;
3639 }
3640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 if (!eap->skip)
3643 clear_lval(&lv);
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 arg = skipwhite(name_end);
3646 } while (!ends_excmd(*arg));
3647
3648 eap->nextcmd = check_nextcmd(arg);
3649}
3650
Bram Moolenaar8c711452005-01-14 21:53:12 +00003651 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003652do_unlet_var(
3653 lval_T *lp,
3654 char_u *name_end,
3655 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003656{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 int ret = OK;
3658 int cc;
3659
3660 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 cc = *name_end;
3663 *name_end = NUL;
3664
3665 /* Normal name or expanded name. */
3666 if (check_changedtick(lp->ll_name))
3667 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003669 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003672 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003673 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003674 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003676 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 else if (lp->ll_range)
3678 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003680 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003681 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003682
3683 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3684 {
3685 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003686 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003687 return FAIL;
3688 ll_li = li;
3689 ++ll_n1;
3690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003691
3692 /* Delete a range of List items. */
3693 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3694 {
3695 li = lp->ll_li->li_next;
3696 listitem_remove(lp->ll_list, lp->ll_li);
3697 lp->ll_li = li;
3698 ++lp->ll_n1;
3699 }
3700 }
3701 else
3702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003704 /* unlet a List item. */
3705 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003707 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003708 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 }
3710
3711 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003712}
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714/*
3715 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003716 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 */
3718 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003719do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720{
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 hashtab_T *ht;
3722 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003723 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003724 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003725 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
Bram Moolenaar33570922005-01-25 22:26:29 +00003727 ht = find_var_ht(name, &varname);
3728 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003730 if (ht == &globvarht)
3731 d = &globvardict;
3732 else if (current_funccal != NULL
3733 && ht == &current_funccal->l_vars.dv_hashtab)
3734 d = &current_funccal->l_vars;
3735 else if (ht == &compat_hashtab)
3736 d = &vimvardict;
3737 else
3738 {
3739 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3740 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3741 }
3742 if (d == NULL)
3743 {
3744 EMSG2(_(e_intern2), "do_unlet()");
3745 return FAIL;
3746 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003747 hi = hash_find(ht, varname);
3748 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003749 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003750 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003751 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003752 || var_check_ro(di->di_flags, name, FALSE)
3753 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003754 return FAIL;
3755
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003756 delete_var(ht, hi);
3757 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760 if (forceit)
3761 return OK;
3762 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 return FAIL;
3764}
3765
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766/*
3767 * Lock or unlock variable indicated by "lp".
3768 * "deep" is the levels to go (-1 for unlimited);
3769 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3770 */
3771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003772do_lock_var(
3773 lval_T *lp,
3774 char_u *name_end,
3775 int deep,
3776 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003777{
3778 int ret = OK;
3779 int cc;
3780 dictitem_T *di;
3781
3782 if (deep == 0) /* nothing to do */
3783 return OK;
3784
3785 if (lp->ll_tv == NULL)
3786 {
3787 cc = *name_end;
3788 *name_end = NUL;
3789
3790 /* Normal name or expanded name. */
3791 if (check_changedtick(lp->ll_name))
3792 ret = FAIL;
3793 else
3794 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003795 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003796 if (di == NULL)
3797 ret = FAIL;
3798 else
3799 {
3800 if (lock)
3801 di->di_flags |= DI_FLAGS_LOCK;
3802 else
3803 di->di_flags &= ~DI_FLAGS_LOCK;
3804 item_lock(&di->di_tv, deep, lock);
3805 }
3806 }
3807 *name_end = cc;
3808 }
3809 else if (lp->ll_range)
3810 {
3811 listitem_T *li = lp->ll_li;
3812
3813 /* (un)lock a range of List items. */
3814 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3815 {
3816 item_lock(&li->li_tv, deep, lock);
3817 li = li->li_next;
3818 ++lp->ll_n1;
3819 }
3820 }
3821 else if (lp->ll_list != NULL)
3822 /* (un)lock a List item. */
3823 item_lock(&lp->ll_li->li_tv, deep, lock);
3824 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003825 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003826 item_lock(&lp->ll_di->di_tv, deep, lock);
3827
3828 return ret;
3829}
3830
3831/*
3832 * Lock or unlock an item. "deep" is nr of levels to go.
3833 */
3834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003835item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003836{
3837 static int recurse = 0;
3838 list_T *l;
3839 listitem_T *li;
3840 dict_T *d;
3841 hashitem_T *hi;
3842 int todo;
3843
3844 if (recurse >= DICT_MAXNEST)
3845 {
3846 EMSG(_("E743: variable nested too deep for (un)lock"));
3847 return;
3848 }
3849 if (deep == 0)
3850 return;
3851 ++recurse;
3852
3853 /* lock/unlock the item itself */
3854 if (lock)
3855 tv->v_lock |= VAR_LOCKED;
3856 else
3857 tv->v_lock &= ~VAR_LOCKED;
3858
3859 switch (tv->v_type)
3860 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003861 case VAR_UNKNOWN:
3862 case VAR_NUMBER:
3863 case VAR_STRING:
3864 case VAR_FUNC:
3865 case VAR_FLOAT:
3866 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003867 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003868 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003869 break;
3870
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003871 case VAR_LIST:
3872 if ((l = tv->vval.v_list) != NULL)
3873 {
3874 if (lock)
3875 l->lv_lock |= VAR_LOCKED;
3876 else
3877 l->lv_lock &= ~VAR_LOCKED;
3878 if (deep < 0 || deep > 1)
3879 /* recursive: lock/unlock the items the List contains */
3880 for (li = l->lv_first; li != NULL; li = li->li_next)
3881 item_lock(&li->li_tv, deep - 1, lock);
3882 }
3883 break;
3884 case VAR_DICT:
3885 if ((d = tv->vval.v_dict) != NULL)
3886 {
3887 if (lock)
3888 d->dv_lock |= VAR_LOCKED;
3889 else
3890 d->dv_lock &= ~VAR_LOCKED;
3891 if (deep < 0 || deep > 1)
3892 {
3893 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003894 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003895 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3896 {
3897 if (!HASHITEM_EMPTY(hi))
3898 {
3899 --todo;
3900 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3901 }
3902 }
3903 }
3904 }
3905 }
3906 --recurse;
3907}
3908
Bram Moolenaara40058a2005-07-11 22:42:07 +00003909/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003910 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3911 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003912 */
3913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003914tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003915{
3916 return (tv->v_lock & VAR_LOCKED)
3917 || (tv->v_type == VAR_LIST
3918 && tv->vval.v_list != NULL
3919 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3920 || (tv->v_type == VAR_DICT
3921 && tv->vval.v_dict != NULL
3922 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3923}
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3926/*
3927 * Delete all "menutrans_" variables.
3928 */
3929 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003930del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931{
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
Bram Moolenaar33570922005-01-25 22:26:29 +00003935 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003936 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003938 {
3939 if (!HASHITEM_EMPTY(hi))
3940 {
3941 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3943 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 }
3945 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947}
3948#endif
3949
3950#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3951
3952/*
3953 * Local string buffer for the next two functions to store a variable name
3954 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3955 * get_user_var_name().
3956 */
3957
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003958static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
3960static char_u *varnamebuf = NULL;
3961static int varnamebuflen = 0;
3962
3963/*
3964 * Function to concatenate a prefix and a variable name.
3965 */
3966 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003967cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968{
3969 int len;
3970
3971 len = (int)STRLEN(name) + 3;
3972 if (len > varnamebuflen)
3973 {
3974 vim_free(varnamebuf);
3975 len += 10; /* some additional space */
3976 varnamebuf = alloc(len);
3977 if (varnamebuf == NULL)
3978 {
3979 varnamebuflen = 0;
3980 return NULL;
3981 }
3982 varnamebuflen = len;
3983 }
3984 *varnamebuf = prefix;
3985 varnamebuf[1] = ':';
3986 STRCPY(varnamebuf + 2, name);
3987 return varnamebuf;
3988}
3989
3990/*
3991 * Function given to ExpandGeneric() to obtain the list of user defined
3992 * (global/buffer/window/built-in) variable names.
3993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003995get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003997 static long_u gdone;
3998 static long_u bdone;
3999 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004000#ifdef FEAT_WINDOWS
4001 static long_u tdone;
4002#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004003 static int vidx;
4004 static hashitem_T *hi;
4005 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004008 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004009 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010#ifdef FEAT_WINDOWS
4011 tdone = 0;
4012#endif
4013 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004014
4015 /* Global variables */
4016 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004019 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004020 else
4021 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004022 while (HASHITEM_EMPTY(hi))
4023 ++hi;
4024 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4025 return cat_prefix_varname('g', hi->hi_key);
4026 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004028
4029 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004030 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004031 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004033 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004035 else
4036 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 while (HASHITEM_EMPTY(hi))
4038 ++hi;
4039 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return (char_u *)"b:changedtick";
4045 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004046
4047 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004048 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004051 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004053 else
4054 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004055 while (HASHITEM_EMPTY(hi))
4056 ++hi;
4057 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004059
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060#ifdef FEAT_WINDOWS
4061 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004062 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004063 if (tdone < ht->ht_used)
4064 {
4065 if (tdone++ == 0)
4066 hi = ht->ht_array;
4067 else
4068 ++hi;
4069 while (HASHITEM_EMPTY(hi))
4070 ++hi;
4071 return cat_prefix_varname('t', hi->hi_key);
4072 }
4073#endif
4074
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 /* v: variables */
4076 if (vidx < VV_LEN)
4077 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079 vim_free(varnamebuf);
4080 varnamebuf = NULL;
4081 varnamebuflen = 0;
4082 return NULL;
4083}
4084
4085#endif /* FEAT_CMDL_COMPL */
4086
4087/*
4088 * types for expressions.
4089 */
4090typedef enum
4091{
4092 TYPE_UNKNOWN = 0
4093 , TYPE_EQUAL /* == */
4094 , TYPE_NEQUAL /* != */
4095 , TYPE_GREATER /* > */
4096 , TYPE_GEQUAL /* >= */
4097 , TYPE_SMALLER /* < */
4098 , TYPE_SEQUAL /* <= */
4099 , TYPE_MATCH /* =~ */
4100 , TYPE_NOMATCH /* !~ */
4101} exptype_T;
4102
4103/*
4104 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4107 */
4108
4109/*
4110 * Handle zero level expression.
4111 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004113 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 * Return OK or FAIL.
4115 */
4116 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004117eval0(
4118 char_u *arg,
4119 typval_T *rettv,
4120 char_u **nextcmd,
4121 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123 int ret;
4124 char_u *p;
4125
4126 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 if (ret == FAIL || !ends_excmd(*p))
4129 {
4130 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 /*
4133 * Report the invalid expression unless the expression evaluation has
4134 * been cancelled due to an aborting error, an interrupt, or an
4135 * exception.
4136 */
4137 if (!aborting())
4138 EMSG2(_(e_invexpr2), arg);
4139 ret = FAIL;
4140 }
4141 if (nextcmd != NULL)
4142 *nextcmd = check_nextcmd(p);
4143
4144 return ret;
4145}
4146
4147/*
4148 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004149 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 *
4151 * "arg" must point to the first non-white of the expression.
4152 * "arg" is advanced to the next non-white after the recognized expression.
4153 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004154 * Note: "rettv.v_lock" is not set.
4155 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 * Return OK or FAIL.
4157 */
4158 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004159eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
4161 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004162 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164 /*
4165 * Get the first variable.
4166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return FAIL;
4169
4170 if ((*arg)[0] == '?')
4171 {
4172 result = FALSE;
4173 if (evaluate)
4174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004175 int error = FALSE;
4176
4177 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004180 if (error)
4181 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183
4184 /*
4185 * Get the second variable.
4186 */
4187 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 return FAIL;
4190
4191 /*
4192 * Check for the ":".
4193 */
4194 if ((*arg)[0] != ':')
4195 {
4196 EMSG(_("E109: Missing ':' after '?'"));
4197 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 return FAIL;
4200 }
4201
4202 /*
4203 * Get the third variable.
4204 */
4205 *arg = skipwhite(*arg + 1);
4206 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4207 {
4208 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004209 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 return FAIL;
4211 }
4212 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215
4216 return OK;
4217}
4218
4219/*
4220 * Handle first level expression:
4221 * expr2 || expr2 || expr2 logical OR
4222 *
4223 * "arg" must point to the first non-white of the expression.
4224 * "arg" is advanced to the next non-white after the recognized expression.
4225 *
4226 * Return OK or FAIL.
4227 */
4228 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004229eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
Bram Moolenaar33570922005-01-25 22:26:29 +00004231 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 long result;
4233 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
4236 /*
4237 * Get the first variable.
4238 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 return FAIL;
4241
4242 /*
4243 * Repeat until there is no following "||".
4244 */
4245 first = TRUE;
4246 result = FALSE;
4247 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4248 {
4249 if (evaluate && first)
4250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004254 if (error)
4255 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 first = FALSE;
4257 }
4258
4259 /*
4260 * Get the second variable.
4261 */
4262 *arg = skipwhite(*arg + 2);
4263 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4264 return FAIL;
4265
4266 /*
4267 * Compute the result.
4268 */
4269 if (evaluate && !result)
4270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 if (error)
4275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 if (evaluate)
4278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 rettv->v_type = VAR_NUMBER;
4280 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 }
4283
4284 return OK;
4285}
4286
4287/*
4288 * Handle second level expression:
4289 * expr3 && expr3 && expr3 logical AND
4290 *
4291 * "arg" must point to the first non-white of the expression.
4292 * "arg" is advanced to the next non-white after the recognized expression.
4293 *
4294 * Return OK or FAIL.
4295 */
4296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004297eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298{
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 long result;
4301 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004302 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304 /*
4305 * Get the first variable.
4306 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004307 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 return FAIL;
4309
4310 /*
4311 * Repeat until there is no following "&&".
4312 */
4313 first = TRUE;
4314 result = TRUE;
4315 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4316 {
4317 if (evaluate && first)
4318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 if (error)
4323 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 first = FALSE;
4325 }
4326
4327 /*
4328 * Get the second variable.
4329 */
4330 *arg = skipwhite(*arg + 2);
4331 if (eval4(arg, &var2, evaluate && result) == FAIL)
4332 return FAIL;
4333
4334 /*
4335 * Compute the result.
4336 */
4337 if (evaluate && result)
4338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (error)
4343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 if (evaluate)
4346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004347 rettv->v_type = VAR_NUMBER;
4348 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 }
4351
4352 return OK;
4353}
4354
4355/*
4356 * Handle third level expression:
4357 * var1 == var2
4358 * var1 =~ var2
4359 * var1 != var2
4360 * var1 !~ var2
4361 * var1 > var2
4362 * var1 >= var2
4363 * var1 < var2
4364 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004365 * var1 is var2
4366 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 *
4368 * "arg" must point to the first non-white of the expression.
4369 * "arg" is advanced to the next non-white after the recognized expression.
4370 *
4371 * Return OK or FAIL.
4372 */
4373 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004374eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
Bram Moolenaar33570922005-01-25 22:26:29 +00004376 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 char_u *p;
4378 int i;
4379 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004380 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 int len = 2;
4382 long n1, n2;
4383 char_u *s1, *s2;
4384 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4385 regmatch_T regmatch;
4386 int ic;
4387 char_u *save_cpo;
4388
4389 /*
4390 * Get the first variable.
4391 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 return FAIL;
4394
4395 p = *arg;
4396 switch (p[0])
4397 {
4398 case '=': if (p[1] == '=')
4399 type = TYPE_EQUAL;
4400 else if (p[1] == '~')
4401 type = TYPE_MATCH;
4402 break;
4403 case '!': if (p[1] == '=')
4404 type = TYPE_NEQUAL;
4405 else if (p[1] == '~')
4406 type = TYPE_NOMATCH;
4407 break;
4408 case '>': if (p[1] != '=')
4409 {
4410 type = TYPE_GREATER;
4411 len = 1;
4412 }
4413 else
4414 type = TYPE_GEQUAL;
4415 break;
4416 case '<': if (p[1] != '=')
4417 {
4418 type = TYPE_SMALLER;
4419 len = 1;
4420 }
4421 else
4422 type = TYPE_SEQUAL;
4423 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 case 'i': if (p[1] == 's')
4425 {
4426 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4427 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004428 i = p[len];
4429 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 {
4431 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4432 type_is = TRUE;
4433 }
4434 }
4435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437
4438 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004439 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 */
4441 if (type != TYPE_UNKNOWN)
4442 {
4443 /* extra question mark appended: ignore case */
4444 if (p[len] == '?')
4445 {
4446 ic = TRUE;
4447 ++len;
4448 }
4449 /* extra '#' appended: match case */
4450 else if (p[len] == '#')
4451 {
4452 ic = FALSE;
4453 ++len;
4454 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004455 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 else
4457 ic = p_ic;
4458
4459 /*
4460 * Get the second variable.
4461 */
4462 *arg = skipwhite(p + len);
4463 if (eval5(arg, &var2, evaluate) == FAIL)
4464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 return FAIL;
4467 }
4468
4469 if (evaluate)
4470 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004471 if (type_is && rettv->v_type != var2.v_type)
4472 {
4473 /* For "is" a different type always means FALSE, for "notis"
4474 * it means TRUE. */
4475 n1 = (type == TYPE_NEQUAL);
4476 }
4477 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4478 {
4479 if (type_is)
4480 {
4481 n1 = (rettv->v_type == var2.v_type
4482 && rettv->vval.v_list == var2.vval.v_list);
4483 if (type == TYPE_NEQUAL)
4484 n1 = !n1;
4485 }
4486 else if (rettv->v_type != var2.v_type
4487 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4488 {
4489 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004490 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004491 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004492 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 clear_tv(rettv);
4494 clear_tv(&var2);
4495 return FAIL;
4496 }
4497 else
4498 {
4499 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004500 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4501 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004502 if (type == TYPE_NEQUAL)
4503 n1 = !n1;
4504 }
4505 }
4506
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004507 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4508 {
4509 if (type_is)
4510 {
4511 n1 = (rettv->v_type == var2.v_type
4512 && rettv->vval.v_dict == var2.vval.v_dict);
4513 if (type == TYPE_NEQUAL)
4514 n1 = !n1;
4515 }
4516 else if (rettv->v_type != var2.v_type
4517 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4518 {
4519 if (rettv->v_type != var2.v_type)
4520 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4521 else
4522 EMSG(_("E736: Invalid operation for Dictionary"));
4523 clear_tv(rettv);
4524 clear_tv(&var2);
4525 return FAIL;
4526 }
4527 else
4528 {
4529 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004530 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4531 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004532 if (type == TYPE_NEQUAL)
4533 n1 = !n1;
4534 }
4535 }
4536
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004537 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4538 {
4539 if (rettv->v_type != var2.v_type
4540 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4541 {
4542 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004543 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004545 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004546 clear_tv(rettv);
4547 clear_tv(&var2);
4548 return FAIL;
4549 }
4550 else
4551 {
4552 /* Compare two Funcrefs for being equal or unequal. */
4553 if (rettv->vval.v_string == NULL
4554 || var2.vval.v_string == NULL)
4555 n1 = FALSE;
4556 else
4557 n1 = STRCMP(rettv->vval.v_string,
4558 var2.vval.v_string) == 0;
4559 if (type == TYPE_NEQUAL)
4560 n1 = !n1;
4561 }
4562 }
4563
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004564#ifdef FEAT_FLOAT
4565 /*
4566 * If one of the two variables is a float, compare as a float.
4567 * When using "=~" or "!~", always compare as string.
4568 */
4569 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4570 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4571 {
4572 float_T f1, f2;
4573
4574 if (rettv->v_type == VAR_FLOAT)
4575 f1 = rettv->vval.v_float;
4576 else
4577 f1 = get_tv_number(rettv);
4578 if (var2.v_type == VAR_FLOAT)
4579 f2 = var2.vval.v_float;
4580 else
4581 f2 = get_tv_number(&var2);
4582 n1 = FALSE;
4583 switch (type)
4584 {
4585 case TYPE_EQUAL: n1 = (f1 == f2); break;
4586 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4587 case TYPE_GREATER: n1 = (f1 > f2); break;
4588 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4589 case TYPE_SMALLER: n1 = (f1 < f2); break;
4590 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4591 case TYPE_UNKNOWN:
4592 case TYPE_MATCH:
4593 case TYPE_NOMATCH: break; /* avoid gcc warning */
4594 }
4595 }
4596#endif
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 /*
4599 * If one of the two variables is a number, compare as a number.
4600 * When using "=~" or "!~", always compare as string.
4601 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004602 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004605 n1 = get_tv_number(rettv);
4606 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 switch (type)
4608 {
4609 case TYPE_EQUAL: n1 = (n1 == n2); break;
4610 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4611 case TYPE_GREATER: n1 = (n1 > n2); break;
4612 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4613 case TYPE_SMALLER: n1 = (n1 < n2); break;
4614 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4615 case TYPE_UNKNOWN:
4616 case TYPE_MATCH:
4617 case TYPE_NOMATCH: break; /* avoid gcc warning */
4618 }
4619 }
4620 else
4621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 s1 = get_tv_string_buf(rettv, buf1);
4623 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4625 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4626 else
4627 i = 0;
4628 n1 = FALSE;
4629 switch (type)
4630 {
4631 case TYPE_EQUAL: n1 = (i == 0); break;
4632 case TYPE_NEQUAL: n1 = (i != 0); break;
4633 case TYPE_GREATER: n1 = (i > 0); break;
4634 case TYPE_GEQUAL: n1 = (i >= 0); break;
4635 case TYPE_SMALLER: n1 = (i < 0); break;
4636 case TYPE_SEQUAL: n1 = (i <= 0); break;
4637
4638 case TYPE_MATCH:
4639 case TYPE_NOMATCH:
4640 /* avoid 'l' flag in 'cpoptions' */
4641 save_cpo = p_cpo;
4642 p_cpo = (char_u *)"";
4643 regmatch.regprog = vim_regcomp(s2,
4644 RE_MAGIC + RE_STRING);
4645 regmatch.rm_ic = ic;
4646 if (regmatch.regprog != NULL)
4647 {
4648 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004649 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (type == TYPE_NOMATCH)
4651 n1 = !n1;
4652 }
4653 p_cpo = save_cpo;
4654 break;
4655
4656 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4657 }
4658 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 clear_tv(rettv);
4660 clear_tv(&var2);
4661 rettv->v_type = VAR_NUMBER;
4662 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
4664 }
4665
4666 return OK;
4667}
4668
4669/*
4670 * Handle fourth level expression:
4671 * + number addition
4672 * - number subtraction
4673 * . string concatenation
4674 *
4675 * "arg" must point to the first non-white of the expression.
4676 * "arg" is advanced to the next non-white after the recognized expression.
4677 *
4678 * Return OK or FAIL.
4679 */
4680 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004681eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682{
Bram Moolenaar33570922005-01-25 22:26:29 +00004683 typval_T var2;
4684 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 int op;
4686 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004687#ifdef FEAT_FLOAT
4688 float_T f1 = 0, f2 = 0;
4689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 char_u *s1, *s2;
4691 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4692 char_u *p;
4693
4694 /*
4695 * Get the first variable.
4696 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004697 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 return FAIL;
4699
4700 /*
4701 * Repeat computing, until no '+', '-' or '.' is following.
4702 */
4703 for (;;)
4704 {
4705 op = **arg;
4706 if (op != '+' && op != '-' && op != '.')
4707 break;
4708
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004709 if ((op != '+' || rettv->v_type != VAR_LIST)
4710#ifdef FEAT_FLOAT
4711 && (op == '.' || rettv->v_type != VAR_FLOAT)
4712#endif
4713 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 {
4715 /* For "list + ...", an illegal use of the first operand as
4716 * a number cannot be determined before evaluating the 2nd
4717 * operand: if this is also a list, all is ok.
4718 * For "something . ...", "something - ..." or "non-list + ...",
4719 * we know that the first operand needs to be a string or number
4720 * without evaluating the 2nd operand. So check before to avoid
4721 * side effects after an error. */
4722 if (evaluate && get_tv_string_chk(rettv) == NULL)
4723 {
4724 clear_tv(rettv);
4725 return FAIL;
4726 }
4727 }
4728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 /*
4730 * Get the second variable.
4731 */
4732 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004733 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004735 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 return FAIL;
4737 }
4738
4739 if (evaluate)
4740 {
4741 /*
4742 * Compute the result.
4743 */
4744 if (op == '.')
4745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004746 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4747 s2 = get_tv_string_buf_chk(&var2, buf2);
4748 if (s2 == NULL) /* type error ? */
4749 {
4750 clear_tv(rettv);
4751 clear_tv(&var2);
4752 return FAIL;
4753 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004754 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 clear_tv(rettv);
4756 rettv->v_type = VAR_STRING;
4757 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004759 else if (op == '+' && rettv->v_type == VAR_LIST
4760 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004761 {
4762 /* concatenate Lists */
4763 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4764 &var3) == FAIL)
4765 {
4766 clear_tv(rettv);
4767 clear_tv(&var2);
4768 return FAIL;
4769 }
4770 clear_tv(rettv);
4771 *rettv = var3;
4772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 else
4774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 int error = FALSE;
4776
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777#ifdef FEAT_FLOAT
4778 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004779 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780 f1 = rettv->vval.v_float;
4781 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783 else
4784#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 n1 = get_tv_number_chk(rettv, &error);
4787 if (error)
4788 {
4789 /* This can only happen for "list + non-list". For
4790 * "non-list + ..." or "something - ...", we returned
4791 * before evaluating the 2nd operand. */
4792 clear_tv(rettv);
4793 return FAIL;
4794 }
4795#ifdef FEAT_FLOAT
4796 if (var2.v_type == VAR_FLOAT)
4797 f1 = n1;
4798#endif
4799 }
4800#ifdef FEAT_FLOAT
4801 if (var2.v_type == VAR_FLOAT)
4802 {
4803 f2 = var2.vval.v_float;
4804 n2 = 0;
4805 }
4806 else
4807#endif
4808 {
4809 n2 = get_tv_number_chk(&var2, &error);
4810 if (error)
4811 {
4812 clear_tv(rettv);
4813 clear_tv(&var2);
4814 return FAIL;
4815 }
4816#ifdef FEAT_FLOAT
4817 if (rettv->v_type == VAR_FLOAT)
4818 f2 = n2;
4819#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004822
4823#ifdef FEAT_FLOAT
4824 /* If there is a float on either side the result is a float. */
4825 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4826 {
4827 if (op == '+')
4828 f1 = f1 + f2;
4829 else
4830 f1 = f1 - f2;
4831 rettv->v_type = VAR_FLOAT;
4832 rettv->vval.v_float = f1;
4833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004835#endif
4836 {
4837 if (op == '+')
4838 n1 = n1 + n2;
4839 else
4840 n1 = n1 - n2;
4841 rettv->v_type = VAR_NUMBER;
4842 rettv->vval.v_number = n1;
4843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004845 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
4847 }
4848 return OK;
4849}
4850
4851/*
4852 * Handle fifth level expression:
4853 * * number multiplication
4854 * / number division
4855 * % number modulo
4856 *
4857 * "arg" must point to the first non-white of the expression.
4858 * "arg" is advanced to the next non-white after the recognized expression.
4859 *
4860 * Return OK or FAIL.
4861 */
4862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004863eval6(
4864 char_u **arg,
4865 typval_T *rettv,
4866 int evaluate,
4867 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
Bram Moolenaar33570922005-01-25 22:26:29 +00004869 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 int op;
4871 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872#ifdef FEAT_FLOAT
4873 int use_float = FALSE;
4874 float_T f1 = 0, f2;
4875#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004876 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
4878 /*
4879 * Get the first variable.
4880 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004881 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 return FAIL;
4883
4884 /*
4885 * Repeat computing, until no '*', '/' or '%' is following.
4886 */
4887 for (;;)
4888 {
4889 op = **arg;
4890 if (op != '*' && op != '/' && op != '%')
4891 break;
4892
4893 if (evaluate)
4894 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895#ifdef FEAT_FLOAT
4896 if (rettv->v_type == VAR_FLOAT)
4897 {
4898 f1 = rettv->vval.v_float;
4899 use_float = TRUE;
4900 n1 = 0;
4901 }
4902 else
4903#endif
4904 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004905 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004906 if (error)
4907 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909 else
4910 n1 = 0;
4911
4912 /*
4913 * Get the second variable.
4914 */
4915 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004916 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 return FAIL;
4918
4919 if (evaluate)
4920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921#ifdef FEAT_FLOAT
4922 if (var2.v_type == VAR_FLOAT)
4923 {
4924 if (!use_float)
4925 {
4926 f1 = n1;
4927 use_float = TRUE;
4928 }
4929 f2 = var2.vval.v_float;
4930 n2 = 0;
4931 }
4932 else
4933#endif
4934 {
4935 n2 = get_tv_number_chk(&var2, &error);
4936 clear_tv(&var2);
4937 if (error)
4938 return FAIL;
4939#ifdef FEAT_FLOAT
4940 if (use_float)
4941 f2 = n2;
4942#endif
4943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 /*
4946 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949#ifdef FEAT_FLOAT
4950 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952 if (op == '*')
4953 f1 = f1 * f2;
4954 else if (op == '/')
4955 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004956# ifdef VMS
4957 /* VMS crashes on divide by zero, work around it */
4958 if (f2 == 0.0)
4959 {
4960 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004961 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004962 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004963 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004964 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004965 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966 }
4967 else
4968 f1 = f1 / f2;
4969# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970 /* We rely on the floating point library to handle divide
4971 * by zero to result in "inf" and not a crash. */
4972 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004977 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 return FAIL;
4979 }
4980 rettv->v_type = VAR_FLOAT;
4981 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 if (op == '*')
4987 n1 = n1 * n2;
4988 else if (op == '/')
4989 {
4990 if (n2 == 0) /* give an error message? */
4991 {
4992 if (n1 == 0)
4993 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4994 else if (n1 < 0)
4995 n1 = -0x7fffffffL;
4996 else
4997 n1 = 0x7fffffffL;
4998 }
4999 else
5000 n1 = n1 / n2;
5001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003 {
5004 if (n2 == 0) /* give an error message? */
5005 n1 = 0;
5006 else
5007 n1 = n1 % n2;
5008 }
5009 rettv->v_type = VAR_NUMBER;
5010 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013 }
5014
5015 return OK;
5016}
5017
5018/*
5019 * Handle sixth level expression:
5020 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005021 * "string" string constant
5022 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 * &option-name option value
5024 * @r register contents
5025 * identifier variable value
5026 * function() function call
5027 * $VAR environment variable
5028 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005029 * [expr, expr] List
5030 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 *
5032 * Also handle:
5033 * ! in front logical NOT
5034 * - in front unary minus
5035 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 * trailing [] subscript in String or List
5037 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 *
5039 * "arg" must point to the first non-white of the expression.
5040 * "arg" is advanced to the next non-white after the recognized expression.
5041 *
5042 * Return OK or FAIL.
5043 */
5044 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005045eval7(
5046 char_u **arg,
5047 typval_T *rettv,
5048 int evaluate,
5049 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 long n;
5052 int len;
5053 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u *start_leader, *end_leader;
5055 int ret = OK;
5056 char_u *alias;
5057
5058 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005060 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 /*
5065 * Skip '!' and '-' characters. They are handled later.
5066 */
5067 start_leader = *arg;
5068 while (**arg == '!' || **arg == '-' || **arg == '+')
5069 *arg = skipwhite(*arg + 1);
5070 end_leader = *arg;
5071
5072 switch (**arg)
5073 {
5074 /*
5075 * Number constant.
5076 */
5077 case '0':
5078 case '1':
5079 case '2':
5080 case '3':
5081 case '4':
5082 case '5':
5083 case '6':
5084 case '7':
5085 case '8':
5086 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005087 {
5088#ifdef FEAT_FLOAT
5089 char_u *p = skipdigits(*arg + 1);
5090 int get_float = FALSE;
5091
5092 /* We accept a float when the format matches
5093 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005094 * strict to avoid backwards compatibility problems.
5095 * Don't look for a float after the "." operator, so that
5096 * ":let vers = 1.2.3" doesn't fail. */
5097 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005099 get_float = TRUE;
5100 p = skipdigits(p + 2);
5101 if (*p == 'e' || *p == 'E')
5102 {
5103 ++p;
5104 if (*p == '-' || *p == '+')
5105 ++p;
5106 if (!vim_isdigit(*p))
5107 get_float = FALSE;
5108 else
5109 p = skipdigits(p + 1);
5110 }
5111 if (ASCII_ISALPHA(*p) || *p == '.')
5112 get_float = FALSE;
5113 }
5114 if (get_float)
5115 {
5116 float_T f;
5117
5118 *arg += string2float(*arg, &f);
5119 if (evaluate)
5120 {
5121 rettv->v_type = VAR_FLOAT;
5122 rettv->vval.v_float = f;
5123 }
5124 }
5125 else
5126#endif
5127 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005128 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005129 *arg += len;
5130 if (evaluate)
5131 {
5132 rettv->v_type = VAR_NUMBER;
5133 rettv->vval.v_number = n;
5134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 /*
5140 * String constant: "string".
5141 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 break;
5144
5145 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 break;
5150
5151 /*
5152 * List: [expr, expr]
5153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 break;
5156
5157 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 * Dictionary: {key: val, key: val}
5159 */
5160 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5161 break;
5162
5163 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005164 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 break;
5168
5169 /*
5170 * Environment variable: $VAR.
5171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
5176 * Register contents: @r.
5177 */
5178 case '@': ++*arg;
5179 if (evaluate)
5180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005182 rettv->vval.v_string = get_reg_contents(**arg,
5183 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
5185 if (**arg != NUL)
5186 ++*arg;
5187 break;
5188
5189 /*
5190 * nested expression: (expression).
5191 */
5192 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005193 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 if (**arg == ')')
5195 ++*arg;
5196 else if (ret == OK)
5197 {
5198 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 ret = FAIL;
5201 }
5202 break;
5203
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 break;
5206 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207
5208 if (ret == NOTDONE)
5209 {
5210 /*
5211 * Must be a variable or function name.
5212 * Can also be a curly-braces kind of name: {expr}.
5213 */
5214 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005215 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 if (alias != NULL)
5217 s = alias;
5218
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005219 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 ret = FAIL;
5221 else
5222 {
5223 if (**arg == '(') /* recursive! */
5224 {
5225 /* If "s" is the name of a variable of type VAR_FUNC
5226 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005227 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228
5229 /* Invoke the function. */
5230 ret = get_func_tv(s, len, rettv, arg,
5231 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005232 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005233
5234 /* If evaluate is FALSE rettv->v_type was not set in
5235 * get_func_tv, but it's needed in handle_subscript() to parse
5236 * what follows. So set it here. */
5237 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5238 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005239 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005240 rettv->v_type = VAR_FUNC;
5241 }
5242
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243 /* Stop the expression evaluation when immediately
5244 * aborting on error, or when an interrupt occurred or
5245 * an exception was thrown but not caught. */
5246 if (aborting())
5247 {
5248 if (ret == OK)
5249 clear_tv(rettv);
5250 ret = FAIL;
5251 }
5252 }
5253 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005254 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005255 else
5256 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005258 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 }
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 *arg = skipwhite(*arg);
5262
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005263 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5264 * expr(expr). */
5265 if (ret == OK)
5266 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
5268 /*
5269 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5270 */
5271 if (ret == OK && evaluate && end_leader > start_leader)
5272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005273 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005274 int val = 0;
5275#ifdef FEAT_FLOAT
5276 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005278 if (rettv->v_type == VAR_FLOAT)
5279 f = rettv->vval.v_float;
5280 else
5281#endif
5282 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 clear_tv(rettv);
5286 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005288 else
5289 {
5290 while (end_leader > start_leader)
5291 {
5292 --end_leader;
5293 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005294 {
5295#ifdef FEAT_FLOAT
5296 if (rettv->v_type == VAR_FLOAT)
5297 f = !f;
5298 else
5299#endif
5300 val = !val;
5301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 {
5304#ifdef FEAT_FLOAT
5305 if (rettv->v_type == VAR_FLOAT)
5306 f = -f;
5307 else
5308#endif
5309 val = -val;
5310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312#ifdef FEAT_FLOAT
5313 if (rettv->v_type == VAR_FLOAT)
5314 {
5315 clear_tv(rettv);
5316 rettv->vval.v_float = f;
5317 }
5318 else
5319#endif
5320 {
5321 clear_tv(rettv);
5322 rettv->v_type = VAR_NUMBER;
5323 rettv->vval.v_number = val;
5324 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327
5328 return ret;
5329}
5330
5331/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005332 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5333 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5335 */
5336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005337eval_index(
5338 char_u **arg,
5339 typval_T *rettv,
5340 int evaluate,
5341 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342{
5343 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005344 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 long len = -1;
5347 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005349 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350
Bram Moolenaara03f2332016-02-06 18:09:59 +01005351 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005353 case VAR_FUNC:
5354 if (verbose)
5355 EMSG(_("E695: Cannot index a Funcref"));
5356 return FAIL;
5357 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005358#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005359 if (verbose)
5360 EMSG(_(e_float_as_string));
5361 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005362#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005364 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005365 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005366 if (verbose)
5367 EMSG(_("E909: Cannot index a special variable"));
5368 return FAIL;
5369 case VAR_UNKNOWN:
5370 if (evaluate)
5371 return FAIL;
5372 /* FALLTHROUGH */
5373
5374 case VAR_STRING:
5375 case VAR_NUMBER:
5376 case VAR_LIST:
5377 case VAR_DICT:
5378 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005379 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005381 init_tv(&var1);
5382 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005383 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 /*
5386 * dict.name
5387 */
5388 key = *arg + 1;
5389 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5390 ;
5391 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 }
5395 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005397 /*
5398 * something[idx]
5399 *
5400 * Get the (first) variable from inside the [].
5401 */
5402 *arg = skipwhite(*arg + 1);
5403 if (**arg == ':')
5404 empty1 = TRUE;
5405 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5406 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005407 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5408 {
5409 /* not a number or string */
5410 clear_tv(&var1);
5411 return FAIL;
5412 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005413
5414 /*
5415 * Get the second variable from inside the [:].
5416 */
5417 if (**arg == ':')
5418 {
5419 range = TRUE;
5420 *arg = skipwhite(*arg + 1);
5421 if (**arg == ']')
5422 empty2 = TRUE;
5423 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005425 if (!empty1)
5426 clear_tv(&var1);
5427 return FAIL;
5428 }
5429 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5430 {
5431 /* not a number or string */
5432 if (!empty1)
5433 clear_tv(&var1);
5434 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005435 return FAIL;
5436 }
5437 }
5438
5439 /* Check for the ']'. */
5440 if (**arg != ']')
5441 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005442 if (verbose)
5443 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 clear_tv(&var1);
5445 if (range)
5446 clear_tv(&var2);
5447 return FAIL;
5448 }
5449 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 }
5451
5452 if (evaluate)
5453 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 n1 = 0;
5455 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 n1 = get_tv_number(&var1);
5458 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460 if (range)
5461 {
5462 if (empty2)
5463 n2 = -1;
5464 else
5465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 n2 = get_tv_number(&var2);
5467 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 }
5470
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005471 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005473 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005474 case VAR_FUNC:
5475 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005476 case VAR_SPECIAL:
5477 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005478 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005479 break; /* not evaluating, skipping over subscript */
5480
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 case VAR_NUMBER:
5482 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005483 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 len = (long)STRLEN(s);
5485 if (range)
5486 {
5487 /* The resulting variable is a substring. If the indexes
5488 * are out of range the result is empty. */
5489 if (n1 < 0)
5490 {
5491 n1 = len + n1;
5492 if (n1 < 0)
5493 n1 = 0;
5494 }
5495 if (n2 < 0)
5496 n2 = len + n2;
5497 else if (n2 >= len)
5498 n2 = len;
5499 if (n1 >= len || n2 < 0 || n1 > n2)
5500 s = NULL;
5501 else
5502 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5503 }
5504 else
5505 {
5506 /* The resulting variable is a string of a single
5507 * character. If the index is too big or negative the
5508 * result is empty. */
5509 if (n1 >= len || n1 < 0)
5510 s = NULL;
5511 else
5512 s = vim_strnsave(s + n1, 1);
5513 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514 clear_tv(rettv);
5515 rettv->v_type = VAR_STRING;
5516 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 break;
5518
5519 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005520 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 if (n1 < 0)
5522 n1 = len + n1;
5523 if (!empty1 && (n1 < 0 || n1 >= len))
5524 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005525 /* For a range we allow invalid values and return an empty
5526 * list. A list index out of range is an error. */
5527 if (!range)
5528 {
5529 if (verbose)
5530 EMSGN(_(e_listidx), n1);
5531 return FAIL;
5532 }
5533 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 }
5535 if (range)
5536 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005537 list_T *l;
5538 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005539
5540 if (n2 < 0)
5541 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005542 else if (n2 >= len)
5543 n2 = len - 1;
5544 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005545 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 l = list_alloc();
5547 if (l == NULL)
5548 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 n1 <= n2; ++n1)
5551 {
5552 if (list_append_tv(l, &item->li_tv) == FAIL)
5553 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005554 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 return FAIL;
5556 }
5557 item = item->li_next;
5558 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 clear_tv(rettv);
5560 rettv->v_type = VAR_LIST;
5561 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005562 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 }
5564 else
5565 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005566 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 clear_tv(rettv);
5568 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569 }
5570 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005571
5572 case VAR_DICT:
5573 if (range)
5574 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005575 if (verbose)
5576 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 if (len == -1)
5578 clear_tv(&var1);
5579 return FAIL;
5580 }
5581 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005582 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583
5584 if (len == -1)
5585 {
5586 key = get_tv_string(&var1);
5587 if (*key == NUL)
5588 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005589 if (verbose)
5590 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 clear_tv(&var1);
5592 return FAIL;
5593 }
5594 }
5595
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005596 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005598 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005599 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 if (len == -1)
5601 clear_tv(&var1);
5602 if (item == NULL)
5603 return FAIL;
5604
5605 copy_tv(&item->di_tv, &var1);
5606 clear_tv(rettv);
5607 *rettv = var1;
5608 }
5609 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005610 }
5611 }
5612
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 return OK;
5614}
5615
5616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 * Get an option value.
5618 * "arg" points to the '&' or '+' before the option name.
5619 * "arg" is advanced to character after the option name.
5620 * Return OK or FAIL.
5621 */
5622 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005623get_option_tv(
5624 char_u **arg,
5625 typval_T *rettv, /* when NULL, only check if option exists */
5626 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 char_u *option_end;
5629 long numval;
5630 char_u *stringval;
5631 int opt_type;
5632 int c;
5633 int working = (**arg == '+'); /* has("+option") */
5634 int ret = OK;
5635 int opt_flags;
5636
5637 /*
5638 * Isolate the option name and find its value.
5639 */
5640 option_end = find_option_end(arg, &opt_flags);
5641 if (option_end == NULL)
5642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005643 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 EMSG2(_("E112: Option name missing: %s"), *arg);
5645 return FAIL;
5646 }
5647
5648 if (!evaluate)
5649 {
5650 *arg = option_end;
5651 return OK;
5652 }
5653
5654 c = *option_end;
5655 *option_end = NUL;
5656 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
5659 if (opt_type == -3) /* invalid name */
5660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005661 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 EMSG2(_("E113: Unknown option: %s"), *arg);
5663 ret = FAIL;
5664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
5667 if (opt_type == -2) /* hidden string option */
5668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 rettv->v_type = VAR_STRING;
5670 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
5672 else if (opt_type == -1) /* hidden number option */
5673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 rettv->v_type = VAR_NUMBER;
5675 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677 else if (opt_type == 1) /* number option */
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 rettv->v_type = VAR_NUMBER;
5680 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 else /* string option */
5683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 rettv->v_type = VAR_STRING;
5685 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 }
5688 else if (working && (opt_type == -2 || opt_type == -1))
5689 ret = FAIL;
5690
5691 *option_end = c; /* put back for error messages */
5692 *arg = option_end;
5693
5694 return ret;
5695}
5696
5697/*
5698 * Allocate a variable for a string constant.
5699 * Return OK or FAIL.
5700 */
5701 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005702get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
5704 char_u *p;
5705 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 int extra = 0;
5707
5708 /*
5709 * Find the end of the string, skipping backslashed characters.
5710 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005711 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
5713 if (*p == '\\' && p[1] != NUL)
5714 {
5715 ++p;
5716 /* A "\<x>" form occupies at least 4 characters, and produces up
5717 * to 6 characters: reserve space for 2 extra */
5718 if (*p == '<')
5719 extra += 2;
5720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722
5723 if (*p != '"')
5724 {
5725 EMSG2(_("E114: Missing quote: %s"), *arg);
5726 return FAIL;
5727 }
5728
5729 /* If only parsing, set *arg and return here */
5730 if (!evaluate)
5731 {
5732 *arg = p + 1;
5733 return OK;
5734 }
5735
5736 /*
5737 * Copy the string into allocated memory, handling backslashed
5738 * characters.
5739 */
5740 name = alloc((unsigned)(p - *arg + extra));
5741 if (name == NULL)
5742 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 rettv->v_type = VAR_STRING;
5744 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 if (*p == '\\')
5749 {
5750 switch (*++p)
5751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 case 'b': *name++ = BS; ++p; break;
5753 case 'e': *name++ = ESC; ++p; break;
5754 case 'f': *name++ = FF; ++p; break;
5755 case 'n': *name++ = NL; ++p; break;
5756 case 'r': *name++ = CAR; ++p; break;
5757 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758
5759 case 'X': /* hex: "\x1", "\x12" */
5760 case 'x':
5761 case 'u': /* Unicode: "\u0023" */
5762 case 'U':
5763 if (vim_isxdigit(p[1]))
5764 {
5765 int n, nr;
5766 int c = toupper(*p);
5767
5768 if (c == 'X')
5769 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005770 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005772 else
5773 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 nr = 0;
5775 while (--n >= 0 && vim_isxdigit(p[1]))
5776 {
5777 ++p;
5778 nr = (nr << 4) + hex2nr(*p);
5779 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781#ifdef FEAT_MBYTE
5782 /* For "\u" store the number according to
5783 * 'encoding'. */
5784 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 else
5787#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 break;
5791
5792 /* octal: "\1", "\12", "\123" */
5793 case '0':
5794 case '1':
5795 case '2':
5796 case '3':
5797 case '4':
5798 case '5':
5799 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 case '7': *name = *p++ - '0';
5801 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 *name = (*name << 3) + *p++ - '0';
5804 if (*p >= '0' && *p <= '7')
5805 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 break;
5809
5810 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 if (extra != 0)
5813 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 break;
5816 }
5817 /* FALLTHROUGH */
5818
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 break;
5821 }
5822 }
5823 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 *arg = p + 1;
5829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 return OK;
5831}
5832
5833/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 * Return OK or FAIL.
5836 */
5837 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005838get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
5840 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 int reduce = 0;
5843
5844 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005845 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 */
5847 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 break;
5853 ++reduce;
5854 ++p;
5855 }
5856 }
5857
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 return FAIL;
5862 }
5863
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 if (!evaluate)
5866 {
5867 *arg = p + 1;
5868 return OK;
5869 }
5870
5871 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 */
5874 str = alloc((unsigned)((p - *arg) - reduce));
5875 if (str == NULL)
5876 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 rettv->v_type = VAR_STRING;
5878 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 break;
5886 ++p;
5887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 *arg = p + 1;
5892
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 return OK;
5894}
5895
5896/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897 * Allocate a variable for a List and fill it from "*arg".
5898 * Return OK or FAIL.
5899 */
5900 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005901get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902{
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 list_T *l = NULL;
5904 typval_T tv;
5905 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906
5907 if (evaluate)
5908 {
5909 l = list_alloc();
5910 if (l == NULL)
5911 return FAIL;
5912 }
5913
5914 *arg = skipwhite(*arg + 1);
5915 while (**arg != ']' && **arg != NUL)
5916 {
5917 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5918 goto failret;
5919 if (evaluate)
5920 {
5921 item = listitem_alloc();
5922 if (item != NULL)
5923 {
5924 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005925 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 list_append(l, item);
5927 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005928 else
5929 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 }
5931
5932 if (**arg == ']')
5933 break;
5934 if (**arg != ',')
5935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 goto failret;
5938 }
5939 *arg = skipwhite(*arg + 1);
5940 }
5941
5942 if (**arg != ']')
5943 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005944 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945failret:
5946 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005947 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 return FAIL;
5949 }
5950
5951 *arg = skipwhite(*arg + 1);
5952 if (evaluate)
5953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005954 rettv->v_type = VAR_LIST;
5955 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 ++l->lv_refcount;
5957 }
5958
5959 return OK;
5960}
5961
5962/*
5963 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005964 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005966 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005967list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005969 list_T *l;
5970
5971 l = (list_T *)alloc_clear(sizeof(list_T));
5972 if (l != NULL)
5973 {
5974 /* Prepend the list to the list of lists for garbage collection. */
5975 if (first_list != NULL)
5976 first_list->lv_used_prev = l;
5977 l->lv_used_prev = NULL;
5978 l->lv_used_next = first_list;
5979 first_list = l;
5980 }
5981 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982}
5983
5984/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005985 * Allocate an empty list for a return value.
5986 * Returns OK or FAIL.
5987 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005988 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005989rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005990{
5991 list_T *l = list_alloc();
5992
5993 if (l == NULL)
5994 return FAIL;
5995
5996 rettv->vval.v_list = l;
5997 rettv->v_type = VAR_LIST;
5998 ++l->lv_refcount;
5999 return OK;
6000}
6001
6002/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 * Unreference a list: decrement the reference count and free it when it
6004 * becomes zero.
6005 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006006 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006007list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006009 if (l != NULL && --l->lv_refcount <= 0)
6010 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011}
6012
6013/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006014 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 * Ignores the reference count.
6016 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006018list_free(
6019 list_T *l,
6020 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021{
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006024 /* Remove the list from the list of lists for garbage collection. */
6025 if (l->lv_used_prev == NULL)
6026 first_list = l->lv_used_next;
6027 else
6028 l->lv_used_prev->lv_used_next = l->lv_used_next;
6029 if (l->lv_used_next != NULL)
6030 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6031
Bram Moolenaard9fba312005-06-26 22:34:35 +00006032 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006034 /* Remove the item before deleting it. */
6035 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006036 if (recurse || (item->li_tv.v_type != VAR_LIST
6037 && item->li_tv.v_type != VAR_DICT))
6038 clear_tv(&item->li_tv);
6039 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040 }
6041 vim_free(l);
6042}
6043
6044/*
6045 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006046 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006048 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006049listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050{
Bram Moolenaar33570922005-01-25 22:26:29 +00006051 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052}
6053
6054/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006055 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006058listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006060 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061 vim_free(item);
6062}
6063
6064/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006065 * Remove a list item from a List and free it. Also clears the value.
6066 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006067 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006068listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006069{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006070 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006071 listitem_free(item);
6072}
6073
6074/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075 * Get the number of items in a list.
6076 */
6077 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006078list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080 if (l == NULL)
6081 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006082 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083}
6084
6085/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006086 * Return TRUE when two lists have exactly the same values.
6087 */
6088 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006089list_equal(
6090 list_T *l1,
6091 list_T *l2,
6092 int ic, /* ignore case for strings */
6093 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006094{
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006097 if (l1 == NULL || l2 == NULL)
6098 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006099 if (l1 == l2)
6100 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006101 if (list_len(l1) != list_len(l2))
6102 return FALSE;
6103
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104 for (item1 = l1->lv_first, item2 = l2->lv_first;
6105 item1 != NULL && item2 != NULL;
6106 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108 return FALSE;
6109 return item1 == NULL && item2 == NULL;
6110}
6111
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006112/*
6113 * Return the dictitem that an entry in a hashtable points to.
6114 */
6115 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006117{
6118 return HI2DI(hi);
6119}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006120
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006122 * Return TRUE when two dictionaries have exactly the same key/values.
6123 */
6124 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006125dict_equal(
6126 dict_T *d1,
6127 dict_T *d2,
6128 int ic, /* ignore case for strings */
6129 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006130{
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 hashitem_T *hi;
6132 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006133 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006134
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006135 if (d1 == NULL || d2 == NULL)
6136 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006137 if (d1 == d2)
6138 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139 if (dict_len(d1) != dict_len(d2))
6140 return FALSE;
6141
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006142 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006145 if (!HASHITEM_EMPTY(hi))
6146 {
6147 item2 = dict_find(d2, hi->hi_key, -1);
6148 if (item2 == NULL)
6149 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006151 return FALSE;
6152 --todo;
6153 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 }
6155 return TRUE;
6156}
6157
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006158static int tv_equal_recurse_limit;
6159
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006160/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006161 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006162 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006163 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006164 */
6165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006166tv_equal(
6167 typval_T *tv1,
6168 typval_T *tv2,
6169 int ic, /* ignore case */
6170 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171{
6172 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006173 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006174 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006175 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006177 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006178 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006180 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181 * recursiveness to a limit. We guess they are equal then.
6182 * A fixed limit has the problem of still taking an awful long time.
6183 * Reduce the limit every time running into it. That should work fine for
6184 * deeply linked structures that are not recursively linked and catch
6185 * recursiveness quickly. */
6186 if (!recursive)
6187 tv_equal_recurse_limit = 1000;
6188 if (recursive_cnt >= tv_equal_recurse_limit)
6189 {
6190 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006191 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006192 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193
6194 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006195 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006196 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006197 ++recursive_cnt;
6198 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6199 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006200 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006201
6202 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203 ++recursive_cnt;
6204 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6205 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006206 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006207
6208 case VAR_FUNC:
6209 return (tv1->vval.v_string != NULL
6210 && tv2->vval.v_string != NULL
6211 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6212
6213 case VAR_NUMBER:
6214 return tv1->vval.v_number == tv2->vval.v_number;
6215
6216 case VAR_STRING:
6217 s1 = get_tv_string_buf(tv1, buf1);
6218 s2 = get_tv_string_buf(tv2, buf2);
6219 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006220
6221 case VAR_SPECIAL:
6222 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006223
6224 case VAR_FLOAT:
6225#ifdef FEAT_FLOAT
6226 return tv1->vval.v_float == tv2->vval.v_float;
6227#endif
6228 case VAR_JOB:
6229#ifdef FEAT_JOB
6230 return tv1->vval.v_job == tv2->vval.v_job;
6231#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006232 case VAR_CHANNEL:
6233#ifdef FEAT_CHANNEL
6234 return tv1->vval.v_channel == tv2->vval.v_channel;
6235#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006236 case VAR_UNKNOWN:
6237 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006238 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006239
Bram Moolenaara03f2332016-02-06 18:09:59 +01006240 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6241 * does not equal anything, not even itself. */
6242 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006243}
6244
6245/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246 * Locate item with index "n" in list "l" and return it.
6247 * A negative index is counted from the end; -1 is the last item.
6248 * Returns NULL when "n" is out of range.
6249 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006250 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006251list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252{
Bram Moolenaar33570922005-01-25 22:26:29 +00006253 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254 long idx;
6255
6256 if (l == NULL)
6257 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006258
6259 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006261 n = l->lv_len + n;
6262
6263 /* Check for index out of range. */
6264 if (n < 0 || n >= l->lv_len)
6265 return NULL;
6266
6267 /* When there is a cached index may start search from there. */
6268 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 if (n < l->lv_idx / 2)
6271 {
6272 /* closest to the start of the list */
6273 item = l->lv_first;
6274 idx = 0;
6275 }
6276 else if (n > (l->lv_idx + l->lv_len) / 2)
6277 {
6278 /* closest to the end of the list */
6279 item = l->lv_last;
6280 idx = l->lv_len - 1;
6281 }
6282 else
6283 {
6284 /* closest to the cached index */
6285 item = l->lv_idx_item;
6286 idx = l->lv_idx;
6287 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288 }
6289 else
6290 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006291 if (n < l->lv_len / 2)
6292 {
6293 /* closest to the start of the list */
6294 item = l->lv_first;
6295 idx = 0;
6296 }
6297 else
6298 {
6299 /* closest to the end of the list */
6300 item = l->lv_last;
6301 idx = l->lv_len - 1;
6302 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006303 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006304
6305 while (n > idx)
6306 {
6307 /* search forward */
6308 item = item->li_next;
6309 ++idx;
6310 }
6311 while (n < idx)
6312 {
6313 /* search backward */
6314 item = item->li_prev;
6315 --idx;
6316 }
6317
6318 /* cache the used index */
6319 l->lv_idx = idx;
6320 l->lv_idx_item = item;
6321
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322 return item;
6323}
6324
6325/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006326 * Get list item "l[idx]" as a number.
6327 */
6328 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006329list_find_nr(
6330 list_T *l,
6331 long idx,
6332 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006333{
6334 listitem_T *li;
6335
6336 li = list_find(l, idx);
6337 if (li == NULL)
6338 {
6339 if (errorp != NULL)
6340 *errorp = TRUE;
6341 return -1L;
6342 }
6343 return get_tv_number_chk(&li->li_tv, errorp);
6344}
6345
6346/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006347 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6348 */
6349 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006350list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006351{
6352 listitem_T *li;
6353
6354 li = list_find(l, idx - 1);
6355 if (li == NULL)
6356 {
6357 EMSGN(_(e_listidx), idx);
6358 return NULL;
6359 }
6360 return get_tv_string(&li->li_tv);
6361}
6362
6363/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006364 * Locate "item" list "l" and return its index.
6365 * Returns -1 when "item" is not in the list.
6366 */
6367 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006368list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006369{
6370 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006372
6373 if (l == NULL)
6374 return -1;
6375 idx = 0;
6376 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6377 ++idx;
6378 if (li == NULL)
6379 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006380 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006381}
6382
6383/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384 * Append item "item" to the end of list "l".
6385 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006386 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006387list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388{
6389 if (l->lv_last == NULL)
6390 {
6391 /* empty list */
6392 l->lv_first = item;
6393 l->lv_last = item;
6394 item->li_prev = NULL;
6395 }
6396 else
6397 {
6398 l->lv_last->li_next = item;
6399 item->li_prev = l->lv_last;
6400 l->lv_last = item;
6401 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006402 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403 item->li_next = NULL;
6404}
6405
6406/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006408 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006411list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006413 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414
Bram Moolenaar05159a02005-02-26 23:04:13 +00006415 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006417 copy_tv(tv, &li->li_tv);
6418 list_append(l, li);
6419 return OK;
6420}
6421
6422/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006423 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006424 * Return FAIL when out of memory.
6425 */
6426 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006427list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006428{
6429 listitem_T *li = listitem_alloc();
6430
6431 if (li == NULL)
6432 return FAIL;
6433 li->li_tv.v_type = VAR_DICT;
6434 li->li_tv.v_lock = 0;
6435 li->li_tv.vval.v_dict = dict;
6436 list_append(list, li);
6437 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438 return OK;
6439}
6440
6441/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006442 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006443 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006444 * Returns FAIL when out of memory.
6445 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006446 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006447list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448{
6449 listitem_T *li = listitem_alloc();
6450
6451 if (li == NULL)
6452 return FAIL;
6453 list_append(l, li);
6454 li->li_tv.v_type = VAR_STRING;
6455 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006456 if (str == NULL)
6457 li->li_tv.vval.v_string = NULL;
6458 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006459 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006460 return FAIL;
6461 return OK;
6462}
6463
6464/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006465 * Append "n" to list "l".
6466 * Returns FAIL when out of memory.
6467 */
6468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006469list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006470{
6471 listitem_T *li;
6472
6473 li = listitem_alloc();
6474 if (li == NULL)
6475 return FAIL;
6476 li->li_tv.v_type = VAR_NUMBER;
6477 li->li_tv.v_lock = 0;
6478 li->li_tv.vval.v_number = n;
6479 list_append(l, li);
6480 return OK;
6481}
6482
6483/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006485 * If "item" is NULL append at the end.
6486 * Return FAIL when out of memory.
6487 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006488 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490{
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492
6493 if (ni == NULL)
6494 return FAIL;
6495 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006496 list_insert(l, ni, item);
6497 return OK;
6498}
6499
6500 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006501list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006502{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503 if (item == NULL)
6504 /* Append new item at end of list. */
6505 list_append(l, ni);
6506 else
6507 {
6508 /* Insert new item before existing item. */
6509 ni->li_prev = item->li_prev;
6510 ni->li_next = item;
6511 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006512 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006514 ++l->lv_idx;
6515 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006517 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006519 l->lv_idx_item = NULL;
6520 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006521 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006522 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524}
6525
6526/*
6527 * Extend "l1" with "l2".
6528 * If "bef" is NULL append at the end, otherwise insert before this item.
6529 * Returns FAIL when out of memory.
6530 */
6531 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006532list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533{
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006535 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006537 /* We also quit the loop when we have inserted the original item count of
6538 * the list, avoid a hang when we extend a list with itself. */
6539 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6541 return FAIL;
6542 return OK;
6543}
6544
6545/*
6546 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6547 * Return FAIL when out of memory.
6548 */
6549 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006550list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551{
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006554 if (l1 == NULL || l2 == NULL)
6555 return FAIL;
6556
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006558 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 if (l == NULL)
6560 return FAIL;
6561 tv->v_type = VAR_LIST;
6562 tv->vval.v_list = l;
6563
6564 /* append all items from the second list */
6565 return list_extend(l, l2, NULL);
6566}
6567
6568/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006569 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006571 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 * Returns NULL when out of memory.
6573 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006575list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576{
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 list_T *copy;
6578 listitem_T *item;
6579 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580
6581 if (orig == NULL)
6582 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583
6584 copy = list_alloc();
6585 if (copy != NULL)
6586 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006587 if (copyID != 0)
6588 {
6589 /* Do this before adding the items, because one of the items may
6590 * refer back to this list. */
6591 orig->lv_copyID = copyID;
6592 orig->lv_copylist = copy;
6593 }
6594 for (item = orig->lv_first; item != NULL && !got_int;
6595 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596 {
6597 ni = listitem_alloc();
6598 if (ni == NULL)
6599 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006600 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 {
6602 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6603 {
6604 vim_free(ni);
6605 break;
6606 }
6607 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006609 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610 list_append(copy, ni);
6611 }
6612 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006613 if (item != NULL)
6614 {
6615 list_unref(copy);
6616 copy = NULL;
6617 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 }
6619
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 return copy;
6621}
6622
6623/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006625 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006626 * This used to be called list_remove, but that conflicts with a Sun header
6627 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006630vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631{
Bram Moolenaar33570922005-01-25 22:26:29 +00006632 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634 /* notify watchers */
6635 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006637 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006638 list_fix_watch(l, ip);
6639 if (ip == item2)
6640 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006642
6643 if (item2->li_next == NULL)
6644 l->lv_last = item->li_prev;
6645 else
6646 item2->li_next->li_prev = item->li_prev;
6647 if (item->li_prev == NULL)
6648 l->lv_first = item2->li_next;
6649 else
6650 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006651 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006652}
6653
6654/*
6655 * Return an allocated string with the string representation of a list.
6656 * May return NULL.
6657 */
6658 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006659list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660{
6661 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662
6663 if (tv->vval.v_list == NULL)
6664 return NULL;
6665 ga_init2(&ga, (int)sizeof(char), 80);
6666 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006667 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006668 {
6669 vim_free(ga.ga_data);
6670 return NULL;
6671 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672 ga_append(&ga, ']');
6673 ga_append(&ga, NUL);
6674 return (char_u *)ga.ga_data;
6675}
6676
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006677typedef struct join_S {
6678 char_u *s;
6679 char_u *tofree;
6680} join_T;
6681
6682 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006683list_join_inner(
6684 garray_T *gap, /* to store the result in */
6685 list_T *l,
6686 char_u *sep,
6687 int echo_style,
6688 int copyID,
6689 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006690{
6691 int i;
6692 join_T *p;
6693 int len;
6694 int sumlen = 0;
6695 int first = TRUE;
6696 char_u *tofree;
6697 char_u numbuf[NUMBUFLEN];
6698 listitem_T *item;
6699 char_u *s;
6700
6701 /* Stringify each item in the list. */
6702 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6703 {
6704 if (echo_style)
6705 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6706 else
6707 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6708 if (s == NULL)
6709 return FAIL;
6710
6711 len = (int)STRLEN(s);
6712 sumlen += len;
6713
Bram Moolenaarcde88542015-08-11 19:14:00 +02006714 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006715 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6716 if (tofree != NULL || s != numbuf)
6717 {
6718 p->s = s;
6719 p->tofree = tofree;
6720 }
6721 else
6722 {
6723 p->s = vim_strnsave(s, len);
6724 p->tofree = p->s;
6725 }
6726
6727 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006728 if (did_echo_string_emsg) /* recursion error, bail out */
6729 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006730 }
6731
6732 /* Allocate result buffer with its total size, avoid re-allocation and
6733 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6734 if (join_gap->ga_len >= 2)
6735 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6736 if (ga_grow(gap, sumlen + 2) == FAIL)
6737 return FAIL;
6738
6739 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6740 {
6741 if (first)
6742 first = FALSE;
6743 else
6744 ga_concat(gap, sep);
6745 p = ((join_T *)join_gap->ga_data) + i;
6746
6747 if (p->s != NULL)
6748 ga_concat(gap, p->s);
6749 line_breakcheck();
6750 }
6751
6752 return OK;
6753}
6754
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006755/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006756 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006757 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006758 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006759 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006760 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006761list_join(
6762 garray_T *gap,
6763 list_T *l,
6764 char_u *sep,
6765 int echo_style,
6766 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006767{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006768 garray_T join_ga;
6769 int retval;
6770 join_T *p;
6771 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006772
Bram Moolenaard39a7512015-04-16 22:51:22 +02006773 if (l->lv_len < 1)
6774 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006775 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6776 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6777
6778 /* Dispose each item in join_ga. */
6779 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006780 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006781 p = (join_T *)join_ga.ga_data;
6782 for (i = 0; i < join_ga.ga_len; ++i)
6783 {
6784 vim_free(p->tofree);
6785 ++p;
6786 }
6787 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006788 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006789
6790 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006791}
6792
6793/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006794 * Return the next (unique) copy ID.
6795 * Used for serializing nested structures.
6796 */
6797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006798get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006799{
6800 current_copyID += COPYID_INC;
6801 return current_copyID;
6802}
6803
6804/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006805 * Garbage collection for lists and dictionaries.
6806 *
6807 * We use reference counts to be able to free most items right away when they
6808 * are no longer used. But for composite items it's possible that it becomes
6809 * unused while the reference count is > 0: When there is a recursive
6810 * reference. Example:
6811 * :let l = [1, 2, 3]
6812 * :let d = {9: l}
6813 * :let l[1] = d
6814 *
6815 * Since this is quite unusual we handle this with garbage collection: every
6816 * once in a while find out which lists and dicts are not referenced from any
6817 * variable.
6818 *
6819 * Here is a good reference text about garbage collection (refers to Python
6820 * but it applies to all reference-counting mechanisms):
6821 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006822 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006823
6824/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825 * Do garbage collection for lists and dicts.
6826 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006827 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006829garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006830{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006831 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006832 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 buf_T *buf;
6834 win_T *wp;
6835 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006836 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006837 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006838 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006839#ifdef FEAT_WINDOWS
6840 tabpage_T *tp;
6841#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006843 /* Only do this once. */
6844 want_garbage_collect = FALSE;
6845 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006846 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006847
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 /* We advance by two because we add one for items referenced through
6849 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006850 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006851
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852 /*
6853 * 1. Go through all accessible variables and mark all lists and dicts
6854 * with copyID.
6855 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006856
6857 /* Don't free variables in the previous_funccal list unless they are only
6858 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006859 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6861 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006862 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6863 NULL);
6864 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6865 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866 }
6867
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006868 /* script-local variables */
6869 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006870 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006871
6872 /* buffer-local variables */
6873 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6875 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876
6877 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006878 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6880 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006881#ifdef FEAT_AUTOCMD
6882 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6884 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006885#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006887#ifdef FEAT_WINDOWS
6888 /* tabpage-local variables */
6889 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6891 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006892#endif
6893
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006895 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896
6897 /* function-local variables */
6898 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6899 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6901 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 }
6903
Bram Moolenaard812df62008-11-09 12:46:09 +00006904 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006906
Bram Moolenaar1dced572012-04-05 16:54:08 +02006907#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006909#endif
6910
Bram Moolenaardb913952012-06-29 12:54:53 +02006911#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006913#endif
6914
6915#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006917#endif
6918
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006919#ifdef FEAT_CHANNEL
6920 abort = abort || set_ref_in_channel(copyID);
6921#endif
6922
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006924 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 /*
6926 * 2. Free lists and dictionaries that are not referenced.
6927 */
6928 did_free = free_unref_items(copyID);
6929
6930 /*
6931 * 3. Check if any funccal can be freed now.
6932 */
6933 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006934 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 if (can_free_funccal(*pfc, copyID))
6936 {
6937 fc = *pfc;
6938 *pfc = fc->caller;
6939 free_funccal(fc, TRUE);
6940 did_free = TRUE;
6941 did_free_funccal = TRUE;
6942 }
6943 else
6944 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006945 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 if (did_free_funccal)
6947 /* When a funccal was freed some more items might be garbage
6948 * collected, so run again. */
6949 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006950 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006951 else if (p_verbose > 0)
6952 {
6953 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6954 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955
6956 return did_free;
6957}
6958
6959/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006960 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961 */
6962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006963free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006965 dict_T *dd, *dd_next;
6966 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 int did_free = FALSE;
6968
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971 */
6972 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006973 {
6974 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006977 /* Free the Dictionary and ordinary items it contains, but don't
6978 * recurse into Lists and Dictionaries, they will be in the list
6979 * of dicts or list of lists. */
6980 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006983 dd = dd_next;
6984 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985
6986 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987 * Go through the list of lists and free items without the copyID.
6988 * But don't free a list that has a watcher (used in a for loop), these
6989 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 */
6991 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006992 {
6993 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6995 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006997 /* Free the List and ordinary items it contains, but don't recurse
6998 * into Lists and Dictionaries, they will be in the list of dicts
6999 * or list of lists. */
7000 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007003 ll = ll_next;
7004 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007005
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 return did_free;
7007}
7008
7009/*
7010 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007011 * "list_stack" is used to add lists to be marked. Can be NULL.
7012 *
7013 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007015 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007016set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017{
7018 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007019 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 hashtab_T *cur_ht;
7022 ht_stack_T *ht_stack = NULL;
7023 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 cur_ht = ht;
7026 for (;;)
7027 {
7028 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 /* Mark each item in the hashtab. If the item contains a hashtab
7031 * it is added to ht_stack, if it contains a list it is added to
7032 * list_stack. */
7033 todo = (int)cur_ht->ht_used;
7034 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7035 if (!HASHITEM_EMPTY(hi))
7036 {
7037 --todo;
7038 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7039 &ht_stack, list_stack);
7040 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042
7043 if (ht_stack == NULL)
7044 break;
7045
7046 /* take an item from the stack */
7047 cur_ht = ht_stack->ht;
7048 tempitem = ht_stack;
7049 ht_stack = ht_stack->prev;
7050 free(tempitem);
7051 }
7052
7053 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007054}
7055
7056/*
7057 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7059 *
7060 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007061 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007063set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065 listitem_T *li;
7066 int abort = FALSE;
7067 list_T *cur_l;
7068 list_stack_T *list_stack = NULL;
7069 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071 cur_l = l;
7072 for (;;)
7073 {
7074 if (!abort)
7075 /* Mark each item in the list. If the item contains a hashtab
7076 * it is added to ht_stack, if it contains a list it is added to
7077 * list_stack. */
7078 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7079 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7080 ht_stack, &list_stack);
7081 if (list_stack == NULL)
7082 break;
7083
7084 /* take an item from the stack */
7085 cur_l = list_stack->list;
7086 tempitem = list_stack;
7087 list_stack = list_stack->prev;
7088 free(tempitem);
7089 }
7090
7091 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007092}
7093
7094/*
7095 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007096 * "list_stack" is used to add lists to be marked. Can be NULL.
7097 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7098 *
7099 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007102set_ref_in_item(
7103 typval_T *tv,
7104 int copyID,
7105 ht_stack_T **ht_stack,
7106 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107{
7108 dict_T *dd;
7109 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007111
Bram Moolenaara03f2332016-02-06 18:09:59 +01007112 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007113 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007114 dd = tv->vval.v_dict;
7115 if (dd != NULL && dd->dv_copyID != copyID)
7116 {
7117 /* Didn't see this dict yet. */
7118 dd->dv_copyID = copyID;
7119 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007120 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007121 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7122 }
7123 else
7124 {
7125 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7126 if (newitem == NULL)
7127 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007128 else
7129 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007130 newitem->ht = &dd->dv_hashtab;
7131 newitem->prev = *ht_stack;
7132 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007133 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007134 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007135 }
7136 }
7137 else if (tv->v_type == VAR_LIST)
7138 {
7139 ll = tv->vval.v_list;
7140 if (ll != NULL && ll->lv_copyID != copyID)
7141 {
7142 /* Didn't see this list yet. */
7143 ll->lv_copyID = copyID;
7144 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007145 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007146 abort = set_ref_in_list(ll, copyID, ht_stack);
7147 }
7148 else
7149 {
7150 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007151 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007152 if (newitem == NULL)
7153 abort = TRUE;
7154 else
7155 {
7156 newitem->list = ll;
7157 newitem->prev = *list_stack;
7158 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007159 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007160 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007161 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007162 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007163 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007164}
7165
7166/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007167 * Allocate an empty header for a dictionary.
7168 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007169 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007170dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171{
Bram Moolenaar33570922005-01-25 22:26:29 +00007172 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007173
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007175 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007176 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007177 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007178 if (first_dict != NULL)
7179 first_dict->dv_used_prev = d;
7180 d->dv_used_next = first_dict;
7181 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007182 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007183
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007185 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007186 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007187 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007188 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007189 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007191}
7192
7193/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007194 * Allocate an empty dict for a return value.
7195 * Returns OK or FAIL.
7196 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007197 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007198rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007199{
7200 dict_T *d = dict_alloc();
7201
7202 if (d == NULL)
7203 return FAIL;
7204
7205 rettv->vval.v_dict = d;
7206 rettv->v_type = VAR_DICT;
7207 ++d->dv_refcount;
7208 return OK;
7209}
7210
7211
7212/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213 * Unreference a Dictionary: decrement the reference count and free it when it
7214 * becomes zero.
7215 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007216 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007217dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007219 if (d != NULL && --d->dv_refcount <= 0)
7220 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221}
7222
7223/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007224 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225 * Ignores the reference count.
7226 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007227 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007228dict_free(
7229 dict_T *d,
7230 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007232 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007233 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007234 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007236 /* Remove the dict from the list of dicts for garbage collection. */
7237 if (d->dv_used_prev == NULL)
7238 first_dict = d->dv_used_next;
7239 else
7240 d->dv_used_prev->dv_used_next = d->dv_used_next;
7241 if (d->dv_used_next != NULL)
7242 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7243
7244 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007245 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007246 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007247 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007249 if (!HASHITEM_EMPTY(hi))
7250 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007251 /* Remove the item before deleting it, just in case there is
7252 * something recursive causing trouble. */
7253 di = HI2DI(hi);
7254 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007255 if (recurse || (di->di_tv.v_type != VAR_LIST
7256 && di->di_tv.v_type != VAR_DICT))
7257 clear_tv(&di->di_tv);
7258 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007259 --todo;
7260 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 vim_free(d);
7264}
7265
7266/*
7267 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007268 * The "key" is copied to the new item.
7269 * Note that the value of the item "di_tv" still needs to be initialized!
7270 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007272 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007273dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274{
Bram Moolenaar33570922005-01-25 22:26:29 +00007275 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007277 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007281 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007282 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007284}
7285
7286/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 * Make a copy of a Dictionary item.
7288 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007290dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007291{
Bram Moolenaar33570922005-01-25 22:26:29 +00007292 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007294 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7295 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296 if (di != NULL)
7297 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007299 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300 copy_tv(&org->di_tv, &di->di_tv);
7301 }
7302 return di;
7303}
7304
7305/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 * Remove item "item" from Dictionary "dict" and free it.
7307 */
7308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007309dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310{
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 if (HASHITEM_EMPTY(hi))
7315 EMSG2(_(e_intern2), "dictitem_remove()");
7316 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 dictitem_free(item);
7319}
7320
7321/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 * Free a dict item. Also clears the value.
7323 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007325dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007327 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007328 if (item->di_flags & DI_FLAGS_ALLOC)
7329 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007330}
7331
7332/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7334 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007335 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336 * Returns NULL when out of memory.
7337 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007339dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340{
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 dict_T *copy;
7342 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345
7346 if (orig == NULL)
7347 return NULL;
7348
7349 copy = dict_alloc();
7350 if (copy != NULL)
7351 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007352 if (copyID != 0)
7353 {
7354 orig->dv_copyID = copyID;
7355 orig->dv_copydict = copy;
7356 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007357 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007358 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007359 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 --todo;
7363
7364 di = dictitem_alloc(hi->hi_key);
7365 if (di == NULL)
7366 break;
7367 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007368 {
7369 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7370 copyID) == FAIL)
7371 {
7372 vim_free(di);
7373 break;
7374 }
7375 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007376 else
7377 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7378 if (dict_add(copy, di) == FAIL)
7379 {
7380 dictitem_free(di);
7381 break;
7382 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007385
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007387 if (todo > 0)
7388 {
7389 dict_unref(copy);
7390 copy = NULL;
7391 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 }
7393
7394 return copy;
7395}
7396
7397/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007399 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007402dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007403{
Bram Moolenaar33570922005-01-25 22:26:29 +00007404 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405}
7406
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007408 * Add a number or string entry to dictionary "d".
7409 * When "str" is NULL use number "nr", otherwise use "str".
7410 * Returns FAIL when out of memory and when key already exists.
7411 */
7412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007413dict_add_nr_str(
7414 dict_T *d,
7415 char *key,
7416 long nr,
7417 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007418{
7419 dictitem_T *item;
7420
7421 item = dictitem_alloc((char_u *)key);
7422 if (item == NULL)
7423 return FAIL;
7424 item->di_tv.v_lock = 0;
7425 if (str == NULL)
7426 {
7427 item->di_tv.v_type = VAR_NUMBER;
7428 item->di_tv.vval.v_number = nr;
7429 }
7430 else
7431 {
7432 item->di_tv.v_type = VAR_STRING;
7433 item->di_tv.vval.v_string = vim_strsave(str);
7434 }
7435 if (dict_add(d, item) == FAIL)
7436 {
7437 dictitem_free(item);
7438 return FAIL;
7439 }
7440 return OK;
7441}
7442
7443/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007444 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007445 * Returns FAIL when out of memory and when key already exists.
7446 */
7447 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007448dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007449{
7450 dictitem_T *item;
7451
7452 item = dictitem_alloc((char_u *)key);
7453 if (item == NULL)
7454 return FAIL;
7455 item->di_tv.v_lock = 0;
7456 item->di_tv.v_type = VAR_LIST;
7457 item->di_tv.vval.v_list = list;
7458 if (dict_add(d, item) == FAIL)
7459 {
7460 dictitem_free(item);
7461 return FAIL;
7462 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007463 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007464 return OK;
7465}
7466
7467/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 * Get the number of items in a Dictionary.
7469 */
7470 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007471dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007472{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007473 if (d == NULL)
7474 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007475 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476}
7477
7478/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 * Find item "key[len]" in Dictionary "d".
7480 * If "len" is negative use strlen(key).
7481 * Returns NULL when not found.
7482 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007483 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007484dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007486#define AKEYLEN 200
7487 char_u buf[AKEYLEN];
7488 char_u *akey;
7489 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007490 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492 if (len < 0)
7493 akey = key;
7494 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496 tofree = akey = vim_strnsave(key, len);
7497 if (akey == NULL)
7498 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007499 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 else
7501 {
7502 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007503 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007504 akey = buf;
7505 }
7506
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007508 vim_free(tofree);
7509 if (HASHITEM_EMPTY(hi))
7510 return NULL;
7511 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512}
7513
7514/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007515 * Get a string item from a dictionary.
7516 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007517 * Returns NULL if the entry doesn't exist or out of memory.
7518 */
7519 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007520get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007521{
7522 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007523 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007524
7525 di = dict_find(d, key, -1);
7526 if (di == NULL)
7527 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007528 s = get_tv_string(&di->di_tv);
7529 if (save && s != NULL)
7530 s = vim_strsave(s);
7531 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007532}
7533
7534/*
7535 * Get a number item from a dictionary.
7536 * Returns 0 if the entry doesn't exist or out of memory.
7537 */
7538 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007539get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007540{
7541 dictitem_T *di;
7542
7543 di = dict_find(d, key, -1);
7544 if (di == NULL)
7545 return 0;
7546 return get_tv_number(&di->di_tv);
7547}
7548
7549/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 * Return an allocated string with the string representation of a Dictionary.
7551 * May return NULL.
7552 */
7553 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007554dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555{
7556 garray_T ga;
7557 int first = TRUE;
7558 char_u *tofree;
7559 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566 return NULL;
7567 ga_init2(&ga, (int)sizeof(char), 80);
7568 ga_append(&ga, '{');
7569
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007570 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007571 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007573 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575 --todo;
7576
7577 if (first)
7578 first = FALSE;
7579 else
7580 ga_concat(&ga, (char_u *)", ");
7581
7582 tofree = string_quote(hi->hi_key, FALSE);
7583 if (tofree != NULL)
7584 {
7585 ga_concat(&ga, tofree);
7586 vim_free(tofree);
7587 }
7588 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007589 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 if (s != NULL)
7591 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007593 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007594 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007595 line_breakcheck();
7596
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007599 if (todo > 0)
7600 {
7601 vim_free(ga.ga_data);
7602 return NULL;
7603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604
7605 ga_append(&ga, '}');
7606 ga_append(&ga, NUL);
7607 return (char_u *)ga.ga_data;
7608}
7609
7610/*
7611 * Allocate a variable for a Dictionary and fill it from "*arg".
7612 * Return OK or FAIL. Returns NOTDONE for {expr}.
7613 */
7614 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007615get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616{
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 dict_T *d = NULL;
7618 typval_T tvkey;
7619 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007620 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624
7625 /*
7626 * First check if it's not a curly-braces thing: {expr}.
7627 * Must do this without evaluating, otherwise a function may be called
7628 * twice. Unfortunately this means we need to call eval1() twice for the
7629 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007630 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007632 if (*start != '}')
7633 {
7634 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7635 return FAIL;
7636 if (*start == '}')
7637 return NOTDONE;
7638 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639
7640 if (evaluate)
7641 {
7642 d = dict_alloc();
7643 if (d == NULL)
7644 return FAIL;
7645 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007646 tvkey.v_type = VAR_UNKNOWN;
7647 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648
7649 *arg = skipwhite(*arg + 1);
7650 while (**arg != '}' && **arg != NUL)
7651 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007652 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 goto failret;
7654 if (**arg != ':')
7655 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007656 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 goto failret;
7659 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007660 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007662 key = get_tv_string_buf_chk(&tvkey, buf);
7663 if (key == NULL || *key == NUL)
7664 {
7665 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7666 if (key != NULL)
7667 EMSG(_(e_emptykey));
7668 clear_tv(&tvkey);
7669 goto failret;
7670 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672
7673 *arg = skipwhite(*arg + 1);
7674 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7675 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007676 if (evaluate)
7677 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 goto failret;
7679 }
7680 if (evaluate)
7681 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007682 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 if (item != NULL)
7684 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007685 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007686 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 clear_tv(&tv);
7688 goto failret;
7689 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007690 item = dictitem_alloc(key);
7691 clear_tv(&tvkey);
7692 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007695 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 if (dict_add(d, item) == FAIL)
7697 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 }
7699 }
7700
7701 if (**arg == '}')
7702 break;
7703 if (**arg != ',')
7704 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007705 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007706 goto failret;
7707 }
7708 *arg = skipwhite(*arg + 1);
7709 }
7710
7711 if (**arg != '}')
7712 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007713 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714failret:
7715 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007716 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 return FAIL;
7718 }
7719
7720 *arg = skipwhite(*arg + 1);
7721 if (evaluate)
7722 {
7723 rettv->v_type = VAR_DICT;
7724 rettv->vval.v_dict = d;
7725 ++d->dv_refcount;
7726 }
7727
7728 return OK;
7729}
7730
Bram Moolenaar77073442016-02-13 23:23:53 +01007731#ifdef FEAT_CHANNEL
7732 static void
7733channel_unref(channel_T *channel)
7734{
7735 if (channel != NULL && --channel->ch_refcount <= 0)
7736 channel_free(channel);
7737}
7738#endif
7739
Bram Moolenaar835dc632016-02-07 14:27:38 +01007740#ifdef FEAT_JOB
7741 static void
7742job_free(job_T *job)
7743{
Bram Moolenaar77073442016-02-13 23:23:53 +01007744 if (job->jv_channel != NULL)
7745 {
7746 /* The channel doesn't count as a references for the job, we need to
7747 * NULL the reference when the job is freed. */
7748 job->jv_channel->ch_job = NULL;
7749 channel_unref(job->jv_channel);
7750 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01007751 mch_clear_job(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007752 vim_free(job);
7753}
7754
7755 static void
7756job_unref(job_T *job)
7757{
7758 if (job != NULL && --job->jv_refcount <= 0)
7759 job_free(job);
7760}
7761
7762/*
7763 * Allocate a job. Sets the refcount to one.
7764 */
7765 static job_T *
7766job_alloc(void)
7767{
7768 job_T *job;
7769
7770 job = (job_T *)alloc_clear(sizeof(job_T));
7771 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007772 job->jv_refcount = 1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007773 return job;
7774}
7775
7776#endif
7777
Bram Moolenaar17a13432016-01-24 14:22:10 +01007778 static char *
7779get_var_special_name(int nr)
7780{
7781 switch (nr)
7782 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007783 case VVAL_FALSE: return "v:false";
7784 case VVAL_TRUE: return "v:true";
7785 case VVAL_NONE: return "v:none";
7786 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007787 }
7788 EMSG2(_(e_intern2), "get_var_special_name()");
7789 return "42";
7790}
7791
Bram Moolenaar8c711452005-01-14 21:53:12 +00007792/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 * Return a string with the string representation of a variable.
7794 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007795 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007796 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007797 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007798 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 */
7800 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007801echo_string(
7802 typval_T *tv,
7803 char_u **tofree,
7804 char_u *numbuf,
7805 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007807 static int recurse = 0;
7808 char_u *r = NULL;
7809
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007812 if (!did_echo_string_emsg)
7813 {
7814 /* Only give this message once for a recursive call to avoid
7815 * flooding the user with errors. And stop iterating over lists
7816 * and dicts. */
7817 did_echo_string_emsg = TRUE;
7818 EMSG(_("E724: variable nested too deep for displaying"));
7819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007821 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 }
7823 ++recurse;
7824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007825 switch (tv->v_type)
7826 {
7827 case VAR_FUNC:
7828 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829 r = tv->vval.v_string;
7830 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007832 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007833 if (tv->vval.v_list == NULL)
7834 {
7835 *tofree = NULL;
7836 r = NULL;
7837 }
7838 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7839 {
7840 *tofree = NULL;
7841 r = (char_u *)"[...]";
7842 }
7843 else
7844 {
7845 tv->vval.v_list->lv_copyID = copyID;
7846 *tofree = list2string(tv, copyID);
7847 r = *tofree;
7848 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007850
Bram Moolenaar8c711452005-01-14 21:53:12 +00007851 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007852 if (tv->vval.v_dict == NULL)
7853 {
7854 *tofree = NULL;
7855 r = NULL;
7856 }
7857 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7858 {
7859 *tofree = NULL;
7860 r = (char_u *)"{...}";
7861 }
7862 else
7863 {
7864 tv->vval.v_dict->dv_copyID = copyID;
7865 *tofree = dict2string(tv, copyID);
7866 r = *tofree;
7867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007870 case VAR_STRING:
7871 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007872 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007873 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007874 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007875 *tofree = NULL;
7876 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007877 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007878
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007880#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007881 *tofree = NULL;
7882 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7883 r = numbuf;
7884 break;
7885#endif
7886
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007887 case VAR_SPECIAL:
7888 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007889 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007890 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007892
Bram Moolenaar8502c702014-06-17 12:51:16 +02007893 if (--recurse == 0)
7894 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896}
7897
7898/*
7899 * Return a string with the string representation of a variable.
7900 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7901 * "numbuf" is used for a number.
7902 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007903 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007904 */
7905 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007906tv2string(
7907 typval_T *tv,
7908 char_u **tofree,
7909 char_u *numbuf,
7910 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911{
7912 switch (tv->v_type)
7913 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 case VAR_FUNC:
7915 *tofree = string_quote(tv->vval.v_string, TRUE);
7916 return *tofree;
7917 case VAR_STRING:
7918 *tofree = string_quote(tv->vval.v_string, FALSE);
7919 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007920#ifdef FEAT_FLOAT
7921 case VAR_FLOAT:
7922 *tofree = NULL;
7923 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7924 return numbuf;
7925#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007926 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007927 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007928 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007929 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007930 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007931 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007932 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007933 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007934 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007935 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007936}
7937
7938/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 * Return string "str" in ' quotes, doubling ' characters.
7940 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 */
7943 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007944string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007945{
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007947 char_u *p, *r, *s;
7948
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 len = (function ? 13 : 3);
7950 if (str != NULL)
7951 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007952 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007953 for (p = str; *p != NUL; mb_ptr_adv(p))
7954 if (*p == '\'')
7955 ++len;
7956 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007957 s = r = alloc(len);
7958 if (r != NULL)
7959 {
7960 if (function)
7961 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007962 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007963 r += 10;
7964 }
7965 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007966 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007967 if (str != NULL)
7968 for (p = str; *p != NUL; )
7969 {
7970 if (*p == '\'')
7971 *r++ = '\'';
7972 MB_COPY_CHAR(p, r);
7973 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007974 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007975 if (function)
7976 *r++ = ')';
7977 *r++ = NUL;
7978 }
7979 return s;
7980}
7981
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007982#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007983/*
7984 * Convert the string "text" to a floating point number.
7985 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7986 * this always uses a decimal point.
7987 * Returns the length of the text that was consumed.
7988 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007989 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007990string2float(
7991 char_u *text,
7992 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007993{
7994 char *s = (char *)text;
7995 float_T f;
7996
7997 f = strtod(s, &s);
7998 *value = f;
7999 return (int)((char_u *)s - text);
8000}
8001#endif
8002
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 * Get the value of an environment variable.
8005 * "arg" is pointing to the '$'. It is advanced to after the name.
8006 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008007 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 */
8009 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008010get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
8012 char_u *string = NULL;
8013 int len;
8014 int cc;
8015 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008016 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
8018 ++*arg;
8019 name = *arg;
8020 len = get_env_len(arg);
8021 if (evaluate)
8022 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008023 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008024 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008025
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008026 cc = name[len];
8027 name[len] = NUL;
8028 /* first try vim_getenv(), fast for normal environment vars */
8029 string = vim_getenv(name, &mustfree);
8030 if (string != NULL && *string != NUL)
8031 {
8032 if (!mustfree)
8033 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008035 else
8036 {
8037 if (mustfree)
8038 vim_free(string);
8039
8040 /* next try expanding things like $VIM and ${HOME} */
8041 string = expand_env_save(name - 1);
8042 if (string != NULL && *string == '$')
8043 {
8044 vim_free(string);
8045 string = NULL;
8046 }
8047 }
8048 name[len] = cc;
8049
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008050 rettv->v_type = VAR_STRING;
8051 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053
8054 return OK;
8055}
8056
8057/*
8058 * Array with names and number of arguments of all internal functions
8059 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8060 */
8061static struct fst
8062{
8063 char *f_name; /* function name */
8064 char f_min_argc; /* minimal number of arguments */
8065 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008066 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008067 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068} functions[] =
8069{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#ifdef FEAT_FLOAT
8071 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008072 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008074 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008075 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008076 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"append", 2, 2, f_append},
8078 {"argc", 0, 0, f_argc},
8079 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008080 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008081 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008082#ifdef FEAT_FLOAT
8083 {"asin", 1, 1, f_asin}, /* WJMc */
8084#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008085 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008086 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008087 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008088 {"assert_false", 1, 2, f_assert_false},
8089 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008090#ifdef FEAT_FLOAT
8091 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008092 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008095 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"bufexists", 1, 1, f_bufexists},
8097 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8098 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8099 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8100 {"buflisted", 1, 1, f_buflisted},
8101 {"bufloaded", 1, 1, f_bufloaded},
8102 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008103 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"bufwinnr", 1, 1, f_bufwinnr},
8105 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008106 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008107 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008108 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008109#ifdef FEAT_FLOAT
8110 {"ceil", 1, 1, f_ceil},
8111#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008112#ifdef FEAT_CHANNEL
8113 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008114 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008115 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008116 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008117 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8118 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar77073442016-02-13 23:23:53 +01008119 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008120#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008121 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008122 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008124 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008126#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008127 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008128 {"complete_add", 1, 1, f_complete_add},
8129 {"complete_check", 0, 0, f_complete_check},
8130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008132 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008133#ifdef FEAT_FLOAT
8134 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008135 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008136#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008137 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008139 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008140 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008141 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008143 {"diff_filler", 1, 1, f_diff_filler},
8144 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008145 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008146 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008148 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"eventhandler", 0, 0, f_eventhandler},
8150 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008151 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008153#ifdef FEAT_FLOAT
8154 {"exp", 1, 1, f_exp},
8155#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008156 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008157 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008158 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8160 {"filereadable", 1, 1, f_filereadable},
8161 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008162 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008163 {"finddir", 1, 3, f_finddir},
8164 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165#ifdef FEAT_FLOAT
8166 {"float2nr", 1, 1, f_float2nr},
8167 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008168 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008169#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008170 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"fnamemodify", 2, 2, f_fnamemodify},
8172 {"foldclosed", 1, 1, f_foldclosed},
8173 {"foldclosedend", 1, 1, f_foldclosedend},
8174 {"foldlevel", 1, 1, f_foldlevel},
8175 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008176 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008178 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008179 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008180 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008181 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008182 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"getchar", 0, 1, f_getchar},
8184 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008185 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"getcmdline", 0, 0, f_getcmdline},
8187 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008188 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008189 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008190 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008191 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008192 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008193 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"getfsize", 1, 1, f_getfsize},
8195 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008196 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008197 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008198 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008199 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008200 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008201 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008202 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008203 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008205 {"gettabvar", 2, 3, f_gettabvar},
8206 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"getwinposx", 0, 0, f_getwinposx},
8208 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008209 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008210 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008211 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008212 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008214 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008215 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008216 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8218 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8219 {"histadd", 2, 2, f_histadd},
8220 {"histdel", 1, 2, f_histdel},
8221 {"histget", 1, 2, f_histget},
8222 {"histnr", 1, 1, f_histnr},
8223 {"hlID", 1, 1, f_hlID},
8224 {"hlexists", 1, 1, f_hlexists},
8225 {"hostname", 0, 0, f_hostname},
8226 {"iconv", 3, 3, f_iconv},
8227 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008228 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008229 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008231 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {"inputrestore", 0, 0, f_inputrestore},
8233 {"inputsave", 0, 0, f_inputsave},
8234 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008235 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008236 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008238 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008239 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008240#ifdef FEAT_JOB
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008241 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008242 {"job_start", 1, 2, f_job_start},
8243 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008244 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008245#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008246 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008247 {"js_decode", 1, 1, f_js_decode},
8248 {"js_encode", 1, 1, f_js_encode},
8249 {"json_decode", 1, 1, f_json_decode},
8250 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008251 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008253 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {"libcall", 3, 3, f_libcall},
8255 {"libcallnr", 3, 3, f_libcallnr},
8256 {"line", 1, 1, f_line},
8257 {"line2byte", 1, 1, f_line2byte},
8258 {"lispindent", 1, 1, f_lispindent},
8259 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008260#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008261 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008262 {"log10", 1, 1, f_log10},
8263#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008264#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008265 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008266#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008267 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008268 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008269 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008270 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008271 {"matchadd", 2, 5, f_matchadd},
8272 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008273 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008274 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008275 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008276 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008277 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008278 {"max", 1, 1, f_max},
8279 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008280#ifdef vim_mkdir
8281 {"mkdir", 1, 3, f_mkdir},
8282#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008283 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008284#ifdef FEAT_MZSCHEME
8285 {"mzeval", 1, 1, f_mzeval},
8286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008288 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008289 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008290 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008291#ifdef FEAT_PERL
8292 {"perleval", 1, 1, f_perleval},
8293#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008294#ifdef FEAT_FLOAT
8295 {"pow", 2, 2, f_pow},
8296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008298 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008299 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008300#ifdef FEAT_PYTHON3
8301 {"py3eval", 1, 1, f_py3eval},
8302#endif
8303#ifdef FEAT_PYTHON
8304 {"pyeval", 1, 1, f_pyeval},
8305#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008306 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008307 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008308 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008309 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008310 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"remote_expr", 2, 3, f_remote_expr},
8312 {"remote_foreground", 1, 1, f_remote_foreground},
8313 {"remote_peek", 1, 2, f_remote_peek},
8314 {"remote_read", 1, 1, f_remote_read},
8315 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008316 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008318 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008320 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008321#ifdef FEAT_FLOAT
8322 {"round", 1, 1, f_round},
8323#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008324 {"screenattr", 2, 2, f_screenattr},
8325 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008326 {"screencol", 0, 0, f_screencol},
8327 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008328 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008329 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008330 {"searchpair", 3, 7, f_searchpair},
8331 {"searchpairpos", 3, 7, f_searchpairpos},
8332 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"server2client", 2, 2, f_server2client},
8334 {"serverlist", 0, 0, f_serverlist},
8335 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008336 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {"setcmdpos", 1, 1, f_setcmdpos},
8338 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008339 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008340 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008341 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008342 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008344 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008345 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008347#ifdef FEAT_CRYPT
8348 {"sha256", 1, 1, f_sha256},
8349#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008350 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008351 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008353#ifdef FEAT_FLOAT
8354 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008355 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008356#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008357 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008358 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008359 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008360 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008361 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008362#ifdef FEAT_FLOAT
8363 {"sqrt", 1, 1, f_sqrt},
8364 {"str2float", 1, 1, f_str2float},
8365#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008366 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008367 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008368 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369#ifdef HAVE_STRFTIME
8370 {"strftime", 1, 2, f_strftime},
8371#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008373 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {"strlen", 1, 1, f_strlen},
8375 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008376 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008378 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008379 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {"substitute", 4, 4, f_substitute},
8381 {"synID", 3, 3, f_synID},
8382 {"synIDattr", 2, 3, f_synIDattr},
8383 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008384 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008385 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008386 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008387 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008388 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008389 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008390 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008391 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008392 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008393#ifdef FEAT_FLOAT
8394 {"tan", 1, 1, f_tan},
8395 {"tanh", 1, 1, f_tanh},
8396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008398 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"tolower", 1, 1, f_tolower},
8400 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008401 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008402#ifdef FEAT_FLOAT
8403 {"trunc", 1, 1, f_trunc},
8404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008406 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008407 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008408 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008409 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {"virtcol", 1, 1, f_virtcol},
8411 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008412 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {"winbufnr", 1, 1, f_winbufnr},
8414 {"wincol", 0, 0, f_wincol},
8415 {"winheight", 1, 1, f_winheight},
8416 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008417 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008419 {"winrestview", 1, 1, f_winrestview},
8420 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008422 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008423 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008424 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425};
8426
8427#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8428
8429/*
8430 * Function given to ExpandGeneric() to obtain the list of internal
8431 * or user defined function names.
8432 */
8433 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008434get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435{
8436 static int intidx = -1;
8437 char_u *name;
8438
8439 if (idx == 0)
8440 intidx = -1;
8441 if (intidx < 0)
8442 {
8443 name = get_user_func_name(xp, idx);
8444 if (name != NULL)
8445 return name;
8446 }
8447 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8448 {
8449 STRCPY(IObuff, functions[intidx].f_name);
8450 STRCAT(IObuff, "(");
8451 if (functions[intidx].f_max_argc == 0)
8452 STRCAT(IObuff, ")");
8453 return IObuff;
8454 }
8455
8456 return NULL;
8457}
8458
8459/*
8460 * Function given to ExpandGeneric() to obtain the list of internal or
8461 * user defined variable or function names.
8462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008464get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465{
8466 static int intidx = -1;
8467 char_u *name;
8468
8469 if (idx == 0)
8470 intidx = -1;
8471 if (intidx < 0)
8472 {
8473 name = get_function_name(xp, idx);
8474 if (name != NULL)
8475 return name;
8476 }
8477 return get_user_var_name(xp, ++intidx);
8478}
8479
8480#endif /* FEAT_CMDL_COMPL */
8481
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008482#if defined(EBCDIC) || defined(PROTO)
8483/*
8484 * Compare struct fst by function name.
8485 */
8486 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008487compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008488{
8489 struct fst *p1 = (struct fst *)s1;
8490 struct fst *p2 = (struct fst *)s2;
8491
8492 return STRCMP(p1->f_name, p2->f_name);
8493}
8494
8495/*
8496 * Sort the function table by function name.
8497 * The sorting of the table above is ASCII dependant.
8498 * On machines using EBCDIC we have to sort it.
8499 */
8500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008501sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008502{
8503 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8504
8505 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8506}
8507#endif
8508
8509
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510/*
8511 * Find internal function in table above.
8512 * Return index, or -1 if not found
8513 */
8514 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008515find_internal_func(
8516 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
8518 int first = 0;
8519 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8520 int cmp;
8521 int x;
8522
8523 /*
8524 * Find the function name in the table. Binary search.
8525 */
8526 while (first <= last)
8527 {
8528 x = first + ((unsigned)(last - first) >> 1);
8529 cmp = STRCMP(name, functions[x].f_name);
8530 if (cmp < 0)
8531 last = x - 1;
8532 else if (cmp > 0)
8533 first = x + 1;
8534 else
8535 return x;
8536 }
8537 return -1;
8538}
8539
8540/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008541 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8542 * name it contains, otherwise return "name".
8543 */
8544 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008545deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008546{
Bram Moolenaar33570922005-01-25 22:26:29 +00008547 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008548 int cc;
8549
8550 cc = name[*lenp];
8551 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008552 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008553 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008554 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008555 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008556 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008557 {
8558 *lenp = 0;
8559 return (char_u *)""; /* just in case */
8560 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008561 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008562 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008563 }
8564
8565 return name;
8566}
8567
8568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 * Allocate a variable for the result of a function.
8570 * Return OK or FAIL.
8571 */
8572 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008573get_func_tv(
8574 char_u *name, /* name of the function */
8575 int len, /* length of "name" */
8576 typval_T *rettv,
8577 char_u **arg, /* argument, pointing to the '(' */
8578 linenr_T firstline, /* first line of range */
8579 linenr_T lastline, /* last line of range */
8580 int *doesrange, /* return: function handled range */
8581 int evaluate,
8582 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583{
8584 char_u *argp;
8585 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008586 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 int argcount = 0; /* number of arguments found */
8588
8589 /*
8590 * Get the arguments.
8591 */
8592 argp = *arg;
8593 while (argcount < MAX_FUNC_ARGS)
8594 {
8595 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8596 if (*argp == ')' || *argp == ',' || *argp == NUL)
8597 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8599 {
8600 ret = FAIL;
8601 break;
8602 }
8603 ++argcount;
8604 if (*argp != ',')
8605 break;
8606 }
8607 if (*argp == ')')
8608 ++argp;
8609 else
8610 ret = FAIL;
8611
8612 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008613 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008614 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008616 {
8617 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008618 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008619 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008620 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622
8623 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625
8626 *arg = skipwhite(argp);
8627 return ret;
8628}
8629
8630
8631/*
8632 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008633 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008634 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008636 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008637call_func(
8638 char_u *funcname, /* name of the function */
8639 int len, /* length of "name" */
8640 typval_T *rettv, /* return value goes here */
8641 int argcount, /* number of "argvars" */
8642 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008643 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008644 linenr_T firstline, /* first line of range */
8645 linenr_T lastline, /* last line of range */
8646 int *doesrange, /* return: function handled range */
8647 int evaluate,
8648 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649{
8650 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651#define ERROR_UNKNOWN 0
8652#define ERROR_TOOMANY 1
8653#define ERROR_TOOFEW 2
8654#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008655#define ERROR_DICT 4
8656#define ERROR_NONE 5
8657#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 int error = ERROR_NONE;
8659 int i;
8660 int llen;
8661 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662#define FLEN_FIXED 40
8663 char_u fname_buf[FLEN_FIXED + 1];
8664 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008665 char_u *name;
8666
8667 /* Make a copy of the name, if it comes from a funcref variable it could
8668 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008669 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008670 if (name == NULL)
8671 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672
8673 /*
8674 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8675 * Change <SNR>123_name() to K_SNR 123_name().
8676 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 llen = eval_fname_script(name);
8679 if (llen > 0)
8680 {
8681 fname_buf[0] = K_SPECIAL;
8682 fname_buf[1] = KS_EXTRA;
8683 fname_buf[2] = (int)KE_SNR;
8684 i = 3;
8685 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8686 {
8687 if (current_SID <= 0)
8688 error = ERROR_SCRIPT;
8689 else
8690 {
8691 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8692 i = (int)STRLEN(fname_buf);
8693 }
8694 }
8695 if (i + STRLEN(name + llen) < FLEN_FIXED)
8696 {
8697 STRCPY(fname_buf + i, name + llen);
8698 fname = fname_buf;
8699 }
8700 else
8701 {
8702 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8703 if (fname == NULL)
8704 error = ERROR_OTHER;
8705 else
8706 {
8707 mch_memmove(fname, fname_buf, (size_t)i);
8708 STRCPY(fname + i, name + llen);
8709 }
8710 }
8711 }
8712 else
8713 fname = name;
8714
8715 *doesrange = FALSE;
8716
8717
8718 /* execute the function if no errors detected and executing */
8719 if (evaluate && error == ERROR_NONE)
8720 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008721 char_u *rfname = fname;
8722
8723 /* Ignore "g:" before a function name. */
8724 if (fname[0] == 'g' && fname[1] == ':')
8725 rfname = fname + 2;
8726
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008727 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8728 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 error = ERROR_UNKNOWN;
8730
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008731 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 {
8733 /*
8734 * User defined function.
8735 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008736 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008737
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008739 /* Trigger FuncUndefined event, may load the function. */
8740 if (fp == NULL
8741 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008742 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008743 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008745 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008746 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 }
8748#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008749 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008750 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008751 {
8752 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008753 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008754 }
8755
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 if (fp != NULL)
8757 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008758 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008760 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008762 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008764 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008765 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 else
8767 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008768 int did_save_redo = FALSE;
8769
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 /*
8771 * Call the user function.
8772 * Save and restore search patterns, script variables and
8773 * redo buffer.
8774 */
8775 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008776#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008777 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008778#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008779 {
8780 saveRedobuff();
8781 did_save_redo = TRUE;
8782 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008783 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008785 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008786 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8787 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8788 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008789 /* Function was unreferenced while being used, free it
8790 * now. */
8791 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008792 if (did_save_redo)
8793 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 restore_search_patterns();
8795 error = ERROR_NONE;
8796 }
8797 }
8798 }
8799 else
8800 {
8801 /*
8802 * Find the function name in the table, call its implementation.
8803 */
8804 i = find_internal_func(fname);
8805 if (i >= 0)
8806 {
8807 if (argcount < functions[i].f_min_argc)
8808 error = ERROR_TOOFEW;
8809 else if (argcount > functions[i].f_max_argc)
8810 error = ERROR_TOOMANY;
8811 else
8812 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008813 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 error = ERROR_NONE;
8816 }
8817 }
8818 }
8819 /*
8820 * The function call (or "FuncUndefined" autocommand sequence) might
8821 * have been aborted by an error, an interrupt, or an explicitly thrown
8822 * exception that has not been caught so far. This situation can be
8823 * tested for by calling aborting(). For an error in an internal
8824 * function or for the "E132" error in call_user_func(), however, the
8825 * throw point at which the "force_abort" flag (temporarily reset by
8826 * emsg()) is normally updated has not been reached yet. We need to
8827 * update that flag first to make aborting() reliable.
8828 */
8829 update_force_abort();
8830 }
8831 if (error == ERROR_NONE)
8832 ret = OK;
8833
8834 /*
8835 * Report an error unless the argument evaluation or function call has been
8836 * cancelled due to an aborting error, an interrupt, or an exception.
8837 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008838 if (!aborting())
8839 {
8840 switch (error)
8841 {
8842 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008843 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008844 break;
8845 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008846 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008847 break;
8848 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008849 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008850 name);
8851 break;
8852 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008853 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008854 name);
8855 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008856 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008857 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008858 name);
8859 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008860 }
8861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 if (fname != name && fname != fname_buf)
8864 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008865 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866
8867 return ret;
8868}
8869
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008870/*
8871 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008872 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008873 */
8874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008875emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008876{
8877 char_u *p;
8878
8879 if (*name == K_SPECIAL)
8880 p = concat_str((char_u *)"<SNR>", name + 3);
8881 else
8882 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008883 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008884 if (p != name)
8885 vim_free(p);
8886}
8887
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008888/*
8889 * Return TRUE for a non-zero Number and a non-empty String.
8890 */
8891 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008892non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008893{
8894 return ((argvars[0].v_type == VAR_NUMBER
8895 && argvars[0].vval.v_number != 0)
8896 || (argvars[0].v_type == VAR_STRING
8897 && argvars[0].vval.v_string != NULL
8898 && *argvars[0].vval.v_string != NUL));
8899}
8900
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901/*********************************************
8902 * Implementation of the built-in functions
8903 */
8904
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008905#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008906static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008907
8908/*
8909 * Get the float value of "argvars[0]" into "f".
8910 * Returns FAIL when the argument is not a Number or Float.
8911 */
8912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008913get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008914{
8915 if (argvars[0].v_type == VAR_FLOAT)
8916 {
8917 *f = argvars[0].vval.v_float;
8918 return OK;
8919 }
8920 if (argvars[0].v_type == VAR_NUMBER)
8921 {
8922 *f = (float_T)argvars[0].vval.v_number;
8923 return OK;
8924 }
8925 EMSG(_("E808: Number or Float required"));
8926 return FAIL;
8927}
8928
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008929/*
8930 * "abs(expr)" function
8931 */
8932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008933f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008934{
8935 if (argvars[0].v_type == VAR_FLOAT)
8936 {
8937 rettv->v_type = VAR_FLOAT;
8938 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8939 }
8940 else
8941 {
8942 varnumber_T n;
8943 int error = FALSE;
8944
8945 n = get_tv_number_chk(&argvars[0], &error);
8946 if (error)
8947 rettv->vval.v_number = -1;
8948 else if (n > 0)
8949 rettv->vval.v_number = n;
8950 else
8951 rettv->vval.v_number = -n;
8952 }
8953}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008954
8955/*
8956 * "acos()" function
8957 */
8958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008959f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008960{
8961 float_T f;
8962
8963 rettv->v_type = VAR_FLOAT;
8964 if (get_float_arg(argvars, &f) == OK)
8965 rettv->vval.v_float = acos(f);
8966 else
8967 rettv->vval.v_float = 0.0;
8968}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008969#endif
8970
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008972 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 */
8974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008975f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008980 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008982 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008983 && !tv_check_lock(l->lv_lock,
8984 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008985 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008987 }
8988 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989 EMSG(_(e_listreq));
8990}
8991
8992/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008993 * "alloc_fail(id, countdown, repeat)" function
8994 */
8995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008996f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008997{
8998 if (argvars[0].v_type != VAR_NUMBER
8999 || argvars[0].vval.v_number <= 0
9000 || argvars[1].v_type != VAR_NUMBER
9001 || argvars[1].vval.v_number < 0
9002 || argvars[2].v_type != VAR_NUMBER)
9003 EMSG(_(e_invarg));
9004 else
9005 {
9006 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009007 if (alloc_fail_id >= aid_last)
9008 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009009 alloc_fail_countdown = argvars[1].vval.v_number;
9010 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009011 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009012 }
9013}
9014
9015/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009016 * "and(expr, expr)" function
9017 */
9018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009019f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009020{
9021 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9022 & get_tv_number_chk(&argvars[1], NULL);
9023}
9024
9025/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009026 * "append(lnum, string/list)" function
9027 */
9028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009029f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009030{
9031 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009032 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009033 list_T *l = NULL;
9034 listitem_T *li = NULL;
9035 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009036 long added = 0;
9037
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009038 /* When coming here from Insert mode, sync undo, so that this can be
9039 * undone separately from what was previously inserted. */
9040 if (u_sync_once == 2)
9041 {
9042 u_sync_once = 1; /* notify that u_sync() was called */
9043 u_sync(TRUE);
9044 }
9045
Bram Moolenaar0d660222005-01-07 21:51:51 +00009046 lnum = get_tv_lnum(argvars);
9047 if (lnum >= 0
9048 && lnum <= curbuf->b_ml.ml_line_count
9049 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009050 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009051 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009052 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009053 l = argvars[1].vval.v_list;
9054 if (l == NULL)
9055 return;
9056 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009057 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009058 for (;;)
9059 {
9060 if (l == NULL)
9061 tv = &argvars[1]; /* append a string */
9062 else if (li == NULL)
9063 break; /* end of list */
9064 else
9065 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009066 line = get_tv_string_chk(tv);
9067 if (line == NULL) /* type error */
9068 {
9069 rettv->vval.v_number = 1; /* Failed */
9070 break;
9071 }
9072 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009073 ++added;
9074 if (l == NULL)
9075 break;
9076 li = li->li_next;
9077 }
9078
9079 appended_lines_mark(lnum, added);
9080 if (curwin->w_cursor.lnum > lnum)
9081 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009083 else
9084 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085}
9086
9087/*
9088 * "argc()" function
9089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009091f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094}
9095
9096/*
9097 * "argidx()" function
9098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009100f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103}
9104
9105/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009106 * "arglistid()" function
9107 */
9108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009109f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009110{
9111 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009112
9113 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009114 wp = find_tabwin(&argvars[0], &argvars[1]);
9115 if (wp != NULL)
9116 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009117}
9118
9119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 * "argv(nr)" function
9121 */
9122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009123f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
9125 int idx;
9126
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009127 if (argvars[0].v_type != VAR_UNKNOWN)
9128 {
9129 idx = get_tv_number_chk(&argvars[0], NULL);
9130 if (idx >= 0 && idx < ARGCOUNT)
9131 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9132 else
9133 rettv->vval.v_string = NULL;
9134 rettv->v_type = VAR_STRING;
9135 }
9136 else if (rettv_list_alloc(rettv) == OK)
9137 for (idx = 0; idx < ARGCOUNT; ++idx)
9138 list_append_string(rettv->vval.v_list,
9139 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140}
9141
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009142static void prepare_assert_error(garray_T*gap);
9143static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9144static void assert_error(garray_T *gap);
9145static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009146
9147/*
9148 * Prepare "gap" for an assert error and add the sourcing position.
9149 */
9150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009151prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009152{
9153 char buf[NUMBUFLEN];
9154
9155 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009156 if (sourcing_name != NULL)
9157 {
9158 ga_concat(gap, sourcing_name);
9159 if (sourcing_lnum > 0)
9160 ga_concat(gap, (char_u *)" ");
9161 }
9162 if (sourcing_lnum > 0)
9163 {
9164 sprintf(buf, "line %ld", (long)sourcing_lnum);
9165 ga_concat(gap, (char_u *)buf);
9166 }
9167 if (sourcing_name != NULL || sourcing_lnum > 0)
9168 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009169}
9170
9171/*
9172 * Fill "gap" with information about an assert error.
9173 */
9174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009175fill_assert_error(
9176 garray_T *gap,
9177 typval_T *opt_msg_tv,
9178 char_u *exp_str,
9179 typval_T *exp_tv,
9180 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009181{
9182 char_u numbuf[NUMBUFLEN];
9183 char_u *tofree;
9184
9185 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9186 {
9187 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9188 vim_free(tofree);
9189 }
9190 else
9191 {
9192 ga_concat(gap, (char_u *)"Expected ");
9193 if (exp_str == NULL)
9194 {
9195 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9196 vim_free(tofree);
9197 }
9198 else
9199 ga_concat(gap, exp_str);
9200 ga_concat(gap, (char_u *)" but got ");
9201 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9202 vim_free(tofree);
9203 }
9204}
Bram Moolenaar43345542015-11-29 17:35:35 +01009205
9206/*
9207 * Add an assert error to v:errors.
9208 */
9209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009210assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009211{
9212 struct vimvar *vp = &vimvars[VV_ERRORS];
9213
9214 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9215 /* Make sure v:errors is a list. */
9216 set_vim_var_list(VV_ERRORS, list_alloc());
9217 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9218}
9219
Bram Moolenaar43345542015-11-29 17:35:35 +01009220/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009221 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009222 */
9223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009224f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009225{
9226 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009227
9228 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9229 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009230 prepare_assert_error(&ga);
9231 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9232 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009233 ga_clear(&ga);
9234 }
9235}
9236
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009237/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009238 * "assert_exception(string[, msg])" function
9239 */
9240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009241f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009242{
9243 garray_T ga;
9244 char *error;
9245
9246 error = (char *)get_tv_string_chk(&argvars[0]);
9247 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9248 {
9249 prepare_assert_error(&ga);
9250 ga_concat(&ga, (char_u *)"v:exception is not set");
9251 assert_error(&ga);
9252 ga_clear(&ga);
9253 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009254 else if (error != NULL
9255 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009256 {
9257 prepare_assert_error(&ga);
9258 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9259 &vimvars[VV_EXCEPTION].vv_tv);
9260 assert_error(&ga);
9261 ga_clear(&ga);
9262 }
9263}
9264
9265/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009266 * "assert_fails(cmd [, error])" function
9267 */
9268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009269f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009270{
9271 char_u *cmd = get_tv_string_chk(&argvars[0]);
9272 garray_T ga;
9273
9274 called_emsg = FALSE;
9275 suppress_errthrow = TRUE;
9276 emsg_silent = TRUE;
9277 do_cmdline_cmd(cmd);
9278 if (!called_emsg)
9279 {
9280 prepare_assert_error(&ga);
9281 ga_concat(&ga, (char_u *)"command did not fail: ");
9282 ga_concat(&ga, cmd);
9283 assert_error(&ga);
9284 ga_clear(&ga);
9285 }
9286 else if (argvars[1].v_type != VAR_UNKNOWN)
9287 {
9288 char_u buf[NUMBUFLEN];
9289 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9290
9291 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9292 {
9293 prepare_assert_error(&ga);
9294 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9295 &vimvars[VV_ERRMSG].vv_tv);
9296 assert_error(&ga);
9297 ga_clear(&ga);
9298 }
9299 }
9300
9301 called_emsg = FALSE;
9302 suppress_errthrow = FALSE;
9303 emsg_silent = FALSE;
9304 emsg_on_display = FALSE;
9305 set_vim_var_string(VV_ERRMSG, NULL, 0);
9306}
9307
9308/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009309 * Common for assert_true() and assert_false().
9310 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009312assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009313{
9314 int error = FALSE;
9315 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009316
Bram Moolenaar37127922016-02-06 20:29:28 +01009317 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009318 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009319 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009320 if (argvars[0].v_type != VAR_NUMBER
9321 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9322 || error)
9323 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009324 prepare_assert_error(&ga);
9325 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009326 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009327 NULL, &argvars[0]);
9328 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009329 ga_clear(&ga);
9330 }
9331}
9332
9333/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009334 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009335 */
9336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009337f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009338{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009339 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009340}
9341
9342/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009343 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009344 */
9345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009346f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009347{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009348 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009349}
9350
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009351#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009352/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009353 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009354 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009356f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009357{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009358 float_T f;
9359
9360 rettv->v_type = VAR_FLOAT;
9361 if (get_float_arg(argvars, &f) == OK)
9362 rettv->vval.v_float = asin(f);
9363 else
9364 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009365}
9366
9367/*
9368 * "atan()" function
9369 */
9370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009371f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009372{
9373 float_T f;
9374
9375 rettv->v_type = VAR_FLOAT;
9376 if (get_float_arg(argvars, &f) == OK)
9377 rettv->vval.v_float = atan(f);
9378 else
9379 rettv->vval.v_float = 0.0;
9380}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009381
9382/*
9383 * "atan2()" function
9384 */
9385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009386f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009387{
9388 float_T fx, fy;
9389
9390 rettv->v_type = VAR_FLOAT;
9391 if (get_float_arg(argvars, &fx) == OK
9392 && get_float_arg(&argvars[1], &fy) == OK)
9393 rettv->vval.v_float = atan2(fx, fy);
9394 else
9395 rettv->vval.v_float = 0.0;
9396}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009397#endif
9398
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399/*
9400 * "browse(save, title, initdir, default)" function
9401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009403f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404{
9405#ifdef FEAT_BROWSE
9406 int save;
9407 char_u *title;
9408 char_u *initdir;
9409 char_u *defname;
9410 char_u buf[NUMBUFLEN];
9411 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009414 save = get_tv_number_chk(&argvars[0], &error);
9415 title = get_tv_string_chk(&argvars[1]);
9416 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9417 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009419 if (error || title == NULL || initdir == NULL || defname == NULL)
9420 rettv->vval.v_string = NULL;
9421 else
9422 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009423 do_browse(save ? BROWSE_SAVE : 0,
9424 title, defname, NULL, initdir, NULL, curbuf);
9425#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009427#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009429}
9430
9431/*
9432 * "browsedir(title, initdir)" function
9433 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009435f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009436{
9437#ifdef FEAT_BROWSE
9438 char_u *title;
9439 char_u *initdir;
9440 char_u buf[NUMBUFLEN];
9441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009442 title = get_tv_string_chk(&argvars[0]);
9443 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009445 if (title == NULL || initdir == NULL)
9446 rettv->vval.v_string = NULL;
9447 else
9448 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009449 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009453 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454}
9455
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009456static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009457
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458/*
9459 * Find a buffer by number or exact name.
9460 */
9461 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009462find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463{
9464 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009466 if (avar->v_type == VAR_NUMBER)
9467 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009468 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009470 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009471 if (buf == NULL)
9472 {
9473 /* No full path name match, try a match with a URL or a "nofile"
9474 * buffer, these don't use the full path. */
9475 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9476 if (buf->b_fname != NULL
9477 && (path_with_url(buf->b_fname)
9478#ifdef FEAT_QUICKFIX
9479 || bt_nofile(buf)
9480#endif
9481 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009482 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009483 break;
9484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 }
9486 return buf;
9487}
9488
9489/*
9490 * "bufexists(expr)" function
9491 */
9492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009493f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496}
9497
9498/*
9499 * "buflisted(expr)" function
9500 */
9501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009502f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503{
9504 buf_T *buf;
9505
9506 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508}
9509
9510/*
9511 * "bufloaded(expr)" function
9512 */
9513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009514f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516 buf_T *buf;
9517
9518 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520}
9521
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009522static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009523
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524/*
9525 * Get buffer by number or pattern.
9526 */
9527 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009528get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 int save_magic;
9532 char_u *save_cpo;
9533 buf_T *buf;
9534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 if (tv->v_type == VAR_NUMBER)
9536 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009537 if (tv->v_type != VAR_STRING)
9538 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 if (name == NULL || *name == NUL)
9540 return curbuf;
9541 if (name[0] == '$' && name[1] == NUL)
9542 return lastbuf;
9543
9544 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9545 save_magic = p_magic;
9546 p_magic = TRUE;
9547 save_cpo = p_cpo;
9548 p_cpo = (char_u *)"";
9549
9550 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009551 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552
9553 p_magic = save_magic;
9554 p_cpo = save_cpo;
9555
9556 /* If not found, try expanding the name, like done for bufexists(). */
9557 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559
9560 return buf;
9561}
9562
9563/*
9564 * "bufname(expr)" function
9565 */
9566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009567f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569 buf_T *buf;
9570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009571 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009573 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 --emsg_off;
9580}
9581
9582/*
9583 * "bufnr(expr)" function
9584 */
9585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009586f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009589 int error = FALSE;
9590 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009594 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009595 --emsg_off;
9596
9597 /* If the buffer isn't found and the second argument is not zero create a
9598 * new buffer. */
9599 if (buf == NULL
9600 && argvars[1].v_type != VAR_UNKNOWN
9601 && get_tv_number_chk(&argvars[1], &error) != 0
9602 && !error
9603 && (name = get_tv_string_chk(&argvars[0])) != NULL
9604 && !error)
9605 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9606
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611}
9612
9613/*
9614 * "bufwinnr(nr)" function
9615 */
9616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009617f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
9619#ifdef FEAT_WINDOWS
9620 win_T *wp;
9621 int winnr = 0;
9622#endif
9623 buf_T *buf;
9624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009625 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009627 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628#ifdef FEAT_WINDOWS
9629 for (wp = firstwin; wp; wp = wp->w_next)
9630 {
9631 ++winnr;
9632 if (wp->w_buffer == buf)
9633 break;
9634 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638#endif
9639 --emsg_off;
9640}
9641
9642/*
9643 * "byte2line(byte)" function
9644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009646f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647{
9648#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650#else
9651 long boff = 0;
9652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009653 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 (linenr_T)0, &boff);
9659#endif
9660}
9661
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009663byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009664{
9665#ifdef FEAT_MBYTE
9666 char_u *t;
9667#endif
9668 char_u *str;
9669 long idx;
9670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009671 str = get_tv_string_chk(&argvars[0]);
9672 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009674 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009675 return;
9676
9677#ifdef FEAT_MBYTE
9678 t = str;
9679 for ( ; idx > 0; idx--)
9680 {
9681 if (*t == NUL) /* EOL reached */
9682 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009683 if (enc_utf8 && comp)
9684 t += utf_ptr2len(t);
9685 else
9686 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009687 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009688 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009689#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009690 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009692#endif
9693}
9694
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009695/*
9696 * "byteidx()" function
9697 */
9698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009699f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009700{
9701 byteidx(argvars, rettv, FALSE);
9702}
9703
9704/*
9705 * "byteidxcomp()" function
9706 */
9707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009708f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009709{
9710 byteidx(argvars, rettv, TRUE);
9711}
9712
Bram Moolenaardb913952012-06-29 12:54:53 +02009713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009714func_call(
9715 char_u *name,
9716 typval_T *args,
9717 dict_T *selfdict,
9718 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009719{
9720 listitem_T *item;
9721 typval_T argv[MAX_FUNC_ARGS + 1];
9722 int argc = 0;
9723 int dummy;
9724 int r = 0;
9725
9726 for (item = args->vval.v_list->lv_first; item != NULL;
9727 item = item->li_next)
9728 {
9729 if (argc == MAX_FUNC_ARGS)
9730 {
9731 EMSG(_("E699: Too many arguments"));
9732 break;
9733 }
9734 /* Make a copy of each argument. This is needed to be able to set
9735 * v_lock to VAR_FIXED in the copy without changing the original list.
9736 */
9737 copy_tv(&item->li_tv, &argv[argc++]);
9738 }
9739
9740 if (item == NULL)
9741 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9742 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9743 &dummy, TRUE, selfdict);
9744
9745 /* Free the arguments. */
9746 while (argc > 0)
9747 clear_tv(&argv[--argc]);
9748
9749 return r;
9750}
9751
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009752/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009753 * "call(func, arglist)" function
9754 */
9755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009756f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009757{
9758 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009759 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009760
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009761 if (argvars[1].v_type != VAR_LIST)
9762 {
9763 EMSG(_(e_listreq));
9764 return;
9765 }
9766 if (argvars[1].vval.v_list == NULL)
9767 return;
9768
9769 if (argvars[0].v_type == VAR_FUNC)
9770 func = argvars[0].vval.v_string;
9771 else
9772 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009773 if (*func == NUL)
9774 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009775
Bram Moolenaare9a41262005-01-15 22:18:47 +00009776 if (argvars[2].v_type != VAR_UNKNOWN)
9777 {
9778 if (argvars[2].v_type != VAR_DICT)
9779 {
9780 EMSG(_(e_dictreq));
9781 return;
9782 }
9783 selfdict = argvars[2].vval.v_dict;
9784 }
9785
Bram Moolenaardb913952012-06-29 12:54:53 +02009786 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009787}
9788
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009789#ifdef FEAT_FLOAT
9790/*
9791 * "ceil({float})" function
9792 */
9793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009794f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009795{
9796 float_T f;
9797
9798 rettv->v_type = VAR_FLOAT;
9799 if (get_float_arg(argvars, &f) == OK)
9800 rettv->vval.v_float = ceil(f);
9801 else
9802 rettv->vval.v_float = 0.0;
9803}
9804#endif
9805
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009806#ifdef FEAT_CHANNEL
9807/*
Bram Moolenaar77073442016-02-13 23:23:53 +01009808 * Get the channel from the argument.
9809 * Returns NULL if the handle is invalid.
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009810 */
Bram Moolenaar77073442016-02-13 23:23:53 +01009811 static channel_T *
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009812get_channel_arg(typval_T *tv)
9813{
Bram Moolenaar77073442016-02-13 23:23:53 +01009814 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009815
Bram Moolenaar77073442016-02-13 23:23:53 +01009816 if (tv->v_type != VAR_CHANNEL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009817 {
9818 EMSG2(_(e_invarg2), get_tv_string(tv));
Bram Moolenaar77073442016-02-13 23:23:53 +01009819 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009820 }
Bram Moolenaar77073442016-02-13 23:23:53 +01009821 channel = tv->vval.v_channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009822
Bram Moolenaar77073442016-02-13 23:23:53 +01009823 if (channel == NULL || !channel_is_open(channel))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009824 {
Bram Moolenaar77073442016-02-13 23:23:53 +01009825 EMSG(_("E906: not an open channel"));
9826 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009827 }
Bram Moolenaar77073442016-02-13 23:23:53 +01009828 return channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009829}
9830
9831/*
9832 * "ch_close()" function
9833 */
9834 static void
9835f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9836{
Bram Moolenaar77073442016-02-13 23:23:53 +01009837 channel_T *channel = get_channel_arg(&argvars[0]);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009838
Bram Moolenaar77073442016-02-13 23:23:53 +01009839 if (channel != NULL)
9840 channel_close(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009841}
9842
9843/*
9844 * Get a callback from "arg". It can be a Funcref or a function name.
9845 * When "arg" is zero return an empty string.
9846 * Return NULL for an invalid argument.
9847 */
9848 static char_u *
9849get_callback(typval_T *arg)
9850{
9851 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9852 return arg->vval.v_string;
9853 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9854 return (char_u *)"";
9855 EMSG(_("E999: Invalid callback argument"));
9856 return NULL;
9857}
9858
9859/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009860 * "ch_logfile()" function
9861 */
9862 static void
9863f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9864{
9865 char_u *fname;
9866 char_u *opt = (char_u *)"";
9867 char_u buf[NUMBUFLEN];
9868 FILE *file = NULL;
9869
9870 fname = get_tv_string(&argvars[0]);
9871 if (argvars[1].v_type == VAR_STRING)
9872 opt = get_tv_string_buf(&argvars[1], buf);
9873 if (*fname != NUL)
9874 {
9875 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
9876 if (file == NULL)
9877 {
9878 EMSG2(_(e_notopen), fname);
9879 return;
9880 }
9881 }
9882 ch_logfile(file);
9883}
9884
9885/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009886 * "ch_open()" function
9887 */
9888 static void
9889f_ch_open(typval_T *argvars, typval_T *rettv)
9890{
9891 char_u *address;
9892 char_u *mode;
9893 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009894 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009895 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009896 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009897 int waittime = 0;
9898 int timeout = 2000;
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009899 ch_mode_T ch_mode = MODE_JSON;
Bram Moolenaar77073442016-02-13 23:23:53 +01009900 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009901
9902 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +01009903 rettv->v_type = VAR_CHANNEL;
9904 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009905
9906 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009907 if (argvars[1].v_type != VAR_UNKNOWN
9908 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009909 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009910 EMSG(_(e_invarg));
9911 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009912 }
9913
9914 /* parse address */
9915 p = vim_strchr(address, ':');
9916 if (p == NULL)
9917 {
9918 EMSG2(_(e_invarg2), address);
9919 return;
9920 }
9921 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009922 port = strtol((char *)p, &rest, 10);
9923 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009924 {
9925 p[-1] = ':';
9926 EMSG2(_(e_invarg2), address);
9927 return;
9928 }
9929
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009930 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009931 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009932 dict_T *dict = argvars[1].vval.v_dict;
9933 dictitem_T *item;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009934
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009935 /* parse argdict */
9936 if ((item = dict_find(dict, (char_u *)"mode", -1)) != NULL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009937 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009938 mode = get_tv_string(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009939 if (STRCMP(mode, "raw") == 0)
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009940 ch_mode = MODE_RAW;
9941 else if (STRCMP(mode, "js") == 0)
9942 ch_mode = MODE_JS;
9943 else if (STRCMP(mode, "json") == 0)
9944 ch_mode = MODE_JSON;
9945 else
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009946 {
9947 EMSG2(_(e_invarg2), mode);
9948 return;
9949 }
9950 }
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009951 if ((item = dict_find(dict, (char_u *)"waittime", -1)) != NULL)
9952 waittime = get_tv_number(&item->di_tv);
9953 if ((item = dict_find(dict, (char_u *)"timeout", -1)) != NULL)
9954 timeout = get_tv_number(&item->di_tv);
9955 if ((item = dict_find(dict, (char_u *)"callback", -1)) != NULL)
9956 callback = get_callback(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009957 }
9958 if (waittime < 0 || timeout < 0)
9959 {
9960 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009961 return;
9962 }
9963
Bram Moolenaar77073442016-02-13 23:23:53 +01009964 channel = channel_open((char *)address, port, waittime, NULL);
9965 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009966 {
Bram Moolenaar77073442016-02-13 23:23:53 +01009967 channel_set_json_mode(channel, ch_mode);
9968 channel_set_timeout(channel, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009969 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +01009970 channel_set_callback(channel, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009971 }
Bram Moolenaar77073442016-02-13 23:23:53 +01009972 rettv->vval.v_channel = channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009973}
9974
9975/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009976 * "ch_readraw()" function
9977 */
9978 static void
9979f_ch_readraw(typval_T *argvars, typval_T *rettv)
9980{
Bram Moolenaar77073442016-02-13 23:23:53 +01009981 channel_T *channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009982
9983 /* return an empty string by default */
9984 rettv->v_type = VAR_STRING;
9985 rettv->vval.v_string = NULL;
9986
Bram Moolenaar77073442016-02-13 23:23:53 +01009987 channel = get_channel_arg(&argvars[0]);
9988 if (channel != NULL)
9989 rettv->vval.v_string = channel_read_block(channel);
9990}
9991
9992/*
9993 * "ch_status()" function
9994 */
9995 static void
9996f_ch_status(typval_T *argvars, typval_T *rettv)
9997{
9998 /* return an empty string by default */
9999 rettv->v_type = VAR_STRING;
10000
10001 if (argvars[0].v_type != VAR_CHANNEL)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010002 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010003 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10004 rettv->vval.v_string = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010005 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010006 else
10007 rettv->vval.v_string = vim_strsave(
10008 (char_u *)channel_status(argvars[0].vval.v_channel));
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010009}
10010
10011/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010012 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010013 * Returns the channel if the caller should read the response.
10014 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010015 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010016 static channel_T *
Bram Moolenaara07fec92016-02-05 21:04:08 +010010017send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010018{
Bram Moolenaar77073442016-02-13 23:23:53 +010010019 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010020 char_u *callback = NULL;
10021
Bram Moolenaar77073442016-02-13 23:23:53 +010010022 channel = get_channel_arg(&argvars[0]);
10023 if (channel == NULL)
10024 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010025
10026 if (argvars[2].v_type != VAR_UNKNOWN)
10027 {
10028 callback = get_callback(&argvars[2]);
10029 if (callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010030 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010031 }
Bram Moolenaara07fec92016-02-05 21:04:08 +010010032 /* Set the callback. An empty callback means no callback and not reading
10033 * the response. */
10034 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010035 channel_set_req_callback(channel, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010036
Bram Moolenaar77073442016-02-13 23:23:53 +010010037 if (channel_send(channel, text, fun) == OK && callback == NULL)
10038 return channel;
10039 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010040}
10041
10042/*
10043 * "ch_sendexpr()" function
10044 */
10045 static void
10046f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10047{
10048 char_u *text;
10049 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010050 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010051 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010052 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010053
10054 /* return an empty string by default */
10055 rettv->v_type = VAR_STRING;
10056 rettv->vval.v_string = NULL;
10057
Bram Moolenaar77073442016-02-13 23:23:53 +010010058 channel = get_channel_arg(&argvars[0]);
10059 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010060 return;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010061
Bram Moolenaar77073442016-02-13 23:23:53 +010010062 ch_mode = channel_get_mode(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010063 if (ch_mode == MODE_RAW)
10064 {
10065 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
10066 return;
10067 }
10068
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010069 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010070 text = json_encode_nr_expr(id, &argvars[1],
10071 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010072 if (text == NULL)
10073 return;
10074
Bram Moolenaar77073442016-02-13 23:23:53 +010010075 channel = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010076 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010077 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010078 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010079 if (channel_read_json_block(channel, id, &listtv) == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010080 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010081 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010082
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010083 /* Move the item from the list and then change the type to
10084 * avoid the value being freed. */
10085 *rettv = list->lv_last->li_tv;
10086 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010087 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010088 }
10089 }
10090}
10091
10092/*
10093 * "ch_sendraw()" function
10094 */
10095 static void
10096f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10097{
10098 char_u buf[NUMBUFLEN];
10099 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010100 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010101
10102 /* return an empty string by default */
10103 rettv->v_type = VAR_STRING;
10104 rettv->vval.v_string = NULL;
10105
10106 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar77073442016-02-13 23:23:53 +010010107 channel = send_common(argvars, text, 0, "sendraw");
10108 if (channel != NULL)
10109 rettv->vval.v_string = channel_read_block(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010110}
10111#endif
10112
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010113/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010114 * "changenr()" function
10115 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010117f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010118{
10119 rettv->vval.v_number = curbuf->b_u_seq_cur;
10120}
10121
10122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 * "char2nr(string)" function
10124 */
10125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010126f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
10128#ifdef FEAT_MBYTE
10129 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010130 {
10131 int utf8 = 0;
10132
10133 if (argvars[1].v_type != VAR_UNKNOWN)
10134 utf8 = get_tv_number_chk(&argvars[1], NULL);
10135
10136 if (utf8)
10137 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10138 else
10139 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 else
10142#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144}
10145
10146/*
10147 * "cindent(lnum)" function
10148 */
10149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010150f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151{
10152#ifdef FEAT_CINDENT
10153 pos_T pos;
10154 linenr_T lnum;
10155
10156 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10159 {
10160 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 curwin->w_cursor = pos;
10163 }
10164 else
10165#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167}
10168
10169/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010170 * "clearmatches()" function
10171 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010173f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010174{
10175#ifdef FEAT_SEARCH_EXTRA
10176 clear_matches(curwin);
10177#endif
10178}
10179
10180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 * "col(string)" function
10182 */
10183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010184f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185{
10186 colnr_T col = 0;
10187 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010188 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010190 fp = var2fpos(&argvars[0], FALSE, &fnum);
10191 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 {
10193 if (fp->col == MAXCOL)
10194 {
10195 /* '> can be MAXCOL, get the length of the line then */
10196 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010197 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 else
10199 col = MAXCOL;
10200 }
10201 else
10202 {
10203 col = fp->col + 1;
10204#ifdef FEAT_VIRTUALEDIT
10205 /* col(".") when the cursor is on the NUL at the end of the line
10206 * because of "coladd" can be seen as an extra column. */
10207 if (virtual_active() && fp == &curwin->w_cursor)
10208 {
10209 char_u *p = ml_get_cursor();
10210
10211 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10212 curwin->w_virtcol - curwin->w_cursor.coladd))
10213 {
10214# ifdef FEAT_MBYTE
10215 int l;
10216
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010217 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 col += l;
10219# else
10220 if (*p != NUL && p[1] == NUL)
10221 ++col;
10222# endif
10223 }
10224 }
10225#endif
10226 }
10227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229}
10230
Bram Moolenaar572cb562005-08-05 21:35:02 +000010231#if defined(FEAT_INS_EXPAND)
10232/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010233 * "complete()" function
10234 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010236f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010237{
10238 int startcol;
10239
10240 if ((State & INSERT) == 0)
10241 {
10242 EMSG(_("E785: complete() can only be used in Insert mode"));
10243 return;
10244 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010245
10246 /* Check for undo allowed here, because if something was already inserted
10247 * the line was already saved for undo and this check isn't done. */
10248 if (!undo_allowed())
10249 return;
10250
Bram Moolenaarade00832006-03-10 21:46:58 +000010251 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10252 {
10253 EMSG(_(e_invarg));
10254 return;
10255 }
10256
10257 startcol = get_tv_number_chk(&argvars[0], NULL);
10258 if (startcol <= 0)
10259 return;
10260
10261 set_completion(startcol - 1, argvars[1].vval.v_list);
10262}
10263
10264/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010265 * "complete_add()" function
10266 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010268f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010269{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010270 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010271}
10272
10273/*
10274 * "complete_check()" function
10275 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010277f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010278{
10279 int saved = RedrawingDisabled;
10280
10281 RedrawingDisabled = 0;
10282 ins_compl_check_keys(0);
10283 rettv->vval.v_number = compl_interrupted;
10284 RedrawingDisabled = saved;
10285}
10286#endif
10287
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288/*
10289 * "confirm(message, buttons[, default [, type]])" function
10290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010292f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293{
10294#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10295 char_u *message;
10296 char_u *buttons = NULL;
10297 char_u buf[NUMBUFLEN];
10298 char_u buf2[NUMBUFLEN];
10299 int def = 1;
10300 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010301 char_u *typestr;
10302 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010304 message = get_tv_string_chk(&argvars[0]);
10305 if (message == NULL)
10306 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010307 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010309 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10310 if (buttons == NULL)
10311 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010312 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010315 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010317 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10318 if (typestr == NULL)
10319 error = TRUE;
10320 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010322 switch (TOUPPER_ASC(*typestr))
10323 {
10324 case 'E': type = VIM_ERROR; break;
10325 case 'Q': type = VIM_QUESTION; break;
10326 case 'I': type = VIM_INFO; break;
10327 case 'W': type = VIM_WARNING; break;
10328 case 'G': type = VIM_GENERIC; break;
10329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 }
10331 }
10332 }
10333 }
10334
10335 if (buttons == NULL || *buttons == NUL)
10336 buttons = (char_u *)_("&Ok");
10337
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010338 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010339 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010340 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341#endif
10342}
10343
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010344/*
10345 * "copy()" function
10346 */
10347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010348f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010349{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010350 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010351}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010353#ifdef FEAT_FLOAT
10354/*
10355 * "cos()" function
10356 */
10357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010358f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010359{
10360 float_T f;
10361
10362 rettv->v_type = VAR_FLOAT;
10363 if (get_float_arg(argvars, &f) == OK)
10364 rettv->vval.v_float = cos(f);
10365 else
10366 rettv->vval.v_float = 0.0;
10367}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010368
10369/*
10370 * "cosh()" function
10371 */
10372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010373f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010374{
10375 float_T f;
10376
10377 rettv->v_type = VAR_FLOAT;
10378 if (get_float_arg(argvars, &f) == OK)
10379 rettv->vval.v_float = cosh(f);
10380 else
10381 rettv->vval.v_float = 0.0;
10382}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010383#endif
10384
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010386 * "count()" function
10387 */
10388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010389f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010390{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010391 long n = 0;
10392 int ic = FALSE;
10393
Bram Moolenaare9a41262005-01-15 22:18:47 +000010394 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010395 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010396 listitem_T *li;
10397 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010398 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010399
Bram Moolenaare9a41262005-01-15 22:18:47 +000010400 if ((l = argvars[0].vval.v_list) != NULL)
10401 {
10402 li = l->lv_first;
10403 if (argvars[2].v_type != VAR_UNKNOWN)
10404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010405 int error = FALSE;
10406
10407 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010408 if (argvars[3].v_type != VAR_UNKNOWN)
10409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010410 idx = get_tv_number_chk(&argvars[3], &error);
10411 if (!error)
10412 {
10413 li = list_find(l, idx);
10414 if (li == NULL)
10415 EMSGN(_(e_listidx), idx);
10416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 if (error)
10419 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010420 }
10421
10422 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010423 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010424 ++n;
10425 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010427 else if (argvars[0].v_type == VAR_DICT)
10428 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010429 int todo;
10430 dict_T *d;
10431 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010432
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010433 if ((d = argvars[0].vval.v_dict) != NULL)
10434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 int error = FALSE;
10436
Bram Moolenaare9a41262005-01-15 22:18:47 +000010437 if (argvars[2].v_type != VAR_UNKNOWN)
10438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010440 if (argvars[3].v_type != VAR_UNKNOWN)
10441 EMSG(_(e_invarg));
10442 }
10443
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010444 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010445 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010446 {
10447 if (!HASHITEM_EMPTY(hi))
10448 {
10449 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010450 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010451 ++n;
10452 }
10453 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454 }
10455 }
10456 else
10457 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010458 rettv->vval.v_number = n;
10459}
10460
10461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10463 *
10464 * Checks the existence of a cscope connection.
10465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010467f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468{
10469#ifdef FEAT_CSCOPE
10470 int num = 0;
10471 char_u *dbpath = NULL;
10472 char_u *prepend = NULL;
10473 char_u buf[NUMBUFLEN];
10474
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010475 if (argvars[0].v_type != VAR_UNKNOWN
10476 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010478 num = (int)get_tv_number(&argvars[0]);
10479 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010480 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 }
10483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485#endif
10486}
10487
10488/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010489 * "cursor(lnum, col)" function, or
10490 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010492 * Moves the cursor to the specified line and column.
10493 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010496f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497{
10498 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010499#ifdef FEAT_VIRTUALEDIT
10500 long coladd = 0;
10501#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010502 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010504 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010505 if (argvars[1].v_type == VAR_UNKNOWN)
10506 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010507 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010508 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010509
Bram Moolenaar493c1782014-05-28 14:34:46 +020010510 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010511 {
10512 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010513 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010514 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010515 line = pos.lnum;
10516 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010517#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010518 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010519#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010520 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010521 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010522 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010523 set_curswant = FALSE;
10524 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010525 }
10526 else
10527 {
10528 line = get_tv_lnum(argvars);
10529 col = get_tv_number_chk(&argvars[1], NULL);
10530#ifdef FEAT_VIRTUALEDIT
10531 if (argvars[2].v_type != VAR_UNKNOWN)
10532 coladd = get_tv_number_chk(&argvars[2], NULL);
10533#endif
10534 }
10535 if (line < 0 || col < 0
10536#ifdef FEAT_VIRTUALEDIT
10537 || coladd < 0
10538#endif
10539 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010540 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 if (line > 0)
10542 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 if (col > 0)
10544 curwin->w_cursor.col = col - 1;
10545#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010546 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547#endif
10548
10549 /* Make sure the cursor is in a valid position. */
10550 check_cursor();
10551#ifdef FEAT_MBYTE
10552 /* Correct cursor for multi-byte character. */
10553 if (has_mbyte)
10554 mb_adjust_cursor();
10555#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010556
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010557 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010558 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559}
10560
10561/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010562 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 */
10564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010565f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010567 int noref = 0;
10568
10569 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010571 if (noref < 0 || noref > 1)
10572 EMSG(_(e_invarg));
10573 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010574 {
10575 current_copyID += COPYID_INC;
10576 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578}
10579
10580/*
10581 * "delete()" function
10582 */
10583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010584f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010586 char_u nbuf[NUMBUFLEN];
10587 char_u *name;
10588 char_u *flags;
10589
10590 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010592 return;
10593
10594 name = get_tv_string(&argvars[0]);
10595 if (name == NULL || *name == NUL)
10596 {
10597 EMSG(_(e_invarg));
10598 return;
10599 }
10600
10601 if (argvars[1].v_type != VAR_UNKNOWN)
10602 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010604 flags = (char_u *)"";
10605
10606 if (*flags == NUL)
10607 /* delete a file */
10608 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10609 else if (STRCMP(flags, "d") == 0)
10610 /* delete an empty directory */
10611 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10612 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010613 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010614 rettv->vval.v_number = delete_recursive(name);
10615 else
10616 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617}
10618
10619/*
10620 * "did_filetype()" function
10621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010623f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624{
10625#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010626 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627#endif
10628}
10629
10630/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010631 * "diff_filler()" function
10632 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010634f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010635{
10636#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010638#endif
10639}
10640
10641/*
10642 * "diff_hlID()" function
10643 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010645f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010646{
10647#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010649 static linenr_T prev_lnum = 0;
10650 static int changedtick = 0;
10651 static int fnum = 0;
10652 static int change_start = 0;
10653 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010654 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010655 int filler_lines;
10656 int col;
10657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010658 if (lnum < 0) /* ignore type error in {lnum} arg */
10659 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010660 if (lnum != prev_lnum
10661 || changedtick != curbuf->b_changedtick
10662 || fnum != curbuf->b_fnum)
10663 {
10664 /* New line, buffer, change: need to get the values. */
10665 filler_lines = diff_check(curwin, lnum);
10666 if (filler_lines < 0)
10667 {
10668 if (filler_lines == -1)
10669 {
10670 change_start = MAXCOL;
10671 change_end = -1;
10672 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10673 hlID = HLF_ADD; /* added line */
10674 else
10675 hlID = HLF_CHD; /* changed line */
10676 }
10677 else
10678 hlID = HLF_ADD; /* added line */
10679 }
10680 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010681 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010682 prev_lnum = lnum;
10683 changedtick = curbuf->b_changedtick;
10684 fnum = curbuf->b_fnum;
10685 }
10686
10687 if (hlID == HLF_CHD || hlID == HLF_TXD)
10688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010690 if (col >= change_start && col <= change_end)
10691 hlID = HLF_TXD; /* changed text */
10692 else
10693 hlID = HLF_CHD; /* changed line */
10694 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010695 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010696#endif
10697}
10698
10699/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010700 * "disable_char_avail_for_testing({expr})" function
10701 */
10702 static void
10703f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10704{
10705 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10706}
10707
10708/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010709 * "empty({expr})" function
10710 */
10711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010712f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010713{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010714 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010715
10716 switch (argvars[0].v_type)
10717 {
10718 case VAR_STRING:
10719 case VAR_FUNC:
10720 n = argvars[0].vval.v_string == NULL
10721 || *argvars[0].vval.v_string == NUL;
10722 break;
10723 case VAR_NUMBER:
10724 n = argvars[0].vval.v_number == 0;
10725 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010726 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010727#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010728 n = argvars[0].vval.v_float == 0.0;
10729 break;
10730#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010731 case VAR_LIST:
10732 n = argvars[0].vval.v_list == NULL
10733 || argvars[0].vval.v_list->lv_first == NULL;
10734 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010735 case VAR_DICT:
10736 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010738 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010739 case VAR_SPECIAL:
10740 n = argvars[0].vval.v_number != VVAL_TRUE;
10741 break;
10742
Bram Moolenaar835dc632016-02-07 14:27:38 +010010743 case VAR_JOB:
10744#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010010745 n = argvars[0].vval.v_job == NULL
10746 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10747 break;
10748#endif
10749 case VAR_CHANNEL:
10750#ifdef FEAT_CMDWIN
10751 n = argvars[0].vval.v_channel == NULL
10752 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010753 break;
10754#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010755 case VAR_UNKNOWN:
10756 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10757 n = TRUE;
10758 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010759 }
10760
10761 rettv->vval.v_number = n;
10762}
10763
10764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 * "escape({string}, {chars})" function
10766 */
10767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010768f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769{
10770 char_u buf[NUMBUFLEN];
10771
Bram Moolenaar758711c2005-02-02 23:11:38 +000010772 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10773 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775}
10776
10777/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010778 * "eval()" function
10779 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010781f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010782{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010783 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010785 s = get_tv_string_chk(&argvars[0]);
10786 if (s != NULL)
10787 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010788
Bram Moolenaar615b9972015-01-14 17:15:05 +010010789 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010790 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10791 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010792 if (p != NULL && !aborting())
10793 EMSG2(_(e_invexpr2), p);
10794 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010796 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010797 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010798 else if (*s != NUL)
10799 EMSG(_(e_trailing));
10800}
10801
10802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 * "eventhandler()" function
10804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010806f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010808 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809}
10810
10811/*
10812 * "executable()" function
10813 */
10814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010815f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010817 char_u *name = get_tv_string(&argvars[0]);
10818
10819 /* Check in $PATH and also check directly if there is a directory name. */
10820 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10821 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010822}
10823
10824/*
10825 * "exepath()" function
10826 */
10827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010828f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010829{
10830 char_u *p = NULL;
10831
Bram Moolenaarb5971142015-03-21 17:32:19 +010010832 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010833 rettv->v_type = VAR_STRING;
10834 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835}
10836
10837/*
10838 * "exists()" function
10839 */
10840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010841f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842{
10843 char_u *p;
10844 char_u *name;
10845 int n = FALSE;
10846 int len = 0;
10847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 if (*p == '$') /* environment variable */
10850 {
10851 /* first try "normal" environment variables (fast) */
10852 if (mch_getenv(p + 1) != NULL)
10853 n = TRUE;
10854 else
10855 {
10856 /* try expanding things like $VIM and ${HOME} */
10857 p = expand_env_save(p);
10858 if (p != NULL && *p != '$')
10859 n = TRUE;
10860 vim_free(p);
10861 }
10862 }
10863 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010865 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010866 if (*skipwhite(p) != NUL)
10867 n = FALSE; /* trailing garbage */
10868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 else if (*p == '*') /* internal or user defined function */
10870 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010871 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 }
10873 else if (*p == ':')
10874 {
10875 n = cmd_exists(p + 1);
10876 }
10877 else if (*p == '#')
10878 {
10879#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010880 if (p[1] == '#')
10881 n = autocmd_supported(p + 2);
10882 else
10883 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884#endif
10885 }
10886 else /* internal variable */
10887 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010888 char_u *tofree;
10889 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010891 /* get_name_len() takes care of expanding curly braces */
10892 name = p;
10893 len = get_name_len(&p, &tofree, TRUE, FALSE);
10894 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010896 if (tofree != NULL)
10897 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010898 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010899 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010901 /* handle d.key, l[idx], f(expr) */
10902 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10903 if (n)
10904 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 }
10906 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010907 if (*p != NUL)
10908 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010910 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 }
10912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914}
10915
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010916#ifdef FEAT_FLOAT
10917/*
10918 * "exp()" function
10919 */
10920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010921f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010922{
10923 float_T f;
10924
10925 rettv->v_type = VAR_FLOAT;
10926 if (get_float_arg(argvars, &f) == OK)
10927 rettv->vval.v_float = exp(f);
10928 else
10929 rettv->vval.v_float = 0.0;
10930}
10931#endif
10932
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933/*
10934 * "expand()" function
10935 */
10936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010937f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938{
10939 char_u *s;
10940 int len;
10941 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010942 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010944 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010945 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010948 if (argvars[1].v_type != VAR_UNKNOWN
10949 && argvars[2].v_type != VAR_UNKNOWN
10950 && get_tv_number_chk(&argvars[2], &error)
10951 && !error)
10952 {
10953 rettv->v_type = VAR_LIST;
10954 rettv->vval.v_list = NULL;
10955 }
10956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 if (*s == '%' || *s == '#' || *s == '<')
10959 {
10960 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010961 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010963 if (rettv->v_type == VAR_LIST)
10964 {
10965 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10966 list_append_string(rettv->vval.v_list, result, -1);
10967 else
10968 vim_free(result);
10969 }
10970 else
10971 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 }
10973 else
10974 {
10975 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010976 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010977 if (argvars[1].v_type != VAR_UNKNOWN
10978 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010979 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 if (!error)
10981 {
10982 ExpandInit(&xpc);
10983 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010984 if (p_wic)
10985 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010986 if (rettv->v_type == VAR_STRING)
10987 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10988 options, WILD_ALL);
10989 else if (rettv_list_alloc(rettv) != FAIL)
10990 {
10991 int i;
10992
10993 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10994 for (i = 0; i < xpc.xp_numfiles; i++)
10995 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10996 ExpandCleanup(&xpc);
10997 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010998 }
10999 else
11000 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 }
11002}
11003
11004/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011005 * Go over all entries in "d2" and add them to "d1".
11006 * When "action" is "error" then a duplicate key is an error.
11007 * When "action" is "force" then a duplicate key is overwritten.
11008 * Otherwise duplicate keys are ignored ("action" is "keep").
11009 */
11010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011011dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011012{
11013 dictitem_T *di1;
11014 hashitem_T *hi2;
11015 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011016 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011017
11018 todo = (int)d2->dv_hashtab.ht_used;
11019 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11020 {
11021 if (!HASHITEM_EMPTY(hi2))
11022 {
11023 --todo;
11024 di1 = dict_find(d1, hi2->hi_key, -1);
11025 if (d1->dv_scope != 0)
11026 {
11027 /* Disallow replacing a builtin function in l: and g:.
11028 * Check the key to be valid when adding to any
11029 * scope. */
11030 if (d1->dv_scope == VAR_DEF_SCOPE
11031 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11032 && var_check_func_name(hi2->hi_key,
11033 di1 == NULL))
11034 break;
11035 if (!valid_varname(hi2->hi_key))
11036 break;
11037 }
11038 if (di1 == NULL)
11039 {
11040 di1 = dictitem_copy(HI2DI(hi2));
11041 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11042 dictitem_free(di1);
11043 }
11044 else if (*action == 'e')
11045 {
11046 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11047 break;
11048 }
11049 else if (*action == 'f' && HI2DI(hi2) != di1)
11050 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011051 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11052 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011053 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011054 clear_tv(&di1->di_tv);
11055 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11056 }
11057 }
11058 }
11059}
11060
11061/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011062 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011063 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011064 */
11065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011066f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011067{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011068 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011069
Bram Moolenaare9a41262005-01-15 22:18:47 +000011070 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011071 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011072 list_T *l1, *l2;
11073 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011074 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011076
Bram Moolenaare9a41262005-01-15 22:18:47 +000011077 l1 = argvars[0].vval.v_list;
11078 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011079 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011080 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011081 {
11082 if (argvars[2].v_type != VAR_UNKNOWN)
11083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 before = get_tv_number_chk(&argvars[2], &error);
11085 if (error)
11086 return; /* type error; errmsg already given */
11087
Bram Moolenaar758711c2005-02-02 23:11:38 +000011088 if (before == l1->lv_len)
11089 item = NULL;
11090 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011091 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011092 item = list_find(l1, before);
11093 if (item == NULL)
11094 {
11095 EMSGN(_(e_listidx), before);
11096 return;
11097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011098 }
11099 }
11100 else
11101 item = NULL;
11102 list_extend(l1, l2, item);
11103
Bram Moolenaare9a41262005-01-15 22:18:47 +000011104 copy_tv(&argvars[0], rettv);
11105 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011107 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11108 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011109 dict_T *d1, *d2;
11110 char_u *action;
11111 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112
11113 d1 = argvars[0].vval.v_dict;
11114 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011115 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011116 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117 {
11118 /* Check the third argument. */
11119 if (argvars[2].v_type != VAR_UNKNOWN)
11120 {
11121 static char *(av[]) = {"keep", "force", "error"};
11122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 action = get_tv_string_chk(&argvars[2]);
11124 if (action == NULL)
11125 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011126 for (i = 0; i < 3; ++i)
11127 if (STRCMP(action, av[i]) == 0)
11128 break;
11129 if (i == 3)
11130 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011131 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011132 return;
11133 }
11134 }
11135 else
11136 action = (char_u *)"force";
11137
Bram Moolenaara9922d62013-05-30 13:01:18 +020011138 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011139
Bram Moolenaare9a41262005-01-15 22:18:47 +000011140 copy_tv(&argvars[0], rettv);
11141 }
11142 }
11143 else
11144 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011145}
11146
11147/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011148 * "feedkeys()" function
11149 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011151f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011152{
11153 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011154 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011155 char_u *keys, *flags;
11156 char_u nbuf[NUMBUFLEN];
11157 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011158 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011159 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011160
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011161 /* This is not allowed in the sandbox. If the commands would still be
11162 * executed in the sandbox it would be OK, but it probably happens later,
11163 * when "sandbox" is no longer set. */
11164 if (check_secure())
11165 return;
11166
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011167 keys = get_tv_string(&argvars[0]);
11168 if (*keys != NUL)
11169 {
11170 if (argvars[1].v_type != VAR_UNKNOWN)
11171 {
11172 flags = get_tv_string_buf(&argvars[1], nbuf);
11173 for ( ; *flags != NUL; ++flags)
11174 {
11175 switch (*flags)
11176 {
11177 case 'n': remap = FALSE; break;
11178 case 'm': remap = TRUE; break;
11179 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011180 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011181 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011182 }
11183 }
11184 }
11185
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011186 /* Need to escape K_SPECIAL and CSI before putting the string in the
11187 * typeahead buffer. */
11188 keys_esc = vim_strsave_escape_csi(keys);
11189 if (keys_esc != NULL)
11190 {
11191 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011192 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011193 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011194 if (vgetc_busy)
11195 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011196 if (execute)
11197 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011198 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011199 }
11200}
11201
11202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 * "filereadable()" function
11204 */
11205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011206f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011208 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 char_u *p;
11210 int n;
11211
Bram Moolenaarc236c162008-07-13 17:41:49 +000011212#ifndef O_NONBLOCK
11213# define O_NONBLOCK 0
11214#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011216 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11217 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 {
11219 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011220 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 }
11222 else
11223 n = FALSE;
11224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226}
11227
11228/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011229 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 * rights to write into.
11231 */
11232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011233f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011235 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236}
11237
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011238 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011239findfilendir(
11240 typval_T *argvars UNUSED,
11241 typval_T *rettv,
11242 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011243{
11244#ifdef FEAT_SEARCHPATH
11245 char_u *fname;
11246 char_u *fresult = NULL;
11247 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11248 char_u *p;
11249 char_u pathbuf[NUMBUFLEN];
11250 int count = 1;
11251 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011252 int error = FALSE;
11253#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011254
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011255 rettv->vval.v_string = NULL;
11256 rettv->v_type = VAR_STRING;
11257
11258#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011260
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011261 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011263 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11264 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011265 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011266 else
11267 {
11268 if (*p != NUL)
11269 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011272 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011273 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011274 }
11275
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011276 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11277 error = TRUE;
11278
11279 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011281 do
11282 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011283 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011284 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011285 fresult = find_file_in_path_option(first ? fname : NULL,
11286 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011287 0, first, path,
11288 find_what,
11289 curbuf->b_ffname,
11290 find_what == FINDFILE_DIR
11291 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011292 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011293
11294 if (fresult != NULL && rettv->v_type == VAR_LIST)
11295 list_append_string(rettv->vval.v_list, fresult, -1);
11296
11297 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011298 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011299
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011300 if (rettv->v_type == VAR_STRING)
11301 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011302#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011303}
11304
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011305static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11306static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011307
11308/*
11309 * Implementation of map() and filter().
11310 */
11311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011312filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011313{
11314 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011315 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011316 listitem_T *li, *nli;
11317 list_T *l = NULL;
11318 dictitem_T *di;
11319 hashtab_T *ht;
11320 hashitem_T *hi;
11321 dict_T *d = NULL;
11322 typval_T save_val;
11323 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011324 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011325 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011326 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011327 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011328 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011329 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011330 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011331
Bram Moolenaare9a41262005-01-15 22:18:47 +000011332 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011333 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011334 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011335 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011336 return;
11337 }
11338 else if (argvars[0].v_type == VAR_DICT)
11339 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011340 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011341 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011342 return;
11343 }
11344 else
11345 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011346 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011347 return;
11348 }
11349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011350 expr = get_tv_string_buf_chk(&argvars[1], buf);
11351 /* On type errors, the preceding call has already displayed an error
11352 * message. Avoid a misleading error message for an empty string that
11353 * was not passed as argument. */
11354 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011356 prepare_vimvar(VV_VAL, &save_val);
11357 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011358
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011359 /* We reset "did_emsg" to be able to detect whether an error
11360 * occurred during evaluation of the expression. */
11361 save_did_emsg = did_emsg;
11362 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011363
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011364 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011365 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011367 vimvars[VV_KEY].vv_type = VAR_STRING;
11368
11369 ht = &d->dv_hashtab;
11370 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011371 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011372 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011374 if (!HASHITEM_EMPTY(hi))
11375 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011376 int r;
11377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378 --todo;
11379 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011380 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011381 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11382 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011383 break;
11384 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011385 r = filter_map_one(&di->di_tv, expr, map, &rem);
11386 clear_tv(&vimvars[VV_KEY].vv_tv);
11387 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 break;
11389 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011390 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011391 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11392 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011393 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011394 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011396 }
11397 }
11398 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011399 }
11400 else
11401 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011402 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11403
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011404 for (li = l->lv_first; li != NULL; li = nli)
11405 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011406 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011407 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011408 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011409 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011410 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011411 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011412 break;
11413 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011415 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011416 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011417 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011418
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011419 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011420 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011421
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011422 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011423 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011424
11425 copy_tv(&argvars[0], rettv);
11426}
11427
11428 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011429filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011430{
Bram Moolenaar33570922005-01-25 22:26:29 +000011431 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011432 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011433 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011434
Bram Moolenaar33570922005-01-25 22:26:29 +000011435 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011436 s = expr;
11437 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011438 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011439 if (*s != NUL) /* check for trailing chars after expr */
11440 {
11441 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011442 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011443 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011444 }
11445 if (map)
11446 {
11447 /* map(): replace the list item value */
11448 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011449 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011450 *tv = rettv;
11451 }
11452 else
11453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011454 int error = FALSE;
11455
Bram Moolenaare9a41262005-01-15 22:18:47 +000011456 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011458 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011459 /* On type error, nothing has been removed; return FAIL to stop the
11460 * loop. The error message was given by get_tv_number_chk(). */
11461 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011462 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011463 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011464 retval = OK;
11465theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011466 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011467 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011468}
11469
11470/*
11471 * "filter()" function
11472 */
11473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011474f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011475{
11476 filter_map(argvars, rettv, FALSE);
11477}
11478
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011479/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011480 * "finddir({fname}[, {path}[, {count}]])" function
11481 */
11482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011483f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011484{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011485 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011486}
11487
11488/*
11489 * "findfile({fname}[, {path}[, {count}]])" function
11490 */
11491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011492f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011493{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011494 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011495}
11496
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011497#ifdef FEAT_FLOAT
11498/*
11499 * "float2nr({float})" function
11500 */
11501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011502f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011503{
11504 float_T f;
11505
11506 if (get_float_arg(argvars, &f) == OK)
11507 {
11508 if (f < -0x7fffffff)
11509 rettv->vval.v_number = -0x7fffffff;
11510 else if (f > 0x7fffffff)
11511 rettv->vval.v_number = 0x7fffffff;
11512 else
11513 rettv->vval.v_number = (varnumber_T)f;
11514 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011515}
11516
11517/*
11518 * "floor({float})" function
11519 */
11520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011521f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011522{
11523 float_T f;
11524
11525 rettv->v_type = VAR_FLOAT;
11526 if (get_float_arg(argvars, &f) == OK)
11527 rettv->vval.v_float = floor(f);
11528 else
11529 rettv->vval.v_float = 0.0;
11530}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011531
11532/*
11533 * "fmod()" function
11534 */
11535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011536f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011537{
11538 float_T fx, fy;
11539
11540 rettv->v_type = VAR_FLOAT;
11541 if (get_float_arg(argvars, &fx) == OK
11542 && get_float_arg(&argvars[1], &fy) == OK)
11543 rettv->vval.v_float = fmod(fx, fy);
11544 else
11545 rettv->vval.v_float = 0.0;
11546}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011547#endif
11548
Bram Moolenaar0d660222005-01-07 21:51:51 +000011549/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011550 * "fnameescape({string})" function
11551 */
11552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011553f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011554{
11555 rettv->vval.v_string = vim_strsave_fnameescape(
11556 get_tv_string(&argvars[0]), FALSE);
11557 rettv->v_type = VAR_STRING;
11558}
11559
11560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 * "fnamemodify({fname}, {mods})" function
11562 */
11563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011564f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565{
11566 char_u *fname;
11567 char_u *mods;
11568 int usedlen = 0;
11569 int len;
11570 char_u *fbuf = NULL;
11571 char_u buf[NUMBUFLEN];
11572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573 fname = get_tv_string_chk(&argvars[0]);
11574 mods = get_tv_string_buf_chk(&argvars[1], buf);
11575 if (fname == NULL || mods == NULL)
11576 fname = NULL;
11577 else
11578 {
11579 len = (int)STRLEN(fname);
11580 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 vim_free(fbuf);
11589}
11590
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011591static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592
11593/*
11594 * "foldclosed()" function
11595 */
11596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011597foldclosed_both(
11598 typval_T *argvars UNUSED,
11599 typval_T *rettv,
11600 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601{
11602#ifdef FEAT_FOLDING
11603 linenr_T lnum;
11604 linenr_T first, last;
11605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11608 {
11609 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11610 {
11611 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011612 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 return;
11616 }
11617 }
11618#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620}
11621
11622/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011623 * "foldclosed()" function
11624 */
11625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011626f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011627{
11628 foldclosed_both(argvars, rettv, FALSE);
11629}
11630
11631/*
11632 * "foldclosedend()" function
11633 */
11634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011635f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011636{
11637 foldclosed_both(argvars, rettv, TRUE);
11638}
11639
11640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 * "foldlevel()" function
11642 */
11643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011644f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645{
11646#ifdef FEAT_FOLDING
11647 linenr_T lnum;
11648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011649 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653}
11654
11655/*
11656 * "foldtext()" function
11657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011659f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660{
11661#ifdef FEAT_FOLDING
11662 linenr_T lnum;
11663 char_u *s;
11664 char_u *r;
11665 int len;
11666 char *txt;
11667#endif
11668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011669 rettv->v_type = VAR_STRING;
11670 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011672 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11673 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11674 <= curbuf->b_ml.ml_line_count
11675 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 {
11677 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011678 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11679 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 {
11681 if (!linewhite(lnum))
11682 break;
11683 ++lnum;
11684 }
11685
11686 /* Find interesting text in this line. */
11687 s = skipwhite(ml_get(lnum));
11688 /* skip C comment-start */
11689 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011692 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011693 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011694 {
11695 s = skipwhite(ml_get(lnum + 1));
11696 if (*s == '*')
11697 s = skipwhite(s + 1);
11698 }
11699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 txt = _("+-%s%3ld lines: ");
11701 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011702 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 + 20 /* for %3ld */
11704 + STRLEN(s))); /* concatenated */
11705 if (r != NULL)
11706 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011707 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11708 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11709 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 len = (int)STRLEN(r);
11711 STRCAT(r, s);
11712 /* remove 'foldmarker' and 'commentstring' */
11713 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011714 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 }
11716 }
11717#endif
11718}
11719
11720/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011721 * "foldtextresult(lnum)" function
11722 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011724f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011725{
11726#ifdef FEAT_FOLDING
11727 linenr_T lnum;
11728 char_u *text;
11729 char_u buf[51];
11730 foldinfo_T foldinfo;
11731 int fold_count;
11732#endif
11733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734 rettv->v_type = VAR_STRING;
11735 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011736#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011738 /* treat illegal types and illegal string values for {lnum} the same */
11739 if (lnum < 0)
11740 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011741 fold_count = foldedCount(curwin, lnum, &foldinfo);
11742 if (fold_count > 0)
11743 {
11744 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11745 &foldinfo, buf);
11746 if (text == buf)
11747 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011748 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011749 }
11750#endif
11751}
11752
11753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 * "foreground()" function
11755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011757f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759#ifdef FEAT_GUI
11760 if (gui.in_use)
11761 gui_mch_set_foreground();
11762#else
11763# ifdef WIN32
11764 win32_set_foreground();
11765# endif
11766#endif
11767}
11768
11769/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011770 * "function()" function
11771 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011773f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011774{
11775 char_u *s;
11776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011777 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011778 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011779 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011780 /* Don't check an autoload name for existence here. */
11781 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011782 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011783 else
11784 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011785 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011786 {
11787 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011788 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011789
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011790 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11791 * also be called from another script. Using trans_function_name()
11792 * would also work, but some plugins depend on the name being
11793 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011794 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011795 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011796 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011797 if (rettv->vval.v_string != NULL)
11798 {
11799 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011800 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011801 }
11802 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011803 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011804 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011805 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011806 }
11807}
11808
11809/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011810 * "garbagecollect()" function
11811 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011813f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011814{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011815 /* This is postponed until we are back at the toplevel, because we may be
11816 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11817 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011818
11819 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11820 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011821}
11822
11823/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011824 * "get()" function
11825 */
11826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011827f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011828{
Bram Moolenaar33570922005-01-25 22:26:29 +000011829 listitem_T *li;
11830 list_T *l;
11831 dictitem_T *di;
11832 dict_T *d;
11833 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011834
Bram Moolenaare9a41262005-01-15 22:18:47 +000011835 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011836 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011837 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011839 int error = FALSE;
11840
11841 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11842 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011843 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011844 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011846 else if (argvars[0].v_type == VAR_DICT)
11847 {
11848 if ((d = argvars[0].vval.v_dict) != NULL)
11849 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011850 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011851 if (di != NULL)
11852 tv = &di->di_tv;
11853 }
11854 }
11855 else
11856 EMSG2(_(e_listdictarg), "get()");
11857
11858 if (tv == NULL)
11859 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011860 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011861 copy_tv(&argvars[2], rettv);
11862 }
11863 else
11864 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011865}
11866
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011867static 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 +000011868
11869/*
11870 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011871 * Return a range (from start to end) of lines in rettv from the specified
11872 * buffer.
11873 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011874 */
11875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011876get_buffer_lines(
11877 buf_T *buf,
11878 linenr_T start,
11879 linenr_T end,
11880 int retlist,
11881 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011882{
11883 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011884
Bram Moolenaar959a1432013-12-14 12:17:38 +010011885 rettv->v_type = VAR_STRING;
11886 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011887 if (retlist && rettv_list_alloc(rettv) == FAIL)
11888 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011889
11890 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11891 return;
11892
11893 if (!retlist)
11894 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011895 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11896 p = ml_get_buf(buf, start, FALSE);
11897 else
11898 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011899 rettv->vval.v_string = vim_strsave(p);
11900 }
11901 else
11902 {
11903 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011904 return;
11905
11906 if (start < 1)
11907 start = 1;
11908 if (end > buf->b_ml.ml_line_count)
11909 end = buf->b_ml.ml_line_count;
11910 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011911 if (list_append_string(rettv->vval.v_list,
11912 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011913 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011914 }
11915}
11916
11917/*
11918 * "getbufline()" function
11919 */
11920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011921f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011922{
11923 linenr_T lnum;
11924 linenr_T end;
11925 buf_T *buf;
11926
11927 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11928 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011929 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011930 --emsg_off;
11931
Bram Moolenaar661b1822005-07-28 22:36:45 +000011932 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011933 if (argvars[2].v_type == VAR_UNKNOWN)
11934 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011935 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011936 end = get_tv_lnum_buf(&argvars[2], buf);
11937
Bram Moolenaar342337a2005-07-21 21:11:17 +000011938 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011939}
11940
Bram Moolenaar0d660222005-01-07 21:51:51 +000011941/*
11942 * "getbufvar()" function
11943 */
11944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011945f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011946{
11947 buf_T *buf;
11948 buf_T *save_curbuf;
11949 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011951 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011953 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11954 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011955 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011956 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011957
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011958 rettv->v_type = VAR_STRING;
11959 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011960
11961 if (buf != NULL && varname != NULL)
11962 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011963 /* set curbuf to be our buf, temporarily */
11964 save_curbuf = curbuf;
11965 curbuf = buf;
11966
Bram Moolenaar0d660222005-01-07 21:51:51 +000011967 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011968 {
11969 if (get_option_tv(&varname, rettv, TRUE) == OK)
11970 done = TRUE;
11971 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011972 else if (STRCMP(varname, "changedtick") == 0)
11973 {
11974 rettv->v_type = VAR_NUMBER;
11975 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011976 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011977 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011978 else
11979 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011980 /* Look up the variable. */
11981 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11982 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11983 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011984 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011985 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011986 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011987 done = TRUE;
11988 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011989 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011990
11991 /* restore previous notion of curbuf */
11992 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011993 }
11994
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011995 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11996 /* use the default value */
11997 copy_tv(&argvars[2], rettv);
11998
Bram Moolenaar0d660222005-01-07 21:51:51 +000011999 --emsg_off;
12000}
12001
12002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003 * "getchar()" function
12004 */
12005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012006f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007{
12008 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012009 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012011 /* Position the cursor. Needed after a message that ends in a space. */
12012 windgoto(msg_row, msg_col);
12013
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 ++no_mapping;
12015 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012016 for (;;)
12017 {
12018 if (argvars[0].v_type == VAR_UNKNOWN)
12019 /* getchar(): blocking wait. */
12020 n = safe_vgetc();
12021 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12022 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012023 n = vpeekc_any();
12024 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012025 /* illegal argument or getchar(0) and no char avail: return zero */
12026 n = 0;
12027 else
12028 /* getchar(0) and char avail: return char */
12029 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012030
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012031 if (n == K_IGNORE)
12032 continue;
12033 break;
12034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 --no_mapping;
12036 --allow_keys;
12037
Bram Moolenaar219b8702006-11-01 14:32:36 +000012038 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12039 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12040 vimvars[VV_MOUSE_COL].vv_nr = 0;
12041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 if (IS_SPECIAL(n) || mod_mask != 0)
12044 {
12045 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12046 int i = 0;
12047
12048 /* Turn a special key into three bytes, plus modifier. */
12049 if (mod_mask != 0)
12050 {
12051 temp[i++] = K_SPECIAL;
12052 temp[i++] = KS_MODIFIER;
12053 temp[i++] = mod_mask;
12054 }
12055 if (IS_SPECIAL(n))
12056 {
12057 temp[i++] = K_SPECIAL;
12058 temp[i++] = K_SECOND(n);
12059 temp[i++] = K_THIRD(n);
12060 }
12061#ifdef FEAT_MBYTE
12062 else if (has_mbyte)
12063 i += (*mb_char2bytes)(n, temp + i);
12064#endif
12065 else
12066 temp[i++] = n;
12067 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012068 rettv->v_type = VAR_STRING;
12069 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012070
12071#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012072 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012073 {
12074 int row = mouse_row;
12075 int col = mouse_col;
12076 win_T *win;
12077 linenr_T lnum;
12078# ifdef FEAT_WINDOWS
12079 win_T *wp;
12080# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012081 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012082
12083 if (row >= 0 && col >= 0)
12084 {
12085 /* Find the window at the mouse coordinates and compute the
12086 * text position. */
12087 win = mouse_find_win(&row, &col);
12088 (void)mouse_comp_pos(win, &row, &col, &lnum);
12089# ifdef FEAT_WINDOWS
12090 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012091 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012092# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012093 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012094 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12095 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12096 }
12097 }
12098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099 }
12100}
12101
12102/*
12103 * "getcharmod()" function
12104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012106f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109}
12110
12111/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012112 * "getcharsearch()" function
12113 */
12114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012115f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012116{
12117 if (rettv_dict_alloc(rettv) != FAIL)
12118 {
12119 dict_T *dict = rettv->vval.v_dict;
12120
12121 dict_add_nr_str(dict, "char", 0L, last_csearch());
12122 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12123 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12124 }
12125}
12126
12127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 * "getcmdline()" function
12129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012131f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133 rettv->v_type = VAR_STRING;
12134 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135}
12136
12137/*
12138 * "getcmdpos()" function
12139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012141f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144}
12145
12146/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012147 * "getcmdtype()" function
12148 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012150f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012151{
12152 rettv->v_type = VAR_STRING;
12153 rettv->vval.v_string = alloc(2);
12154 if (rettv->vval.v_string != NULL)
12155 {
12156 rettv->vval.v_string[0] = get_cmdline_type();
12157 rettv->vval.v_string[1] = NUL;
12158 }
12159}
12160
12161/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012162 * "getcmdwintype()" function
12163 */
12164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012165f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012166{
12167 rettv->v_type = VAR_STRING;
12168 rettv->vval.v_string = NULL;
12169#ifdef FEAT_CMDWIN
12170 rettv->vval.v_string = alloc(2);
12171 if (rettv->vval.v_string != NULL)
12172 {
12173 rettv->vval.v_string[0] = cmdwin_type;
12174 rettv->vval.v_string[1] = NUL;
12175 }
12176#endif
12177}
12178
12179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180 * "getcwd()" function
12181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012183f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012185 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012186 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012188 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012189 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012190
12191 wp = find_tabwin(&argvars[0], &argvars[1]);
12192 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012194 if (wp->w_localdir != NULL)
12195 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12196 else if(globaldir != NULL)
12197 rettv->vval.v_string = vim_strsave(globaldir);
12198 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012199 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012200 cwd = alloc(MAXPATHL);
12201 if (cwd != NULL)
12202 {
12203 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12204 rettv->vval.v_string = vim_strsave(cwd);
12205 vim_free(cwd);
12206 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012207 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012208#ifdef BACKSLASH_IN_FILENAME
12209 if (rettv->vval.v_string != NULL)
12210 slash_adjust(rettv->vval.v_string);
12211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 }
12213}
12214
12215/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012216 * "getfontname()" function
12217 */
12218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012219f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012220{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012221 rettv->v_type = VAR_STRING;
12222 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012223#ifdef FEAT_GUI
12224 if (gui.in_use)
12225 {
12226 GuiFont font;
12227 char_u *name = NULL;
12228
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012229 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012230 {
12231 /* Get the "Normal" font. Either the name saved by
12232 * hl_set_font_name() or from the font ID. */
12233 font = gui.norm_font;
12234 name = hl_get_font_name();
12235 }
12236 else
12237 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012238 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012239 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12240 return;
12241 font = gui_mch_get_font(name, FALSE);
12242 if (font == NOFONT)
12243 return; /* Invalid font name, return empty string. */
12244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012246 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012247 gui_mch_free_font(font);
12248 }
12249#endif
12250}
12251
12252/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012253 * "getfperm({fname})" function
12254 */
12255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012256f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012257{
12258 char_u *fname;
12259 struct stat st;
12260 char_u *perm = NULL;
12261 char_u flags[] = "rwx";
12262 int i;
12263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012266 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012267 if (mch_stat((char *)fname, &st) >= 0)
12268 {
12269 perm = vim_strsave((char_u *)"---------");
12270 if (perm != NULL)
12271 {
12272 for (i = 0; i < 9; i++)
12273 {
12274 if (st.st_mode & (1 << (8 - i)))
12275 perm[i] = flags[i % 3];
12276 }
12277 }
12278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012280}
12281
12282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 * "getfsize({fname})" function
12284 */
12285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012286f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287{
12288 char_u *fname;
12289 struct stat st;
12290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012293 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294
12295 if (mch_stat((char *)fname, &st) >= 0)
12296 {
12297 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012298 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012300 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012301 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012302
12303 /* non-perfect check for overflow */
12304 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12305 rettv->vval.v_number = -2;
12306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307 }
12308 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012309 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310}
12311
12312/*
12313 * "getftime({fname})" function
12314 */
12315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012316f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317{
12318 char_u *fname;
12319 struct stat st;
12320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322
12323 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012326 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327}
12328
12329/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012330 * "getftype({fname})" function
12331 */
12332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012333f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012334{
12335 char_u *fname;
12336 struct stat st;
12337 char_u *type = NULL;
12338 char *t;
12339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012340 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012342 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012343 if (mch_lstat((char *)fname, &st) >= 0)
12344 {
12345#ifdef S_ISREG
12346 if (S_ISREG(st.st_mode))
12347 t = "file";
12348 else if (S_ISDIR(st.st_mode))
12349 t = "dir";
12350# ifdef S_ISLNK
12351 else if (S_ISLNK(st.st_mode))
12352 t = "link";
12353# endif
12354# ifdef S_ISBLK
12355 else if (S_ISBLK(st.st_mode))
12356 t = "bdev";
12357# endif
12358# ifdef S_ISCHR
12359 else if (S_ISCHR(st.st_mode))
12360 t = "cdev";
12361# endif
12362# ifdef S_ISFIFO
12363 else if (S_ISFIFO(st.st_mode))
12364 t = "fifo";
12365# endif
12366# ifdef S_ISSOCK
12367 else if (S_ISSOCK(st.st_mode))
12368 t = "fifo";
12369# endif
12370 else
12371 t = "other";
12372#else
12373# ifdef S_IFMT
12374 switch (st.st_mode & S_IFMT)
12375 {
12376 case S_IFREG: t = "file"; break;
12377 case S_IFDIR: t = "dir"; break;
12378# ifdef S_IFLNK
12379 case S_IFLNK: t = "link"; break;
12380# endif
12381# ifdef S_IFBLK
12382 case S_IFBLK: t = "bdev"; break;
12383# endif
12384# ifdef S_IFCHR
12385 case S_IFCHR: t = "cdev"; break;
12386# endif
12387# ifdef S_IFIFO
12388 case S_IFIFO: t = "fifo"; break;
12389# endif
12390# ifdef S_IFSOCK
12391 case S_IFSOCK: t = "socket"; break;
12392# endif
12393 default: t = "other";
12394 }
12395# else
12396 if (mch_isdir(fname))
12397 t = "dir";
12398 else
12399 t = "file";
12400# endif
12401#endif
12402 type = vim_strsave((char_u *)t);
12403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012404 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012405}
12406
12407/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012408 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012409 */
12410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012411f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012412{
12413 linenr_T lnum;
12414 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012415 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416
12417 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012418 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012419 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012420 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012421 retlist = FALSE;
12422 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012423 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012424 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012425 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012426 retlist = TRUE;
12427 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012428
Bram Moolenaar342337a2005-07-21 21:11:17 +000012429 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012430}
12431
12432/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012433 * "getmatches()" function
12434 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012436f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012437{
12438#ifdef FEAT_SEARCH_EXTRA
12439 dict_T *dict;
12440 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012441 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012442
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012443 if (rettv_list_alloc(rettv) == OK)
12444 {
12445 while (cur != NULL)
12446 {
12447 dict = dict_alloc();
12448 if (dict == NULL)
12449 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012450 if (cur->match.regprog == NULL)
12451 {
12452 /* match added with matchaddpos() */
12453 for (i = 0; i < MAXPOSMATCH; ++i)
12454 {
12455 llpos_T *llpos;
12456 char buf[6];
12457 list_T *l;
12458
12459 llpos = &cur->pos.pos[i];
12460 if (llpos->lnum == 0)
12461 break;
12462 l = list_alloc();
12463 if (l == NULL)
12464 break;
12465 list_append_number(l, (varnumber_T)llpos->lnum);
12466 if (llpos->col > 0)
12467 {
12468 list_append_number(l, (varnumber_T)llpos->col);
12469 list_append_number(l, (varnumber_T)llpos->len);
12470 }
12471 sprintf(buf, "pos%d", i + 1);
12472 dict_add_list(dict, buf, l);
12473 }
12474 }
12475 else
12476 {
12477 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12478 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012479 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012480 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12481 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012482# ifdef FEAT_CONCEAL
12483 if (cur->conceal_char)
12484 {
12485 char_u buf[MB_MAXBYTES + 1];
12486
12487 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12488 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12489 }
12490# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012491 list_append_dict(rettv->vval.v_list, dict);
12492 cur = cur->next;
12493 }
12494 }
12495#endif
12496}
12497
12498/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012499 * "getpid()" function
12500 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012502f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012503{
12504 rettv->vval.v_number = mch_get_pid();
12505}
12506
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012507static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012508
12509/*
12510 * "getcurpos()" function
12511 */
12512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012513f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012514{
12515 getpos_both(argvars, rettv, TRUE);
12516}
12517
Bram Moolenaar18081e32008-02-20 19:11:07 +000012518/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012519 * "getpos(string)" function
12520 */
12521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012522f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012523{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012524 getpos_both(argvars, rettv, FALSE);
12525}
12526
12527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012528getpos_both(
12529 typval_T *argvars,
12530 typval_T *rettv,
12531 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012532{
Bram Moolenaara5525202006-03-02 22:52:09 +000012533 pos_T *fp;
12534 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012535 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012536
12537 if (rettv_list_alloc(rettv) == OK)
12538 {
12539 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012540 if (getcurpos)
12541 fp = &curwin->w_cursor;
12542 else
12543 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012544 if (fnum != -1)
12545 list_append_number(l, (varnumber_T)fnum);
12546 else
12547 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012548 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12549 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012550 list_append_number(l, (fp != NULL)
12551 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012552 : (varnumber_T)0);
12553 list_append_number(l,
12554#ifdef FEAT_VIRTUALEDIT
12555 (fp != NULL) ? (varnumber_T)fp->coladd :
12556#endif
12557 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012558 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012559 {
12560 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012561 list_append_number(l, curwin->w_curswant == MAXCOL ?
12562 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012563 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012564 }
12565 else
12566 rettv->vval.v_number = FALSE;
12567}
12568
12569/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012570 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012571 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012573f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012574{
12575#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012576 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012577#endif
12578
Bram Moolenaar2641f772005-03-25 21:58:17 +000012579#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012580 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012581 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012582 wp = NULL;
12583 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12584 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012585 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012586 if (wp == NULL)
12587 return;
12588 }
12589
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012590 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012591 }
12592#endif
12593}
12594
12595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 * "getreg()" function
12597 */
12598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012599f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600{
12601 char_u *strregname;
12602 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012603 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012604 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012605 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012607 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012609 strregname = get_tv_string_chk(&argvars[0]);
12610 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012611 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012613 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012614 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12615 return_list = get_tv_number_chk(&argvars[2], &error);
12616 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012619 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012620
12621 if (error)
12622 return;
12623
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 regname = (strregname == NULL ? '"' : *strregname);
12625 if (regname == 0)
12626 regname = '"';
12627
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012628 if (return_list)
12629 {
12630 rettv->v_type = VAR_LIST;
12631 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12632 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012633 if (rettv->vval.v_list != NULL)
12634 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012635 }
12636 else
12637 {
12638 rettv->v_type = VAR_STRING;
12639 rettv->vval.v_string = get_reg_contents(regname,
12640 arg2 ? GREG_EXPR_SRC : 0);
12641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642}
12643
12644/*
12645 * "getregtype()" function
12646 */
12647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012648f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649{
12650 char_u *strregname;
12651 int regname;
12652 char_u buf[NUMBUFLEN + 2];
12653 long reglen = 0;
12654
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012655 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012656 {
12657 strregname = get_tv_string_chk(&argvars[0]);
12658 if (strregname == NULL) /* type error; errmsg already given */
12659 {
12660 rettv->v_type = VAR_STRING;
12661 rettv->vval.v_string = NULL;
12662 return;
12663 }
12664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 else
12666 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012667 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668
12669 regname = (strregname == NULL ? '"' : *strregname);
12670 if (regname == 0)
12671 regname = '"';
12672
12673 buf[0] = NUL;
12674 buf[1] = NUL;
12675 switch (get_reg_type(regname, &reglen))
12676 {
12677 case MLINE: buf[0] = 'V'; break;
12678 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 case MBLOCK:
12680 buf[0] = Ctrl_V;
12681 sprintf((char *)buf + 1, "%ld", reglen + 1);
12682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 rettv->v_type = VAR_STRING;
12685 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686}
12687
12688/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012689 * "gettabvar()" function
12690 */
12691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012692f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012693{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012694 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012695 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012696 dictitem_T *v;
12697 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012698 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012699
12700 rettv->v_type = VAR_STRING;
12701 rettv->vval.v_string = NULL;
12702
12703 varname = get_tv_string_chk(&argvars[1]);
12704 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12705 if (tp != NULL && varname != NULL)
12706 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012707 /* Set tp to be our tabpage, temporarily. Also set the window to the
12708 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012709 if (switch_win(&oldcurwin, &oldtabpage,
12710 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012711 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012712 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012713 /* look up the variable */
12714 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12715 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12716 if (v != NULL)
12717 {
12718 copy_tv(&v->di_tv, rettv);
12719 done = TRUE;
12720 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012721 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012722
12723 /* restore previous notion of curwin */
12724 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012725 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012726
12727 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012728 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012729}
12730
12731/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012732 * "gettabwinvar()" function
12733 */
12734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012735f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012736{
12737 getwinvar(argvars, rettv, 1);
12738}
12739
12740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741 * "getwinposx()" function
12742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012744f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747#ifdef FEAT_GUI
12748 if (gui.in_use)
12749 {
12750 int x, y;
12751
12752 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 }
12755#endif
12756}
12757
12758/*
12759 * "getwinposy()" function
12760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012762f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765#ifdef FEAT_GUI
12766 if (gui.in_use)
12767 {
12768 int x, y;
12769
12770 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 }
12773#endif
12774}
12775
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012776/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012777 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012778 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012779 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012780find_win_by_nr(
12781 typval_T *vp,
12782 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012783{
12784#ifdef FEAT_WINDOWS
12785 win_T *wp;
12786#endif
12787 int nr;
12788
12789 nr = get_tv_number_chk(vp, NULL);
12790
12791#ifdef FEAT_WINDOWS
12792 if (nr < 0)
12793 return NULL;
12794 if (nr == 0)
12795 return curwin;
12796
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012797 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12798 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012799 if (--nr <= 0)
12800 break;
12801 return wp;
12802#else
12803 if (nr == 0 || nr == 1)
12804 return curwin;
12805 return NULL;
12806#endif
12807}
12808
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012810 * Find window specified by "wvp" in tabpage "tvp".
12811 */
12812 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012813find_tabwin(
12814 typval_T *wvp, /* VAR_UNKNOWN for current window */
12815 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012816{
12817 win_T *wp = NULL;
12818 tabpage_T *tp = NULL;
12819 long n;
12820
12821 if (wvp->v_type != VAR_UNKNOWN)
12822 {
12823 if (tvp->v_type != VAR_UNKNOWN)
12824 {
12825 n = get_tv_number(tvp);
12826 if (n >= 0)
12827 tp = find_tabpage(n);
12828 }
12829 else
12830 tp = curtab;
12831
12832 if (tp != NULL)
12833 wp = find_win_by_nr(wvp, tp);
12834 }
12835 else
12836 wp = curwin;
12837
12838 return wp;
12839}
12840
12841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 * "getwinvar()" function
12843 */
12844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012845f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012847 getwinvar(argvars, rettv, 0);
12848}
12849
12850/*
12851 * getwinvar() and gettabwinvar()
12852 */
12853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012854getwinvar(
12855 typval_T *argvars,
12856 typval_T *rettv,
12857 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012858{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012859 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012861 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012862 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012863 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012864#ifdef FEAT_WINDOWS
12865 win_T *oldcurwin;
12866 tabpage_T *oldtabpage;
12867 int need_switch_win;
12868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012870#ifdef FEAT_WINDOWS
12871 if (off == 1)
12872 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12873 else
12874 tp = curtab;
12875#endif
12876 win = find_win_by_nr(&argvars[off], tp);
12877 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012878 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012880 rettv->v_type = VAR_STRING;
12881 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882
12883 if (win != NULL && varname != NULL)
12884 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012885#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012886 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012887 * otherwise the window is not valid. Only do this when needed,
12888 * autocommands get blocked. */
12889 need_switch_win = !(tp == curtab && win == curwin);
12890 if (!need_switch_win
12891 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12892#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012893 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012894 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012895 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012896 if (get_option_tv(&varname, rettv, 1) == OK)
12897 done = TRUE;
12898 }
12899 else
12900 {
12901 /* Look up the variable. */
12902 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12903 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12904 varname, FALSE);
12905 if (v != NULL)
12906 {
12907 copy_tv(&v->di_tv, rettv);
12908 done = TRUE;
12909 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012912
Bram Moolenaarba117c22015-09-29 16:53:22 +020012913#ifdef FEAT_WINDOWS
12914 if (need_switch_win)
12915 /* restore previous notion of curwin */
12916 restore_win(oldcurwin, oldtabpage, TRUE);
12917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918 }
12919
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012920 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12921 /* use the default return value */
12922 copy_tv(&argvars[off + 2], rettv);
12923
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924 --emsg_off;
12925}
12926
12927/*
12928 * "glob()" function
12929 */
12930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012931f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012933 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012935 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012937 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012938 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012940 if (argvars[1].v_type != VAR_UNKNOWN)
12941 {
12942 if (get_tv_number_chk(&argvars[1], &error))
12943 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012944 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012945 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012946 if (get_tv_number_chk(&argvars[2], &error))
12947 {
12948 rettv->v_type = VAR_LIST;
12949 rettv->vval.v_list = NULL;
12950 }
12951 if (argvars[3].v_type != VAR_UNKNOWN
12952 && get_tv_number_chk(&argvars[3], &error))
12953 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012954 }
12955 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012956 if (!error)
12957 {
12958 ExpandInit(&xpc);
12959 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012960 if (p_wic)
12961 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012962 if (rettv->v_type == VAR_STRING)
12963 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012964 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012965 else if (rettv_list_alloc(rettv) != FAIL)
12966 {
12967 int i;
12968
12969 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12970 NULL, options, WILD_ALL_KEEP);
12971 for (i = 0; i < xpc.xp_numfiles; i++)
12972 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12973
12974 ExpandCleanup(&xpc);
12975 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012976 }
12977 else
12978 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979}
12980
12981/*
12982 * "globpath()" function
12983 */
12984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012985f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012987 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012989 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012990 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012991 garray_T ga;
12992 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012994 /* When the optional second argument is non-zero, don't remove matches
12995 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012996 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012997 if (argvars[2].v_type != VAR_UNKNOWN)
12998 {
12999 if (get_tv_number_chk(&argvars[2], &error))
13000 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013001 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013002 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013003 if (get_tv_number_chk(&argvars[3], &error))
13004 {
13005 rettv->v_type = VAR_LIST;
13006 rettv->vval.v_list = NULL;
13007 }
13008 if (argvars[4].v_type != VAR_UNKNOWN
13009 && get_tv_number_chk(&argvars[4], &error))
13010 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013011 }
13012 }
13013 if (file != NULL && !error)
13014 {
13015 ga_init2(&ga, (int)sizeof(char_u *), 10);
13016 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13017 if (rettv->v_type == VAR_STRING)
13018 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13019 else if (rettv_list_alloc(rettv) != FAIL)
13020 for (i = 0; i < ga.ga_len; ++i)
13021 list_append_string(rettv->vval.v_list,
13022 ((char_u **)(ga.ga_data))[i], -1);
13023 ga_clear_strings(&ga);
13024 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013025 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013026 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027}
13028
13029/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013030 * "glob2regpat()" function
13031 */
13032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013033f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013034{
13035 char_u *pat = get_tv_string_chk(&argvars[0]);
13036
13037 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013038 rettv->vval.v_string = (pat == NULL)
13039 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013040}
13041
13042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 * "has()" function
13044 */
13045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013046f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047{
13048 int i;
13049 char_u *name;
13050 int n = FALSE;
13051 static char *(has_list[]) =
13052 {
13053#ifdef AMIGA
13054 "amiga",
13055# ifdef FEAT_ARP
13056 "arp",
13057# endif
13058#endif
13059#ifdef __BEOS__
13060 "beos",
13061#endif
13062#ifdef MSDOS
13063# ifdef DJGPP
13064 "dos32",
13065# else
13066 "dos16",
13067# endif
13068#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013069#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070 "mac",
13071#endif
13072#if defined(MACOS_X_UNIX)
13073 "macunix",
13074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075#ifdef __QNX__
13076 "qnx",
13077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078#ifdef UNIX
13079 "unix",
13080#endif
13081#ifdef VMS
13082 "vms",
13083#endif
13084#ifdef WIN16
13085 "win16",
13086#endif
13087#ifdef WIN32
13088 "win32",
13089#endif
13090#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13091 "win32unix",
13092#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013093#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 "win64",
13095#endif
13096#ifdef EBCDIC
13097 "ebcdic",
13098#endif
13099#ifndef CASE_INSENSITIVE_FILENAME
13100 "fname_case",
13101#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013102#ifdef HAVE_ACL
13103 "acl",
13104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105#ifdef FEAT_ARABIC
13106 "arabic",
13107#endif
13108#ifdef FEAT_AUTOCMD
13109 "autocmd",
13110#endif
13111#ifdef FEAT_BEVAL
13112 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013113# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13114 "balloon_multiline",
13115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116#endif
13117#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13118 "builtin_terms",
13119# ifdef ALL_BUILTIN_TCAPS
13120 "all_builtin_terms",
13121# endif
13122#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013123#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13124 || defined(FEAT_GUI_W32) \
13125 || defined(FEAT_GUI_MOTIF))
13126 "browsefilter",
13127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128#ifdef FEAT_BYTEOFF
13129 "byte_offset",
13130#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013131#ifdef FEAT_CHANNEL
13132 "channel",
13133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134#ifdef FEAT_CINDENT
13135 "cindent",
13136#endif
13137#ifdef FEAT_CLIENTSERVER
13138 "clientserver",
13139#endif
13140#ifdef FEAT_CLIPBOARD
13141 "clipboard",
13142#endif
13143#ifdef FEAT_CMDL_COMPL
13144 "cmdline_compl",
13145#endif
13146#ifdef FEAT_CMDHIST
13147 "cmdline_hist",
13148#endif
13149#ifdef FEAT_COMMENTS
13150 "comments",
13151#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013152#ifdef FEAT_CONCEAL
13153 "conceal",
13154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155#ifdef FEAT_CRYPT
13156 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013157 "crypt-blowfish",
13158 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159#endif
13160#ifdef FEAT_CSCOPE
13161 "cscope",
13162#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013163#ifdef FEAT_CURSORBIND
13164 "cursorbind",
13165#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013166#ifdef CURSOR_SHAPE
13167 "cursorshape",
13168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169#ifdef DEBUG
13170 "debug",
13171#endif
13172#ifdef FEAT_CON_DIALOG
13173 "dialog_con",
13174#endif
13175#ifdef FEAT_GUI_DIALOG
13176 "dialog_gui",
13177#endif
13178#ifdef FEAT_DIFF
13179 "diff",
13180#endif
13181#ifdef FEAT_DIGRAPHS
13182 "digraphs",
13183#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013184#ifdef FEAT_DIRECTX
13185 "directx",
13186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187#ifdef FEAT_DND
13188 "dnd",
13189#endif
13190#ifdef FEAT_EMACS_TAGS
13191 "emacs_tags",
13192#endif
13193 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013194 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195#ifdef FEAT_SEARCH_EXTRA
13196 "extra_search",
13197#endif
13198#ifdef FEAT_FKMAP
13199 "farsi",
13200#endif
13201#ifdef FEAT_SEARCHPATH
13202 "file_in_path",
13203#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013204#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013205 "filterpipe",
13206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207#ifdef FEAT_FIND_ID
13208 "find_in_path",
13209#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013210#ifdef FEAT_FLOAT
13211 "float",
13212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213#ifdef FEAT_FOLDING
13214 "folding",
13215#endif
13216#ifdef FEAT_FOOTER
13217 "footer",
13218#endif
13219#if !defined(USE_SYSTEM) && defined(UNIX)
13220 "fork",
13221#endif
13222#ifdef FEAT_GETTEXT
13223 "gettext",
13224#endif
13225#ifdef FEAT_GUI
13226 "gui",
13227#endif
13228#ifdef FEAT_GUI_ATHENA
13229# ifdef FEAT_GUI_NEXTAW
13230 "gui_neXtaw",
13231# else
13232 "gui_athena",
13233# endif
13234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235#ifdef FEAT_GUI_GTK
13236 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013239#ifdef FEAT_GUI_GNOME
13240 "gui_gnome",
13241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242#ifdef FEAT_GUI_MAC
13243 "gui_mac",
13244#endif
13245#ifdef FEAT_GUI_MOTIF
13246 "gui_motif",
13247#endif
13248#ifdef FEAT_GUI_PHOTON
13249 "gui_photon",
13250#endif
13251#ifdef FEAT_GUI_W16
13252 "gui_win16",
13253#endif
13254#ifdef FEAT_GUI_W32
13255 "gui_win32",
13256#endif
13257#ifdef FEAT_HANGULIN
13258 "hangul_input",
13259#endif
13260#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13261 "iconv",
13262#endif
13263#ifdef FEAT_INS_EXPAND
13264 "insert_expand",
13265#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013266#ifdef FEAT_JOB
13267 "job",
13268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269#ifdef FEAT_JUMPLIST
13270 "jumplist",
13271#endif
13272#ifdef FEAT_KEYMAP
13273 "keymap",
13274#endif
13275#ifdef FEAT_LANGMAP
13276 "langmap",
13277#endif
13278#ifdef FEAT_LIBCALL
13279 "libcall",
13280#endif
13281#ifdef FEAT_LINEBREAK
13282 "linebreak",
13283#endif
13284#ifdef FEAT_LISP
13285 "lispindent",
13286#endif
13287#ifdef FEAT_LISTCMDS
13288 "listcmds",
13289#endif
13290#ifdef FEAT_LOCALMAP
13291 "localmap",
13292#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013293#ifdef FEAT_LUA
13294# ifndef DYNAMIC_LUA
13295 "lua",
13296# endif
13297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298#ifdef FEAT_MENU
13299 "menu",
13300#endif
13301#ifdef FEAT_SESSION
13302 "mksession",
13303#endif
13304#ifdef FEAT_MODIFY_FNAME
13305 "modify_fname",
13306#endif
13307#ifdef FEAT_MOUSE
13308 "mouse",
13309#endif
13310#ifdef FEAT_MOUSESHAPE
13311 "mouseshape",
13312#endif
13313#if defined(UNIX) || defined(VMS)
13314# ifdef FEAT_MOUSE_DEC
13315 "mouse_dec",
13316# endif
13317# ifdef FEAT_MOUSE_GPM
13318 "mouse_gpm",
13319# endif
13320# ifdef FEAT_MOUSE_JSB
13321 "mouse_jsbterm",
13322# endif
13323# ifdef FEAT_MOUSE_NET
13324 "mouse_netterm",
13325# endif
13326# ifdef FEAT_MOUSE_PTERM
13327 "mouse_pterm",
13328# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013329# ifdef FEAT_MOUSE_SGR
13330 "mouse_sgr",
13331# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013332# ifdef FEAT_SYSMOUSE
13333 "mouse_sysmouse",
13334# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013335# ifdef FEAT_MOUSE_URXVT
13336 "mouse_urxvt",
13337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338# ifdef FEAT_MOUSE_XTERM
13339 "mouse_xterm",
13340# endif
13341#endif
13342#ifdef FEAT_MBYTE
13343 "multi_byte",
13344#endif
13345#ifdef FEAT_MBYTE_IME
13346 "multi_byte_ime",
13347#endif
13348#ifdef FEAT_MULTI_LANG
13349 "multi_lang",
13350#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013351#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013352#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013353 "mzscheme",
13354#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356#ifdef FEAT_OLE
13357 "ole",
13358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359#ifdef FEAT_PATH_EXTRA
13360 "path_extra",
13361#endif
13362#ifdef FEAT_PERL
13363#ifndef DYNAMIC_PERL
13364 "perl",
13365#endif
13366#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013367#ifdef FEAT_PERSISTENT_UNDO
13368 "persistent_undo",
13369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370#ifdef FEAT_PYTHON
13371#ifndef DYNAMIC_PYTHON
13372 "python",
13373#endif
13374#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013375#ifdef FEAT_PYTHON3
13376#ifndef DYNAMIC_PYTHON3
13377 "python3",
13378#endif
13379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380#ifdef FEAT_POSTSCRIPT
13381 "postscript",
13382#endif
13383#ifdef FEAT_PRINTER
13384 "printer",
13385#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013386#ifdef FEAT_PROFILE
13387 "profile",
13388#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013389#ifdef FEAT_RELTIME
13390 "reltime",
13391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392#ifdef FEAT_QUICKFIX
13393 "quickfix",
13394#endif
13395#ifdef FEAT_RIGHTLEFT
13396 "rightleft",
13397#endif
13398#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13399 "ruby",
13400#endif
13401#ifdef FEAT_SCROLLBIND
13402 "scrollbind",
13403#endif
13404#ifdef FEAT_CMDL_INFO
13405 "showcmd",
13406 "cmdline_info",
13407#endif
13408#ifdef FEAT_SIGNS
13409 "signs",
13410#endif
13411#ifdef FEAT_SMARTINDENT
13412 "smartindent",
13413#endif
13414#ifdef FEAT_SNIFF
13415 "sniff",
13416#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013417#ifdef STARTUPTIME
13418 "startuptime",
13419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420#ifdef FEAT_STL_OPT
13421 "statusline",
13422#endif
13423#ifdef FEAT_SUN_WORKSHOP
13424 "sun_workshop",
13425#endif
13426#ifdef FEAT_NETBEANS_INTG
13427 "netbeans_intg",
13428#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013429#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013430 "spell",
13431#endif
13432#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433 "syntax",
13434#endif
13435#if defined(USE_SYSTEM) || !defined(UNIX)
13436 "system",
13437#endif
13438#ifdef FEAT_TAG_BINS
13439 "tag_binary",
13440#endif
13441#ifdef FEAT_TAG_OLDSTATIC
13442 "tag_old_static",
13443#endif
13444#ifdef FEAT_TAG_ANYWHITE
13445 "tag_any_white",
13446#endif
13447#ifdef FEAT_TCL
13448# ifndef DYNAMIC_TCL
13449 "tcl",
13450# endif
13451#endif
13452#ifdef TERMINFO
13453 "terminfo",
13454#endif
13455#ifdef FEAT_TERMRESPONSE
13456 "termresponse",
13457#endif
13458#ifdef FEAT_TEXTOBJ
13459 "textobjects",
13460#endif
13461#ifdef HAVE_TGETENT
13462 "tgetent",
13463#endif
13464#ifdef FEAT_TITLE
13465 "title",
13466#endif
13467#ifdef FEAT_TOOLBAR
13468 "toolbar",
13469#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013470#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13471 "unnamedplus",
13472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473#ifdef FEAT_USR_CMDS
13474 "user-commands", /* was accidentally included in 5.4 */
13475 "user_commands",
13476#endif
13477#ifdef FEAT_VIMINFO
13478 "viminfo",
13479#endif
13480#ifdef FEAT_VERTSPLIT
13481 "vertsplit",
13482#endif
13483#ifdef FEAT_VIRTUALEDIT
13484 "virtualedit",
13485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487#ifdef FEAT_VISUALEXTRA
13488 "visualextra",
13489#endif
13490#ifdef FEAT_VREPLACE
13491 "vreplace",
13492#endif
13493#ifdef FEAT_WILDIGN
13494 "wildignore",
13495#endif
13496#ifdef FEAT_WILDMENU
13497 "wildmenu",
13498#endif
13499#ifdef FEAT_WINDOWS
13500 "windows",
13501#endif
13502#ifdef FEAT_WAK
13503 "winaltkeys",
13504#endif
13505#ifdef FEAT_WRITEBACKUP
13506 "writebackup",
13507#endif
13508#ifdef FEAT_XIM
13509 "xim",
13510#endif
13511#ifdef FEAT_XFONTSET
13512 "xfontset",
13513#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013514#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013515 "xpm",
13516 "xpm_w32", /* for backward compatibility */
13517#else
13518# if defined(HAVE_XPM)
13519 "xpm",
13520# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522#ifdef USE_XSMP
13523 "xsmp",
13524#endif
13525#ifdef USE_XSMP_INTERACT
13526 "xsmp_interact",
13527#endif
13528#ifdef FEAT_XCLIPBOARD
13529 "xterm_clipboard",
13530#endif
13531#ifdef FEAT_XTERM_SAVE
13532 "xterm_save",
13533#endif
13534#if defined(UNIX) && defined(FEAT_X11)
13535 "X11",
13536#endif
13537 NULL
13538 };
13539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 for (i = 0; has_list[i] != NULL; ++i)
13542 if (STRICMP(name, has_list[i]) == 0)
13543 {
13544 n = TRUE;
13545 break;
13546 }
13547
13548 if (n == FALSE)
13549 {
13550 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013551 {
13552 if (name[5] == '-'
13553 && STRLEN(name) > 11
13554 && vim_isdigit(name[6])
13555 && vim_isdigit(name[8])
13556 && vim_isdigit(name[10]))
13557 {
13558 int major = atoi((char *)name + 6);
13559 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013560
13561 /* Expect "patch-9.9.01234". */
13562 n = (major < VIM_VERSION_MAJOR
13563 || (major == VIM_VERSION_MAJOR
13564 && (minor < VIM_VERSION_MINOR
13565 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013566 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013567 }
13568 else
13569 n = has_patch(atoi((char *)name + 5));
13570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 else if (STRICMP(name, "vim_starting") == 0)
13572 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013573#ifdef FEAT_MBYTE
13574 else if (STRICMP(name, "multi_byte_encoding") == 0)
13575 n = has_mbyte;
13576#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013577#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13578 else if (STRICMP(name, "balloon_multiline") == 0)
13579 n = multiline_balloon_available();
13580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581#ifdef DYNAMIC_TCL
13582 else if (STRICMP(name, "tcl") == 0)
13583 n = tcl_enabled(FALSE);
13584#endif
13585#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13586 else if (STRICMP(name, "iconv") == 0)
13587 n = iconv_enabled(FALSE);
13588#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013589#ifdef DYNAMIC_LUA
13590 else if (STRICMP(name, "lua") == 0)
13591 n = lua_enabled(FALSE);
13592#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013593#ifdef DYNAMIC_MZSCHEME
13594 else if (STRICMP(name, "mzscheme") == 0)
13595 n = mzscheme_enabled(FALSE);
13596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597#ifdef DYNAMIC_RUBY
13598 else if (STRICMP(name, "ruby") == 0)
13599 n = ruby_enabled(FALSE);
13600#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013601#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602#ifdef DYNAMIC_PYTHON
13603 else if (STRICMP(name, "python") == 0)
13604 n = python_enabled(FALSE);
13605#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013606#endif
13607#ifdef FEAT_PYTHON3
13608#ifdef DYNAMIC_PYTHON3
13609 else if (STRICMP(name, "python3") == 0)
13610 n = python3_enabled(FALSE);
13611#endif
13612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613#ifdef DYNAMIC_PERL
13614 else if (STRICMP(name, "perl") == 0)
13615 n = perl_enabled(FALSE);
13616#endif
13617#ifdef FEAT_GUI
13618 else if (STRICMP(name, "gui_running") == 0)
13619 n = (gui.in_use || gui.starting);
13620# ifdef FEAT_GUI_W32
13621 else if (STRICMP(name, "gui_win32s") == 0)
13622 n = gui_is_win32s();
13623# endif
13624# ifdef FEAT_BROWSE
13625 else if (STRICMP(name, "browse") == 0)
13626 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13627# endif
13628#endif
13629#ifdef FEAT_SYN_HL
13630 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013631 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632#endif
13633#if defined(WIN3264)
13634 else if (STRICMP(name, "win95") == 0)
13635 n = mch_windows95();
13636#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013637#ifdef FEAT_NETBEANS_INTG
13638 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013639 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 }
13642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013643 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644}
13645
13646/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013647 * "has_key()" function
13648 */
13649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013650f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013651{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013652 if (argvars[0].v_type != VAR_DICT)
13653 {
13654 EMSG(_(e_dictreq));
13655 return;
13656 }
13657 if (argvars[0].vval.v_dict == NULL)
13658 return;
13659
13660 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013661 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013662}
13663
13664/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013665 * "haslocaldir()" function
13666 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013668f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013669{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013670 win_T *wp = NULL;
13671
13672 wp = find_tabwin(&argvars[0], &argvars[1]);
13673 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013674}
13675
13676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 * "hasmapto()" function
13678 */
13679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013680f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681{
13682 char_u *name;
13683 char_u *mode;
13684 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013685 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013687 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013688 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 mode = (char_u *)"nvo";
13690 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013693 if (argvars[2].v_type != VAR_UNKNOWN)
13694 abbr = get_tv_number(&argvars[2]);
13695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696
Bram Moolenaar2c932302006-03-18 21:42:09 +000013697 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013698 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013700 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701}
13702
13703/*
13704 * "histadd()" function
13705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013707f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708{
13709#ifdef FEAT_CMDHIST
13710 int histype;
13711 char_u *str;
13712 char_u buf[NUMBUFLEN];
13713#endif
13714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013715 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 if (check_restricted() || check_secure())
13717 return;
13718#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13720 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 if (histype >= 0)
13722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013723 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 if (*str != NUL)
13725 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013726 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013728 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 return;
13730 }
13731 }
13732#endif
13733}
13734
13735/*
13736 * "histdel()" function
13737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013739f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740{
13741#ifdef FEAT_CMDHIST
13742 int n;
13743 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013744 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13747 if (str == NULL)
13748 n = 0;
13749 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013751 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013752 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013754 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 else
13757 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013758 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013759 get_tv_string_buf(&argvars[1], buf));
13760 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761#endif
13762}
13763
13764/*
13765 * "histget()" function
13766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013768f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769{
13770#ifdef FEAT_CMDHIST
13771 int type;
13772 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013773 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013775 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13776 if (str == NULL)
13777 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013779 {
13780 type = get_histtype(str);
13781 if (argvars[1].v_type == VAR_UNKNOWN)
13782 idx = get_history_idx(type);
13783 else
13784 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13785 /* -1 on type error */
13786 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013789 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013791 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792}
13793
13794/*
13795 * "histnr()" function
13796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013798f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799{
13800 int i;
13801
13802#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013803 char_u *history = get_tv_string_chk(&argvars[0]);
13804
13805 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806 if (i >= HIST_CMD && i < HIST_COUNT)
13807 i = get_history_idx(i);
13808 else
13809#endif
13810 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013811 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812}
13813
13814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 * "highlightID(name)" function
13816 */
13817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013818f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821}
13822
13823/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013824 * "highlight_exists()" function
13825 */
13826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013827f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013828{
13829 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13830}
13831
13832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 * "hostname()" function
13834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013836f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837{
13838 char_u hostname[256];
13839
13840 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841 rettv->v_type = VAR_STRING;
13842 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843}
13844
13845/*
13846 * iconv() function
13847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013849f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850{
13851#ifdef FEAT_MBYTE
13852 char_u buf1[NUMBUFLEN];
13853 char_u buf2[NUMBUFLEN];
13854 char_u *from, *to, *str;
13855 vimconv_T vimconv;
13856#endif
13857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013858 rettv->v_type = VAR_STRING;
13859 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860
13861#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013862 str = get_tv_string(&argvars[0]);
13863 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13864 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 vimconv.vc_type = CONV_NONE;
13866 convert_setup(&vimconv, from, to);
13867
13868 /* If the encodings are equal, no conversion needed. */
13869 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013870 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013872 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873
13874 convert_setup(&vimconv, NULL, NULL);
13875 vim_free(from);
13876 vim_free(to);
13877#endif
13878}
13879
13880/*
13881 * "indent()" function
13882 */
13883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013884f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885{
13886 linenr_T lnum;
13887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013888 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013890 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013892 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893}
13894
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013895/*
13896 * "index()" function
13897 */
13898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013899f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013900{
Bram Moolenaar33570922005-01-25 22:26:29 +000013901 list_T *l;
13902 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013903 long idx = 0;
13904 int ic = FALSE;
13905
13906 rettv->vval.v_number = -1;
13907 if (argvars[0].v_type != VAR_LIST)
13908 {
13909 EMSG(_(e_listreq));
13910 return;
13911 }
13912 l = argvars[0].vval.v_list;
13913 if (l != NULL)
13914 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013915 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013916 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013918 int error = FALSE;
13919
Bram Moolenaar758711c2005-02-02 23:11:38 +000013920 /* Start at specified item. Use the cached index that list_find()
13921 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013922 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013923 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013924 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013925 ic = get_tv_number_chk(&argvars[3], &error);
13926 if (error)
13927 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013928 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013929
Bram Moolenaar758711c2005-02-02 23:11:38 +000013930 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013931 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013932 {
13933 rettv->vval.v_number = idx;
13934 break;
13935 }
13936 }
13937}
13938
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939static int inputsecret_flag = 0;
13940
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013941static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013942
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013944 * This function is used by f_input() and f_inputdialog() functions. The third
13945 * argument to f_input() specifies the type of completion to use at the
13946 * prompt. The third argument to f_inputdialog() specifies the value to return
13947 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 */
13949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013950get_user_input(
13951 typval_T *argvars,
13952 typval_T *rettv,
13953 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 char_u *p = NULL;
13957 int c;
13958 char_u buf[NUMBUFLEN];
13959 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013960 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013961 int xp_type = EXPAND_NOTHING;
13962 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013964 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013965 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966
13967#ifdef NO_CONSOLE_INPUT
13968 /* While starting up, there is no place to enter text. */
13969 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971#endif
13972
13973 cmd_silent = FALSE; /* Want to see the prompt. */
13974 if (prompt != NULL)
13975 {
13976 /* Only the part of the message after the last NL is considered as
13977 * prompt for the command line */
13978 p = vim_strrchr(prompt, '\n');
13979 if (p == NULL)
13980 p = prompt;
13981 else
13982 {
13983 ++p;
13984 c = *p;
13985 *p = NUL;
13986 msg_start();
13987 msg_clr_eos();
13988 msg_puts_attr(prompt, echo_attr);
13989 msg_didout = FALSE;
13990 msg_starthere();
13991 *p = c;
13992 }
13993 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013995 if (argvars[1].v_type != VAR_UNKNOWN)
13996 {
13997 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13998 if (defstr != NULL)
13999 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014001 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014002 {
14003 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014004 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014005 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014006
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014007 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014008 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014009
Bram Moolenaar4463f292005-09-25 22:20:24 +000014010 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14011 if (xp_name == NULL)
14012 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014013
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014014 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014015
Bram Moolenaar4463f292005-09-25 22:20:24 +000014016 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14017 &xp_arg) == FAIL)
14018 return;
14019 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014020 }
14021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014022 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014023 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014024 int save_ex_normal_busy = ex_normal_busy;
14025 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014027 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14028 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014029 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014030 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014031 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014032 && argvars[1].v_type != VAR_UNKNOWN
14033 && argvars[2].v_type != VAR_UNKNOWN)
14034 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14035 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014036
14037 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014039 /* since the user typed this, no need to wait for return */
14040 need_wait_return = FALSE;
14041 msg_didout = FALSE;
14042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043 cmd_silent = cmd_silent_save;
14044}
14045
14046/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014047 * "input()" function
14048 * Also handles inputsecret() when inputsecret is set.
14049 */
14050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014051f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014052{
14053 get_user_input(argvars, rettv, FALSE);
14054}
14055
14056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 * "inputdialog()" function
14058 */
14059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014060f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061{
14062#if defined(FEAT_GUI_TEXTDIALOG)
14063 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14064 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14065 {
14066 char_u *message;
14067 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014068 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014070 message = get_tv_string_chk(&argvars[0]);
14071 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014072 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014073 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 else
14075 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014076 if (message != NULL && defstr != NULL
14077 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014078 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014079 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 else
14081 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014082 if (message != NULL && defstr != NULL
14083 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014084 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085 rettv->vval.v_string = vim_strsave(
14086 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014088 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014090 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091 }
14092 else
14093#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014094 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095}
14096
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014097/*
14098 * "inputlist()" function
14099 */
14100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014101f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014102{
14103 listitem_T *li;
14104 int selected;
14105 int mouse_used;
14106
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014107#ifdef NO_CONSOLE_INPUT
14108 /* While starting up, there is no place to enter text. */
14109 if (no_console_input())
14110 return;
14111#endif
14112 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14113 {
14114 EMSG2(_(e_listarg), "inputlist()");
14115 return;
14116 }
14117
14118 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014119 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014120 lines_left = Rows; /* avoid more prompt */
14121 msg_scroll = TRUE;
14122 msg_clr_eos();
14123
14124 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14125 {
14126 msg_puts(get_tv_string(&li->li_tv));
14127 msg_putchar('\n');
14128 }
14129
14130 /* Ask for choice. */
14131 selected = prompt_for_number(&mouse_used);
14132 if (mouse_used)
14133 selected -= lines_left;
14134
14135 rettv->vval.v_number = selected;
14136}
14137
14138
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14140
14141/*
14142 * "inputrestore()" function
14143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014145f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146{
14147 if (ga_userinput.ga_len > 0)
14148 {
14149 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14151 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014152 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 }
14154 else if (p_verbose > 1)
14155 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014156 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 }
14159}
14160
14161/*
14162 * "inputsave()" function
14163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014165f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014167 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 if (ga_grow(&ga_userinput, 1) == OK)
14169 {
14170 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14171 + ga_userinput.ga_len);
14172 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014173 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 }
14175 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014176 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177}
14178
14179/*
14180 * "inputsecret()" function
14181 */
14182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014183f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184{
14185 ++cmdline_star;
14186 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014187 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 --cmdline_star;
14189 --inputsecret_flag;
14190}
14191
14192/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014193 * "insert()" function
14194 */
14195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014196f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014197{
14198 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014199 listitem_T *item;
14200 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014201 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014202
14203 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014204 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014205 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014206 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014207 {
14208 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209 before = get_tv_number_chk(&argvars[2], &error);
14210 if (error)
14211 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014212
Bram Moolenaar758711c2005-02-02 23:11:38 +000014213 if (before == l->lv_len)
14214 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014215 else
14216 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014217 item = list_find(l, before);
14218 if (item == NULL)
14219 {
14220 EMSGN(_(e_listidx), before);
14221 l = NULL;
14222 }
14223 }
14224 if (l != NULL)
14225 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014226 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014227 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014228 }
14229 }
14230}
14231
14232/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014233 * "invert(expr)" function
14234 */
14235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014236f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014237{
14238 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14239}
14240
14241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 * "isdirectory()" function
14243 */
14244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014245f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014247 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248}
14249
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014250/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014251 * "islocked()" function
14252 */
14253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014254f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014255{
14256 lval_T lv;
14257 char_u *end;
14258 dictitem_T *di;
14259
14260 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014261 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14262 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014263 if (end != NULL && lv.ll_name != NULL)
14264 {
14265 if (*end != NUL)
14266 EMSG(_(e_trailing));
14267 else
14268 {
14269 if (lv.ll_tv == NULL)
14270 {
14271 if (check_changedtick(lv.ll_name))
14272 rettv->vval.v_number = 1; /* always locked */
14273 else
14274 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014275 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014276 if (di != NULL)
14277 {
14278 /* Consider a variable locked when:
14279 * 1. the variable itself is locked
14280 * 2. the value of the variable is locked.
14281 * 3. the List or Dict value is locked.
14282 */
14283 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14284 || tv_islocked(&di->di_tv));
14285 }
14286 }
14287 }
14288 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014289 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014290 else if (lv.ll_newkey != NULL)
14291 EMSG2(_(e_dictkey), lv.ll_newkey);
14292 else if (lv.ll_list != NULL)
14293 /* List item. */
14294 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14295 else
14296 /* Dictionary item. */
14297 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14298 }
14299 }
14300
14301 clear_lval(&lv);
14302}
14303
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014304static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014305
14306/*
14307 * Turn a dict into a list:
14308 * "what" == 0: list of keys
14309 * "what" == 1: list of values
14310 * "what" == 2: list of items
14311 */
14312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014313dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014314{
Bram Moolenaar33570922005-01-25 22:26:29 +000014315 list_T *l2;
14316 dictitem_T *di;
14317 hashitem_T *hi;
14318 listitem_T *li;
14319 listitem_T *li2;
14320 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014321 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014322
Bram Moolenaar8c711452005-01-14 21:53:12 +000014323 if (argvars[0].v_type != VAR_DICT)
14324 {
14325 EMSG(_(e_dictreq));
14326 return;
14327 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014328 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014329 return;
14330
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014331 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014332 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014333
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014334 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014335 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014336 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014337 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014338 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014339 --todo;
14340 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014341
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014342 li = listitem_alloc();
14343 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014344 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014345 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014346
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014347 if (what == 0)
14348 {
14349 /* keys() */
14350 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014351 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014352 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14353 }
14354 else if (what == 1)
14355 {
14356 /* values() */
14357 copy_tv(&di->di_tv, &li->li_tv);
14358 }
14359 else
14360 {
14361 /* items() */
14362 l2 = list_alloc();
14363 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014364 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014365 li->li_tv.vval.v_list = l2;
14366 if (l2 == NULL)
14367 break;
14368 ++l2->lv_refcount;
14369
14370 li2 = listitem_alloc();
14371 if (li2 == NULL)
14372 break;
14373 list_append(l2, li2);
14374 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014375 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014376 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14377
14378 li2 = listitem_alloc();
14379 if (li2 == NULL)
14380 break;
14381 list_append(l2, li2);
14382 copy_tv(&di->di_tv, &li2->li_tv);
14383 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014384 }
14385 }
14386}
14387
14388/*
14389 * "items(dict)" function
14390 */
14391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014392f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014393{
14394 dict_list(argvars, rettv, 2);
14395}
14396
Bram Moolenaar835dc632016-02-07 14:27:38 +010014397#ifdef FEAT_JOB
14398/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014399 * "job_getchannel()" function
14400 */
14401 static void
14402f_job_getchannel(typval_T *argvars, typval_T *rettv)
14403{
14404 if (argvars[0].v_type != VAR_JOB)
14405 EMSG(_(e_invarg));
14406 else
14407 {
14408 job_T *job = argvars[0].vval.v_job;
14409
Bram Moolenaar77073442016-02-13 23:23:53 +010014410 rettv->v_type = VAR_CHANNEL;
14411 rettv->vval.v_channel = job->jv_channel;
14412 if (job->jv_channel != NULL)
14413 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014414 }
14415}
14416
14417/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014418 * "job_start()" function
14419 */
14420 static void
14421f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14422{
14423 job_T *job;
14424 char_u *cmd = NULL;
14425#if defined(UNIX)
14426# define USE_ARGV
14427 char **argv = NULL;
14428 int argc = 0;
14429#else
14430 garray_T ga;
14431#endif
14432
14433 rettv->v_type = VAR_JOB;
14434 job = job_alloc();
14435 rettv->vval.v_job = job;
14436 if (job == NULL)
14437 return;
14438
14439 rettv->vval.v_job->jv_status = JOB_FAILED;
14440#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014441 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014442#endif
14443
14444 if (argvars[0].v_type == VAR_STRING)
14445 {
14446 /* Command is a string. */
14447 cmd = argvars[0].vval.v_string;
14448#ifdef USE_ARGV
14449 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14450 return;
14451 argv[argc] = NULL;
14452#endif
14453 }
14454 else if (argvars[0].v_type != VAR_LIST
14455 || argvars[0].vval.v_list == NULL
14456 || argvars[0].vval.v_list->lv_len < 1)
14457 {
14458 EMSG(_(e_invarg));
14459 return;
14460 }
14461 else
14462 {
14463 list_T *l = argvars[0].vval.v_list;
14464 listitem_T *li;
14465 char_u *s;
14466
14467#ifdef USE_ARGV
14468 /* Pass argv[] to mch_call_shell(). */
14469 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14470 if (argv == NULL)
14471 return;
14472#endif
14473 for (li = l->lv_first; li != NULL; li = li->li_next)
14474 {
14475 s = get_tv_string_chk(&li->li_tv);
14476 if (s == NULL)
14477 goto theend;
14478#ifdef USE_ARGV
14479 argv[argc++] = (char *)s;
14480#else
14481 if (li != l->lv_first)
14482 {
14483 s = vim_strsave_shellescape(s, FALSE, TRUE);
14484 if (s == NULL)
14485 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014486 ga_concat(&ga, s);
14487 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014488 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014489 else
14490 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014491 if (li->li_next != NULL)
14492 ga_append(&ga, ' ');
14493#endif
14494 }
14495#ifdef USE_ARGV
14496 argv[argc] = NULL;
14497#else
14498 cmd = ga.ga_data;
14499#endif
14500 }
14501#ifdef USE_ARGV
14502 mch_start_job(argv, job);
14503#else
14504 mch_start_job(cmd, job);
14505#endif
14506
14507theend:
14508#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014509 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014510#else
14511 vim_free(ga.ga_data);
14512#endif
14513}
14514
14515/*
14516 * "job_status()" function
14517 */
14518 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014519f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014520{
14521 char *result;
14522
14523 if (argvars[0].v_type != VAR_JOB)
14524 EMSG(_(e_invarg));
14525 else
14526 {
14527 job_T *job = argvars[0].vval.v_job;
14528
14529 if (job->jv_status == JOB_ENDED)
14530 /* No need to check, dead is dead. */
14531 result = "dead";
14532 else if (job->jv_status == JOB_FAILED)
14533 result = "fail";
14534 else
14535 result = mch_job_status(job);
14536 rettv->v_type = VAR_STRING;
14537 rettv->vval.v_string = vim_strsave((char_u *)result);
14538 }
14539}
14540
14541/*
14542 * "job_stop()" function
14543 */
14544 static void
14545f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14546{
14547 if (argvars[0].v_type != VAR_JOB)
14548 EMSG(_(e_invarg));
14549 else
14550 {
14551 char_u *arg;
14552
14553 if (argvars[1].v_type == VAR_UNKNOWN)
14554 arg = (char_u *)"";
14555 else
14556 {
14557 arg = get_tv_string_chk(&argvars[1]);
14558 if (arg == NULL)
14559 {
14560 EMSG(_(e_invarg));
14561 return;
14562 }
14563 }
14564 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14565 rettv->vval.v_number = 0;
14566 else
14567 rettv->vval.v_number = 1;
14568 }
14569}
14570#endif
14571
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014573 * "join()" function
14574 */
14575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014576f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014577{
14578 garray_T ga;
14579 char_u *sep;
14580
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014581 if (argvars[0].v_type != VAR_LIST)
14582 {
14583 EMSG(_(e_listreq));
14584 return;
14585 }
14586 if (argvars[0].vval.v_list == NULL)
14587 return;
14588 if (argvars[1].v_type == VAR_UNKNOWN)
14589 sep = (char_u *)" ";
14590 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014591 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014592
14593 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014594
14595 if (sep != NULL)
14596 {
14597 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014598 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014599 ga_append(&ga, NUL);
14600 rettv->vval.v_string = (char_u *)ga.ga_data;
14601 }
14602 else
14603 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014604}
14605
14606/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014607 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014608 */
14609 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014610f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014611{
14612 js_read_T reader;
14613
14614 reader.js_buf = get_tv_string(&argvars[0]);
14615 reader.js_fill = NULL;
14616 reader.js_used = 0;
14617 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14618 EMSG(_(e_invarg));
14619}
14620
14621/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014622 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014623 */
14624 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014625f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014626{
14627 rettv->v_type = VAR_STRING;
14628 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14629}
14630
14631/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014632 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014633 */
14634 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014635f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014636{
14637 js_read_T reader;
14638
14639 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014640 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014641 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014642 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014643 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014644}
14645
14646/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014647 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014648 */
14649 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014650f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014651{
14652 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014653 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014654}
14655
14656/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014657 * "keys()" function
14658 */
14659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014660f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014661{
14662 dict_list(argvars, rettv, 0);
14663}
14664
14665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 * "last_buffer_nr()" function.
14667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014669f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670{
14671 int n = 0;
14672 buf_T *buf;
14673
14674 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14675 if (n < buf->b_fnum)
14676 n = buf->b_fnum;
14677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014678 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014679}
14680
14681/*
14682 * "len()" function
14683 */
14684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014685f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014686{
14687 switch (argvars[0].v_type)
14688 {
14689 case VAR_STRING:
14690 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014691 rettv->vval.v_number = (varnumber_T)STRLEN(
14692 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014693 break;
14694 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014695 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014696 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014697 case VAR_DICT:
14698 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14699 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014700 case VAR_UNKNOWN:
14701 case VAR_SPECIAL:
14702 case VAR_FLOAT:
14703 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014704 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014705 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014706 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014707 break;
14708 }
14709}
14710
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014711static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014712
14713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014714libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014715{
14716#ifdef FEAT_LIBCALL
14717 char_u *string_in;
14718 char_u **string_result;
14719 int nr_result;
14720#endif
14721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014722 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014723 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014724 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014725
14726 if (check_restricted() || check_secure())
14727 return;
14728
14729#ifdef FEAT_LIBCALL
14730 /* The first two args must be strings, otherwise its meaningless */
14731 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14732 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014733 string_in = NULL;
14734 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014735 string_in = argvars[2].vval.v_string;
14736 if (type == VAR_NUMBER)
14737 string_result = NULL;
14738 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014739 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014740 if (mch_libcall(argvars[0].vval.v_string,
14741 argvars[1].vval.v_string,
14742 string_in,
14743 argvars[2].vval.v_number,
14744 string_result,
14745 &nr_result) == OK
14746 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014747 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014748 }
14749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750}
14751
14752/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014753 * "libcall()" function
14754 */
14755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014756f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014757{
14758 libcall_common(argvars, rettv, VAR_STRING);
14759}
14760
14761/*
14762 * "libcallnr()" function
14763 */
14764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014765f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014766{
14767 libcall_common(argvars, rettv, VAR_NUMBER);
14768}
14769
14770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 * "line(string)" function
14772 */
14773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014774f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775{
14776 linenr_T lnum = 0;
14777 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014778 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014780 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781 if (fp != NULL)
14782 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014783 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784}
14785
14786/*
14787 * "line2byte(lnum)" function
14788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014790f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791{
14792#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794#else
14795 linenr_T lnum;
14796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014797 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014799 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014801 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14802 if (rettv->vval.v_number >= 0)
14803 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804#endif
14805}
14806
14807/*
14808 * "lispindent(lnum)" function
14809 */
14810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014811f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812{
14813#ifdef FEAT_LISP
14814 pos_T pos;
14815 linenr_T lnum;
14816
14817 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014818 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14820 {
14821 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014822 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 curwin->w_cursor = pos;
14824 }
14825 else
14826#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828}
14829
14830/*
14831 * "localtime()" function
14832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014834f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014836 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837}
14838
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014839static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840
14841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014842get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843{
14844 char_u *keys;
14845 char_u *which;
14846 char_u buf[NUMBUFLEN];
14847 char_u *keys_buf = NULL;
14848 char_u *rhs;
14849 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014850 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014851 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014852 mapblock_T *mp;
14853 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854
14855 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014856 rettv->v_type = VAR_STRING;
14857 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014859 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860 if (*keys == NUL)
14861 return;
14862
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014863 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014865 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014866 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014867 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014868 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014869 if (argvars[3].v_type != VAR_UNKNOWN)
14870 get_dict = get_tv_number(&argvars[3]);
14871 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 else
14874 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014875 if (which == NULL)
14876 return;
14877
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878 mode = get_map_mode(&which, 0);
14879
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014880 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014881 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014883
14884 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014886 /* Return a string. */
14887 if (rhs != NULL)
14888 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889
Bram Moolenaarbd743252010-10-20 21:23:33 +020014890 }
14891 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14892 {
14893 /* Return a dictionary. */
14894 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14895 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14896 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897
Bram Moolenaarbd743252010-10-20 21:23:33 +020014898 dict_add_nr_str(dict, "lhs", 0L, lhs);
14899 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14900 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14901 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14902 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14903 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14904 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014905 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014906 dict_add_nr_str(dict, "mode", 0L, mapmode);
14907
14908 vim_free(lhs);
14909 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 }
14911}
14912
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014913#ifdef FEAT_FLOAT
14914/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014915 * "log()" function
14916 */
14917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014918f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014919{
14920 float_T f;
14921
14922 rettv->v_type = VAR_FLOAT;
14923 if (get_float_arg(argvars, &f) == OK)
14924 rettv->vval.v_float = log(f);
14925 else
14926 rettv->vval.v_float = 0.0;
14927}
14928
14929/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014930 * "log10()" function
14931 */
14932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014933f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014934{
14935 float_T f;
14936
14937 rettv->v_type = VAR_FLOAT;
14938 if (get_float_arg(argvars, &f) == OK)
14939 rettv->vval.v_float = log10(f);
14940 else
14941 rettv->vval.v_float = 0.0;
14942}
14943#endif
14944
Bram Moolenaar1dced572012-04-05 16:54:08 +020014945#ifdef FEAT_LUA
14946/*
14947 * "luaeval()" function
14948 */
14949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014950f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014951{
14952 char_u *str;
14953 char_u buf[NUMBUFLEN];
14954
14955 str = get_tv_string_buf(&argvars[0], buf);
14956 do_luaeval(str, argvars + 1, rettv);
14957}
14958#endif
14959
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014961 * "map()" function
14962 */
14963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014964f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014965{
14966 filter_map(argvars, rettv, TRUE);
14967}
14968
14969/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014970 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971 */
14972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014973f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014975 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976}
14977
14978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014979 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 */
14981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014982f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985}
14986
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014987static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988
14989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014990find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014992 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014993 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014994 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995 char_u *pat;
14996 regmatch_T regmatch;
14997 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014998 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999 char_u *save_cpo;
15000 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015001 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015002 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015003 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015004 list_T *l = NULL;
15005 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015006 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015007 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008
15009 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15010 save_cpo = p_cpo;
15011 p_cpo = (char_u *)"";
15012
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015013 rettv->vval.v_number = -1;
15014 if (type == 3)
15015 {
15016 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015017 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015018 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015019 }
15020 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015022 rettv->v_type = VAR_STRING;
15023 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015026 if (argvars[0].v_type == VAR_LIST)
15027 {
15028 if ((l = argvars[0].vval.v_list) == NULL)
15029 goto theend;
15030 li = l->lv_first;
15031 }
15032 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015033 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015034 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015035 len = (long)STRLEN(str);
15036 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015038 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15039 if (pat == NULL)
15040 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015041
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015042 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015044 int error = FALSE;
15045
15046 start = get_tv_number_chk(&argvars[2], &error);
15047 if (error)
15048 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015049 if (l != NULL)
15050 {
15051 li = list_find(l, start);
15052 if (li == NULL)
15053 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015054 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015055 }
15056 else
15057 {
15058 if (start < 0)
15059 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015060 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015061 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015062 /* When "count" argument is there ignore matches before "start",
15063 * otherwise skip part of the string. Differs when pattern is "^"
15064 * or "\<". */
15065 if (argvars[3].v_type != VAR_UNKNOWN)
15066 startcol = start;
15067 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015068 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015069 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015070 len -= start;
15071 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015072 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015073
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015074 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015075 nth = get_tv_number_chk(&argvars[3], &error);
15076 if (error)
15077 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078 }
15079
15080 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15081 if (regmatch.regprog != NULL)
15082 {
15083 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015084
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015085 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015086 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015087 if (l != NULL)
15088 {
15089 if (li == NULL)
15090 {
15091 match = FALSE;
15092 break;
15093 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015094 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015095 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015096 if (str == NULL)
15097 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015098 }
15099
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015100 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015101
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015102 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015103 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015104 if (l == NULL && !match)
15105 break;
15106
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015107 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015108 if (l != NULL)
15109 {
15110 li = li->li_next;
15111 ++idx;
15112 }
15113 else
15114 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015115#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015116 startcol = (colnr_T)(regmatch.startp[0]
15117 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015118#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015119 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015120#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015121 if (startcol > (colnr_T)len
15122 || str + startcol <= regmatch.startp[0])
15123 {
15124 match = FALSE;
15125 break;
15126 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015127 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015128 }
15129
15130 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015132 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015133 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015134 int i;
15135
15136 /* return list with matched string and submatches */
15137 for (i = 0; i < NSUBEXP; ++i)
15138 {
15139 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015140 {
15141 if (list_append_string(rettv->vval.v_list,
15142 (char_u *)"", 0) == FAIL)
15143 break;
15144 }
15145 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015146 regmatch.startp[i],
15147 (int)(regmatch.endp[i] - regmatch.startp[i]))
15148 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015149 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015150 }
15151 }
15152 else if (type == 2)
15153 {
15154 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015155 if (l != NULL)
15156 copy_tv(&li->li_tv, rettv);
15157 else
15158 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015160 }
15161 else if (l != NULL)
15162 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163 else
15164 {
15165 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015166 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167 (varnumber_T)(regmatch.startp[0] - str);
15168 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015169 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015171 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172 }
15173 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015174 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175 }
15176
15177theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015178 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 p_cpo = save_cpo;
15180}
15181
15182/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015183 * "match()" function
15184 */
15185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015186f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015187{
15188 find_some_match(argvars, rettv, 1);
15189}
15190
15191/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015192 * "matchadd()" function
15193 */
15194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015195f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015196{
15197#ifdef FEAT_SEARCH_EXTRA
15198 char_u buf[NUMBUFLEN];
15199 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15200 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15201 int prio = 10; /* default priority */
15202 int id = -1;
15203 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015204 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015205
15206 rettv->vval.v_number = -1;
15207
15208 if (grp == NULL || pat == NULL)
15209 return;
15210 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015211 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015212 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015213 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015214 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015215 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015216 if (argvars[4].v_type != VAR_UNKNOWN)
15217 {
15218 if (argvars[4].v_type != VAR_DICT)
15219 {
15220 EMSG(_(e_dictreq));
15221 return;
15222 }
15223 if (dict_find(argvars[4].vval.v_dict,
15224 (char_u *)"conceal", -1) != NULL)
15225 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15226 (char_u *)"conceal", FALSE);
15227 }
15228 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015229 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015230 if (error == TRUE)
15231 return;
15232 if (id >= 1 && id <= 3)
15233 {
15234 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15235 return;
15236 }
15237
Bram Moolenaar6561d522015-07-21 15:48:27 +020015238 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15239 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015240#endif
15241}
15242
15243/*
15244 * "matchaddpos()" function
15245 */
15246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015247f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015248{
15249#ifdef FEAT_SEARCH_EXTRA
15250 char_u buf[NUMBUFLEN];
15251 char_u *group;
15252 int prio = 10;
15253 int id = -1;
15254 int error = FALSE;
15255 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015256 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015257
15258 rettv->vval.v_number = -1;
15259
15260 group = get_tv_string_buf_chk(&argvars[0], buf);
15261 if (group == NULL)
15262 return;
15263
15264 if (argvars[1].v_type != VAR_LIST)
15265 {
15266 EMSG2(_(e_listarg), "matchaddpos()");
15267 return;
15268 }
15269 l = argvars[1].vval.v_list;
15270 if (l == NULL)
15271 return;
15272
15273 if (argvars[2].v_type != VAR_UNKNOWN)
15274 {
15275 prio = get_tv_number_chk(&argvars[2], &error);
15276 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015277 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015278 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015279 if (argvars[4].v_type != VAR_UNKNOWN)
15280 {
15281 if (argvars[4].v_type != VAR_DICT)
15282 {
15283 EMSG(_(e_dictreq));
15284 return;
15285 }
15286 if (dict_find(argvars[4].vval.v_dict,
15287 (char_u *)"conceal", -1) != NULL)
15288 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15289 (char_u *)"conceal", FALSE);
15290 }
15291 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015292 }
15293 if (error == TRUE)
15294 return;
15295
15296 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15297 if (id == 1 || id == 2)
15298 {
15299 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15300 return;
15301 }
15302
Bram Moolenaar6561d522015-07-21 15:48:27 +020015303 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15304 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015305#endif
15306}
15307
15308/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015309 * "matcharg()" function
15310 */
15311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015312f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015313{
15314 if (rettv_list_alloc(rettv) == OK)
15315 {
15316#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015317 int id = get_tv_number(&argvars[0]);
15318 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015319
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015320 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015321 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015322 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15323 {
15324 list_append_string(rettv->vval.v_list,
15325 syn_id2name(m->hlg_id), -1);
15326 list_append_string(rettv->vval.v_list, m->pattern, -1);
15327 }
15328 else
15329 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015330 list_append_string(rettv->vval.v_list, NULL, -1);
15331 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015332 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015333 }
15334#endif
15335 }
15336}
15337
15338/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015339 * "matchdelete()" function
15340 */
15341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015342f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015343{
15344#ifdef FEAT_SEARCH_EXTRA
15345 rettv->vval.v_number = match_delete(curwin,
15346 (int)get_tv_number(&argvars[0]), TRUE);
15347#endif
15348}
15349
15350/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015351 * "matchend()" function
15352 */
15353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015354f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015355{
15356 find_some_match(argvars, rettv, 0);
15357}
15358
15359/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015360 * "matchlist()" function
15361 */
15362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015363f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015364{
15365 find_some_match(argvars, rettv, 3);
15366}
15367
15368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015369 * "matchstr()" function
15370 */
15371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015372f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015373{
15374 find_some_match(argvars, rettv, 2);
15375}
15376
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015377static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015378
15379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015380max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015381{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015382 long n = 0;
15383 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015384 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015385
15386 if (argvars[0].v_type == VAR_LIST)
15387 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015388 list_T *l;
15389 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015390
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015391 l = argvars[0].vval.v_list;
15392 if (l != NULL)
15393 {
15394 li = l->lv_first;
15395 if (li != NULL)
15396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015397 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015398 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015399 {
15400 li = li->li_next;
15401 if (li == NULL)
15402 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015403 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015404 if (domax ? i > n : i < n)
15405 n = i;
15406 }
15407 }
15408 }
15409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015410 else if (argvars[0].v_type == VAR_DICT)
15411 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015412 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015413 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015414 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015415 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015416
15417 d = argvars[0].vval.v_dict;
15418 if (d != NULL)
15419 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015420 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015421 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015422 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015423 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015424 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015425 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015426 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015427 if (first)
15428 {
15429 n = i;
15430 first = FALSE;
15431 }
15432 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015433 n = i;
15434 }
15435 }
15436 }
15437 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015438 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015439 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015440 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015441}
15442
15443/*
15444 * "max()" function
15445 */
15446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015447f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015448{
15449 max_min(argvars, rettv, TRUE);
15450}
15451
15452/*
15453 * "min()" function
15454 */
15455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015456f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015457{
15458 max_min(argvars, rettv, FALSE);
15459}
15460
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015461static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015462
15463/*
15464 * Create the directory in which "dir" is located, and higher levels when
15465 * needed.
15466 */
15467 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015468mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015469{
15470 char_u *p;
15471 char_u *updir;
15472 int r = FAIL;
15473
15474 /* Get end of directory name in "dir".
15475 * We're done when it's "/" or "c:/". */
15476 p = gettail_sep(dir);
15477 if (p <= get_past_head(dir))
15478 return OK;
15479
15480 /* If the directory exists we're done. Otherwise: create it.*/
15481 updir = vim_strnsave(dir, (int)(p - dir));
15482 if (updir == NULL)
15483 return FAIL;
15484 if (mch_isdir(updir))
15485 r = OK;
15486 else if (mkdir_recurse(updir, prot) == OK)
15487 r = vim_mkdir_emsg(updir, prot);
15488 vim_free(updir);
15489 return r;
15490}
15491
15492#ifdef vim_mkdir
15493/*
15494 * "mkdir()" function
15495 */
15496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015497f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015498{
15499 char_u *dir;
15500 char_u buf[NUMBUFLEN];
15501 int prot = 0755;
15502
15503 rettv->vval.v_number = FAIL;
15504 if (check_restricted() || check_secure())
15505 return;
15506
15507 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015508 if (*dir == NUL)
15509 rettv->vval.v_number = FAIL;
15510 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015511 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015512 if (*gettail(dir) == NUL)
15513 /* remove trailing slashes */
15514 *gettail_sep(dir) = NUL;
15515
15516 if (argvars[1].v_type != VAR_UNKNOWN)
15517 {
15518 if (argvars[2].v_type != VAR_UNKNOWN)
15519 prot = get_tv_number_chk(&argvars[2], NULL);
15520 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15521 mkdir_recurse(dir, prot);
15522 }
15523 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015524 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015525}
15526#endif
15527
Bram Moolenaar0d660222005-01-07 21:51:51 +000015528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 * "mode()" function
15530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015532f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015534 char_u buf[3];
15535
15536 buf[1] = NUL;
15537 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 if (VIsual_active)
15540 {
15541 if (VIsual_select)
15542 buf[0] = VIsual_mode + 's' - 'v';
15543 else
15544 buf[0] = VIsual_mode;
15545 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015546 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015547 || State == CONFIRM)
15548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015550 if (State == ASKMORE)
15551 buf[1] = 'm';
15552 else if (State == CONFIRM)
15553 buf[1] = '?';
15554 }
15555 else if (State == EXTERNCMD)
15556 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 else if (State & INSERT)
15558 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015559#ifdef FEAT_VREPLACE
15560 if (State & VREPLACE_FLAG)
15561 {
15562 buf[0] = 'R';
15563 buf[1] = 'v';
15564 }
15565 else
15566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 if (State & REPLACE_FLAG)
15568 buf[0] = 'R';
15569 else
15570 buf[0] = 'i';
15571 }
15572 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015575 if (exmode_active)
15576 buf[1] = 'v';
15577 }
15578 else if (exmode_active)
15579 {
15580 buf[0] = 'c';
15581 buf[1] = 'e';
15582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015586 if (finish_op)
15587 buf[1] = 'o';
15588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015590 /* Clear out the minor mode when the argument is not a non-zero number or
15591 * non-empty string. */
15592 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015593 buf[1] = NUL;
15594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015595 rettv->vval.v_string = vim_strsave(buf);
15596 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597}
15598
Bram Moolenaar429fa852013-04-15 12:27:36 +020015599#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015600/*
15601 * "mzeval()" function
15602 */
15603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015604f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015605{
15606 char_u *str;
15607 char_u buf[NUMBUFLEN];
15608
15609 str = get_tv_string_buf(&argvars[0], buf);
15610 do_mzeval(str, rettv);
15611}
Bram Moolenaar75676462013-01-30 14:55:42 +010015612
15613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015614mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015615{
15616 typval_T argvars[3];
15617
15618 argvars[0].v_type = VAR_STRING;
15619 argvars[0].vval.v_string = name;
15620 copy_tv(args, &argvars[1]);
15621 argvars[2].v_type = VAR_UNKNOWN;
15622 f_call(argvars, rettv);
15623 clear_tv(&argvars[1]);
15624}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015625#endif
15626
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015628 * "nextnonblank()" function
15629 */
15630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015631f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015632{
15633 linenr_T lnum;
15634
15635 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015638 {
15639 lnum = 0;
15640 break;
15641 }
15642 if (*skipwhite(ml_get(lnum)) != NUL)
15643 break;
15644 }
15645 rettv->vval.v_number = lnum;
15646}
15647
15648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649 * "nr2char()" function
15650 */
15651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015652f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653{
15654 char_u buf[NUMBUFLEN];
15655
15656#ifdef FEAT_MBYTE
15657 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015658 {
15659 int utf8 = 0;
15660
15661 if (argvars[1].v_type != VAR_UNKNOWN)
15662 utf8 = get_tv_number_chk(&argvars[1], NULL);
15663 if (utf8)
15664 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15665 else
15666 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 else
15669#endif
15670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015671 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 buf[1] = NUL;
15673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015674 rettv->v_type = VAR_STRING;
15675 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015676}
15677
15678/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015679 * "or(expr, expr)" function
15680 */
15681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015682f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015683{
15684 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15685 | get_tv_number_chk(&argvars[1], NULL);
15686}
15687
15688/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015689 * "pathshorten()" function
15690 */
15691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015692f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015693{
15694 char_u *p;
15695
15696 rettv->v_type = VAR_STRING;
15697 p = get_tv_string_chk(&argvars[0]);
15698 if (p == NULL)
15699 rettv->vval.v_string = NULL;
15700 else
15701 {
15702 p = vim_strsave(p);
15703 rettv->vval.v_string = p;
15704 if (p != NULL)
15705 shorten_dir(p);
15706 }
15707}
15708
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015709#ifdef FEAT_PERL
15710/*
15711 * "perleval()" function
15712 */
15713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015714f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015715{
15716 char_u *str;
15717 char_u buf[NUMBUFLEN];
15718
15719 str = get_tv_string_buf(&argvars[0], buf);
15720 do_perleval(str, rettv);
15721}
15722#endif
15723
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015724#ifdef FEAT_FLOAT
15725/*
15726 * "pow()" function
15727 */
15728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015729f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015730{
15731 float_T fx, fy;
15732
15733 rettv->v_type = VAR_FLOAT;
15734 if (get_float_arg(argvars, &fx) == OK
15735 && get_float_arg(&argvars[1], &fy) == OK)
15736 rettv->vval.v_float = pow(fx, fy);
15737 else
15738 rettv->vval.v_float = 0.0;
15739}
15740#endif
15741
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015742/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743 * "prevnonblank()" function
15744 */
15745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015746f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015747{
15748 linenr_T lnum;
15749
15750 lnum = get_tv_lnum(argvars);
15751 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15752 lnum = 0;
15753 else
15754 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15755 --lnum;
15756 rettv->vval.v_number = lnum;
15757}
15758
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015759/* This dummy va_list is here because:
15760 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15761 * - locally in the function results in a "used before set" warning
15762 * - using va_start() to initialize it gives "function with fixed args" error */
15763static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015764
Bram Moolenaar8c711452005-01-14 21:53:12 +000015765/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015766 * "printf()" function
15767 */
15768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015769f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015770{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015771 char_u buf[NUMBUFLEN];
15772 int len;
15773 char_u *s;
15774 int saved_did_emsg = did_emsg;
15775 char *fmt;
15776
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015777 rettv->v_type = VAR_STRING;
15778 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015779
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015780 /* Get the required length, allocate the buffer and do it for real. */
15781 did_emsg = FALSE;
15782 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15783 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15784 if (!did_emsg)
15785 {
15786 s = alloc(len + 1);
15787 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015788 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015789 rettv->vval.v_string = s;
15790 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015791 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015792 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015793 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015794}
15795
15796/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015797 * "pumvisible()" function
15798 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015800f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015801{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015802#ifdef FEAT_INS_EXPAND
15803 if (pum_visible())
15804 rettv->vval.v_number = 1;
15805#endif
15806}
15807
Bram Moolenaardb913952012-06-29 12:54:53 +020015808#ifdef FEAT_PYTHON3
15809/*
15810 * "py3eval()" function
15811 */
15812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015813f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015814{
15815 char_u *str;
15816 char_u buf[NUMBUFLEN];
15817
15818 str = get_tv_string_buf(&argvars[0], buf);
15819 do_py3eval(str, rettv);
15820}
15821#endif
15822
15823#ifdef FEAT_PYTHON
15824/*
15825 * "pyeval()" function
15826 */
15827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015828f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015829{
15830 char_u *str;
15831 char_u buf[NUMBUFLEN];
15832
15833 str = get_tv_string_buf(&argvars[0], buf);
15834 do_pyeval(str, rettv);
15835}
15836#endif
15837
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015838/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015839 * "range()" function
15840 */
15841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015842f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015843{
15844 long start;
15845 long end;
15846 long stride = 1;
15847 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015848 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015850 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015851 if (argvars[1].v_type == VAR_UNKNOWN)
15852 {
15853 end = start - 1;
15854 start = 0;
15855 }
15856 else
15857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015858 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015859 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015860 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015861 }
15862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015863 if (error)
15864 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015865 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015866 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015867 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015868 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015869 else
15870 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015871 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015872 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015873 if (list_append_number(rettv->vval.v_list,
15874 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015875 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015876 }
15877}
15878
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015879/*
15880 * "readfile()" function
15881 */
15882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015883f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015884{
15885 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015886 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015887 char_u *fname;
15888 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015889 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15890 int io_size = sizeof(buf);
15891 int readlen; /* size of last fread() */
15892 char_u *prev = NULL; /* previously read bytes, if any */
15893 long prevlen = 0; /* length of data in prev */
15894 long prevsize = 0; /* size of prev buffer */
15895 long maxline = MAXLNUM;
15896 long cnt = 0;
15897 char_u *p; /* position in buf */
15898 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015899
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015900 if (argvars[1].v_type != VAR_UNKNOWN)
15901 {
15902 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15903 binary = TRUE;
15904 if (argvars[2].v_type != VAR_UNKNOWN)
15905 maxline = get_tv_number(&argvars[2]);
15906 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015907
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015908 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015909 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015910
15911 /* Always open the file in binary mode, library functions have a mind of
15912 * their own about CR-LF conversion. */
15913 fname = get_tv_string(&argvars[0]);
15914 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15915 {
15916 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15917 return;
15918 }
15919
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015920 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015921 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015922 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015923
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015924 /* This for loop processes what was read, but is also entered at end
15925 * of file so that either:
15926 * - an incomplete line gets written
15927 * - a "binary" file gets an empty line at the end if it ends in a
15928 * newline. */
15929 for (p = buf, start = buf;
15930 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15931 ++p)
15932 {
15933 if (*p == '\n' || readlen <= 0)
15934 {
15935 listitem_T *li;
15936 char_u *s = NULL;
15937 long_u len = p - start;
15938
15939 /* Finished a line. Remove CRs before NL. */
15940 if (readlen > 0 && !binary)
15941 {
15942 while (len > 0 && start[len - 1] == '\r')
15943 --len;
15944 /* removal may cross back to the "prev" string */
15945 if (len == 0)
15946 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15947 --prevlen;
15948 }
15949 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015950 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015951 else
15952 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015953 /* Change "prev" buffer to be the right size. This way
15954 * the bytes are only copied once, and very long lines are
15955 * allocated only once. */
15956 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015957 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015958 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015959 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015960 prev = NULL; /* the list will own the string */
15961 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015962 }
15963 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015964 if (s == NULL)
15965 {
15966 do_outofmem_msg((long_u) prevlen + len + 1);
15967 failed = TRUE;
15968 break;
15969 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015970
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015971 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015972 {
15973 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015974 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015975 break;
15976 }
15977 li->li_tv.v_type = VAR_STRING;
15978 li->li_tv.v_lock = 0;
15979 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015980 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015981
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015982 start = p + 1; /* step over newline */
15983 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015984 break;
15985 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015986 else if (*p == NUL)
15987 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015988#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015989 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15990 * when finding the BF and check the previous two bytes. */
15991 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015992 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015993 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15994 * + 1, these may be in the "prev" string. */
15995 char_u back1 = p >= buf + 1 ? p[-1]
15996 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15997 char_u back2 = p >= buf + 2 ? p[-2]
15998 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15999 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016000
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016001 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016002 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016003 char_u *dest = p - 2;
16004
16005 /* Usually a BOM is at the beginning of a file, and so at
16006 * the beginning of a line; then we can just step over it.
16007 */
16008 if (start == dest)
16009 start = p + 1;
16010 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016011 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016012 /* have to shuffle buf to close gap */
16013 int adjust_prevlen = 0;
16014
16015 if (dest < buf)
16016 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016017 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016018 dest = buf;
16019 }
16020 if (readlen > p - buf + 1)
16021 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16022 readlen -= 3 - adjust_prevlen;
16023 prevlen -= adjust_prevlen;
16024 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016025 }
16026 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016027 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016028#endif
16029 } /* for */
16030
16031 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16032 break;
16033 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016034 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016035 /* There's part of a line in buf, store it in "prev". */
16036 if (p - start + prevlen >= prevsize)
16037 {
16038 /* need bigger "prev" buffer */
16039 char_u *newprev;
16040
16041 /* A common use case is ordinary text files and "prev" gets a
16042 * fragment of a line, so the first allocation is made
16043 * small, to avoid repeatedly 'allocing' large and
16044 * 'reallocing' small. */
16045 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016046 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016047 else
16048 {
16049 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016050 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016051 prevsize = grow50pc > growmin ? grow50pc : growmin;
16052 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016053 newprev = prev == NULL ? alloc(prevsize)
16054 : vim_realloc(prev, prevsize);
16055 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016056 {
16057 do_outofmem_msg((long_u)prevsize);
16058 failed = TRUE;
16059 break;
16060 }
16061 prev = newprev;
16062 }
16063 /* Add the line part to end of "prev". */
16064 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016065 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016066 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016067 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016068
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016069 /*
16070 * For a negative line count use only the lines at the end of the file,
16071 * free the rest.
16072 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016073 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016074 while (cnt > -maxline)
16075 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016076 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016077 --cnt;
16078 }
16079
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016080 if (failed)
16081 {
16082 list_free(rettv->vval.v_list, TRUE);
16083 /* readfile doc says an empty list is returned on error */
16084 rettv->vval.v_list = list_alloc();
16085 }
16086
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016087 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016088 fclose(fd);
16089}
16090
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016091#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016092static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016093
16094/*
16095 * Convert a List to proftime_T.
16096 * Return FAIL when there is something wrong.
16097 */
16098 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016099list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016100{
16101 long n1, n2;
16102 int error = FALSE;
16103
16104 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16105 || arg->vval.v_list->lv_len != 2)
16106 return FAIL;
16107 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16108 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16109# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016110 tm->HighPart = n1;
16111 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016112# else
16113 tm->tv_sec = n1;
16114 tm->tv_usec = n2;
16115# endif
16116 return error ? FAIL : OK;
16117}
16118#endif /* FEAT_RELTIME */
16119
16120/*
16121 * "reltime()" function
16122 */
16123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016124f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016125{
16126#ifdef FEAT_RELTIME
16127 proftime_T res;
16128 proftime_T start;
16129
16130 if (argvars[0].v_type == VAR_UNKNOWN)
16131 {
16132 /* No arguments: get current time. */
16133 profile_start(&res);
16134 }
16135 else if (argvars[1].v_type == VAR_UNKNOWN)
16136 {
16137 if (list2proftime(&argvars[0], &res) == FAIL)
16138 return;
16139 profile_end(&res);
16140 }
16141 else
16142 {
16143 /* Two arguments: compute the difference. */
16144 if (list2proftime(&argvars[0], &start) == FAIL
16145 || list2proftime(&argvars[1], &res) == FAIL)
16146 return;
16147 profile_sub(&res, &start);
16148 }
16149
16150 if (rettv_list_alloc(rettv) == OK)
16151 {
16152 long n1, n2;
16153
16154# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016155 n1 = res.HighPart;
16156 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016157# else
16158 n1 = res.tv_sec;
16159 n2 = res.tv_usec;
16160# endif
16161 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16162 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16163 }
16164#endif
16165}
16166
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016167#ifdef FEAT_FLOAT
16168/*
16169 * "reltimefloat()" function
16170 */
16171 static void
16172f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16173{
16174# ifdef FEAT_RELTIME
16175 proftime_T tm;
16176# endif
16177
16178 rettv->v_type = VAR_FLOAT;
16179 rettv->vval.v_float = 0;
16180# ifdef FEAT_RELTIME
16181 if (list2proftime(&argvars[0], &tm) == OK)
16182 rettv->vval.v_float = profile_float(&tm);
16183# endif
16184}
16185#endif
16186
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016187/*
16188 * "reltimestr()" function
16189 */
16190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016191f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016192{
16193#ifdef FEAT_RELTIME
16194 proftime_T tm;
16195#endif
16196
16197 rettv->v_type = VAR_STRING;
16198 rettv->vval.v_string = NULL;
16199#ifdef FEAT_RELTIME
16200 if (list2proftime(&argvars[0], &tm) == OK)
16201 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16202#endif
16203}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016204
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016206static void make_connection(void);
16207static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016208
16209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016210make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016211{
16212 if (X_DISPLAY == NULL
16213# ifdef FEAT_GUI
16214 && !gui.in_use
16215# endif
16216 )
16217 {
16218 x_force_connect = TRUE;
16219 setup_term_clip();
16220 x_force_connect = FALSE;
16221 }
16222}
16223
16224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016225check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016226{
16227 make_connection();
16228 if (X_DISPLAY == NULL)
16229 {
16230 EMSG(_("E240: No connection to Vim server"));
16231 return FAIL;
16232 }
16233 return OK;
16234}
16235#endif
16236
16237#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016238static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016239
16240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016241remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242{
16243 char_u *server_name;
16244 char_u *keys;
16245 char_u *r = NULL;
16246 char_u buf[NUMBUFLEN];
16247# ifdef WIN32
16248 HWND w;
16249# else
16250 Window w;
16251# endif
16252
16253 if (check_restricted() || check_secure())
16254 return;
16255
16256# ifdef FEAT_X11
16257 if (check_connection() == FAIL)
16258 return;
16259# endif
16260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016261 server_name = get_tv_string_chk(&argvars[0]);
16262 if (server_name == NULL)
16263 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016264 keys = get_tv_string_buf(&argvars[1], buf);
16265# ifdef WIN32
16266 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16267# else
16268 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16269 < 0)
16270# endif
16271 {
16272 if (r != NULL)
16273 EMSG(r); /* sending worked but evaluation failed */
16274 else
16275 EMSG2(_("E241: Unable to send to %s"), server_name);
16276 return;
16277 }
16278
16279 rettv->vval.v_string = r;
16280
16281 if (argvars[2].v_type != VAR_UNKNOWN)
16282 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016283 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016284 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016285 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016287 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016288 v.di_tv.v_type = VAR_STRING;
16289 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016290 idvar = get_tv_string_chk(&argvars[2]);
16291 if (idvar != NULL)
16292 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016293 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016294 }
16295}
16296#endif
16297
16298/*
16299 * "remote_expr()" function
16300 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016302f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016303{
16304 rettv->v_type = VAR_STRING;
16305 rettv->vval.v_string = NULL;
16306#ifdef FEAT_CLIENTSERVER
16307 remote_common(argvars, rettv, TRUE);
16308#endif
16309}
16310
16311/*
16312 * "remote_foreground()" function
16313 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016315f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016317#ifdef FEAT_CLIENTSERVER
16318# ifdef WIN32
16319 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016320 {
16321 char_u *server_name = get_tv_string_chk(&argvars[0]);
16322
16323 if (server_name != NULL)
16324 serverForeground(server_name);
16325 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016326# else
16327 /* Send a foreground() expression to the server. */
16328 argvars[1].v_type = VAR_STRING;
16329 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16330 argvars[2].v_type = VAR_UNKNOWN;
16331 remote_common(argvars, rettv, TRUE);
16332 vim_free(argvars[1].vval.v_string);
16333# endif
16334#endif
16335}
16336
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016338f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016339{
16340#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016341 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016342 char_u *s = NULL;
16343# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016344 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016345# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016346 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016347
16348 if (check_restricted() || check_secure())
16349 {
16350 rettv->vval.v_number = -1;
16351 return;
16352 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016353 serverid = get_tv_string_chk(&argvars[0]);
16354 if (serverid == NULL)
16355 {
16356 rettv->vval.v_number = -1;
16357 return; /* type error; errmsg already given */
16358 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016359# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016360 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016361 if (n == 0)
16362 rettv->vval.v_number = -1;
16363 else
16364 {
16365 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16366 rettv->vval.v_number = (s != NULL);
16367 }
16368# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369 if (check_connection() == FAIL)
16370 return;
16371
16372 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016373 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016374# endif
16375
16376 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016378 char_u *retvar;
16379
Bram Moolenaar33570922005-01-25 22:26:29 +000016380 v.di_tv.v_type = VAR_STRING;
16381 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016382 retvar = get_tv_string_chk(&argvars[1]);
16383 if (retvar != NULL)
16384 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016385 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386 }
16387#else
16388 rettv->vval.v_number = -1;
16389#endif
16390}
16391
Bram Moolenaar0d660222005-01-07 21:51:51 +000016392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016393f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016394{
16395 char_u *r = NULL;
16396
16397#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016398 char_u *serverid = get_tv_string_chk(&argvars[0]);
16399
16400 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016401 {
16402# ifdef WIN32
16403 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016404 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016405
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016406 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016407 if (n != 0)
16408 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16409 if (r == NULL)
16410# else
16411 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016412 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016413# endif
16414 EMSG(_("E277: Unable to read a server reply"));
16415 }
16416#endif
16417 rettv->v_type = VAR_STRING;
16418 rettv->vval.v_string = r;
16419}
16420
16421/*
16422 * "remote_send()" function
16423 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016425f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016426{
16427 rettv->v_type = VAR_STRING;
16428 rettv->vval.v_string = NULL;
16429#ifdef FEAT_CLIENTSERVER
16430 remote_common(argvars, rettv, FALSE);
16431#endif
16432}
16433
16434/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016435 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016436 */
16437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016438f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016439{
Bram Moolenaar33570922005-01-25 22:26:29 +000016440 list_T *l;
16441 listitem_T *item, *item2;
16442 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016443 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016444 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016445 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016446 dict_T *d;
16447 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016448 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016449
Bram Moolenaar8c711452005-01-14 21:53:12 +000016450 if (argvars[0].v_type == VAR_DICT)
16451 {
16452 if (argvars[2].v_type != VAR_UNKNOWN)
16453 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016454 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016455 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016457 key = get_tv_string_chk(&argvars[1]);
16458 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016460 di = dict_find(d, key, -1);
16461 if (di == NULL)
16462 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016463 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16464 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016465 {
16466 *rettv = di->di_tv;
16467 init_tv(&di->di_tv);
16468 dictitem_remove(d, di);
16469 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016471 }
16472 }
16473 else if (argvars[0].v_type != VAR_LIST)
16474 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016475 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016476 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016478 int error = FALSE;
16479
16480 idx = get_tv_number_chk(&argvars[1], &error);
16481 if (error)
16482 ; /* type error: do nothing, errmsg already given */
16483 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016484 EMSGN(_(e_listidx), idx);
16485 else
16486 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016487 if (argvars[2].v_type == VAR_UNKNOWN)
16488 {
16489 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016490 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016491 *rettv = item->li_tv;
16492 vim_free(item);
16493 }
16494 else
16495 {
16496 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016497 end = get_tv_number_chk(&argvars[2], &error);
16498 if (error)
16499 ; /* type error: do nothing */
16500 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016501 EMSGN(_(e_listidx), end);
16502 else
16503 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016504 int cnt = 0;
16505
16506 for (li = item; li != NULL; li = li->li_next)
16507 {
16508 ++cnt;
16509 if (li == item2)
16510 break;
16511 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016512 if (li == NULL) /* didn't find "item2" after "item" */
16513 EMSG(_(e_invrange));
16514 else
16515 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016516 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016517 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016518 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016519 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016520 l->lv_first = item;
16521 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016522 item->li_prev = NULL;
16523 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016524 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016525 }
16526 }
16527 }
16528 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016529 }
16530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531}
16532
16533/*
16534 * "rename({from}, {to})" function
16535 */
16536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016537f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538{
16539 char_u buf[NUMBUFLEN];
16540
16541 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016542 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016544 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16545 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546}
16547
16548/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016549 * "repeat()" function
16550 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016552f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016553{
16554 char_u *p;
16555 int n;
16556 int slen;
16557 int len;
16558 char_u *r;
16559 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016560
16561 n = get_tv_number(&argvars[1]);
16562 if (argvars[0].v_type == VAR_LIST)
16563 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016564 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016565 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016566 if (list_extend(rettv->vval.v_list,
16567 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016568 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016569 }
16570 else
16571 {
16572 p = get_tv_string(&argvars[0]);
16573 rettv->v_type = VAR_STRING;
16574 rettv->vval.v_string = NULL;
16575
16576 slen = (int)STRLEN(p);
16577 len = slen * n;
16578 if (len <= 0)
16579 return;
16580
16581 r = alloc(len + 1);
16582 if (r != NULL)
16583 {
16584 for (i = 0; i < n; i++)
16585 mch_memmove(r + i * slen, p, (size_t)slen);
16586 r[len] = NUL;
16587 }
16588
16589 rettv->vval.v_string = r;
16590 }
16591}
16592
16593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594 * "resolve()" function
16595 */
16596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016597f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598{
16599 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016600#ifdef HAVE_READLINK
16601 char_u *buf = NULL;
16602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016604 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605#ifdef FEAT_SHORTCUT
16606 {
16607 char_u *v = NULL;
16608
16609 v = mch_resolve_shortcut(p);
16610 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016611 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016613 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 }
16615#else
16616# ifdef HAVE_READLINK
16617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 char_u *cpy;
16619 int len;
16620 char_u *remain = NULL;
16621 char_u *q;
16622 int is_relative_to_current = FALSE;
16623 int has_trailing_pathsep = FALSE;
16624 int limit = 100;
16625
16626 p = vim_strsave(p);
16627
16628 if (p[0] == '.' && (vim_ispathsep(p[1])
16629 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16630 is_relative_to_current = TRUE;
16631
16632 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016633 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016636 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638
16639 q = getnextcomp(p);
16640 if (*q != NUL)
16641 {
16642 /* Separate the first path component in "p", and keep the
16643 * remainder (beginning with the path separator). */
16644 remain = vim_strsave(q - 1);
16645 q[-1] = NUL;
16646 }
16647
Bram Moolenaard9462e32011-04-11 21:35:11 +020016648 buf = alloc(MAXPATHL + 1);
16649 if (buf == NULL)
16650 goto fail;
16651
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 for (;;)
16653 {
16654 for (;;)
16655 {
16656 len = readlink((char *)p, (char *)buf, MAXPATHL);
16657 if (len <= 0)
16658 break;
16659 buf[len] = NUL;
16660
16661 if (limit-- == 0)
16662 {
16663 vim_free(p);
16664 vim_free(remain);
16665 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016666 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 goto fail;
16668 }
16669
16670 /* Ensure that the result will have a trailing path separator
16671 * if the argument has one. */
16672 if (remain == NULL && has_trailing_pathsep)
16673 add_pathsep(buf);
16674
16675 /* Separate the first path component in the link value and
16676 * concatenate the remainders. */
16677 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16678 if (*q != NUL)
16679 {
16680 if (remain == NULL)
16681 remain = vim_strsave(q - 1);
16682 else
16683 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016684 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 if (cpy != NULL)
16686 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 vim_free(remain);
16688 remain = cpy;
16689 }
16690 }
16691 q[-1] = NUL;
16692 }
16693
16694 q = gettail(p);
16695 if (q > p && *q == NUL)
16696 {
16697 /* Ignore trailing path separator. */
16698 q[-1] = NUL;
16699 q = gettail(p);
16700 }
16701 if (q > p && !mch_isFullName(buf))
16702 {
16703 /* symlink is relative to directory of argument */
16704 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16705 if (cpy != NULL)
16706 {
16707 STRCPY(cpy, p);
16708 STRCPY(gettail(cpy), buf);
16709 vim_free(p);
16710 p = cpy;
16711 }
16712 }
16713 else
16714 {
16715 vim_free(p);
16716 p = vim_strsave(buf);
16717 }
16718 }
16719
16720 if (remain == NULL)
16721 break;
16722
16723 /* Append the first path component of "remain" to "p". */
16724 q = getnextcomp(remain + 1);
16725 len = q - remain - (*q != NUL);
16726 cpy = vim_strnsave(p, STRLEN(p) + len);
16727 if (cpy != NULL)
16728 {
16729 STRNCAT(cpy, remain, len);
16730 vim_free(p);
16731 p = cpy;
16732 }
16733 /* Shorten "remain". */
16734 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016735 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 else
16737 {
16738 vim_free(remain);
16739 remain = NULL;
16740 }
16741 }
16742
16743 /* If the result is a relative path name, make it explicitly relative to
16744 * the current directory if and only if the argument had this form. */
16745 if (!vim_ispathsep(*p))
16746 {
16747 if (is_relative_to_current
16748 && *p != NUL
16749 && !(p[0] == '.'
16750 && (p[1] == NUL
16751 || vim_ispathsep(p[1])
16752 || (p[1] == '.'
16753 && (p[2] == NUL
16754 || vim_ispathsep(p[2]))))))
16755 {
16756 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016757 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 if (cpy != NULL)
16759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 vim_free(p);
16761 p = cpy;
16762 }
16763 }
16764 else if (!is_relative_to_current)
16765 {
16766 /* Strip leading "./". */
16767 q = p;
16768 while (q[0] == '.' && vim_ispathsep(q[1]))
16769 q += 2;
16770 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016771 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 }
16773 }
16774
16775 /* Ensure that the result will have no trailing path separator
16776 * if the argument had none. But keep "/" or "//". */
16777 if (!has_trailing_pathsep)
16778 {
16779 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016780 if (after_pathsep(p, q))
16781 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782 }
16783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016784 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 }
16786# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016787 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788# endif
16789#endif
16790
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016791 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792
16793#ifdef HAVE_READLINK
16794fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016795 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016797 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798}
16799
16800/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016801 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 */
16803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016804f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805{
Bram Moolenaar33570922005-01-25 22:26:29 +000016806 list_T *l;
16807 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808
Bram Moolenaar0d660222005-01-07 21:51:51 +000016809 if (argvars[0].v_type != VAR_LIST)
16810 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016811 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016812 && !tv_check_lock(l->lv_lock,
16813 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016814 {
16815 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016816 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016817 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016818 while (li != NULL)
16819 {
16820 ni = li->li_prev;
16821 list_append(l, li);
16822 li = ni;
16823 }
16824 rettv->vval.v_list = l;
16825 rettv->v_type = VAR_LIST;
16826 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016827 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829}
16830
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016831#define SP_NOMOVE 0x01 /* don't move cursor */
16832#define SP_REPEAT 0x02 /* repeat to find outer pair */
16833#define SP_RETCOUNT 0x04 /* return matchcount */
16834#define SP_SETPCMARK 0x08 /* set previous context mark */
16835#define SP_START 0x10 /* accept match at start position */
16836#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16837#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016838#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016839
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016840static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016841
16842/*
16843 * Get flags for a search function.
16844 * Possibly sets "p_ws".
16845 * Returns BACKWARD, FORWARD or zero (for an error).
16846 */
16847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016848get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016849{
16850 int dir = FORWARD;
16851 char_u *flags;
16852 char_u nbuf[NUMBUFLEN];
16853 int mask;
16854
16855 if (varp->v_type != VAR_UNKNOWN)
16856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016857 flags = get_tv_string_buf_chk(varp, nbuf);
16858 if (flags == NULL)
16859 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016860 while (*flags != NUL)
16861 {
16862 switch (*flags)
16863 {
16864 case 'b': dir = BACKWARD; break;
16865 case 'w': p_ws = TRUE; break;
16866 case 'W': p_ws = FALSE; break;
16867 default: mask = 0;
16868 if (flagsp != NULL)
16869 switch (*flags)
16870 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016871 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016872 case 'e': mask = SP_END; break;
16873 case 'm': mask = SP_RETCOUNT; break;
16874 case 'n': mask = SP_NOMOVE; break;
16875 case 'p': mask = SP_SUBPAT; break;
16876 case 'r': mask = SP_REPEAT; break;
16877 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016878 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016879 }
16880 if (mask == 0)
16881 {
16882 EMSG2(_(e_invarg2), flags);
16883 dir = 0;
16884 }
16885 else
16886 *flagsp |= mask;
16887 }
16888 if (dir == 0)
16889 break;
16890 ++flags;
16891 }
16892 }
16893 return dir;
16894}
16895
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016897 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016899 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016900search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016902 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 char_u *pat;
16904 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016905 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906 int save_p_ws = p_ws;
16907 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016908 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016909 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016910 proftime_T tm;
16911#ifdef FEAT_RELTIME
16912 long time_limit = 0;
16913#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016914 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016915 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016917 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016918 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016919 if (dir == 0)
16920 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016921 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016922 if (flags & SP_START)
16923 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016924 if (flags & SP_END)
16925 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016926 if (flags & SP_COLUMN)
16927 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016928
Bram Moolenaar76929292008-01-06 19:07:36 +000016929 /* Optional arguments: line number to stop searching and timeout. */
16930 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016931 {
16932 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16933 if (lnum_stop < 0)
16934 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016935#ifdef FEAT_RELTIME
16936 if (argvars[3].v_type != VAR_UNKNOWN)
16937 {
16938 time_limit = get_tv_number_chk(&argvars[3], NULL);
16939 if (time_limit < 0)
16940 goto theend;
16941 }
16942#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016943 }
16944
Bram Moolenaar76929292008-01-06 19:07:36 +000016945#ifdef FEAT_RELTIME
16946 /* Set the time limit, if there is one. */
16947 profile_setlimit(time_limit, &tm);
16948#endif
16949
Bram Moolenaar231334e2005-07-25 20:46:57 +000016950 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016951 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016952 * Check to make sure only those flags are set.
16953 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16954 * flags cannot be set. Check for that condition also.
16955 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016956 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016957 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016959 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016960 goto theend;
16961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016963 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016964 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016965 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016966 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016968 if (flags & SP_SUBPAT)
16969 retval = subpatnum;
16970 else
16971 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016972 if (flags & SP_SETPCMARK)
16973 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016975 if (match_pos != NULL)
16976 {
16977 /* Store the match cursor position */
16978 match_pos->lnum = pos.lnum;
16979 match_pos->col = pos.col + 1;
16980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 /* "/$" will put the cursor after the end of the line, may need to
16982 * correct that here */
16983 check_cursor();
16984 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016985
16986 /* If 'n' flag is used: restore cursor position. */
16987 if (flags & SP_NOMOVE)
16988 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016989 else
16990 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016991theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016993
16994 return retval;
16995}
16996
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016997#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016998
16999/*
17000 * round() is not in C90, use ceil() or floor() instead.
17001 */
17002 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017003vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017004{
17005 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17006}
17007
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017008/*
17009 * "round({float})" function
17010 */
17011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017012f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017013{
17014 float_T f;
17015
17016 rettv->v_type = VAR_FLOAT;
17017 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017018 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017019 else
17020 rettv->vval.v_float = 0.0;
17021}
17022#endif
17023
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017024/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017025 * "screenattr()" function
17026 */
17027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017028f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017029{
17030 int row;
17031 int col;
17032 int c;
17033
17034 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17035 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17036 if (row < 0 || row >= screen_Rows
17037 || col < 0 || col >= screen_Columns)
17038 c = -1;
17039 else
17040 c = ScreenAttrs[LineOffset[row] + col];
17041 rettv->vval.v_number = c;
17042}
17043
17044/*
17045 * "screenchar()" function
17046 */
17047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017048f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017049{
17050 int row;
17051 int col;
17052 int off;
17053 int c;
17054
17055 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17056 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17057 if (row < 0 || row >= screen_Rows
17058 || col < 0 || col >= screen_Columns)
17059 c = -1;
17060 else
17061 {
17062 off = LineOffset[row] + col;
17063#ifdef FEAT_MBYTE
17064 if (enc_utf8 && ScreenLinesUC[off] != 0)
17065 c = ScreenLinesUC[off];
17066 else
17067#endif
17068 c = ScreenLines[off];
17069 }
17070 rettv->vval.v_number = c;
17071}
17072
17073/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017074 * "screencol()" function
17075 *
17076 * First column is 1 to be consistent with virtcol().
17077 */
17078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017079f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017080{
17081 rettv->vval.v_number = screen_screencol() + 1;
17082}
17083
17084/*
17085 * "screenrow()" function
17086 */
17087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017088f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017089{
17090 rettv->vval.v_number = screen_screenrow() + 1;
17091}
17092
17093/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017094 * "search()" function
17095 */
17096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017097f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017098{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017099 int flags = 0;
17100
17101 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102}
17103
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017105 * "searchdecl()" function
17106 */
17107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017108f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017109{
17110 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017111 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017112 int error = FALSE;
17113 char_u *name;
17114
17115 rettv->vval.v_number = 1; /* default: FAIL */
17116
17117 name = get_tv_string_chk(&argvars[0]);
17118 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017119 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017120 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017121 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17122 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17123 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017124 if (!error && name != NULL)
17125 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017126 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017127}
17128
17129/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017130 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017132 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017133searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134{
17135 char_u *spat, *mpat, *epat;
17136 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138 int dir;
17139 int flags = 0;
17140 char_u nbuf1[NUMBUFLEN];
17141 char_u nbuf2[NUMBUFLEN];
17142 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017143 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017144 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017145 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017148 spat = get_tv_string_chk(&argvars[0]);
17149 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17150 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17151 if (spat == NULL || mpat == NULL || epat == NULL)
17152 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 /* Handle the optional fourth argument: flags */
17155 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017156 if (dir == 0)
17157 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017158
17159 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017160 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17161 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017162 if ((flags & (SP_END | SP_SUBPAT)) != 0
17163 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017164 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017165 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017166 goto theend;
17167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017169 /* Using 'r' implies 'W', otherwise it doesn't work. */
17170 if (flags & SP_REPEAT)
17171 p_ws = FALSE;
17172
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017173 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017174 if (argvars[3].v_type == VAR_UNKNOWN
17175 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 skip = (char_u *)"";
17177 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017179 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017180 if (argvars[5].v_type != VAR_UNKNOWN)
17181 {
17182 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17183 if (lnum_stop < 0)
17184 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017185#ifdef FEAT_RELTIME
17186 if (argvars[6].v_type != VAR_UNKNOWN)
17187 {
17188 time_limit = get_tv_number_chk(&argvars[6], NULL);
17189 if (time_limit < 0)
17190 goto theend;
17191 }
17192#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017193 }
17194 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017195 if (skip == NULL)
17196 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017198 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017199 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017200
17201theend:
17202 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017203
17204 return retval;
17205}
17206
17207/*
17208 * "searchpair()" function
17209 */
17210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017211f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017212{
17213 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17214}
17215
17216/*
17217 * "searchpairpos()" function
17218 */
17219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017220f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017221{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017222 pos_T match_pos;
17223 int lnum = 0;
17224 int col = 0;
17225
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017226 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017227 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017228
17229 if (searchpair_cmn(argvars, &match_pos) > 0)
17230 {
17231 lnum = match_pos.lnum;
17232 col = match_pos.col;
17233 }
17234
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017235 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17236 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017237}
17238
17239/*
17240 * Search for a start/middle/end thing.
17241 * Used by searchpair(), see its documentation for the details.
17242 * Returns 0 or -1 for no match,
17243 */
17244 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017245do_searchpair(
17246 char_u *spat, /* start pattern */
17247 char_u *mpat, /* middle pattern */
17248 char_u *epat, /* end pattern */
17249 int dir, /* BACKWARD or FORWARD */
17250 char_u *skip, /* skip expression */
17251 int flags, /* SP_SETPCMARK and other SP_ values */
17252 pos_T *match_pos,
17253 linenr_T lnum_stop, /* stop at this line if not zero */
17254 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017255{
17256 char_u *save_cpo;
17257 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17258 long retval = 0;
17259 pos_T pos;
17260 pos_T firstpos;
17261 pos_T foundpos;
17262 pos_T save_cursor;
17263 pos_T save_pos;
17264 int n;
17265 int r;
17266 int nest = 1;
17267 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017268 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017269 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017270
17271 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17272 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017273 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017274
Bram Moolenaar76929292008-01-06 19:07:36 +000017275#ifdef FEAT_RELTIME
17276 /* Set the time limit, if there is one. */
17277 profile_setlimit(time_limit, &tm);
17278#endif
17279
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017280 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17281 * start/middle/end (pat3, for the top pair). */
17282 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17283 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17284 if (pat2 == NULL || pat3 == NULL)
17285 goto theend;
17286 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17287 if (*mpat == NUL)
17288 STRCPY(pat3, pat2);
17289 else
17290 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17291 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017292 if (flags & SP_START)
17293 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017294
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 save_cursor = curwin->w_cursor;
17296 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017297 clearpos(&firstpos);
17298 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299 pat = pat3;
17300 for (;;)
17301 {
17302 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017303 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17305 /* didn't find it or found the first match again: FAIL */
17306 break;
17307
17308 if (firstpos.lnum == 0)
17309 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017310 if (equalpos(pos, foundpos))
17311 {
17312 /* Found the same position again. Can happen with a pattern that
17313 * has "\zs" at the end and searching backwards. Advance one
17314 * character and try again. */
17315 if (dir == BACKWARD)
17316 decl(&pos);
17317 else
17318 incl(&pos);
17319 }
17320 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017322 /* clear the start flag to avoid getting stuck here */
17323 options &= ~SEARCH_START;
17324
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325 /* If the skip pattern matches, ignore this match. */
17326 if (*skip != NUL)
17327 {
17328 save_pos = curwin->w_cursor;
17329 curwin->w_cursor = pos;
17330 r = eval_to_bool(skip, &err, NULL, FALSE);
17331 curwin->w_cursor = save_pos;
17332 if (err)
17333 {
17334 /* Evaluating {skip} caused an error, break here. */
17335 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017336 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 break;
17338 }
17339 if (r)
17340 continue;
17341 }
17342
17343 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17344 {
17345 /* Found end when searching backwards or start when searching
17346 * forward: nested pair. */
17347 ++nest;
17348 pat = pat2; /* nested, don't search for middle */
17349 }
17350 else
17351 {
17352 /* Found end when searching forward or start when searching
17353 * backward: end of (nested) pair; or found middle in outer pair. */
17354 if (--nest == 1)
17355 pat = pat3; /* outer level, search for middle */
17356 }
17357
17358 if (nest == 0)
17359 {
17360 /* Found the match: return matchcount or line number. */
17361 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017362 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017364 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017365 if (flags & SP_SETPCMARK)
17366 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 curwin->w_cursor = pos;
17368 if (!(flags & SP_REPEAT))
17369 break;
17370 nest = 1; /* search for next unmatched */
17371 }
17372 }
17373
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017374 if (match_pos != NULL)
17375 {
17376 /* Store the match cursor position */
17377 match_pos->lnum = curwin->w_cursor.lnum;
17378 match_pos->col = curwin->w_cursor.col + 1;
17379 }
17380
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017382 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 curwin->w_cursor = save_cursor;
17384
17385theend:
17386 vim_free(pat2);
17387 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017388 if (p_cpo == empty_option)
17389 p_cpo = save_cpo;
17390 else
17391 /* Darn, evaluating the {skip} expression changed the value. */
17392 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017393
17394 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395}
17396
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017397/*
17398 * "searchpos()" function
17399 */
17400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017401f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017402{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017403 pos_T match_pos;
17404 int lnum = 0;
17405 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017406 int n;
17407 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017408
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017409 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017410 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017411
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017412 n = search_cmn(argvars, &match_pos, &flags);
17413 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017414 {
17415 lnum = match_pos.lnum;
17416 col = match_pos.col;
17417 }
17418
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017419 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17420 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017421 if (flags & SP_SUBPAT)
17422 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017423}
17424
Bram Moolenaar0d660222005-01-07 21:51:51 +000017425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017426f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017428#ifdef FEAT_CLIENTSERVER
17429 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017430 char_u *server = get_tv_string_chk(&argvars[0]);
17431 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432
Bram Moolenaar0d660222005-01-07 21:51:51 +000017433 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017434 if (server == NULL || reply == NULL)
17435 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017436 if (check_restricted() || check_secure())
17437 return;
17438# ifdef FEAT_X11
17439 if (check_connection() == FAIL)
17440 return;
17441# endif
17442
17443 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017445 EMSG(_("E258: Unable to send to client"));
17446 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017448 rettv->vval.v_number = 0;
17449#else
17450 rettv->vval.v_number = -1;
17451#endif
17452}
17453
Bram Moolenaar0d660222005-01-07 21:51:51 +000017454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017455f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017456{
17457 char_u *r = NULL;
17458
17459#ifdef FEAT_CLIENTSERVER
17460# ifdef WIN32
17461 r = serverGetVimNames();
17462# else
17463 make_connection();
17464 if (X_DISPLAY != NULL)
17465 r = serverGetVimNames(X_DISPLAY);
17466# endif
17467#endif
17468 rettv->v_type = VAR_STRING;
17469 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470}
17471
17472/*
17473 * "setbufvar()" function
17474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017476f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477{
17478 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017481 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 char_u nbuf[NUMBUFLEN];
17483
17484 if (check_restricted() || check_secure())
17485 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017486 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17487 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017488 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 varp = &argvars[2];
17490
17491 if (buf != NULL && varname != NULL && varp != NULL)
17492 {
17493 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495
17496 if (*varname == '&')
17497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017498 long numval;
17499 char_u *strval;
17500 int error = FALSE;
17501
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017503 numval = get_tv_number_chk(varp, &error);
17504 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017505 if (!error && strval != NULL)
17506 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 }
17508 else
17509 {
17510 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17511 if (bufvarname != NULL)
17512 {
17513 STRCPY(bufvarname, "b:");
17514 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017515 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 vim_free(bufvarname);
17517 }
17518 }
17519
17520 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523}
17524
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017526f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017527{
17528 dict_T *d;
17529 dictitem_T *di;
17530 char_u *csearch;
17531
17532 if (argvars[0].v_type != VAR_DICT)
17533 {
17534 EMSG(_(e_dictreq));
17535 return;
17536 }
17537
17538 if ((d = argvars[0].vval.v_dict) != NULL)
17539 {
17540 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17541 if (csearch != NULL)
17542 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017543#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017544 if (enc_utf8)
17545 {
17546 int pcc[MAX_MCO];
17547 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017548
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017549 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17550 }
17551 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017552#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017553 set_last_csearch(PTR2CHAR(csearch),
17554 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017555 }
17556
17557 di = dict_find(d, (char_u *)"forward", -1);
17558 if (di != NULL)
17559 set_csearch_direction(get_tv_number(&di->di_tv)
17560 ? FORWARD : BACKWARD);
17561
17562 di = dict_find(d, (char_u *)"until", -1);
17563 if (di != NULL)
17564 set_csearch_until(!!get_tv_number(&di->di_tv));
17565 }
17566}
17567
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568/*
17569 * "setcmdpos()" function
17570 */
17571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017572f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017574 int pos = (int)get_tv_number(&argvars[0]) - 1;
17575
17576 if (pos >= 0)
17577 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578}
17579
17580/*
17581 * "setline()" function
17582 */
17583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017584f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585{
17586 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017587 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017588 list_T *l = NULL;
17589 listitem_T *li = NULL;
17590 long added = 0;
17591 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017593 lnum = get_tv_lnum(&argvars[0]);
17594 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017596 l = argvars[1].vval.v_list;
17597 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017599 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017600 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017601
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017602 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017603 for (;;)
17604 {
17605 if (l != NULL)
17606 {
17607 /* list argument, get next string */
17608 if (li == NULL)
17609 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017610 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017611 li = li->li_next;
17612 }
17613
17614 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017615 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017616 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017617
17618 /* When coming here from Insert mode, sync undo, so that this can be
17619 * undone separately from what was previously inserted. */
17620 if (u_sync_once == 2)
17621 {
17622 u_sync_once = 1; /* notify that u_sync() was called */
17623 u_sync(TRUE);
17624 }
17625
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017626 if (lnum <= curbuf->b_ml.ml_line_count)
17627 {
17628 /* existing line, replace it */
17629 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17630 {
17631 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017632 if (lnum == curwin->w_cursor.lnum)
17633 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017634 rettv->vval.v_number = 0; /* OK */
17635 }
17636 }
17637 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17638 {
17639 /* lnum is one past the last line, append the line */
17640 ++added;
17641 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17642 rettv->vval.v_number = 0; /* OK */
17643 }
17644
17645 if (l == NULL) /* only one string argument */
17646 break;
17647 ++lnum;
17648 }
17649
17650 if (added > 0)
17651 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652}
17653
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017654static 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 +000017655
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017657 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017658 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017660set_qf_ll_list(
17661 win_T *wp UNUSED,
17662 typval_T *list_arg UNUSED,
17663 typval_T *action_arg UNUSED,
17664 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017665{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017666#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017667 char_u *act;
17668 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017669#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017670
Bram Moolenaar2641f772005-03-25 21:58:17 +000017671 rettv->vval.v_number = -1;
17672
17673#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017674 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017675 EMSG(_(e_listreq));
17676 else
17677 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017678 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017679
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017680 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017681 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017682 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017683 if (act == NULL)
17684 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017685 if (*act == 'a' || *act == 'r')
17686 action = *act;
17687 }
17688
Bram Moolenaar81484f42012-12-05 15:16:47 +010017689 if (l != NULL && set_errorlist(wp, l, action,
17690 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017691 rettv->vval.v_number = 0;
17692 }
17693#endif
17694}
17695
17696/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017697 * "setloclist()" function
17698 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017700f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017701{
17702 win_T *win;
17703
17704 rettv->vval.v_number = -1;
17705
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017706 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017707 if (win != NULL)
17708 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17709}
17710
17711/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017712 * "setmatches()" function
17713 */
17714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017715f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017716{
17717#ifdef FEAT_SEARCH_EXTRA
17718 list_T *l;
17719 listitem_T *li;
17720 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017721 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017722
17723 rettv->vval.v_number = -1;
17724 if (argvars[0].v_type != VAR_LIST)
17725 {
17726 EMSG(_(e_listreq));
17727 return;
17728 }
17729 if ((l = argvars[0].vval.v_list) != NULL)
17730 {
17731
17732 /* To some extent make sure that we are dealing with a list from
17733 * "getmatches()". */
17734 li = l->lv_first;
17735 while (li != NULL)
17736 {
17737 if (li->li_tv.v_type != VAR_DICT
17738 || (d = li->li_tv.vval.v_dict) == NULL)
17739 {
17740 EMSG(_(e_invarg));
17741 return;
17742 }
17743 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017744 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17745 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017746 && dict_find(d, (char_u *)"priority", -1) != NULL
17747 && dict_find(d, (char_u *)"id", -1) != NULL))
17748 {
17749 EMSG(_(e_invarg));
17750 return;
17751 }
17752 li = li->li_next;
17753 }
17754
17755 clear_matches(curwin);
17756 li = l->lv_first;
17757 while (li != NULL)
17758 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017759 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017760 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017761 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017762 char_u *group;
17763 int priority;
17764 int id;
17765 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017766
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017767 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017768 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17769 {
17770 if (s == NULL)
17771 {
17772 s = list_alloc();
17773 if (s == NULL)
17774 return;
17775 }
17776
17777 /* match from matchaddpos() */
17778 for (i = 1; i < 9; i++)
17779 {
17780 sprintf((char *)buf, (char *)"pos%d", i);
17781 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17782 {
17783 if (di->di_tv.v_type != VAR_LIST)
17784 return;
17785
17786 list_append_tv(s, &di->di_tv);
17787 s->lv_refcount++;
17788 }
17789 else
17790 break;
17791 }
17792 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017793
17794 group = get_dict_string(d, (char_u *)"group", FALSE);
17795 priority = (int)get_dict_number(d, (char_u *)"priority");
17796 id = (int)get_dict_number(d, (char_u *)"id");
17797 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17798 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17799 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017800 if (i == 0)
17801 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017802 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017803 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017804 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017805 }
17806 else
17807 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017808 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017809 list_unref(s);
17810 s = NULL;
17811 }
17812
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017813 li = li->li_next;
17814 }
17815 rettv->vval.v_number = 0;
17816 }
17817#endif
17818}
17819
17820/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017821 * "setpos()" function
17822 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017824f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017825{
17826 pos_T pos;
17827 int fnum;
17828 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017829 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017830
Bram Moolenaar08250432008-02-13 11:42:46 +000017831 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017832 name = get_tv_string_chk(argvars);
17833 if (name != NULL)
17834 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017835 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017836 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017837 if (--pos.col < 0)
17838 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017839 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017840 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017841 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017842 if (fnum == curbuf->b_fnum)
17843 {
17844 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017845 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017846 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017847 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017848 curwin->w_set_curswant = FALSE;
17849 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017850 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017851 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017852 }
17853 else
17854 EMSG(_(e_invarg));
17855 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017856 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17857 {
17858 /* set mark */
17859 if (setmark_pos(name[1], &pos, fnum) == OK)
17860 rettv->vval.v_number = 0;
17861 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017862 else
17863 EMSG(_(e_invarg));
17864 }
17865 }
17866}
17867
17868/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017869 * "setqflist()" function
17870 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017872f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017873{
17874 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17875}
17876
17877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 * "setreg()" function
17879 */
17880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017881f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882{
17883 int regname;
17884 char_u *strregname;
17885 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017886 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887 int append;
17888 char_u yank_type;
17889 long block_len;
17890
17891 block_len = -1;
17892 yank_type = MAUTO;
17893 append = FALSE;
17894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017895 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017896 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017898 if (strregname == NULL)
17899 return; /* type error; errmsg already given */
17900 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 if (regname == 0 || regname == '@')
17902 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017904 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017906 stropt = get_tv_string_chk(&argvars[2]);
17907 if (stropt == NULL)
17908 return; /* type error */
17909 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 switch (*stropt)
17911 {
17912 case 'a': case 'A': /* append */
17913 append = TRUE;
17914 break;
17915 case 'v': case 'c': /* character-wise selection */
17916 yank_type = MCHAR;
17917 break;
17918 case 'V': case 'l': /* line-wise selection */
17919 yank_type = MLINE;
17920 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 case 'b': case Ctrl_V: /* block-wise selection */
17922 yank_type = MBLOCK;
17923 if (VIM_ISDIGIT(stropt[1]))
17924 {
17925 ++stropt;
17926 block_len = getdigits(&stropt) - 1;
17927 --stropt;
17928 }
17929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 }
17931 }
17932
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017933 if (argvars[1].v_type == VAR_LIST)
17934 {
17935 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017936 char_u **allocval;
17937 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017938 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017939 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017940 int len = argvars[1].vval.v_list->lv_len;
17941 listitem_T *li;
17942
Bram Moolenaar7d647822014-04-05 21:28:56 +020017943 /* First half: use for pointers to result lines; second half: use for
17944 * pointers to allocated copies. */
17945 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017946 if (lstval == NULL)
17947 return;
17948 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017949 allocval = lstval + len + 2;
17950 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017951
17952 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17953 li = li->li_next)
17954 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017955 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017956 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017957 goto free_lstval;
17958 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017959 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017960 /* Need to make a copy, next get_tv_string_buf_chk() will
17961 * overwrite the string. */
17962 strval = vim_strsave(buf);
17963 if (strval == NULL)
17964 goto free_lstval;
17965 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017966 }
17967 *curval++ = strval;
17968 }
17969 *curval++ = NULL;
17970
17971 write_reg_contents_lst(regname, lstval, -1,
17972 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017973free_lstval:
17974 while (curallocval > allocval)
17975 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017976 vim_free(lstval);
17977 }
17978 else
17979 {
17980 strval = get_tv_string_chk(&argvars[1]);
17981 if (strval == NULL)
17982 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017983 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017985 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017986 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987}
17988
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017989/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017990 * "settabvar()" function
17991 */
17992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017993f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017994{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017995#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017996 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017997 tabpage_T *tp;
17998#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017999 char_u *varname, *tabvarname;
18000 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018001
18002 rettv->vval.v_number = 0;
18003
18004 if (check_restricted() || check_secure())
18005 return;
18006
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018007#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018008 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018009#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018010 varname = get_tv_string_chk(&argvars[1]);
18011 varp = &argvars[2];
18012
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018013 if (varname != NULL && varp != NULL
18014#ifdef FEAT_WINDOWS
18015 && tp != NULL
18016#endif
18017 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018018 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018019#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018020 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018021 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018022#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018023
18024 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18025 if (tabvarname != NULL)
18026 {
18027 STRCPY(tabvarname, "t:");
18028 STRCPY(tabvarname + 2, varname);
18029 set_var(tabvarname, varp, TRUE);
18030 vim_free(tabvarname);
18031 }
18032
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018033#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018034 /* Restore current tabpage */
18035 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018036 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018037#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018038 }
18039}
18040
18041/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018042 * "settabwinvar()" function
18043 */
18044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018045f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018046{
18047 setwinvar(argvars, rettv, 1);
18048}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049
18050/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018051 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018054f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018056 setwinvar(argvars, rettv, 0);
18057}
18058
18059/*
18060 * "setwinvar()" and "settabwinvar()" functions
18061 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018062
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018064setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018065{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 win_T *win;
18067#ifdef FEAT_WINDOWS
18068 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018069 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018070 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071#endif
18072 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018073 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018075 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076
18077 if (check_restricted() || check_secure())
18078 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018079
18080#ifdef FEAT_WINDOWS
18081 if (off == 1)
18082 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18083 else
18084 tp = curtab;
18085#endif
18086 win = find_win_by_nr(&argvars[off], tp);
18087 varname = get_tv_string_chk(&argvars[off + 1]);
18088 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089
18090 if (win != NULL && varname != NULL && varp != NULL)
18091 {
18092#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018093 need_switch_win = !(tp == curtab && win == curwin);
18094 if (!need_switch_win
18095 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018098 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018100 long numval;
18101 char_u *strval;
18102 int error = FALSE;
18103
18104 ++varname;
18105 numval = get_tv_number_chk(varp, &error);
18106 strval = get_tv_string_buf_chk(varp, nbuf);
18107 if (!error && strval != NULL)
18108 set_option_value(varname, numval, strval, OPT_LOCAL);
18109 }
18110 else
18111 {
18112 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18113 if (winvarname != NULL)
18114 {
18115 STRCPY(winvarname, "w:");
18116 STRCPY(winvarname + 2, varname);
18117 set_var(winvarname, varp, TRUE);
18118 vim_free(winvarname);
18119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 }
18121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018123 if (need_switch_win)
18124 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125#endif
18126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127}
18128
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018129#ifdef FEAT_CRYPT
18130/*
18131 * "sha256({string})" function
18132 */
18133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018134f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018135{
18136 char_u *p;
18137
18138 p = get_tv_string(&argvars[0]);
18139 rettv->vval.v_string = vim_strsave(
18140 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18141 rettv->v_type = VAR_STRING;
18142}
18143#endif /* FEAT_CRYPT */
18144
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018146 * "shellescape({string})" function
18147 */
18148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018149f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018150{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018151 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018152 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018153 rettv->v_type = VAR_STRING;
18154}
18155
18156/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018157 * shiftwidth() function
18158 */
18159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018160f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018161{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018162 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018163}
18164
18165/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018166 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 */
18168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018169f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018171 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172
Bram Moolenaar0d660222005-01-07 21:51:51 +000018173 p = get_tv_string(&argvars[0]);
18174 rettv->vval.v_string = vim_strsave(p);
18175 simplify_filename(rettv->vval.v_string); /* simplify in place */
18176 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177}
18178
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018179#ifdef FEAT_FLOAT
18180/*
18181 * "sin()" function
18182 */
18183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018184f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018185{
18186 float_T f;
18187
18188 rettv->v_type = VAR_FLOAT;
18189 if (get_float_arg(argvars, &f) == OK)
18190 rettv->vval.v_float = sin(f);
18191 else
18192 rettv->vval.v_float = 0.0;
18193}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018194
18195/*
18196 * "sinh()" function
18197 */
18198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018199f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018200{
18201 float_T f;
18202
18203 rettv->v_type = VAR_FLOAT;
18204 if (get_float_arg(argvars, &f) == OK)
18205 rettv->vval.v_float = sinh(f);
18206 else
18207 rettv->vval.v_float = 0.0;
18208}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018209#endif
18210
Bram Moolenaar0d660222005-01-07 21:51:51 +000018211static int
18212#ifdef __BORLANDC__
18213 _RTLENTRYF
18214#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018215 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018216static int
18217#ifdef __BORLANDC__
18218 _RTLENTRYF
18219#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018220 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018221
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018222/* struct used in the array that's given to qsort() */
18223typedef struct
18224{
18225 listitem_T *item;
18226 int idx;
18227} sortItem_T;
18228
Bram Moolenaar0d660222005-01-07 21:51:51 +000018229static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018230static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018231static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018232#ifdef FEAT_FLOAT
18233static int item_compare_float;
18234#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018235static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018236static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018237static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018238static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018239static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018240#define ITEM_COMPARE_FAIL 999
18241
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018243 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245 static int
18246#ifdef __BORLANDC__
18247_RTLENTRYF
18248#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018249item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018251 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018252 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018253 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018254 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018255 int res;
18256 char_u numbuf1[NUMBUFLEN];
18257 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018259 si1 = (sortItem_T *)s1;
18260 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018261 tv1 = &si1->item->li_tv;
18262 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018263
18264 if (item_compare_numbers)
18265 {
18266 long v1 = get_tv_number(tv1);
18267 long v2 = get_tv_number(tv2);
18268
18269 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18270 }
18271
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018272#ifdef FEAT_FLOAT
18273 if (item_compare_float)
18274 {
18275 float_T v1 = get_tv_float(tv1);
18276 float_T v2 = get_tv_float(tv2);
18277
18278 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18279 }
18280#endif
18281
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018282 /* tv2string() puts quotes around a string and allocates memory. Don't do
18283 * that for string variables. Use a single quote when comparing with a
18284 * non-string to do what the docs promise. */
18285 if (tv1->v_type == VAR_STRING)
18286 {
18287 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18288 p1 = (char_u *)"'";
18289 else
18290 p1 = tv1->vval.v_string;
18291 }
18292 else
18293 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18294 if (tv2->v_type == VAR_STRING)
18295 {
18296 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18297 p2 = (char_u *)"'";
18298 else
18299 p2 = tv2->vval.v_string;
18300 }
18301 else
18302 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018303 if (p1 == NULL)
18304 p1 = (char_u *)"";
18305 if (p2 == NULL)
18306 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018307 if (!item_compare_numeric)
18308 {
18309 if (item_compare_ic)
18310 res = STRICMP(p1, p2);
18311 else
18312 res = STRCMP(p1, p2);
18313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018315 {
18316 double n1, n2;
18317 n1 = strtod((char *)p1, (char **)&p1);
18318 n2 = strtod((char *)p2, (char **)&p2);
18319 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18320 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018321
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018322 /* When the result would be zero, compare the item indexes. Makes the
18323 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018324 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018325 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018326
Bram Moolenaar0d660222005-01-07 21:51:51 +000018327 vim_free(tofree1);
18328 vim_free(tofree2);
18329 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330}
18331
18332 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018333#ifdef __BORLANDC__
18334_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018336item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018337{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018338 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018339 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018340 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018341 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018342 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018344 /* shortcut after failure in previous call; compare all items equal */
18345 if (item_compare_func_err)
18346 return 0;
18347
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018348 si1 = (sortItem_T *)s1;
18349 si2 = (sortItem_T *)s2;
18350
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018351 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018352 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018353 copy_tv(&si1->item->li_tv, &argv[0]);
18354 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018355
18356 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018357 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018358 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18359 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018360 clear_tv(&argv[0]);
18361 clear_tv(&argv[1]);
18362
18363 if (res == FAIL)
18364 res = ITEM_COMPARE_FAIL;
18365 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018366 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18367 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018368 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018369 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018370
18371 /* When the result would be zero, compare the pointers themselves. Makes
18372 * the sort stable. */
18373 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018374 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018375
Bram Moolenaar0d660222005-01-07 21:51:51 +000018376 return res;
18377}
18378
18379/*
18380 * "sort({list})" function
18381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018383do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384{
Bram Moolenaar33570922005-01-25 22:26:29 +000018385 list_T *l;
18386 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018387 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018388 long len;
18389 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390
Bram Moolenaar0d660222005-01-07 21:51:51 +000018391 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018392 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393 else
18394 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018395 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018396 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018397 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18398 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018399 return;
18400 rettv->vval.v_list = l;
18401 rettv->v_type = VAR_LIST;
18402 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403
Bram Moolenaar0d660222005-01-07 21:51:51 +000018404 len = list_len(l);
18405 if (len <= 1)
18406 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407
Bram Moolenaar0d660222005-01-07 21:51:51 +000018408 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018409 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018410 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018411#ifdef FEAT_FLOAT
18412 item_compare_float = FALSE;
18413#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018414 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018415 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018416 if (argvars[1].v_type != VAR_UNKNOWN)
18417 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018418 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018419 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018420 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018421 else
18422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018423 int error = FALSE;
18424
18425 i = get_tv_number_chk(&argvars[1], &error);
18426 if (error)
18427 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018428 if (i == 1)
18429 item_compare_ic = TRUE;
18430 else
18431 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018432 if (item_compare_func != NULL)
18433 {
18434 if (STRCMP(item_compare_func, "n") == 0)
18435 {
18436 item_compare_func = NULL;
18437 item_compare_numeric = TRUE;
18438 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018439 else if (STRCMP(item_compare_func, "N") == 0)
18440 {
18441 item_compare_func = NULL;
18442 item_compare_numbers = TRUE;
18443 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018444#ifdef FEAT_FLOAT
18445 else if (STRCMP(item_compare_func, "f") == 0)
18446 {
18447 item_compare_func = NULL;
18448 item_compare_float = TRUE;
18449 }
18450#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018451 else if (STRCMP(item_compare_func, "i") == 0)
18452 {
18453 item_compare_func = NULL;
18454 item_compare_ic = TRUE;
18455 }
18456 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018457 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018458
18459 if (argvars[2].v_type != VAR_UNKNOWN)
18460 {
18461 /* optional third argument: {dict} */
18462 if (argvars[2].v_type != VAR_DICT)
18463 {
18464 EMSG(_(e_dictreq));
18465 return;
18466 }
18467 item_compare_selfdict = argvars[2].vval.v_dict;
18468 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470
Bram Moolenaar0d660222005-01-07 21:51:51 +000018471 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018472 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018473 if (ptrs == NULL)
18474 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475
Bram Moolenaar327aa022014-03-25 18:24:23 +010018476 i = 0;
18477 if (sort)
18478 {
18479 /* sort(): ptrs will be the list to sort */
18480 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018481 {
18482 ptrs[i].item = li;
18483 ptrs[i].idx = i;
18484 ++i;
18485 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018486
18487 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018488 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018489 /* test the compare function */
18490 if (item_compare_func != NULL
18491 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018492 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018493 EMSG(_("E702: Sort compare function failed"));
18494 else
18495 {
18496 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018497 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018498 item_compare_func == NULL ? item_compare : item_compare2);
18499
18500 if (!item_compare_func_err)
18501 {
18502 /* Clear the List and append the items in sorted order. */
18503 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18504 l->lv_len = 0;
18505 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018506 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018507 }
18508 }
18509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018510 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018511 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018512 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018513
18514 /* f_uniq(): ptrs will be a stack of items to remove */
18515 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018516 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018517 item_compare_func_ptr = item_compare_func
18518 ? item_compare2 : item_compare;
18519
18520 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18521 li = li->li_next)
18522 {
18523 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18524 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018525 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018526 if (item_compare_func_err)
18527 {
18528 EMSG(_("E882: Uniq compare function failed"));
18529 break;
18530 }
18531 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018533 if (!item_compare_func_err)
18534 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018535 while (--i >= 0)
18536 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018537 li = ptrs[i].item->li_next;
18538 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018539 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018540 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018541 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018542 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018543 list_fix_watch(l, li);
18544 listitem_free(li);
18545 l->lv_len--;
18546 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018547 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018548 }
18549
18550 vim_free(ptrs);
18551 }
18552}
18553
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018554/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018555 * "sort({list})" function
18556 */
18557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018558f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018559{
18560 do_sort_uniq(argvars, rettv, TRUE);
18561}
18562
18563/*
18564 * "uniq({list})" function
18565 */
18566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018567f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018568{
18569 do_sort_uniq(argvars, rettv, FALSE);
18570}
18571
18572/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018573 * "soundfold({word})" function
18574 */
18575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018576f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018577{
18578 char_u *s;
18579
18580 rettv->v_type = VAR_STRING;
18581 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018582#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018583 rettv->vval.v_string = eval_soundfold(s);
18584#else
18585 rettv->vval.v_string = vim_strsave(s);
18586#endif
18587}
18588
18589/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018590 * "spellbadword()" function
18591 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018593f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018594{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018595 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018596 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018597 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018598
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018599 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018600 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018601
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018602#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018603 if (argvars[0].v_type == VAR_UNKNOWN)
18604 {
18605 /* Find the start and length of the badly spelled word. */
18606 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18607 if (len != 0)
18608 word = ml_get_cursor();
18609 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018610 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018611 {
18612 char_u *str = get_tv_string_chk(&argvars[0]);
18613 int capcol = -1;
18614
18615 if (str != NULL)
18616 {
18617 /* Check the argument for spelling. */
18618 while (*str != NUL)
18619 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018620 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018621 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018622 {
18623 word = str;
18624 break;
18625 }
18626 str += len;
18627 }
18628 }
18629 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018630#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018631
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018632 list_append_string(rettv->vval.v_list, word, len);
18633 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018634 attr == HLF_SPB ? "bad" :
18635 attr == HLF_SPR ? "rare" :
18636 attr == HLF_SPL ? "local" :
18637 attr == HLF_SPC ? "caps" :
18638 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018639}
18640
18641/*
18642 * "spellsuggest()" function
18643 */
18644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018645f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018646{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018647#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018648 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018649 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018650 int maxcount;
18651 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018652 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018653 listitem_T *li;
18654 int need_capital = FALSE;
18655#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018656
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018657 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018658 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018659
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018660#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018661 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018662 {
18663 str = get_tv_string(&argvars[0]);
18664 if (argvars[1].v_type != VAR_UNKNOWN)
18665 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018666 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018667 if (maxcount <= 0)
18668 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018669 if (argvars[2].v_type != VAR_UNKNOWN)
18670 {
18671 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18672 if (typeerr)
18673 return;
18674 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018675 }
18676 else
18677 maxcount = 25;
18678
Bram Moolenaar4770d092006-01-12 23:22:24 +000018679 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018680
18681 for (i = 0; i < ga.ga_len; ++i)
18682 {
18683 str = ((char_u **)ga.ga_data)[i];
18684
18685 li = listitem_alloc();
18686 if (li == NULL)
18687 vim_free(str);
18688 else
18689 {
18690 li->li_tv.v_type = VAR_STRING;
18691 li->li_tv.v_lock = 0;
18692 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018693 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018694 }
18695 }
18696 ga_clear(&ga);
18697 }
18698#endif
18699}
18700
Bram Moolenaar0d660222005-01-07 21:51:51 +000018701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018702f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018703{
18704 char_u *str;
18705 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018706 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018707 regmatch_T regmatch;
18708 char_u patbuf[NUMBUFLEN];
18709 char_u *save_cpo;
18710 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018711 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018712 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018713 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018714
18715 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18716 save_cpo = p_cpo;
18717 p_cpo = (char_u *)"";
18718
18719 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018720 if (argvars[1].v_type != VAR_UNKNOWN)
18721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018722 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18723 if (pat == NULL)
18724 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018725 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018726 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018727 }
18728 if (pat == NULL || *pat == NUL)
18729 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018730
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018731 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018733 if (typeerr)
18734 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735
Bram Moolenaar0d660222005-01-07 21:51:51 +000018736 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18737 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018739 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018740 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018741 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018742 if (*str == NUL)
18743 match = FALSE; /* empty item at the end */
18744 else
18745 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018746 if (match)
18747 end = regmatch.startp[0];
18748 else
18749 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018750 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18751 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018752 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018753 if (list_append_string(rettv->vval.v_list, str,
18754 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018755 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018756 }
18757 if (!match)
18758 break;
18759 /* Advance to just after the match. */
18760 if (regmatch.endp[0] > str)
18761 col = 0;
18762 else
18763 {
18764 /* Don't get stuck at the same match. */
18765#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018766 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018767#else
18768 col = 1;
18769#endif
18770 }
18771 str = regmatch.endp[0];
18772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773
Bram Moolenaar473de612013-06-08 18:19:48 +020018774 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776
Bram Moolenaar0d660222005-01-07 21:51:51 +000018777 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778}
18779
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018780#ifdef FEAT_FLOAT
18781/*
18782 * "sqrt()" function
18783 */
18784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018785f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018786{
18787 float_T f;
18788
18789 rettv->v_type = VAR_FLOAT;
18790 if (get_float_arg(argvars, &f) == OK)
18791 rettv->vval.v_float = sqrt(f);
18792 else
18793 rettv->vval.v_float = 0.0;
18794}
18795
18796/*
18797 * "str2float()" function
18798 */
18799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018800f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018801{
18802 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18803
18804 if (*p == '+')
18805 p = skipwhite(p + 1);
18806 (void)string2float(p, &rettv->vval.v_float);
18807 rettv->v_type = VAR_FLOAT;
18808}
18809#endif
18810
Bram Moolenaar2c932302006-03-18 21:42:09 +000018811/*
18812 * "str2nr()" function
18813 */
18814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018815f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018816{
18817 int base = 10;
18818 char_u *p;
18819 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018820 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018821
18822 if (argvars[1].v_type != VAR_UNKNOWN)
18823 {
18824 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018825 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018826 {
18827 EMSG(_(e_invarg));
18828 return;
18829 }
18830 }
18831
18832 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018833 if (*p == '+')
18834 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018835 switch (base)
18836 {
18837 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18838 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18839 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18840 default: what = 0;
18841 }
18842 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018843 rettv->vval.v_number = n;
18844}
18845
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846#ifdef HAVE_STRFTIME
18847/*
18848 * "strftime({format}[, {time}])" function
18849 */
18850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018851f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852{
18853 char_u result_buf[256];
18854 struct tm *curtime;
18855 time_t seconds;
18856 char_u *p;
18857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018858 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018860 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018861 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 seconds = time(NULL);
18863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018864 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 curtime = localtime(&seconds);
18866 /* MSVC returns NULL for an invalid value of seconds. */
18867 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018868 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018869 else
18870 {
18871# ifdef FEAT_MBYTE
18872 vimconv_T conv;
18873 char_u *enc;
18874
18875 conv.vc_type = CONV_NONE;
18876 enc = enc_locale();
18877 convert_setup(&conv, p_enc, enc);
18878 if (conv.vc_type != CONV_NONE)
18879 p = string_convert(&conv, p, NULL);
18880# endif
18881 if (p != NULL)
18882 (void)strftime((char *)result_buf, sizeof(result_buf),
18883 (char *)p, curtime);
18884 else
18885 result_buf[0] = NUL;
18886
18887# ifdef FEAT_MBYTE
18888 if (conv.vc_type != CONV_NONE)
18889 vim_free(p);
18890 convert_setup(&conv, enc, p_enc);
18891 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018892 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893 else
18894# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018895 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896
18897# ifdef FEAT_MBYTE
18898 /* Release conversion descriptors */
18899 convert_setup(&conv, NULL, NULL);
18900 vim_free(enc);
18901# endif
18902 }
18903}
18904#endif
18905
18906/*
18907 * "stridx()" function
18908 */
18909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018910f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911{
18912 char_u buf[NUMBUFLEN];
18913 char_u *needle;
18914 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018915 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018917 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018919 needle = get_tv_string_chk(&argvars[1]);
18920 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018921 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018922 if (needle == NULL || haystack == NULL)
18923 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924
Bram Moolenaar33570922005-01-25 22:26:29 +000018925 if (argvars[2].v_type != VAR_UNKNOWN)
18926 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018927 int error = FALSE;
18928
18929 start_idx = get_tv_number_chk(&argvars[2], &error);
18930 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018931 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018932 if (start_idx >= 0)
18933 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018934 }
18935
18936 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18937 if (pos != NULL)
18938 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939}
18940
18941/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018942 * "string()" function
18943 */
18944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018945f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018946{
18947 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018948 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018949
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018950 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018951 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018952 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018953 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018954 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955}
18956
18957/*
18958 * "strlen()" function
18959 */
18960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018961f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018963 rettv->vval.v_number = (varnumber_T)(STRLEN(
18964 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965}
18966
18967/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018968 * "strchars()" function
18969 */
18970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018971f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018972{
18973 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018974 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018975#ifdef FEAT_MBYTE
18976 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018977 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018978#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018979
18980 if (argvars[1].v_type != VAR_UNKNOWN)
18981 skipcc = get_tv_number_chk(&argvars[1], NULL);
18982 if (skipcc < 0 || skipcc > 1)
18983 EMSG(_(e_invarg));
18984 else
18985 {
18986#ifdef FEAT_MBYTE
18987 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18988 while (*s != NUL)
18989 {
18990 func_mb_ptr2char_adv(&s);
18991 ++len;
18992 }
18993 rettv->vval.v_number = len;
18994#else
18995 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18996#endif
18997 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018998}
18999
19000/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019001 * "strdisplaywidth()" function
19002 */
19003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019004f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019005{
19006 char_u *s = get_tv_string(&argvars[0]);
19007 int col = 0;
19008
19009 if (argvars[1].v_type != VAR_UNKNOWN)
19010 col = get_tv_number(&argvars[1]);
19011
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019012 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019013}
19014
19015/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019016 * "strwidth()" function
19017 */
19018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019019f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019020{
19021 char_u *s = get_tv_string(&argvars[0]);
19022
19023 rettv->vval.v_number = (varnumber_T)(
19024#ifdef FEAT_MBYTE
19025 mb_string2cells(s, -1)
19026#else
19027 STRLEN(s)
19028#endif
19029 );
19030}
19031
19032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 * "strpart()" function
19034 */
19035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019036f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037{
19038 char_u *p;
19039 int n;
19040 int len;
19041 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019042 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019044 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045 slen = (int)STRLEN(p);
19046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019047 n = get_tv_number_chk(&argvars[1], &error);
19048 if (error)
19049 len = 0;
19050 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019051 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052 else
19053 len = slen - n; /* default len: all bytes that are available. */
19054
19055 /*
19056 * Only return the overlap between the specified part and the actual
19057 * string.
19058 */
19059 if (n < 0)
19060 {
19061 len += n;
19062 n = 0;
19063 }
19064 else if (n > slen)
19065 n = slen;
19066 if (len < 0)
19067 len = 0;
19068 else if (n + len > slen)
19069 len = slen - n;
19070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019071 rettv->v_type = VAR_STRING;
19072 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073}
19074
19075/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019076 * "strridx()" function
19077 */
19078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019079f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019080{
19081 char_u buf[NUMBUFLEN];
19082 char_u *needle;
19083 char_u *haystack;
19084 char_u *rest;
19085 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019086 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019088 needle = get_tv_string_chk(&argvars[1]);
19089 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019090
19091 rettv->vval.v_number = -1;
19092 if (needle == NULL || haystack == NULL)
19093 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019094
19095 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019096 if (argvars[2].v_type != VAR_UNKNOWN)
19097 {
19098 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019099 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019100 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019101 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019102 }
19103 else
19104 end_idx = haystack_len;
19105
Bram Moolenaar0d660222005-01-07 21:51:51 +000019106 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019107 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019108 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019109 lastmatch = haystack + end_idx;
19110 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019111 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019112 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019113 for (rest = haystack; *rest != '\0'; ++rest)
19114 {
19115 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019116 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019117 break;
19118 lastmatch = rest;
19119 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019120 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019121
19122 if (lastmatch == NULL)
19123 rettv->vval.v_number = -1;
19124 else
19125 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19126}
19127
19128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 * "strtrans()" function
19130 */
19131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019132f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019134 rettv->v_type = VAR_STRING;
19135 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136}
19137
19138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019139 * "submatch()" function
19140 */
19141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019142f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019143{
Bram Moolenaar41571762014-04-02 19:00:58 +020019144 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019145 int no;
19146 int retList = 0;
19147
19148 no = (int)get_tv_number_chk(&argvars[0], &error);
19149 if (error)
19150 return;
19151 error = FALSE;
19152 if (argvars[1].v_type != VAR_UNKNOWN)
19153 retList = get_tv_number_chk(&argvars[1], &error);
19154 if (error)
19155 return;
19156
19157 if (retList == 0)
19158 {
19159 rettv->v_type = VAR_STRING;
19160 rettv->vval.v_string = reg_submatch(no);
19161 }
19162 else
19163 {
19164 rettv->v_type = VAR_LIST;
19165 rettv->vval.v_list = reg_submatch_list(no);
19166 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019167}
19168
19169/*
19170 * "substitute()" function
19171 */
19172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019173f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019174{
19175 char_u patbuf[NUMBUFLEN];
19176 char_u subbuf[NUMBUFLEN];
19177 char_u flagsbuf[NUMBUFLEN];
19178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019179 char_u *str = get_tv_string_chk(&argvars[0]);
19180 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19181 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19182 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19183
Bram Moolenaar0d660222005-01-07 21:51:51 +000019184 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019185 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19186 rettv->vval.v_string = NULL;
19187 else
19188 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019189}
19190
19191/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019192 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019195f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196{
19197 int id = 0;
19198#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019199 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200 long col;
19201 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019202 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019204 lnum = get_tv_lnum(argvars); /* -1 on type error */
19205 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19206 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019208 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019209 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019210 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211#endif
19212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019213 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214}
19215
19216/*
19217 * "synIDattr(id, what [, mode])" function
19218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019220f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221{
19222 char_u *p = NULL;
19223#ifdef FEAT_SYN_HL
19224 int id;
19225 char_u *what;
19226 char_u *mode;
19227 char_u modebuf[NUMBUFLEN];
19228 int modec;
19229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019230 id = get_tv_number(&argvars[0]);
19231 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019232 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019234 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019236 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237 modec = 0; /* replace invalid with current */
19238 }
19239 else
19240 {
19241#ifdef FEAT_GUI
19242 if (gui.in_use)
19243 modec = 'g';
19244 else
19245#endif
19246 if (t_colors > 1)
19247 modec = 'c';
19248 else
19249 modec = 't';
19250 }
19251
19252
19253 switch (TOLOWER_ASC(what[0]))
19254 {
19255 case 'b':
19256 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19257 p = highlight_color(id, what, modec);
19258 else /* bold */
19259 p = highlight_has_attr(id, HL_BOLD, modec);
19260 break;
19261
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019262 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263 p = highlight_color(id, what, modec);
19264 break;
19265
19266 case 'i':
19267 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19268 p = highlight_has_attr(id, HL_INVERSE, modec);
19269 else /* italic */
19270 p = highlight_has_attr(id, HL_ITALIC, modec);
19271 break;
19272
19273 case 'n': /* name */
19274 p = get_highlight_name(NULL, id - 1);
19275 break;
19276
19277 case 'r': /* reverse */
19278 p = highlight_has_attr(id, HL_INVERSE, modec);
19279 break;
19280
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019281 case 's':
19282 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19283 p = highlight_color(id, what, modec);
19284 else /* standout */
19285 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286 break;
19287
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019288 case 'u':
19289 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19290 /* underline */
19291 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19292 else
19293 /* undercurl */
19294 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 break;
19296 }
19297
19298 if (p != NULL)
19299 p = vim_strsave(p);
19300#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019301 rettv->v_type = VAR_STRING;
19302 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303}
19304
19305/*
19306 * "synIDtrans(id)" function
19307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019309f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310{
19311 int id;
19312
19313#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019314 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315
19316 if (id > 0)
19317 id = syn_get_final_id(id);
19318 else
19319#endif
19320 id = 0;
19321
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323}
19324
19325/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019326 * "synconcealed(lnum, col)" function
19327 */
19328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019329f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019330{
19331#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19332 long lnum;
19333 long col;
19334 int syntax_flags = 0;
19335 int cchar;
19336 int matchid = 0;
19337 char_u str[NUMBUFLEN];
19338#endif
19339
19340 rettv->v_type = VAR_LIST;
19341 rettv->vval.v_list = NULL;
19342
19343#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19344 lnum = get_tv_lnum(argvars); /* -1 on type error */
19345 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19346
19347 vim_memset(str, NUL, sizeof(str));
19348
19349 if (rettv_list_alloc(rettv) != FAIL)
19350 {
19351 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19352 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19353 && curwin->w_p_cole > 0)
19354 {
19355 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19356 syntax_flags = get_syntax_info(&matchid);
19357
19358 /* get the conceal character */
19359 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19360 {
19361 cchar = syn_get_sub_char();
19362 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19363 cchar = lcs_conceal;
19364 if (cchar != NUL)
19365 {
19366# ifdef FEAT_MBYTE
19367 if (has_mbyte)
19368 (*mb_char2bytes)(cchar, str);
19369 else
19370# endif
19371 str[0] = cchar;
19372 }
19373 }
19374 }
19375
19376 list_append_number(rettv->vval.v_list,
19377 (syntax_flags & HL_CONCEAL) != 0);
19378 /* -1 to auto-determine strlen */
19379 list_append_string(rettv->vval.v_list, str, -1);
19380 list_append_number(rettv->vval.v_list, matchid);
19381 }
19382#endif
19383}
19384
19385/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019386 * "synstack(lnum, col)" function
19387 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019389f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019390{
19391#ifdef FEAT_SYN_HL
19392 long lnum;
19393 long col;
19394 int i;
19395 int id;
19396#endif
19397
19398 rettv->v_type = VAR_LIST;
19399 rettv->vval.v_list = NULL;
19400
19401#ifdef FEAT_SYN_HL
19402 lnum = get_tv_lnum(argvars); /* -1 on type error */
19403 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19404
19405 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019406 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019407 && rettv_list_alloc(rettv) != FAIL)
19408 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019409 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019410 for (i = 0; ; ++i)
19411 {
19412 id = syn_get_stack_item(i);
19413 if (id < 0)
19414 break;
19415 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19416 break;
19417 }
19418 }
19419#endif
19420}
19421
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019423get_cmd_output_as_rettv(
19424 typval_T *argvars,
19425 typval_T *rettv,
19426 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019428 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019430 char_u *infile = NULL;
19431 char_u buf[NUMBUFLEN];
19432 int err = FALSE;
19433 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019434 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019435 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019437 rettv->v_type = VAR_STRING;
19438 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019439 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019440 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019441
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019442 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019443 {
19444 /*
19445 * Write the string to a temp file, to be used for input of the shell
19446 * command.
19447 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019448 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019449 {
19450 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019451 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019452 }
19453
19454 fd = mch_fopen((char *)infile, WRITEBIN);
19455 if (fd == NULL)
19456 {
19457 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019458 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019459 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019460 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019461 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019462 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19463 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019464 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019465 else
19466 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019467 size_t len;
19468
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019469 p = get_tv_string_buf_chk(&argvars[1], buf);
19470 if (p == NULL)
19471 {
19472 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019473 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019474 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019475 len = STRLEN(p);
19476 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019477 err = TRUE;
19478 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019479 if (fclose(fd) != 0)
19480 err = TRUE;
19481 if (err)
19482 {
19483 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019484 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019485 }
19486 }
19487
Bram Moolenaar52a72462014-08-29 15:53:52 +020019488 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19489 * echoes typeahead, that messes up the display. */
19490 if (!msg_silent)
19491 flags += SHELL_COOKED;
19492
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019493 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019495 int len;
19496 listitem_T *li;
19497 char_u *s = NULL;
19498 char_u *start;
19499 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019500 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501
Bram Moolenaar52a72462014-08-29 15:53:52 +020019502 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019503 if (res == NULL)
19504 goto errret;
19505
19506 list = list_alloc();
19507 if (list == NULL)
19508 goto errret;
19509
19510 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019512 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019513 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019514 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019515 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019516
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019517 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019518 if (s == NULL)
19519 goto errret;
19520
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019521 for (p = s; start < end; ++p, ++start)
19522 *p = *start == NUL ? NL : *start;
19523 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019524
19525 li = listitem_alloc();
19526 if (li == NULL)
19527 {
19528 vim_free(s);
19529 goto errret;
19530 }
19531 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019532 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019533 li->li_tv.vval.v_string = s;
19534 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019536
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019537 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019538 rettv->v_type = VAR_LIST;
19539 rettv->vval.v_list = list;
19540 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019542 else
19543 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019544 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019545#ifdef USE_CR
19546 /* translate <CR> into <NL> */
19547 if (res != NULL)
19548 {
19549 char_u *s;
19550
19551 for (s = res; *s; ++s)
19552 {
19553 if (*s == CAR)
19554 *s = NL;
19555 }
19556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557#else
19558# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019559 /* translate <CR><NL> into <NL> */
19560 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019562 char_u *s, *d;
19563
19564 d = res;
19565 for (s = res; *s; ++s)
19566 {
19567 if (s[0] == CAR && s[1] == NL)
19568 ++s;
19569 *d++ = *s;
19570 }
19571 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573# endif
19574#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019575 rettv->vval.v_string = res;
19576 res = NULL;
19577 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019578
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019579errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019580 if (infile != NULL)
19581 {
19582 mch_remove(infile);
19583 vim_free(infile);
19584 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019585 if (res != NULL)
19586 vim_free(res);
19587 if (list != NULL)
19588 list_free(list, TRUE);
19589}
19590
19591/*
19592 * "system()" function
19593 */
19594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019595f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019596{
19597 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19598}
19599
19600/*
19601 * "systemlist()" function
19602 */
19603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019604f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019605{
19606 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607}
19608
19609/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019610 * "tabpagebuflist()" function
19611 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019613f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019614{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019615#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019616 tabpage_T *tp;
19617 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019618
19619 if (argvars[0].v_type == VAR_UNKNOWN)
19620 wp = firstwin;
19621 else
19622 {
19623 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19624 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019625 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019626 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019627 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019628 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019629 for (; wp != NULL; wp = wp->w_next)
19630 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019631 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019632 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019633 }
19634#endif
19635}
19636
19637
19638/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019639 * "tabpagenr()" function
19640 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019642f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019643{
19644 int nr = 1;
19645#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019646 char_u *arg;
19647
19648 if (argvars[0].v_type != VAR_UNKNOWN)
19649 {
19650 arg = get_tv_string_chk(&argvars[0]);
19651 nr = 0;
19652 if (arg != NULL)
19653 {
19654 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019655 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019656 else
19657 EMSG2(_(e_invexpr2), arg);
19658 }
19659 }
19660 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019661 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019662#endif
19663 rettv->vval.v_number = nr;
19664}
19665
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019666
19667#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019668static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019669
19670/*
19671 * Common code for tabpagewinnr() and winnr().
19672 */
19673 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019674get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019675{
19676 win_T *twin;
19677 int nr = 1;
19678 win_T *wp;
19679 char_u *arg;
19680
19681 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19682 if (argvar->v_type != VAR_UNKNOWN)
19683 {
19684 arg = get_tv_string_chk(argvar);
19685 if (arg == NULL)
19686 nr = 0; /* type error; errmsg already given */
19687 else if (STRCMP(arg, "$") == 0)
19688 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19689 else if (STRCMP(arg, "#") == 0)
19690 {
19691 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19692 if (twin == NULL)
19693 nr = 0;
19694 }
19695 else
19696 {
19697 EMSG2(_(e_invexpr2), arg);
19698 nr = 0;
19699 }
19700 }
19701
19702 if (nr > 0)
19703 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19704 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019705 {
19706 if (wp == NULL)
19707 {
19708 /* didn't find it in this tabpage */
19709 nr = 0;
19710 break;
19711 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019712 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019713 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019714 return nr;
19715}
19716#endif
19717
19718/*
19719 * "tabpagewinnr()" function
19720 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019722f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019723{
19724 int nr = 1;
19725#ifdef FEAT_WINDOWS
19726 tabpage_T *tp;
19727
19728 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19729 if (tp == NULL)
19730 nr = 0;
19731 else
19732 nr = get_winnr(tp, &argvars[1]);
19733#endif
19734 rettv->vval.v_number = nr;
19735}
19736
19737
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019738/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019739 * "tagfiles()" function
19740 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019742f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019743{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019744 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019745 tagname_T tn;
19746 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019747
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019748 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019749 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019750 fname = alloc(MAXPATHL);
19751 if (fname == NULL)
19752 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019753
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019754 for (first = TRUE; ; first = FALSE)
19755 if (get_tagfname(&tn, first, fname) == FAIL
19756 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019757 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019758 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019759 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019760}
19761
19762/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019763 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019764 */
19765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019766f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019767{
19768 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019769
19770 tag_pattern = get_tv_string(&argvars[0]);
19771
19772 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019773 if (*tag_pattern == NUL)
19774 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019775
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019776 if (rettv_list_alloc(rettv) == OK)
19777 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019778}
19779
19780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781 * "tempname()" function
19782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019784f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785{
19786 static int x = 'A';
19787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019788 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019789 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790
19791 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19792 * names. Skip 'I' and 'O', they are used for shell redirection. */
19793 do
19794 {
19795 if (x == 'Z')
19796 x = '0';
19797 else if (x == '9')
19798 x = 'A';
19799 else
19800 {
19801#ifdef EBCDIC
19802 if (x == 'I')
19803 x = 'J';
19804 else if (x == 'R')
19805 x = 'S';
19806 else
19807#endif
19808 ++x;
19809 }
19810 } while (x == 'I' || x == 'O');
19811}
19812
19813/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019814 * "test(list)" function: Just checking the walls...
19815 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019817f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019818{
19819 /* Used for unit testing. Change the code below to your liking. */
19820#if 0
19821 listitem_T *li;
19822 list_T *l;
19823 char_u *bad, *good;
19824
19825 if (argvars[0].v_type != VAR_LIST)
19826 return;
19827 l = argvars[0].vval.v_list;
19828 if (l == NULL)
19829 return;
19830 li = l->lv_first;
19831 if (li == NULL)
19832 return;
19833 bad = get_tv_string(&li->li_tv);
19834 li = li->li_next;
19835 if (li == NULL)
19836 return;
19837 good = get_tv_string(&li->li_tv);
19838 rettv->vval.v_number = test_edit_score(bad, good);
19839#endif
19840}
19841
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019842#ifdef FEAT_FLOAT
19843/*
19844 * "tan()" function
19845 */
19846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019847f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019848{
19849 float_T f;
19850
19851 rettv->v_type = VAR_FLOAT;
19852 if (get_float_arg(argvars, &f) == OK)
19853 rettv->vval.v_float = tan(f);
19854 else
19855 rettv->vval.v_float = 0.0;
19856}
19857
19858/*
19859 * "tanh()" function
19860 */
19861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019862f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019863{
19864 float_T f;
19865
19866 rettv->v_type = VAR_FLOAT;
19867 if (get_float_arg(argvars, &f) == OK)
19868 rettv->vval.v_float = tanh(f);
19869 else
19870 rettv->vval.v_float = 0.0;
19871}
19872#endif
19873
Bram Moolenaard52d9742005-08-21 22:20:28 +000019874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 * "tolower(string)" function
19876 */
19877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019878f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879{
19880 char_u *p;
19881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019882 p = vim_strsave(get_tv_string(&argvars[0]));
19883 rettv->v_type = VAR_STRING;
19884 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885
19886 if (p != NULL)
19887 while (*p != NUL)
19888 {
19889#ifdef FEAT_MBYTE
19890 int l;
19891
19892 if (enc_utf8)
19893 {
19894 int c, lc;
19895
19896 c = utf_ptr2char(p);
19897 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019898 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 /* TODO: reallocate string when byte count changes. */
19900 if (utf_char2len(lc) == l)
19901 utf_char2bytes(lc, p);
19902 p += l;
19903 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019904 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 p += l; /* skip multi-byte character */
19906 else
19907#endif
19908 {
19909 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19910 ++p;
19911 }
19912 }
19913}
19914
19915/*
19916 * "toupper(string)" function
19917 */
19918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019919f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019921 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019922 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923}
19924
19925/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019926 * "tr(string, fromstr, tostr)" function
19927 */
19928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019929f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019930{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019931 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019932 char_u *fromstr;
19933 char_u *tostr;
19934 char_u *p;
19935#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019936 int inlen;
19937 int fromlen;
19938 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019939 int idx;
19940 char_u *cpstr;
19941 int cplen;
19942 int first = TRUE;
19943#endif
19944 char_u buf[NUMBUFLEN];
19945 char_u buf2[NUMBUFLEN];
19946 garray_T ga;
19947
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019948 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019949 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19950 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019951
19952 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019953 rettv->v_type = VAR_STRING;
19954 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019955 if (fromstr == NULL || tostr == NULL)
19956 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019957 ga_init2(&ga, (int)sizeof(char), 80);
19958
19959#ifdef FEAT_MBYTE
19960 if (!has_mbyte)
19961#endif
19962 /* not multi-byte: fromstr and tostr must be the same length */
19963 if (STRLEN(fromstr) != STRLEN(tostr))
19964 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019965#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019966error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019967#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019968 EMSG2(_(e_invarg2), fromstr);
19969 ga_clear(&ga);
19970 return;
19971 }
19972
19973 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019974 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019975 {
19976#ifdef FEAT_MBYTE
19977 if (has_mbyte)
19978 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019979 inlen = (*mb_ptr2len)(in_str);
19980 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019981 cplen = inlen;
19982 idx = 0;
19983 for (p = fromstr; *p != NUL; p += fromlen)
19984 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019985 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019986 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019987 {
19988 for (p = tostr; *p != NUL; p += tolen)
19989 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019990 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019991 if (idx-- == 0)
19992 {
19993 cplen = tolen;
19994 cpstr = p;
19995 break;
19996 }
19997 }
19998 if (*p == NUL) /* tostr is shorter than fromstr */
19999 goto error;
20000 break;
20001 }
20002 ++idx;
20003 }
20004
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020005 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020006 {
20007 /* Check that fromstr and tostr have the same number of
20008 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020009 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020010 first = FALSE;
20011 for (p = tostr; *p != NUL; p += tolen)
20012 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020013 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020014 --idx;
20015 }
20016 if (idx != 0)
20017 goto error;
20018 }
20019
Bram Moolenaarcde88542015-08-11 19:14:00 +020020020 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020021 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020022 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020023
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020024 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020025 }
20026 else
20027#endif
20028 {
20029 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020030 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020031 if (p != NULL)
20032 ga_append(&ga, tostr[p - fromstr]);
20033 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020034 ga_append(&ga, *in_str);
20035 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020036 }
20037 }
20038
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020039 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020040 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020041 ga_append(&ga, NUL);
20042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020043 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020044}
20045
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020046#ifdef FEAT_FLOAT
20047/*
20048 * "trunc({float})" function
20049 */
20050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020051f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020052{
20053 float_T f;
20054
20055 rettv->v_type = VAR_FLOAT;
20056 if (get_float_arg(argvars, &f) == OK)
20057 /* trunc() is not in C90, use floor() or ceil() instead. */
20058 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20059 else
20060 rettv->vval.v_float = 0.0;
20061}
20062#endif
20063
Bram Moolenaar8299df92004-07-10 09:47:34 +000020064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 * "type(expr)" function
20066 */
20067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020068f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020070 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020071
20072 switch (argvars[0].v_type)
20073 {
20074 case VAR_NUMBER: n = 0; break;
20075 case VAR_STRING: n = 1; break;
20076 case VAR_FUNC: n = 2; break;
20077 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020078 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020079 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020080 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020081 if (argvars[0].vval.v_number == VVAL_FALSE
20082 || argvars[0].vval.v_number == VVAL_TRUE)
20083 n = 6;
20084 else
20085 n = 7;
20086 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020087 case VAR_JOB: n = 8; break;
20088 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020089 case VAR_UNKNOWN:
20090 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20091 n = -1;
20092 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020093 }
20094 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095}
20096
20097/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020098 * "undofile(name)" function
20099 */
20100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020101f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020102{
20103 rettv->v_type = VAR_STRING;
20104#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020105 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020106 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020107
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020108 if (*fname == NUL)
20109 {
20110 /* If there is no file name there will be no undo file. */
20111 rettv->vval.v_string = NULL;
20112 }
20113 else
20114 {
20115 char_u *ffname = FullName_save(fname, FALSE);
20116
20117 if (ffname != NULL)
20118 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20119 vim_free(ffname);
20120 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020121 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020122#else
20123 rettv->vval.v_string = NULL;
20124#endif
20125}
20126
20127/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020128 * "undotree()" function
20129 */
20130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020131f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020132{
20133 if (rettv_dict_alloc(rettv) == OK)
20134 {
20135 dict_T *dict = rettv->vval.v_dict;
20136 list_T *list;
20137
Bram Moolenaar730cde92010-06-27 05:18:54 +020020138 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020139 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020140 dict_add_nr_str(dict, "save_last",
20141 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020142 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20143 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020144 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020145
20146 list = list_alloc();
20147 if (list != NULL)
20148 {
20149 u_eval_tree(curbuf->b_u_oldhead, list);
20150 dict_add_list(dict, "entries", list);
20151 }
20152 }
20153}
20154
20155/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020156 * "values(dict)" function
20157 */
20158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020159f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020160{
20161 dict_list(argvars, rettv, 1);
20162}
20163
20164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 * "virtcol(string)" function
20166 */
20167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020168f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169{
20170 colnr_T vcol = 0;
20171 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020172 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020174 fp = var2fpos(&argvars[0], FALSE, &fnum);
20175 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20176 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 {
20178 getvvcol(curwin, fp, NULL, NULL, &vcol);
20179 ++vcol;
20180 }
20181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020182 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183}
20184
20185/*
20186 * "visualmode()" function
20187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020189f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 char_u str[2];
20192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020193 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 str[0] = curbuf->b_visual_mode_eval;
20195 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020196 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197
20198 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020199 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201}
20202
20203/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020204 * "wildmenumode()" function
20205 */
20206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020207f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020208{
20209#ifdef FEAT_WILDMENU
20210 if (wild_menu_showing)
20211 rettv->vval.v_number = 1;
20212#endif
20213}
20214
20215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216 * "winbufnr(nr)" function
20217 */
20218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020219f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220{
20221 win_T *wp;
20222
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020223 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020225 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020227 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228}
20229
20230/*
20231 * "wincol()" function
20232 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020234f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235{
20236 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020237 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238}
20239
20240/*
20241 * "winheight(nr)" function
20242 */
20243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020244f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245{
20246 win_T *wp;
20247
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020248 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020250 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020252 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253}
20254
20255/*
20256 * "winline()" function
20257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020259f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260{
20261 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020262 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263}
20264
20265/*
20266 * "winnr()" function
20267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020269f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270{
20271 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020272
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020274 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020276 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277}
20278
20279/*
20280 * "winrestcmd()" function
20281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020283f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284{
20285#ifdef FEAT_WINDOWS
20286 win_T *wp;
20287 int winnr = 1;
20288 garray_T ga;
20289 char_u buf[50];
20290
20291 ga_init2(&ga, (int)sizeof(char), 70);
20292 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20293 {
20294 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20295 ga_concat(&ga, buf);
20296# ifdef FEAT_VERTSPLIT
20297 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20298 ga_concat(&ga, buf);
20299# endif
20300 ++winnr;
20301 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020302 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020304 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020306 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020308 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309}
20310
20311/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020312 * "winrestview()" function
20313 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020315f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020316{
20317 dict_T *dict;
20318
20319 if (argvars[0].v_type != VAR_DICT
20320 || (dict = argvars[0].vval.v_dict) == NULL)
20321 EMSG(_(e_invarg));
20322 else
20323 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020324 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20325 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20326 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20327 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020328#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020329 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20330 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020331#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020332 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20333 {
20334 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20335 curwin->w_set_curswant = FALSE;
20336 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020337
Bram Moolenaar82c25852014-05-28 16:47:16 +020020338 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20339 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020340#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020341 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20342 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020343#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020344 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20345 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20346 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20347 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020348
20349 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020350 win_new_height(curwin, curwin->w_height);
20351# ifdef FEAT_VERTSPLIT
20352 win_new_width(curwin, W_WIDTH(curwin));
20353# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020354 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020355
Bram Moolenaarb851a962014-10-31 15:45:52 +010020356 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020357 curwin->w_topline = 1;
20358 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20359 curwin->w_topline = curbuf->b_ml.ml_line_count;
20360#ifdef FEAT_DIFF
20361 check_topfill(curwin, TRUE);
20362#endif
20363 }
20364}
20365
20366/*
20367 * "winsaveview()" function
20368 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020370f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020371{
20372 dict_T *dict;
20373
Bram Moolenaara800b422010-06-27 01:15:55 +020020374 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020375 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020376 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020377
20378 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20379 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20380#ifdef FEAT_VIRTUALEDIT
20381 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20382#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020383 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020384 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20385
20386 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20387#ifdef FEAT_DIFF
20388 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20389#endif
20390 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20391 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20392}
20393
20394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395 * "winwidth(nr)" function
20396 */
20397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020398f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399{
20400 win_T *wp;
20401
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020402 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020404 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 else
20406#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020407 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020409 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410#endif
20411}
20412
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020414 * "wordcount()" function
20415 */
20416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020417f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020418{
20419 if (rettv_dict_alloc(rettv) == FAIL)
20420 return;
20421 cursor_pos_info(rettv->vval.v_dict);
20422}
20423
20424/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020425 * Write list of strings to file
20426 */
20427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020428write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020429{
20430 listitem_T *li;
20431 int c;
20432 int ret = OK;
20433 char_u *s;
20434
20435 for (li = list->lv_first; li != NULL; li = li->li_next)
20436 {
20437 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20438 {
20439 if (*s == '\n')
20440 c = putc(NUL, fd);
20441 else
20442 c = putc(*s, fd);
20443 if (c == EOF)
20444 {
20445 ret = FAIL;
20446 break;
20447 }
20448 }
20449 if (!binary || li->li_next != NULL)
20450 if (putc('\n', fd) == EOF)
20451 {
20452 ret = FAIL;
20453 break;
20454 }
20455 if (ret == FAIL)
20456 {
20457 EMSG(_(e_write));
20458 break;
20459 }
20460 }
20461 return ret;
20462}
20463
20464/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020465 * "writefile()" function
20466 */
20467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020468f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020469{
20470 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020471 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020472 char_u *fname;
20473 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020474 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020475
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020476 if (check_restricted() || check_secure())
20477 return;
20478
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020479 if (argvars[0].v_type != VAR_LIST)
20480 {
20481 EMSG2(_(e_listarg), "writefile()");
20482 return;
20483 }
20484 if (argvars[0].vval.v_list == NULL)
20485 return;
20486
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020487 if (argvars[2].v_type != VAR_UNKNOWN)
20488 {
20489 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20490 binary = TRUE;
20491 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20492 append = TRUE;
20493 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020494
20495 /* Always open the file in binary mode, library functions have a mind of
20496 * their own about CR-LF conversion. */
20497 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020498 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20499 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020500 {
20501 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20502 ret = -1;
20503 }
20504 else
20505 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020506 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20507 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020508 fclose(fd);
20509 }
20510
20511 rettv->vval.v_number = ret;
20512}
20513
20514/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020515 * "xor(expr, expr)" function
20516 */
20517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020518f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020519{
20520 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20521 ^ get_tv_number_chk(&argvars[1], NULL);
20522}
20523
20524
20525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020527 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 */
20529 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020530var2fpos(
20531 typval_T *varp,
20532 int dollar_lnum, /* TRUE when $ is last line */
20533 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020535 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020537 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538
Bram Moolenaara5525202006-03-02 22:52:09 +000020539 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020540 if (varp->v_type == VAR_LIST)
20541 {
20542 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020543 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020544 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020545 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020546
20547 l = varp->vval.v_list;
20548 if (l == NULL)
20549 return NULL;
20550
20551 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020552 pos.lnum = list_find_nr(l, 0L, &error);
20553 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020554 return NULL; /* invalid line number */
20555
20556 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020557 pos.col = list_find_nr(l, 1L, &error);
20558 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020559 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020560 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020561
20562 /* We accept "$" for the column number: last column. */
20563 li = list_find(l, 1L);
20564 if (li != NULL && li->li_tv.v_type == VAR_STRING
20565 && li->li_tv.vval.v_string != NULL
20566 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20567 pos.col = len + 1;
20568
Bram Moolenaara5525202006-03-02 22:52:09 +000020569 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020570 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020571 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020572 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020573
Bram Moolenaara5525202006-03-02 22:52:09 +000020574#ifdef FEAT_VIRTUALEDIT
20575 /* Get the virtual offset. Defaults to zero. */
20576 pos.coladd = list_find_nr(l, 2L, &error);
20577 if (error)
20578 pos.coladd = 0;
20579#endif
20580
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020581 return &pos;
20582 }
20583
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020584 name = get_tv_string_chk(varp);
20585 if (name == NULL)
20586 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020587 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020589 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20590 {
20591 if (VIsual_active)
20592 return &VIsual;
20593 return &curwin->w_cursor;
20594 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020595 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020597 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20599 return NULL;
20600 return pp;
20601 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020602
20603#ifdef FEAT_VIRTUALEDIT
20604 pos.coladd = 0;
20605#endif
20606
Bram Moolenaar477933c2007-07-17 14:32:23 +000020607 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020608 {
20609 pos.col = 0;
20610 if (name[1] == '0') /* "w0": first visible line */
20611 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020612 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020613 pos.lnum = curwin->w_topline;
20614 return &pos;
20615 }
20616 else if (name[1] == '$') /* "w$": last visible line */
20617 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020618 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020619 pos.lnum = curwin->w_botline - 1;
20620 return &pos;
20621 }
20622 }
20623 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020625 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 {
20627 pos.lnum = curbuf->b_ml.ml_line_count;
20628 pos.col = 0;
20629 }
20630 else
20631 {
20632 pos.lnum = curwin->w_cursor.lnum;
20633 pos.col = (colnr_T)STRLEN(ml_get_curline());
20634 }
20635 return &pos;
20636 }
20637 return NULL;
20638}
20639
20640/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020641 * Convert list in "arg" into a position and optional file number.
20642 * When "fnump" is NULL there is no file number, only 3 items.
20643 * Note that the column is passed on as-is, the caller may want to decrement
20644 * it to use 1 for the first column.
20645 * Return FAIL when conversion is not possible, doesn't check the position for
20646 * validity.
20647 */
20648 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020649list2fpos(
20650 typval_T *arg,
20651 pos_T *posp,
20652 int *fnump,
20653 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020654{
20655 list_T *l = arg->vval.v_list;
20656 long i = 0;
20657 long n;
20658
Bram Moolenaar493c1782014-05-28 14:34:46 +020020659 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20660 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020661 if (arg->v_type != VAR_LIST
20662 || l == NULL
20663 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020664 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020665 return FAIL;
20666
20667 if (fnump != NULL)
20668 {
20669 n = list_find_nr(l, i++, NULL); /* fnum */
20670 if (n < 0)
20671 return FAIL;
20672 if (n == 0)
20673 n = curbuf->b_fnum; /* current buffer */
20674 *fnump = n;
20675 }
20676
20677 n = list_find_nr(l, i++, NULL); /* lnum */
20678 if (n < 0)
20679 return FAIL;
20680 posp->lnum = n;
20681
20682 n = list_find_nr(l, i++, NULL); /* col */
20683 if (n < 0)
20684 return FAIL;
20685 posp->col = n;
20686
20687#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020688 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020689 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020690 posp->coladd = 0;
20691 else
20692 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020693#endif
20694
Bram Moolenaar493c1782014-05-28 14:34:46 +020020695 if (curswantp != NULL)
20696 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20697
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020698 return OK;
20699}
20700
20701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 * Get the length of an environment variable name.
20703 * Advance "arg" to the first character after the name.
20704 * Return 0 for error.
20705 */
20706 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020707get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708{
20709 char_u *p;
20710 int len;
20711
20712 for (p = *arg; vim_isIDc(*p); ++p)
20713 ;
20714 if (p == *arg) /* no name found */
20715 return 0;
20716
20717 len = (int)(p - *arg);
20718 *arg = p;
20719 return len;
20720}
20721
20722/*
20723 * Get the length of the name of a function or internal variable.
20724 * "arg" is advanced to the first non-white character after the name.
20725 * Return 0 if something is wrong.
20726 */
20727 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020728get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729{
20730 char_u *p;
20731 int len;
20732
20733 /* Find the end of the name. */
20734 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020735 {
20736 if (*p == ':')
20737 {
20738 /* "s:" is start of "s:var", but "n:" is not and can be used in
20739 * slice "[n:]". Also "xx:" is not a namespace. */
20740 len = (int)(p - *arg);
20741 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20742 || len > 1)
20743 break;
20744 }
20745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746 if (p == *arg) /* no name found */
20747 return 0;
20748
20749 len = (int)(p - *arg);
20750 *arg = skipwhite(p);
20751
20752 return len;
20753}
20754
20755/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020756 * Get the length of the name of a variable or function.
20757 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020759 * Return -1 if curly braces expansion failed.
20760 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 * If the name contains 'magic' {}'s, expand them and return the
20762 * expanded name in an allocated string via 'alias' - caller must free.
20763 */
20764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020765get_name_len(
20766 char_u **arg,
20767 char_u **alias,
20768 int evaluate,
20769 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770{
20771 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 char_u *p;
20773 char_u *expr_start;
20774 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775
20776 *alias = NULL; /* default to no alias */
20777
20778 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20779 && (*arg)[2] == (int)KE_SNR)
20780 {
20781 /* hard coded <SNR>, already translated */
20782 *arg += 3;
20783 return get_id_len(arg) + 3;
20784 }
20785 len = eval_fname_script(*arg);
20786 if (len > 0)
20787 {
20788 /* literal "<SID>", "s:" or "<SNR>" */
20789 *arg += len;
20790 }
20791
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020793 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020795 p = find_name_end(*arg, &expr_start, &expr_end,
20796 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 if (expr_start != NULL)
20798 {
20799 char_u *temp_string;
20800
20801 if (!evaluate)
20802 {
20803 len += (int)(p - *arg);
20804 *arg = skipwhite(p);
20805 return len;
20806 }
20807
20808 /*
20809 * Include any <SID> etc in the expanded string:
20810 * Thus the -len here.
20811 */
20812 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20813 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020814 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 *alias = temp_string;
20816 *arg = skipwhite(p);
20817 return (int)STRLEN(temp_string);
20818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819
20820 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020821 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 EMSG2(_(e_invexpr2), *arg);
20823
20824 return len;
20825}
20826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020827/*
20828 * Find the end of a variable or function name, taking care of magic braces.
20829 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20830 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020831 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020832 * Return a pointer to just after the name. Equal to "arg" if there is no
20833 * valid name.
20834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020836find_name_end(
20837 char_u *arg,
20838 char_u **expr_start,
20839 char_u **expr_end,
20840 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020842 int mb_nest = 0;
20843 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020845 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020847 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020849 *expr_start = NULL;
20850 *expr_end = NULL;
20851 }
20852
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020853 /* Quick check for valid starting character. */
20854 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20855 return arg;
20856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020857 for (p = arg; *p != NUL
20858 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020859 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020860 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020861 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020862 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020863 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020864 if (*p == '\'')
20865 {
20866 /* skip over 'string' to avoid counting [ and ] inside it. */
20867 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20868 ;
20869 if (*p == NUL)
20870 break;
20871 }
20872 else if (*p == '"')
20873 {
20874 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20875 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20876 if (*p == '\\' && p[1] != NUL)
20877 ++p;
20878 if (*p == NUL)
20879 break;
20880 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020881 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20882 {
20883 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020884 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020885 len = (int)(p - arg);
20886 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020887 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020888 break;
20889 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020891 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020893 if (*p == '[')
20894 ++br_nest;
20895 else if (*p == ']')
20896 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020899 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020901 if (*p == '{')
20902 {
20903 mb_nest++;
20904 if (expr_start != NULL && *expr_start == NULL)
20905 *expr_start = p;
20906 }
20907 else if (*p == '}')
20908 {
20909 mb_nest--;
20910 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20911 *expr_end = p;
20912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 }
20915
20916 return p;
20917}
20918
20919/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020920 * Expands out the 'magic' {}'s in a variable/function name.
20921 * Note that this can call itself recursively, to deal with
20922 * constructs like foo{bar}{baz}{bam}
20923 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20924 * "in_start" ^
20925 * "expr_start" ^
20926 * "expr_end" ^
20927 * "in_end" ^
20928 *
20929 * Returns a new allocated string, which the caller must free.
20930 * Returns NULL for failure.
20931 */
20932 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020933make_expanded_name(
20934 char_u *in_start,
20935 char_u *expr_start,
20936 char_u *expr_end,
20937 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020938{
20939 char_u c1;
20940 char_u *retval = NULL;
20941 char_u *temp_result;
20942 char_u *nextcmd = NULL;
20943
20944 if (expr_end == NULL || in_end == NULL)
20945 return NULL;
20946 *expr_start = NUL;
20947 *expr_end = NUL;
20948 c1 = *in_end;
20949 *in_end = NUL;
20950
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020951 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020952 if (temp_result != NULL && nextcmd == NULL)
20953 {
20954 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20955 + (in_end - expr_end) + 1));
20956 if (retval != NULL)
20957 {
20958 STRCPY(retval, in_start);
20959 STRCAT(retval, temp_result);
20960 STRCAT(retval, expr_end + 1);
20961 }
20962 }
20963 vim_free(temp_result);
20964
20965 *in_end = c1; /* put char back for error messages */
20966 *expr_start = '{';
20967 *expr_end = '}';
20968
20969 if (retval != NULL)
20970 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020971 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020972 if (expr_start != NULL)
20973 {
20974 /* Further expansion! */
20975 temp_result = make_expanded_name(retval, expr_start,
20976 expr_end, temp_result);
20977 vim_free(retval);
20978 retval = temp_result;
20979 }
20980 }
20981
20982 return retval;
20983}
20984
20985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020987 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 */
20989 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020990eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020992 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20993}
20994
20995/*
20996 * Return TRUE if character "c" can be used as the first character in a
20997 * variable or function name (excluding '{' and '}').
20998 */
20999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021000eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021001{
21002 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003}
21004
21005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 * Set number v: variable to "val".
21007 */
21008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021009set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021011 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012}
21013
21014/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021015 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 */
21017 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021018get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021020 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021}
21022
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021023/*
21024 * Get string v: variable value. Uses a static buffer, can only be used once.
21025 */
21026 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021027get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021028{
21029 return get_tv_string(&vimvars[idx].vv_tv);
21030}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021031
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021033 * Get List v: variable value. Caller must take care of reference count when
21034 * needed.
21035 */
21036 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021037get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021038{
21039 return vimvars[idx].vv_list;
21040}
21041
21042/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021043 * Set v:char to character "c".
21044 */
21045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021046set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021047{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021048 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021049
21050#ifdef FEAT_MBYTE
21051 if (has_mbyte)
21052 buf[(*mb_char2bytes)(c, buf)] = NUL;
21053 else
21054#endif
21055 {
21056 buf[0] = c;
21057 buf[1] = NUL;
21058 }
21059 set_vim_var_string(VV_CHAR, buf, -1);
21060}
21061
21062/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021063 * Set v:count to "count" and v:count1 to "count1".
21064 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 */
21066 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021067set_vcount(
21068 long count,
21069 long count1,
21070 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021072 if (set_prevcount)
21073 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021074 vimvars[VV_COUNT].vv_nr = count;
21075 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076}
21077
21078/*
21079 * Set string v: variable to a copy of "val".
21080 */
21081 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021082set_vim_var_string(
21083 int idx,
21084 char_u *val,
21085 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086{
Bram Moolenaara542c682016-01-31 16:28:04 +010021087 clear_tv(&vimvars[idx].vv_di.di_tv);
21088 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021090 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021092 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021094 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095}
21096
21097/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021098 * Set List v: variable to "val".
21099 */
21100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021101set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021102{
Bram Moolenaara542c682016-01-31 16:28:04 +010021103 clear_tv(&vimvars[idx].vv_di.di_tv);
21104 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021105 vimvars[idx].vv_list = val;
21106 if (val != NULL)
21107 ++val->lv_refcount;
21108}
21109
21110/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021111 * Set Dictionary v: variable to "val".
21112 */
21113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021114set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021115{
21116 int todo;
21117 hashitem_T *hi;
21118
Bram Moolenaara542c682016-01-31 16:28:04 +010021119 clear_tv(&vimvars[idx].vv_di.di_tv);
21120 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021121 vimvars[idx].vv_dict = val;
21122 if (val != NULL)
21123 {
21124 ++val->dv_refcount;
21125
21126 /* Set readonly */
21127 todo = (int)val->dv_hashtab.ht_used;
21128 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21129 {
21130 if (HASHITEM_EMPTY(hi))
21131 continue;
21132 --todo;
21133 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21134 }
21135 }
21136}
21137
21138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 * Set v:register if needed.
21140 */
21141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021142set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143{
21144 char_u regname;
21145
21146 if (c == 0 || c == ' ')
21147 regname = '"';
21148 else
21149 regname = c;
21150 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021151 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 set_vim_var_string(VV_REG, &regname, 1);
21153}
21154
21155/*
21156 * Get or set v:exception. If "oldval" == NULL, return the current value.
21157 * Otherwise, restore the value to "oldval" and return NULL.
21158 * Must always be called in pairs to save and restore v:exception! Does not
21159 * take care of memory allocations.
21160 */
21161 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021162v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163{
21164 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021165 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166
Bram Moolenaare9a41262005-01-15 22:18:47 +000021167 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 return NULL;
21169}
21170
21171/*
21172 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21173 * Otherwise, restore the value to "oldval" and return NULL.
21174 * Must always be called in pairs to save and restore v:throwpoint! Does not
21175 * take care of memory allocations.
21176 */
21177 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021178v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179{
21180 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021181 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182
Bram Moolenaare9a41262005-01-15 22:18:47 +000021183 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 return NULL;
21185}
21186
21187#if defined(FEAT_AUTOCMD) || defined(PROTO)
21188/*
21189 * Set v:cmdarg.
21190 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21191 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21192 * Must always be called in pairs!
21193 */
21194 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021195set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196{
21197 char_u *oldval;
21198 char_u *newval;
21199 unsigned len;
21200
Bram Moolenaare9a41262005-01-15 22:18:47 +000021201 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021202 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021204 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021205 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021206 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 }
21208
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021209 if (eap->force_bin == FORCE_BIN)
21210 len = 6;
21211 else if (eap->force_bin == FORCE_NOBIN)
21212 len = 8;
21213 else
21214 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021215
21216 if (eap->read_edit)
21217 len += 7;
21218
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021219 if (eap->force_ff != 0)
21220 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21221# ifdef FEAT_MBYTE
21222 if (eap->force_enc != 0)
21223 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021224 if (eap->bad_char != 0)
21225 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021226# endif
21227
21228 newval = alloc(len + 1);
21229 if (newval == NULL)
21230 return NULL;
21231
21232 if (eap->force_bin == FORCE_BIN)
21233 sprintf((char *)newval, " ++bin");
21234 else if (eap->force_bin == FORCE_NOBIN)
21235 sprintf((char *)newval, " ++nobin");
21236 else
21237 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021238
21239 if (eap->read_edit)
21240 STRCAT(newval, " ++edit");
21241
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021242 if (eap->force_ff != 0)
21243 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21244 eap->cmd + eap->force_ff);
21245# ifdef FEAT_MBYTE
21246 if (eap->force_enc != 0)
21247 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21248 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021249 if (eap->bad_char == BAD_KEEP)
21250 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21251 else if (eap->bad_char == BAD_DROP)
21252 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21253 else if (eap->bad_char != 0)
21254 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021255# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021256 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021257 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258}
21259#endif
21260
21261/*
21262 * Get the value of internal variable "name".
21263 * Return OK or FAIL.
21264 */
21265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021266get_var_tv(
21267 char_u *name,
21268 int len, /* length of "name" */
21269 typval_T *rettv, /* NULL when only checking existence */
21270 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21271 int verbose, /* may give error message */
21272 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273{
21274 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021275 typval_T *tv = NULL;
21276 typval_T atv;
21277 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279
21280 /* truncate the name, so that we can use strcmp() */
21281 cc = name[len];
21282 name[len] = NUL;
21283
21284 /*
21285 * Check for "b:changedtick".
21286 */
21287 if (STRCMP(name, "b:changedtick") == 0)
21288 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021289 atv.v_type = VAR_NUMBER;
21290 atv.vval.v_number = curbuf->b_changedtick;
21291 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 }
21293
21294 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 * Check for user-defined variables.
21296 */
21297 else
21298 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021299 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021301 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021302 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021303 if (dip != NULL)
21304 *dip = v;
21305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 }
21307
Bram Moolenaare9a41262005-01-15 22:18:47 +000021308 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021310 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021311 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 ret = FAIL;
21313 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021314 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021315 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316
21317 name[len] = cc;
21318
21319 return ret;
21320}
21321
21322/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021323 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21324 * Also handle function call with Funcref variable: func(expr)
21325 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21326 */
21327 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021328handle_subscript(
21329 char_u **arg,
21330 typval_T *rettv,
21331 int evaluate, /* do more than finding the end */
21332 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021333{
21334 int ret = OK;
21335 dict_T *selfdict = NULL;
21336 char_u *s;
21337 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021338 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021339
21340 while (ret == OK
21341 && (**arg == '['
21342 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021343 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021344 && !vim_iswhite(*(*arg - 1)))
21345 {
21346 if (**arg == '(')
21347 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021348 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021349 if (evaluate)
21350 {
21351 functv = *rettv;
21352 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021353
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021354 /* Invoke the function. Recursive! */
21355 s = functv.vval.v_string;
21356 }
21357 else
21358 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021359 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021360 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21361 &len, evaluate, selfdict);
21362
21363 /* Clear the funcref afterwards, so that deleting it while
21364 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021365 if (evaluate)
21366 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021367
21368 /* Stop the expression evaluation when immediately aborting on
21369 * error, or when an interrupt occurred or an exception was thrown
21370 * but not caught. */
21371 if (aborting())
21372 {
21373 if (ret == OK)
21374 clear_tv(rettv);
21375 ret = FAIL;
21376 }
21377 dict_unref(selfdict);
21378 selfdict = NULL;
21379 }
21380 else /* **arg == '[' || **arg == '.' */
21381 {
21382 dict_unref(selfdict);
21383 if (rettv->v_type == VAR_DICT)
21384 {
21385 selfdict = rettv->vval.v_dict;
21386 if (selfdict != NULL)
21387 ++selfdict->dv_refcount;
21388 }
21389 else
21390 selfdict = NULL;
21391 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21392 {
21393 clear_tv(rettv);
21394 ret = FAIL;
21395 }
21396 }
21397 }
21398 dict_unref(selfdict);
21399 return ret;
21400}
21401
21402/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021403 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021404 * value).
21405 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021406 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021407alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021408{
Bram Moolenaar33570922005-01-25 22:26:29 +000021409 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021410}
21411
21412/*
21413 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 * The string "s" must have been allocated, it is consumed.
21415 * Return NULL for out of memory, the variable otherwise.
21416 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021417 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021418alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419{
Bram Moolenaar33570922005-01-25 22:26:29 +000021420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021422 rettv = alloc_tv();
21423 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021425 rettv->v_type = VAR_STRING;
21426 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 }
21428 else
21429 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021430 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431}
21432
21433/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021434 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021437free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438{
21439 if (varp != NULL)
21440 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021441 switch (varp->v_type)
21442 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021443 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021444 func_unref(varp->vval.v_string);
21445 /*FALLTHROUGH*/
21446 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021447 vim_free(varp->vval.v_string);
21448 break;
21449 case VAR_LIST:
21450 list_unref(varp->vval.v_list);
21451 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021452 case VAR_DICT:
21453 dict_unref(varp->vval.v_dict);
21454 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021455 case VAR_JOB:
21456#ifdef FEAT_JOB
21457 job_unref(varp->vval.v_job);
21458 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021459#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021460 case VAR_CHANNEL:
21461#ifdef FEAT_CHANNEL
21462 channel_unref(varp->vval.v_channel);
21463 break;
21464#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021465 case VAR_NUMBER:
21466 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021467 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021468 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021469 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 vim_free(varp);
21472 }
21473}
21474
21475/*
21476 * Free the memory for a variable value and set the value to NULL or 0.
21477 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021479clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480{
21481 if (varp != NULL)
21482 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021483 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021485 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021486 func_unref(varp->vval.v_string);
21487 /*FALLTHROUGH*/
21488 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021489 vim_free(varp->vval.v_string);
21490 varp->vval.v_string = NULL;
21491 break;
21492 case VAR_LIST:
21493 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021494 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021495 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021496 case VAR_DICT:
21497 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021498 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021499 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021500 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021501 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021502 varp->vval.v_number = 0;
21503 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021504 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021505#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021506 varp->vval.v_float = 0.0;
21507 break;
21508#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021509 case VAR_JOB:
21510#ifdef FEAT_JOB
21511 job_unref(varp->vval.v_job);
21512 varp->vval.v_job = NULL;
21513#endif
21514 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021515 case VAR_CHANNEL:
21516#ifdef FEAT_CHANNEL
21517 channel_unref(varp->vval.v_channel);
21518 varp->vval.v_channel = NULL;
21519#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021520 case VAR_UNKNOWN:
21521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021523 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 }
21525}
21526
21527/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021528 * Set the value of a variable to NULL without freeing items.
21529 */
21530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021531init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021532{
21533 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021534 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021535}
21536
21537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 * Get the number value of a variable.
21539 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021540 * For incompatible types, return 0.
21541 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21542 * caller of incompatible types: it sets *denote to TRUE if "denote"
21543 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 */
21545 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021546get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021548 int error = FALSE;
21549
21550 return get_tv_number_chk(varp, &error); /* return 0L on error */
21551}
21552
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021553 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021554get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021555{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021556 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021558 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021560 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021561 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021562 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021563#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021564 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021565 break;
21566#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021567 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021568 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021569 break;
21570 case VAR_STRING:
21571 if (varp->vval.v_string != NULL)
21572 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021573 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021574 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021575 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021576 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021577 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021578 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021579 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021580 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021581 case VAR_SPECIAL:
21582 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21583 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021584 case VAR_JOB:
21585#ifdef FEAT_JOB
21586 EMSG(_("E910: Using a Job as a Number"));
21587 break;
21588#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021589 case VAR_CHANNEL:
21590#ifdef FEAT_CHANNEL
21591 EMSG(_("E913: Using a Channel as a Number"));
21592 break;
21593#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021594 case VAR_UNKNOWN:
21595 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021596 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021598 if (denote == NULL) /* useful for values that must be unsigned */
21599 n = -1;
21600 else
21601 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021602 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603}
21604
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021605#ifdef FEAT_FLOAT
21606 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021607get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021608{
21609 switch (varp->v_type)
21610 {
21611 case VAR_NUMBER:
21612 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021613 case VAR_FLOAT:
21614 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021615 case VAR_FUNC:
21616 EMSG(_("E891: Using a Funcref as a Float"));
21617 break;
21618 case VAR_STRING:
21619 EMSG(_("E892: Using a String as a Float"));
21620 break;
21621 case VAR_LIST:
21622 EMSG(_("E893: Using a List as a Float"));
21623 break;
21624 case VAR_DICT:
21625 EMSG(_("E894: Using a Dictionary as a Float"));
21626 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021627 case VAR_SPECIAL:
21628 EMSG(_("E907: Using a special value as a Float"));
21629 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021630 case VAR_JOB:
21631# ifdef FEAT_JOB
21632 EMSG(_("E911: Using a Job as a Float"));
21633 break;
21634# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021635 case VAR_CHANNEL:
21636# ifdef FEAT_CHANNEL
21637 EMSG(_("E914: Using a Channel as a Float"));
21638 break;
21639# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021640 case VAR_UNKNOWN:
21641 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021642 break;
21643 }
21644 return 0;
21645}
21646#endif
21647
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021649 * Get the lnum from the first argument.
21650 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021651 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 */
21653 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021654get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655{
Bram Moolenaar33570922005-01-25 22:26:29 +000021656 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 linenr_T lnum;
21658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021659 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 if (lnum == 0) /* no valid number, try using line() */
21661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021662 rettv.v_type = VAR_NUMBER;
21663 f_line(argvars, &rettv);
21664 lnum = rettv.vval.v_number;
21665 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 }
21667 return lnum;
21668}
21669
21670/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021671 * Get the lnum from the first argument.
21672 * Also accepts "$", then "buf" is used.
21673 * Returns 0 on error.
21674 */
21675 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021676get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021677{
21678 if (argvars[0].v_type == VAR_STRING
21679 && argvars[0].vval.v_string != NULL
21680 && argvars[0].vval.v_string[0] == '$'
21681 && buf != NULL)
21682 return buf->b_ml.ml_line_count;
21683 return get_tv_number_chk(&argvars[0], NULL);
21684}
21685
21686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 * Get the string value of a variable.
21688 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021689 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21690 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 * If the String variable has never been set, return an empty string.
21692 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021693 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21694 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 */
21696 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021697get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698{
21699 static char_u mybuf[NUMBUFLEN];
21700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021701 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702}
21703
21704 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021705get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021707 char_u *res = get_tv_string_buf_chk(varp, buf);
21708
21709 return res != NULL ? res : (char_u *)"";
21710}
21711
Bram Moolenaar7d647822014-04-05 21:28:56 +020021712/*
21713 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21714 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021715 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021716get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021717{
21718 static char_u mybuf[NUMBUFLEN];
21719
21720 return get_tv_string_buf_chk(varp, mybuf);
21721}
21722
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021723 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021724get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021725{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021726 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021728 case VAR_NUMBER:
21729 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21730 return buf;
21731 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021732 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021733 break;
21734 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021735 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021736 break;
21737 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021738 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021739 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021740 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021741#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021742 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021743 break;
21744#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021745 case VAR_STRING:
21746 if (varp->vval.v_string != NULL)
21747 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021748 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021749 case VAR_SPECIAL:
21750 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21751 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021752 case VAR_JOB:
21753#ifdef FEAT_JOB
21754 {
21755 job_T *job = varp->vval.v_job;
21756 char *status = job->jv_status == JOB_FAILED ? "fail"
21757 : job->jv_status == JOB_ENDED ? "dead"
21758 : "run";
21759# ifdef UNIX
21760 vim_snprintf((char *)buf, NUMBUFLEN,
21761 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021762# elif defined(WIN32)
21763 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021764 "process %ld %s",
21765 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021766 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021767# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021768 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021769 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21770# endif
21771 return buf;
21772 }
21773#endif
21774 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021775 case VAR_CHANNEL:
21776#ifdef FEAT_CHANNEL
21777 {
21778 channel_T *channel = varp->vval.v_channel;
21779 char *status = channel_status(channel);
21780
21781 vim_snprintf((char *)buf, NUMBUFLEN,
21782 "channel %d %s", channel->ch_id, status);
21783 return buf;
21784 }
21785#endif
21786 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021787 case VAR_UNKNOWN:
21788 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021789 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021791 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792}
21793
21794/*
21795 * Find variable "name" in the list of variables.
21796 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021797 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021798 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021799 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021801 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021802find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021805 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806
Bram Moolenaara7043832005-01-21 11:56:39 +000021807 ht = find_var_ht(name, &varname);
21808 if (htp != NULL)
21809 *htp = ht;
21810 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021812 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813}
21814
21815/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021816 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021817 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021819 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021820find_var_in_ht(
21821 hashtab_T *ht,
21822 int htname,
21823 char_u *varname,
21824 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021825{
Bram Moolenaar33570922005-01-25 22:26:29 +000021826 hashitem_T *hi;
21827
21828 if (*varname == NUL)
21829 {
21830 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021831 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021832 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021833 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021834 case 'g': return &globvars_var;
21835 case 'v': return &vimvars_var;
21836 case 'b': return &curbuf->b_bufvar;
21837 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021838#ifdef FEAT_WINDOWS
21839 case 't': return &curtab->tp_winvar;
21840#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021841 case 'l': return current_funccal == NULL
21842 ? NULL : &current_funccal->l_vars_var;
21843 case 'a': return current_funccal == NULL
21844 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021845 }
21846 return NULL;
21847 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021848
21849 hi = hash_find(ht, varname);
21850 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021851 {
21852 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021853 * worked find the variable again. Don't auto-load a script if it was
21854 * loaded already, otherwise it would be loaded every time when
21855 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021856 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021857 {
21858 /* Note: script_autoload() may make "hi" invalid. It must either
21859 * be obtained again or not used. */
21860 if (!script_autoload(varname, FALSE) || aborting())
21861 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021862 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021863 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021864 if (HASHITEM_EMPTY(hi))
21865 return NULL;
21866 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021867 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021868}
21869
21870/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021871 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021872 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021873 * Set "varname" to the start of name without ':'.
21874 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021875 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021876find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021878 hashitem_T *hi;
21879
Bram Moolenaar73627d02015-08-11 15:46:09 +020021880 if (name[0] == NUL)
21881 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 if (name[1] != ':')
21883 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021884 /* The name must not start with a colon or #. */
21885 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 return NULL;
21887 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021888
21889 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021890 hi = hash_find(&compat_hashtab, name);
21891 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021892 return &compat_hashtab;
21893
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021895 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021896 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 }
21898 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021899 if (*name == 'g') /* global variable */
21900 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021901 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21902 */
21903 if (vim_strchr(name + 2, ':') != NULL
21904 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021905 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021907 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021909 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021910#ifdef FEAT_WINDOWS
21911 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021912 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021913#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021914 if (*name == 'v') /* v: variable */
21915 return &vimvarht;
21916 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021917 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021918 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021919 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 if (*name == 's' /* script variable */
21921 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21922 return &SCRIPT_VARS(current_SID);
21923 return NULL;
21924}
21925
21926/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021927 * Get function call environment based on bactrace debug level
21928 */
21929 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021930get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021931{
21932 int i;
21933 funccall_T *funccal;
21934 funccall_T *temp_funccal;
21935
21936 funccal = current_funccal;
21937 if (debug_backtrace_level > 0)
21938 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021939 for (i = 0; i < debug_backtrace_level; i++)
21940 {
21941 temp_funccal = funccal->caller;
21942 if (temp_funccal)
21943 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021944 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021945 /* backtrace level overflow. reset to max */
21946 debug_backtrace_level = i;
21947 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021948 }
21949 return funccal;
21950}
21951
21952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021954 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 * Returns NULL when it doesn't exist.
21956 */
21957 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021958get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959{
Bram Moolenaar33570922005-01-25 22:26:29 +000021960 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021962 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963 if (v == NULL)
21964 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021965 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966}
21967
21968/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021969 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 * sourcing this script and when executing functions defined in the script.
21971 */
21972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021973new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974{
Bram Moolenaara7043832005-01-21 11:56:39 +000021975 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021976 hashtab_T *ht;
21977 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021978
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21980 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021981 /* Re-allocating ga_data means that an ht_array pointing to
21982 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021983 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021984 for (i = 1; i <= ga_scripts.ga_len; ++i)
21985 {
21986 ht = &SCRIPT_VARS(i);
21987 if (ht->ht_mask == HT_INIT_SIZE - 1)
21988 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021989 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021991 }
21992
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 while (ga_scripts.ga_len < id)
21994 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021995 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021996 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021997 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 }
22000 }
22001}
22002
22003/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022004 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22005 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006 */
22007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022008init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009{
Bram Moolenaar33570922005-01-25 22:26:29 +000022010 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022011 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022012 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022013 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022014 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022015 dict_var->di_tv.vval.v_dict = dict;
22016 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022017 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022018 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22019 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020}
22021
22022/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022023 * Unreference a dictionary initialized by init_var_dict().
22024 */
22025 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022026unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022027{
22028 /* Now the dict needs to be freed if no one else is using it, go back to
22029 * normal reference counting. */
22030 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22031 dict_unref(dict);
22032}
22033
22034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022036 * Frees all allocated variables and the value they contain.
22037 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 */
22039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022040vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022041{
22042 vars_clear_ext(ht, TRUE);
22043}
22044
22045/*
22046 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22047 */
22048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022049vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050{
Bram Moolenaara7043832005-01-21 11:56:39 +000022051 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022052 hashitem_T *hi;
22053 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054
Bram Moolenaar33570922005-01-25 22:26:29 +000022055 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022056 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022057 for (hi = ht->ht_array; todo > 0; ++hi)
22058 {
22059 if (!HASHITEM_EMPTY(hi))
22060 {
22061 --todo;
22062
Bram Moolenaar33570922005-01-25 22:26:29 +000022063 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022064 * ht_array might change then. hash_clear() takes care of it
22065 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022066 v = HI2DI(hi);
22067 if (free_val)
22068 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022069 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022070 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022071 }
22072 }
22073 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022074 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075}
22076
Bram Moolenaara7043832005-01-21 11:56:39 +000022077/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022078 * Delete a variable from hashtab "ht" at item "hi".
22079 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022082delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083{
Bram Moolenaar33570922005-01-25 22:26:29 +000022084 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022085
22086 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022087 clear_tv(&di->di_tv);
22088 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089}
22090
22091/*
22092 * List the value of one internal variable.
22093 */
22094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022095list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022097 char_u *tofree;
22098 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022099 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022100
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022101 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022102 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022103 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022104 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105}
22106
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022108list_one_var_a(
22109 char_u *prefix,
22110 char_u *name,
22111 int type,
22112 char_u *string,
22113 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114{
Bram Moolenaar31859182007-08-14 20:41:13 +000022115 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22116 msg_start();
22117 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 if (name != NULL) /* "a:" vars don't have a name stored */
22119 msg_puts(name);
22120 msg_putchar(' ');
22121 msg_advance(22);
22122 if (type == VAR_NUMBER)
22123 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022124 else if (type == VAR_FUNC)
22125 msg_putchar('*');
22126 else if (type == VAR_LIST)
22127 {
22128 msg_putchar('[');
22129 if (*string == '[')
22130 ++string;
22131 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022132 else if (type == VAR_DICT)
22133 {
22134 msg_putchar('{');
22135 if (*string == '{')
22136 ++string;
22137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138 else
22139 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022140
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022142
22143 if (type == VAR_FUNC)
22144 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022145 if (*first)
22146 {
22147 msg_clr_eos();
22148 *first = FALSE;
22149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150}
22151
22152/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022153 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 * If the variable already exists, the value is updated.
22155 * Otherwise the variable is created.
22156 */
22157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022158set_var(
22159 char_u *name,
22160 typval_T *tv,
22161 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162{
Bram Moolenaar33570922005-01-25 22:26:29 +000022163 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022165 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022167 ht = find_var_ht(name, &varname);
22168 if (ht == NULL || *varname == NUL)
22169 {
22170 EMSG2(_(e_illvar), name);
22171 return;
22172 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022173 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022174
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022175 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22176 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022177
Bram Moolenaar33570922005-01-25 22:26:29 +000022178 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022180 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022181 if (var_check_ro(v->di_flags, name, FALSE)
22182 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022183 return;
22184 if (v->di_tv.v_type != tv->v_type
22185 && !((v->di_tv.v_type == VAR_STRING
22186 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022187 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022188 || tv->v_type == VAR_NUMBER))
22189#ifdef FEAT_FLOAT
22190 && !((v->di_tv.v_type == VAR_NUMBER
22191 || v->di_tv.v_type == VAR_FLOAT)
22192 && (tv->v_type == VAR_NUMBER
22193 || tv->v_type == VAR_FLOAT))
22194#endif
22195 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022196 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022197 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022198 return;
22199 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022200
22201 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022202 * Handle setting internal v: variables separately where needed to
22203 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022204 */
22205 if (ht == &vimvarht)
22206 {
22207 if (v->di_tv.v_type == VAR_STRING)
22208 {
22209 vim_free(v->di_tv.vval.v_string);
22210 if (copy || tv->v_type != VAR_STRING)
22211 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22212 else
22213 {
22214 /* Take over the string to avoid an extra alloc/free. */
22215 v->di_tv.vval.v_string = tv->vval.v_string;
22216 tv->vval.v_string = NULL;
22217 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022218 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022219 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022220 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022221 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022222 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022223 if (STRCMP(varname, "searchforward") == 0)
22224 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022225#ifdef FEAT_SEARCH_EXTRA
22226 else if (STRCMP(varname, "hlsearch") == 0)
22227 {
22228 no_hlsearch = !v->di_tv.vval.v_number;
22229 redraw_all_later(SOME_VALID);
22230 }
22231#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022232 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022233 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022234 else if (v->di_tv.v_type != tv->v_type)
22235 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022236 }
22237
22238 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 }
22240 else /* add a new variable */
22241 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022242 /* Can't add "v:" variable. */
22243 if (ht == &vimvarht)
22244 {
22245 EMSG2(_(e_illvar), name);
22246 return;
22247 }
22248
Bram Moolenaar92124a32005-06-17 22:03:40 +000022249 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022250 if (!valid_varname(varname))
22251 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022252
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022253 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22254 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022255 if (v == NULL)
22256 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022257 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022258 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022260 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261 return;
22262 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022263 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022265
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022266 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022267 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022268 else
22269 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022270 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022271 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022272 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274}
22275
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022276/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022277 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022278 * Also give an error message.
22279 */
22280 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022281var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022282{
22283 if (flags & DI_FLAGS_RO)
22284 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022285 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022286 return TRUE;
22287 }
22288 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22289 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022290 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022291 return TRUE;
22292 }
22293 return FALSE;
22294}
22295
22296/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022297 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22298 * Also give an error message.
22299 */
22300 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022301var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022302{
22303 if (flags & DI_FLAGS_FIX)
22304 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022305 EMSG2(_("E795: Cannot delete variable %s"),
22306 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022307 return TRUE;
22308 }
22309 return FALSE;
22310}
22311
22312/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022313 * Check if a funcref is assigned to a valid variable name.
22314 * Return TRUE and give an error if not.
22315 */
22316 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022317var_check_func_name(
22318 char_u *name, /* points to start of variable name */
22319 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022320{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022321 /* Allow for w: b: s: and t:. */
22322 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022323 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22324 ? name[2] : name[0]))
22325 {
22326 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22327 name);
22328 return TRUE;
22329 }
22330 /* Don't allow hiding a function. When "v" is not NULL we might be
22331 * assigning another function to the same var, the type is checked
22332 * below. */
22333 if (new_var && function_exists(name))
22334 {
22335 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22336 name);
22337 return TRUE;
22338 }
22339 return FALSE;
22340}
22341
22342/*
22343 * Check if a variable name is valid.
22344 * Return FALSE and give an error if not.
22345 */
22346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022347valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022348{
22349 char_u *p;
22350
22351 for (p = varname; *p != NUL; ++p)
22352 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22353 && *p != AUTOLOAD_CHAR)
22354 {
22355 EMSG2(_(e_illvar), varname);
22356 return FALSE;
22357 }
22358 return TRUE;
22359}
22360
22361/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022362 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022363 * Also give an error message, using "name" or _("name") when use_gettext is
22364 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022365 */
22366 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022367tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022368{
22369 if (lock & VAR_LOCKED)
22370 {
22371 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022372 name == NULL ? (char_u *)_("Unknown")
22373 : use_gettext ? (char_u *)_(name)
22374 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022375 return TRUE;
22376 }
22377 if (lock & VAR_FIXED)
22378 {
22379 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022380 name == NULL ? (char_u *)_("Unknown")
22381 : use_gettext ? (char_u *)_(name)
22382 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022383 return TRUE;
22384 }
22385 return FALSE;
22386}
22387
22388/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022389 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022390 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022391 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022392 * It is OK for "from" and "to" to point to the same item. This is used to
22393 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022394 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022395 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022396copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022398 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022399 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022400 switch (from->v_type)
22401 {
22402 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022403 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022404 to->vval.v_number = from->vval.v_number;
22405 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022406 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022407#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022408 to->vval.v_float = from->vval.v_float;
22409 break;
22410#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022411 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022412#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022413 to->vval.v_job = from->vval.v_job;
22414 ++to->vval.v_job->jv_refcount;
22415 break;
22416#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022417 case VAR_CHANNEL:
22418#ifdef FEAT_CHANNEL
22419 to->vval.v_channel = from->vval.v_channel;
22420 ++to->vval.v_channel->ch_refcount;
22421 break;
22422#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022423 case VAR_STRING:
22424 case VAR_FUNC:
22425 if (from->vval.v_string == NULL)
22426 to->vval.v_string = NULL;
22427 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022428 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022429 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022430 if (from->v_type == VAR_FUNC)
22431 func_ref(to->vval.v_string);
22432 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022433 break;
22434 case VAR_LIST:
22435 if (from->vval.v_list == NULL)
22436 to->vval.v_list = NULL;
22437 else
22438 {
22439 to->vval.v_list = from->vval.v_list;
22440 ++to->vval.v_list->lv_refcount;
22441 }
22442 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022443 case VAR_DICT:
22444 if (from->vval.v_dict == NULL)
22445 to->vval.v_dict = NULL;
22446 else
22447 {
22448 to->vval.v_dict = from->vval.v_dict;
22449 ++to->vval.v_dict->dv_refcount;
22450 }
22451 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022452 case VAR_UNKNOWN:
22453 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022454 break;
22455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456}
22457
22458/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022459 * Make a copy of an item.
22460 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022461 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22462 * reference to an already copied list/dict can be used.
22463 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022464 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022465 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022466item_copy(
22467 typval_T *from,
22468 typval_T *to,
22469 int deep,
22470 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022471{
22472 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022473 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022474
Bram Moolenaar33570922005-01-25 22:26:29 +000022475 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022476 {
22477 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022478 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022479 }
22480 ++recurse;
22481
22482 switch (from->v_type)
22483 {
22484 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022485 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022486 case VAR_STRING:
22487 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022488 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022489 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022490 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022491 copy_tv(from, to);
22492 break;
22493 case VAR_LIST:
22494 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022495 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022496 if (from->vval.v_list == NULL)
22497 to->vval.v_list = NULL;
22498 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22499 {
22500 /* use the copy made earlier */
22501 to->vval.v_list = from->vval.v_list->lv_copylist;
22502 ++to->vval.v_list->lv_refcount;
22503 }
22504 else
22505 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22506 if (to->vval.v_list == NULL)
22507 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022508 break;
22509 case VAR_DICT:
22510 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022511 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022512 if (from->vval.v_dict == NULL)
22513 to->vval.v_dict = NULL;
22514 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22515 {
22516 /* use the copy made earlier */
22517 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22518 ++to->vval.v_dict->dv_refcount;
22519 }
22520 else
22521 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22522 if (to->vval.v_dict == NULL)
22523 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022524 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022525 case VAR_UNKNOWN:
22526 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022527 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022528 }
22529 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022530 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022531}
22532
22533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 * ":echo expr1 ..." print each argument separated with a space, add a
22535 * newline at the end.
22536 * ":echon expr1 ..." print each argument plain.
22537 */
22538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022539ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540{
22541 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022542 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022543 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 char_u *p;
22545 int needclr = TRUE;
22546 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022547 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548
22549 if (eap->skip)
22550 ++emsg_skip;
22551 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22552 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022553 /* If eval1() causes an error message the text from the command may
22554 * still need to be cleared. E.g., "echo 22,44". */
22555 need_clr_eos = needclr;
22556
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022558 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 {
22560 /*
22561 * Report the invalid expression unless the expression evaluation
22562 * has been cancelled due to an aborting error, an interrupt, or an
22563 * exception.
22564 */
22565 if (!aborting())
22566 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022567 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 break;
22569 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022570 need_clr_eos = FALSE;
22571
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 if (!eap->skip)
22573 {
22574 if (atstart)
22575 {
22576 atstart = FALSE;
22577 /* Call msg_start() after eval1(), evaluating the expression
22578 * may cause a message to appear. */
22579 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022580 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022581 /* Mark the saved text as finishing the line, so that what
22582 * follows is displayed on a new line when scrolling back
22583 * at the more prompt. */
22584 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587 }
22588 else if (eap->cmdidx == CMD_echo)
22589 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022590 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022591 if (p != NULL)
22592 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022594 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022596 if (*p != TAB && needclr)
22597 {
22598 /* remove any text still there from the command */
22599 msg_clr_eos();
22600 needclr = FALSE;
22601 }
22602 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 }
22604 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022605 {
22606#ifdef FEAT_MBYTE
22607 if (has_mbyte)
22608 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022609 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022610
22611 (void)msg_outtrans_len_attr(p, i, echo_attr);
22612 p += i - 1;
22613 }
22614 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022616 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022619 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022621 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 arg = skipwhite(arg);
22623 }
22624 eap->nextcmd = check_nextcmd(arg);
22625
22626 if (eap->skip)
22627 --emsg_skip;
22628 else
22629 {
22630 /* remove text that may still be there from the command */
22631 if (needclr)
22632 msg_clr_eos();
22633 if (eap->cmdidx == CMD_echo)
22634 msg_end();
22635 }
22636}
22637
22638/*
22639 * ":echohl {name}".
22640 */
22641 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022642ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643{
22644 int id;
22645
22646 id = syn_name2id(eap->arg);
22647 if (id == 0)
22648 echo_attr = 0;
22649 else
22650 echo_attr = syn_id2attr(id);
22651}
22652
22653/*
22654 * ":execute expr1 ..." execute the result of an expression.
22655 * ":echomsg expr1 ..." Print a message
22656 * ":echoerr expr1 ..." Print an error
22657 * Each gets spaces around each argument and a newline at the end for
22658 * echo commands
22659 */
22660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022661ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662{
22663 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022664 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 int ret = OK;
22666 char_u *p;
22667 garray_T ga;
22668 int len;
22669 int save_did_emsg;
22670
22671 ga_init2(&ga, 1, 80);
22672
22673 if (eap->skip)
22674 ++emsg_skip;
22675 while (*arg != NUL && *arg != '|' && *arg != '\n')
22676 {
22677 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022678 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679 {
22680 /*
22681 * Report the invalid expression unless the expression evaluation
22682 * has been cancelled due to an aborting error, an interrupt, or an
22683 * exception.
22684 */
22685 if (!aborting())
22686 EMSG2(_(e_invexpr2), p);
22687 ret = FAIL;
22688 break;
22689 }
22690
22691 if (!eap->skip)
22692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022693 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 len = (int)STRLEN(p);
22695 if (ga_grow(&ga, len + 2) == FAIL)
22696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022697 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 ret = FAIL;
22699 break;
22700 }
22701 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 ga.ga_len += len;
22705 }
22706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022707 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 arg = skipwhite(arg);
22709 }
22710
22711 if (ret != FAIL && ga.ga_data != NULL)
22712 {
22713 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022716 out_flush();
22717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 else if (eap->cmdidx == CMD_echoerr)
22719 {
22720 /* We don't want to abort following commands, restore did_emsg. */
22721 save_did_emsg = did_emsg;
22722 EMSG((char_u *)ga.ga_data);
22723 if (!force_abort)
22724 did_emsg = save_did_emsg;
22725 }
22726 else if (eap->cmdidx == CMD_execute)
22727 do_cmdline((char_u *)ga.ga_data,
22728 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22729 }
22730
22731 ga_clear(&ga);
22732
22733 if (eap->skip)
22734 --emsg_skip;
22735
22736 eap->nextcmd = check_nextcmd(arg);
22737}
22738
22739/*
22740 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22741 * "arg" points to the "&" or '+' when called, to "option" when returning.
22742 * Returns NULL when no option name found. Otherwise pointer to the char
22743 * after the option name.
22744 */
22745 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022746find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747{
22748 char_u *p = *arg;
22749
22750 ++p;
22751 if (*p == 'g' && p[1] == ':')
22752 {
22753 *opt_flags = OPT_GLOBAL;
22754 p += 2;
22755 }
22756 else if (*p == 'l' && p[1] == ':')
22757 {
22758 *opt_flags = OPT_LOCAL;
22759 p += 2;
22760 }
22761 else
22762 *opt_flags = 0;
22763
22764 if (!ASCII_ISALPHA(*p))
22765 return NULL;
22766 *arg = p;
22767
22768 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22769 p += 4; /* termcap option */
22770 else
22771 while (ASCII_ISALPHA(*p))
22772 ++p;
22773 return p;
22774}
22775
22776/*
22777 * ":function"
22778 */
22779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022780ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781{
22782 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022783 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784 int j;
22785 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022787 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 char_u *name = NULL;
22789 char_u *p;
22790 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022791 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 garray_T newargs;
22793 garray_T newlines;
22794 int varargs = FALSE;
22795 int mustend = FALSE;
22796 int flags = 0;
22797 ufunc_T *fp;
22798 int indent;
22799 int nesting;
22800 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022801 dictitem_T *v;
22802 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022803 static int func_nr = 0; /* number for nameless function */
22804 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022805 hashtab_T *ht;
22806 int todo;
22807 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022808 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809
22810 /*
22811 * ":function" without argument: list functions.
22812 */
22813 if (ends_excmd(*eap->arg))
22814 {
22815 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022816 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022817 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022818 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022819 {
22820 if (!HASHITEM_EMPTY(hi))
22821 {
22822 --todo;
22823 fp = HI2UF(hi);
22824 if (!isdigit(*fp->uf_name))
22825 list_func_head(fp, FALSE);
22826 }
22827 }
22828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829 eap->nextcmd = check_nextcmd(eap->arg);
22830 return;
22831 }
22832
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022833 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022834 * ":function /pat": list functions matching pattern.
22835 */
22836 if (*eap->arg == '/')
22837 {
22838 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22839 if (!eap->skip)
22840 {
22841 regmatch_T regmatch;
22842
22843 c = *p;
22844 *p = NUL;
22845 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22846 *p = c;
22847 if (regmatch.regprog != NULL)
22848 {
22849 regmatch.rm_ic = p_ic;
22850
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022851 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022852 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22853 {
22854 if (!HASHITEM_EMPTY(hi))
22855 {
22856 --todo;
22857 fp = HI2UF(hi);
22858 if (!isdigit(*fp->uf_name)
22859 && vim_regexec(&regmatch, fp->uf_name, 0))
22860 list_func_head(fp, FALSE);
22861 }
22862 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022863 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022864 }
22865 }
22866 if (*p == '/')
22867 ++p;
22868 eap->nextcmd = check_nextcmd(p);
22869 return;
22870 }
22871
22872 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022873 * Get the function name. There are these situations:
22874 * func normal function name
22875 * "name" == func, "fudi.fd_dict" == NULL
22876 * dict.func new dictionary entry
22877 * "name" == NULL, "fudi.fd_dict" set,
22878 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22879 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022880 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022881 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22882 * dict.func existing dict entry that's not a Funcref
22883 * "name" == NULL, "fudi.fd_dict" set,
22884 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022885 * s:func script-local function name
22886 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022889 name = trans_function_name(&p, eap->skip, 0, &fudi);
22890 paren = (vim_strchr(p, '(') != NULL);
22891 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 {
22893 /*
22894 * Return on an invalid expression in braces, unless the expression
22895 * evaluation has been cancelled due to an aborting error, an
22896 * interrupt, or an exception.
22897 */
22898 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022899 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022900 if (!eap->skip && fudi.fd_newkey != NULL)
22901 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022902 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 else
22906 eap->skip = TRUE;
22907 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022908
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 /* An error in a function call during evaluation of an expression in magic
22910 * braces should not cause the function not to be defined. */
22911 saved_did_emsg = did_emsg;
22912 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913
22914 /*
22915 * ":function func" with only function name: list function.
22916 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022917 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 {
22919 if (!ends_excmd(*skipwhite(p)))
22920 {
22921 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022922 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923 }
22924 eap->nextcmd = check_nextcmd(p);
22925 if (eap->nextcmd != NULL)
22926 *p = NUL;
22927 if (!eap->skip && !got_int)
22928 {
22929 fp = find_func(name);
22930 if (fp != NULL)
22931 {
22932 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022933 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022935 if (FUNCLINE(fp, j) == NULL)
22936 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 msg_putchar('\n');
22938 msg_outnum((long)(j + 1));
22939 if (j < 9)
22940 msg_putchar(' ');
22941 if (j < 99)
22942 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022943 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944 out_flush(); /* show a line at a time */
22945 ui_breakcheck();
22946 }
22947 if (!got_int)
22948 {
22949 msg_putchar('\n');
22950 msg_puts((char_u *)" endfunction");
22951 }
22952 }
22953 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022954 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022956 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 }
22958
22959 /*
22960 * ":function name(arg1, arg2)" Define function.
22961 */
22962 p = skipwhite(p);
22963 if (*p != '(')
22964 {
22965 if (!eap->skip)
22966 {
22967 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022968 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 }
22970 /* attempt to continue by skipping some text */
22971 if (vim_strchr(p, '(') != NULL)
22972 p = vim_strchr(p, '(');
22973 }
22974 p = skipwhite(p + 1);
22975
22976 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22977 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22978
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022979 if (!eap->skip)
22980 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022981 /* Check the name of the function. Unless it's a dictionary function
22982 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022983 if (name != NULL)
22984 arg = name;
22985 else
22986 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022987 if (arg != NULL && (fudi.fd_di == NULL
22988 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022989 {
22990 if (*arg == K_SPECIAL)
22991 j = 3;
22992 else
22993 j = 0;
22994 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22995 : eval_isnamec(arg[j])))
22996 ++j;
22997 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022998 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022999 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023000 /* Disallow using the g: dict. */
23001 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23002 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023003 }
23004
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 /*
23006 * Isolate the arguments: "arg1, arg2, ...)"
23007 */
23008 while (*p != ')')
23009 {
23010 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23011 {
23012 varargs = TRUE;
23013 p += 3;
23014 mustend = TRUE;
23015 }
23016 else
23017 {
23018 arg = p;
23019 while (ASCII_ISALNUM(*p) || *p == '_')
23020 ++p;
23021 if (arg == p || isdigit(*arg)
23022 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23023 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23024 {
23025 if (!eap->skip)
23026 EMSG2(_("E125: Illegal argument: %s"), arg);
23027 break;
23028 }
23029 if (ga_grow(&newargs, 1) == FAIL)
23030 goto erret;
23031 c = *p;
23032 *p = NUL;
23033 arg = vim_strsave(arg);
23034 if (arg == NULL)
23035 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023036
23037 /* Check for duplicate argument name. */
23038 for (i = 0; i < newargs.ga_len; ++i)
23039 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23040 {
23041 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023042 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023043 goto erret;
23044 }
23045
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23047 *p = c;
23048 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049 if (*p == ',')
23050 ++p;
23051 else
23052 mustend = TRUE;
23053 }
23054 p = skipwhite(p);
23055 if (mustend && *p != ')')
23056 {
23057 if (!eap->skip)
23058 EMSG2(_(e_invarg2), eap->arg);
23059 break;
23060 }
23061 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023062 if (*p != ')')
23063 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064 ++p; /* skip the ')' */
23065
Bram Moolenaare9a41262005-01-15 22:18:47 +000023066 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 for (;;)
23068 {
23069 p = skipwhite(p);
23070 if (STRNCMP(p, "range", 5) == 0)
23071 {
23072 flags |= FC_RANGE;
23073 p += 5;
23074 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023075 else if (STRNCMP(p, "dict", 4) == 0)
23076 {
23077 flags |= FC_DICT;
23078 p += 4;
23079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080 else if (STRNCMP(p, "abort", 5) == 0)
23081 {
23082 flags |= FC_ABORT;
23083 p += 5;
23084 }
23085 else
23086 break;
23087 }
23088
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023089 /* When there is a line break use what follows for the function body.
23090 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23091 if (*p == '\n')
23092 line_arg = p + 1;
23093 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 EMSG(_(e_trailing));
23095
23096 /*
23097 * Read the body of the function, until ":endfunction" is found.
23098 */
23099 if (KeyTyped)
23100 {
23101 /* Check if the function already exists, don't let the user type the
23102 * whole function before telling him it doesn't work! For a script we
23103 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023104 if (!eap->skip && !eap->forceit)
23105 {
23106 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23107 EMSG(_(e_funcdict));
23108 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023109 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023112 if (!eap->skip && did_emsg)
23113 goto erret;
23114
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 msg_putchar('\n'); /* don't overwrite the function name */
23116 cmdline_row = msg_row;
23117 }
23118
23119 indent = 2;
23120 nesting = 0;
23121 for (;;)
23122 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023123 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023124 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023125 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023126 saved_wait_return = FALSE;
23127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023129 sourcing_lnum_off = sourcing_lnum;
23130
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023131 if (line_arg != NULL)
23132 {
23133 /* Use eap->arg, split up in parts by line breaks. */
23134 theline = line_arg;
23135 p = vim_strchr(theline, '\n');
23136 if (p == NULL)
23137 line_arg += STRLEN(line_arg);
23138 else
23139 {
23140 *p = NUL;
23141 line_arg = p + 1;
23142 }
23143 }
23144 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 theline = getcmdline(':', 0L, indent);
23146 else
23147 theline = eap->getline(':', eap->cookie, indent);
23148 if (KeyTyped)
23149 lines_left = Rows - 1;
23150 if (theline == NULL)
23151 {
23152 EMSG(_("E126: Missing :endfunction"));
23153 goto erret;
23154 }
23155
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023156 /* Detect line continuation: sourcing_lnum increased more than one. */
23157 if (sourcing_lnum > sourcing_lnum_off + 1)
23158 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23159 else
23160 sourcing_lnum_off = 0;
23161
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 if (skip_until != NULL)
23163 {
23164 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23165 * don't check for ":endfunc". */
23166 if (STRCMP(theline, skip_until) == 0)
23167 {
23168 vim_free(skip_until);
23169 skip_until = NULL;
23170 }
23171 }
23172 else
23173 {
23174 /* skip ':' and blanks*/
23175 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23176 ;
23177
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023178 /* Check for "endfunction". */
23179 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023181 if (line_arg == NULL)
23182 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 break;
23184 }
23185
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023186 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 * at "end". */
23188 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23189 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023190 else if (STRNCMP(p, "if", 2) == 0
23191 || STRNCMP(p, "wh", 2) == 0
23192 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 || STRNCMP(p, "try", 3) == 0)
23194 indent += 2;
23195
23196 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023197 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023199 if (*p == '!')
23200 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023202 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23203 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023205 ++nesting;
23206 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 }
23208 }
23209
23210 /* Check for ":append" or ":insert". */
23211 p = skip_range(p, NULL);
23212 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23213 || (p[0] == 'i'
23214 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23215 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23216 skip_until = vim_strsave((char_u *)".");
23217
23218 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23219 arg = skipwhite(skiptowhite(p));
23220 if (arg[0] == '<' && arg[1] =='<'
23221 && ((p[0] == 'p' && p[1] == 'y'
23222 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23223 || (p[0] == 'p' && p[1] == 'e'
23224 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23225 || (p[0] == 't' && p[1] == 'c'
23226 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023227 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23228 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23230 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023231 || (p[0] == 'm' && p[1] == 'z'
23232 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 ))
23234 {
23235 /* ":python <<" continues until a dot, like ":append" */
23236 p = skipwhite(arg + 2);
23237 if (*p == NUL)
23238 skip_until = vim_strsave((char_u *)".");
23239 else
23240 skip_until = vim_strsave(p);
23241 }
23242 }
23243
23244 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023245 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023246 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023247 if (line_arg == NULL)
23248 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023249 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023250 }
23251
23252 /* Copy the line to newly allocated memory. get_one_sourceline()
23253 * allocates 250 bytes per line, this saves 80% on average. The cost
23254 * is an extra alloc/free. */
23255 p = vim_strsave(theline);
23256 if (p != NULL)
23257 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023258 if (line_arg == NULL)
23259 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023260 theline = p;
23261 }
23262
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023263 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23264
23265 /* Add NULL lines for continuation lines, so that the line count is
23266 * equal to the index in the growarray. */
23267 while (sourcing_lnum_off-- > 0)
23268 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023269
23270 /* Check for end of eap->arg. */
23271 if (line_arg != NULL && *line_arg == NUL)
23272 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 }
23274
23275 /* Don't define the function when skipping commands or when an error was
23276 * detected. */
23277 if (eap->skip || did_emsg)
23278 goto erret;
23279
23280 /*
23281 * If there are no errors, add the function
23282 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023283 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023284 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023285 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023286 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023287 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023288 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023289 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023290 goto erret;
23291 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023292
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023293 fp = find_func(name);
23294 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023296 if (!eap->forceit)
23297 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023298 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023299 goto erret;
23300 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023301 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023302 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023303 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023304 name);
23305 goto erret;
23306 }
23307 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023308 ga_clear_strings(&(fp->uf_args));
23309 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023310 vim_free(name);
23311 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313 }
23314 else
23315 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023316 char numbuf[20];
23317
23318 fp = NULL;
23319 if (fudi.fd_newkey == NULL && !eap->forceit)
23320 {
23321 EMSG(_(e_funcdict));
23322 goto erret;
23323 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023324 if (fudi.fd_di == NULL)
23325 {
23326 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023327 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023328 goto erret;
23329 }
23330 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023331 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023332 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023333
23334 /* Give the function a sequential number. Can only be used with a
23335 * Funcref! */
23336 vim_free(name);
23337 sprintf(numbuf, "%d", ++func_nr);
23338 name = vim_strsave((char_u *)numbuf);
23339 if (name == NULL)
23340 goto erret;
23341 }
23342
23343 if (fp == NULL)
23344 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023345 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023346 {
23347 int slen, plen;
23348 char_u *scriptname;
23349
23350 /* Check that the autoload name matches the script name. */
23351 j = FAIL;
23352 if (sourcing_name != NULL)
23353 {
23354 scriptname = autoload_name(name);
23355 if (scriptname != NULL)
23356 {
23357 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023358 plen = (int)STRLEN(p);
23359 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023360 if (slen > plen && fnamecmp(p,
23361 sourcing_name + slen - plen) == 0)
23362 j = OK;
23363 vim_free(scriptname);
23364 }
23365 }
23366 if (j == FAIL)
23367 {
23368 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23369 goto erret;
23370 }
23371 }
23372
23373 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 if (fp == NULL)
23375 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023376
23377 if (fudi.fd_dict != NULL)
23378 {
23379 if (fudi.fd_di == NULL)
23380 {
23381 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023382 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023383 if (fudi.fd_di == NULL)
23384 {
23385 vim_free(fp);
23386 goto erret;
23387 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023388 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23389 {
23390 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023391 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023392 goto erret;
23393 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023394 }
23395 else
23396 /* overwrite existing dict entry */
23397 clear_tv(&fudi.fd_di->di_tv);
23398 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023399 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023400 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023401 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023402
23403 /* behave like "dict" was used */
23404 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023405 }
23406
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023408 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023409 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23410 {
23411 vim_free(fp);
23412 goto erret;
23413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023415 fp->uf_args = newargs;
23416 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023417#ifdef FEAT_PROFILE
23418 fp->uf_tml_count = NULL;
23419 fp->uf_tml_total = NULL;
23420 fp->uf_tml_self = NULL;
23421 fp->uf_profiling = FALSE;
23422 if (prof_def_func())
23423 func_do_profile(fp);
23424#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023425 fp->uf_varargs = varargs;
23426 fp->uf_flags = flags;
23427 fp->uf_calls = 0;
23428 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023429 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430
23431erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432 ga_clear_strings(&newargs);
23433 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023434ret_free:
23435 vim_free(skip_until);
23436 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023439 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023440}
23441
23442/*
23443 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023444 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023446 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023447 * TFN_INT: internal function name OK
23448 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023449 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450 * Advances "pp" to just after the function name (if no error).
23451 */
23452 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023453trans_function_name(
23454 char_u **pp,
23455 int skip, /* only find the end, don't evaluate */
23456 int flags,
23457 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023459 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 char_u *start;
23461 char_u *end;
23462 int lead;
23463 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023465 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023466
23467 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023468 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023470
23471 /* Check for hard coded <SNR>: already translated function ID (from a user
23472 * command). */
23473 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23474 && (*pp)[2] == (int)KE_SNR)
23475 {
23476 *pp += 3;
23477 len = get_id_len(pp) + 3;
23478 return vim_strnsave(start, len);
23479 }
23480
23481 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23482 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023484 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023486
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023487 /* Note that TFN_ flags use the same values as GLV_ flags. */
23488 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023489 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023490 if (end == start)
23491 {
23492 if (!skip)
23493 EMSG(_("E129: Function name required"));
23494 goto theend;
23495 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023496 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023497 {
23498 /*
23499 * Report an invalid expression in braces, unless the expression
23500 * evaluation has been cancelled due to an aborting error, an
23501 * interrupt, or an exception.
23502 */
23503 if (!aborting())
23504 {
23505 if (end != NULL)
23506 EMSG2(_(e_invarg2), start);
23507 }
23508 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023509 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023510 goto theend;
23511 }
23512
23513 if (lv.ll_tv != NULL)
23514 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023515 if (fdp != NULL)
23516 {
23517 fdp->fd_dict = lv.ll_dict;
23518 fdp->fd_newkey = lv.ll_newkey;
23519 lv.ll_newkey = NULL;
23520 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023521 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023522 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23523 {
23524 name = vim_strsave(lv.ll_tv->vval.v_string);
23525 *pp = end;
23526 }
23527 else
23528 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023529 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23530 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023531 EMSG(_(e_funcref));
23532 else
23533 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023534 name = NULL;
23535 }
23536 goto theend;
23537 }
23538
23539 if (lv.ll_name == NULL)
23540 {
23541 /* Error found, but continue after the function name. */
23542 *pp = end;
23543 goto theend;
23544 }
23545
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023546 /* Check if the name is a Funcref. If so, use the value. */
23547 if (lv.ll_exp_name != NULL)
23548 {
23549 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023550 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023551 if (name == lv.ll_exp_name)
23552 name = NULL;
23553 }
23554 else
23555 {
23556 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023557 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023558 if (name == *pp)
23559 name = NULL;
23560 }
23561 if (name != NULL)
23562 {
23563 name = vim_strsave(name);
23564 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023565 if (STRNCMP(name, "<SNR>", 5) == 0)
23566 {
23567 /* Change "<SNR>" to the byte sequence. */
23568 name[0] = K_SPECIAL;
23569 name[1] = KS_EXTRA;
23570 name[2] = (int)KE_SNR;
23571 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23572 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023573 goto theend;
23574 }
23575
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023576 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023577 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023578 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023579 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23580 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23581 {
23582 /* When there was "s:" already or the name expanded to get a
23583 * leading "s:" then remove it. */
23584 lv.ll_name += 2;
23585 len -= 2;
23586 lead = 2;
23587 }
23588 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023589 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023590 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023591 /* skip over "s:" and "g:" */
23592 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023593 lv.ll_name += 2;
23594 len = (int)(end - lv.ll_name);
23595 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023596
23597 /*
23598 * Copy the function name to allocated memory.
23599 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23600 * Accept <SNR>123_name() outside a script.
23601 */
23602 if (skip)
23603 lead = 0; /* do nothing */
23604 else if (lead > 0)
23605 {
23606 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023607 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23608 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023609 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023610 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023611 if (current_SID <= 0)
23612 {
23613 EMSG(_(e_usingsid));
23614 goto theend;
23615 }
23616 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23617 lead += (int)STRLEN(sid_buf);
23618 }
23619 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023620 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023621 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023622 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023623 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023624 goto theend;
23625 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023626 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023627 {
23628 char_u *cp = vim_strchr(lv.ll_name, ':');
23629
23630 if (cp != NULL && cp < end)
23631 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023632 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023633 goto theend;
23634 }
23635 }
23636
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023637 name = alloc((unsigned)(len + lead + 1));
23638 if (name != NULL)
23639 {
23640 if (lead > 0)
23641 {
23642 name[0] = K_SPECIAL;
23643 name[1] = KS_EXTRA;
23644 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023645 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023646 STRCPY(name + 3, sid_buf);
23647 }
23648 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023649 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023650 }
23651 *pp = end;
23652
23653theend:
23654 clear_lval(&lv);
23655 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656}
23657
23658/*
23659 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23660 * Return 2 if "p" starts with "s:".
23661 * Return 0 otherwise.
23662 */
23663 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023664eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023666 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23667 * the standard library function. */
23668 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23669 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 return 5;
23671 if (p[0] == 's' && p[1] == ':')
23672 return 2;
23673 return 0;
23674}
23675
23676/*
23677 * Return TRUE if "p" starts with "<SID>" or "s:".
23678 * Only works if eval_fname_script() returned non-zero for "p"!
23679 */
23680 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023681eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023682{
23683 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23684}
23685
23686/*
23687 * List the head of the function: "name(arg1, arg2)".
23688 */
23689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023690list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691{
23692 int j;
23693
23694 msg_start();
23695 if (indent)
23696 MSG_PUTS(" ");
23697 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023698 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 {
23700 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023701 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 }
23703 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023704 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023706 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 {
23708 if (j)
23709 MSG_PUTS(", ");
23710 msg_puts(FUNCARG(fp, j));
23711 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023712 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023713 {
23714 if (j)
23715 MSG_PUTS(", ");
23716 MSG_PUTS("...");
23717 }
23718 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023719 if (fp->uf_flags & FC_ABORT)
23720 MSG_PUTS(" abort");
23721 if (fp->uf_flags & FC_RANGE)
23722 MSG_PUTS(" range");
23723 if (fp->uf_flags & FC_DICT)
23724 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023725 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023726 if (p_verbose > 0)
23727 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728}
23729
23730/*
23731 * Find a function by name, return pointer to it in ufuncs.
23732 * Return NULL for unknown function.
23733 */
23734 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023735find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023737 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023739 hi = hash_find(&func_hashtab, name);
23740 if (!HASHITEM_EMPTY(hi))
23741 return HI2UF(hi);
23742 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743}
23744
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023745#if defined(EXITFREE) || defined(PROTO)
23746 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023747free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023748{
23749 hashitem_T *hi;
23750
23751 /* Need to start all over every time, because func_free() may change the
23752 * hash table. */
23753 while (func_hashtab.ht_used > 0)
23754 for (hi = func_hashtab.ht_array; ; ++hi)
23755 if (!HASHITEM_EMPTY(hi))
23756 {
23757 func_free(HI2UF(hi));
23758 break;
23759 }
23760}
23761#endif
23762
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023763 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023764translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023765{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023766 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023767 return find_internal_func(name) >= 0;
23768 return find_func(name) != NULL;
23769}
23770
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023771/*
23772 * Return TRUE if a function "name" exists.
23773 */
23774 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023775function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023776{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023777 char_u *nm = name;
23778 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023779 int n = FALSE;
23780
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023781 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23782 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023783 nm = skipwhite(nm);
23784
23785 /* Only accept "funcname", "funcname ", "funcname (..." and
23786 * "funcname(...", not "funcname!...". */
23787 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023788 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023789 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023790 return n;
23791}
23792
Bram Moolenaara1544c02013-05-30 12:35:52 +020023793 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023794get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023795{
23796 char_u *nm = name;
23797 char_u *p;
23798
23799 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23800
23801 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023802 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023803 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023804
Bram Moolenaara1544c02013-05-30 12:35:52 +020023805 vim_free(p);
23806 return NULL;
23807}
23808
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023809/*
23810 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023811 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23812 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023813 */
23814 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023815builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023816{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023817 char_u *p;
23818
23819 if (!ASCII_ISLOWER(name[0]))
23820 return FALSE;
23821 p = vim_strchr(name, AUTOLOAD_CHAR);
23822 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023823}
23824
Bram Moolenaar05159a02005-02-26 23:04:13 +000023825#if defined(FEAT_PROFILE) || defined(PROTO)
23826/*
23827 * Start profiling function "fp".
23828 */
23829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023830func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023831{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023832 int len = fp->uf_lines.ga_len;
23833
23834 if (len == 0)
23835 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023836 fp->uf_tm_count = 0;
23837 profile_zero(&fp->uf_tm_self);
23838 profile_zero(&fp->uf_tm_total);
23839 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023840 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023841 if (fp->uf_tml_total == NULL)
23842 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023843 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023844 if (fp->uf_tml_self == NULL)
23845 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023846 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023847 fp->uf_tml_idx = -1;
23848 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23849 || fp->uf_tml_self == NULL)
23850 return; /* out of memory */
23851
23852 fp->uf_profiling = TRUE;
23853}
23854
23855/*
23856 * Dump the profiling results for all functions in file "fd".
23857 */
23858 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023859func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023860{
23861 hashitem_T *hi;
23862 int todo;
23863 ufunc_T *fp;
23864 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023865 ufunc_T **sorttab;
23866 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023867
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023868 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023869 if (todo == 0)
23870 return; /* nothing to dump */
23871
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023872 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023873
Bram Moolenaar05159a02005-02-26 23:04:13 +000023874 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23875 {
23876 if (!HASHITEM_EMPTY(hi))
23877 {
23878 --todo;
23879 fp = HI2UF(hi);
23880 if (fp->uf_profiling)
23881 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023882 if (sorttab != NULL)
23883 sorttab[st_len++] = fp;
23884
Bram Moolenaar05159a02005-02-26 23:04:13 +000023885 if (fp->uf_name[0] == K_SPECIAL)
23886 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23887 else
23888 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23889 if (fp->uf_tm_count == 1)
23890 fprintf(fd, "Called 1 time\n");
23891 else
23892 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23893 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23894 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23895 fprintf(fd, "\n");
23896 fprintf(fd, "count total (s) self (s)\n");
23897
23898 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23899 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023900 if (FUNCLINE(fp, i) == NULL)
23901 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023902 prof_func_line(fd, fp->uf_tml_count[i],
23903 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023904 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23905 }
23906 fprintf(fd, "\n");
23907 }
23908 }
23909 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023910
23911 if (sorttab != NULL && st_len > 0)
23912 {
23913 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23914 prof_total_cmp);
23915 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23916 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23917 prof_self_cmp);
23918 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23919 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023920
23921 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023922}
Bram Moolenaar73830342005-02-28 22:48:19 +000023923
23924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023925prof_sort_list(
23926 FILE *fd,
23927 ufunc_T **sorttab,
23928 int st_len,
23929 char *title,
23930 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023931{
23932 int i;
23933 ufunc_T *fp;
23934
23935 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23936 fprintf(fd, "count total (s) self (s) function\n");
23937 for (i = 0; i < 20 && i < st_len; ++i)
23938 {
23939 fp = sorttab[i];
23940 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23941 prefer_self);
23942 if (fp->uf_name[0] == K_SPECIAL)
23943 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23944 else
23945 fprintf(fd, " %s()\n", fp->uf_name);
23946 }
23947 fprintf(fd, "\n");
23948}
23949
23950/*
23951 * Print the count and times for one function or function line.
23952 */
23953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023954prof_func_line(
23955 FILE *fd,
23956 int count,
23957 proftime_T *total,
23958 proftime_T *self,
23959 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023960{
23961 if (count > 0)
23962 {
23963 fprintf(fd, "%5d ", count);
23964 if (prefer_self && profile_equal(total, self))
23965 fprintf(fd, " ");
23966 else
23967 fprintf(fd, "%s ", profile_msg(total));
23968 if (!prefer_self && profile_equal(total, self))
23969 fprintf(fd, " ");
23970 else
23971 fprintf(fd, "%s ", profile_msg(self));
23972 }
23973 else
23974 fprintf(fd, " ");
23975}
23976
23977/*
23978 * Compare function for total time sorting.
23979 */
23980 static int
23981#ifdef __BORLANDC__
23982_RTLENTRYF
23983#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023984prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023985{
23986 ufunc_T *p1, *p2;
23987
23988 p1 = *(ufunc_T **)s1;
23989 p2 = *(ufunc_T **)s2;
23990 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23991}
23992
23993/*
23994 * Compare function for self time sorting.
23995 */
23996 static int
23997#ifdef __BORLANDC__
23998_RTLENTRYF
23999#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024000prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024001{
24002 ufunc_T *p1, *p2;
24003
24004 p1 = *(ufunc_T **)s1;
24005 p2 = *(ufunc_T **)s2;
24006 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24007}
24008
Bram Moolenaar05159a02005-02-26 23:04:13 +000024009#endif
24010
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024011/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024012 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024013 * Return TRUE if a package was loaded.
24014 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024015 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024016script_autoload(
24017 char_u *name,
24018 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024019{
24020 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024021 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024022 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024023 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024024
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024025 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024026 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024027 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024028 return FALSE;
24029
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024030 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024031
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024032 /* Find the name in the list of previously loaded package names. Skip
24033 * "autoload/", it's always the same. */
24034 for (i = 0; i < ga_loaded.ga_len; ++i)
24035 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24036 break;
24037 if (!reload && i < ga_loaded.ga_len)
24038 ret = FALSE; /* was loaded already */
24039 else
24040 {
24041 /* Remember the name if it wasn't loaded already. */
24042 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24043 {
24044 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24045 tofree = NULL;
24046 }
24047
24048 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024049 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024050 ret = TRUE;
24051 }
24052
24053 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024054 return ret;
24055}
24056
24057/*
24058 * Return the autoload script name for a function or variable name.
24059 * Returns NULL when out of memory.
24060 */
24061 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024062autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024063{
24064 char_u *p;
24065 char_u *scriptname;
24066
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024067 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024068 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24069 if (scriptname == NULL)
24070 return FALSE;
24071 STRCPY(scriptname, "autoload/");
24072 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024073 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024074 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024075 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024076 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024077 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024078}
24079
Bram Moolenaar071d4272004-06-13 20:20:40 +000024080#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24081
24082/*
24083 * Function given to ExpandGeneric() to obtain the list of user defined
24084 * function names.
24085 */
24086 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024087get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024089 static long_u done;
24090 static hashitem_T *hi;
24091 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024092
24093 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024095 done = 0;
24096 hi = func_hashtab.ht_array;
24097 }
24098 if (done < func_hashtab.ht_used)
24099 {
24100 if (done++ > 0)
24101 ++hi;
24102 while (HASHITEM_EMPTY(hi))
24103 ++hi;
24104 fp = HI2UF(hi);
24105
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024106 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024107 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024108
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024109 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24110 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111
24112 cat_func_name(IObuff, fp);
24113 if (xp->xp_context != EXPAND_USER_FUNC)
24114 {
24115 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024116 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117 STRCAT(IObuff, ")");
24118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 return IObuff;
24120 }
24121 return NULL;
24122}
24123
24124#endif /* FEAT_CMDL_COMPL */
24125
24126/*
24127 * Copy the function name of "fp" to buffer "buf".
24128 * "buf" must be able to hold the function name plus three bytes.
24129 * Takes care of script-local function names.
24130 */
24131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024132cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024134 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135 {
24136 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024137 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138 }
24139 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024140 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141}
24142
24143/*
24144 * ":delfunction {name}"
24145 */
24146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024147ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024149 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 char_u *p;
24151 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024152 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153
24154 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024155 name = trans_function_name(&p, eap->skip, 0, &fudi);
24156 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024158 {
24159 if (fudi.fd_dict != NULL && !eap->skip)
24160 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024161 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163 if (!ends_excmd(*skipwhite(p)))
24164 {
24165 vim_free(name);
24166 EMSG(_(e_trailing));
24167 return;
24168 }
24169 eap->nextcmd = check_nextcmd(p);
24170 if (eap->nextcmd != NULL)
24171 *p = NUL;
24172
24173 if (!eap->skip)
24174 fp = find_func(name);
24175 vim_free(name);
24176
24177 if (!eap->skip)
24178 {
24179 if (fp == NULL)
24180 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024181 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 return;
24183 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024184 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 {
24186 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24187 return;
24188 }
24189
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024190 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024192 /* Delete the dict item that refers to the function, it will
24193 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024194 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024196 else
24197 func_free(fp);
24198 }
24199}
24200
24201/*
24202 * Free a function and remove it from the list of functions.
24203 */
24204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024205func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024206{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024207 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024208
24209 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024210 ga_clear_strings(&(fp->uf_args));
24211 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024212#ifdef FEAT_PROFILE
24213 vim_free(fp->uf_tml_count);
24214 vim_free(fp->uf_tml_total);
24215 vim_free(fp->uf_tml_self);
24216#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024218 /* remove the function from the function hashtable */
24219 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24220 if (HASHITEM_EMPTY(hi))
24221 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024222 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024223 hash_remove(&func_hashtab, hi);
24224
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024225 vim_free(fp);
24226}
24227
24228/*
24229 * Unreference a Function: decrement the reference count and free it when it
24230 * becomes zero. Only for numbered functions.
24231 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024232 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024233func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024234{
24235 ufunc_T *fp;
24236
24237 if (name != NULL && isdigit(*name))
24238 {
24239 fp = find_func(name);
24240 if (fp == NULL)
24241 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024242 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024243 {
24244 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024245 * when "uf_calls" becomes zero. */
24246 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024247 func_free(fp);
24248 }
24249 }
24250}
24251
24252/*
24253 * Count a reference to a Function.
24254 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024255 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024256func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024257{
24258 ufunc_T *fp;
24259
24260 if (name != NULL && isdigit(*name))
24261 {
24262 fp = find_func(name);
24263 if (fp == NULL)
24264 EMSG2(_(e_intern2), "func_ref()");
24265 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024266 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267 }
24268}
24269
24270/*
24271 * Call a user function.
24272 */
24273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024274call_user_func(
24275 ufunc_T *fp, /* pointer to function */
24276 int argcount, /* nr of args */
24277 typval_T *argvars, /* arguments */
24278 typval_T *rettv, /* return value */
24279 linenr_T firstline, /* first line of range */
24280 linenr_T lastline, /* last line of range */
24281 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282{
Bram Moolenaar33570922005-01-25 22:26:29 +000024283 char_u *save_sourcing_name;
24284 linenr_T save_sourcing_lnum;
24285 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024286 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024287 int save_did_emsg;
24288 static int depth = 0;
24289 dictitem_T *v;
24290 int fixvar_idx = 0; /* index in fixvar[] */
24291 int i;
24292 int ai;
24293 char_u numbuf[NUMBUFLEN];
24294 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024295 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024296#ifdef FEAT_PROFILE
24297 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024298 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300
24301 /* If depth of calling is getting too high, don't execute the function */
24302 if (depth >= p_mfd)
24303 {
24304 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024305 rettv->v_type = VAR_NUMBER;
24306 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 return;
24308 }
24309 ++depth;
24310
24311 line_breakcheck(); /* check for CTRL-C hit */
24312
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024313 fc = (funccall_T *)alloc(sizeof(funccall_T));
24314 fc->caller = current_funccal;
24315 current_funccal = fc;
24316 fc->func = fp;
24317 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024318 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024319 fc->linenr = 0;
24320 fc->returned = FALSE;
24321 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024323 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24324 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325
Bram Moolenaar33570922005-01-25 22:26:29 +000024326 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024327 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024328 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24329 * each argument variable and saves a lot of time.
24330 */
24331 /*
24332 * Init l: variables.
24333 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024334 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024335 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024336 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024337 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24338 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024339 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024340 name = v->di_key;
24341 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024342 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024343 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024344 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024345 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024346 v->di_tv.vval.v_dict = selfdict;
24347 ++selfdict->dv_refcount;
24348 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024349
Bram Moolenaar33570922005-01-25 22:26:29 +000024350 /*
24351 * Init a: variables.
24352 * Set a:0 to "argcount".
24353 * Set a:000 to a list with room for the "..." arguments.
24354 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024355 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024356 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024357 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024358 /* Use "name" to avoid a warning from some compiler that checks the
24359 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024360 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024361 name = v->di_key;
24362 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024363 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024364 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024365 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024366 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024367 v->di_tv.vval.v_list = &fc->l_varlist;
24368 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24369 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24370 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024371
24372 /*
24373 * Set a:firstline to "firstline" and a:lastline to "lastline".
24374 * Set a:name to named arguments.
24375 * Set a:N to the "..." arguments.
24376 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024377 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024378 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024379 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024380 (varnumber_T)lastline);
24381 for (i = 0; i < argcount; ++i)
24382 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024383 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024384 if (ai < 0)
24385 /* named argument a:name */
24386 name = FUNCARG(fp, i);
24387 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024388 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024389 /* "..." argument a:1, a:2, etc. */
24390 sprintf((char *)numbuf, "%d", ai + 1);
24391 name = numbuf;
24392 }
24393 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24394 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024395 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024396 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24397 }
24398 else
24399 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024400 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24401 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024402 if (v == NULL)
24403 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024404 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024405 }
24406 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024407 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024408
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024409 /* Note: the values are copied directly to avoid alloc/free.
24410 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024411 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024412 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024413
24414 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24415 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024416 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24417 fc->l_listitems[ai].li_tv = argvars[i];
24418 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024419 }
24420 }
24421
Bram Moolenaar071d4272004-06-13 20:20:40 +000024422 /* Don't redraw while executing the function. */
24423 ++RedrawingDisabled;
24424 save_sourcing_name = sourcing_name;
24425 save_sourcing_lnum = sourcing_lnum;
24426 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024427 /* need space for function name + ("function " + 3) or "[number]" */
24428 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24429 + STRLEN(fp->uf_name) + 20;
24430 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024431 if (sourcing_name != NULL)
24432 {
24433 if (save_sourcing_name != NULL
24434 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024435 sprintf((char *)sourcing_name, "%s[%d]..",
24436 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024437 else
24438 STRCPY(sourcing_name, "function ");
24439 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24440
24441 if (p_verbose >= 12)
24442 {
24443 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024444 verbose_enter_scroll();
24445
Bram Moolenaar555b2802005-05-19 21:08:39 +000024446 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447 if (p_verbose >= 14)
24448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024450 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024451 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024452 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024453
24454 msg_puts((char_u *)"(");
24455 for (i = 0; i < argcount; ++i)
24456 {
24457 if (i > 0)
24458 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024459 if (argvars[i].v_type == VAR_NUMBER)
24460 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024461 else
24462 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024463 /* Do not want errors such as E724 here. */
24464 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024465 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024466 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024467 if (s != NULL)
24468 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024469 if (vim_strsize(s) > MSG_BUF_CLEN)
24470 {
24471 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24472 s = buf;
24473 }
24474 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024475 vim_free(tofree);
24476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024477 }
24478 }
24479 msg_puts((char_u *)")");
24480 }
24481 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024482
24483 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484 --no_wait_return;
24485 }
24486 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024487#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024488 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024489 {
24490 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24491 func_do_profile(fp);
24492 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024493 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024494 {
24495 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024496 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024497 profile_zero(&fp->uf_tm_children);
24498 }
24499 script_prof_save(&wait_start);
24500 }
24501#endif
24502
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024504 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505 save_did_emsg = did_emsg;
24506 did_emsg = FALSE;
24507
24508 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024509 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24511
24512 --RedrawingDisabled;
24513
24514 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024515 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024517 clear_tv(rettv);
24518 rettv->v_type = VAR_NUMBER;
24519 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024520 }
24521
Bram Moolenaar05159a02005-02-26 23:04:13 +000024522#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024523 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024524 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024525 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024526 profile_end(&call_start);
24527 profile_sub_wait(&wait_start, &call_start);
24528 profile_add(&fp->uf_tm_total, &call_start);
24529 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024530 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024531 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024532 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24533 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024534 }
24535 }
24536#endif
24537
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 /* when being verbose, mention the return value */
24539 if (p_verbose >= 12)
24540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024542 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024543
Bram Moolenaar071d4272004-06-13 20:20:40 +000024544 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024545 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024546 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024547 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024548 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024549 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024551 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024552 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024553 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024554 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024555
Bram Moolenaar555b2802005-05-19 21:08:39 +000024556 /* The value may be very long. Skip the middle part, so that we
24557 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024558 * truncate it at the end. Don't want errors such as E724 here. */
24559 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024560 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024561 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024562 if (s != NULL)
24563 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024564 if (vim_strsize(s) > MSG_BUF_CLEN)
24565 {
24566 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24567 s = buf;
24568 }
24569 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024570 vim_free(tofree);
24571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024572 }
24573 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024574
24575 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576 --no_wait_return;
24577 }
24578
24579 vim_free(sourcing_name);
24580 sourcing_name = save_sourcing_name;
24581 sourcing_lnum = save_sourcing_lnum;
24582 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024583#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024584 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024585 script_prof_restore(&wait_start);
24586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024587
24588 if (p_verbose >= 12 && sourcing_name != NULL)
24589 {
24590 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024591 verbose_enter_scroll();
24592
Bram Moolenaar555b2802005-05-19 21:08:39 +000024593 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024594 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024595
24596 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597 --no_wait_return;
24598 }
24599
24600 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024601 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024602 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024603
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024604 /* If the a:000 list and the l: and a: dicts are not referenced we can
24605 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024606 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24607 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24608 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24609 {
24610 free_funccal(fc, FALSE);
24611 }
24612 else
24613 {
24614 hashitem_T *hi;
24615 listitem_T *li;
24616 int todo;
24617
24618 /* "fc" is still in use. This can happen when returning "a:000" or
24619 * assigning "l:" to a global variable.
24620 * Link "fc" in the list for garbage collection later. */
24621 fc->caller = previous_funccal;
24622 previous_funccal = fc;
24623
24624 /* Make a copy of the a: variables, since we didn't do that above. */
24625 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24626 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24627 {
24628 if (!HASHITEM_EMPTY(hi))
24629 {
24630 --todo;
24631 v = HI2DI(hi);
24632 copy_tv(&v->di_tv, &v->di_tv);
24633 }
24634 }
24635
24636 /* Make a copy of the a:000 items, since we didn't do that above. */
24637 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24638 copy_tv(&li->li_tv, &li->li_tv);
24639 }
24640}
24641
24642/*
24643 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024644 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024645 */
24646 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024647can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024648{
24649 return (fc->l_varlist.lv_copyID != copyID
24650 && fc->l_vars.dv_copyID != copyID
24651 && fc->l_avars.dv_copyID != copyID);
24652}
24653
24654/*
24655 * Free "fc" and what it contains.
24656 */
24657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024658free_funccal(
24659 funccall_T *fc,
24660 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024661{
24662 listitem_T *li;
24663
24664 /* The a: variables typevals may not have been allocated, only free the
24665 * allocated variables. */
24666 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24667
24668 /* free all l: variables */
24669 vars_clear(&fc->l_vars.dv_hashtab);
24670
24671 /* Free the a:000 variables if they were allocated. */
24672 if (free_val)
24673 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24674 clear_tv(&li->li_tv);
24675
24676 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677}
24678
24679/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024680 * Add a number variable "name" to dict "dp" with value "nr".
24681 */
24682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024683add_nr_var(
24684 dict_T *dp,
24685 dictitem_T *v,
24686 char *name,
24687 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024688{
24689 STRCPY(v->di_key, name);
24690 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24691 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24692 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024693 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024694 v->di_tv.vval.v_number = nr;
24695}
24696
24697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024698 * ":return [expr]"
24699 */
24700 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024701ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702{
24703 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024704 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705 int returning = FALSE;
24706
24707 if (current_funccal == NULL)
24708 {
24709 EMSG(_("E133: :return not inside a function"));
24710 return;
24711 }
24712
24713 if (eap->skip)
24714 ++emsg_skip;
24715
24716 eap->nextcmd = NULL;
24717 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024718 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719 {
24720 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024721 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024722 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024723 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024724 }
24725 /* It's safer to return also on error. */
24726 else if (!eap->skip)
24727 {
24728 /*
24729 * Return unless the expression evaluation has been cancelled due to an
24730 * aborting error, an interrupt, or an exception.
24731 */
24732 if (!aborting())
24733 returning = do_return(eap, FALSE, TRUE, NULL);
24734 }
24735
24736 /* When skipping or the return gets pending, advance to the next command
24737 * in this line (!returning). Otherwise, ignore the rest of the line.
24738 * Following lines will be ignored by get_func_line(). */
24739 if (returning)
24740 eap->nextcmd = NULL;
24741 else if (eap->nextcmd == NULL) /* no argument */
24742 eap->nextcmd = check_nextcmd(arg);
24743
24744 if (eap->skip)
24745 --emsg_skip;
24746}
24747
24748/*
24749 * Return from a function. Possibly makes the return pending. Also called
24750 * for a pending return at the ":endtry" or after returning from an extra
24751 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024752 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024753 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024754 * FALSE when the return gets pending.
24755 */
24756 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024757do_return(
24758 exarg_T *eap,
24759 int reanimate,
24760 int is_cmd,
24761 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762{
24763 int idx;
24764 struct condstack *cstack = eap->cstack;
24765
24766 if (reanimate)
24767 /* Undo the return. */
24768 current_funccal->returned = FALSE;
24769
24770 /*
24771 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24772 * not in its finally clause (which then is to be executed next) is found.
24773 * In this case, make the ":return" pending for execution at the ":endtry".
24774 * Otherwise, return normally.
24775 */
24776 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24777 if (idx >= 0)
24778 {
24779 cstack->cs_pending[idx] = CSTP_RETURN;
24780
24781 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024782 /* A pending return again gets pending. "rettv" points to an
24783 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024784 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024785 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024786 else
24787 {
24788 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024789 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024790 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024791 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024793 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024794 {
24795 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024796 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024797 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024798 else
24799 EMSG(_(e_outofmem));
24800 }
24801 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024802 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024803
24804 if (reanimate)
24805 {
24806 /* The pending return value could be overwritten by a ":return"
24807 * without argument in a finally clause; reset the default
24808 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024809 current_funccal->rettv->v_type = VAR_NUMBER;
24810 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024811 }
24812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024813 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814 }
24815 else
24816 {
24817 current_funccal->returned = TRUE;
24818
24819 /* If the return is carried out now, store the return value. For
24820 * a return immediately after reanimation, the value is already
24821 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024822 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024824 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024825 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024826 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024827 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828 }
24829 }
24830
24831 return idx < 0;
24832}
24833
24834/*
24835 * Free the variable with a pending return value.
24836 */
24837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024838discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839{
Bram Moolenaar33570922005-01-25 22:26:29 +000024840 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841}
24842
24843/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024844 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024845 * is an allocated string. Used by report_pending() for verbose messages.
24846 */
24847 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024848get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024850 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024851 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024852 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024854 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024855 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024856 if (s == NULL)
24857 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024858
24859 STRCPY(IObuff, ":return ");
24860 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24861 if (STRLEN(s) + 8 >= IOSIZE)
24862 STRCPY(IObuff + IOSIZE - 4, "...");
24863 vim_free(tofree);
24864 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024865}
24866
24867/*
24868 * Get next function line.
24869 * Called by do_cmdline() to get the next line.
24870 * Returns allocated string, or NULL for end of function.
24871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024872 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024873get_func_line(
24874 int c UNUSED,
24875 void *cookie,
24876 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877{
Bram Moolenaar33570922005-01-25 22:26:29 +000024878 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024879 ufunc_T *fp = fcp->func;
24880 char_u *retval;
24881 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024882
24883 /* If breakpoints have been added/deleted need to check for it. */
24884 if (fcp->dbg_tick != debug_tick)
24885 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024886 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024887 sourcing_lnum);
24888 fcp->dbg_tick = debug_tick;
24889 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024890#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024891 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024892 func_line_end(cookie);
24893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894
Bram Moolenaar05159a02005-02-26 23:04:13 +000024895 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024896 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24897 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024898 retval = NULL;
24899 else
24900 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024901 /* Skip NULL lines (continuation lines). */
24902 while (fcp->linenr < gap->ga_len
24903 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24904 ++fcp->linenr;
24905 if (fcp->linenr >= gap->ga_len)
24906 retval = NULL;
24907 else
24908 {
24909 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24910 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024911#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024912 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024913 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024914#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916 }
24917
24918 /* Did we encounter a breakpoint? */
24919 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24920 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024921 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024923 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924 sourcing_lnum);
24925 fcp->dbg_tick = debug_tick;
24926 }
24927
24928 return retval;
24929}
24930
Bram Moolenaar05159a02005-02-26 23:04:13 +000024931#if defined(FEAT_PROFILE) || defined(PROTO)
24932/*
24933 * Called when starting to read a function line.
24934 * "sourcing_lnum" must be correct!
24935 * When skipping lines it may not actually be executed, but we won't find out
24936 * until later and we need to store the time now.
24937 */
24938 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024939func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024940{
24941 funccall_T *fcp = (funccall_T *)cookie;
24942 ufunc_T *fp = fcp->func;
24943
24944 if (fp->uf_profiling && sourcing_lnum >= 1
24945 && sourcing_lnum <= fp->uf_lines.ga_len)
24946 {
24947 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024948 /* Skip continuation lines. */
24949 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24950 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024951 fp->uf_tml_execed = FALSE;
24952 profile_start(&fp->uf_tml_start);
24953 profile_zero(&fp->uf_tml_children);
24954 profile_get_wait(&fp->uf_tml_wait);
24955 }
24956}
24957
24958/*
24959 * Called when actually executing a function line.
24960 */
24961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024962func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024963{
24964 funccall_T *fcp = (funccall_T *)cookie;
24965 ufunc_T *fp = fcp->func;
24966
24967 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24968 fp->uf_tml_execed = TRUE;
24969}
24970
24971/*
24972 * Called when done with a function line.
24973 */
24974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024975func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024976{
24977 funccall_T *fcp = (funccall_T *)cookie;
24978 ufunc_T *fp = fcp->func;
24979
24980 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24981 {
24982 if (fp->uf_tml_execed)
24983 {
24984 ++fp->uf_tml_count[fp->uf_tml_idx];
24985 profile_end(&fp->uf_tml_start);
24986 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024987 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024988 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24989 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024990 }
24991 fp->uf_tml_idx = -1;
24992 }
24993}
24994#endif
24995
Bram Moolenaar071d4272004-06-13 20:20:40 +000024996/*
24997 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024998 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024999 */
25000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025001func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025002{
Bram Moolenaar33570922005-01-25 22:26:29 +000025003 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004
25005 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25006 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025007 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025008 || fcp->returned);
25009}
25010
25011/*
25012 * return TRUE if cookie indicates a function which "abort"s on errors.
25013 */
25014 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025015func_has_abort(
25016 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025018 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019}
25020
25021#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25022typedef enum
25023{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025024 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25025 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25026 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027} var_flavour_T;
25028
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025029static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025030
25031 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025032var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025033{
25034 char_u *p = varname;
25035
25036 if (ASCII_ISUPPER(*p))
25037 {
25038 while (*(++p))
25039 if (ASCII_ISLOWER(*p))
25040 return VAR_FLAVOUR_SESSION;
25041 return VAR_FLAVOUR_VIMINFO;
25042 }
25043 else
25044 return VAR_FLAVOUR_DEFAULT;
25045}
25046#endif
25047
25048#if defined(FEAT_VIMINFO) || defined(PROTO)
25049/*
25050 * Restore global vars that start with a capital from the viminfo file
25051 */
25052 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025053read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054{
25055 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025056 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025057 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025058 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025059
25060 if (!writing && (find_viminfo_parameter('!') != NULL))
25061 {
25062 tab = vim_strchr(virp->vir_line + 1, '\t');
25063 if (tab != NULL)
25064 {
25065 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025066 switch (*tab)
25067 {
25068 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025069#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025070 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025071#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025072 case 'D': type = VAR_DICT; break;
25073 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025074 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076
25077 tab = vim_strchr(tab, '\t');
25078 if (tab != NULL)
25079 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025080 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025081 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025082 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025084#ifdef FEAT_FLOAT
25085 else if (type == VAR_FLOAT)
25086 (void)string2float(tab + 1, &tv.vval.v_float);
25087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025088 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025089 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025090 if (type == VAR_DICT || type == VAR_LIST)
25091 {
25092 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25093
25094 if (etv == NULL)
25095 /* Failed to parse back the dict or list, use it as a
25096 * string. */
25097 tv.v_type = VAR_STRING;
25098 else
25099 {
25100 vim_free(tv.vval.v_string);
25101 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025102 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025103 }
25104 }
25105
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025106 /* when in a function use global variables */
25107 save_funccal = current_funccal;
25108 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025109 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025110 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025111
25112 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025113 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025114 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25115 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025116 }
25117 }
25118 }
25119
25120 return viminfo_readline(virp);
25121}
25122
25123/*
25124 * Write global vars that start with a capital to the viminfo file
25125 */
25126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025127write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128{
Bram Moolenaar33570922005-01-25 22:26:29 +000025129 hashitem_T *hi;
25130 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025131 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025132 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025133 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025134 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025135 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025136
25137 if (find_viminfo_parameter('!') == NULL)
25138 return;
25139
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025140 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025141
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025142 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025143 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025144 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025145 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025146 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025147 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025148 this_var = HI2DI(hi);
25149 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025150 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025151 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025152 {
25153 case VAR_STRING: s = "STR"; break;
25154 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025155 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025156 case VAR_DICT: s = "DIC"; break;
25157 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025158 case VAR_SPECIAL: s = "XPL"; break;
25159
25160 case VAR_UNKNOWN:
25161 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025162 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025163 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025164 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025165 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025166 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025167 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025168 if (p != NULL)
25169 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025170 vim_free(tofree);
25171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025172 }
25173 }
25174}
25175#endif
25176
25177#if defined(FEAT_SESSION) || defined(PROTO)
25178 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025179store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180{
Bram Moolenaar33570922005-01-25 22:26:29 +000025181 hashitem_T *hi;
25182 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025183 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025184 char_u *p, *t;
25185
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025186 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025187 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025188 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025189 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025190 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025191 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025192 this_var = HI2DI(hi);
25193 if ((this_var->di_tv.v_type == VAR_NUMBER
25194 || this_var->di_tv.v_type == VAR_STRING)
25195 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025196 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025197 /* Escape special characters with a backslash. Turn a LF and
25198 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025199 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025200 (char_u *)"\\\"\n\r");
25201 if (p == NULL) /* out of memory */
25202 break;
25203 for (t = p; *t != NUL; ++t)
25204 if (*t == '\n')
25205 *t = 'n';
25206 else if (*t == '\r')
25207 *t = 'r';
25208 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025209 this_var->di_key,
25210 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25211 : ' ',
25212 p,
25213 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25214 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025215 || put_eol(fd) == FAIL)
25216 {
25217 vim_free(p);
25218 return FAIL;
25219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220 vim_free(p);
25221 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025222#ifdef FEAT_FLOAT
25223 else if (this_var->di_tv.v_type == VAR_FLOAT
25224 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25225 {
25226 float_T f = this_var->di_tv.vval.v_float;
25227 int sign = ' ';
25228
25229 if (f < 0)
25230 {
25231 f = -f;
25232 sign = '-';
25233 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025234 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025235 this_var->di_key, sign, f) < 0)
25236 || put_eol(fd) == FAIL)
25237 return FAIL;
25238 }
25239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025240 }
25241 }
25242 return OK;
25243}
25244#endif
25245
Bram Moolenaar661b1822005-07-28 22:36:45 +000025246/*
25247 * Display script name where an item was last set.
25248 * Should only be invoked when 'verbose' is non-zero.
25249 */
25250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025251last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025252{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025253 char_u *p;
25254
Bram Moolenaar661b1822005-07-28 22:36:45 +000025255 if (scriptID != 0)
25256 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025257 p = home_replace_save(NULL, get_scriptname(scriptID));
25258 if (p != NULL)
25259 {
25260 verbose_enter();
25261 MSG_PUTS(_("\n\tLast set from "));
25262 MSG_PUTS(p);
25263 vim_free(p);
25264 verbose_leave();
25265 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025266 }
25267}
25268
Bram Moolenaard812df62008-11-09 12:46:09 +000025269/*
25270 * List v:oldfiles in a nice way.
25271 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025272 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025273ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025274{
25275 list_T *l = vimvars[VV_OLDFILES].vv_list;
25276 listitem_T *li;
25277 int nr = 0;
25278
25279 if (l == NULL)
25280 msg((char_u *)_("No old files"));
25281 else
25282 {
25283 msg_start();
25284 msg_scroll = TRUE;
25285 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25286 {
25287 msg_outnum((long)++nr);
25288 MSG_PUTS(": ");
25289 msg_outtrans(get_tv_string(&li->li_tv));
25290 msg_putchar('\n');
25291 out_flush(); /* output one line at a time */
25292 ui_breakcheck();
25293 }
25294 /* Assume "got_int" was set to truncate the listing. */
25295 got_int = FALSE;
25296
25297#ifdef FEAT_BROWSE_CMD
25298 if (cmdmod.browse)
25299 {
25300 quit_more = FALSE;
25301 nr = prompt_for_number(FALSE);
25302 msg_starthere();
25303 if (nr > 0)
25304 {
25305 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25306 (long)nr);
25307
25308 if (p != NULL)
25309 {
25310 p = expand_env_save(p);
25311 eap->arg = p;
25312 eap->cmdidx = CMD_edit;
25313 cmdmod.browse = FALSE;
25314 do_exedit(eap, NULL);
25315 vim_free(p);
25316 }
25317 }
25318 }
25319#endif
25320 }
25321}
25322
Bram Moolenaar53744302015-07-17 17:38:22 +020025323/* reset v:option_new, v:option_old and v:option_type */
25324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025325reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025326{
25327 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25328 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25329 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25330}
25331
25332
Bram Moolenaar071d4272004-06-13 20:20:40 +000025333#endif /* FEAT_EVAL */
25334
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025336#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337
25338#ifdef WIN3264
25339/*
25340 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25341 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025342static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25343static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25344static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345
25346/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025347 * Get the short path (8.3) for the filename in "fnamep".
25348 * Only works for a valid file name.
25349 * When the path gets longer "fnamep" is changed and the allocated buffer
25350 * is put in "bufp".
25351 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25352 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025353 */
25354 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025355get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025356{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025357 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025358 char_u *newbuf;
25359
25360 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 l = GetShortPathName(*fnamep, *fnamep, len);
25362 if (l > len - 1)
25363 {
25364 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025365 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366 newbuf = vim_strnsave(*fnamep, l);
25367 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025368 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369
25370 vim_free(*bufp);
25371 *fnamep = *bufp = newbuf;
25372
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025373 /* Really should always succeed, as the buffer is big enough. */
25374 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025375 }
25376
25377 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025378 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379}
25380
25381/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025382 * Get the short path (8.3) for the filename in "fname". The converted
25383 * path is returned in "bufp".
25384 *
25385 * Some of the directories specified in "fname" may not exist. This function
25386 * will shorten the existing directories at the beginning of the path and then
25387 * append the remaining non-existing path.
25388 *
25389 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025390 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025391 * bufp - Pointer to an allocated buffer for the filename.
25392 * fnamelen - Length of the filename pointed to by fname
25393 *
25394 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395 */
25396 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025397shortpath_for_invalid_fname(
25398 char_u **fname,
25399 char_u **bufp,
25400 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025401{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025402 char_u *short_fname, *save_fname, *pbuf_unused;
25403 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025405 int old_len, len;
25406 int new_len, sfx_len;
25407 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025408
25409 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025410 old_len = *fnamelen;
25411 save_fname = vim_strnsave(*fname, old_len);
25412 pbuf_unused = NULL;
25413 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025415 endp = save_fname + old_len - 1; /* Find the end of the copy */
25416 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025418 /*
25419 * Try shortening the supplied path till it succeeds by removing one
25420 * directory at a time from the tail of the path.
25421 */
25422 len = 0;
25423 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025424 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025425 /* go back one path-separator */
25426 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25427 --endp;
25428 if (endp <= save_fname)
25429 break; /* processed the complete path */
25430
25431 /*
25432 * Replace the path separator with a NUL and try to shorten the
25433 * resulting path.
25434 */
25435 ch = *endp;
25436 *endp = 0;
25437 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025438 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025439 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25440 {
25441 retval = FAIL;
25442 goto theend;
25443 }
25444 *endp = ch; /* preserve the string */
25445
25446 if (len > 0)
25447 break; /* successfully shortened the path */
25448
25449 /* failed to shorten the path. Skip the path separator */
25450 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451 }
25452
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025453 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025454 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025455 /*
25456 * Succeeded in shortening the path. Now concatenate the shortened
25457 * path with the remaining path at the tail.
25458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025460 /* Compute the length of the new path. */
25461 sfx_len = (int)(save_endp - endp) + 1;
25462 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025464 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025466 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025468 /* There is not enough space in the currently allocated string,
25469 * copy it to a buffer big enough. */
25470 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025472 {
25473 retval = FAIL;
25474 goto theend;
25475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476 }
25477 else
25478 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025479 /* Transfer short_fname to the main buffer (it's big enough),
25480 * unless get_short_pathname() did its work in-place. */
25481 *fname = *bufp = save_fname;
25482 if (short_fname != save_fname)
25483 vim_strncpy(save_fname, short_fname, len);
25484 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025486
25487 /* concat the not-shortened part of the path */
25488 vim_strncpy(*fname + len, endp, sfx_len);
25489 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025491
25492theend:
25493 vim_free(pbuf_unused);
25494 vim_free(save_fname);
25495
25496 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025497}
25498
25499/*
25500 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025501 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025502 */
25503 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025504shortpath_for_partial(
25505 char_u **fnamep,
25506 char_u **bufp,
25507 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025508{
25509 int sepcount, len, tflen;
25510 char_u *p;
25511 char_u *pbuf, *tfname;
25512 int hasTilde;
25513
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025514 /* Count up the path separators from the RHS.. so we know which part
25515 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025517 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 if (vim_ispathsep(*p))
25519 ++sepcount;
25520
25521 /* Need full path first (use expand_env() to remove a "~/") */
25522 hasTilde = (**fnamep == '~');
25523 if (hasTilde)
25524 pbuf = tfname = expand_env_save(*fnamep);
25525 else
25526 pbuf = tfname = FullName_save(*fnamep, FALSE);
25527
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025528 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025530 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25531 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532
25533 if (len == 0)
25534 {
25535 /* Don't have a valid filename, so shorten the rest of the
25536 * path if we can. This CAN give us invalid 8.3 filenames, but
25537 * there's not a lot of point in guessing what it might be.
25538 */
25539 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025540 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25541 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025542 }
25543
25544 /* Count the paths backward to find the beginning of the desired string. */
25545 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025546 {
25547#ifdef FEAT_MBYTE
25548 if (has_mbyte)
25549 p -= mb_head_off(tfname, p);
25550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551 if (vim_ispathsep(*p))
25552 {
25553 if (sepcount == 0 || (hasTilde && sepcount == 1))
25554 break;
25555 else
25556 sepcount --;
25557 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559 if (hasTilde)
25560 {
25561 --p;
25562 if (p >= tfname)
25563 *p = '~';
25564 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025566 }
25567 else
25568 ++p;
25569
25570 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25571 vim_free(*bufp);
25572 *fnamelen = (int)STRLEN(p);
25573 *bufp = pbuf;
25574 *fnamep = p;
25575
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025576 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577}
25578#endif /* WIN3264 */
25579
25580/*
25581 * Adjust a filename, according to a string of modifiers.
25582 * *fnamep must be NUL terminated when called. When returning, the length is
25583 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025584 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585 * When there is an error, *fnamep is set to NULL.
25586 */
25587 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025588modify_fname(
25589 char_u *src, /* string with modifiers */
25590 int *usedlen, /* characters after src that are used */
25591 char_u **fnamep, /* file name so far */
25592 char_u **bufp, /* buffer for allocated file name or NULL */
25593 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594{
25595 int valid = 0;
25596 char_u *tail;
25597 char_u *s, *p, *pbuf;
25598 char_u dirname[MAXPATHL];
25599 int c;
25600 int has_fullname = 0;
25601#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025602 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025603 int has_shortname = 0;
25604#endif
25605
25606repeat:
25607 /* ":p" - full path/file_name */
25608 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25609 {
25610 has_fullname = 1;
25611
25612 valid |= VALID_PATH;
25613 *usedlen += 2;
25614
25615 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25616 if ((*fnamep)[0] == '~'
25617#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25618 && ((*fnamep)[1] == '/'
25619# ifdef BACKSLASH_IN_FILENAME
25620 || (*fnamep)[1] == '\\'
25621# endif
25622 || (*fnamep)[1] == NUL)
25623
25624#endif
25625 )
25626 {
25627 *fnamep = expand_env_save(*fnamep);
25628 vim_free(*bufp); /* free any allocated file name */
25629 *bufp = *fnamep;
25630 if (*fnamep == NULL)
25631 return -1;
25632 }
25633
25634 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025635 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 {
25637 if (vim_ispathsep(*p)
25638 && p[1] == '.'
25639 && (p[2] == NUL
25640 || vim_ispathsep(p[2])
25641 || (p[2] == '.'
25642 && (p[3] == NUL || vim_ispathsep(p[3])))))
25643 break;
25644 }
25645
25646 /* FullName_save() is slow, don't use it when not needed. */
25647 if (*p != NUL || !vim_isAbsName(*fnamep))
25648 {
25649 *fnamep = FullName_save(*fnamep, *p != NUL);
25650 vim_free(*bufp); /* free any allocated file name */
25651 *bufp = *fnamep;
25652 if (*fnamep == NULL)
25653 return -1;
25654 }
25655
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025656#ifdef WIN3264
25657# if _WIN32_WINNT >= 0x0500
25658 if (vim_strchr(*fnamep, '~') != NULL)
25659 {
25660 /* Expand 8.3 filename to full path. Needed to make sure the same
25661 * file does not have two different names.
25662 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25663 p = alloc(_MAX_PATH + 1);
25664 if (p != NULL)
25665 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025666 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025667 {
25668 vim_free(*bufp);
25669 *bufp = *fnamep = p;
25670 }
25671 else
25672 vim_free(p);
25673 }
25674 }
25675# endif
25676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025677 /* Append a path separator to a directory. */
25678 if (mch_isdir(*fnamep))
25679 {
25680 /* Make room for one or two extra characters. */
25681 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25682 vim_free(*bufp); /* free any allocated file name */
25683 *bufp = *fnamep;
25684 if (*fnamep == NULL)
25685 return -1;
25686 add_pathsep(*fnamep);
25687 }
25688 }
25689
25690 /* ":." - path relative to the current directory */
25691 /* ":~" - path relative to the home directory */
25692 /* ":8" - shortname path - postponed till after */
25693 while (src[*usedlen] == ':'
25694 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25695 {
25696 *usedlen += 2;
25697 if (c == '8')
25698 {
25699#ifdef WIN3264
25700 has_shortname = 1; /* Postpone this. */
25701#endif
25702 continue;
25703 }
25704 pbuf = NULL;
25705 /* Need full path first (use expand_env() to remove a "~/") */
25706 if (!has_fullname)
25707 {
25708 if (c == '.' && **fnamep == '~')
25709 p = pbuf = expand_env_save(*fnamep);
25710 else
25711 p = pbuf = FullName_save(*fnamep, FALSE);
25712 }
25713 else
25714 p = *fnamep;
25715
25716 has_fullname = 0;
25717
25718 if (p != NULL)
25719 {
25720 if (c == '.')
25721 {
25722 mch_dirname(dirname, MAXPATHL);
25723 s = shorten_fname(p, dirname);
25724 if (s != NULL)
25725 {
25726 *fnamep = s;
25727 if (pbuf != NULL)
25728 {
25729 vim_free(*bufp); /* free any allocated file name */
25730 *bufp = pbuf;
25731 pbuf = NULL;
25732 }
25733 }
25734 }
25735 else
25736 {
25737 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25738 /* Only replace it when it starts with '~' */
25739 if (*dirname == '~')
25740 {
25741 s = vim_strsave(dirname);
25742 if (s != NULL)
25743 {
25744 *fnamep = s;
25745 vim_free(*bufp);
25746 *bufp = s;
25747 }
25748 }
25749 }
25750 vim_free(pbuf);
25751 }
25752 }
25753
25754 tail = gettail(*fnamep);
25755 *fnamelen = (int)STRLEN(*fnamep);
25756
25757 /* ":h" - head, remove "/file_name", can be repeated */
25758 /* Don't remove the first "/" or "c:\" */
25759 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25760 {
25761 valid |= VALID_HEAD;
25762 *usedlen += 2;
25763 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025764 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025765 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766 *fnamelen = (int)(tail - *fnamep);
25767#ifdef VMS
25768 if (*fnamelen > 0)
25769 *fnamelen += 1; /* the path separator is part of the path */
25770#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025771 if (*fnamelen == 0)
25772 {
25773 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25774 p = vim_strsave((char_u *)".");
25775 if (p == NULL)
25776 return -1;
25777 vim_free(*bufp);
25778 *bufp = *fnamep = tail = p;
25779 *fnamelen = 1;
25780 }
25781 else
25782 {
25783 while (tail > s && !after_pathsep(s, tail))
25784 mb_ptr_back(*fnamep, tail);
25785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786 }
25787
25788 /* ":8" - shortname */
25789 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25790 {
25791 *usedlen += 2;
25792#ifdef WIN3264
25793 has_shortname = 1;
25794#endif
25795 }
25796
25797#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025798 /*
25799 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025800 */
25801 if (has_shortname)
25802 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025803 /* Copy the string if it is shortened by :h and when it wasn't copied
25804 * yet, because we are going to change it in place. Avoids changing
25805 * the buffer name for "%:8". */
25806 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025807 {
25808 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025809 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025810 return -1;
25811 vim_free(*bufp);
25812 *bufp = *fnamep = p;
25813 }
25814
25815 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025816 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025817 if (!has_fullname && !vim_isAbsName(*fnamep))
25818 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025819 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025820 return -1;
25821 }
25822 else
25823 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025824 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825
Bram Moolenaardc935552011-08-17 15:23:23 +020025826 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025827 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025828 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829 return -1;
25830
25831 if (l == 0)
25832 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025833 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025835 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836 return -1;
25837 }
25838 *fnamelen = l;
25839 }
25840 }
25841#endif /* WIN3264 */
25842
25843 /* ":t" - tail, just the basename */
25844 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25845 {
25846 *usedlen += 2;
25847 *fnamelen -= (int)(tail - *fnamep);
25848 *fnamep = tail;
25849 }
25850
25851 /* ":e" - extension, can be repeated */
25852 /* ":r" - root, without extension, can be repeated */
25853 while (src[*usedlen] == ':'
25854 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25855 {
25856 /* find a '.' in the tail:
25857 * - for second :e: before the current fname
25858 * - otherwise: The last '.'
25859 */
25860 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25861 s = *fnamep - 2;
25862 else
25863 s = *fnamep + *fnamelen - 1;
25864 for ( ; s > tail; --s)
25865 if (s[0] == '.')
25866 break;
25867 if (src[*usedlen + 1] == 'e') /* :e */
25868 {
25869 if (s > tail)
25870 {
25871 *fnamelen += (int)(*fnamep - (s + 1));
25872 *fnamep = s + 1;
25873#ifdef VMS
25874 /* cut version from the extension */
25875 s = *fnamep + *fnamelen - 1;
25876 for ( ; s > *fnamep; --s)
25877 if (s[0] == ';')
25878 break;
25879 if (s > *fnamep)
25880 *fnamelen = s - *fnamep;
25881#endif
25882 }
25883 else if (*fnamep <= tail)
25884 *fnamelen = 0;
25885 }
25886 else /* :r */
25887 {
25888 if (s > tail) /* remove one extension */
25889 *fnamelen = (int)(s - *fnamep);
25890 }
25891 *usedlen += 2;
25892 }
25893
25894 /* ":s?pat?foo?" - substitute */
25895 /* ":gs?pat?foo?" - global substitute */
25896 if (src[*usedlen] == ':'
25897 && (src[*usedlen + 1] == 's'
25898 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25899 {
25900 char_u *str;
25901 char_u *pat;
25902 char_u *sub;
25903 int sep;
25904 char_u *flags;
25905 int didit = FALSE;
25906
25907 flags = (char_u *)"";
25908 s = src + *usedlen + 2;
25909 if (src[*usedlen + 1] == 'g')
25910 {
25911 flags = (char_u *)"g";
25912 ++s;
25913 }
25914
25915 sep = *s++;
25916 if (sep)
25917 {
25918 /* find end of pattern */
25919 p = vim_strchr(s, sep);
25920 if (p != NULL)
25921 {
25922 pat = vim_strnsave(s, (int)(p - s));
25923 if (pat != NULL)
25924 {
25925 s = p + 1;
25926 /* find end of substitution */
25927 p = vim_strchr(s, sep);
25928 if (p != NULL)
25929 {
25930 sub = vim_strnsave(s, (int)(p - s));
25931 str = vim_strnsave(*fnamep, *fnamelen);
25932 if (sub != NULL && str != NULL)
25933 {
25934 *usedlen = (int)(p + 1 - src);
25935 s = do_string_sub(str, pat, sub, flags);
25936 if (s != NULL)
25937 {
25938 *fnamep = s;
25939 *fnamelen = (int)STRLEN(s);
25940 vim_free(*bufp);
25941 *bufp = s;
25942 didit = TRUE;
25943 }
25944 }
25945 vim_free(sub);
25946 vim_free(str);
25947 }
25948 vim_free(pat);
25949 }
25950 }
25951 /* after using ":s", repeat all the modifiers */
25952 if (didit)
25953 goto repeat;
25954 }
25955 }
25956
Bram Moolenaar26df0922014-02-23 23:39:13 +010025957 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25958 {
25959 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25960 if (p == NULL)
25961 return -1;
25962 vim_free(*bufp);
25963 *bufp = *fnamep = p;
25964 *fnamelen = (int)STRLEN(p);
25965 *usedlen += 2;
25966 }
25967
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968 return valid;
25969}
25970
25971/*
25972 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25973 * "flags" can be "g" to do a global substitute.
25974 * Returns an allocated string, NULL for error.
25975 */
25976 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025977do_string_sub(
25978 char_u *str,
25979 char_u *pat,
25980 char_u *sub,
25981 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025982{
25983 int sublen;
25984 regmatch_T regmatch;
25985 int i;
25986 int do_all;
25987 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025988 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989 garray_T ga;
25990 char_u *ret;
25991 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025992 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993
25994 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25995 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025996 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997
25998 ga_init2(&ga, 1, 200);
25999
26000 do_all = (flags[0] == 'g');
26001
26002 regmatch.rm_ic = p_ic;
26003 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26004 if (regmatch.regprog != NULL)
26005 {
26006 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026007 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026008 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26009 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026010 /* Skip empty match except for first match. */
26011 if (regmatch.startp[0] == regmatch.endp[0])
26012 {
26013 if (zero_width == regmatch.startp[0])
26014 {
26015 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026016 i = MB_PTR2LEN(tail);
26017 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26018 (size_t)i);
26019 ga.ga_len += i;
26020 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026021 continue;
26022 }
26023 zero_width = regmatch.startp[0];
26024 }
26025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026 /*
26027 * Get some space for a temporary buffer to do the substitution
26028 * into. It will contain:
26029 * - The text up to where the match is.
26030 * - The substituted text.
26031 * - The text after the match.
26032 */
26033 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026034 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26036 {
26037 ga_clear(&ga);
26038 break;
26039 }
26040
26041 /* copy the text up to where the match is */
26042 i = (int)(regmatch.startp[0] - tail);
26043 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26044 /* add the substituted text */
26045 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26046 + ga.ga_len + i, TRUE, TRUE, FALSE);
26047 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026048 tail = regmatch.endp[0];
26049 if (*tail == NUL)
26050 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051 if (!do_all)
26052 break;
26053 }
26054
26055 if (ga.ga_data != NULL)
26056 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26057
Bram Moolenaar473de612013-06-08 18:19:48 +020026058 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026059 }
26060
26061 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26062 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026063 if (p_cpo == empty_option)
26064 p_cpo = save_cpo;
26065 else
26066 /* Darn, evaluating {sub} expression changed the value. */
26067 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068
26069 return ret;
26070}
26071
26072#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */