blob: 017decc4af1ee39bd4897bbf1ef877c22702aff1 [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 Moolenaar81661fb2016-02-18 22:23:34 +0100507static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
509static void f_ch_open(typval_T *argvars, typval_T *rettv);
510static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100511static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
512static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100513static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100514static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100516static void f_changenr(typval_T *argvars, typval_T *rettv);
517static void f_char2nr(typval_T *argvars, typval_T *rettv);
518static void f_cindent(typval_T *argvars, typval_T *rettv);
519static void f_clearmatches(typval_T *argvars, typval_T *rettv);
520static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000521#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100522static void f_complete(typval_T *argvars, typval_T *rettv);
523static void f_complete_add(typval_T *argvars, typval_T *rettv);
524static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000525#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100526static void f_confirm(typval_T *argvars, typval_T *rettv);
527static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100529static void f_cos(typval_T *argvars, typval_T *rettv);
530static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000531#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100532static void f_count(typval_T *argvars, typval_T *rettv);
533static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
534static void f_cursor(typval_T *argsvars, typval_T *rettv);
535static void f_deepcopy(typval_T *argvars, typval_T *rettv);
536static void f_delete(typval_T *argvars, typval_T *rettv);
537static void f_did_filetype(typval_T *argvars, typval_T *rettv);
538static void f_diff_filler(typval_T *argvars, typval_T *rettv);
539static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100540static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_empty(typval_T *argvars, typval_T *rettv);
542static void f_escape(typval_T *argvars, typval_T *rettv);
543static void f_eval(typval_T *argvars, typval_T *rettv);
544static void f_eventhandler(typval_T *argvars, typval_T *rettv);
545static void f_executable(typval_T *argvars, typval_T *rettv);
546static void f_exepath(typval_T *argvars, typval_T *rettv);
547static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200550#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_expand(typval_T *argvars, typval_T *rettv);
552static void f_extend(typval_T *argvars, typval_T *rettv);
553static void f_feedkeys(typval_T *argvars, typval_T *rettv);
554static void f_filereadable(typval_T *argvars, typval_T *rettv);
555static void f_filewritable(typval_T *argvars, typval_T *rettv);
556static void f_filter(typval_T *argvars, typval_T *rettv);
557static void f_finddir(typval_T *argvars, typval_T *rettv);
558static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000559#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100560static void f_float2nr(typval_T *argvars, typval_T *rettv);
561static void f_floor(typval_T *argvars, typval_T *rettv);
562static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000563#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_fnameescape(typval_T *argvars, typval_T *rettv);
565static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
566static void f_foldclosed(typval_T *argvars, typval_T *rettv);
567static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
568static void f_foldlevel(typval_T *argvars, typval_T *rettv);
569static void f_foldtext(typval_T *argvars, typval_T *rettv);
570static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
571static void f_foreground(typval_T *argvars, typval_T *rettv);
572static void f_function(typval_T *argvars, typval_T *rettv);
573static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
574static void f_get(typval_T *argvars, typval_T *rettv);
575static void f_getbufline(typval_T *argvars, typval_T *rettv);
576static void f_getbufvar(typval_T *argvars, typval_T *rettv);
577static void f_getchar(typval_T *argvars, typval_T *rettv);
578static void f_getcharmod(typval_T *argvars, typval_T *rettv);
579static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
580static void f_getcmdline(typval_T *argvars, typval_T *rettv);
581static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
582static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
583static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
584static void f_getcwd(typval_T *argvars, typval_T *rettv);
585static void f_getfontname(typval_T *argvars, typval_T *rettv);
586static void f_getfperm(typval_T *argvars, typval_T *rettv);
587static void f_getfsize(typval_T *argvars, typval_T *rettv);
588static void f_getftime(typval_T *argvars, typval_T *rettv);
589static void f_getftype(typval_T *argvars, typval_T *rettv);
590static void f_getline(typval_T *argvars, typval_T *rettv);
591static void f_getmatches(typval_T *argvars, typval_T *rettv);
592static void f_getpid(typval_T *argvars, typval_T *rettv);
593static void f_getcurpos(typval_T *argvars, typval_T *rettv);
594static void f_getpos(typval_T *argvars, typval_T *rettv);
595static void f_getqflist(typval_T *argvars, typval_T *rettv);
596static void f_getreg(typval_T *argvars, typval_T *rettv);
597static void f_getregtype(typval_T *argvars, typval_T *rettv);
598static void f_gettabvar(typval_T *argvars, typval_T *rettv);
599static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
600static void f_getwinposx(typval_T *argvars, typval_T *rettv);
601static void f_getwinposy(typval_T *argvars, typval_T *rettv);
602static void f_getwinvar(typval_T *argvars, typval_T *rettv);
603static void f_glob(typval_T *argvars, typval_T *rettv);
604static void f_globpath(typval_T *argvars, typval_T *rettv);
605static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
606static void f_has(typval_T *argvars, typval_T *rettv);
607static void f_has_key(typval_T *argvars, typval_T *rettv);
608static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
609static void f_hasmapto(typval_T *argvars, typval_T *rettv);
610static void f_histadd(typval_T *argvars, typval_T *rettv);
611static void f_histdel(typval_T *argvars, typval_T *rettv);
612static void f_histget(typval_T *argvars, typval_T *rettv);
613static void f_histnr(typval_T *argvars, typval_T *rettv);
614static void f_hlID(typval_T *argvars, typval_T *rettv);
615static void f_hlexists(typval_T *argvars, typval_T *rettv);
616static void f_hostname(typval_T *argvars, typval_T *rettv);
617static void f_iconv(typval_T *argvars, typval_T *rettv);
618static void f_indent(typval_T *argvars, typval_T *rettv);
619static void f_index(typval_T *argvars, typval_T *rettv);
620static void f_input(typval_T *argvars, typval_T *rettv);
621static void f_inputdialog(typval_T *argvars, typval_T *rettv);
622static void f_inputlist(typval_T *argvars, typval_T *rettv);
623static void f_inputrestore(typval_T *argvars, typval_T *rettv);
624static void f_inputsave(typval_T *argvars, typval_T *rettv);
625static void f_inputsecret(typval_T *argvars, typval_T *rettv);
626static void f_insert(typval_T *argvars, typval_T *rettv);
627static void f_invert(typval_T *argvars, typval_T *rettv);
628static void f_isdirectory(typval_T *argvars, typval_T *rettv);
629static void f_islocked(typval_T *argvars, typval_T *rettv);
630static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100631#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100632# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100633static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100634# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +0100635static void f_job_start(typval_T *argvars, typval_T *rettv);
636static void f_job_stop(typval_T *argvars, typval_T *rettv);
637static void f_job_status(typval_T *argvars, typval_T *rettv);
638#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100639static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100640static void f_js_decode(typval_T *argvars, typval_T *rettv);
641static void f_js_encode(typval_T *argvars, typval_T *rettv);
642static void f_json_decode(typval_T *argvars, typval_T *rettv);
643static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_keys(typval_T *argvars, typval_T *rettv);
645static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
646static void f_len(typval_T *argvars, typval_T *rettv);
647static void f_libcall(typval_T *argvars, typval_T *rettv);
648static void f_libcallnr(typval_T *argvars, typval_T *rettv);
649static void f_line(typval_T *argvars, typval_T *rettv);
650static void f_line2byte(typval_T *argvars, typval_T *rettv);
651static void f_lispindent(typval_T *argvars, typval_T *rettv);
652static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000653#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100654static void f_log(typval_T *argvars, typval_T *rettv);
655static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000656#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200657#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200659#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_map(typval_T *argvars, typval_T *rettv);
661static void f_maparg(typval_T *argvars, typval_T *rettv);
662static void f_mapcheck(typval_T *argvars, typval_T *rettv);
663static void f_match(typval_T *argvars, typval_T *rettv);
664static void f_matchadd(typval_T *argvars, typval_T *rettv);
665static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
666static void f_matcharg(typval_T *argvars, typval_T *rettv);
667static void f_matchdelete(typval_T *argvars, typval_T *rettv);
668static void f_matchend(typval_T *argvars, typval_T *rettv);
669static void f_matchlist(typval_T *argvars, typval_T *rettv);
670static void f_matchstr(typval_T *argvars, typval_T *rettv);
671static void f_max(typval_T *argvars, typval_T *rettv);
672static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000673#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000675#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100677#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
681static void f_nr2char(typval_T *argvars, typval_T *rettv);
682static void f_or(typval_T *argvars, typval_T *rettv);
683static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100684#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100686#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000687#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
691static void f_printf(typval_T *argvars, typval_T *rettv);
692static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200693#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200695#endif
696#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200698#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_range(typval_T *argvars, typval_T *rettv);
700static void f_readfile(typval_T *argvars, typval_T *rettv);
701static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100702#ifdef FEAT_FLOAT
703static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
704#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_reltimestr(typval_T *argvars, typval_T *rettv);
706static void f_remote_expr(typval_T *argvars, typval_T *rettv);
707static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
708static void f_remote_peek(typval_T *argvars, typval_T *rettv);
709static void f_remote_read(typval_T *argvars, typval_T *rettv);
710static void f_remote_send(typval_T *argvars, typval_T *rettv);
711static void f_remove(typval_T *argvars, typval_T *rettv);
712static void f_rename(typval_T *argvars, typval_T *rettv);
713static void f_repeat(typval_T *argvars, typval_T *rettv);
714static void f_resolve(typval_T *argvars, typval_T *rettv);
715static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100717static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_screenattr(typval_T *argvars, typval_T *rettv);
720static void f_screenchar(typval_T *argvars, typval_T *rettv);
721static void f_screencol(typval_T *argvars, typval_T *rettv);
722static void f_screenrow(typval_T *argvars, typval_T *rettv);
723static void f_search(typval_T *argvars, typval_T *rettv);
724static void f_searchdecl(typval_T *argvars, typval_T *rettv);
725static void f_searchpair(typval_T *argvars, typval_T *rettv);
726static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
727static void f_searchpos(typval_T *argvars, typval_T *rettv);
728static void f_server2client(typval_T *argvars, typval_T *rettv);
729static void f_serverlist(typval_T *argvars, typval_T *rettv);
730static void f_setbufvar(typval_T *argvars, typval_T *rettv);
731static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
732static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
733static void f_setline(typval_T *argvars, typval_T *rettv);
734static void f_setloclist(typval_T *argvars, typval_T *rettv);
735static void f_setmatches(typval_T *argvars, typval_T *rettv);
736static void f_setpos(typval_T *argvars, typval_T *rettv);
737static void f_setqflist(typval_T *argvars, typval_T *rettv);
738static void f_setreg(typval_T *argvars, typval_T *rettv);
739static void f_settabvar(typval_T *argvars, typval_T *rettv);
740static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
741static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100742#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100744#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100745static void f_shellescape(typval_T *argvars, typval_T *rettv);
746static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
747static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000748#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_sin(typval_T *argvars, typval_T *rettv);
750static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_sort(typval_T *argvars, typval_T *rettv);
753static void f_soundfold(typval_T *argvars, typval_T *rettv);
754static void f_spellbadword(typval_T *argvars, typval_T *rettv);
755static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
756static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000757#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sqrt(typval_T *argvars, typval_T *rettv);
759static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000760#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_str2nr(typval_T *argvars, typval_T *rettv);
762static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000763#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000765#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_stridx(typval_T *argvars, typval_T *rettv);
767static void f_string(typval_T *argvars, typval_T *rettv);
768static void f_strlen(typval_T *argvars, typval_T *rettv);
769static void f_strpart(typval_T *argvars, typval_T *rettv);
770static void f_strridx(typval_T *argvars, typval_T *rettv);
771static void f_strtrans(typval_T *argvars, typval_T *rettv);
772static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
773static void f_strwidth(typval_T *argvars, typval_T *rettv);
774static void f_submatch(typval_T *argvars, typval_T *rettv);
775static void f_substitute(typval_T *argvars, typval_T *rettv);
776static void f_synID(typval_T *argvars, typval_T *rettv);
777static void f_synIDattr(typval_T *argvars, typval_T *rettv);
778static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
779static void f_synstack(typval_T *argvars, typval_T *rettv);
780static void f_synconcealed(typval_T *argvars, typval_T *rettv);
781static void f_system(typval_T *argvars, typval_T *rettv);
782static void f_systemlist(typval_T *argvars, typval_T *rettv);
783static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
784static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
785static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
786static void f_taglist(typval_T *argvars, typval_T *rettv);
787static void f_tagfiles(typval_T *argvars, typval_T *rettv);
788static void f_tempname(typval_T *argvars, typval_T *rettv);
789static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200790#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100791static void f_tan(typval_T *argvars, typval_T *rettv);
792static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200793#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_tolower(typval_T *argvars, typval_T *rettv);
795static void f_toupper(typval_T *argvars, typval_T *rettv);
796static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000797#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100798static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000799#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_type(typval_T *argvars, typval_T *rettv);
801static void f_undofile(typval_T *argvars, typval_T *rettv);
802static void f_undotree(typval_T *argvars, typval_T *rettv);
803static void f_uniq(typval_T *argvars, typval_T *rettv);
804static void f_values(typval_T *argvars, typval_T *rettv);
805static void f_virtcol(typval_T *argvars, typval_T *rettv);
806static void f_visualmode(typval_T *argvars, typval_T *rettv);
807static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
808static void f_winbufnr(typval_T *argvars, typval_T *rettv);
809static void f_wincol(typval_T *argvars, typval_T *rettv);
810static void f_winheight(typval_T *argvars, typval_T *rettv);
811static void f_winline(typval_T *argvars, typval_T *rettv);
812static void f_winnr(typval_T *argvars, typval_T *rettv);
813static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
814static void f_winrestview(typval_T *argvars, typval_T *rettv);
815static void f_winsaveview(typval_T *argvars, typval_T *rettv);
816static void f_winwidth(typval_T *argvars, typval_T *rettv);
817static void f_writefile(typval_T *argvars, typval_T *rettv);
818static void f_wordcount(typval_T *argvars, typval_T *rettv);
819static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000820
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100821static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
822static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
823static int get_env_len(char_u **arg);
824static int get_id_len(char_u **arg);
825static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
826static 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 +0000827#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
828#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
829 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100830static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
831static int eval_isnamec(int c);
832static int eval_isnamec1(int c);
833static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
834static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static typval_T *alloc_string_tv(char_u *string);
836static void init_tv(typval_T *varp);
837static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100838#ifdef FEAT_FLOAT
839static float_T get_tv_float(typval_T *varp);
840#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static linenr_T get_tv_lnum(typval_T *argvars);
842static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
843static char_u *get_tv_string(typval_T *varp);
844static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
845static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
846static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
847static hashtab_T *find_var_ht(char_u *name, char_u **varname);
848static funccall_T *get_funccal(void);
849static void vars_clear_ext(hashtab_T *ht, int free_val);
850static void delete_var(hashtab_T *ht, hashitem_T *hi);
851static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
852static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
853static void set_var(char_u *name, typval_T *varp, int copy);
854static int var_check_ro(int flags, char_u *name, int use_gettext);
855static int var_check_fixed(int flags, char_u *name, int use_gettext);
856static int var_check_func_name(char_u *name, int new_var);
857static int valid_varname(char_u *varname);
858static int tv_check_lock(int lock, char_u *name, int use_gettext);
859static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
860static char_u *find_option_end(char_u **arg, int *opt_flags);
861static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
862static int eval_fname_script(char_u *p);
863static int eval_fname_sid(char_u *p);
864static void list_func_head(ufunc_T *fp, int indent);
865static ufunc_T *find_func(char_u *name);
866static int function_exists(char_u *name);
867static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static void func_do_profile(ufunc_T *fp);
870static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
871static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000872static int
873# ifdef __BORLANDC__
874 _RTLENTRYF
875# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100876 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000877static int
878# ifdef __BORLANDC__
879 _RTLENTRYF
880# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000882#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883static int script_autoload(char_u *name, int reload);
884static char_u *autoload_name(char_u *name);
885static void cat_func_name(char_u *buf, ufunc_T *fp);
886static void func_free(ufunc_T *fp);
887static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
888static int can_free_funccal(funccall_T *fc, int copyID) ;
889static void free_funccal(funccall_T *fc, int free_val);
890static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
891static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
892static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
893static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
894static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
895static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
896static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
897static int write_list(FILE *fd, list_T *list, int binary);
898static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000899
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200900
901#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100902static int compare_func_name(const void *s1, const void *s2);
903static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200904#endif
905
Bram Moolenaar33570922005-01-25 22:26:29 +0000906/*
907 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000908 */
909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100910eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000911{
Bram Moolenaar33570922005-01-25 22:26:29 +0000912 int i;
913 struct vimvar *p;
914
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200915 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
916 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200917 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000918 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000919 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000920
921 for (i = 0; i < VV_LEN; ++i)
922 {
923 p = &vimvars[i];
924 STRCPY(p->vv_di.di_key, p->vv_name);
925 if (p->vv_flags & VV_RO)
926 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
927 else if (p->vv_flags & VV_RO_SBX)
928 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
929 else
930 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000931
932 /* add to v: scope dict, unless the value is not always available */
933 if (p->vv_type != VAR_UNKNOWN)
934 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000935 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000936 /* add to compat scope dict */
937 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100939 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
940
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100942 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200943 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100944 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100945
946 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
947 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
948 set_vim_var_nr(VV_NONE, VVAL_NONE);
949 set_vim_var_nr(VV_NULL, VVAL_NULL);
950
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200951 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200952
953#ifdef EBCDIC
954 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100955 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200956 */
957 sortFunctions();
958#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000959}
960
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000961#if defined(EXITFREE) || defined(PROTO)
962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100963eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000964{
965 int i;
966 struct vimvar *p;
967
968 for (i = 0; i < VV_LEN; ++i)
969 {
970 p = &vimvars[i];
971 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000972 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000973 vim_free(p->vv_str);
974 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000975 }
976 else if (p->vv_di.di_tv.v_type == VAR_LIST)
977 {
978 list_unref(p->vv_list);
979 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000980 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000981 }
982 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000983 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000984 hash_clear(&compat_hashtab);
985
Bram Moolenaard9fba312005-06-26 22:34:35 +0000986 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100987# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200988 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100989# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990
991 /* global variables */
992 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000993
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000994 /* autoloaded script names */
995 ga_clear_strings(&ga_loaded);
996
Bram Moolenaarcca74132013-09-25 21:00:28 +0200997 /* Script-local variables. First clear all the variables and in a second
998 * loop free the scriptvar_T, because a variable in one script might hold
999 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001000 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001001 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001002 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001003 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001004 ga_clear(&ga_scripts);
1005
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001006 /* unreferenced lists and dicts */
1007 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001008
1009 /* functions */
1010 free_all_functions();
1011 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001012}
1013#endif
1014
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 * Return the name of the executed function.
1017 */
1018 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001019func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022}
1023
1024/*
1025 * Return the address holding the next breakpoint line for a funccall cookie.
1026 */
1027 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001028func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar33570922005-01-25 22:26:29 +00001030 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/*
1034 * Return the address holding the debug tick for a funccall cookie.
1035 */
1036 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001037func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
Bram Moolenaar33570922005-01-25 22:26:29 +00001039 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
1041
1042/*
1043 * Return the nesting level for a funccall cookie.
1044 */
1045 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001046func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar33570922005-01-25 22:26:29 +00001048 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
1050
1051/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001052funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001054/* pointer to list of previously used funccal, still around because some
1055 * item in it is still being used. */
1056funccall_T *previous_funccal = NULL;
1057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058/*
1059 * Return TRUE when a function was ended by a ":return" command.
1060 */
1061 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001062current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063{
1064 return current_funccal->returned;
1065}
1066
1067
1068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 * Set an internal variable to a string value. Creates the variable if it does
1070 * not already exist.
1071 */
1072 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001073set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001075 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001076 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
1078 val = vim_strsave(value);
1079 if (val != NULL)
1080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001081 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001082 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001084 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001085 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 }
1088}
1089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001091static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092static char_u *redir_endp = NULL;
1093static char_u *redir_varname = NULL;
1094
1095/*
1096 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001097 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 * Returns OK if successfully completed the setup. FAIL otherwise.
1099 */
1100 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001101var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102{
1103 int save_emsg;
1104 int err;
1105 typval_T tv;
1106
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001108 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 {
1110 EMSG(_(e_invarg));
1111 return FAIL;
1112 }
1113
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001114 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 redir_varname = vim_strsave(name);
1116 if (redir_varname == NULL)
1117 return FAIL;
1118
1119 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1120 if (redir_lval == NULL)
1121 {
1122 var_redir_stop();
1123 return FAIL;
1124 }
1125
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 /* The output is stored in growarray "redir_ga" until redirection ends. */
1127 ga_init2(&redir_ga, (int)sizeof(char), 500);
1128
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001130 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001131 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1133 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001134 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 if (redir_endp != NULL && *redir_endp != NUL)
1136 /* Trailing characters are present after the variable name */
1137 EMSG(_(e_trailing));
1138 else
1139 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001140 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 var_redir_stop();
1142 return FAIL;
1143 }
1144
1145 /* check if we can write to the variable: set it to or append an empty
1146 * string */
1147 save_emsg = did_emsg;
1148 did_emsg = FALSE;
1149 tv.v_type = VAR_STRING;
1150 tv.vval.v_string = (char_u *)"";
1151 if (append)
1152 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1153 else
1154 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001155 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001157 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 if (err)
1159 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001160 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 var_redir_stop();
1162 return FAIL;
1163 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164
1165 return OK;
1166}
1167
1168/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001169 * Append "value[value_len]" to the variable set by var_redir_start().
1170 * The actual appending is postponed until redirection ends, because the value
1171 * appended may in fact be the string we write to, changing it may cause freed
1172 * memory to be used:
1173 * :redir => foo
1174 * :let foo
1175 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 */
1177 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001178var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001180 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181
1182 if (redir_lval == NULL)
1183 return;
1184
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001185 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001186 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001190 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001191 {
1192 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001193 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001194 }
1195 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197}
1198
1199/*
1200 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 */
1203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001204var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001206 typval_T tv;
1207
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208 if (redir_lval != NULL)
1209 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001210 /* If there was no error: assign the text to the variable. */
1211 if (redir_endp != NULL)
1212 {
1213 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1214 tv.v_type = VAR_STRING;
1215 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001216 /* Call get_lval() again, if it's inside a Dict or List it may
1217 * have changed. */
1218 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001219 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001220 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1221 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1222 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001223 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001224
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001225 /* free the collected output */
1226 vim_free(redir_ga.ga_data);
1227 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001228
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229 vim_free(redir_lval);
1230 redir_lval = NULL;
1231 }
1232 vim_free(redir_varname);
1233 redir_varname = NULL;
1234}
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236# if defined(FEAT_MBYTE) || defined(PROTO)
1237 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001238eval_charconvert(
1239 char_u *enc_from,
1240 char_u *enc_to,
1241 char_u *fname_from,
1242 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243{
1244 int err = FALSE;
1245
1246 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1247 set_vim_var_string(VV_CC_TO, enc_to, -1);
1248 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1249 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1250 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1251 err = TRUE;
1252 set_vim_var_string(VV_CC_FROM, NULL, -1);
1253 set_vim_var_string(VV_CC_TO, NULL, -1);
1254 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1255 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1256
1257 if (err)
1258 return FAIL;
1259 return OK;
1260}
1261# endif
1262
1263# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1264 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001265eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266{
1267 int err = FALSE;
1268
1269 set_vim_var_string(VV_FNAME_IN, fname, -1);
1270 set_vim_var_string(VV_CMDARG, args, -1);
1271 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1272 err = TRUE;
1273 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1274 set_vim_var_string(VV_CMDARG, NULL, -1);
1275
1276 if (err)
1277 {
1278 mch_remove(fname);
1279 return FAIL;
1280 }
1281 return OK;
1282}
1283# endif
1284
1285# if defined(FEAT_DIFF) || defined(PROTO)
1286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287eval_diff(
1288 char_u *origfile,
1289 char_u *newfile,
1290 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
1292 int err = FALSE;
1293
1294 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1295 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1296 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1297 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1298 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1299 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1300 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1301}
1302
1303 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001304eval_patch(
1305 char_u *origfile,
1306 char_u *difffile,
1307 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308{
1309 int err;
1310
1311 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1312 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1313 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1314 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1315 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1316 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1317 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1318}
1319# endif
1320
1321/*
1322 * Top level evaluation function, returning a boolean.
1323 * Sets "error" to TRUE if there was an error.
1324 * Return TRUE or FALSE.
1325 */
1326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327eval_to_bool(
1328 char_u *arg,
1329 int *error,
1330 char_u **nextcmd,
1331 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 int retval = FALSE;
1335
1336 if (skip)
1337 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001338 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 else
1341 {
1342 *error = FALSE;
1343 if (!skip)
1344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001345 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348 }
1349 if (skip)
1350 --emsg_skip;
1351
1352 return retval;
1353}
1354
1355/*
1356 * Top level evaluation function, returning a string. If "skip" is TRUE,
1357 * only parsing to "nextcmd" is done, without reporting errors. Return
1358 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1359 */
1360 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001361eval_to_string_skip(
1362 char_u *arg,
1363 char_u **nextcmd,
1364 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
Bram Moolenaar33570922005-01-25 22:26:29 +00001366 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 char_u *retval;
1368
1369 if (skip)
1370 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 retval = NULL;
1373 else
1374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 retval = vim_strsave(get_tv_string(&tv));
1376 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378 if (skip)
1379 --emsg_skip;
1380
1381 return retval;
1382}
1383
1384/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001385 * Skip over an expression at "*pp".
1386 * Return FAIL for an error, OK otherwise.
1387 */
1388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001389skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390{
Bram Moolenaar33570922005-01-25 22:26:29 +00001391 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001392
1393 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001395}
1396
1397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001399 * When "convert" is TRUE convert a List into a sequence of lines and convert
1400 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 * Return pointer to allocated memory, or NULL for failure.
1402 */
1403 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001404eval_to_string(
1405 char_u *arg,
1406 char_u **nextcmd,
1407 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001411 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001412#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001413 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 retval = NULL;
1418 else
1419 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001420 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001421 {
1422 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001423 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001424 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001425 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001426 if (tv.vval.v_list->lv_len > 0)
1427 ga_append(&ga, NL);
1428 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001429 ga_append(&ga, NUL);
1430 retval = (char_u *)ga.ga_data;
1431 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001432#ifdef FEAT_FLOAT
1433 else if (convert && tv.v_type == VAR_FLOAT)
1434 {
1435 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1436 retval = vim_strsave(numbuf);
1437 }
1438#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001439 else
1440 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001441 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443
1444 return retval;
1445}
1446
1447/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001448 * Call eval_to_string() without using current local variables and using
1449 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 */
1451 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001452eval_to_string_safe(
1453 char_u *arg,
1454 char_u **nextcmd,
1455 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 char_u *retval;
1458 void *save_funccalp;
1459
1460 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001461 if (use_sandbox)
1462 ++sandbox;
1463 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001464 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001465 if (use_sandbox)
1466 --sandbox;
1467 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 restore_funccal(save_funccalp);
1469 return retval;
1470}
1471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472/*
1473 * Top level evaluation function, returning a number.
1474 * Evaluates "expr" silently.
1475 * Returns -1 for an error.
1476 */
1477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001478eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
Bram Moolenaar33570922005-01-25 22:26:29 +00001480 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001482 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 ++emsg_off;
1485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 retval = -1;
1488 else
1489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001490 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
1493 --emsg_off;
1494
1495 return retval;
1496}
1497
Bram Moolenaara40058a2005-07-11 22:42:07 +00001498/*
1499 * Prepare v: variable "idx" to be used.
1500 * Save the current typeval in "save_tv".
1501 * When not used yet add the variable to the v: hashtable.
1502 */
1503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001505{
1506 *save_tv = vimvars[idx].vv_tv;
1507 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1508 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1509}
1510
1511/*
1512 * Restore v: variable "idx" to typeval "save_tv".
1513 * When no longer defined, remove the variable from the v: hashtable.
1514 */
1515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001516restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001517{
1518 hashitem_T *hi;
1519
Bram Moolenaara40058a2005-07-11 22:42:07 +00001520 vimvars[idx].vv_tv = *save_tv;
1521 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1522 {
1523 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1524 if (HASHITEM_EMPTY(hi))
1525 EMSG2(_(e_intern2), "restore_vimvar()");
1526 else
1527 hash_remove(&vimvarht, hi);
1528 }
1529}
1530
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001531#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001532/*
1533 * Evaluate an expression to a list with suggestions.
1534 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001535 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001536 */
1537 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001538eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001539{
1540 typval_T save_val;
1541 typval_T rettv;
1542 list_T *list = NULL;
1543 char_u *p = skipwhite(expr);
1544
1545 /* Set "v:val" to the bad word. */
1546 prepare_vimvar(VV_VAL, &save_val);
1547 vimvars[VV_VAL].vv_type = VAR_STRING;
1548 vimvars[VV_VAL].vv_str = badword;
1549 if (p_verbose == 0)
1550 ++emsg_off;
1551
1552 if (eval1(&p, &rettv, TRUE) == OK)
1553 {
1554 if (rettv.v_type != VAR_LIST)
1555 clear_tv(&rettv);
1556 else
1557 list = rettv.vval.v_list;
1558 }
1559
1560 if (p_verbose == 0)
1561 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001562 restore_vimvar(VV_VAL, &save_val);
1563
1564 return list;
1565}
1566
1567/*
1568 * "list" is supposed to contain two items: a word and a number. Return the
1569 * word in "pp" and the number as the return value.
1570 * Return -1 if anything isn't right.
1571 * Used to get the good word and score from the eval_spell_expr() result.
1572 */
1573 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001574get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001575{
1576 listitem_T *li;
1577
1578 li = list->lv_first;
1579 if (li == NULL)
1580 return -1;
1581 *pp = get_tv_string(&li->li_tv);
1582
1583 li = li->li_next;
1584 if (li == NULL)
1585 return -1;
1586 return get_tv_number(&li->li_tv);
1587}
1588#endif
1589
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001590/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001591 * Top level evaluation function.
1592 * Returns an allocated typval_T with the result.
1593 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001594 */
1595 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001596eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001597{
1598 typval_T *tv;
1599
1600 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001601 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602 {
1603 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001604 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001605 }
1606
1607 return tv;
1608}
1609
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001613 * Uses argv[argc] for the function arguments. Only Number and String
1614 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001618call_vim_function(
1619 char_u *func,
1620 int argc,
1621 char_u **argv,
1622 int safe, /* use the sandbox */
1623 int str_arg_only, /* all arguments are strings */
1624 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
Bram Moolenaar33570922005-01-25 22:26:29 +00001626 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 long n;
1628 int len;
1629 int i;
1630 int doesrange;
1631 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001634 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 for (i = 0; i < argc; i++)
1639 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001640 /* Pass a NULL or empty argument as an empty string */
1641 if (argv[i] == NULL || *argv[i] == NUL)
1642 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001643 argvars[i].v_type = VAR_STRING;
1644 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001645 continue;
1646 }
1647
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001648 if (str_arg_only)
1649 len = 0;
1650 else
1651 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001652 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (len != 0 && len == (int)STRLEN(argv[i]))
1654 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001655 argvars[i].v_type = VAR_NUMBER;
1656 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 }
1658 else
1659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001660 argvars[i].v_type = VAR_STRING;
1661 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 }
1664
1665 if (safe)
1666 {
1667 save_funccalp = save_funccal();
1668 ++sandbox;
1669 }
1670
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1672 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (safe)
1676 {
1677 --sandbox;
1678 restore_funccal(save_funccalp);
1679 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 vim_free(argvars);
1681
1682 if (ret == FAIL)
1683 clear_tv(rettv);
1684
1685 return ret;
1686}
1687
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001688/*
1689 * Call vimL function "func" and return the result as a number.
1690 * Returns -1 when calling the function fails.
1691 * Uses argv[argc] for the function arguments.
1692 */
1693 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001694call_func_retnr(
1695 char_u *func,
1696 int argc,
1697 char_u **argv,
1698 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001699{
1700 typval_T rettv;
1701 long retval;
1702
1703 /* All arguments are passed as strings, no conversion to number. */
1704 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1705 return -1;
1706
1707 retval = get_tv_number_chk(&rettv, NULL);
1708 clear_tv(&rettv);
1709 return retval;
1710}
1711
1712#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1713 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1714
Bram Moolenaar4f688582007-07-24 12:34:30 +00001715# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001717 * Call vimL function "func" and return the result as a string.
1718 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719 * Uses argv[argc] for the function arguments.
1720 */
1721 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001722call_func_retstr(
1723 char_u *func,
1724 int argc,
1725 char_u **argv,
1726 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727{
1728 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001729 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001731 /* All arguments are passed as strings, no conversion to number. */
1732 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733 return NULL;
1734
1735 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 return retval;
1738}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001739# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001740
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001741/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001742 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001744 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745 */
1746 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001747call_func_retlist(
1748 char_u *func,
1749 int argc,
1750 char_u **argv,
1751 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001752{
1753 typval_T rettv;
1754
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001755 /* All arguments are passed as strings, no conversion to number. */
1756 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757 return NULL;
1758
1759 if (rettv.v_type != VAR_LIST)
1760 {
1761 clear_tv(&rettv);
1762 return NULL;
1763 }
1764
1765 return rettv.vval.v_list;
1766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#endif
1768
1769/*
1770 * Save the current function call pointer, and set it to NULL.
1771 * Used when executing autocommands and for ":source".
1772 */
1773 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001776 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 current_funccal = NULL;
1779 return (void *)fc;
1780}
1781
1782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001785 funccall_T *fc = (funccall_T *)vfc;
1786
1787 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788}
1789
Bram Moolenaar05159a02005-02-26 23:04:13 +00001790#if defined(FEAT_PROFILE) || defined(PROTO)
1791/*
1792 * Prepare profiling for entering a child or something else that is not
1793 * counted for the script/function itself.
1794 * Should always be called in pair with prof_child_exit().
1795 */
1796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001797prof_child_enter(
1798 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001799{
1800 funccall_T *fc = current_funccal;
1801
1802 if (fc != NULL && fc->func->uf_profiling)
1803 profile_start(&fc->prof_child);
1804 script_prof_save(tm);
1805}
1806
1807/*
1808 * Take care of time spent in a child.
1809 * Should always be called after prof_child_enter().
1810 */
1811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001812prof_child_exit(
1813 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001814{
1815 funccall_T *fc = current_funccal;
1816
1817 if (fc != NULL && fc->func->uf_profiling)
1818 {
1819 profile_end(&fc->prof_child);
1820 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1821 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1822 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1823 }
1824 script_prof_restore(tm);
1825}
1826#endif
1827
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829#ifdef FEAT_FOLDING
1830/*
1831 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1832 * it in "*cp". Doesn't give error messages.
1833 */
1834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001835eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836{
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 int retval;
1839 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001840 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1841 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
1843 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001844 if (use_sandbox)
1845 ++sandbox;
1846 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 retval = 0;
1850 else
1851 {
1852 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 if (tv.v_type == VAR_NUMBER)
1854 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001855 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 retval = 0;
1857 else
1858 {
1859 /* If the result is a string, check if there is a non-digit before
1860 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if (!VIM_ISDIGIT(*s) && *s != '-')
1863 *cp = *s++;
1864 retval = atol((char *)s);
1865 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001869 if (use_sandbox)
1870 --sandbox;
1871 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873 return retval;
1874}
1875#endif
1876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 * ":let" list all variable values
1879 * ":let var1 var2" list variable values
1880 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 * ":let var += expr" assignment command.
1882 * ":let var -= expr" assignment command.
1883 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 */
1886 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001887ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
1889 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 int var_count = 0;
1894 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001896 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001897 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
Bram Moolenaardb552d602006-03-23 22:59:57 +00001899 argend = skip_var_list(arg, &var_count, &semicolon);
1900 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001902 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1903 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 expr = skipwhite(argend);
1905 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1906 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001908 /*
1909 * ":let" without "=": list variables
1910 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 if (*arg == '[')
1912 EMSG(_(e_invarg));
1913 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001915 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001919 list_glob_vars(&first);
1920 list_buf_vars(&first);
1921 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001922#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001924#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 list_script_vars(&first);
1926 list_func_vars(&first);
1927 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 eap->nextcmd = check_nextcmd(arg);
1930 }
1931 else
1932 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 op[0] = '=';
1934 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001935 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001937 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1938 op[0] = *expr; /* +=, -= or .= */
1939 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001941 else
1942 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (eap->skip)
1945 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (eap->skip)
1948 {
1949 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 --emsg_skip;
1952 }
1953 else if (i != FAIL)
1954 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959 }
1960}
1961
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962/*
1963 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1964 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 * When "nextchars" is not NULL it points to a string with characters that
1966 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1967 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 * Returns OK or FAIL;
1969 */
1970 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001971ex_let_vars(
1972 char_u *arg_start,
1973 typval_T *tv,
1974 int copy, /* copy values from "tv", don't move */
1975 int semicolon, /* from skip_var_list() */
1976 int var_count, /* from skip_var_list() */
1977 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978{
1979 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001980 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 listitem_T *item;
1983 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984
1985 if (*arg != '[')
1986 {
1987 /*
1988 * ":let var = expr" or ":for var in list"
1989 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 return OK;
1993 }
1994
1995 /*
1996 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1997 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001998 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 {
2000 EMSG(_(e_listreq));
2001 return FAIL;
2002 }
2003
2004 i = list_len(l);
2005 if (semicolon == 0 && var_count < i)
2006 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002007 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 return FAIL;
2009 }
2010 if (var_count - semicolon > i)
2011 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002012 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 return FAIL;
2014 }
2015
2016 item = l->lv_first;
2017 while (*arg != ']')
2018 {
2019 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 item = item->li_next;
2022 if (arg == NULL)
2023 return FAIL;
2024
2025 arg = skipwhite(arg);
2026 if (*arg == ';')
2027 {
2028 /* Put the rest of the list (may be empty) in the var after ';'.
2029 * Create a new list for this. */
2030 l = list_alloc();
2031 if (l == NULL)
2032 return FAIL;
2033 while (item != NULL)
2034 {
2035 list_append_tv(l, &item->li_tv);
2036 item = item->li_next;
2037 }
2038
2039 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002040 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 ltv.vval.v_list = l;
2042 l->lv_refcount = 1;
2043
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2045 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 clear_tv(&ltv);
2047 if (arg == NULL)
2048 return FAIL;
2049 break;
2050 }
2051 else if (*arg != ',' && *arg != ']')
2052 {
2053 EMSG2(_(e_intern2), "ex_let_vars()");
2054 return FAIL;
2055 }
2056 }
2057
2058 return OK;
2059}
2060
2061/*
2062 * Skip over assignable variable "var" or list of variables "[var, var]".
2063 * Used for ":let varvar = expr" and ":for varvar in expr".
2064 * For "[var, var]" increment "*var_count" for each variable.
2065 * for "[var, var; var]" set "semicolon".
2066 * Return NULL for an error.
2067 */
2068 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002069skip_var_list(
2070 char_u *arg,
2071 int *var_count,
2072 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002073{
2074 char_u *p, *s;
2075
2076 if (*arg == '[')
2077 {
2078 /* "[var, var]": find the matching ']'. */
2079 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002080 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 {
2082 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2083 s = skip_var_one(p);
2084 if (s == p)
2085 {
2086 EMSG2(_(e_invarg2), p);
2087 return NULL;
2088 }
2089 ++*var_count;
2090
2091 p = skipwhite(s);
2092 if (*p == ']')
2093 break;
2094 else if (*p == ';')
2095 {
2096 if (*semicolon == 1)
2097 {
2098 EMSG(_("Double ; in list of variables"));
2099 return NULL;
2100 }
2101 *semicolon = 1;
2102 }
2103 else if (*p != ',')
2104 {
2105 EMSG2(_(e_invarg2), p);
2106 return NULL;
2107 }
2108 }
2109 return p + 1;
2110 }
2111 else
2112 return skip_var_one(arg);
2113}
2114
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002115/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002116 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002117 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002119 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002120skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002122 if (*arg == '@' && arg[1] != NUL)
2123 return arg + 2;
2124 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2125 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126}
2127
Bram Moolenaara7043832005-01-21 11:56:39 +00002128/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002129 * List variables for hashtab "ht" with prefix "prefix".
2130 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002131 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002133list_hashtable_vars(
2134 hashtab_T *ht,
2135 char_u *prefix,
2136 int empty,
2137 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002138{
Bram Moolenaar33570922005-01-25 22:26:29 +00002139 hashitem_T *hi;
2140 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141 int todo;
2142
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002143 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002144 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2145 {
2146 if (!HASHITEM_EMPTY(hi))
2147 {
2148 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002149 di = HI2DI(hi);
2150 if (empty || di->di_tv.v_type != VAR_STRING
2151 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002153 }
2154 }
2155}
2156
2157/*
2158 * List global variables.
2159 */
2160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002161list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002162{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002164}
2165
2166/*
2167 * List buffer variables.
2168 */
2169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002170list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002171{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002172 char_u numbuf[NUMBUFLEN];
2173
Bram Moolenaar429fa852013-04-15 12:27:36 +02002174 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002176
2177 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2179 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002180}
2181
2182/*
2183 * List window variables.
2184 */
2185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002187{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002188 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002190}
2191
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002192#ifdef FEAT_WINDOWS
2193/*
2194 * List tab page variables.
2195 */
2196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002197list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002198{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002199 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201}
2202#endif
2203
Bram Moolenaara7043832005-01-21 11:56:39 +00002204/*
2205 * List Vim variables.
2206 */
2207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211}
2212
2213/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002214 * List script-local variables, if there is a script.
2215 */
2216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002218{
2219 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2221 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002222}
2223
2224/*
2225 * List function variables, if there is a function.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002229{
2230 if (current_funccal != NULL)
2231 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002232 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002233}
2234
2235/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 * List variables in "arg".
2237 */
2238 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240{
2241 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 char_u *name_start;
2245 char_u *arg_subsc;
2246 char_u *tofree;
2247 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248
2249 while (!ends_excmd(*arg) && !got_int)
2250 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002253 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2255 {
2256 emsg_severe = TRUE;
2257 EMSG(_(e_trailing));
2258 break;
2259 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 /* get_name_len() takes care of expanding curly braces */
2264 name_start = name = arg;
2265 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2266 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 /* This is mainly to keep test 49 working: when expanding
2269 * curly braces fails overrule the exception error message. */
2270 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 emsg_severe = TRUE;
2273 EMSG2(_(e_invarg2), arg);
2274 break;
2275 }
2276 error = TRUE;
2277 }
2278 else
2279 {
2280 if (tofree != NULL)
2281 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002282 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 else
2285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 /* handle d.key, l[idx], f(expr) */
2287 arg_subsc = arg;
2288 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002296 case 'g': list_glob_vars(first); break;
2297 case 'b': list_buf_vars(first); break;
2298 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002299#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002300 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002301#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 case 'v': list_vim_vars(first); break;
2303 case 's': list_script_vars(first); break;
2304 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 default:
2306 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002307 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002308 }
2309 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 {
2311 char_u numbuf[NUMBUFLEN];
2312 char_u *tf;
2313 int c;
2314 char_u *s;
2315
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002316 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 c = *arg;
2318 *arg = NUL;
2319 list_one_var_a((char_u *)"",
2320 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002321 tv.v_type,
2322 s == NULL ? (char_u *)"" : s,
2323 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002324 *arg = c;
2325 vim_free(tf);
2326 }
2327 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 }
2330 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002331
2332 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334
2335 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 }
2337
2338 return arg;
2339}
2340
2341/*
2342 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2343 * Returns a pointer to the char just after the var name.
2344 * Returns NULL if there is an error.
2345 */
2346 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002347ex_let_one(
2348 char_u *arg, /* points to variable name */
2349 typval_T *tv, /* value to assign to variable */
2350 int copy, /* copy value from "tv" */
2351 char_u *endchars, /* valid chars after variable name or NULL */
2352 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353{
2354 int c1;
2355 char_u *name;
2356 char_u *p;
2357 char_u *arg_end = NULL;
2358 int len;
2359 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361
2362 /*
2363 * ":let $VAR = expr": Set environment variable.
2364 */
2365 if (*arg == '$')
2366 {
2367 /* Find the end of the name. */
2368 ++arg;
2369 name = arg;
2370 len = get_env_len(&arg);
2371 if (len == 0)
2372 EMSG2(_(e_invarg2), name - 1);
2373 else
2374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002375 if (op != NULL && (*op == '+' || *op == '-'))
2376 EMSG2(_(e_letwrong), op);
2377 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002380 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 {
2382 c1 = name[len];
2383 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 p = get_tv_string_chk(tv);
2385 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 {
2387 int mustfree = FALSE;
2388 char_u *s = vim_getenv(name, &mustfree);
2389
2390 if (s != NULL)
2391 {
2392 p = tofree = concat_str(s, p);
2393 if (mustfree)
2394 vim_free(s);
2395 }
2396 }
2397 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002398 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 if (STRICMP(name, "HOME") == 0)
2401 init_homedir();
2402 else if (didset_vim && STRICMP(name, "VIM") == 0)
2403 didset_vim = FALSE;
2404 else if (didset_vimruntime
2405 && STRICMP(name, "VIMRUNTIME") == 0)
2406 didset_vimruntime = FALSE;
2407 arg_end = arg;
2408 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002411 }
2412 }
2413 }
2414
2415 /*
2416 * ":let &option = expr": Set option value.
2417 * ":let &l:option = expr": Set local option value.
2418 * ":let &g:option = expr": Set global option value.
2419 */
2420 else if (*arg == '&')
2421 {
2422 /* Find the end of the name. */
2423 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002424 if (p == NULL || (endchars != NULL
2425 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 EMSG(_(e_letunexp));
2427 else
2428 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 long n;
2430 int opt_type;
2431 long numval;
2432 char_u *stringval = NULL;
2433 char_u *s;
2434
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 c1 = *p;
2436 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437
2438 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 s = get_tv_string_chk(tv); /* != NULL if number or string */
2440 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 {
2442 opt_type = get_option_value(arg, &numval,
2443 &stringval, opt_flags);
2444 if ((opt_type == 1 && *op == '.')
2445 || (opt_type == 0 && *op != '.'))
2446 EMSG2(_(e_letwrong), op);
2447 else
2448 {
2449 if (opt_type == 1) /* number */
2450 {
2451 if (*op == '+')
2452 n = numval + n;
2453 else
2454 n = numval - n;
2455 }
2456 else if (opt_type == 0 && stringval != NULL) /* string */
2457 {
2458 s = concat_str(stringval, s);
2459 vim_free(stringval);
2460 stringval = s;
2461 }
2462 }
2463 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 if (s != NULL)
2465 {
2466 set_option_value(arg, n, s, opt_flags);
2467 arg_end = p;
2468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 }
2472 }
2473
2474 /*
2475 * ":let @r = expr": Set register contents.
2476 */
2477 else if (*arg == '@')
2478 {
2479 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 if (op != NULL && (*op == '+' || *op == '-'))
2481 EMSG2(_(e_letwrong), op);
2482 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002483 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 EMSG(_(e_letunexp));
2485 else
2486 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002487 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 char_u *s;
2489
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002490 p = get_tv_string_chk(tv);
2491 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002493 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 if (s != NULL)
2495 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 vim_free(s);
2498 }
2499 }
2500 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 arg_end = arg + 1;
2504 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002505 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 }
2507 }
2508
2509 /*
2510 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002513 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002517 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2521 EMSG(_(e_letunexp));
2522 else
2523 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002524 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 arg_end = p;
2526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 }
2530
2531 else
2532 EMSG2(_(e_invarg2), arg);
2533
2534 return arg_end;
2535}
2536
2537/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2539 */
2540 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002541check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542{
2543 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2544 {
2545 EMSG2(_(e_readonlyvar), arg);
2546 return TRUE;
2547 }
2548 return FALSE;
2549}
2550
2551/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * Get an lval: variable, Dict item or List item that can be assigned a value
2553 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2554 * "name.key", "name.key[expr]" etc.
2555 * Indexing only works if "name" is an existing List or Dictionary.
2556 * "name" points to the start of the name.
2557 * If "rettv" is not NULL it points to the value to be assigned.
2558 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2559 * wrong; must end in space or cmd separator.
2560 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002561 * flags:
2562 * GLV_QUIET: do not give error messages
2563 * GLV_NO_AUTOLOAD: do not use script autoloading
2564 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002566 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002568 */
2569 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002570get_lval(
2571 char_u *name,
2572 typval_T *rettv,
2573 lval_T *lp,
2574 int unlet,
2575 int skip,
2576 int flags, /* GLV_ values */
2577 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 char_u *expr_start, *expr_end;
2581 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002582 dictitem_T *v;
2583 typval_T var1;
2584 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002588 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002590 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594
2595 if (skip)
2596 {
2597 /* When skipping just find the end of the name. */
2598 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002599 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 }
2601
2602 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002603 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (expr_start != NULL)
2605 {
2606 /* Don't expand the name when we already know there is an error. */
2607 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2608 && *p != '[' && *p != '.')
2609 {
2610 EMSG(_(e_trailing));
2611 return NULL;
2612 }
2613
2614 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2615 if (lp->ll_exp_name == NULL)
2616 {
2617 /* Report an invalid expression in braces, unless the
2618 * expression evaluation has been cancelled due to an
2619 * aborting error, an interrupt, or an exception. */
2620 if (!aborting() && !quiet)
2621 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002622 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 EMSG2(_(e_invarg2), name);
2624 return NULL;
2625 }
2626 }
2627 lp->ll_name = lp->ll_exp_name;
2628 }
2629 else
2630 lp->ll_name = name;
2631
2632 /* Without [idx] or .key we are done. */
2633 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2634 return p;
2635
2636 cc = *p;
2637 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002638 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (v == NULL && !quiet)
2640 EMSG2(_(e_undefvar), lp->ll_name);
2641 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 if (v == NULL)
2643 return NULL;
2644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 /*
2646 * Loop until no more [idx] or .key is following.
2647 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002648 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2652 && !(lp->ll_tv->v_type == VAR_DICT
2653 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
2656 EMSG(_("E689: Can only index a List or Dictionary"));
2657 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_("E708: [:] must come last"));
2663 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 len = -1;
2667 if (*p == '.')
2668 {
2669 key = p + 1;
2670 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2671 ;
2672 if (len == 0)
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (!quiet)
2675 EMSG(_(e_emptykey));
2676 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 }
2678 p = key + len;
2679 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002680 else
2681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 if (*p == ':')
2685 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 else
2687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 empty1 = FALSE;
2689 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002691 if (get_tv_string_chk(&var1) == NULL)
2692 {
2693 /* not a number or string */
2694 clear_tv(&var1);
2695 return NULL;
2696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698
2699 /* Optionally get the second index [ :expr]. */
2700 if (*p == ':')
2701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (rettv != NULL && (rettv->v_type != VAR_LIST
2711 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
2714 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (!empty1)
2716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 p = skipwhite(p + 1);
2720 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 else
2723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2726 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002731 if (get_tv_string_chk(&var2) == NULL)
2732 {
2733 /* not a number or string */
2734 if (!empty1)
2735 clear_tv(&var1);
2736 clear_tv(&var2);
2737 return NULL;
2738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
2748 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 if (!empty1)
2750 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 }
2755
2756 /* Skip to past ']'. */
2757 ++p;
2758 }
2759
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 {
2762 if (len == -1)
2763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002765 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (*key == NUL)
2767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (!quiet)
2769 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 }
2773 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002774 lp->ll_list = NULL;
2775 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002776 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 /* When assigning to a scope dictionary check that a function and
2779 * variable name is valid (only variable name unless it is l: or
2780 * g: dictionary). Disallow overwriting a builtin function. */
2781 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 int prevval;
2784 int wrong;
2785
2786 if (len != -1)
2787 {
2788 prevval = key[len];
2789 key[len] = NUL;
2790 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002791 else
2792 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002793 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2794 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002796 || !valid_varname(key);
2797 if (len != -1)
2798 key[len] = prevval;
2799 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 return NULL;
2801 }
2802
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002805 /* Can't add "v:" variable. */
2806 if (lp->ll_dict == &vimvardict)
2807 {
2808 EMSG2(_(e_illvar), name);
2809 return NULL;
2810 }
2811
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002812 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002816 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 if (len == -1)
2818 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 }
2821 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 if (len == -1)
2826 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 p = NULL;
2829 break;
2830 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002831 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002832 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 return NULL;
2834
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 if (len == -1)
2836 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 }
2839 else
2840 {
2841 /*
2842 * Get the number and item for the only or first index of the List.
2843 */
2844 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 else
2847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002848 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 clear_tv(&var1);
2850 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002851 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_list = lp->ll_tv->vval.v_list;
2853 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2854 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002856 if (lp->ll_n1 < 0)
2857 {
2858 lp->ll_n1 = 0;
2859 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2860 }
2861 }
2862 if (lp->ll_li == NULL)
2863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002866 if (!quiet)
2867 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 }
2870
2871 /*
2872 * May need to find the item or absolute index for the second
2873 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 * When no index given: "lp->ll_empty2" is TRUE.
2875 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002879 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002885 {
2886 if (!quiet)
2887 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002889 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 }
2892
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2894 if (lp->ll_n1 < 0)
2895 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2896 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002897 {
2898 if (!quiet)
2899 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002901 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002902 }
2903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002906 }
2907
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 return p;
2909}
2910
2911/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002912 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 */
2914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002915clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916{
2917 vim_free(lp->ll_exp_name);
2918 vim_free(lp->ll_newkey);
2919}
2920
2921/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002922 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 */
2926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002927set_var_lval(
2928 lval_T *lp,
2929 char_u *endp,
2930 typval_T *rettv,
2931 int copy,
2932 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933{
2934 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002935 listitem_T *ri;
2936 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937
2938 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 cc = *endp;
2943 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 if (op != NULL && *op != '=')
2945 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002948 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002949 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002950 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002951 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002953 if ((di == NULL
2954 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2955 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2956 FALSE)))
2957 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 set_var(lp->ll_name, &tv, FALSE);
2959 clear_tv(&tv);
2960 }
2961 }
2962 else
2963 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002965 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002967 else if (tv_check_lock(lp->ll_newkey == NULL
2968 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002969 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002970 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 else if (lp->ll_range)
2972 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002973 listitem_T *ll_li = lp->ll_li;
2974 int ll_n1 = lp->ll_n1;
2975
2976 /*
2977 * Check whether any of the list items is locked
2978 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002979 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002980 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002981 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002982 return;
2983 ri = ri->li_next;
2984 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2985 break;
2986 ll_li = ll_li->li_next;
2987 ++ll_n1;
2988 }
2989
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 /*
2991 * Assign the List values to the list items.
2992 */
2993 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002995 if (op != NULL && *op != '=')
2996 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2997 else
2998 {
2999 clear_tv(&lp->ll_li->li_tv);
3000 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 ri = ri->li_next;
3003 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3004 break;
3005 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003008 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003009 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 ri = NULL;
3011 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 lp->ll_li = lp->ll_li->li_next;
3015 ++lp->ll_n1;
3016 }
3017 if (ri != NULL)
3018 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 else if (lp->ll_empty2
3020 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 : lp->ll_n1 != lp->ll_n2)
3022 EMSG(_("E711: List value has not enough items"));
3023 }
3024 else
3025 {
3026 /*
3027 * Assign to a List or Dictionary item.
3028 */
3029 if (lp->ll_newkey != NULL)
3030 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003031 if (op != NULL && *op != '=')
3032 {
3033 EMSG2(_(e_letwrong), op);
3034 return;
3035 }
3036
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003038 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 if (di == NULL)
3040 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003041 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3042 {
3043 vim_free(di);
3044 return;
3045 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 else if (op != NULL && *op != '=')
3049 {
3050 tv_op(lp->ll_tv, rettv, op);
3051 return;
3052 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003053 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003055
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003056 /*
3057 * Assign the value to the variable or list item.
3058 */
3059 if (copy)
3060 copy_tv(rettv, lp->ll_tv);
3061 else
3062 {
3063 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003064 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003065 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066 }
3067 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003068}
3069
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003070/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3072 * Returns OK or FAIL.
3073 */
3074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003075tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076{
3077 long n;
3078 char_u numbuf[NUMBUFLEN];
3079 char_u *s;
3080
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003081 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3082 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3083 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 {
3085 switch (tv1->v_type)
3086 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003087 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 case VAR_DICT:
3089 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003090 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003091 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003092 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 break;
3094
3095 case VAR_LIST:
3096 if (*op != '+' || tv2->v_type != VAR_LIST)
3097 break;
3098 /* List += List */
3099 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3100 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3101 return OK;
3102
3103 case VAR_NUMBER:
3104 case VAR_STRING:
3105 if (tv2->v_type == VAR_LIST)
3106 break;
3107 if (*op == '+' || *op == '-')
3108 {
3109 /* nr += nr or nr -= nr*/
3110 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111#ifdef FEAT_FLOAT
3112 if (tv2->v_type == VAR_FLOAT)
3113 {
3114 float_T f = n;
3115
3116 if (*op == '+')
3117 f += tv2->vval.v_float;
3118 else
3119 f -= tv2->vval.v_float;
3120 clear_tv(tv1);
3121 tv1->v_type = VAR_FLOAT;
3122 tv1->vval.v_float = f;
3123 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003124 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125#endif
3126 {
3127 if (*op == '+')
3128 n += get_tv_number(tv2);
3129 else
3130 n -= get_tv_number(tv2);
3131 clear_tv(tv1);
3132 tv1->v_type = VAR_NUMBER;
3133 tv1->vval.v_number = n;
3134 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 }
3136 else
3137 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138 if (tv2->v_type == VAR_FLOAT)
3139 break;
3140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 /* str .= str */
3142 s = get_tv_string(tv1);
3143 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3144 clear_tv(tv1);
3145 tv1->v_type = VAR_STRING;
3146 tv1->vval.v_string = s;
3147 }
3148 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149
3150#ifdef FEAT_FLOAT
3151 case VAR_FLOAT:
3152 {
3153 float_T f;
3154
3155 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3156 && tv2->v_type != VAR_NUMBER
3157 && tv2->v_type != VAR_STRING))
3158 break;
3159 if (tv2->v_type == VAR_FLOAT)
3160 f = tv2->vval.v_float;
3161 else
3162 f = get_tv_number(tv2);
3163 if (*op == '+')
3164 tv1->vval.v_float += f;
3165 else
3166 tv1->vval.v_float -= f;
3167 }
3168 return OK;
3169#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003170 }
3171 }
3172
3173 EMSG2(_(e_letwrong), op);
3174 return FAIL;
3175}
3176
3177/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 * Add a watcher to a list.
3179 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003181list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182{
3183 lw->lw_next = l->lv_watch;
3184 l->lv_watch = lw;
3185}
3186
3187/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003188 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 * No warning when it isn't found...
3190 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003192list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193{
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195
3196 lwp = &l->lv_watch;
3197 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3198 {
3199 if (lw == lwrem)
3200 {
3201 *lwp = lw->lw_next;
3202 break;
3203 }
3204 lwp = &lw->lw_next;
3205 }
3206}
3207
3208/*
3209 * Just before removing an item from a list: advance watchers to the next
3210 * item.
3211 */
3212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003213list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214{
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216
3217 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3218 if (lw->lw_item == item)
3219 lw->lw_item = item->li_next;
3220}
3221
3222/*
3223 * Evaluate the expression used in a ":for var in expr" command.
3224 * "arg" points to "var".
3225 * Set "*errp" to TRUE for an error, FALSE otherwise;
3226 * Return a pointer that holds the info. Null when there is an error.
3227 */
3228 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003229eval_for_line(
3230 char_u *arg,
3231 int *errp,
3232 char_u **nextcmdp,
3233 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 typval_T tv;
3238 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239
3240 *errp = TRUE; /* default: there is an error */
3241
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 if (fi == NULL)
3244 return NULL;
3245
3246 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3247 if (expr == NULL)
3248 return fi;
3249
3250 expr = skipwhite(expr);
3251 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3252 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003253 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 return fi;
3255 }
3256
3257 if (skip)
3258 ++emsg_skip;
3259 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3260 {
3261 *errp = FALSE;
3262 if (!skip)
3263 {
3264 l = tv.vval.v_list;
3265 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 clear_tv(&tv);
3269 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 else
3271 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003272 /* No need to increment the refcount, it's already set for the
3273 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 fi->fi_list = l;
3275 list_add_watch(l, &fi->fi_lw);
3276 fi->fi_lw.lw_item = l->lv_first;
3277 }
3278 }
3279 }
3280 if (skip)
3281 --emsg_skip;
3282
3283 return fi;
3284}
3285
3286/*
3287 * Use the first item in a ":for" list. Advance to the next.
3288 * Assign the values to the variable (list). "arg" points to the first one.
3289 * Return TRUE when a valid item was found, FALSE when at end of list or
3290 * something wrong.
3291 */
3292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003295 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298
3299 item = fi->fi_lw.lw_item;
3300 if (item == NULL)
3301 result = FALSE;
3302 else
3303 {
3304 fi->fi_lw.lw_item = item->li_next;
3305 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3306 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3307 }
3308 return result;
3309}
3310
3311/*
3312 * Free the structure used to store info used by ":for".
3313 */
3314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003315free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316{
Bram Moolenaar33570922005-01-25 22:26:29 +00003317 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003319 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 list_unref(fi->fi_list);
3323 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 vim_free(fi);
3325}
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3328
3329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003330set_context_for_expression(
3331 expand_T *xp,
3332 char_u *arg,
3333 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 int got_eq = FALSE;
3336 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 if (cmdidx == CMD_let)
3340 {
3341 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003342 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 {
3344 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003345 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 {
3347 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003348 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 if (vim_iswhite(*p))
3350 break;
3351 }
3352 return;
3353 }
3354 }
3355 else
3356 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3357 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 while ((xp->xp_pattern = vim_strpbrk(arg,
3359 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3360 {
3361 c = *xp->xp_pattern;
3362 if (c == '&')
3363 {
3364 c = xp->xp_pattern[1];
3365 if (c == '&')
3366 {
3367 ++xp->xp_pattern;
3368 xp->xp_context = cmdidx != CMD_let || got_eq
3369 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3370 }
3371 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003374 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3375 xp->xp_pattern += 2;
3376
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379 else if (c == '$')
3380 {
3381 /* environment variable */
3382 xp->xp_context = EXPAND_ENV_VARS;
3383 }
3384 else if (c == '=')
3385 {
3386 got_eq = TRUE;
3387 xp->xp_context = EXPAND_EXPRESSION;
3388 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003389 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 && xp->xp_context == EXPAND_FUNCTIONS
3391 && vim_strchr(xp->xp_pattern, '(') == NULL)
3392 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003393 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 break;
3395 }
3396 else if (cmdidx != CMD_let || got_eq)
3397 {
3398 if (c == '"') /* string */
3399 {
3400 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3401 if (c == '\\' && xp->xp_pattern[1] != NUL)
3402 ++xp->xp_pattern;
3403 xp->xp_context = EXPAND_NOTHING;
3404 }
3405 else if (c == '\'') /* literal string */
3406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003407 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3409 /* skip */ ;
3410 xp->xp_context = EXPAND_NOTHING;
3411 }
3412 else if (c == '|')
3413 {
3414 if (xp->xp_pattern[1] == '|')
3415 {
3416 ++xp->xp_pattern;
3417 xp->xp_context = EXPAND_EXPRESSION;
3418 }
3419 else
3420 xp->xp_context = EXPAND_COMMANDS;
3421 }
3422 else
3423 xp->xp_context = EXPAND_EXPRESSION;
3424 }
3425 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003426 /* Doesn't look like something valid, expand as an expression
3427 * anyway. */
3428 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 arg = xp->xp_pattern;
3430 if (*arg != NUL)
3431 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3432 /* skip */ ;
3433 }
3434 xp->xp_pattern = arg;
3435}
3436
3437#endif /* FEAT_CMDL_COMPL */
3438
3439/*
3440 * ":1,25call func(arg1, arg2)" function call.
3441 */
3442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003443ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 char_u *arg = eap->arg;
3446 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003448 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 linenr_T lnum;
3452 int doesrange;
3453 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003456 if (eap->skip)
3457 {
3458 /* trans_function_name() doesn't work well when skipping, use eval0()
3459 * instead to skip to any following command, e.g. for:
3460 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003461 ++emsg_skip;
3462 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3463 clear_tv(&rettv);
3464 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003465 return;
3466 }
3467
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003468 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003469 if (fudi.fd_newkey != NULL)
3470 {
3471 /* Still need to give an error message for missing key. */
3472 EMSG2(_(e_dictkey), fudi.fd_newkey);
3473 vim_free(fudi.fd_newkey);
3474 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003475 if (tofree == NULL)
3476 return;
3477
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003478 /* Increase refcount on dictionary, it could get deleted when evaluating
3479 * the arguments. */
3480 if (fudi.fd_dict != NULL)
3481 ++fudi.fd_dict->dv_refcount;
3482
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003483 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003484 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003485 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar532c7802005-01-27 14:44:31 +00003487 /* Skip white space to allow ":call func ()". Not good, but required for
3488 * backward compatibility. */
3489 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003490 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 if (*startarg != '(')
3493 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003494 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 goto end;
3496 }
3497
3498 /*
3499 * When skipping, evaluate the function once, to find the end of the
3500 * arguments.
3501 * When the function takes a range, this is discovered after the first
3502 * call, and the loop is broken.
3503 */
3504 if (eap->skip)
3505 {
3506 ++emsg_skip;
3507 lnum = eap->line2; /* do it once, also with an invalid range */
3508 }
3509 else
3510 lnum = eap->line1;
3511 for ( ; lnum <= eap->line2; ++lnum)
3512 {
3513 if (!eap->skip && eap->addr_count > 0)
3514 {
3515 curwin->w_cursor.lnum = lnum;
3516 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003517#ifdef FEAT_VIRTUALEDIT
3518 curwin->w_cursor.coladd = 0;
3519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003522 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003523 eap->line1, eap->line2, &doesrange,
3524 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 failed = TRUE;
3527 break;
3528 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003529
3530 /* Handle a function returning a Funcref, Dictionary or List. */
3531 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3532 {
3533 failed = TRUE;
3534 break;
3535 }
3536
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (doesrange || eap->skip)
3539 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003542 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003543 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (aborting())
3546 break;
3547 }
3548 if (eap->skip)
3549 --emsg_skip;
3550
3551 if (!failed)
3552 {
3553 /* Check for trailing illegal characters and a following command. */
3554 if (!ends_excmd(*arg))
3555 {
3556 emsg_severe = TRUE;
3557 EMSG(_(e_trailing));
3558 }
3559 else
3560 eap->nextcmd = check_nextcmd(arg);
3561 }
3562
3563end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003564 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003565 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568/*
3569 * ":unlet[!] var1 ... " command.
3570 */
3571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003572ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003574 ex_unletlock(eap, eap->arg, 0);
3575}
3576
3577/*
3578 * ":lockvar" and ":unlockvar" commands
3579 */
3580 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003581ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 int deep = 2;
3585
3586 if (eap->forceit)
3587 deep = -1;
3588 else if (vim_isdigit(*arg))
3589 {
3590 deep = getdigits(&arg);
3591 arg = skipwhite(arg);
3592 }
3593
3594 ex_unletlock(eap, arg, deep);
3595}
3596
3597/*
3598 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3599 */
3600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601ex_unletlock(
3602 exarg_T *eap,
3603 char_u *argstart,
3604 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605{
3606 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
3611 do
3612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003614 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003615 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 if (lv.ll_name == NULL)
3617 error = TRUE; /* error but continue parsing */
3618 if (name_end == NULL || (!vim_iswhite(*name_end)
3619 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 if (name_end != NULL)
3622 {
3623 emsg_severe = TRUE;
3624 EMSG(_(e_trailing));
3625 }
3626 if (!(eap->skip || error))
3627 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 break;
3629 }
3630
3631 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003632 {
3633 if (eap->cmdidx == CMD_unlet)
3634 {
3635 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3636 error = TRUE;
3637 }
3638 else
3639 {
3640 if (do_lock_var(&lv, name_end, deep,
3641 eap->cmdidx == CMD_lockvar) == FAIL)
3642 error = TRUE;
3643 }
3644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 if (!eap->skip)
3647 clear_lval(&lv);
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 arg = skipwhite(name_end);
3650 } while (!ends_excmd(*arg));
3651
3652 eap->nextcmd = check_nextcmd(arg);
3653}
3654
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003656do_unlet_var(
3657 lval_T *lp,
3658 char_u *name_end,
3659 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 int ret = OK;
3662 int cc;
3663
3664 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 cc = *name_end;
3667 *name_end = NUL;
3668
3669 /* Normal name or expanded name. */
3670 if (check_changedtick(lp->ll_name))
3671 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003673 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003675 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003676 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003677 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003678 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003679 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003680 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003681 else if (lp->ll_range)
3682 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003683 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003684 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003685 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003686
3687 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3688 {
3689 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003690 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003691 return FAIL;
3692 ll_li = li;
3693 ++ll_n1;
3694 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695
3696 /* Delete a range of List items. */
3697 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3698 {
3699 li = lp->ll_li->li_next;
3700 listitem_remove(lp->ll_list, lp->ll_li);
3701 lp->ll_li = li;
3702 ++lp->ll_n1;
3703 }
3704 }
3705 else
3706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003707 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708 /* unlet a List item. */
3709 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003712 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 }
3714
3715 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003716}
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718/*
3719 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003720 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 */
3722 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003723do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 hashtab_T *ht;
3726 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003727 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003728 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003729 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 ht = find_var_ht(name, &varname);
3732 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003734 if (ht == &globvarht)
3735 d = &globvardict;
3736 else if (current_funccal != NULL
3737 && ht == &current_funccal->l_vars.dv_hashtab)
3738 d = &current_funccal->l_vars;
3739 else if (ht == &compat_hashtab)
3740 d = &vimvardict;
3741 else
3742 {
3743 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3744 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3745 }
3746 if (d == NULL)
3747 {
3748 EMSG2(_(e_intern2), "do_unlet()");
3749 return FAIL;
3750 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003751 hi = hash_find(ht, varname);
3752 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003753 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003754 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003755 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003756 || var_check_ro(di->di_flags, name, FALSE)
3757 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003758 return FAIL;
3759
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760 delete_var(ht, hi);
3761 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003764 if (forceit)
3765 return OK;
3766 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 return FAIL;
3768}
3769
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003770/*
3771 * Lock or unlock variable indicated by "lp".
3772 * "deep" is the levels to go (-1 for unlimited);
3773 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3774 */
3775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003776do_lock_var(
3777 lval_T *lp,
3778 char_u *name_end,
3779 int deep,
3780 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003781{
3782 int ret = OK;
3783 int cc;
3784 dictitem_T *di;
3785
3786 if (deep == 0) /* nothing to do */
3787 return OK;
3788
3789 if (lp->ll_tv == NULL)
3790 {
3791 cc = *name_end;
3792 *name_end = NUL;
3793
3794 /* Normal name or expanded name. */
3795 if (check_changedtick(lp->ll_name))
3796 ret = FAIL;
3797 else
3798 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003799 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800 if (di == NULL)
3801 ret = FAIL;
3802 else
3803 {
3804 if (lock)
3805 di->di_flags |= DI_FLAGS_LOCK;
3806 else
3807 di->di_flags &= ~DI_FLAGS_LOCK;
3808 item_lock(&di->di_tv, deep, lock);
3809 }
3810 }
3811 *name_end = cc;
3812 }
3813 else if (lp->ll_range)
3814 {
3815 listitem_T *li = lp->ll_li;
3816
3817 /* (un)lock a range of List items. */
3818 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3819 {
3820 item_lock(&li->li_tv, deep, lock);
3821 li = li->li_next;
3822 ++lp->ll_n1;
3823 }
3824 }
3825 else if (lp->ll_list != NULL)
3826 /* (un)lock a List item. */
3827 item_lock(&lp->ll_li->li_tv, deep, lock);
3828 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003829 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003830 item_lock(&lp->ll_di->di_tv, deep, lock);
3831
3832 return ret;
3833}
3834
3835/*
3836 * Lock or unlock an item. "deep" is nr of levels to go.
3837 */
3838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003839item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003840{
3841 static int recurse = 0;
3842 list_T *l;
3843 listitem_T *li;
3844 dict_T *d;
3845 hashitem_T *hi;
3846 int todo;
3847
3848 if (recurse >= DICT_MAXNEST)
3849 {
3850 EMSG(_("E743: variable nested too deep for (un)lock"));
3851 return;
3852 }
3853 if (deep == 0)
3854 return;
3855 ++recurse;
3856
3857 /* lock/unlock the item itself */
3858 if (lock)
3859 tv->v_lock |= VAR_LOCKED;
3860 else
3861 tv->v_lock &= ~VAR_LOCKED;
3862
3863 switch (tv->v_type)
3864 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003865 case VAR_UNKNOWN:
3866 case VAR_NUMBER:
3867 case VAR_STRING:
3868 case VAR_FUNC:
3869 case VAR_FLOAT:
3870 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003871 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003872 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003873 break;
3874
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003875 case VAR_LIST:
3876 if ((l = tv->vval.v_list) != NULL)
3877 {
3878 if (lock)
3879 l->lv_lock |= VAR_LOCKED;
3880 else
3881 l->lv_lock &= ~VAR_LOCKED;
3882 if (deep < 0 || deep > 1)
3883 /* recursive: lock/unlock the items the List contains */
3884 for (li = l->lv_first; li != NULL; li = li->li_next)
3885 item_lock(&li->li_tv, deep - 1, lock);
3886 }
3887 break;
3888 case VAR_DICT:
3889 if ((d = tv->vval.v_dict) != NULL)
3890 {
3891 if (lock)
3892 d->dv_lock |= VAR_LOCKED;
3893 else
3894 d->dv_lock &= ~VAR_LOCKED;
3895 if (deep < 0 || deep > 1)
3896 {
3897 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003898 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003899 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3900 {
3901 if (!HASHITEM_EMPTY(hi))
3902 {
3903 --todo;
3904 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3905 }
3906 }
3907 }
3908 }
3909 }
3910 --recurse;
3911}
3912
Bram Moolenaara40058a2005-07-11 22:42:07 +00003913/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003914 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3915 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003916 */
3917 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003918tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003919{
3920 return (tv->v_lock & VAR_LOCKED)
3921 || (tv->v_type == VAR_LIST
3922 && tv->vval.v_list != NULL
3923 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3924 || (tv->v_type == VAR_DICT
3925 && tv->vval.v_dict != NULL
3926 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3927}
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3930/*
3931 * Delete all "menutrans_" variables.
3932 */
3933 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003934del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003940 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 {
3943 if (!HASHITEM_EMPTY(hi))
3944 {
3945 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3947 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 }
3949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951}
3952#endif
3953
3954#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3955
3956/*
3957 * Local string buffer for the next two functions to store a variable name
3958 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3959 * get_user_var_name().
3960 */
3961
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003962static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
3964static char_u *varnamebuf = NULL;
3965static int varnamebuflen = 0;
3966
3967/*
3968 * Function to concatenate a prefix and a variable name.
3969 */
3970 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003971cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
3973 int len;
3974
3975 len = (int)STRLEN(name) + 3;
3976 if (len > varnamebuflen)
3977 {
3978 vim_free(varnamebuf);
3979 len += 10; /* some additional space */
3980 varnamebuf = alloc(len);
3981 if (varnamebuf == NULL)
3982 {
3983 varnamebuflen = 0;
3984 return NULL;
3985 }
3986 varnamebuflen = len;
3987 }
3988 *varnamebuf = prefix;
3989 varnamebuf[1] = ':';
3990 STRCPY(varnamebuf + 2, name);
3991 return varnamebuf;
3992}
3993
3994/*
3995 * Function given to ExpandGeneric() to obtain the list of user defined
3996 * (global/buffer/window/built-in) variable names.
3997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003999get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004001 static long_u gdone;
4002 static long_u bdone;
4003 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004004#ifdef FEAT_WINDOWS
4005 static long_u tdone;
4006#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 static int vidx;
4008 static hashitem_T *hi;
4009 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
4011 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004012 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004013 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004014#ifdef FEAT_WINDOWS
4015 tdone = 0;
4016#endif
4017 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004018
4019 /* Global variables */
4020 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004022 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004023 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004024 else
4025 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004026 while (HASHITEM_EMPTY(hi))
4027 ++hi;
4028 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4029 return cat_prefix_varname('g', hi->hi_key);
4030 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004032
4033 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004034 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004039 else
4040 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004041 while (HASHITEM_EMPTY(hi))
4042 ++hi;
4043 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004045 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 return (char_u *)"b:changedtick";
4049 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004050
4051 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004052 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004055 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004057 else
4058 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004059 while (HASHITEM_EMPTY(hi))
4060 ++hi;
4061 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004063
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004064#ifdef FEAT_WINDOWS
4065 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004066 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067 if (tdone < ht->ht_used)
4068 {
4069 if (tdone++ == 0)
4070 hi = ht->ht_array;
4071 else
4072 ++hi;
4073 while (HASHITEM_EMPTY(hi))
4074 ++hi;
4075 return cat_prefix_varname('t', hi->hi_key);
4076 }
4077#endif
4078
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 /* v: variables */
4080 if (vidx < VV_LEN)
4081 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083 vim_free(varnamebuf);
4084 varnamebuf = NULL;
4085 varnamebuflen = 0;
4086 return NULL;
4087}
4088
4089#endif /* FEAT_CMDL_COMPL */
4090
4091/*
4092 * types for expressions.
4093 */
4094typedef enum
4095{
4096 TYPE_UNKNOWN = 0
4097 , TYPE_EQUAL /* == */
4098 , TYPE_NEQUAL /* != */
4099 , TYPE_GREATER /* > */
4100 , TYPE_GEQUAL /* >= */
4101 , TYPE_SMALLER /* < */
4102 , TYPE_SEQUAL /* <= */
4103 , TYPE_MATCH /* =~ */
4104 , TYPE_NOMATCH /* !~ */
4105} exptype_T;
4106
4107/*
4108 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4111 */
4112
4113/*
4114 * Handle zero level expression.
4115 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004117 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 * Return OK or FAIL.
4119 */
4120 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004121eval0(
4122 char_u *arg,
4123 typval_T *rettv,
4124 char_u **nextcmd,
4125 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126{
4127 int ret;
4128 char_u *p;
4129
4130 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (ret == FAIL || !ends_excmd(*p))
4133 {
4134 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 /*
4137 * Report the invalid expression unless the expression evaluation has
4138 * been cancelled due to an aborting error, an interrupt, or an
4139 * exception.
4140 */
4141 if (!aborting())
4142 EMSG2(_(e_invexpr2), arg);
4143 ret = FAIL;
4144 }
4145 if (nextcmd != NULL)
4146 *nextcmd = check_nextcmd(p);
4147
4148 return ret;
4149}
4150
4151/*
4152 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004153 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 *
4155 * "arg" must point to the first non-white of the expression.
4156 * "arg" is advanced to the next non-white after the recognized expression.
4157 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004158 * Note: "rettv.v_lock" is not set.
4159 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 * Return OK or FAIL.
4161 */
4162 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004163eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
4165 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004166 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
4168 /*
4169 * Get the first variable.
4170 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 return FAIL;
4173
4174 if ((*arg)[0] == '?')
4175 {
4176 result = FALSE;
4177 if (evaluate)
4178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 int error = FALSE;
4180
4181 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 if (error)
4185 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187
4188 /*
4189 * Get the second variable.
4190 */
4191 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 return FAIL;
4194
4195 /*
4196 * Check for the ":".
4197 */
4198 if ((*arg)[0] != ':')
4199 {
4200 EMSG(_("E109: Missing ':' after '?'"));
4201 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 return FAIL;
4204 }
4205
4206 /*
4207 * Get the third variable.
4208 */
4209 *arg = skipwhite(*arg + 1);
4210 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4211 {
4212 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return FAIL;
4215 }
4216 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219
4220 return OK;
4221}
4222
4223/*
4224 * Handle first level expression:
4225 * expr2 || expr2 || expr2 logical OR
4226 *
4227 * "arg" must point to the first non-white of the expression.
4228 * "arg" is advanced to the next non-white after the recognized expression.
4229 *
4230 * Return OK or FAIL.
4231 */
4232 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004233eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
Bram Moolenaar33570922005-01-25 22:26:29 +00004235 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 long result;
4237 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
4240 /*
4241 * Get the first variable.
4242 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 return FAIL;
4245
4246 /*
4247 * Repeat until there is no following "||".
4248 */
4249 first = TRUE;
4250 result = FALSE;
4251 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4252 {
4253 if (evaluate && first)
4254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (error)
4259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 first = FALSE;
4261 }
4262
4263 /*
4264 * Get the second variable.
4265 */
4266 *arg = skipwhite(*arg + 2);
4267 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4268 return FAIL;
4269
4270 /*
4271 * Compute the result.
4272 */
4273 if (evaluate && !result)
4274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 if (error)
4279 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 if (evaluate)
4282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 rettv->v_type = VAR_NUMBER;
4284 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287
4288 return OK;
4289}
4290
4291/*
4292 * Handle second level expression:
4293 * expr3 && expr3 && expr3 logical AND
4294 *
4295 * "arg" must point to the first non-white of the expression.
4296 * "arg" is advanced to the next non-white after the recognized expression.
4297 *
4298 * Return OK or FAIL.
4299 */
4300 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004301eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302{
Bram Moolenaar33570922005-01-25 22:26:29 +00004303 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 long result;
4305 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 /*
4309 * Get the first variable.
4310 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return FAIL;
4313
4314 /*
4315 * Repeat until there is no following "&&".
4316 */
4317 first = TRUE;
4318 result = TRUE;
4319 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4320 {
4321 if (evaluate && first)
4322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 if (error)
4327 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 first = FALSE;
4329 }
4330
4331 /*
4332 * Get the second variable.
4333 */
4334 *arg = skipwhite(*arg + 2);
4335 if (eval4(arg, &var2, evaluate && result) == FAIL)
4336 return FAIL;
4337
4338 /*
4339 * Compute the result.
4340 */
4341 if (evaluate && result)
4342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 if (error)
4347 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 if (evaluate)
4350 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004351 rettv->v_type = VAR_NUMBER;
4352 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 }
4355
4356 return OK;
4357}
4358
4359/*
4360 * Handle third level expression:
4361 * var1 == var2
4362 * var1 =~ var2
4363 * var1 != var2
4364 * var1 !~ var2
4365 * var1 > var2
4366 * var1 >= var2
4367 * var1 < var2
4368 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004369 * var1 is var2
4370 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 *
4372 * "arg" must point to the first non-white of the expression.
4373 * "arg" is advanced to the next non-white after the recognized expression.
4374 *
4375 * Return OK or FAIL.
4376 */
4377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004378eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379{
Bram Moolenaar33570922005-01-25 22:26:29 +00004380 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 char_u *p;
4382 int i;
4383 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004384 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 int len = 2;
4386 long n1, n2;
4387 char_u *s1, *s2;
4388 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4389 regmatch_T regmatch;
4390 int ic;
4391 char_u *save_cpo;
4392
4393 /*
4394 * Get the first variable.
4395 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return FAIL;
4398
4399 p = *arg;
4400 switch (p[0])
4401 {
4402 case '=': if (p[1] == '=')
4403 type = TYPE_EQUAL;
4404 else if (p[1] == '~')
4405 type = TYPE_MATCH;
4406 break;
4407 case '!': if (p[1] == '=')
4408 type = TYPE_NEQUAL;
4409 else if (p[1] == '~')
4410 type = TYPE_NOMATCH;
4411 break;
4412 case '>': if (p[1] != '=')
4413 {
4414 type = TYPE_GREATER;
4415 len = 1;
4416 }
4417 else
4418 type = TYPE_GEQUAL;
4419 break;
4420 case '<': if (p[1] != '=')
4421 {
4422 type = TYPE_SMALLER;
4423 len = 1;
4424 }
4425 else
4426 type = TYPE_SEQUAL;
4427 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004428 case 'i': if (p[1] == 's')
4429 {
4430 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4431 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004432 i = p[len];
4433 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 {
4435 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4436 type_is = TRUE;
4437 }
4438 }
4439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441
4442 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004443 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 */
4445 if (type != TYPE_UNKNOWN)
4446 {
4447 /* extra question mark appended: ignore case */
4448 if (p[len] == '?')
4449 {
4450 ic = TRUE;
4451 ++len;
4452 }
4453 /* extra '#' appended: match case */
4454 else if (p[len] == '#')
4455 {
4456 ic = FALSE;
4457 ++len;
4458 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004459 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 else
4461 ic = p_ic;
4462
4463 /*
4464 * Get the second variable.
4465 */
4466 *arg = skipwhite(p + len);
4467 if (eval5(arg, &var2, evaluate) == FAIL)
4468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004469 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 return FAIL;
4471 }
4472
4473 if (evaluate)
4474 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004475 if (type_is && rettv->v_type != var2.v_type)
4476 {
4477 /* For "is" a different type always means FALSE, for "notis"
4478 * it means TRUE. */
4479 n1 = (type == TYPE_NEQUAL);
4480 }
4481 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4482 {
4483 if (type_is)
4484 {
4485 n1 = (rettv->v_type == var2.v_type
4486 && rettv->vval.v_list == var2.vval.v_list);
4487 if (type == TYPE_NEQUAL)
4488 n1 = !n1;
4489 }
4490 else if (rettv->v_type != var2.v_type
4491 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4492 {
4493 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004494 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004496 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 clear_tv(rettv);
4498 clear_tv(&var2);
4499 return FAIL;
4500 }
4501 else
4502 {
4503 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004504 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4505 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 if (type == TYPE_NEQUAL)
4507 n1 = !n1;
4508 }
4509 }
4510
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004511 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4512 {
4513 if (type_is)
4514 {
4515 n1 = (rettv->v_type == var2.v_type
4516 && rettv->vval.v_dict == var2.vval.v_dict);
4517 if (type == TYPE_NEQUAL)
4518 n1 = !n1;
4519 }
4520 else if (rettv->v_type != var2.v_type
4521 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4522 {
4523 if (rettv->v_type != var2.v_type)
4524 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4525 else
4526 EMSG(_("E736: Invalid operation for Dictionary"));
4527 clear_tv(rettv);
4528 clear_tv(&var2);
4529 return FAIL;
4530 }
4531 else
4532 {
4533 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004534 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4535 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004536 if (type == TYPE_NEQUAL)
4537 n1 = !n1;
4538 }
4539 }
4540
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004541 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4542 {
4543 if (rettv->v_type != var2.v_type
4544 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4545 {
4546 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004547 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004548 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004549 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 clear_tv(rettv);
4551 clear_tv(&var2);
4552 return FAIL;
4553 }
4554 else
4555 {
4556 /* Compare two Funcrefs for being equal or unequal. */
4557 if (rettv->vval.v_string == NULL
4558 || var2.vval.v_string == NULL)
4559 n1 = FALSE;
4560 else
4561 n1 = STRCMP(rettv->vval.v_string,
4562 var2.vval.v_string) == 0;
4563 if (type == TYPE_NEQUAL)
4564 n1 = !n1;
4565 }
4566 }
4567
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004568#ifdef FEAT_FLOAT
4569 /*
4570 * If one of the two variables is a float, compare as a float.
4571 * When using "=~" or "!~", always compare as string.
4572 */
4573 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4574 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4575 {
4576 float_T f1, f2;
4577
4578 if (rettv->v_type == VAR_FLOAT)
4579 f1 = rettv->vval.v_float;
4580 else
4581 f1 = get_tv_number(rettv);
4582 if (var2.v_type == VAR_FLOAT)
4583 f2 = var2.vval.v_float;
4584 else
4585 f2 = get_tv_number(&var2);
4586 n1 = FALSE;
4587 switch (type)
4588 {
4589 case TYPE_EQUAL: n1 = (f1 == f2); break;
4590 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4591 case TYPE_GREATER: n1 = (f1 > f2); break;
4592 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4593 case TYPE_SMALLER: n1 = (f1 < f2); break;
4594 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4595 case TYPE_UNKNOWN:
4596 case TYPE_MATCH:
4597 case TYPE_NOMATCH: break; /* avoid gcc warning */
4598 }
4599 }
4600#endif
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 /*
4603 * If one of the two variables is a number, compare as a number.
4604 * When using "=~" or "!~", always compare as string.
4605 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004606 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 n1 = get_tv_number(rettv);
4610 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 switch (type)
4612 {
4613 case TYPE_EQUAL: n1 = (n1 == n2); break;
4614 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4615 case TYPE_GREATER: n1 = (n1 > n2); break;
4616 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4617 case TYPE_SMALLER: n1 = (n1 < n2); break;
4618 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4619 case TYPE_UNKNOWN:
4620 case TYPE_MATCH:
4621 case TYPE_NOMATCH: break; /* avoid gcc warning */
4622 }
4623 }
4624 else
4625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 s1 = get_tv_string_buf(rettv, buf1);
4627 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4629 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4630 else
4631 i = 0;
4632 n1 = FALSE;
4633 switch (type)
4634 {
4635 case TYPE_EQUAL: n1 = (i == 0); break;
4636 case TYPE_NEQUAL: n1 = (i != 0); break;
4637 case TYPE_GREATER: n1 = (i > 0); break;
4638 case TYPE_GEQUAL: n1 = (i >= 0); break;
4639 case TYPE_SMALLER: n1 = (i < 0); break;
4640 case TYPE_SEQUAL: n1 = (i <= 0); break;
4641
4642 case TYPE_MATCH:
4643 case TYPE_NOMATCH:
4644 /* avoid 'l' flag in 'cpoptions' */
4645 save_cpo = p_cpo;
4646 p_cpo = (char_u *)"";
4647 regmatch.regprog = vim_regcomp(s2,
4648 RE_MAGIC + RE_STRING);
4649 regmatch.rm_ic = ic;
4650 if (regmatch.regprog != NULL)
4651 {
4652 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004653 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 if (type == TYPE_NOMATCH)
4655 n1 = !n1;
4656 }
4657 p_cpo = save_cpo;
4658 break;
4659
4660 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4661 }
4662 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004663 clear_tv(rettv);
4664 clear_tv(&var2);
4665 rettv->v_type = VAR_NUMBER;
4666 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
4668 }
4669
4670 return OK;
4671}
4672
4673/*
4674 * Handle fourth level expression:
4675 * + number addition
4676 * - number subtraction
4677 * . string concatenation
4678 *
4679 * "arg" must point to the first non-white of the expression.
4680 * "arg" is advanced to the next non-white after the recognized expression.
4681 *
4682 * Return OK or FAIL.
4683 */
4684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004685eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
Bram Moolenaar33570922005-01-25 22:26:29 +00004687 typval_T var2;
4688 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 int op;
4690 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004691#ifdef FEAT_FLOAT
4692 float_T f1 = 0, f2 = 0;
4693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 char_u *s1, *s2;
4695 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4696 char_u *p;
4697
4698 /*
4699 * Get the first variable.
4700 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004701 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 return FAIL;
4703
4704 /*
4705 * Repeat computing, until no '+', '-' or '.' is following.
4706 */
4707 for (;;)
4708 {
4709 op = **arg;
4710 if (op != '+' && op != '-' && op != '.')
4711 break;
4712
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713 if ((op != '+' || rettv->v_type != VAR_LIST)
4714#ifdef FEAT_FLOAT
4715 && (op == '.' || rettv->v_type != VAR_FLOAT)
4716#endif
4717 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 {
4719 /* For "list + ...", an illegal use of the first operand as
4720 * a number cannot be determined before evaluating the 2nd
4721 * operand: if this is also a list, all is ok.
4722 * For "something . ...", "something - ..." or "non-list + ...",
4723 * we know that the first operand needs to be a string or number
4724 * without evaluating the 2nd operand. So check before to avoid
4725 * side effects after an error. */
4726 if (evaluate && get_tv_string_chk(rettv) == NULL)
4727 {
4728 clear_tv(rettv);
4729 return FAIL;
4730 }
4731 }
4732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 /*
4734 * Get the second variable.
4735 */
4736 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004737 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004739 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 return FAIL;
4741 }
4742
4743 if (evaluate)
4744 {
4745 /*
4746 * Compute the result.
4747 */
4748 if (op == '.')
4749 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004750 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4751 s2 = get_tv_string_buf_chk(&var2, buf2);
4752 if (s2 == NULL) /* type error ? */
4753 {
4754 clear_tv(rettv);
4755 clear_tv(&var2);
4756 return FAIL;
4757 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004758 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004759 clear_tv(rettv);
4760 rettv->v_type = VAR_STRING;
4761 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004763 else if (op == '+' && rettv->v_type == VAR_LIST
4764 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004765 {
4766 /* concatenate Lists */
4767 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4768 &var3) == FAIL)
4769 {
4770 clear_tv(rettv);
4771 clear_tv(&var2);
4772 return FAIL;
4773 }
4774 clear_tv(rettv);
4775 *rettv = var3;
4776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 else
4778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004779 int error = FALSE;
4780
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004781#ifdef FEAT_FLOAT
4782 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004783 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004784 f1 = rettv->vval.v_float;
4785 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 else
4788#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004790 n1 = get_tv_number_chk(rettv, &error);
4791 if (error)
4792 {
4793 /* This can only happen for "list + non-list". For
4794 * "non-list + ..." or "something - ...", we returned
4795 * before evaluating the 2nd operand. */
4796 clear_tv(rettv);
4797 return FAIL;
4798 }
4799#ifdef FEAT_FLOAT
4800 if (var2.v_type == VAR_FLOAT)
4801 f1 = n1;
4802#endif
4803 }
4804#ifdef FEAT_FLOAT
4805 if (var2.v_type == VAR_FLOAT)
4806 {
4807 f2 = var2.vval.v_float;
4808 n2 = 0;
4809 }
4810 else
4811#endif
4812 {
4813 n2 = get_tv_number_chk(&var2, &error);
4814 if (error)
4815 {
4816 clear_tv(rettv);
4817 clear_tv(&var2);
4818 return FAIL;
4819 }
4820#ifdef FEAT_FLOAT
4821 if (rettv->v_type == VAR_FLOAT)
4822 f2 = n2;
4823#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004825 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004826
4827#ifdef FEAT_FLOAT
4828 /* If there is a float on either side the result is a float. */
4829 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4830 {
4831 if (op == '+')
4832 f1 = f1 + f2;
4833 else
4834 f1 = f1 - f2;
4835 rettv->v_type = VAR_FLOAT;
4836 rettv->vval.v_float = f1;
4837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839#endif
4840 {
4841 if (op == '+')
4842 n1 = n1 + n2;
4843 else
4844 n1 = n1 - n2;
4845 rettv->v_type = VAR_NUMBER;
4846 rettv->vval.v_number = n1;
4847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
4851 }
4852 return OK;
4853}
4854
4855/*
4856 * Handle fifth level expression:
4857 * * number multiplication
4858 * / number division
4859 * % number modulo
4860 *
4861 * "arg" must point to the first non-white of the expression.
4862 * "arg" is advanced to the next non-white after the recognized expression.
4863 *
4864 * Return OK or FAIL.
4865 */
4866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004867eval6(
4868 char_u **arg,
4869 typval_T *rettv,
4870 int evaluate,
4871 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872{
Bram Moolenaar33570922005-01-25 22:26:29 +00004873 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 int op;
4875 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876#ifdef FEAT_FLOAT
4877 int use_float = FALSE;
4878 float_T f1 = 0, f2;
4879#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004880 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882 /*
4883 * Get the first variable.
4884 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004885 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 return FAIL;
4887
4888 /*
4889 * Repeat computing, until no '*', '/' or '%' is following.
4890 */
4891 for (;;)
4892 {
4893 op = **arg;
4894 if (op != '*' && op != '/' && op != '%')
4895 break;
4896
4897 if (evaluate)
4898 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899#ifdef FEAT_FLOAT
4900 if (rettv->v_type == VAR_FLOAT)
4901 {
4902 f1 = rettv->vval.v_float;
4903 use_float = TRUE;
4904 n1 = 0;
4905 }
4906 else
4907#endif
4908 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004910 if (error)
4911 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 else
4914 n1 = 0;
4915
4916 /*
4917 * Get the second variable.
4918 */
4919 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004920 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 return FAIL;
4922
4923 if (evaluate)
4924 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004925#ifdef FEAT_FLOAT
4926 if (var2.v_type == VAR_FLOAT)
4927 {
4928 if (!use_float)
4929 {
4930 f1 = n1;
4931 use_float = TRUE;
4932 }
4933 f2 = var2.vval.v_float;
4934 n2 = 0;
4935 }
4936 else
4937#endif
4938 {
4939 n2 = get_tv_number_chk(&var2, &error);
4940 clear_tv(&var2);
4941 if (error)
4942 return FAIL;
4943#ifdef FEAT_FLOAT
4944 if (use_float)
4945 f2 = n2;
4946#endif
4947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
4949 /*
4950 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004951 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953#ifdef FEAT_FLOAT
4954 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 if (op == '*')
4957 f1 = f1 * f2;
4958 else if (op == '/')
4959 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004960# ifdef VMS
4961 /* VMS crashes on divide by zero, work around it */
4962 if (f2 == 0.0)
4963 {
4964 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004965 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 }
4971 else
4972 f1 = f1 / f2;
4973# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974 /* We rely on the floating point library to handle divide
4975 * by zero to result in "inf" and not a crash. */
4976 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004977# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004981 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 return FAIL;
4983 }
4984 rettv->v_type = VAR_FLOAT;
4985 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990 if (op == '*')
4991 n1 = n1 * n2;
4992 else if (op == '/')
4993 {
4994 if (n2 == 0) /* give an error message? */
4995 {
4996 if (n1 == 0)
4997 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4998 else if (n1 < 0)
4999 n1 = -0x7fffffffL;
5000 else
5001 n1 = 0x7fffffffL;
5002 }
5003 else
5004 n1 = n1 / n2;
5005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005007 {
5008 if (n2 == 0) /* give an error message? */
5009 n1 = 0;
5010 else
5011 n1 = n1 % n2;
5012 }
5013 rettv->v_type = VAR_NUMBER;
5014 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 }
5018
5019 return OK;
5020}
5021
5022/*
5023 * Handle sixth level expression:
5024 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005025 * "string" string constant
5026 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 * &option-name option value
5028 * @r register contents
5029 * identifier variable value
5030 * function() function call
5031 * $VAR environment variable
5032 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005033 * [expr, expr] List
5034 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 *
5036 * Also handle:
5037 * ! in front logical NOT
5038 * - in front unary minus
5039 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 * trailing [] subscript in String or List
5041 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 *
5043 * "arg" must point to the first non-white of the expression.
5044 * "arg" is advanced to the next non-white after the recognized expression.
5045 *
5046 * Return OK or FAIL.
5047 */
5048 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005049eval7(
5050 char_u **arg,
5051 typval_T *rettv,
5052 int evaluate,
5053 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 long n;
5056 int len;
5057 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 char_u *start_leader, *end_leader;
5059 int ret = OK;
5060 char_u *alias;
5061
5062 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005064 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068 /*
5069 * Skip '!' and '-' characters. They are handled later.
5070 */
5071 start_leader = *arg;
5072 while (**arg == '!' || **arg == '-' || **arg == '+')
5073 *arg = skipwhite(*arg + 1);
5074 end_leader = *arg;
5075
5076 switch (**arg)
5077 {
5078 /*
5079 * Number constant.
5080 */
5081 case '0':
5082 case '1':
5083 case '2':
5084 case '3':
5085 case '4':
5086 case '5':
5087 case '6':
5088 case '7':
5089 case '8':
5090 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005091 {
5092#ifdef FEAT_FLOAT
5093 char_u *p = skipdigits(*arg + 1);
5094 int get_float = FALSE;
5095
5096 /* We accept a float when the format matches
5097 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005098 * strict to avoid backwards compatibility problems.
5099 * Don't look for a float after the "." operator, so that
5100 * ":let vers = 1.2.3" doesn't fail. */
5101 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005103 get_float = TRUE;
5104 p = skipdigits(p + 2);
5105 if (*p == 'e' || *p == 'E')
5106 {
5107 ++p;
5108 if (*p == '-' || *p == '+')
5109 ++p;
5110 if (!vim_isdigit(*p))
5111 get_float = FALSE;
5112 else
5113 p = skipdigits(p + 1);
5114 }
5115 if (ASCII_ISALPHA(*p) || *p == '.')
5116 get_float = FALSE;
5117 }
5118 if (get_float)
5119 {
5120 float_T f;
5121
5122 *arg += string2float(*arg, &f);
5123 if (evaluate)
5124 {
5125 rettv->v_type = VAR_FLOAT;
5126 rettv->vval.v_float = f;
5127 }
5128 }
5129 else
5130#endif
5131 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005132 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005133 *arg += len;
5134 if (evaluate)
5135 {
5136 rettv->v_type = VAR_NUMBER;
5137 rettv->vval.v_number = n;
5138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142
5143 /*
5144 * String constant: "string".
5145 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005146 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 break;
5148
5149 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005152 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005153 break;
5154
5155 /*
5156 * List: [expr, expr]
5157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005158 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 break;
5160
5161 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 * Dictionary: {key: val, key: val}
5163 */
5164 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5165 break;
5166
5167 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005168 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 break;
5172
5173 /*
5174 * Environment variable: $VAR.
5175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178
5179 /*
5180 * Register contents: @r.
5181 */
5182 case '@': ++*arg;
5183 if (evaluate)
5184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005186 rettv->vval.v_string = get_reg_contents(**arg,
5187 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189 if (**arg != NUL)
5190 ++*arg;
5191 break;
5192
5193 /*
5194 * nested expression: (expression).
5195 */
5196 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005197 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (**arg == ')')
5199 ++*arg;
5200 else if (ret == OK)
5201 {
5202 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005203 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 ret = FAIL;
5205 }
5206 break;
5207
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 break;
5210 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211
5212 if (ret == NOTDONE)
5213 {
5214 /*
5215 * Must be a variable or function name.
5216 * Can also be a curly-braces kind of name: {expr}.
5217 */
5218 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005219 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 if (alias != NULL)
5221 s = alias;
5222
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005223 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 ret = FAIL;
5225 else
5226 {
5227 if (**arg == '(') /* recursive! */
5228 {
5229 /* If "s" is the name of a variable of type VAR_FUNC
5230 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005231 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232
5233 /* Invoke the function. */
5234 ret = get_func_tv(s, len, rettv, arg,
5235 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005236 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005237
5238 /* If evaluate is FALSE rettv->v_type was not set in
5239 * get_func_tv, but it's needed in handle_subscript() to parse
5240 * what follows. So set it here. */
5241 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5242 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005243 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005244 rettv->v_type = VAR_FUNC;
5245 }
5246
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 /* Stop the expression evaluation when immediately
5248 * aborting on error, or when an interrupt occurred or
5249 * an exception was thrown but not caught. */
5250 if (aborting())
5251 {
5252 if (ret == OK)
5253 clear_tv(rettv);
5254 ret = FAIL;
5255 }
5256 }
5257 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005258 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005259 else
5260 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005262 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 }
5264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 *arg = skipwhite(*arg);
5266
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005267 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5268 * expr(expr). */
5269 if (ret == OK)
5270 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
5272 /*
5273 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5274 */
5275 if (ret == OK && evaluate && end_leader > start_leader)
5276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005278 int val = 0;
5279#ifdef FEAT_FLOAT
5280 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005282 if (rettv->v_type == VAR_FLOAT)
5283 f = rettv->vval.v_float;
5284 else
5285#endif
5286 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005289 clear_tv(rettv);
5290 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 else
5293 {
5294 while (end_leader > start_leader)
5295 {
5296 --end_leader;
5297 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005298 {
5299#ifdef FEAT_FLOAT
5300 if (rettv->v_type == VAR_FLOAT)
5301 f = !f;
5302 else
5303#endif
5304 val = !val;
5305 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005307 {
5308#ifdef FEAT_FLOAT
5309 if (rettv->v_type == VAR_FLOAT)
5310 f = -f;
5311 else
5312#endif
5313 val = -val;
5314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005315 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005316#ifdef FEAT_FLOAT
5317 if (rettv->v_type == VAR_FLOAT)
5318 {
5319 clear_tv(rettv);
5320 rettv->vval.v_float = f;
5321 }
5322 else
5323#endif
5324 {
5325 clear_tv(rettv);
5326 rettv->v_type = VAR_NUMBER;
5327 rettv->vval.v_number = val;
5328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331
5332 return ret;
5333}
5334
5335/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005336 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5337 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5339 */
5340 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005341eval_index(
5342 char_u **arg,
5343 typval_T *rettv,
5344 int evaluate,
5345 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346{
5347 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005348 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 long len = -1;
5351 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354
Bram Moolenaara03f2332016-02-06 18:09:59 +01005355 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005357 case VAR_FUNC:
5358 if (verbose)
5359 EMSG(_("E695: Cannot index a Funcref"));
5360 return FAIL;
5361 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005362#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 if (verbose)
5364 EMSG(_(e_float_as_string));
5365 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005366#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005367 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005368 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005369 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005370 if (verbose)
5371 EMSG(_("E909: Cannot index a special variable"));
5372 return FAIL;
5373 case VAR_UNKNOWN:
5374 if (evaluate)
5375 return FAIL;
5376 /* FALLTHROUGH */
5377
5378 case VAR_STRING:
5379 case VAR_NUMBER:
5380 case VAR_LIST:
5381 case VAR_DICT:
5382 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005383 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005385 init_tv(&var1);
5386 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 /*
5390 * dict.name
5391 */
5392 key = *arg + 1;
5393 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5394 ;
5395 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005397 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 }
5399 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 /*
5402 * something[idx]
5403 *
5404 * Get the (first) variable from inside the [].
5405 */
5406 *arg = skipwhite(*arg + 1);
5407 if (**arg == ':')
5408 empty1 = TRUE;
5409 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5410 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005411 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5412 {
5413 /* not a number or string */
5414 clear_tv(&var1);
5415 return FAIL;
5416 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417
5418 /*
5419 * Get the second variable from inside the [:].
5420 */
5421 if (**arg == ':')
5422 {
5423 range = TRUE;
5424 *arg = skipwhite(*arg + 1);
5425 if (**arg == ']')
5426 empty2 = TRUE;
5427 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005429 if (!empty1)
5430 clear_tv(&var1);
5431 return FAIL;
5432 }
5433 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5434 {
5435 /* not a number or string */
5436 if (!empty1)
5437 clear_tv(&var1);
5438 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439 return FAIL;
5440 }
5441 }
5442
5443 /* Check for the ']'. */
5444 if (**arg != ']')
5445 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005446 if (verbose)
5447 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 clear_tv(&var1);
5449 if (range)
5450 clear_tv(&var2);
5451 return FAIL;
5452 }
5453 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455
5456 if (evaluate)
5457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 n1 = 0;
5459 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 n1 = get_tv_number(&var1);
5462 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 }
5464 if (range)
5465 {
5466 if (empty2)
5467 n2 = -1;
5468 else
5469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 n2 = get_tv_number(&var2);
5471 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 }
5473 }
5474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005477 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005478 case VAR_FUNC:
5479 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005480 case VAR_SPECIAL:
5481 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005482 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005483 break; /* not evaluating, skipping over subscript */
5484
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 case VAR_NUMBER:
5486 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488 len = (long)STRLEN(s);
5489 if (range)
5490 {
5491 /* The resulting variable is a substring. If the indexes
5492 * are out of range the result is empty. */
5493 if (n1 < 0)
5494 {
5495 n1 = len + n1;
5496 if (n1 < 0)
5497 n1 = 0;
5498 }
5499 if (n2 < 0)
5500 n2 = len + n2;
5501 else if (n2 >= len)
5502 n2 = len;
5503 if (n1 >= len || n2 < 0 || n1 > n2)
5504 s = NULL;
5505 else
5506 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5507 }
5508 else
5509 {
5510 /* The resulting variable is a string of a single
5511 * character. If the index is too big or negative the
5512 * result is empty. */
5513 if (n1 >= len || n1 < 0)
5514 s = NULL;
5515 else
5516 s = vim_strnsave(s + n1, 1);
5517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518 clear_tv(rettv);
5519 rettv->v_type = VAR_STRING;
5520 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 break;
5522
5523 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 if (n1 < 0)
5526 n1 = len + n1;
5527 if (!empty1 && (n1 < 0 || n1 >= len))
5528 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005529 /* For a range we allow invalid values and return an empty
5530 * list. A list index out of range is an error. */
5531 if (!range)
5532 {
5533 if (verbose)
5534 EMSGN(_(e_listidx), n1);
5535 return FAIL;
5536 }
5537 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 }
5539 if (range)
5540 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005541 list_T *l;
5542 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543
5544 if (n2 < 0)
5545 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005546 else if (n2 >= len)
5547 n2 = len - 1;
5548 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005549 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 l = list_alloc();
5551 if (l == NULL)
5552 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 n1 <= n2; ++n1)
5555 {
5556 if (list_append_tv(l, &item->li_tv) == FAIL)
5557 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005558 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 return FAIL;
5560 }
5561 item = item->li_next;
5562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 clear_tv(rettv);
5564 rettv->v_type = VAR_LIST;
5565 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005566 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 }
5568 else
5569 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005570 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 clear_tv(rettv);
5572 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 }
5574 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575
5576 case VAR_DICT:
5577 if (range)
5578 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005579 if (verbose)
5580 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 if (len == -1)
5582 clear_tv(&var1);
5583 return FAIL;
5584 }
5585 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587
5588 if (len == -1)
5589 {
5590 key = get_tv_string(&var1);
5591 if (*key == NUL)
5592 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005593 if (verbose)
5594 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 clear_tv(&var1);
5596 return FAIL;
5597 }
5598 }
5599
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005600 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005602 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005603 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 if (len == -1)
5605 clear_tv(&var1);
5606 if (item == NULL)
5607 return FAIL;
5608
5609 copy_tv(&item->di_tv, &var1);
5610 clear_tv(rettv);
5611 *rettv = var1;
5612 }
5613 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 }
5615 }
5616
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 return OK;
5618}
5619
5620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 * Get an option value.
5622 * "arg" points to the '&' or '+' before the option name.
5623 * "arg" is advanced to character after the option name.
5624 * Return OK or FAIL.
5625 */
5626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005627get_option_tv(
5628 char_u **arg,
5629 typval_T *rettv, /* when NULL, only check if option exists */
5630 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 char_u *option_end;
5633 long numval;
5634 char_u *stringval;
5635 int opt_type;
5636 int c;
5637 int working = (**arg == '+'); /* has("+option") */
5638 int ret = OK;
5639 int opt_flags;
5640
5641 /*
5642 * Isolate the option name and find its value.
5643 */
5644 option_end = find_option_end(arg, &opt_flags);
5645 if (option_end == NULL)
5646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 EMSG2(_("E112: Option name missing: %s"), *arg);
5649 return FAIL;
5650 }
5651
5652 if (!evaluate)
5653 {
5654 *arg = option_end;
5655 return OK;
5656 }
5657
5658 c = *option_end;
5659 *option_end = NUL;
5660 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005661 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
5663 if (opt_type == -3) /* invalid name */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 EMSG2(_("E113: Unknown option: %s"), *arg);
5667 ret = FAIL;
5668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 if (opt_type == -2) /* hidden string option */
5672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 rettv->v_type = VAR_STRING;
5674 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
5676 else if (opt_type == -1) /* hidden number option */
5677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 rettv->v_type = VAR_NUMBER;
5679 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681 else if (opt_type == 1) /* number option */
5682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 rettv->v_type = VAR_NUMBER;
5684 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 else /* string option */
5687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688 rettv->v_type = VAR_STRING;
5689 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 }
5692 else if (working && (opt_type == -2 || opt_type == -1))
5693 ret = FAIL;
5694
5695 *option_end = c; /* put back for error messages */
5696 *arg = option_end;
5697
5698 return ret;
5699}
5700
5701/*
5702 * Allocate a variable for a string constant.
5703 * Return OK or FAIL.
5704 */
5705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005706get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 char_u *p;
5709 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 int extra = 0;
5711
5712 /*
5713 * Find the end of the string, skipping backslashed characters.
5714 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
5717 if (*p == '\\' && p[1] != NUL)
5718 {
5719 ++p;
5720 /* A "\<x>" form occupies at least 4 characters, and produces up
5721 * to 6 characters: reserve space for 2 extra */
5722 if (*p == '<')
5723 extra += 2;
5724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726
5727 if (*p != '"')
5728 {
5729 EMSG2(_("E114: Missing quote: %s"), *arg);
5730 return FAIL;
5731 }
5732
5733 /* If only parsing, set *arg and return here */
5734 if (!evaluate)
5735 {
5736 *arg = p + 1;
5737 return OK;
5738 }
5739
5740 /*
5741 * Copy the string into allocated memory, handling backslashed
5742 * characters.
5743 */
5744 name = alloc((unsigned)(p - *arg + extra));
5745 if (name == NULL)
5746 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 rettv->v_type = VAR_STRING;
5748 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 if (*p == '\\')
5753 {
5754 switch (*++p)
5755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 case 'b': *name++ = BS; ++p; break;
5757 case 'e': *name++ = ESC; ++p; break;
5758 case 'f': *name++ = FF; ++p; break;
5759 case 'n': *name++ = NL; ++p; break;
5760 case 'r': *name++ = CAR; ++p; break;
5761 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763 case 'X': /* hex: "\x1", "\x12" */
5764 case 'x':
5765 case 'u': /* Unicode: "\u0023" */
5766 case 'U':
5767 if (vim_isxdigit(p[1]))
5768 {
5769 int n, nr;
5770 int c = toupper(*p);
5771
5772 if (c == 'X')
5773 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005774 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005776 else
5777 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 nr = 0;
5779 while (--n >= 0 && vim_isxdigit(p[1]))
5780 {
5781 ++p;
5782 nr = (nr << 4) + hex2nr(*p);
5783 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785#ifdef FEAT_MBYTE
5786 /* For "\u" store the number according to
5787 * 'encoding'. */
5788 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 else
5791#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 break;
5795
5796 /* octal: "\1", "\12", "\123" */
5797 case '0':
5798 case '1':
5799 case '2':
5800 case '3':
5801 case '4':
5802 case '5':
5803 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 case '7': *name = *p++ - '0';
5805 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 *name = (*name << 3) + *p++ - '0';
5808 if (*p >= '0' && *p <= '7')
5809 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 break;
5813
5814 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if (extra != 0)
5817 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 break;
5820 }
5821 /* FALLTHROUGH */
5822
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 break;
5825 }
5826 }
5827 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 *arg = p + 1;
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 return OK;
5835}
5836
5837/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 * Return OK or FAIL.
5840 */
5841 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005842get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843{
5844 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 int reduce = 0;
5847
5848 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 */
5851 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 break;
5857 ++reduce;
5858 ++p;
5859 }
5860 }
5861
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 return FAIL;
5866 }
5867
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 if (!evaluate)
5870 {
5871 *arg = p + 1;
5872 return OK;
5873 }
5874
5875 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 */
5878 str = alloc((unsigned)((p - *arg) - reduce));
5879 if (str == NULL)
5880 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 rettv->v_type = VAR_STRING;
5882 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 break;
5890 ++p;
5891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 *arg = p + 1;
5896
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 return OK;
5898}
5899
5900/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 * Allocate a variable for a List and fill it from "*arg".
5902 * Return OK or FAIL.
5903 */
5904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005905get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906{
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 list_T *l = NULL;
5908 typval_T tv;
5909 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910
5911 if (evaluate)
5912 {
5913 l = list_alloc();
5914 if (l == NULL)
5915 return FAIL;
5916 }
5917
5918 *arg = skipwhite(*arg + 1);
5919 while (**arg != ']' && **arg != NUL)
5920 {
5921 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5922 goto failret;
5923 if (evaluate)
5924 {
5925 item = listitem_alloc();
5926 if (item != NULL)
5927 {
5928 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005929 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 list_append(l, item);
5931 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005932 else
5933 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 }
5935
5936 if (**arg == ']')
5937 break;
5938 if (**arg != ',')
5939 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 goto failret;
5942 }
5943 *arg = skipwhite(*arg + 1);
5944 }
5945
5946 if (**arg != ']')
5947 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005948 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949failret:
5950 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005951 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 return FAIL;
5953 }
5954
5955 *arg = skipwhite(*arg + 1);
5956 if (evaluate)
5957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005958 rettv->v_type = VAR_LIST;
5959 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 ++l->lv_refcount;
5961 }
5962
5963 return OK;
5964}
5965
5966/*
5967 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005968 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005970 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005973 list_T *l;
5974
5975 l = (list_T *)alloc_clear(sizeof(list_T));
5976 if (l != NULL)
5977 {
5978 /* Prepend the list to the list of lists for garbage collection. */
5979 if (first_list != NULL)
5980 first_list->lv_used_prev = l;
5981 l->lv_used_prev = NULL;
5982 l->lv_used_next = first_list;
5983 first_list = l;
5984 }
5985 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986}
5987
5988/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005989 * Allocate an empty list for a return value.
5990 * Returns OK or FAIL.
5991 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005992 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005993rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005994{
5995 list_T *l = list_alloc();
5996
5997 if (l == NULL)
5998 return FAIL;
5999
6000 rettv->vval.v_list = l;
6001 rettv->v_type = VAR_LIST;
6002 ++l->lv_refcount;
6003 return OK;
6004}
6005
6006/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 * Unreference a list: decrement the reference count and free it when it
6008 * becomes zero.
6009 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006011list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006013 if (l != NULL && --l->lv_refcount <= 0)
6014 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015}
6016
6017/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006018 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 * Ignores the reference count.
6020 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006022list_free(
6023 list_T *l,
6024 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025{
Bram Moolenaar33570922005-01-25 22:26:29 +00006026 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006028 /* Remove the list from the list of lists for garbage collection. */
6029 if (l->lv_used_prev == NULL)
6030 first_list = l->lv_used_next;
6031 else
6032 l->lv_used_prev->lv_used_next = l->lv_used_next;
6033 if (l->lv_used_next != NULL)
6034 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6035
Bram Moolenaard9fba312005-06-26 22:34:35 +00006036 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006038 /* Remove the item before deleting it. */
6039 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006040 if (recurse || (item->li_tv.v_type != VAR_LIST
6041 && item->li_tv.v_type != VAR_DICT))
6042 clear_tv(&item->li_tv);
6043 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 }
6045 vim_free(l);
6046}
6047
6048/*
6049 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006050 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006052 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006053listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaar33570922005-01-25 22:26:29 +00006055 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056}
6057
6058/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006059 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006062listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006064 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 vim_free(item);
6066}
6067
6068/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006069 * Remove a list item from a List and free it. Also clears the value.
6070 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006072listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006073{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006074 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075 listitem_free(item);
6076}
6077
6078/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079 * Get the number of items in a list.
6080 */
6081 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006082list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 if (l == NULL)
6085 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006086 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087}
6088
6089/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 * Return TRUE when two lists have exactly the same values.
6091 */
6092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006093list_equal(
6094 list_T *l1,
6095 list_T *l2,
6096 int ic, /* ignore case for strings */
6097 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098{
Bram Moolenaar33570922005-01-25 22:26:29 +00006099 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006101 if (l1 == NULL || l2 == NULL)
6102 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006103 if (l1 == l2)
6104 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006105 if (list_len(l1) != list_len(l2))
6106 return FALSE;
6107
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108 for (item1 = l1->lv_first, item2 = l2->lv_first;
6109 item1 != NULL && item2 != NULL;
6110 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112 return FALSE;
6113 return item1 == NULL && item2 == NULL;
6114}
6115
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006116/*
6117 * Return the dictitem that an entry in a hashtable points to.
6118 */
6119 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006121{
6122 return HI2DI(hi);
6123}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006124
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006126 * Return TRUE when two dictionaries have exactly the same key/values.
6127 */
6128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006129dict_equal(
6130 dict_T *d1,
6131 dict_T *d2,
6132 int ic, /* ignore case for strings */
6133 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006134{
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 hashitem_T *hi;
6136 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006137 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006138
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006139 if (d1 == NULL || d2 == NULL)
6140 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006141 if (d1 == d2)
6142 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143 if (dict_len(d1) != dict_len(d2))
6144 return FALSE;
6145
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006146 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006149 if (!HASHITEM_EMPTY(hi))
6150 {
6151 item2 = dict_find(d2, hi->hi_key, -1);
6152 if (item2 == NULL)
6153 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006154 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 return FALSE;
6156 --todo;
6157 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006158 }
6159 return TRUE;
6160}
6161
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006162static int tv_equal_recurse_limit;
6163
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006164/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006165 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006166 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006167 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006168 */
6169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006170tv_equal(
6171 typval_T *tv1,
6172 typval_T *tv2,
6173 int ic, /* ignore case */
6174 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175{
6176 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006177 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006178 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006179 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006181 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006182 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006184 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185 * recursiveness to a limit. We guess they are equal then.
6186 * A fixed limit has the problem of still taking an awful long time.
6187 * Reduce the limit every time running into it. That should work fine for
6188 * deeply linked structures that are not recursively linked and catch
6189 * recursiveness quickly. */
6190 if (!recursive)
6191 tv_equal_recurse_limit = 1000;
6192 if (recursive_cnt >= tv_equal_recurse_limit)
6193 {
6194 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006195 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006197
6198 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006200 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201 ++recursive_cnt;
6202 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6203 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006204 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006205
6206 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 ++recursive_cnt;
6208 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6209 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006210 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006211
6212 case VAR_FUNC:
6213 return (tv1->vval.v_string != NULL
6214 && tv2->vval.v_string != NULL
6215 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6216
6217 case VAR_NUMBER:
6218 return tv1->vval.v_number == tv2->vval.v_number;
6219
6220 case VAR_STRING:
6221 s1 = get_tv_string_buf(tv1, buf1);
6222 s2 = get_tv_string_buf(tv2, buf2);
6223 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006224
6225 case VAR_SPECIAL:
6226 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006227
6228 case VAR_FLOAT:
6229#ifdef FEAT_FLOAT
6230 return tv1->vval.v_float == tv2->vval.v_float;
6231#endif
6232 case VAR_JOB:
6233#ifdef FEAT_JOB
6234 return tv1->vval.v_job == tv2->vval.v_job;
6235#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006236 case VAR_CHANNEL:
6237#ifdef FEAT_CHANNEL
6238 return tv1->vval.v_channel == tv2->vval.v_channel;
6239#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006240 case VAR_UNKNOWN:
6241 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006243
Bram Moolenaara03f2332016-02-06 18:09:59 +01006244 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6245 * does not equal anything, not even itself. */
6246 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006247}
6248
6249/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006250 * Locate item with index "n" in list "l" and return it.
6251 * A negative index is counted from the end; -1 is the last item.
6252 * Returns NULL when "n" is out of range.
6253 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006254 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006255list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256{
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258 long idx;
6259
6260 if (l == NULL)
6261 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006262
6263 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006265 n = l->lv_len + n;
6266
6267 /* Check for index out of range. */
6268 if (n < 0 || n >= l->lv_len)
6269 return NULL;
6270
6271 /* When there is a cached index may start search from there. */
6272 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006273 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006274 if (n < l->lv_idx / 2)
6275 {
6276 /* closest to the start of the list */
6277 item = l->lv_first;
6278 idx = 0;
6279 }
6280 else if (n > (l->lv_idx + l->lv_len) / 2)
6281 {
6282 /* closest to the end of the list */
6283 item = l->lv_last;
6284 idx = l->lv_len - 1;
6285 }
6286 else
6287 {
6288 /* closest to the cached index */
6289 item = l->lv_idx_item;
6290 idx = l->lv_idx;
6291 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292 }
6293 else
6294 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006295 if (n < l->lv_len / 2)
6296 {
6297 /* closest to the start of the list */
6298 item = l->lv_first;
6299 idx = 0;
6300 }
6301 else
6302 {
6303 /* closest to the end of the list */
6304 item = l->lv_last;
6305 idx = l->lv_len - 1;
6306 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006308
6309 while (n > idx)
6310 {
6311 /* search forward */
6312 item = item->li_next;
6313 ++idx;
6314 }
6315 while (n < idx)
6316 {
6317 /* search backward */
6318 item = item->li_prev;
6319 --idx;
6320 }
6321
6322 /* cache the used index */
6323 l->lv_idx = idx;
6324 l->lv_idx_item = item;
6325
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 return item;
6327}
6328
6329/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006330 * Get list item "l[idx]" as a number.
6331 */
6332 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006333list_find_nr(
6334 list_T *l,
6335 long idx,
6336 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006337{
6338 listitem_T *li;
6339
6340 li = list_find(l, idx);
6341 if (li == NULL)
6342 {
6343 if (errorp != NULL)
6344 *errorp = TRUE;
6345 return -1L;
6346 }
6347 return get_tv_number_chk(&li->li_tv, errorp);
6348}
6349
6350/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006351 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6352 */
6353 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006354list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006355{
6356 listitem_T *li;
6357
6358 li = list_find(l, idx - 1);
6359 if (li == NULL)
6360 {
6361 EMSGN(_(e_listidx), idx);
6362 return NULL;
6363 }
6364 return get_tv_string(&li->li_tv);
6365}
6366
6367/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006368 * Locate "item" list "l" and return its index.
6369 * Returns -1 when "item" is not in the list.
6370 */
6371 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006372list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006373{
6374 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006376
6377 if (l == NULL)
6378 return -1;
6379 idx = 0;
6380 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6381 ++idx;
6382 if (li == NULL)
6383 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006384 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006385}
6386
6387/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388 * Append item "item" to the end of list "l".
6389 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006391list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392{
6393 if (l->lv_last == NULL)
6394 {
6395 /* empty list */
6396 l->lv_first = item;
6397 l->lv_last = item;
6398 item->li_prev = NULL;
6399 }
6400 else
6401 {
6402 l->lv_last->li_next = item;
6403 item->li_prev = l->lv_last;
6404 l->lv_last = item;
6405 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006406 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 item->li_next = NULL;
6408}
6409
6410/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006414 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006415list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006417 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006421 copy_tv(tv, &li->li_tv);
6422 list_append(l, li);
6423 return OK;
6424}
6425
6426/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006427 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006428 * Return FAIL when out of memory.
6429 */
6430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006431list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432{
6433 listitem_T *li = listitem_alloc();
6434
6435 if (li == NULL)
6436 return FAIL;
6437 li->li_tv.v_type = VAR_DICT;
6438 li->li_tv.v_lock = 0;
6439 li->li_tv.vval.v_dict = dict;
6440 list_append(list, li);
6441 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 return OK;
6443}
6444
6445/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006446 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006447 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448 * Returns FAIL when out of memory.
6449 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006451list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006452{
6453 listitem_T *li = listitem_alloc();
6454
6455 if (li == NULL)
6456 return FAIL;
6457 list_append(l, li);
6458 li->li_tv.v_type = VAR_STRING;
6459 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006460 if (str == NULL)
6461 li->li_tv.vval.v_string = NULL;
6462 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006463 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006464 return FAIL;
6465 return OK;
6466}
6467
6468/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006469 * Append "n" to list "l".
6470 * Returns FAIL when out of memory.
6471 */
6472 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006473list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006474{
6475 listitem_T *li;
6476
6477 li = listitem_alloc();
6478 if (li == NULL)
6479 return FAIL;
6480 li->li_tv.v_type = VAR_NUMBER;
6481 li->li_tv.v_lock = 0;
6482 li->li_tv.vval.v_number = n;
6483 list_append(l, li);
6484 return OK;
6485}
6486
6487/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006488 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006489 * If "item" is NULL append at the end.
6490 * Return FAIL when out of memory.
6491 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006493list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494{
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496
6497 if (ni == NULL)
6498 return FAIL;
6499 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006500 list_insert(l, ni, item);
6501 return OK;
6502}
6503
6504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006505list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006506{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 if (item == NULL)
6508 /* Append new item at end of list. */
6509 list_append(l, ni);
6510 else
6511 {
6512 /* Insert new item before existing item. */
6513 ni->li_prev = item->li_prev;
6514 ni->li_next = item;
6515 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006516 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006518 ++l->lv_idx;
6519 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 l->lv_idx_item = NULL;
6524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528}
6529
6530/*
6531 * Extend "l1" with "l2".
6532 * If "bef" is NULL append at the end, otherwise insert before this item.
6533 * Returns FAIL when out of memory.
6534 */
6535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006536list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537{
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006539 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006541 /* We also quit the loop when we have inserted the original item count of
6542 * the list, avoid a hang when we extend a list with itself. */
6543 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6545 return FAIL;
6546 return OK;
6547}
6548
6549/*
6550 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6551 * Return FAIL when out of memory.
6552 */
6553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006554list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006558 if (l1 == NULL || l2 == NULL)
6559 return FAIL;
6560
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 if (l == NULL)
6564 return FAIL;
6565 tv->v_type = VAR_LIST;
6566 tv->vval.v_list = l;
6567
6568 /* append all items from the second list */
6569 return list_extend(l, l2, NULL);
6570}
6571
6572/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006573 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006575 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576 * Returns NULL when out of memory.
6577 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006579list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580{
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 list_T *copy;
6582 listitem_T *item;
6583 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584
6585 if (orig == NULL)
6586 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587
6588 copy = list_alloc();
6589 if (copy != NULL)
6590 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 if (copyID != 0)
6592 {
6593 /* Do this before adding the items, because one of the items may
6594 * refer back to this list. */
6595 orig->lv_copyID = copyID;
6596 orig->lv_copylist = copy;
6597 }
6598 for (item = orig->lv_first; item != NULL && !got_int;
6599 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 {
6601 ni = listitem_alloc();
6602 if (ni == NULL)
6603 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006604 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006605 {
6606 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6607 {
6608 vim_free(ni);
6609 break;
6610 }
6611 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006613 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 list_append(copy, ni);
6615 }
6616 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006617 if (item != NULL)
6618 {
6619 list_unref(copy);
6620 copy = NULL;
6621 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 }
6623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624 return copy;
6625}
6626
6627/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006628 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006629 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006630 * This used to be called list_remove, but that conflicts with a Sun header
6631 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006633 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006634vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635{
Bram Moolenaar33570922005-01-25 22:26:29 +00006636 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006638 /* notify watchers */
6639 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006641 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006642 list_fix_watch(l, ip);
6643 if (ip == item2)
6644 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646
6647 if (item2->li_next == NULL)
6648 l->lv_last = item->li_prev;
6649 else
6650 item2->li_next->li_prev = item->li_prev;
6651 if (item->li_prev == NULL)
6652 l->lv_first = item2->li_next;
6653 else
6654 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006655 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656}
6657
6658/*
6659 * Return an allocated string with the string representation of a list.
6660 * May return NULL.
6661 */
6662 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006663list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664{
6665 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666
6667 if (tv->vval.v_list == NULL)
6668 return NULL;
6669 ga_init2(&ga, (int)sizeof(char), 80);
6670 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006671 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006672 {
6673 vim_free(ga.ga_data);
6674 return NULL;
6675 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676 ga_append(&ga, ']');
6677 ga_append(&ga, NUL);
6678 return (char_u *)ga.ga_data;
6679}
6680
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006681typedef struct join_S {
6682 char_u *s;
6683 char_u *tofree;
6684} join_T;
6685
6686 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006687list_join_inner(
6688 garray_T *gap, /* to store the result in */
6689 list_T *l,
6690 char_u *sep,
6691 int echo_style,
6692 int copyID,
6693 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694{
6695 int i;
6696 join_T *p;
6697 int len;
6698 int sumlen = 0;
6699 int first = TRUE;
6700 char_u *tofree;
6701 char_u numbuf[NUMBUFLEN];
6702 listitem_T *item;
6703 char_u *s;
6704
6705 /* Stringify each item in the list. */
6706 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6707 {
6708 if (echo_style)
6709 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6710 else
6711 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6712 if (s == NULL)
6713 return FAIL;
6714
6715 len = (int)STRLEN(s);
6716 sumlen += len;
6717
Bram Moolenaarcde88542015-08-11 19:14:00 +02006718 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006719 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6720 if (tofree != NULL || s != numbuf)
6721 {
6722 p->s = s;
6723 p->tofree = tofree;
6724 }
6725 else
6726 {
6727 p->s = vim_strnsave(s, len);
6728 p->tofree = p->s;
6729 }
6730
6731 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006732 if (did_echo_string_emsg) /* recursion error, bail out */
6733 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006734 }
6735
6736 /* Allocate result buffer with its total size, avoid re-allocation and
6737 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6738 if (join_gap->ga_len >= 2)
6739 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6740 if (ga_grow(gap, sumlen + 2) == FAIL)
6741 return FAIL;
6742
6743 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6744 {
6745 if (first)
6746 first = FALSE;
6747 else
6748 ga_concat(gap, sep);
6749 p = ((join_T *)join_gap->ga_data) + i;
6750
6751 if (p->s != NULL)
6752 ga_concat(gap, p->s);
6753 line_breakcheck();
6754 }
6755
6756 return OK;
6757}
6758
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006759/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006760 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006761 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006762 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006763 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006765list_join(
6766 garray_T *gap,
6767 list_T *l,
6768 char_u *sep,
6769 int echo_style,
6770 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006771{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006772 garray_T join_ga;
6773 int retval;
6774 join_T *p;
6775 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776
Bram Moolenaard39a7512015-04-16 22:51:22 +02006777 if (l->lv_len < 1)
6778 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006779 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6780 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6781
6782 /* Dispose each item in join_ga. */
6783 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785 p = (join_T *)join_ga.ga_data;
6786 for (i = 0; i < join_ga.ga_len; ++i)
6787 {
6788 vim_free(p->tofree);
6789 ++p;
6790 }
6791 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006793
6794 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795}
6796
6797/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006798 * Return the next (unique) copy ID.
6799 * Used for serializing nested structures.
6800 */
6801 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006802get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006803{
6804 current_copyID += COPYID_INC;
6805 return current_copyID;
6806}
6807
6808/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006809 * Garbage collection for lists and dictionaries.
6810 *
6811 * We use reference counts to be able to free most items right away when they
6812 * are no longer used. But for composite items it's possible that it becomes
6813 * unused while the reference count is > 0: When there is a recursive
6814 * reference. Example:
6815 * :let l = [1, 2, 3]
6816 * :let d = {9: l}
6817 * :let l[1] = d
6818 *
6819 * Since this is quite unusual we handle this with garbage collection: every
6820 * once in a while find out which lists and dicts are not referenced from any
6821 * variable.
6822 *
6823 * Here is a good reference text about garbage collection (refers to Python
6824 * but it applies to all reference-counting mechanisms):
6825 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006826 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006827
6828/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829 * Do garbage collection for lists and dicts.
6830 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006831 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006833garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006834{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006836 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 buf_T *buf;
6838 win_T *wp;
6839 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006840 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006841 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006842 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006843#ifdef FEAT_WINDOWS
6844 tabpage_T *tp;
6845#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006846
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006847 /* Only do this once. */
6848 want_garbage_collect = FALSE;
6849 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006850 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006851
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006852 /* We advance by two because we add one for items referenced through
6853 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006854 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006855
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 /*
6857 * 1. Go through all accessible variables and mark all lists and dicts
6858 * with copyID.
6859 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860
6861 /* Don't free variables in the previous_funccal list unless they are only
6862 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006863 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6865 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006866 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6867 NULL);
6868 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6869 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 }
6871
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 /* script-local variables */
6873 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875
6876 /* buffer-local variables */
6877 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006878 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6879 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006880
6881 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006882 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6884 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006885#ifdef FEAT_AUTOCMD
6886 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006887 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6888 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006889#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006891#ifdef FEAT_WINDOWS
6892 /* tabpage-local variables */
6893 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006894 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6895 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006896#endif
6897
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900
6901 /* function-local variables */
6902 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6903 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6905 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906 }
6907
Bram Moolenaard812df62008-11-09 12:46:09 +00006908 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006910
Bram Moolenaar1dced572012-04-05 16:54:08 +02006911#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006913#endif
6914
Bram Moolenaardb913952012-06-29 12:54:53 +02006915#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006917#endif
6918
6919#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006921#endif
6922
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006923#ifdef FEAT_CHANNEL
6924 abort = abort || set_ref_in_channel(copyID);
6925#endif
6926
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006928 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 /*
6930 * 2. Free lists and dictionaries that are not referenced.
6931 */
6932 did_free = free_unref_items(copyID);
6933
6934 /*
6935 * 3. Check if any funccal can be freed now.
6936 */
6937 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006938 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 if (can_free_funccal(*pfc, copyID))
6940 {
6941 fc = *pfc;
6942 *pfc = fc->caller;
6943 free_funccal(fc, TRUE);
6944 did_free = TRUE;
6945 did_free_funccal = TRUE;
6946 }
6947 else
6948 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 if (did_free_funccal)
6951 /* When a funccal was freed some more items might be garbage
6952 * collected, so run again. */
6953 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 else if (p_verbose > 0)
6956 {
6957 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6958 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959
6960 return did_free;
6961}
6962
6963/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006964 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 */
6966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006967free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006969 dict_T *dd, *dd_next;
6970 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 int did_free = FALSE;
6972
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975 */
6976 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006977 {
6978 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006981 /* Free the Dictionary and ordinary items it contains, but don't
6982 * recurse into Lists and Dictionaries, they will be in the list
6983 * of dicts or list of lists. */
6984 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006987 dd = dd_next;
6988 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989
6990 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991 * Go through the list of lists and free items without the copyID.
6992 * But don't free a list that has a watcher (used in a for loop), these
6993 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 */
6995 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006996 {
6997 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6999 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007001 /* Free the List and ordinary items it contains, but don't recurse
7002 * into Lists and Dictionaries, they will be in the list of dicts
7003 * or list of lists. */
7004 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 ll = ll_next;
7008 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007009
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 return did_free;
7011}
7012
7013/*
7014 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007015 * "list_stack" is used to add lists to be marked. Can be NULL.
7016 *
7017 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007018 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007019 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007020set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021{
7022 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 hashtab_T *cur_ht;
7026 ht_stack_T *ht_stack = NULL;
7027 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 cur_ht = ht;
7030 for (;;)
7031 {
7032 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 /* Mark each item in the hashtab. If the item contains a hashtab
7035 * it is added to ht_stack, if it contains a list it is added to
7036 * list_stack. */
7037 todo = (int)cur_ht->ht_used;
7038 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7039 if (!HASHITEM_EMPTY(hi))
7040 {
7041 --todo;
7042 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7043 &ht_stack, list_stack);
7044 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046
7047 if (ht_stack == NULL)
7048 break;
7049
7050 /* take an item from the stack */
7051 cur_ht = ht_stack->ht;
7052 tempitem = ht_stack;
7053 ht_stack = ht_stack->prev;
7054 free(tempitem);
7055 }
7056
7057 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058}
7059
7060/*
7061 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7063 *
7064 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007065 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007067set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007069 listitem_T *li;
7070 int abort = FALSE;
7071 list_T *cur_l;
7072 list_stack_T *list_stack = NULL;
7073 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 cur_l = l;
7076 for (;;)
7077 {
7078 if (!abort)
7079 /* Mark each item in the list. If the item contains a hashtab
7080 * it is added to ht_stack, if it contains a list it is added to
7081 * list_stack. */
7082 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7083 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7084 ht_stack, &list_stack);
7085 if (list_stack == NULL)
7086 break;
7087
7088 /* take an item from the stack */
7089 cur_l = list_stack->list;
7090 tempitem = list_stack;
7091 list_stack = list_stack->prev;
7092 free(tempitem);
7093 }
7094
7095 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096}
7097
7098/*
7099 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007100 * "list_stack" is used to add lists to be marked. Can be NULL.
7101 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7102 *
7103 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007104 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007106set_ref_in_item(
7107 typval_T *tv,
7108 int copyID,
7109 ht_stack_T **ht_stack,
7110 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007111{
7112 dict_T *dd;
7113 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007114 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007115
Bram Moolenaara03f2332016-02-06 18:09:59 +01007116 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007117 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007118 dd = tv->vval.v_dict;
7119 if (dd != NULL && dd->dv_copyID != copyID)
7120 {
7121 /* Didn't see this dict yet. */
7122 dd->dv_copyID = copyID;
7123 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007124 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007125 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7126 }
7127 else
7128 {
7129 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7130 if (newitem == NULL)
7131 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 else
7133 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007134 newitem->ht = &dd->dv_hashtab;
7135 newitem->prev = *ht_stack;
7136 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007138 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007139 }
7140 }
7141 else if (tv->v_type == VAR_LIST)
7142 {
7143 ll = tv->vval.v_list;
7144 if (ll != NULL && ll->lv_copyID != copyID)
7145 {
7146 /* Didn't see this list yet. */
7147 ll->lv_copyID = copyID;
7148 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007149 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007150 abort = set_ref_in_list(ll, copyID, ht_stack);
7151 }
7152 else
7153 {
7154 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007155 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007156 if (newitem == NULL)
7157 abort = TRUE;
7158 else
7159 {
7160 newitem->list = ll;
7161 newitem->prev = *list_stack;
7162 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007163 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007164 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007165 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007167 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168}
7169
7170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 * Allocate an empty header for a dictionary.
7172 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007173 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007174dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175{
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007180 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007181 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007182 if (first_dict != NULL)
7183 first_dict->dv_used_prev = d;
7184 d->dv_used_next = first_dict;
7185 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007186 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007189 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007190 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007193 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007194 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007195}
7196
7197/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007198 * Allocate an empty dict for a return value.
7199 * Returns OK or FAIL.
7200 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007202rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007203{
7204 dict_T *d = dict_alloc();
7205
7206 if (d == NULL)
7207 return FAIL;
7208
7209 rettv->vval.v_dict = d;
7210 rettv->v_type = VAR_DICT;
7211 ++d->dv_refcount;
7212 return OK;
7213}
7214
7215
7216/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 * Unreference a Dictionary: decrement the reference count and free it when it
7218 * becomes zero.
7219 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007221dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007223 if (d != NULL && --d->dv_refcount <= 0)
7224 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225}
7226
7227/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007228 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229 * Ignores the reference count.
7230 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007232dict_free(
7233 dict_T *d,
7234 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007236 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007238 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007240 /* Remove the dict from the list of dicts for garbage collection. */
7241 if (d->dv_used_prev == NULL)
7242 first_dict = d->dv_used_next;
7243 else
7244 d->dv_used_prev->dv_used_next = d->dv_used_next;
7245 if (d->dv_used_next != NULL)
7246 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7247
7248 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007249 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007250 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007253 if (!HASHITEM_EMPTY(hi))
7254 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007255 /* Remove the item before deleting it, just in case there is
7256 * something recursive causing trouble. */
7257 di = HI2DI(hi);
7258 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007259 if (recurse || (di->di_tv.v_type != VAR_LIST
7260 && di->di_tv.v_type != VAR_DICT))
7261 clear_tv(&di->di_tv);
7262 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007263 --todo;
7264 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 vim_free(d);
7268}
7269
7270/*
7271 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007272 * The "key" is copied to the new item.
7273 * Note that the value of the item "di_tv" still needs to be initialized!
7274 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007276 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007277dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278{
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007281 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007283 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007285 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007286 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288}
7289
7290/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007291 * Make a copy of a Dictionary item.
7292 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007294dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295{
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007298 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7299 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300 if (di != NULL)
7301 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007303 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 copy_tv(&org->di_tv, &di->di_tv);
7305 }
7306 return di;
7307}
7308
7309/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 * Remove item "item" from Dictionary "dict" and free it.
7311 */
7312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007313dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314{
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 if (HASHITEM_EMPTY(hi))
7319 EMSG2(_(e_intern2), "dictitem_remove()");
7320 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 dictitem_free(item);
7323}
7324
7325/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 * Free a dict item. Also clears the value.
7327 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007329dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007330{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007332 if (item->di_flags & DI_FLAGS_ALLOC)
7333 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007334}
7335
7336/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7338 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007339 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 * Returns NULL when out of memory.
7341 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007343dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007344{
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 dict_T *copy;
7346 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349
7350 if (orig == NULL)
7351 return NULL;
7352
7353 copy = dict_alloc();
7354 if (copy != NULL)
7355 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007356 if (copyID != 0)
7357 {
7358 orig->dv_copyID = copyID;
7359 orig->dv_copydict = copy;
7360 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007361 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007362 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 --todo;
7367
7368 di = dictitem_alloc(hi->hi_key);
7369 if (di == NULL)
7370 break;
7371 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007372 {
7373 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7374 copyID) == FAIL)
7375 {
7376 vim_free(di);
7377 break;
7378 }
7379 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007380 else
7381 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7382 if (dict_add(copy, di) == FAIL)
7383 {
7384 dictitem_free(di);
7385 break;
7386 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 if (todo > 0)
7392 {
7393 dict_unref(copy);
7394 copy = NULL;
7395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 }
7397
7398 return copy;
7399}
7400
7401/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007403 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007405 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007406dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407{
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409}
7410
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007412 * Add a number or string entry to dictionary "d".
7413 * When "str" is NULL use number "nr", otherwise use "str".
7414 * Returns FAIL when out of memory and when key already exists.
7415 */
7416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007417dict_add_nr_str(
7418 dict_T *d,
7419 char *key,
7420 long nr,
7421 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007422{
7423 dictitem_T *item;
7424
7425 item = dictitem_alloc((char_u *)key);
7426 if (item == NULL)
7427 return FAIL;
7428 item->di_tv.v_lock = 0;
7429 if (str == NULL)
7430 {
7431 item->di_tv.v_type = VAR_NUMBER;
7432 item->di_tv.vval.v_number = nr;
7433 }
7434 else
7435 {
7436 item->di_tv.v_type = VAR_STRING;
7437 item->di_tv.vval.v_string = vim_strsave(str);
7438 }
7439 if (dict_add(d, item) == FAIL)
7440 {
7441 dictitem_free(item);
7442 return FAIL;
7443 }
7444 return OK;
7445}
7446
7447/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007448 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007449 * Returns FAIL when out of memory and when key already exists.
7450 */
7451 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007452dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007453{
7454 dictitem_T *item;
7455
7456 item = dictitem_alloc((char_u *)key);
7457 if (item == NULL)
7458 return FAIL;
7459 item->di_tv.v_lock = 0;
7460 item->di_tv.v_type = VAR_LIST;
7461 item->di_tv.vval.v_list = list;
7462 if (dict_add(d, item) == FAIL)
7463 {
7464 dictitem_free(item);
7465 return FAIL;
7466 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007467 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007468 return OK;
7469}
7470
7471/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007472 * Get the number of items in a Dictionary.
7473 */
7474 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007475dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 if (d == NULL)
7478 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007479 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480}
7481
7482/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483 * Find item "key[len]" in Dictionary "d".
7484 * If "len" is negative use strlen(key).
7485 * Returns NULL when not found.
7486 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007487 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007488dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007490#define AKEYLEN 200
7491 char_u buf[AKEYLEN];
7492 char_u *akey;
7493 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007494 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496 if (len < 0)
7497 akey = key;
7498 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007499 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 tofree = akey = vim_strnsave(key, len);
7501 if (akey == NULL)
7502 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007503 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007504 else
7505 {
7506 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007507 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007508 akey = buf;
7509 }
7510
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512 vim_free(tofree);
7513 if (HASHITEM_EMPTY(hi))
7514 return NULL;
7515 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516}
7517
7518/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007519 * Get a string item from a dictionary.
7520 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007521 * Returns NULL if the entry doesn't exist or out of memory.
7522 */
7523 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007524get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007525{
7526 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007527 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007528
7529 di = dict_find(d, key, -1);
7530 if (di == NULL)
7531 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007532 s = get_tv_string(&di->di_tv);
7533 if (save && s != NULL)
7534 s = vim_strsave(s);
7535 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007536}
7537
7538/*
7539 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007540 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541 */
7542 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007543get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007544{
7545 dictitem_T *di;
7546
7547 di = dict_find(d, key, -1);
7548 if (di == NULL)
7549 return 0;
7550 return get_tv_number(&di->di_tv);
7551}
7552
7553/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 * Return an allocated string with the string representation of a Dictionary.
7555 * May return NULL.
7556 */
7557 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007558dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559{
7560 garray_T ga;
7561 int first = TRUE;
7562 char_u *tofree;
7563 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007567 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007569 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 return NULL;
7571 ga_init2(&ga, (int)sizeof(char), 80);
7572 ga_append(&ga, '{');
7573
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007574 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007575 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007579 --todo;
7580
7581 if (first)
7582 first = FALSE;
7583 else
7584 ga_concat(&ga, (char_u *)", ");
7585
7586 tofree = string_quote(hi->hi_key, FALSE);
7587 if (tofree != NULL)
7588 {
7589 ga_concat(&ga, tofree);
7590 vim_free(tofree);
7591 }
7592 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007593 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 if (s != NULL)
7595 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007597 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007598 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007599 line_breakcheck();
7600
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007603 if (todo > 0)
7604 {
7605 vim_free(ga.ga_data);
7606 return NULL;
7607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608
7609 ga_append(&ga, '}');
7610 ga_append(&ga, NUL);
7611 return (char_u *)ga.ga_data;
7612}
7613
7614/*
7615 * Allocate a variable for a Dictionary and fill it from "*arg".
7616 * Return OK or FAIL. Returns NOTDONE for {expr}.
7617 */
7618 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007619get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620{
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 dict_T *d = NULL;
7622 typval_T tvkey;
7623 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007624 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628
7629 /*
7630 * First check if it's not a curly-braces thing: {expr}.
7631 * Must do this without evaluating, otherwise a function may be called
7632 * twice. Unfortunately this means we need to call eval1() twice for the
7633 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007634 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 if (*start != '}')
7637 {
7638 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7639 return FAIL;
7640 if (*start == '}')
7641 return NOTDONE;
7642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643
7644 if (evaluate)
7645 {
7646 d = dict_alloc();
7647 if (d == NULL)
7648 return FAIL;
7649 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007650 tvkey.v_type = VAR_UNKNOWN;
7651 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652
7653 *arg = skipwhite(*arg + 1);
7654 while (**arg != '}' && **arg != NUL)
7655 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007656 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 goto failret;
7658 if (**arg != ':')
7659 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007660 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 goto failret;
7663 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007664 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007666 key = get_tv_string_buf_chk(&tvkey, buf);
7667 if (key == NULL || *key == NUL)
7668 {
7669 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7670 if (key != NULL)
7671 EMSG(_(e_emptykey));
7672 clear_tv(&tvkey);
7673 goto failret;
7674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676
7677 *arg = skipwhite(*arg + 1);
7678 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7679 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007680 if (evaluate)
7681 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 goto failret;
7683 }
7684 if (evaluate)
7685 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007686 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 if (item != NULL)
7688 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007689 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007690 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 clear_tv(&tv);
7692 goto failret;
7693 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007694 item = dictitem_alloc(key);
7695 clear_tv(&tvkey);
7696 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007699 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007700 if (dict_add(d, item) == FAIL)
7701 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 }
7703 }
7704
7705 if (**arg == '}')
7706 break;
7707 if (**arg != ',')
7708 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007709 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 goto failret;
7711 }
7712 *arg = skipwhite(*arg + 1);
7713 }
7714
7715 if (**arg != '}')
7716 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007717 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718failret:
7719 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007720 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 return FAIL;
7722 }
7723
7724 *arg = skipwhite(*arg + 1);
7725 if (evaluate)
7726 {
7727 rettv->v_type = VAR_DICT;
7728 rettv->vval.v_dict = d;
7729 ++d->dv_refcount;
7730 }
7731
7732 return OK;
7733}
7734
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007735#if defined(FEAT_CHANNEL) || defined(PROTO)
7736/*
7737 * Decrement the reference count on "channel" and free it when it goes down to
7738 * zero.
7739 * Returns TRUE when the channel was freed.
7740 */
7741 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007742channel_unref(channel_T *channel)
7743{
7744 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007745 {
Bram Moolenaar77073442016-02-13 23:23:53 +01007746 channel_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007747 return TRUE;
7748 }
7749 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007750}
7751#endif
7752
Bram Moolenaar835dc632016-02-07 14:27:38 +01007753#ifdef FEAT_JOB
7754 static void
7755job_free(job_T *job)
7756{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007757# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007758 if (job->jv_channel != NULL)
7759 {
7760 /* The channel doesn't count as a references for the job, we need to
7761 * NULL the reference when the job is freed. */
7762 job->jv_channel->ch_job = NULL;
7763 channel_unref(job->jv_channel);
7764 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007765# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007766 mch_clear_job(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007767 vim_free(job);
7768}
7769
7770 static void
7771job_unref(job_T *job)
7772{
7773 if (job != NULL && --job->jv_refcount <= 0)
7774 job_free(job);
7775}
7776
7777/*
7778 * Allocate a job. Sets the refcount to one.
7779 */
7780 static job_T *
7781job_alloc(void)
7782{
7783 job_T *job;
7784
7785 job = (job_T *)alloc_clear(sizeof(job_T));
7786 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007787 job->jv_refcount = 1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007788 return job;
7789}
7790
7791#endif
7792
Bram Moolenaar17a13432016-01-24 14:22:10 +01007793 static char *
7794get_var_special_name(int nr)
7795{
7796 switch (nr)
7797 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007798 case VVAL_FALSE: return "v:false";
7799 case VVAL_TRUE: return "v:true";
7800 case VVAL_NONE: return "v:none";
7801 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007802 }
7803 EMSG2(_(e_intern2), "get_var_special_name()");
7804 return "42";
7805}
7806
Bram Moolenaar8c711452005-01-14 21:53:12 +00007807/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007808 * Return a string with the string representation of a variable.
7809 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007810 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007811 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007812 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007813 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007814 */
7815 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007816echo_string(
7817 typval_T *tv,
7818 char_u **tofree,
7819 char_u *numbuf,
7820 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007821{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 static int recurse = 0;
7823 char_u *r = NULL;
7824
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007827 if (!did_echo_string_emsg)
7828 {
7829 /* Only give this message once for a recursive call to avoid
7830 * flooding the user with errors. And stop iterating over lists
7831 * and dicts. */
7832 did_echo_string_emsg = TRUE;
7833 EMSG(_("E724: variable nested too deep for displaying"));
7834 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007835 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007836 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 }
7838 ++recurse;
7839
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840 switch (tv->v_type)
7841 {
7842 case VAR_FUNC:
7843 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007844 r = tv->vval.v_string;
7845 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007846
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007847 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007848 if (tv->vval.v_list == NULL)
7849 {
7850 *tofree = NULL;
7851 r = NULL;
7852 }
7853 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7854 {
7855 *tofree = NULL;
7856 r = (char_u *)"[...]";
7857 }
7858 else
7859 {
7860 tv->vval.v_list->lv_copyID = copyID;
7861 *tofree = list2string(tv, copyID);
7862 r = *tofree;
7863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007865
Bram Moolenaar8c711452005-01-14 21:53:12 +00007866 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007867 if (tv->vval.v_dict == NULL)
7868 {
7869 *tofree = NULL;
7870 r = NULL;
7871 }
7872 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7873 {
7874 *tofree = NULL;
7875 r = (char_u *)"{...}";
7876 }
7877 else
7878 {
7879 tv->vval.v_dict->dv_copyID = copyID;
7880 *tofree = dict2string(tv, copyID);
7881 r = *tofree;
7882 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007883 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007884
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885 case VAR_STRING:
7886 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007887 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007888 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007889 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007890 *tofree = NULL;
7891 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007892 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007893
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007895#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007896 *tofree = NULL;
7897 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7898 r = numbuf;
7899 break;
7900#endif
7901
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007902 case VAR_SPECIAL:
7903 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007904 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007905 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007907
Bram Moolenaar8502c702014-06-17 12:51:16 +02007908 if (--recurse == 0)
7909 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007910 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911}
7912
7913/*
7914 * Return a string with the string representation of a variable.
7915 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7916 * "numbuf" is used for a number.
7917 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007918 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007919 */
7920 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007921tv2string(
7922 typval_T *tv,
7923 char_u **tofree,
7924 char_u *numbuf,
7925 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926{
7927 switch (tv->v_type)
7928 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007929 case VAR_FUNC:
7930 *tofree = string_quote(tv->vval.v_string, TRUE);
7931 return *tofree;
7932 case VAR_STRING:
7933 *tofree = string_quote(tv->vval.v_string, FALSE);
7934 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007935#ifdef FEAT_FLOAT
7936 case VAR_FLOAT:
7937 *tofree = NULL;
7938 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7939 return numbuf;
7940#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007941 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007944 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007945 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007946 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007947 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007948 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007950 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007951}
7952
7953/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 * Return string "str" in ' quotes, doubling ' characters.
7955 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007957 */
7958 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007959string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960{
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962 char_u *p, *r, *s;
7963
Bram Moolenaar33570922005-01-25 22:26:29 +00007964 len = (function ? 13 : 3);
7965 if (str != NULL)
7966 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007967 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007968 for (p = str; *p != NUL; mb_ptr_adv(p))
7969 if (*p == '\'')
7970 ++len;
7971 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972 s = r = alloc(len);
7973 if (r != NULL)
7974 {
7975 if (function)
7976 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978 r += 10;
7979 }
7980 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 if (str != NULL)
7983 for (p = str; *p != NUL; )
7984 {
7985 if (*p == '\'')
7986 *r++ = '\'';
7987 MB_COPY_CHAR(p, r);
7988 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007989 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 if (function)
7991 *r++ = ')';
7992 *r++ = NUL;
7993 }
7994 return s;
7995}
7996
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007997#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007998/*
7999 * Convert the string "text" to a floating point number.
8000 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8001 * this always uses a decimal point.
8002 * Returns the length of the text that was consumed.
8003 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008005string2float(
8006 char_u *text,
8007 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008008{
8009 char *s = (char *)text;
8010 float_T f;
8011
8012 f = strtod(s, &s);
8013 *value = f;
8014 return (int)((char_u *)s - text);
8015}
8016#endif
8017
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 * Get the value of an environment variable.
8020 * "arg" is pointing to the '$'. It is advanced to after the name.
8021 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008022 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 */
8024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008025get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026{
8027 char_u *string = NULL;
8028 int len;
8029 int cc;
8030 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008031 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032
8033 ++*arg;
8034 name = *arg;
8035 len = get_env_len(arg);
8036 if (evaluate)
8037 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008038 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008039 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008040
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008041 cc = name[len];
8042 name[len] = NUL;
8043 /* first try vim_getenv(), fast for normal environment vars */
8044 string = vim_getenv(name, &mustfree);
8045 if (string != NULL && *string != NUL)
8046 {
8047 if (!mustfree)
8048 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008050 else
8051 {
8052 if (mustfree)
8053 vim_free(string);
8054
8055 /* next try expanding things like $VIM and ${HOME} */
8056 string = expand_env_save(name - 1);
8057 if (string != NULL && *string == '$')
8058 {
8059 vim_free(string);
8060 string = NULL;
8061 }
8062 }
8063 name[len] = cc;
8064
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065 rettv->v_type = VAR_STRING;
8066 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 }
8068
8069 return OK;
8070}
8071
8072/*
8073 * Array with names and number of arguments of all internal functions
8074 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8075 */
8076static struct fst
8077{
8078 char *f_name; /* function name */
8079 char f_min_argc; /* minimal number of arguments */
8080 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008081 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008082 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083} functions[] =
8084{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008085#ifdef FEAT_FLOAT
8086 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008087 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008089 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008090 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008091 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"append", 2, 2, f_append},
8093 {"argc", 0, 0, f_argc},
8094 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008095 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008096 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008097#ifdef FEAT_FLOAT
8098 {"asin", 1, 1, f_asin}, /* WJMc */
8099#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008100 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008101 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008102 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008103 {"assert_false", 1, 2, f_assert_false},
8104 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008105#ifdef FEAT_FLOAT
8106 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008107 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008110 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"bufexists", 1, 1, f_bufexists},
8112 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8113 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8114 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8115 {"buflisted", 1, 1, f_buflisted},
8116 {"bufloaded", 1, 1, f_bufloaded},
8117 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008118 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"bufwinnr", 1, 1, f_bufwinnr},
8120 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008121 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008122 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008123 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008124#ifdef FEAT_FLOAT
8125 {"ceil", 1, 1, f_ceil},
8126#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008127#ifdef FEAT_CHANNEL
8128 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008129 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008130 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008131 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008132 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008133 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8134 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008135 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008136 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008137#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008138 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008139 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008141 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008143#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008144 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008145 {"complete_add", 1, 1, f_complete_add},
8146 {"complete_check", 0, 0, f_complete_check},
8147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008149 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008150#ifdef FEAT_FLOAT
8151 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008152 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008153#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008154 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008156 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008157 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008158 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008160 {"diff_filler", 1, 1, f_diff_filler},
8161 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008162 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008163 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008165 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"eventhandler", 0, 0, f_eventhandler},
8167 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008168 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008170#ifdef FEAT_FLOAT
8171 {"exp", 1, 1, f_exp},
8172#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008173 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008174 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008175 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8177 {"filereadable", 1, 1, f_filereadable},
8178 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008179 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008180 {"finddir", 1, 3, f_finddir},
8181 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182#ifdef FEAT_FLOAT
8183 {"float2nr", 1, 1, f_float2nr},
8184 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008185 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008186#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008187 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"fnamemodify", 2, 2, f_fnamemodify},
8189 {"foldclosed", 1, 1, f_foldclosed},
8190 {"foldclosedend", 1, 1, f_foldclosedend},
8191 {"foldlevel", 1, 1, f_foldlevel},
8192 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008193 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008195 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008196 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008197 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008198 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008199 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"getchar", 0, 1, f_getchar},
8201 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008202 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {"getcmdline", 0, 0, f_getcmdline},
8204 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008205 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008206 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008207 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008208 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008209 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008210 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {"getfsize", 1, 1, f_getfsize},
8212 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008213 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008214 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008215 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008216 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008217 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008218 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008219 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008220 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008222 {"gettabvar", 2, 3, f_gettabvar},
8223 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"getwinposx", 0, 0, f_getwinposx},
8225 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008226 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008227 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008228 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008229 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008231 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008232 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008233 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8235 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8236 {"histadd", 2, 2, f_histadd},
8237 {"histdel", 1, 2, f_histdel},
8238 {"histget", 1, 2, f_histget},
8239 {"histnr", 1, 1, f_histnr},
8240 {"hlID", 1, 1, f_hlID},
8241 {"hlexists", 1, 1, f_hlexists},
8242 {"hostname", 0, 0, f_hostname},
8243 {"iconv", 3, 3, f_iconv},
8244 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008245 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008246 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008248 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"inputrestore", 0, 0, f_inputrestore},
8250 {"inputsave", 0, 0, f_inputsave},
8251 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008252 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008253 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008255 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008256 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008257#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008258# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008259 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008260# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008261 {"job_start", 1, 2, f_job_start},
8262 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008263 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008264#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008265 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008266 {"js_decode", 1, 1, f_js_decode},
8267 {"js_encode", 1, 1, f_js_encode},
8268 {"json_decode", 1, 1, f_json_decode},
8269 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008270 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008272 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"libcall", 3, 3, f_libcall},
8274 {"libcallnr", 3, 3, f_libcallnr},
8275 {"line", 1, 1, f_line},
8276 {"line2byte", 1, 1, f_line2byte},
8277 {"lispindent", 1, 1, f_lispindent},
8278 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008279#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008280 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008281 {"log10", 1, 1, f_log10},
8282#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008283#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008284 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008285#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008286 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008287 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008288 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008289 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008290 {"matchadd", 2, 5, f_matchadd},
8291 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008292 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008293 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008294 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008295 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008296 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008297 {"max", 1, 1, f_max},
8298 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008299#ifdef vim_mkdir
8300 {"mkdir", 1, 3, f_mkdir},
8301#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008302 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008303#ifdef FEAT_MZSCHEME
8304 {"mzeval", 1, 1, f_mzeval},
8305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008307 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008308 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008309 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008310#ifdef FEAT_PERL
8311 {"perleval", 1, 1, f_perleval},
8312#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008313#ifdef FEAT_FLOAT
8314 {"pow", 2, 2, f_pow},
8315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008317 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008318 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008319#ifdef FEAT_PYTHON3
8320 {"py3eval", 1, 1, f_py3eval},
8321#endif
8322#ifdef FEAT_PYTHON
8323 {"pyeval", 1, 1, f_pyeval},
8324#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008325 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008326 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008327 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008328 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008329 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"remote_expr", 2, 3, f_remote_expr},
8331 {"remote_foreground", 1, 1, f_remote_foreground},
8332 {"remote_peek", 1, 2, f_remote_peek},
8333 {"remote_read", 1, 1, f_remote_read},
8334 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008335 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008337 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008339 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008340#ifdef FEAT_FLOAT
8341 {"round", 1, 1, f_round},
8342#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008343 {"screenattr", 2, 2, f_screenattr},
8344 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008345 {"screencol", 0, 0, f_screencol},
8346 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008347 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008348 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008349 {"searchpair", 3, 7, f_searchpair},
8350 {"searchpairpos", 3, 7, f_searchpairpos},
8351 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"server2client", 2, 2, f_server2client},
8353 {"serverlist", 0, 0, f_serverlist},
8354 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008355 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"setcmdpos", 1, 1, f_setcmdpos},
8357 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008358 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008359 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008360 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008361 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008363 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008364 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008366#ifdef FEAT_CRYPT
8367 {"sha256", 1, 1, f_sha256},
8368#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008369 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008370 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008372#ifdef FEAT_FLOAT
8373 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008374 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008375#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008376 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008377 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008378 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008379 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008380 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008381#ifdef FEAT_FLOAT
8382 {"sqrt", 1, 1, f_sqrt},
8383 {"str2float", 1, 1, f_str2float},
8384#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008385 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008386 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008387 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388#ifdef HAVE_STRFTIME
8389 {"strftime", 1, 2, f_strftime},
8390#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008391 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008392 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"strlen", 1, 1, f_strlen},
8394 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008395 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008397 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008398 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"substitute", 4, 4, f_substitute},
8400 {"synID", 3, 3, f_synID},
8401 {"synIDattr", 2, 3, f_synIDattr},
8402 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008403 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008404 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008405 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008406 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008407 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008408 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008409 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008410 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008411 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008412#ifdef FEAT_FLOAT
8413 {"tan", 1, 1, f_tan},
8414 {"tanh", 1, 1, f_tanh},
8415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008417 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {"tolower", 1, 1, f_tolower},
8419 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008420 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008421#ifdef FEAT_FLOAT
8422 {"trunc", 1, 1, f_trunc},
8423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008425 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008426 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008427 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008428 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"virtcol", 1, 1, f_virtcol},
8430 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008431 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {"winbufnr", 1, 1, f_winbufnr},
8433 {"wincol", 0, 0, f_wincol},
8434 {"winheight", 1, 1, f_winheight},
8435 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008436 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008438 {"winrestview", 1, 1, f_winrestview},
8439 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008441 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008442 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008443 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444};
8445
8446#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8447
8448/*
8449 * Function given to ExpandGeneric() to obtain the list of internal
8450 * or user defined function names.
8451 */
8452 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008453get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 static int intidx = -1;
8456 char_u *name;
8457
8458 if (idx == 0)
8459 intidx = -1;
8460 if (intidx < 0)
8461 {
8462 name = get_user_func_name(xp, idx);
8463 if (name != NULL)
8464 return name;
8465 }
8466 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8467 {
8468 STRCPY(IObuff, functions[intidx].f_name);
8469 STRCAT(IObuff, "(");
8470 if (functions[intidx].f_max_argc == 0)
8471 STRCAT(IObuff, ")");
8472 return IObuff;
8473 }
8474
8475 return NULL;
8476}
8477
8478/*
8479 * Function given to ExpandGeneric() to obtain the list of internal or
8480 * user defined variable or function names.
8481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008483get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 static int intidx = -1;
8486 char_u *name;
8487
8488 if (idx == 0)
8489 intidx = -1;
8490 if (intidx < 0)
8491 {
8492 name = get_function_name(xp, idx);
8493 if (name != NULL)
8494 return name;
8495 }
8496 return get_user_var_name(xp, ++intidx);
8497}
8498
8499#endif /* FEAT_CMDL_COMPL */
8500
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008501#if defined(EBCDIC) || defined(PROTO)
8502/*
8503 * Compare struct fst by function name.
8504 */
8505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008506compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008507{
8508 struct fst *p1 = (struct fst *)s1;
8509 struct fst *p2 = (struct fst *)s2;
8510
8511 return STRCMP(p1->f_name, p2->f_name);
8512}
8513
8514/*
8515 * Sort the function table by function name.
8516 * The sorting of the table above is ASCII dependant.
8517 * On machines using EBCDIC we have to sort it.
8518 */
8519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008520sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008521{
8522 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8523
8524 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8525}
8526#endif
8527
8528
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529/*
8530 * Find internal function in table above.
8531 * Return index, or -1 if not found
8532 */
8533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008534find_internal_func(
8535 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
8537 int first = 0;
8538 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8539 int cmp;
8540 int x;
8541
8542 /*
8543 * Find the function name in the table. Binary search.
8544 */
8545 while (first <= last)
8546 {
8547 x = first + ((unsigned)(last - first) >> 1);
8548 cmp = STRCMP(name, functions[x].f_name);
8549 if (cmp < 0)
8550 last = x - 1;
8551 else if (cmp > 0)
8552 first = x + 1;
8553 else
8554 return x;
8555 }
8556 return -1;
8557}
8558
8559/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008560 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8561 * name it contains, otherwise return "name".
8562 */
8563 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008564deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008565{
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008567 int cc;
8568
8569 cc = name[*lenp];
8570 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008571 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008572 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008575 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008576 {
8577 *lenp = 0;
8578 return (char_u *)""; /* just in case */
8579 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008580 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008581 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008582 }
8583
8584 return name;
8585}
8586
8587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 * Allocate a variable for the result of a function.
8589 * Return OK or FAIL.
8590 */
8591 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008592get_func_tv(
8593 char_u *name, /* name of the function */
8594 int len, /* length of "name" */
8595 typval_T *rettv,
8596 char_u **arg, /* argument, pointing to the '(' */
8597 linenr_T firstline, /* first line of range */
8598 linenr_T lastline, /* last line of range */
8599 int *doesrange, /* return: function handled range */
8600 int evaluate,
8601 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
8603 char_u *argp;
8604 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008605 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 int argcount = 0; /* number of arguments found */
8607
8608 /*
8609 * Get the arguments.
8610 */
8611 argp = *arg;
8612 while (argcount < MAX_FUNC_ARGS)
8613 {
8614 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8615 if (*argp == ')' || *argp == ',' || *argp == NUL)
8616 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8618 {
8619 ret = FAIL;
8620 break;
8621 }
8622 ++argcount;
8623 if (*argp != ',')
8624 break;
8625 }
8626 if (*argp == ')')
8627 ++argp;
8628 else
8629 ret = FAIL;
8630
8631 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008635 {
8636 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008637 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008638 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008639 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641
8642 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008643 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644
8645 *arg = skipwhite(argp);
8646 return ret;
8647}
8648
8649
8650/*
8651 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008652 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008653 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008656call_func(
8657 char_u *funcname, /* name of the function */
8658 int len, /* length of "name" */
8659 typval_T *rettv, /* return value goes here */
8660 int argcount, /* number of "argvars" */
8661 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008662 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008663 linenr_T firstline, /* first line of range */
8664 linenr_T lastline, /* last line of range */
8665 int *doesrange, /* return: function handled range */
8666 int evaluate,
8667 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
8669 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670#define ERROR_UNKNOWN 0
8671#define ERROR_TOOMANY 1
8672#define ERROR_TOOFEW 2
8673#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008674#define ERROR_DICT 4
8675#define ERROR_NONE 5
8676#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 int error = ERROR_NONE;
8678 int i;
8679 int llen;
8680 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681#define FLEN_FIXED 40
8682 char_u fname_buf[FLEN_FIXED + 1];
8683 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008684 char_u *name;
8685
8686 /* Make a copy of the name, if it comes from a funcref variable it could
8687 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008688 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008689 if (name == NULL)
8690 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691
8692 /*
8693 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8694 * Change <SNR>123_name() to K_SNR 123_name().
8695 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 llen = eval_fname_script(name);
8698 if (llen > 0)
8699 {
8700 fname_buf[0] = K_SPECIAL;
8701 fname_buf[1] = KS_EXTRA;
8702 fname_buf[2] = (int)KE_SNR;
8703 i = 3;
8704 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8705 {
8706 if (current_SID <= 0)
8707 error = ERROR_SCRIPT;
8708 else
8709 {
8710 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8711 i = (int)STRLEN(fname_buf);
8712 }
8713 }
8714 if (i + STRLEN(name + llen) < FLEN_FIXED)
8715 {
8716 STRCPY(fname_buf + i, name + llen);
8717 fname = fname_buf;
8718 }
8719 else
8720 {
8721 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8722 if (fname == NULL)
8723 error = ERROR_OTHER;
8724 else
8725 {
8726 mch_memmove(fname, fname_buf, (size_t)i);
8727 STRCPY(fname + i, name + llen);
8728 }
8729 }
8730 }
8731 else
8732 fname = name;
8733
8734 *doesrange = FALSE;
8735
8736
8737 /* execute the function if no errors detected and executing */
8738 if (evaluate && error == ERROR_NONE)
8739 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008740 char_u *rfname = fname;
8741
8742 /* Ignore "g:" before a function name. */
8743 if (fname[0] == 'g' && fname[1] == ':')
8744 rfname = fname + 2;
8745
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008746 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8747 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 error = ERROR_UNKNOWN;
8749
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008750 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {
8752 /*
8753 * User defined function.
8754 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008755 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008756
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008758 /* Trigger FuncUndefined event, may load the function. */
8759 if (fp == NULL
8760 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008761 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008762 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008764 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008765 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 }
8767#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008768 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008769 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008770 {
8771 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008772 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008773 }
8774
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 if (fp != NULL)
8776 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008777 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008779 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008781 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008783 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008784 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 else
8786 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008787 int did_save_redo = FALSE;
8788
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 /*
8790 * Call the user function.
8791 * Save and restore search patterns, script variables and
8792 * redo buffer.
8793 */
8794 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008795#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008796 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008797#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008798 {
8799 saveRedobuff();
8800 did_save_redo = TRUE;
8801 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008802 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008804 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008805 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8806 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8807 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008808 /* Function was unreferenced while being used, free it
8809 * now. */
8810 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008811 if (did_save_redo)
8812 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 restore_search_patterns();
8814 error = ERROR_NONE;
8815 }
8816 }
8817 }
8818 else
8819 {
8820 /*
8821 * Find the function name in the table, call its implementation.
8822 */
8823 i = find_internal_func(fname);
8824 if (i >= 0)
8825 {
8826 if (argcount < functions[i].f_min_argc)
8827 error = ERROR_TOOFEW;
8828 else if (argcount > functions[i].f_max_argc)
8829 error = ERROR_TOOMANY;
8830 else
8831 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008832 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 error = ERROR_NONE;
8835 }
8836 }
8837 }
8838 /*
8839 * The function call (or "FuncUndefined" autocommand sequence) might
8840 * have been aborted by an error, an interrupt, or an explicitly thrown
8841 * exception that has not been caught so far. This situation can be
8842 * tested for by calling aborting(). For an error in an internal
8843 * function or for the "E132" error in call_user_func(), however, the
8844 * throw point at which the "force_abort" flag (temporarily reset by
8845 * emsg()) is normally updated has not been reached yet. We need to
8846 * update that flag first to make aborting() reliable.
8847 */
8848 update_force_abort();
8849 }
8850 if (error == ERROR_NONE)
8851 ret = OK;
8852
8853 /*
8854 * Report an error unless the argument evaluation or function call has been
8855 * cancelled due to an aborting error, an interrupt, or an exception.
8856 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008857 if (!aborting())
8858 {
8859 switch (error)
8860 {
8861 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008862 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008863 break;
8864 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008865 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008866 break;
8867 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008868 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008869 name);
8870 break;
8871 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008872 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008873 name);
8874 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008876 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008877 name);
8878 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008879 }
8880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 if (fname != name && fname != fname_buf)
8883 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008884 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885
8886 return ret;
8887}
8888
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008889/*
8890 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008891 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008892 */
8893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008894emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008895{
8896 char_u *p;
8897
8898 if (*name == K_SPECIAL)
8899 p = concat_str((char_u *)"<SNR>", name + 3);
8900 else
8901 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008902 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008903 if (p != name)
8904 vim_free(p);
8905}
8906
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008907/*
8908 * Return TRUE for a non-zero Number and a non-empty String.
8909 */
8910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008911non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008912{
8913 return ((argvars[0].v_type == VAR_NUMBER
8914 && argvars[0].vval.v_number != 0)
8915 || (argvars[0].v_type == VAR_STRING
8916 && argvars[0].vval.v_string != NULL
8917 && *argvars[0].vval.v_string != NUL));
8918}
8919
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920/*********************************************
8921 * Implementation of the built-in functions
8922 */
8923
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008924#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008925static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008926
8927/*
8928 * Get the float value of "argvars[0]" into "f".
8929 * Returns FAIL when the argument is not a Number or Float.
8930 */
8931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008932get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008933{
8934 if (argvars[0].v_type == VAR_FLOAT)
8935 {
8936 *f = argvars[0].vval.v_float;
8937 return OK;
8938 }
8939 if (argvars[0].v_type == VAR_NUMBER)
8940 {
8941 *f = (float_T)argvars[0].vval.v_number;
8942 return OK;
8943 }
8944 EMSG(_("E808: Number or Float required"));
8945 return FAIL;
8946}
8947
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008948/*
8949 * "abs(expr)" function
8950 */
8951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008952f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008953{
8954 if (argvars[0].v_type == VAR_FLOAT)
8955 {
8956 rettv->v_type = VAR_FLOAT;
8957 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8958 }
8959 else
8960 {
8961 varnumber_T n;
8962 int error = FALSE;
8963
8964 n = get_tv_number_chk(&argvars[0], &error);
8965 if (error)
8966 rettv->vval.v_number = -1;
8967 else if (n > 0)
8968 rettv->vval.v_number = n;
8969 else
8970 rettv->vval.v_number = -n;
8971 }
8972}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008973
8974/*
8975 * "acos()" function
8976 */
8977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008978f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008979{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01008980 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008981
8982 rettv->v_type = VAR_FLOAT;
8983 if (get_float_arg(argvars, &f) == OK)
8984 rettv->vval.v_float = acos(f);
8985 else
8986 rettv->vval.v_float = 0.0;
8987}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008988#endif
8989
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008991 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 */
8993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008994f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995{
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008999 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009001 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009002 && !tv_check_lock(l->lv_lock,
9003 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009004 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009006 }
9007 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009008 EMSG(_(e_listreq));
9009}
9010
9011/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009012 * "alloc_fail(id, countdown, repeat)" function
9013 */
9014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009015f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009016{
9017 if (argvars[0].v_type != VAR_NUMBER
9018 || argvars[0].vval.v_number <= 0
9019 || argvars[1].v_type != VAR_NUMBER
9020 || argvars[1].vval.v_number < 0
9021 || argvars[2].v_type != VAR_NUMBER)
9022 EMSG(_(e_invarg));
9023 else
9024 {
9025 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009026 if (alloc_fail_id >= aid_last)
9027 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009028 alloc_fail_countdown = argvars[1].vval.v_number;
9029 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009030 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009031 }
9032}
9033
9034/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009035 * "and(expr, expr)" function
9036 */
9037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009038f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009039{
9040 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9041 & get_tv_number_chk(&argvars[1], NULL);
9042}
9043
9044/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009045 * "append(lnum, string/list)" function
9046 */
9047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009048f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009049{
9050 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 list_T *l = NULL;
9053 listitem_T *li = NULL;
9054 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009055 long added = 0;
9056
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009057 /* When coming here from Insert mode, sync undo, so that this can be
9058 * undone separately from what was previously inserted. */
9059 if (u_sync_once == 2)
9060 {
9061 u_sync_once = 1; /* notify that u_sync() was called */
9062 u_sync(TRUE);
9063 }
9064
Bram Moolenaar0d660222005-01-07 21:51:51 +00009065 lnum = get_tv_lnum(argvars);
9066 if (lnum >= 0
9067 && lnum <= curbuf->b_ml.ml_line_count
9068 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009069 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009070 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009071 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009072 l = argvars[1].vval.v_list;
9073 if (l == NULL)
9074 return;
9075 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009076 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009077 for (;;)
9078 {
9079 if (l == NULL)
9080 tv = &argvars[1]; /* append a string */
9081 else if (li == NULL)
9082 break; /* end of list */
9083 else
9084 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009085 line = get_tv_string_chk(tv);
9086 if (line == NULL) /* type error */
9087 {
9088 rettv->vval.v_number = 1; /* Failed */
9089 break;
9090 }
9091 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009092 ++added;
9093 if (l == NULL)
9094 break;
9095 li = li->li_next;
9096 }
9097
9098 appended_lines_mark(lnum, added);
9099 if (curwin->w_cursor.lnum > lnum)
9100 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009102 else
9103 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104}
9105
9106/*
9107 * "argc()" function
9108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009110f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113}
9114
9115/*
9116 * "argidx()" function
9117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009119f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122}
9123
9124/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009125 * "arglistid()" function
9126 */
9127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009128f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009129{
9130 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009131
9132 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009133 wp = find_tabwin(&argvars[0], &argvars[1]);
9134 if (wp != NULL)
9135 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009136}
9137
9138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 * "argv(nr)" function
9140 */
9141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009142f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
9144 int idx;
9145
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009146 if (argvars[0].v_type != VAR_UNKNOWN)
9147 {
9148 idx = get_tv_number_chk(&argvars[0], NULL);
9149 if (idx >= 0 && idx < ARGCOUNT)
9150 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9151 else
9152 rettv->vval.v_string = NULL;
9153 rettv->v_type = VAR_STRING;
9154 }
9155 else if (rettv_list_alloc(rettv) == OK)
9156 for (idx = 0; idx < ARGCOUNT; ++idx)
9157 list_append_string(rettv->vval.v_list,
9158 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159}
9160
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009161static void prepare_assert_error(garray_T*gap);
9162static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9163static void assert_error(garray_T *gap);
9164static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009165
9166/*
9167 * Prepare "gap" for an assert error and add the sourcing position.
9168 */
9169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009170prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009171{
9172 char buf[NUMBUFLEN];
9173
9174 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009175 if (sourcing_name != NULL)
9176 {
9177 ga_concat(gap, sourcing_name);
9178 if (sourcing_lnum > 0)
9179 ga_concat(gap, (char_u *)" ");
9180 }
9181 if (sourcing_lnum > 0)
9182 {
9183 sprintf(buf, "line %ld", (long)sourcing_lnum);
9184 ga_concat(gap, (char_u *)buf);
9185 }
9186 if (sourcing_name != NULL || sourcing_lnum > 0)
9187 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009188}
9189
9190/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009191 * Append "str" to "gap", escaping unprintable characters.
9192 * Changes NL to \n, CR to \r, etc.
9193 */
9194 static void
9195ga_concat_esc(garray_T *gap, char_u *str)
9196{
9197 char_u *p;
9198 char_u buf[NUMBUFLEN];
9199
9200 for (p = str; *p != NUL; ++p)
9201 switch (*p)
9202 {
9203 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9204 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9205 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9206 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9207 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9208 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9209 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9210 default:
9211 if (*p < ' ')
9212 {
9213 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9214 ga_concat(gap, buf);
9215 }
9216 else
9217 ga_append(gap, *p);
9218 break;
9219 }
9220}
9221
9222/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009223 * Fill "gap" with information about an assert error.
9224 */
9225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009226fill_assert_error(
9227 garray_T *gap,
9228 typval_T *opt_msg_tv,
9229 char_u *exp_str,
9230 typval_T *exp_tv,
9231 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009232{
9233 char_u numbuf[NUMBUFLEN];
9234 char_u *tofree;
9235
9236 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9237 {
9238 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9239 vim_free(tofree);
9240 }
9241 else
9242 {
9243 ga_concat(gap, (char_u *)"Expected ");
9244 if (exp_str == NULL)
9245 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009246 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009247 vim_free(tofree);
9248 }
9249 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009250 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009251 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009252 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009253 vim_free(tofree);
9254 }
9255}
Bram Moolenaar43345542015-11-29 17:35:35 +01009256
9257/*
9258 * Add an assert error to v:errors.
9259 */
9260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009261assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009262{
9263 struct vimvar *vp = &vimvars[VV_ERRORS];
9264
9265 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9266 /* Make sure v:errors is a list. */
9267 set_vim_var_list(VV_ERRORS, list_alloc());
9268 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9269}
9270
Bram Moolenaar43345542015-11-29 17:35:35 +01009271/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009272 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009273 */
9274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009275f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009276{
9277 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009278
9279 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9280 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009281 prepare_assert_error(&ga);
9282 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9283 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009284 ga_clear(&ga);
9285 }
9286}
9287
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009288/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009289 * "assert_exception(string[, msg])" function
9290 */
9291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009292f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009293{
9294 garray_T ga;
9295 char *error;
9296
9297 error = (char *)get_tv_string_chk(&argvars[0]);
9298 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9299 {
9300 prepare_assert_error(&ga);
9301 ga_concat(&ga, (char_u *)"v:exception is not set");
9302 assert_error(&ga);
9303 ga_clear(&ga);
9304 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009305 else if (error != NULL
9306 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009307 {
9308 prepare_assert_error(&ga);
9309 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9310 &vimvars[VV_EXCEPTION].vv_tv);
9311 assert_error(&ga);
9312 ga_clear(&ga);
9313 }
9314}
9315
9316/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009317 * "assert_fails(cmd [, error])" function
9318 */
9319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009320f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009321{
9322 char_u *cmd = get_tv_string_chk(&argvars[0]);
9323 garray_T ga;
9324
9325 called_emsg = FALSE;
9326 suppress_errthrow = TRUE;
9327 emsg_silent = TRUE;
9328 do_cmdline_cmd(cmd);
9329 if (!called_emsg)
9330 {
9331 prepare_assert_error(&ga);
9332 ga_concat(&ga, (char_u *)"command did not fail: ");
9333 ga_concat(&ga, cmd);
9334 assert_error(&ga);
9335 ga_clear(&ga);
9336 }
9337 else if (argvars[1].v_type != VAR_UNKNOWN)
9338 {
9339 char_u buf[NUMBUFLEN];
9340 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9341
9342 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9343 {
9344 prepare_assert_error(&ga);
9345 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9346 &vimvars[VV_ERRMSG].vv_tv);
9347 assert_error(&ga);
9348 ga_clear(&ga);
9349 }
9350 }
9351
9352 called_emsg = FALSE;
9353 suppress_errthrow = FALSE;
9354 emsg_silent = FALSE;
9355 emsg_on_display = FALSE;
9356 set_vim_var_string(VV_ERRMSG, NULL, 0);
9357}
9358
9359/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009360 * Common for assert_true() and assert_false().
9361 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009363assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009364{
9365 int error = FALSE;
9366 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009367
Bram Moolenaar37127922016-02-06 20:29:28 +01009368 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009369 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009370 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009371 if (argvars[0].v_type != VAR_NUMBER
9372 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9373 || error)
9374 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009375 prepare_assert_error(&ga);
9376 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009377 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009378 NULL, &argvars[0]);
9379 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009380 ga_clear(&ga);
9381 }
9382}
9383
9384/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009385 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009386 */
9387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009388f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009389{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009390 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009391}
9392
9393/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009394 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009395 */
9396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009397f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009398{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009399 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009400}
9401
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009402#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009403/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009404 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009405 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009407f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009408{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009409 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009410
9411 rettv->v_type = VAR_FLOAT;
9412 if (get_float_arg(argvars, &f) == OK)
9413 rettv->vval.v_float = asin(f);
9414 else
9415 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009416}
9417
9418/*
9419 * "atan()" function
9420 */
9421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009422f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009423{
9424 float_T f;
9425
9426 rettv->v_type = VAR_FLOAT;
9427 if (get_float_arg(argvars, &f) == OK)
9428 rettv->vval.v_float = atan(f);
9429 else
9430 rettv->vval.v_float = 0.0;
9431}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009432
9433/*
9434 * "atan2()" function
9435 */
9436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009437f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009438{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009439 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009440
9441 rettv->v_type = VAR_FLOAT;
9442 if (get_float_arg(argvars, &fx) == OK
9443 && get_float_arg(&argvars[1], &fy) == OK)
9444 rettv->vval.v_float = atan2(fx, fy);
9445 else
9446 rettv->vval.v_float = 0.0;
9447}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009448#endif
9449
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450/*
9451 * "browse(save, title, initdir, default)" function
9452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009454f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456#ifdef FEAT_BROWSE
9457 int save;
9458 char_u *title;
9459 char_u *initdir;
9460 char_u *defname;
9461 char_u buf[NUMBUFLEN];
9462 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009463 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009465 save = get_tv_number_chk(&argvars[0], &error);
9466 title = get_tv_string_chk(&argvars[1]);
9467 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9468 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 if (error || title == NULL || initdir == NULL || defname == NULL)
9471 rettv->vval.v_string = NULL;
9472 else
9473 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009474 do_browse(save ? BROWSE_SAVE : 0,
9475 title, defname, NULL, initdir, NULL, curbuf);
9476#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009478#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009480}
9481
9482/*
9483 * "browsedir(title, initdir)" function
9484 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009486f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009487{
9488#ifdef FEAT_BROWSE
9489 char_u *title;
9490 char_u *initdir;
9491 char_u buf[NUMBUFLEN];
9492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009493 title = get_tv_string_chk(&argvars[0]);
9494 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009496 if (title == NULL || initdir == NULL)
9497 rettv->vval.v_string = NULL;
9498 else
9499 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009500 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505}
9506
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009507static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009508
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509/*
9510 * Find a buffer by number or exact name.
9511 */
9512 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009513find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009517 if (avar->v_type == VAR_NUMBER)
9518 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009519 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009521 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009522 if (buf == NULL)
9523 {
9524 /* No full path name match, try a match with a URL or a "nofile"
9525 * buffer, these don't use the full path. */
9526 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9527 if (buf->b_fname != NULL
9528 && (path_with_url(buf->b_fname)
9529#ifdef FEAT_QUICKFIX
9530 || bt_nofile(buf)
9531#endif
9532 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009533 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009534 break;
9535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 }
9537 return buf;
9538}
9539
9540/*
9541 * "bufexists(expr)" function
9542 */
9543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009544f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009546 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547}
9548
9549/*
9550 * "buflisted(expr)" function
9551 */
9552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009553f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554{
9555 buf_T *buf;
9556
9557 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559}
9560
9561/*
9562 * "bufloaded(expr)" function
9563 */
9564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009565f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566{
9567 buf_T *buf;
9568
9569 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571}
9572
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009573static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009574
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575/*
9576 * Get buffer by number or pattern.
9577 */
9578 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009579get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 int save_magic;
9583 char_u *save_cpo;
9584 buf_T *buf;
9585
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 if (tv->v_type == VAR_NUMBER)
9587 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009588 if (tv->v_type != VAR_STRING)
9589 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 if (name == NULL || *name == NUL)
9591 return curbuf;
9592 if (name[0] == '$' && name[1] == NUL)
9593 return lastbuf;
9594
9595 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9596 save_magic = p_magic;
9597 p_magic = TRUE;
9598 save_cpo = p_cpo;
9599 p_cpo = (char_u *)"";
9600
9601 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009602 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603
9604 p_magic = save_magic;
9605 p_cpo = save_cpo;
9606
9607 /* If not found, try expanding the name, like done for bufexists(). */
9608 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
9611 return buf;
9612}
9613
9614/*
9615 * "bufname(expr)" function
9616 */
9617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009618f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619{
9620 buf_T *buf;
9621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009624 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 --emsg_off;
9631}
9632
9633/*
9634 * "bufnr(expr)" function
9635 */
9636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009637f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
9639 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009640 int error = FALSE;
9641 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009645 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009646 --emsg_off;
9647
9648 /* If the buffer isn't found and the second argument is not zero create a
9649 * new buffer. */
9650 if (buf == NULL
9651 && argvars[1].v_type != VAR_UNKNOWN
9652 && get_tv_number_chk(&argvars[1], &error) != 0
9653 && !error
9654 && (name = get_tv_string_chk(&argvars[0])) != NULL
9655 && !error)
9656 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9657
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662}
9663
9664/*
9665 * "bufwinnr(nr)" function
9666 */
9667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009668f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
9670#ifdef FEAT_WINDOWS
9671 win_T *wp;
9672 int winnr = 0;
9673#endif
9674 buf_T *buf;
9675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009676 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009678 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679#ifdef FEAT_WINDOWS
9680 for (wp = firstwin; wp; wp = wp->w_next)
9681 {
9682 ++winnr;
9683 if (wp->w_buffer == buf)
9684 break;
9685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689#endif
9690 --emsg_off;
9691}
9692
9693/*
9694 * "byte2line(byte)" function
9695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009697f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701#else
9702 long boff = 0;
9703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009704 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 (linenr_T)0, &boff);
9710#endif
9711}
9712
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009714byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009715{
9716#ifdef FEAT_MBYTE
9717 char_u *t;
9718#endif
9719 char_u *str;
9720 long idx;
9721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009722 str = get_tv_string_chk(&argvars[0]);
9723 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009725 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009726 return;
9727
9728#ifdef FEAT_MBYTE
9729 t = str;
9730 for ( ; idx > 0; idx--)
9731 {
9732 if (*t == NUL) /* EOL reached */
9733 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009734 if (enc_utf8 && comp)
9735 t += utf_ptr2len(t);
9736 else
9737 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009738 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009739 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009740#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009741 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009743#endif
9744}
9745
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009746/*
9747 * "byteidx()" function
9748 */
9749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009750f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009751{
9752 byteidx(argvars, rettv, FALSE);
9753}
9754
9755/*
9756 * "byteidxcomp()" function
9757 */
9758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009759f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009760{
9761 byteidx(argvars, rettv, TRUE);
9762}
9763
Bram Moolenaardb913952012-06-29 12:54:53 +02009764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009765func_call(
9766 char_u *name,
9767 typval_T *args,
9768 dict_T *selfdict,
9769 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009770{
9771 listitem_T *item;
9772 typval_T argv[MAX_FUNC_ARGS + 1];
9773 int argc = 0;
9774 int dummy;
9775 int r = 0;
9776
9777 for (item = args->vval.v_list->lv_first; item != NULL;
9778 item = item->li_next)
9779 {
9780 if (argc == MAX_FUNC_ARGS)
9781 {
9782 EMSG(_("E699: Too many arguments"));
9783 break;
9784 }
9785 /* Make a copy of each argument. This is needed to be able to set
9786 * v_lock to VAR_FIXED in the copy without changing the original list.
9787 */
9788 copy_tv(&item->li_tv, &argv[argc++]);
9789 }
9790
9791 if (item == NULL)
9792 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9793 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9794 &dummy, TRUE, selfdict);
9795
9796 /* Free the arguments. */
9797 while (argc > 0)
9798 clear_tv(&argv[--argc]);
9799
9800 return r;
9801}
9802
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009803/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009804 * "call(func, arglist)" function
9805 */
9806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009807f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009808{
9809 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009810 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009811
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009812 if (argvars[1].v_type != VAR_LIST)
9813 {
9814 EMSG(_(e_listreq));
9815 return;
9816 }
9817 if (argvars[1].vval.v_list == NULL)
9818 return;
9819
9820 if (argvars[0].v_type == VAR_FUNC)
9821 func = argvars[0].vval.v_string;
9822 else
9823 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009824 if (*func == NUL)
9825 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009826
Bram Moolenaare9a41262005-01-15 22:18:47 +00009827 if (argvars[2].v_type != VAR_UNKNOWN)
9828 {
9829 if (argvars[2].v_type != VAR_DICT)
9830 {
9831 EMSG(_(e_dictreq));
9832 return;
9833 }
9834 selfdict = argvars[2].vval.v_dict;
9835 }
9836
Bram Moolenaardb913952012-06-29 12:54:53 +02009837 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009838}
9839
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009840#ifdef FEAT_FLOAT
9841/*
9842 * "ceil({float})" function
9843 */
9844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009845f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009846{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009847 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009848
9849 rettv->v_type = VAR_FLOAT;
9850 if (get_float_arg(argvars, &f) == OK)
9851 rettv->vval.v_float = ceil(f);
9852 else
9853 rettv->vval.v_float = 0.0;
9854}
9855#endif
9856
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009857#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009858/*
9859 * Get a callback from "arg". It can be a Funcref or a function name.
9860 * When "arg" is zero return an empty string.
9861 * Return NULL for an invalid argument.
9862 */
9863 static char_u *
9864get_callback(typval_T *arg)
9865{
9866 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9867 return arg->vval.v_string;
9868 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9869 return (char_u *)"";
9870 EMSG(_("E999: Invalid callback argument"));
9871 return NULL;
9872}
9873
9874/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009875 * Get the option entries from the dict in "tv", parse them and put the result
9876 * in "opt".
9877 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009878 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009879 */
9880 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009881get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009882{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009883 typval_T *item;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009884 char_u *mode;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009885 dict_T *dict;
9886 int todo;
9887 hashitem_T *hi;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009888
Bram Moolenaar8600ace2016-02-19 23:31:40 +01009889 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009890 if (tv->v_type == VAR_UNKNOWN)
9891 return OK;
9892 if (tv->v_type != VAR_DICT)
9893 {
9894 EMSG(_(e_invarg));
9895 return FAIL;
9896 }
9897 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009898 if (dict == NULL)
9899 return OK;
9900
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009901 todo = (int)dict->dv_hashtab.ht_used;
9902 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
9903 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009904 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009905 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009906
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009907 if (STRCMP(hi->hi_key, "mode") == 0)
9908 {
9909 if (!(supported & JO_MODE))
9910 break;
9911 opt->jo_set |= JO_MODE;
9912 mode = get_tv_string(item);
9913 if (STRCMP(mode, "nl") == 0)
9914 opt->jo_mode = MODE_NL;
9915 else if (STRCMP(mode, "raw") == 0)
9916 opt->jo_mode = MODE_RAW;
9917 else if (STRCMP(mode, "js") == 0)
9918 opt->jo_mode = MODE_JS;
9919 else if (STRCMP(mode, "json") == 0)
9920 opt->jo_mode = MODE_JSON;
9921 else
9922 {
9923 EMSG2(_(e_invarg2), mode);
9924 return FAIL;
9925 }
9926 }
9927 else if (STRCMP(hi->hi_key, "callback") == 0)
9928 {
9929 if (!(supported & JO_CALLBACK))
9930 break;
9931 opt->jo_set |= JO_CALLBACK;
9932 opt->jo_callback = get_callback(item);
9933 if (opt->jo_callback == NULL)
9934 {
9935 EMSG2(_(e_invarg2), "callback");
9936 return FAIL;
9937 }
9938 }
9939 else if (STRCMP(hi->hi_key, "waittime") == 0)
9940 {
9941 if (!(supported & JO_WAITTIME))
9942 break;
9943 opt->jo_set |= JO_WAITTIME;
9944 opt->jo_waittime = get_tv_number(item);
9945 }
9946 else if (STRCMP(hi->hi_key, "timeout") == 0)
9947 {
9948 if (!(supported & JO_TIMEOUT))
9949 break;
9950 opt->jo_set |= JO_TIMEOUT;
9951 opt->jo_timeout = get_tv_number(item);
9952 }
9953 else
9954 break;
9955 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009956 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009957 if (todo > 0)
9958 {
9959 EMSG2(_(e_invarg2), hi->hi_key);
9960 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009961 }
9962
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009963 return OK;
9964}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009965#endif
9966
9967#ifdef FEAT_CHANNEL
9968/*
9969 * Get the channel from the argument.
9970 * Returns NULL if the handle is invalid.
9971 */
9972 static channel_T *
9973get_channel_arg(typval_T *tv)
9974{
9975 channel_T *channel;
9976
9977 if (tv->v_type != VAR_CHANNEL)
9978 {
9979 EMSG2(_(e_invarg2), get_tv_string(tv));
9980 return NULL;
9981 }
9982 channel = tv->vval.v_channel;
9983
9984 if (channel == NULL || !channel_is_open(channel))
9985 {
9986 EMSG(_("E906: not an open channel"));
9987 return NULL;
9988 }
9989 return channel;
9990}
9991
9992/*
9993 * "ch_close()" function
9994 */
9995 static void
9996f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9997{
9998 channel_T *channel = get_channel_arg(&argvars[0]);
9999
10000 if (channel != NULL)
10001 channel_close(channel);
10002}
10003
10004/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010005 * "ch_log()" function
10006 */
10007 static void
10008f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10009{
10010 char_u *msg = get_tv_string(&argvars[0]);
10011 channel_T *channel = NULL;
10012
10013 if (argvars[1].v_type != VAR_UNKNOWN)
10014 channel = get_channel_arg(&argvars[1]);
10015
10016 ch_log(channel, (char *)msg);
10017}
10018
10019/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010020 * "ch_logfile()" function
10021 */
10022 static void
10023f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10024{
10025 char_u *fname;
10026 char_u *opt = (char_u *)"";
10027 char_u buf[NUMBUFLEN];
10028 FILE *file = NULL;
10029
10030 fname = get_tv_string(&argvars[0]);
10031 if (argvars[1].v_type == VAR_STRING)
10032 opt = get_tv_string_buf(&argvars[1], buf);
10033 if (*fname != NUL)
10034 {
10035 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10036 if (file == NULL)
10037 {
10038 EMSG2(_(e_notopen), fname);
10039 return;
10040 }
10041 }
10042 ch_logfile(file);
10043}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010044
10045/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010046 * "ch_open()" function
10047 */
10048 static void
10049f_ch_open(typval_T *argvars, typval_T *rettv)
10050{
10051 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010052 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010053 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010054 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010055 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010056 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010057
10058 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010059 rettv->v_type = VAR_CHANNEL;
10060 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010061
10062 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010063 if (argvars[1].v_type != VAR_UNKNOWN
10064 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010065 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010066 EMSG(_(e_invarg));
10067 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010068 }
10069
10070 /* parse address */
10071 p = vim_strchr(address, ':');
10072 if (p == NULL)
10073 {
10074 EMSG2(_(e_invarg2), address);
10075 return;
10076 }
10077 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010078 port = strtol((char *)p, &rest, 10);
10079 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010080 {
10081 p[-1] = ':';
10082 EMSG2(_(e_invarg2), address);
10083 return;
10084 }
10085
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010086 /* parse options */
10087 opt.jo_mode = MODE_JSON;
10088 opt.jo_callback = NULL;
10089 opt.jo_waittime = 0;
10090 opt.jo_timeout = 2000;
10091 if (get_job_options(&argvars[1], &opt,
10092 JO_MODE + JO_CALLBACK + JO_WAITTIME + JO_TIMEOUT) == FAIL)
10093 return;
10094 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010095 {
10096 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010097 return;
10098 }
10099
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010100 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010101 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010102 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010103 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010104 opt.jo_set = JO_ALL;
10105 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010106 }
10107}
10108
10109/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010110 * "ch_readraw()" function
10111 */
10112 static void
10113f_ch_readraw(typval_T *argvars, typval_T *rettv)
10114{
Bram Moolenaar77073442016-02-13 23:23:53 +010010115 channel_T *channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010116
10117 /* return an empty string by default */
10118 rettv->v_type = VAR_STRING;
10119 rettv->vval.v_string = NULL;
10120
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010121 /* TODO: use timeout from the options */
10122
Bram Moolenaar77073442016-02-13 23:23:53 +010010123 channel = get_channel_arg(&argvars[0]);
10124 if (channel != NULL)
10125 rettv->vval.v_string = channel_read_block(channel);
10126}
10127
10128/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010129 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010130 * Returns the channel if the caller should read the response.
10131 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010132 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010133 static channel_T *
Bram Moolenaara07fec92016-02-05 21:04:08 +010010134send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010135{
Bram Moolenaar77073442016-02-13 23:23:53 +010010136 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010137 jobopt_T opt;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010138
Bram Moolenaar77073442016-02-13 23:23:53 +010010139 channel = get_channel_arg(&argvars[0]);
10140 if (channel == NULL)
10141 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010142
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010143 opt.jo_callback = NULL;
10144 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10145 return NULL;
10146
Bram Moolenaara07fec92016-02-05 21:04:08 +010010147 /* Set the callback. An empty callback means no callback and not reading
10148 * the response. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010149 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
10150 channel_set_req_callback(channel, opt.jo_callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010151
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010152 if (channel_send(channel, text, fun) == OK && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010153 return channel;
10154 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010155}
10156
10157/*
10158 * "ch_sendexpr()" function
10159 */
10160 static void
10161f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10162{
10163 char_u *text;
10164 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010165 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010166 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010167 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010168
10169 /* return an empty string by default */
10170 rettv->v_type = VAR_STRING;
10171 rettv->vval.v_string = NULL;
10172
Bram Moolenaar77073442016-02-13 23:23:53 +010010173 channel = get_channel_arg(&argvars[0]);
10174 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010175 return;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010176
Bram Moolenaar77073442016-02-13 23:23:53 +010010177 ch_mode = channel_get_mode(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010178 if (ch_mode == MODE_RAW)
10179 {
10180 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
10181 return;
10182 }
10183
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010184 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010185 text = json_encode_nr_expr(id, &argvars[1],
10186 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010187 if (text == NULL)
10188 return;
10189
Bram Moolenaar77073442016-02-13 23:23:53 +010010190 channel = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010191 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010192 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010193 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010194 if (channel_read_json_block(channel, id, &listtv) == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010195 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010196 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010197
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010198 /* Move the item from the list and then change the type to
10199 * avoid the value being freed. */
10200 *rettv = list->lv_last->li_tv;
10201 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010202 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010203 }
10204 }
10205}
10206
10207/*
10208 * "ch_sendraw()" function
10209 */
10210 static void
10211f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10212{
10213 char_u buf[NUMBUFLEN];
10214 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010215 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010216
10217 /* return an empty string by default */
10218 rettv->v_type = VAR_STRING;
10219 rettv->vval.v_string = NULL;
10220
10221 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar77073442016-02-13 23:23:53 +010010222 channel = send_common(argvars, text, 0, "sendraw");
10223 if (channel != NULL)
10224 rettv->vval.v_string = channel_read_block(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010225}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010226
10227/*
10228 * "ch_setoptions()" function
10229 */
10230 static void
10231f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10232{
10233 channel_T *channel;
10234 jobopt_T opt;
10235
10236 channel = get_channel_arg(&argvars[0]);
10237 if (channel == NULL)
10238 return;
10239 if (get_job_options(&argvars[1], &opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010240 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010241 channel_set_options(channel, &opt);
10242}
10243
10244/*
10245 * "ch_status()" function
10246 */
10247 static void
10248f_ch_status(typval_T *argvars, typval_T *rettv)
10249{
10250 /* return an empty string by default */
10251 rettv->v_type = VAR_STRING;
10252
10253 if (argvars[0].v_type != VAR_CHANNEL)
10254 {
10255 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10256 rettv->vval.v_string = NULL;
10257 }
10258 else
10259 rettv->vval.v_string = vim_strsave(
10260 (char_u *)channel_status(argvars[0].vval.v_channel));
10261}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010262#endif
10263
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010264/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010265 * "changenr()" function
10266 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010268f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010269{
10270 rettv->vval.v_number = curbuf->b_u_seq_cur;
10271}
10272
10273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 * "char2nr(string)" function
10275 */
10276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010277f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278{
10279#ifdef FEAT_MBYTE
10280 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010281 {
10282 int utf8 = 0;
10283
10284 if (argvars[1].v_type != VAR_UNKNOWN)
10285 utf8 = get_tv_number_chk(&argvars[1], NULL);
10286
10287 if (utf8)
10288 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10289 else
10290 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 else
10293#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010294 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295}
10296
10297/*
10298 * "cindent(lnum)" function
10299 */
10300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010301f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302{
10303#ifdef FEAT_CINDENT
10304 pos_T pos;
10305 linenr_T lnum;
10306
10307 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10310 {
10311 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 curwin->w_cursor = pos;
10314 }
10315 else
10316#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010317 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318}
10319
10320/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010321 * "clearmatches()" function
10322 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010324f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010325{
10326#ifdef FEAT_SEARCH_EXTRA
10327 clear_matches(curwin);
10328#endif
10329}
10330
10331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 * "col(string)" function
10333 */
10334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010335f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336{
10337 colnr_T col = 0;
10338 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010339 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010341 fp = var2fpos(&argvars[0], FALSE, &fnum);
10342 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 {
10344 if (fp->col == MAXCOL)
10345 {
10346 /* '> can be MAXCOL, get the length of the line then */
10347 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010348 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 else
10350 col = MAXCOL;
10351 }
10352 else
10353 {
10354 col = fp->col + 1;
10355#ifdef FEAT_VIRTUALEDIT
10356 /* col(".") when the cursor is on the NUL at the end of the line
10357 * because of "coladd" can be seen as an extra column. */
10358 if (virtual_active() && fp == &curwin->w_cursor)
10359 {
10360 char_u *p = ml_get_cursor();
10361
10362 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10363 curwin->w_virtcol - curwin->w_cursor.coladd))
10364 {
10365# ifdef FEAT_MBYTE
10366 int l;
10367
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010368 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 col += l;
10370# else
10371 if (*p != NUL && p[1] == NUL)
10372 ++col;
10373# endif
10374 }
10375 }
10376#endif
10377 }
10378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380}
10381
Bram Moolenaar572cb562005-08-05 21:35:02 +000010382#if defined(FEAT_INS_EXPAND)
10383/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010384 * "complete()" function
10385 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010387f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010388{
10389 int startcol;
10390
10391 if ((State & INSERT) == 0)
10392 {
10393 EMSG(_("E785: complete() can only be used in Insert mode"));
10394 return;
10395 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010396
10397 /* Check for undo allowed here, because if something was already inserted
10398 * the line was already saved for undo and this check isn't done. */
10399 if (!undo_allowed())
10400 return;
10401
Bram Moolenaarade00832006-03-10 21:46:58 +000010402 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10403 {
10404 EMSG(_(e_invarg));
10405 return;
10406 }
10407
10408 startcol = get_tv_number_chk(&argvars[0], NULL);
10409 if (startcol <= 0)
10410 return;
10411
10412 set_completion(startcol - 1, argvars[1].vval.v_list);
10413}
10414
10415/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010416 * "complete_add()" function
10417 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010419f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010420{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010421 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010422}
10423
10424/*
10425 * "complete_check()" function
10426 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010428f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010429{
10430 int saved = RedrawingDisabled;
10431
10432 RedrawingDisabled = 0;
10433 ins_compl_check_keys(0);
10434 rettv->vval.v_number = compl_interrupted;
10435 RedrawingDisabled = saved;
10436}
10437#endif
10438
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439/*
10440 * "confirm(message, buttons[, default [, type]])" function
10441 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010443f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444{
10445#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10446 char_u *message;
10447 char_u *buttons = NULL;
10448 char_u buf[NUMBUFLEN];
10449 char_u buf2[NUMBUFLEN];
10450 int def = 1;
10451 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010452 char_u *typestr;
10453 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010455 message = get_tv_string_chk(&argvars[0]);
10456 if (message == NULL)
10457 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010458 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10461 if (buttons == NULL)
10462 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010463 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010465 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010466 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10469 if (typestr == NULL)
10470 error = TRUE;
10471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 switch (TOUPPER_ASC(*typestr))
10474 {
10475 case 'E': type = VIM_ERROR; break;
10476 case 'Q': type = VIM_QUESTION; break;
10477 case 'I': type = VIM_INFO; break;
10478 case 'W': type = VIM_WARNING; break;
10479 case 'G': type = VIM_GENERIC; break;
10480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 }
10482 }
10483 }
10484 }
10485
10486 if (buttons == NULL || *buttons == NUL)
10487 buttons = (char_u *)_("&Ok");
10488
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010489 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010490 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010491 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492#endif
10493}
10494
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010495/*
10496 * "copy()" function
10497 */
10498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010499f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010500{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010501 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010502}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010504#ifdef FEAT_FLOAT
10505/*
10506 * "cos()" function
10507 */
10508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010509f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010510{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010511 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010512
10513 rettv->v_type = VAR_FLOAT;
10514 if (get_float_arg(argvars, &f) == OK)
10515 rettv->vval.v_float = cos(f);
10516 else
10517 rettv->vval.v_float = 0.0;
10518}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010519
10520/*
10521 * "cosh()" function
10522 */
10523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010524f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010525{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010526 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010527
10528 rettv->v_type = VAR_FLOAT;
10529 if (get_float_arg(argvars, &f) == OK)
10530 rettv->vval.v_float = cosh(f);
10531 else
10532 rettv->vval.v_float = 0.0;
10533}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010534#endif
10535
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010537 * "count()" function
10538 */
10539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010540f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010541{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010542 long n = 0;
10543 int ic = FALSE;
10544
Bram Moolenaare9a41262005-01-15 22:18:47 +000010545 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010546 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 listitem_T *li;
10548 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010549 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010550
Bram Moolenaare9a41262005-01-15 22:18:47 +000010551 if ((l = argvars[0].vval.v_list) != NULL)
10552 {
10553 li = l->lv_first;
10554 if (argvars[2].v_type != VAR_UNKNOWN)
10555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556 int error = FALSE;
10557
10558 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010559 if (argvars[3].v_type != VAR_UNKNOWN)
10560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010561 idx = get_tv_number_chk(&argvars[3], &error);
10562 if (!error)
10563 {
10564 li = list_find(l, idx);
10565 if (li == NULL)
10566 EMSGN(_(e_listidx), idx);
10567 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010569 if (error)
10570 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010571 }
10572
10573 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010574 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010575 ++n;
10576 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010577 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578 else if (argvars[0].v_type == VAR_DICT)
10579 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010580 int todo;
10581 dict_T *d;
10582 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010584 if ((d = argvars[0].vval.v_dict) != NULL)
10585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 int error = FALSE;
10587
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588 if (argvars[2].v_type != VAR_UNKNOWN)
10589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010590 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010591 if (argvars[3].v_type != VAR_UNKNOWN)
10592 EMSG(_(e_invarg));
10593 }
10594
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010595 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010596 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010597 {
10598 if (!HASHITEM_EMPTY(hi))
10599 {
10600 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010601 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010602 ++n;
10603 }
10604 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 }
10606 }
10607 else
10608 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010609 rettv->vval.v_number = n;
10610}
10611
10612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10614 *
10615 * Checks the existence of a cscope connection.
10616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010618f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619{
10620#ifdef FEAT_CSCOPE
10621 int num = 0;
10622 char_u *dbpath = NULL;
10623 char_u *prepend = NULL;
10624 char_u buf[NUMBUFLEN];
10625
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010626 if (argvars[0].v_type != VAR_UNKNOWN
10627 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629 num = (int)get_tv_number(&argvars[0]);
10630 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010631 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010632 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 }
10634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636#endif
10637}
10638
10639/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010640 * "cursor(lnum, col)" function, or
10641 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010643 * Moves the cursor to the specified line and column.
10644 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010647f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648{
10649 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010650#ifdef FEAT_VIRTUALEDIT
10651 long coladd = 0;
10652#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010653 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010655 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010656 if (argvars[1].v_type == VAR_UNKNOWN)
10657 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010658 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010659 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010660
Bram Moolenaar493c1782014-05-28 14:34:46 +020010661 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010662 {
10663 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010664 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010665 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010666 line = pos.lnum;
10667 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010668#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010669 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010670#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010671 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010672 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010673 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010674 set_curswant = FALSE;
10675 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010676 }
10677 else
10678 {
10679 line = get_tv_lnum(argvars);
10680 col = get_tv_number_chk(&argvars[1], NULL);
10681#ifdef FEAT_VIRTUALEDIT
10682 if (argvars[2].v_type != VAR_UNKNOWN)
10683 coladd = get_tv_number_chk(&argvars[2], NULL);
10684#endif
10685 }
10686 if (line < 0 || col < 0
10687#ifdef FEAT_VIRTUALEDIT
10688 || coladd < 0
10689#endif
10690 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010691 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 if (line > 0)
10693 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 if (col > 0)
10695 curwin->w_cursor.col = col - 1;
10696#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010697 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698#endif
10699
10700 /* Make sure the cursor is in a valid position. */
10701 check_cursor();
10702#ifdef FEAT_MBYTE
10703 /* Correct cursor for multi-byte character. */
10704 if (has_mbyte)
10705 mb_adjust_cursor();
10706#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010707
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010708 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010709 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710}
10711
10712/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010713 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 */
10715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010716f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010718 int noref = 0;
10719
10720 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010722 if (noref < 0 || noref > 1)
10723 EMSG(_(e_invarg));
10724 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010725 {
10726 current_copyID += COPYID_INC;
10727 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729}
10730
10731/*
10732 * "delete()" function
10733 */
10734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010735f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010737 char_u nbuf[NUMBUFLEN];
10738 char_u *name;
10739 char_u *flags;
10740
10741 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010743 return;
10744
10745 name = get_tv_string(&argvars[0]);
10746 if (name == NULL || *name == NUL)
10747 {
10748 EMSG(_(e_invarg));
10749 return;
10750 }
10751
10752 if (argvars[1].v_type != VAR_UNKNOWN)
10753 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010755 flags = (char_u *)"";
10756
10757 if (*flags == NUL)
10758 /* delete a file */
10759 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10760 else if (STRCMP(flags, "d") == 0)
10761 /* delete an empty directory */
10762 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10763 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010764 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010765 rettv->vval.v_number = delete_recursive(name);
10766 else
10767 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768}
10769
10770/*
10771 * "did_filetype()" function
10772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010774f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775{
10776#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778#endif
10779}
10780
10781/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010782 * "diff_filler()" function
10783 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010785f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010786{
10787#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010789#endif
10790}
10791
10792/*
10793 * "diff_hlID()" function
10794 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010796f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010797{
10798#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010799 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010800 static linenr_T prev_lnum = 0;
10801 static int changedtick = 0;
10802 static int fnum = 0;
10803 static int change_start = 0;
10804 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010805 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010806 int filler_lines;
10807 int col;
10808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 if (lnum < 0) /* ignore type error in {lnum} arg */
10810 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010811 if (lnum != prev_lnum
10812 || changedtick != curbuf->b_changedtick
10813 || fnum != curbuf->b_fnum)
10814 {
10815 /* New line, buffer, change: need to get the values. */
10816 filler_lines = diff_check(curwin, lnum);
10817 if (filler_lines < 0)
10818 {
10819 if (filler_lines == -1)
10820 {
10821 change_start = MAXCOL;
10822 change_end = -1;
10823 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10824 hlID = HLF_ADD; /* added line */
10825 else
10826 hlID = HLF_CHD; /* changed line */
10827 }
10828 else
10829 hlID = HLF_ADD; /* added line */
10830 }
10831 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010832 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010833 prev_lnum = lnum;
10834 changedtick = curbuf->b_changedtick;
10835 fnum = curbuf->b_fnum;
10836 }
10837
10838 if (hlID == HLF_CHD || hlID == HLF_TXD)
10839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010841 if (col >= change_start && col <= change_end)
10842 hlID = HLF_TXD; /* changed text */
10843 else
10844 hlID = HLF_CHD; /* changed line */
10845 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010846 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010847#endif
10848}
10849
10850/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010851 * "disable_char_avail_for_testing({expr})" function
10852 */
10853 static void
10854f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10855{
10856 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10857}
10858
10859/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010860 * "empty({expr})" function
10861 */
10862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010863f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010864{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010865 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010866
10867 switch (argvars[0].v_type)
10868 {
10869 case VAR_STRING:
10870 case VAR_FUNC:
10871 n = argvars[0].vval.v_string == NULL
10872 || *argvars[0].vval.v_string == NUL;
10873 break;
10874 case VAR_NUMBER:
10875 n = argvars[0].vval.v_number == 0;
10876 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010877 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010878#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010879 n = argvars[0].vval.v_float == 0.0;
10880 break;
10881#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010882 case VAR_LIST:
10883 n = argvars[0].vval.v_list == NULL
10884 || argvars[0].vval.v_list->lv_first == NULL;
10885 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010886 case VAR_DICT:
10887 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010888 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010889 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010890 case VAR_SPECIAL:
10891 n = argvars[0].vval.v_number != VVAL_TRUE;
10892 break;
10893
Bram Moolenaar835dc632016-02-07 14:27:38 +010010894 case VAR_JOB:
10895#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010010896 n = argvars[0].vval.v_job == NULL
10897 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10898 break;
10899#endif
10900 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010010901#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010902 n = argvars[0].vval.v_channel == NULL
10903 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010904 break;
10905#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010906 case VAR_UNKNOWN:
10907 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10908 n = TRUE;
10909 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010910 }
10911
10912 rettv->vval.v_number = n;
10913}
10914
10915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 * "escape({string}, {chars})" function
10917 */
10918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010919f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920{
10921 char_u buf[NUMBUFLEN];
10922
Bram Moolenaar758711c2005-02-02 23:11:38 +000010923 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10924 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926}
10927
10928/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010929 * "eval()" function
10930 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010932f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010933{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010934 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010936 s = get_tv_string_chk(&argvars[0]);
10937 if (s != NULL)
10938 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010939
Bram Moolenaar615b9972015-01-14 17:15:05 +010010940 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010941 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10942 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010943 if (p != NULL && !aborting())
10944 EMSG2(_(e_invexpr2), p);
10945 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010947 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010948 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010949 else if (*s != NUL)
10950 EMSG(_(e_trailing));
10951}
10952
10953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 * "eventhandler()" function
10955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010957f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010959 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960}
10961
10962/*
10963 * "executable()" function
10964 */
10965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010966f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010968 char_u *name = get_tv_string(&argvars[0]);
10969
10970 /* Check in $PATH and also check directly if there is a directory name. */
10971 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10972 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010973}
10974
10975/*
10976 * "exepath()" function
10977 */
10978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010979f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010980{
10981 char_u *p = NULL;
10982
Bram Moolenaarb5971142015-03-21 17:32:19 +010010983 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010984 rettv->v_type = VAR_STRING;
10985 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986}
10987
10988/*
10989 * "exists()" function
10990 */
10991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010992f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993{
10994 char_u *p;
10995 char_u *name;
10996 int n = FALSE;
10997 int len = 0;
10998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 if (*p == '$') /* environment variable */
11001 {
11002 /* first try "normal" environment variables (fast) */
11003 if (mch_getenv(p + 1) != NULL)
11004 n = TRUE;
11005 else
11006 {
11007 /* try expanding things like $VIM and ${HOME} */
11008 p = expand_env_save(p);
11009 if (p != NULL && *p != '$')
11010 n = TRUE;
11011 vim_free(p);
11012 }
11013 }
11014 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011017 if (*skipwhite(p) != NUL)
11018 n = FALSE; /* trailing garbage */
11019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 else if (*p == '*') /* internal or user defined function */
11021 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011022 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 }
11024 else if (*p == ':')
11025 {
11026 n = cmd_exists(p + 1);
11027 }
11028 else if (*p == '#')
11029 {
11030#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011031 if (p[1] == '#')
11032 n = autocmd_supported(p + 2);
11033 else
11034 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035#endif
11036 }
11037 else /* internal variable */
11038 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011039 char_u *tofree;
11040 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011042 /* get_name_len() takes care of expanding curly braces */
11043 name = p;
11044 len = get_name_len(&p, &tofree, TRUE, FALSE);
11045 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011047 if (tofree != NULL)
11048 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011049 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011050 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011052 /* handle d.key, l[idx], f(expr) */
11053 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11054 if (n)
11055 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 }
11057 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011058 if (*p != NUL)
11059 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011061 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 }
11063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065}
11066
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011067#ifdef FEAT_FLOAT
11068/*
11069 * "exp()" function
11070 */
11071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011072f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011073{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011074 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011075
11076 rettv->v_type = VAR_FLOAT;
11077 if (get_float_arg(argvars, &f) == OK)
11078 rettv->vval.v_float = exp(f);
11079 else
11080 rettv->vval.v_float = 0.0;
11081}
11082#endif
11083
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084/*
11085 * "expand()" function
11086 */
11087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011088f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089{
11090 char_u *s;
11091 int len;
11092 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011093 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011096 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011099 if (argvars[1].v_type != VAR_UNKNOWN
11100 && argvars[2].v_type != VAR_UNKNOWN
11101 && get_tv_number_chk(&argvars[2], &error)
11102 && !error)
11103 {
11104 rettv->v_type = VAR_LIST;
11105 rettv->vval.v_list = NULL;
11106 }
11107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 if (*s == '%' || *s == '#' || *s == '<')
11110 {
11111 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011112 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011114 if (rettv->v_type == VAR_LIST)
11115 {
11116 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11117 list_append_string(rettv->vval.v_list, result, -1);
11118 else
11119 vim_free(result);
11120 }
11121 else
11122 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 }
11124 else
11125 {
11126 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011127 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 if (argvars[1].v_type != VAR_UNKNOWN
11129 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011130 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011131 if (!error)
11132 {
11133 ExpandInit(&xpc);
11134 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011135 if (p_wic)
11136 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011137 if (rettv->v_type == VAR_STRING)
11138 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11139 options, WILD_ALL);
11140 else if (rettv_list_alloc(rettv) != FAIL)
11141 {
11142 int i;
11143
11144 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11145 for (i = 0; i < xpc.xp_numfiles; i++)
11146 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11147 ExpandCleanup(&xpc);
11148 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011149 }
11150 else
11151 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 }
11153}
11154
11155/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011156 * Go over all entries in "d2" and add them to "d1".
11157 * When "action" is "error" then a duplicate key is an error.
11158 * When "action" is "force" then a duplicate key is overwritten.
11159 * Otherwise duplicate keys are ignored ("action" is "keep").
11160 */
11161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011162dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011163{
11164 dictitem_T *di1;
11165 hashitem_T *hi2;
11166 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011167 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011168
11169 todo = (int)d2->dv_hashtab.ht_used;
11170 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11171 {
11172 if (!HASHITEM_EMPTY(hi2))
11173 {
11174 --todo;
11175 di1 = dict_find(d1, hi2->hi_key, -1);
11176 if (d1->dv_scope != 0)
11177 {
11178 /* Disallow replacing a builtin function in l: and g:.
11179 * Check the key to be valid when adding to any
11180 * scope. */
11181 if (d1->dv_scope == VAR_DEF_SCOPE
11182 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11183 && var_check_func_name(hi2->hi_key,
11184 di1 == NULL))
11185 break;
11186 if (!valid_varname(hi2->hi_key))
11187 break;
11188 }
11189 if (di1 == NULL)
11190 {
11191 di1 = dictitem_copy(HI2DI(hi2));
11192 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11193 dictitem_free(di1);
11194 }
11195 else if (*action == 'e')
11196 {
11197 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11198 break;
11199 }
11200 else if (*action == 'f' && HI2DI(hi2) != di1)
11201 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011202 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11203 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011204 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011205 clear_tv(&di1->di_tv);
11206 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11207 }
11208 }
11209 }
11210}
11211
11212/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011213 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011214 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011215 */
11216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011217f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011218{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011219 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011220
Bram Moolenaare9a41262005-01-15 22:18:47 +000011221 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011222 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011223 list_T *l1, *l2;
11224 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011225 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011226 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011227
Bram Moolenaare9a41262005-01-15 22:18:47 +000011228 l1 = argvars[0].vval.v_list;
11229 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011230 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011231 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011232 {
11233 if (argvars[2].v_type != VAR_UNKNOWN)
11234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 before = get_tv_number_chk(&argvars[2], &error);
11236 if (error)
11237 return; /* type error; errmsg already given */
11238
Bram Moolenaar758711c2005-02-02 23:11:38 +000011239 if (before == l1->lv_len)
11240 item = NULL;
11241 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011242 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011243 item = list_find(l1, before);
11244 if (item == NULL)
11245 {
11246 EMSGN(_(e_listidx), before);
11247 return;
11248 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011249 }
11250 }
11251 else
11252 item = NULL;
11253 list_extend(l1, l2, item);
11254
Bram Moolenaare9a41262005-01-15 22:18:47 +000011255 copy_tv(&argvars[0], rettv);
11256 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011257 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011258 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11259 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011260 dict_T *d1, *d2;
11261 char_u *action;
11262 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011263
11264 d1 = argvars[0].vval.v_dict;
11265 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011266 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011267 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011268 {
11269 /* Check the third argument. */
11270 if (argvars[2].v_type != VAR_UNKNOWN)
11271 {
11272 static char *(av[]) = {"keep", "force", "error"};
11273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011274 action = get_tv_string_chk(&argvars[2]);
11275 if (action == NULL)
11276 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011277 for (i = 0; i < 3; ++i)
11278 if (STRCMP(action, av[i]) == 0)
11279 break;
11280 if (i == 3)
11281 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011282 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011283 return;
11284 }
11285 }
11286 else
11287 action = (char_u *)"force";
11288
Bram Moolenaara9922d62013-05-30 13:01:18 +020011289 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011290
Bram Moolenaare9a41262005-01-15 22:18:47 +000011291 copy_tv(&argvars[0], rettv);
11292 }
11293 }
11294 else
11295 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011296}
11297
11298/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011299 * "feedkeys()" function
11300 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011302f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011303{
11304 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011305 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011306 char_u *keys, *flags;
11307 char_u nbuf[NUMBUFLEN];
11308 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011309 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011310 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011311
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011312 /* This is not allowed in the sandbox. If the commands would still be
11313 * executed in the sandbox it would be OK, but it probably happens later,
11314 * when "sandbox" is no longer set. */
11315 if (check_secure())
11316 return;
11317
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011318 keys = get_tv_string(&argvars[0]);
11319 if (*keys != NUL)
11320 {
11321 if (argvars[1].v_type != VAR_UNKNOWN)
11322 {
11323 flags = get_tv_string_buf(&argvars[1], nbuf);
11324 for ( ; *flags != NUL; ++flags)
11325 {
11326 switch (*flags)
11327 {
11328 case 'n': remap = FALSE; break;
11329 case 'm': remap = TRUE; break;
11330 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011331 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011332 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011333 }
11334 }
11335 }
11336
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011337 /* Need to escape K_SPECIAL and CSI before putting the string in the
11338 * typeahead buffer. */
11339 keys_esc = vim_strsave_escape_csi(keys);
11340 if (keys_esc != NULL)
11341 {
11342 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011343 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011344 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011345 if (vgetc_busy)
11346 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011347 if (execute)
11348 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011349 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011350 }
11351}
11352
11353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 * "filereadable()" function
11355 */
11356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011357f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011359 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 char_u *p;
11361 int n;
11362
Bram Moolenaarc236c162008-07-13 17:41:49 +000011363#ifndef O_NONBLOCK
11364# define O_NONBLOCK 0
11365#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011367 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11368 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 {
11370 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011371 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 }
11373 else
11374 n = FALSE;
11375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377}
11378
11379/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011380 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 * rights to write into.
11382 */
11383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011384f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011386 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387}
11388
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011389 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011390findfilendir(
11391 typval_T *argvars UNUSED,
11392 typval_T *rettv,
11393 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011394{
11395#ifdef FEAT_SEARCHPATH
11396 char_u *fname;
11397 char_u *fresult = NULL;
11398 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11399 char_u *p;
11400 char_u pathbuf[NUMBUFLEN];
11401 int count = 1;
11402 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011403 int error = FALSE;
11404#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011405
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011406 rettv->vval.v_string = NULL;
11407 rettv->v_type = VAR_STRING;
11408
11409#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011411
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011412 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11415 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011416 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011417 else
11418 {
11419 if (*p != NUL)
11420 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011422 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011423 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011425 }
11426
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011427 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11428 error = TRUE;
11429
11430 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011432 do
11433 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011434 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011435 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 fresult = find_file_in_path_option(first ? fname : NULL,
11437 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011438 0, first, path,
11439 find_what,
11440 curbuf->b_ffname,
11441 find_what == FINDFILE_DIR
11442 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011444
11445 if (fresult != NULL && rettv->v_type == VAR_LIST)
11446 list_append_string(rettv->vval.v_list, fresult, -1);
11447
11448 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011450
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011451 if (rettv->v_type == VAR_STRING)
11452 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011453#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011454}
11455
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011456static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11457static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011458
11459/*
11460 * Implementation of map() and filter().
11461 */
11462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011463filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011464{
11465 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011466 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011467 listitem_T *li, *nli;
11468 list_T *l = NULL;
11469 dictitem_T *di;
11470 hashtab_T *ht;
11471 hashitem_T *hi;
11472 dict_T *d = NULL;
11473 typval_T save_val;
11474 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011475 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011476 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011477 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011478 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011479 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011480 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011481 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011482
Bram Moolenaare9a41262005-01-15 22:18:47 +000011483 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011484 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011485 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011486 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011487 return;
11488 }
11489 else if (argvars[0].v_type == VAR_DICT)
11490 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011491 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011492 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011493 return;
11494 }
11495 else
11496 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011497 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011498 return;
11499 }
11500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 expr = get_tv_string_buf_chk(&argvars[1], buf);
11502 /* On type errors, the preceding call has already displayed an error
11503 * message. Avoid a misleading error message for an empty string that
11504 * was not passed as argument. */
11505 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011507 prepare_vimvar(VV_VAL, &save_val);
11508 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011509
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011510 /* We reset "did_emsg" to be able to detect whether an error
11511 * occurred during evaluation of the expression. */
11512 save_did_emsg = did_emsg;
11513 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011514
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011515 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011516 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011518 vimvars[VV_KEY].vv_type = VAR_STRING;
11519
11520 ht = &d->dv_hashtab;
11521 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011522 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011523 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011525 if (!HASHITEM_EMPTY(hi))
11526 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011527 int r;
11528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011529 --todo;
11530 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011531 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011532 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11533 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011534 break;
11535 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011536 r = filter_map_one(&di->di_tv, expr, map, &rem);
11537 clear_tv(&vimvars[VV_KEY].vv_tv);
11538 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539 break;
11540 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011541 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011542 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11543 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011544 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011546 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011547 }
11548 }
11549 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 }
11551 else
11552 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011553 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011555 for (li = l->lv_first; li != NULL; li = nli)
11556 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011557 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011558 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011560 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011561 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011562 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011563 break;
11564 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011565 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011566 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011567 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011568 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011569
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011570 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011571 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011572
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011573 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011574 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011575
11576 copy_tv(&argvars[0], rettv);
11577}
11578
11579 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011580filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011581{
Bram Moolenaar33570922005-01-25 22:26:29 +000011582 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011583 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011584 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011585
Bram Moolenaar33570922005-01-25 22:26:29 +000011586 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011587 s = expr;
11588 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011589 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011590 if (*s != NUL) /* check for trailing chars after expr */
11591 {
11592 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011593 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011594 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011595 }
11596 if (map)
11597 {
11598 /* map(): replace the list item value */
11599 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011600 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011601 *tv = rettv;
11602 }
11603 else
11604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605 int error = FALSE;
11606
Bram Moolenaare9a41262005-01-15 22:18:47 +000011607 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011608 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011609 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 /* On type error, nothing has been removed; return FAIL to stop the
11611 * loop. The error message was given by get_tv_number_chk(). */
11612 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011613 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011614 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011615 retval = OK;
11616theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011617 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011618 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011619}
11620
11621/*
11622 * "filter()" function
11623 */
11624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011625f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011626{
11627 filter_map(argvars, rettv, FALSE);
11628}
11629
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011630/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011631 * "finddir({fname}[, {path}[, {count}]])" function
11632 */
11633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011634f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011635{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011636 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011637}
11638
11639/*
11640 * "findfile({fname}[, {path}[, {count}]])" function
11641 */
11642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011643f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011644{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011645 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011646}
11647
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011648#ifdef FEAT_FLOAT
11649/*
11650 * "float2nr({float})" function
11651 */
11652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011653f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011654{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011655 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011656
11657 if (get_float_arg(argvars, &f) == OK)
11658 {
11659 if (f < -0x7fffffff)
11660 rettv->vval.v_number = -0x7fffffff;
11661 else if (f > 0x7fffffff)
11662 rettv->vval.v_number = 0x7fffffff;
11663 else
11664 rettv->vval.v_number = (varnumber_T)f;
11665 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011666}
11667
11668/*
11669 * "floor({float})" function
11670 */
11671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011672f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011673{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011674 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011675
11676 rettv->v_type = VAR_FLOAT;
11677 if (get_float_arg(argvars, &f) == OK)
11678 rettv->vval.v_float = floor(f);
11679 else
11680 rettv->vval.v_float = 0.0;
11681}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011682
11683/*
11684 * "fmod()" function
11685 */
11686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011687f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011688{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011689 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011690
11691 rettv->v_type = VAR_FLOAT;
11692 if (get_float_arg(argvars, &fx) == OK
11693 && get_float_arg(&argvars[1], &fy) == OK)
11694 rettv->vval.v_float = fmod(fx, fy);
11695 else
11696 rettv->vval.v_float = 0.0;
11697}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011698#endif
11699
Bram Moolenaar0d660222005-01-07 21:51:51 +000011700/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011701 * "fnameescape({string})" function
11702 */
11703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011704f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011705{
11706 rettv->vval.v_string = vim_strsave_fnameescape(
11707 get_tv_string(&argvars[0]), FALSE);
11708 rettv->v_type = VAR_STRING;
11709}
11710
11711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 * "fnamemodify({fname}, {mods})" function
11713 */
11714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011715f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
11717 char_u *fname;
11718 char_u *mods;
11719 int usedlen = 0;
11720 int len;
11721 char_u *fbuf = NULL;
11722 char_u buf[NUMBUFLEN];
11723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011724 fname = get_tv_string_chk(&argvars[0]);
11725 mods = get_tv_string_buf_chk(&argvars[1], buf);
11726 if (fname == NULL || mods == NULL)
11727 fname = NULL;
11728 else
11729 {
11730 len = (int)STRLEN(fname);
11731 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011736 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011738 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 vim_free(fbuf);
11740}
11741
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011742static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743
11744/*
11745 * "foldclosed()" function
11746 */
11747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011748foldclosed_both(
11749 typval_T *argvars UNUSED,
11750 typval_T *rettv,
11751 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752{
11753#ifdef FEAT_FOLDING
11754 linenr_T lnum;
11755 linenr_T first, last;
11756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11759 {
11760 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11761 {
11762 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 return;
11767 }
11768 }
11769#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771}
11772
11773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011774 * "foldclosed()" function
11775 */
11776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011777f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011778{
11779 foldclosed_both(argvars, rettv, FALSE);
11780}
11781
11782/*
11783 * "foldclosedend()" function
11784 */
11785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011786f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011787{
11788 foldclosed_both(argvars, rettv, TRUE);
11789}
11790
11791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 * "foldlevel()" function
11793 */
11794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011795f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796{
11797#ifdef FEAT_FOLDING
11798 linenr_T lnum;
11799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804}
11805
11806/*
11807 * "foldtext()" function
11808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011810f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811{
11812#ifdef FEAT_FOLDING
11813 linenr_T lnum;
11814 char_u *s;
11815 char_u *r;
11816 int len;
11817 char *txt;
11818#endif
11819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011820 rettv->v_type = VAR_STRING;
11821 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011823 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11824 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11825 <= curbuf->b_ml.ml_line_count
11826 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 {
11828 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011829 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11830 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 {
11832 if (!linewhite(lnum))
11833 break;
11834 ++lnum;
11835 }
11836
11837 /* Find interesting text in this line. */
11838 s = skipwhite(ml_get(lnum));
11839 /* skip C comment-start */
11840 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011843 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011844 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011845 {
11846 s = skipwhite(ml_get(lnum + 1));
11847 if (*s == '*')
11848 s = skipwhite(s + 1);
11849 }
11850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 txt = _("+-%s%3ld lines: ");
11852 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011853 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 + 20 /* for %3ld */
11855 + STRLEN(s))); /* concatenated */
11856 if (r != NULL)
11857 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011858 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11859 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11860 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861 len = (int)STRLEN(r);
11862 STRCAT(r, s);
11863 /* remove 'foldmarker' and 'commentstring' */
11864 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866 }
11867 }
11868#endif
11869}
11870
11871/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011872 * "foldtextresult(lnum)" function
11873 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011875f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011876{
11877#ifdef FEAT_FOLDING
11878 linenr_T lnum;
11879 char_u *text;
11880 char_u buf[51];
11881 foldinfo_T foldinfo;
11882 int fold_count;
11883#endif
11884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885 rettv->v_type = VAR_STRING;
11886 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011887#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011889 /* treat illegal types and illegal string values for {lnum} the same */
11890 if (lnum < 0)
11891 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011892 fold_count = foldedCount(curwin, lnum, &foldinfo);
11893 if (fold_count > 0)
11894 {
11895 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11896 &foldinfo, buf);
11897 if (text == buf)
11898 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011900 }
11901#endif
11902}
11903
11904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 * "foreground()" function
11906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011908f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910#ifdef FEAT_GUI
11911 if (gui.in_use)
11912 gui_mch_set_foreground();
11913#else
11914# ifdef WIN32
11915 win32_set_foreground();
11916# endif
11917#endif
11918}
11919
11920/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011921 * "function()" function
11922 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011924f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011925{
11926 char_u *s;
11927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011928 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011929 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011930 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011931 /* Don't check an autoload name for existence here. */
11932 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011933 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011934 else
11935 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011936 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011937 {
11938 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011939 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011940
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011941 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11942 * also be called from another script. Using trans_function_name()
11943 * would also work, but some plugins depend on the name being
11944 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011945 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011946 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011947 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011948 if (rettv->vval.v_string != NULL)
11949 {
11950 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011951 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011952 }
11953 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011954 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011955 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011957 }
11958}
11959
11960/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011961 * "garbagecollect()" function
11962 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011964f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011965{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011966 /* This is postponed until we are back at the toplevel, because we may be
11967 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11968 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011969
11970 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11971 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011972}
11973
11974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011975 * "get()" function
11976 */
11977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011978f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011979{
Bram Moolenaar33570922005-01-25 22:26:29 +000011980 listitem_T *li;
11981 list_T *l;
11982 dictitem_T *di;
11983 dict_T *d;
11984 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011985
Bram Moolenaare9a41262005-01-15 22:18:47 +000011986 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011987 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011988 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011990 int error = FALSE;
11991
11992 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11993 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011994 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011995 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011997 else if (argvars[0].v_type == VAR_DICT)
11998 {
11999 if ((d = argvars[0].vval.v_dict) != NULL)
12000 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012001 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012002 if (di != NULL)
12003 tv = &di->di_tv;
12004 }
12005 }
12006 else
12007 EMSG2(_(e_listdictarg), "get()");
12008
12009 if (tv == NULL)
12010 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012011 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012012 copy_tv(&argvars[2], rettv);
12013 }
12014 else
12015 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012016}
12017
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012018static 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 +000012019
12020/*
12021 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012022 * Return a range (from start to end) of lines in rettv from the specified
12023 * buffer.
12024 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012025 */
12026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012027get_buffer_lines(
12028 buf_T *buf,
12029 linenr_T start,
12030 linenr_T end,
12031 int retlist,
12032 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012033{
12034 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012035
Bram Moolenaar959a1432013-12-14 12:17:38 +010012036 rettv->v_type = VAR_STRING;
12037 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012038 if (retlist && rettv_list_alloc(rettv) == FAIL)
12039 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012040
12041 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12042 return;
12043
12044 if (!retlist)
12045 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012046 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12047 p = ml_get_buf(buf, start, FALSE);
12048 else
12049 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012050 rettv->vval.v_string = vim_strsave(p);
12051 }
12052 else
12053 {
12054 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012055 return;
12056
12057 if (start < 1)
12058 start = 1;
12059 if (end > buf->b_ml.ml_line_count)
12060 end = buf->b_ml.ml_line_count;
12061 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012062 if (list_append_string(rettv->vval.v_list,
12063 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012064 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012065 }
12066}
12067
12068/*
12069 * "getbufline()" function
12070 */
12071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012072f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012073{
12074 linenr_T lnum;
12075 linenr_T end;
12076 buf_T *buf;
12077
12078 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12079 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012080 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012081 --emsg_off;
12082
Bram Moolenaar661b1822005-07-28 22:36:45 +000012083 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012084 if (argvars[2].v_type == VAR_UNKNOWN)
12085 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012086 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012087 end = get_tv_lnum_buf(&argvars[2], buf);
12088
Bram Moolenaar342337a2005-07-21 21:11:17 +000012089 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012090}
12091
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092/*
12093 * "getbufvar()" function
12094 */
12095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012096f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012097{
12098 buf_T *buf;
12099 buf_T *save_curbuf;
12100 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012101 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012102 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012104 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12105 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012106 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012107 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012108
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012109 rettv->v_type = VAR_STRING;
12110 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012111
12112 if (buf != NULL && varname != NULL)
12113 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012114 /* set curbuf to be our buf, temporarily */
12115 save_curbuf = curbuf;
12116 curbuf = buf;
12117
Bram Moolenaar0d660222005-01-07 21:51:51 +000012118 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012119 {
12120 if (get_option_tv(&varname, rettv, TRUE) == OK)
12121 done = TRUE;
12122 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012123 else if (STRCMP(varname, "changedtick") == 0)
12124 {
12125 rettv->v_type = VAR_NUMBER;
12126 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012127 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012128 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012129 else
12130 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012131 /* Look up the variable. */
12132 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12133 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12134 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012135 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012136 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012137 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012138 done = TRUE;
12139 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012140 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012141
12142 /* restore previous notion of curbuf */
12143 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012144 }
12145
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012146 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12147 /* use the default value */
12148 copy_tv(&argvars[2], rettv);
12149
Bram Moolenaar0d660222005-01-07 21:51:51 +000012150 --emsg_off;
12151}
12152
12153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 * "getchar()" function
12155 */
12156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012157f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158{
12159 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012160 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012162 /* Position the cursor. Needed after a message that ends in a space. */
12163 windgoto(msg_row, msg_col);
12164
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 ++no_mapping;
12166 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012167 for (;;)
12168 {
12169 if (argvars[0].v_type == VAR_UNKNOWN)
12170 /* getchar(): blocking wait. */
12171 n = safe_vgetc();
12172 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12173 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012174 n = vpeekc_any();
12175 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012176 /* illegal argument or getchar(0) and no char avail: return zero */
12177 n = 0;
12178 else
12179 /* getchar(0) and char avail: return char */
12180 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012181
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012182 if (n == K_IGNORE)
12183 continue;
12184 break;
12185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 --no_mapping;
12187 --allow_keys;
12188
Bram Moolenaar219b8702006-11-01 14:32:36 +000012189 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12190 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12191 vimvars[VV_MOUSE_COL].vv_nr = 0;
12192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 if (IS_SPECIAL(n) || mod_mask != 0)
12195 {
12196 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12197 int i = 0;
12198
12199 /* Turn a special key into three bytes, plus modifier. */
12200 if (mod_mask != 0)
12201 {
12202 temp[i++] = K_SPECIAL;
12203 temp[i++] = KS_MODIFIER;
12204 temp[i++] = mod_mask;
12205 }
12206 if (IS_SPECIAL(n))
12207 {
12208 temp[i++] = K_SPECIAL;
12209 temp[i++] = K_SECOND(n);
12210 temp[i++] = K_THIRD(n);
12211 }
12212#ifdef FEAT_MBYTE
12213 else if (has_mbyte)
12214 i += (*mb_char2bytes)(n, temp + i);
12215#endif
12216 else
12217 temp[i++] = n;
12218 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219 rettv->v_type = VAR_STRING;
12220 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012221
12222#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012223 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012224 {
12225 int row = mouse_row;
12226 int col = mouse_col;
12227 win_T *win;
12228 linenr_T lnum;
12229# ifdef FEAT_WINDOWS
12230 win_T *wp;
12231# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012232 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012233
12234 if (row >= 0 && col >= 0)
12235 {
12236 /* Find the window at the mouse coordinates and compute the
12237 * text position. */
12238 win = mouse_find_win(&row, &col);
12239 (void)mouse_comp_pos(win, &row, &col, &lnum);
12240# ifdef FEAT_WINDOWS
12241 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012242 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012243# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012244 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012245 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12246 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12247 }
12248 }
12249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250 }
12251}
12252
12253/*
12254 * "getcharmod()" function
12255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012257f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012259 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260}
12261
12262/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012263 * "getcharsearch()" function
12264 */
12265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012266f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012267{
12268 if (rettv_dict_alloc(rettv) != FAIL)
12269 {
12270 dict_T *dict = rettv->vval.v_dict;
12271
12272 dict_add_nr_str(dict, "char", 0L, last_csearch());
12273 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12274 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12275 }
12276}
12277
12278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279 * "getcmdline()" function
12280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012282f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284 rettv->v_type = VAR_STRING;
12285 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286}
12287
12288/*
12289 * "getcmdpos()" function
12290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012292f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295}
12296
12297/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012298 * "getcmdtype()" function
12299 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012301f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012302{
12303 rettv->v_type = VAR_STRING;
12304 rettv->vval.v_string = alloc(2);
12305 if (rettv->vval.v_string != NULL)
12306 {
12307 rettv->vval.v_string[0] = get_cmdline_type();
12308 rettv->vval.v_string[1] = NUL;
12309 }
12310}
12311
12312/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012313 * "getcmdwintype()" function
12314 */
12315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012316f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012317{
12318 rettv->v_type = VAR_STRING;
12319 rettv->vval.v_string = NULL;
12320#ifdef FEAT_CMDWIN
12321 rettv->vval.v_string = alloc(2);
12322 if (rettv->vval.v_string != NULL)
12323 {
12324 rettv->vval.v_string[0] = cmdwin_type;
12325 rettv->vval.v_string[1] = NUL;
12326 }
12327#endif
12328}
12329
12330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 * "getcwd()" function
12332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012334f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012336 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012337 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012339 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012340 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012341
12342 wp = find_tabwin(&argvars[0], &argvars[1]);
12343 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012345 if (wp->w_localdir != NULL)
12346 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12347 else if(globaldir != NULL)
12348 rettv->vval.v_string = vim_strsave(globaldir);
12349 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012350 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012351 cwd = alloc(MAXPATHL);
12352 if (cwd != NULL)
12353 {
12354 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12355 rettv->vval.v_string = vim_strsave(cwd);
12356 vim_free(cwd);
12357 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012358 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012359#ifdef BACKSLASH_IN_FILENAME
12360 if (rettv->vval.v_string != NULL)
12361 slash_adjust(rettv->vval.v_string);
12362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 }
12364}
12365
12366/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012367 * "getfontname()" function
12368 */
12369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012370f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012371{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012372 rettv->v_type = VAR_STRING;
12373 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012374#ifdef FEAT_GUI
12375 if (gui.in_use)
12376 {
12377 GuiFont font;
12378 char_u *name = NULL;
12379
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012380 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012381 {
12382 /* Get the "Normal" font. Either the name saved by
12383 * hl_set_font_name() or from the font ID. */
12384 font = gui.norm_font;
12385 name = hl_get_font_name();
12386 }
12387 else
12388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012390 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12391 return;
12392 font = gui_mch_get_font(name, FALSE);
12393 if (font == NOFONT)
12394 return; /* Invalid font name, return empty string. */
12395 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012397 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012398 gui_mch_free_font(font);
12399 }
12400#endif
12401}
12402
12403/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012404 * "getfperm({fname})" function
12405 */
12406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012407f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012408{
12409 char_u *fname;
12410 struct stat st;
12411 char_u *perm = NULL;
12412 char_u flags[] = "rwx";
12413 int i;
12414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012417 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012418 if (mch_stat((char *)fname, &st) >= 0)
12419 {
12420 perm = vim_strsave((char_u *)"---------");
12421 if (perm != NULL)
12422 {
12423 for (i = 0; i < 9; i++)
12424 {
12425 if (st.st_mode & (1 << (8 - i)))
12426 perm[i] = flags[i % 3];
12427 }
12428 }
12429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012431}
12432
12433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 * "getfsize({fname})" function
12435 */
12436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012437f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438{
12439 char_u *fname;
12440 struct stat st;
12441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012442 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445
12446 if (mch_stat((char *)fname, &st) >= 0)
12447 {
12448 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012452 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012453
12454 /* non-perfect check for overflow */
12455 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12456 rettv->vval.v_number = -2;
12457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 }
12459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012460 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461}
12462
12463/*
12464 * "getftime({fname})" function
12465 */
12466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012467f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468{
12469 char_u *fname;
12470 struct stat st;
12471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012472 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473
12474 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012477 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478}
12479
12480/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012481 * "getftype({fname})" function
12482 */
12483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012484f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012485{
12486 char_u *fname;
12487 struct stat st;
12488 char_u *type = NULL;
12489 char *t;
12490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012493 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012494 if (mch_lstat((char *)fname, &st) >= 0)
12495 {
12496#ifdef S_ISREG
12497 if (S_ISREG(st.st_mode))
12498 t = "file";
12499 else if (S_ISDIR(st.st_mode))
12500 t = "dir";
12501# ifdef S_ISLNK
12502 else if (S_ISLNK(st.st_mode))
12503 t = "link";
12504# endif
12505# ifdef S_ISBLK
12506 else if (S_ISBLK(st.st_mode))
12507 t = "bdev";
12508# endif
12509# ifdef S_ISCHR
12510 else if (S_ISCHR(st.st_mode))
12511 t = "cdev";
12512# endif
12513# ifdef S_ISFIFO
12514 else if (S_ISFIFO(st.st_mode))
12515 t = "fifo";
12516# endif
12517# ifdef S_ISSOCK
12518 else if (S_ISSOCK(st.st_mode))
12519 t = "fifo";
12520# endif
12521 else
12522 t = "other";
12523#else
12524# ifdef S_IFMT
12525 switch (st.st_mode & S_IFMT)
12526 {
12527 case S_IFREG: t = "file"; break;
12528 case S_IFDIR: t = "dir"; break;
12529# ifdef S_IFLNK
12530 case S_IFLNK: t = "link"; break;
12531# endif
12532# ifdef S_IFBLK
12533 case S_IFBLK: t = "bdev"; break;
12534# endif
12535# ifdef S_IFCHR
12536 case S_IFCHR: t = "cdev"; break;
12537# endif
12538# ifdef S_IFIFO
12539 case S_IFIFO: t = "fifo"; break;
12540# endif
12541# ifdef S_IFSOCK
12542 case S_IFSOCK: t = "socket"; break;
12543# endif
12544 default: t = "other";
12545 }
12546# else
12547 if (mch_isdir(fname))
12548 t = "dir";
12549 else
12550 t = "file";
12551# endif
12552#endif
12553 type = vim_strsave((char_u *)t);
12554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012555 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012556}
12557
12558/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012559 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012560 */
12561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012562f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012563{
12564 linenr_T lnum;
12565 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012566 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012567
12568 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012569 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012570 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012571 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012572 retlist = FALSE;
12573 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012574 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012575 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012576 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012577 retlist = TRUE;
12578 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012579
Bram Moolenaar342337a2005-07-21 21:11:17 +000012580 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012581}
12582
12583/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012584 * "getmatches()" function
12585 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012587f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012588{
12589#ifdef FEAT_SEARCH_EXTRA
12590 dict_T *dict;
12591 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012592 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012593
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012594 if (rettv_list_alloc(rettv) == OK)
12595 {
12596 while (cur != NULL)
12597 {
12598 dict = dict_alloc();
12599 if (dict == NULL)
12600 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012601 if (cur->match.regprog == NULL)
12602 {
12603 /* match added with matchaddpos() */
12604 for (i = 0; i < MAXPOSMATCH; ++i)
12605 {
12606 llpos_T *llpos;
12607 char buf[6];
12608 list_T *l;
12609
12610 llpos = &cur->pos.pos[i];
12611 if (llpos->lnum == 0)
12612 break;
12613 l = list_alloc();
12614 if (l == NULL)
12615 break;
12616 list_append_number(l, (varnumber_T)llpos->lnum);
12617 if (llpos->col > 0)
12618 {
12619 list_append_number(l, (varnumber_T)llpos->col);
12620 list_append_number(l, (varnumber_T)llpos->len);
12621 }
12622 sprintf(buf, "pos%d", i + 1);
12623 dict_add_list(dict, buf, l);
12624 }
12625 }
12626 else
12627 {
12628 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12629 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012630 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012631 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12632 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012633# ifdef FEAT_CONCEAL
12634 if (cur->conceal_char)
12635 {
12636 char_u buf[MB_MAXBYTES + 1];
12637
12638 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12639 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12640 }
12641# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012642 list_append_dict(rettv->vval.v_list, dict);
12643 cur = cur->next;
12644 }
12645 }
12646#endif
12647}
12648
12649/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012650 * "getpid()" function
12651 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012653f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012654{
12655 rettv->vval.v_number = mch_get_pid();
12656}
12657
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012658static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012659
12660/*
12661 * "getcurpos()" function
12662 */
12663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012664f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012665{
12666 getpos_both(argvars, rettv, TRUE);
12667}
12668
Bram Moolenaar18081e32008-02-20 19:11:07 +000012669/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012670 * "getpos(string)" function
12671 */
12672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012673f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012674{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012675 getpos_both(argvars, rettv, FALSE);
12676}
12677
12678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012679getpos_both(
12680 typval_T *argvars,
12681 typval_T *rettv,
12682 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012683{
Bram Moolenaara5525202006-03-02 22:52:09 +000012684 pos_T *fp;
12685 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012686 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012687
12688 if (rettv_list_alloc(rettv) == OK)
12689 {
12690 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012691 if (getcurpos)
12692 fp = &curwin->w_cursor;
12693 else
12694 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012695 if (fnum != -1)
12696 list_append_number(l, (varnumber_T)fnum);
12697 else
12698 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012699 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12700 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012701 list_append_number(l, (fp != NULL)
12702 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012703 : (varnumber_T)0);
12704 list_append_number(l,
12705#ifdef FEAT_VIRTUALEDIT
12706 (fp != NULL) ? (varnumber_T)fp->coladd :
12707#endif
12708 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012709 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012710 {
12711 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012712 list_append_number(l, curwin->w_curswant == MAXCOL ?
12713 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012714 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012715 }
12716 else
12717 rettv->vval.v_number = FALSE;
12718}
12719
12720/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012721 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012722 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012724f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012725{
12726#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012727 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012728#endif
12729
Bram Moolenaar2641f772005-03-25 21:58:17 +000012730#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012731 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012732 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012733 wp = NULL;
12734 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12735 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012736 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012737 if (wp == NULL)
12738 return;
12739 }
12740
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012741 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012742 }
12743#endif
12744}
12745
12746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 * "getreg()" function
12748 */
12749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012750f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751{
12752 char_u *strregname;
12753 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012754 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012755 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012758 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012760 strregname = get_tv_string_chk(&argvars[0]);
12761 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012762 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012764 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012765 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12766 return_list = get_tv_number_chk(&argvars[2], &error);
12767 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012770 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012771
12772 if (error)
12773 return;
12774
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 regname = (strregname == NULL ? '"' : *strregname);
12776 if (regname == 0)
12777 regname = '"';
12778
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012779 if (return_list)
12780 {
12781 rettv->v_type = VAR_LIST;
12782 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12783 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012784 if (rettv->vval.v_list != NULL)
12785 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012786 }
12787 else
12788 {
12789 rettv->v_type = VAR_STRING;
12790 rettv->vval.v_string = get_reg_contents(regname,
12791 arg2 ? GREG_EXPR_SRC : 0);
12792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793}
12794
12795/*
12796 * "getregtype()" function
12797 */
12798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012799f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800{
12801 char_u *strregname;
12802 int regname;
12803 char_u buf[NUMBUFLEN + 2];
12804 long reglen = 0;
12805
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012806 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012807 {
12808 strregname = get_tv_string_chk(&argvars[0]);
12809 if (strregname == NULL) /* type error; errmsg already given */
12810 {
12811 rettv->v_type = VAR_STRING;
12812 rettv->vval.v_string = NULL;
12813 return;
12814 }
12815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 else
12817 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012818 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819
12820 regname = (strregname == NULL ? '"' : *strregname);
12821 if (regname == 0)
12822 regname = '"';
12823
12824 buf[0] = NUL;
12825 buf[1] = NUL;
12826 switch (get_reg_type(regname, &reglen))
12827 {
12828 case MLINE: buf[0] = 'V'; break;
12829 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830 case MBLOCK:
12831 buf[0] = Ctrl_V;
12832 sprintf((char *)buf + 1, "%ld", reglen + 1);
12833 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->v_type = VAR_STRING;
12836 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837}
12838
12839/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012840 * "gettabvar()" function
12841 */
12842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012843f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012844{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012845 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012846 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012847 dictitem_T *v;
12848 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012849 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012850
12851 rettv->v_type = VAR_STRING;
12852 rettv->vval.v_string = NULL;
12853
12854 varname = get_tv_string_chk(&argvars[1]);
12855 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12856 if (tp != NULL && varname != NULL)
12857 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012858 /* Set tp to be our tabpage, temporarily. Also set the window to the
12859 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012860 if (switch_win(&oldcurwin, &oldtabpage,
12861 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012862 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012863 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012864 /* look up the variable */
12865 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12866 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12867 if (v != NULL)
12868 {
12869 copy_tv(&v->di_tv, rettv);
12870 done = TRUE;
12871 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012872 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012873
12874 /* restore previous notion of curwin */
12875 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012876 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012877
12878 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012879 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012880}
12881
12882/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012883 * "gettabwinvar()" function
12884 */
12885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012886f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012887{
12888 getwinvar(argvars, rettv, 1);
12889}
12890
12891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 * "getwinposx()" function
12893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012895f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898#ifdef FEAT_GUI
12899 if (gui.in_use)
12900 {
12901 int x, y;
12902
12903 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905 }
12906#endif
12907}
12908
12909/*
12910 * "getwinposy()" function
12911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012913f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916#ifdef FEAT_GUI
12917 if (gui.in_use)
12918 {
12919 int x, y;
12920
12921 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923 }
12924#endif
12925}
12926
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012927/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012928 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012929 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012930 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012931find_win_by_nr(
12932 typval_T *vp,
12933 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012934{
12935#ifdef FEAT_WINDOWS
12936 win_T *wp;
12937#endif
12938 int nr;
12939
12940 nr = get_tv_number_chk(vp, NULL);
12941
12942#ifdef FEAT_WINDOWS
12943 if (nr < 0)
12944 return NULL;
12945 if (nr == 0)
12946 return curwin;
12947
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012948 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12949 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012950 if (--nr <= 0)
12951 break;
12952 return wp;
12953#else
12954 if (nr == 0 || nr == 1)
12955 return curwin;
12956 return NULL;
12957#endif
12958}
12959
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012961 * Find window specified by "wvp" in tabpage "tvp".
12962 */
12963 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012964find_tabwin(
12965 typval_T *wvp, /* VAR_UNKNOWN for current window */
12966 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012967{
12968 win_T *wp = NULL;
12969 tabpage_T *tp = NULL;
12970 long n;
12971
12972 if (wvp->v_type != VAR_UNKNOWN)
12973 {
12974 if (tvp->v_type != VAR_UNKNOWN)
12975 {
12976 n = get_tv_number(tvp);
12977 if (n >= 0)
12978 tp = find_tabpage(n);
12979 }
12980 else
12981 tp = curtab;
12982
12983 if (tp != NULL)
12984 wp = find_win_by_nr(wvp, tp);
12985 }
12986 else
12987 wp = curwin;
12988
12989 return wp;
12990}
12991
12992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993 * "getwinvar()" function
12994 */
12995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012996f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012998 getwinvar(argvars, rettv, 0);
12999}
13000
13001/*
13002 * getwinvar() and gettabwinvar()
13003 */
13004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013005getwinvar(
13006 typval_T *argvars,
13007 typval_T *rettv,
13008 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013009{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013010 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013012 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013013 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013014 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013015#ifdef FEAT_WINDOWS
13016 win_T *oldcurwin;
13017 tabpage_T *oldtabpage;
13018 int need_switch_win;
13019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013021#ifdef FEAT_WINDOWS
13022 if (off == 1)
13023 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13024 else
13025 tp = curtab;
13026#endif
13027 win = find_win_by_nr(&argvars[off], tp);
13028 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013029 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013031 rettv->v_type = VAR_STRING;
13032 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033
13034 if (win != NULL && varname != NULL)
13035 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013036#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013037 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013038 * otherwise the window is not valid. Only do this when needed,
13039 * autocommands get blocked. */
13040 need_switch_win = !(tp == curtab && win == curwin);
13041 if (!need_switch_win
13042 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13043#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013044 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013045 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013046 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013047 if (get_option_tv(&varname, rettv, 1) == OK)
13048 done = TRUE;
13049 }
13050 else
13051 {
13052 /* Look up the variable. */
13053 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13054 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13055 varname, FALSE);
13056 if (v != NULL)
13057 {
13058 copy_tv(&v->di_tv, rettv);
13059 done = TRUE;
13060 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013063
Bram Moolenaarba117c22015-09-29 16:53:22 +020013064#ifdef FEAT_WINDOWS
13065 if (need_switch_win)
13066 /* restore previous notion of curwin */
13067 restore_win(oldcurwin, oldtabpage, TRUE);
13068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069 }
13070
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013071 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13072 /* use the default return value */
13073 copy_tv(&argvars[off + 2], rettv);
13074
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 --emsg_off;
13076}
13077
13078/*
13079 * "glob()" function
13080 */
13081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013082f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013084 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013086 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013088 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013089 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013090 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013091 if (argvars[1].v_type != VAR_UNKNOWN)
13092 {
13093 if (get_tv_number_chk(&argvars[1], &error))
13094 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013095 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013096 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013097 if (get_tv_number_chk(&argvars[2], &error))
13098 {
13099 rettv->v_type = VAR_LIST;
13100 rettv->vval.v_list = NULL;
13101 }
13102 if (argvars[3].v_type != VAR_UNKNOWN
13103 && get_tv_number_chk(&argvars[3], &error))
13104 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013105 }
13106 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013107 if (!error)
13108 {
13109 ExpandInit(&xpc);
13110 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013111 if (p_wic)
13112 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013113 if (rettv->v_type == VAR_STRING)
13114 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013115 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013116 else if (rettv_list_alloc(rettv) != FAIL)
13117 {
13118 int i;
13119
13120 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13121 NULL, options, WILD_ALL_KEEP);
13122 for (i = 0; i < xpc.xp_numfiles; i++)
13123 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13124
13125 ExpandCleanup(&xpc);
13126 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013127 }
13128 else
13129 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130}
13131
13132/*
13133 * "globpath()" function
13134 */
13135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013136f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013138 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013140 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013141 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013142 garray_T ga;
13143 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013145 /* When the optional second argument is non-zero, don't remove matches
13146 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013148 if (argvars[2].v_type != VAR_UNKNOWN)
13149 {
13150 if (get_tv_number_chk(&argvars[2], &error))
13151 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013152 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013153 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013154 if (get_tv_number_chk(&argvars[3], &error))
13155 {
13156 rettv->v_type = VAR_LIST;
13157 rettv->vval.v_list = NULL;
13158 }
13159 if (argvars[4].v_type != VAR_UNKNOWN
13160 && get_tv_number_chk(&argvars[4], &error))
13161 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013162 }
13163 }
13164 if (file != NULL && !error)
13165 {
13166 ga_init2(&ga, (int)sizeof(char_u *), 10);
13167 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13168 if (rettv->v_type == VAR_STRING)
13169 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13170 else if (rettv_list_alloc(rettv) != FAIL)
13171 for (i = 0; i < ga.ga_len; ++i)
13172 list_append_string(rettv->vval.v_list,
13173 ((char_u **)(ga.ga_data))[i], -1);
13174 ga_clear_strings(&ga);
13175 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013176 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013177 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178}
13179
13180/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013181 * "glob2regpat()" function
13182 */
13183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013184f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013185{
13186 char_u *pat = get_tv_string_chk(&argvars[0]);
13187
13188 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013189 rettv->vval.v_string = (pat == NULL)
13190 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013191}
13192
13193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 * "has()" function
13195 */
13196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013197f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198{
13199 int i;
13200 char_u *name;
13201 int n = FALSE;
13202 static char *(has_list[]) =
13203 {
13204#ifdef AMIGA
13205 "amiga",
13206# ifdef FEAT_ARP
13207 "arp",
13208# endif
13209#endif
13210#ifdef __BEOS__
13211 "beos",
13212#endif
13213#ifdef MSDOS
13214# ifdef DJGPP
13215 "dos32",
13216# else
13217 "dos16",
13218# endif
13219#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013220#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 "mac",
13222#endif
13223#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013224 "macunix", /* built with 'darwin' enabled */
13225#endif
13226#if defined(__APPLE__) && __APPLE__ == 1
13227 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229#ifdef __QNX__
13230 "qnx",
13231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232#ifdef UNIX
13233 "unix",
13234#endif
13235#ifdef VMS
13236 "vms",
13237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238#ifdef WIN32
13239 "win32",
13240#endif
13241#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13242 "win32unix",
13243#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013244#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 "win64",
13246#endif
13247#ifdef EBCDIC
13248 "ebcdic",
13249#endif
13250#ifndef CASE_INSENSITIVE_FILENAME
13251 "fname_case",
13252#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013253#ifdef HAVE_ACL
13254 "acl",
13255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256#ifdef FEAT_ARABIC
13257 "arabic",
13258#endif
13259#ifdef FEAT_AUTOCMD
13260 "autocmd",
13261#endif
13262#ifdef FEAT_BEVAL
13263 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013264# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13265 "balloon_multiline",
13266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267#endif
13268#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13269 "builtin_terms",
13270# ifdef ALL_BUILTIN_TCAPS
13271 "all_builtin_terms",
13272# endif
13273#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013274#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13275 || defined(FEAT_GUI_W32) \
13276 || defined(FEAT_GUI_MOTIF))
13277 "browsefilter",
13278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279#ifdef FEAT_BYTEOFF
13280 "byte_offset",
13281#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013282#ifdef FEAT_CHANNEL
13283 "channel",
13284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285#ifdef FEAT_CINDENT
13286 "cindent",
13287#endif
13288#ifdef FEAT_CLIENTSERVER
13289 "clientserver",
13290#endif
13291#ifdef FEAT_CLIPBOARD
13292 "clipboard",
13293#endif
13294#ifdef FEAT_CMDL_COMPL
13295 "cmdline_compl",
13296#endif
13297#ifdef FEAT_CMDHIST
13298 "cmdline_hist",
13299#endif
13300#ifdef FEAT_COMMENTS
13301 "comments",
13302#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013303#ifdef FEAT_CONCEAL
13304 "conceal",
13305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306#ifdef FEAT_CRYPT
13307 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013308 "crypt-blowfish",
13309 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310#endif
13311#ifdef FEAT_CSCOPE
13312 "cscope",
13313#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013314#ifdef FEAT_CURSORBIND
13315 "cursorbind",
13316#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013317#ifdef CURSOR_SHAPE
13318 "cursorshape",
13319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320#ifdef DEBUG
13321 "debug",
13322#endif
13323#ifdef FEAT_CON_DIALOG
13324 "dialog_con",
13325#endif
13326#ifdef FEAT_GUI_DIALOG
13327 "dialog_gui",
13328#endif
13329#ifdef FEAT_DIFF
13330 "diff",
13331#endif
13332#ifdef FEAT_DIGRAPHS
13333 "digraphs",
13334#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013335#ifdef FEAT_DIRECTX
13336 "directx",
13337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338#ifdef FEAT_DND
13339 "dnd",
13340#endif
13341#ifdef FEAT_EMACS_TAGS
13342 "emacs_tags",
13343#endif
13344 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013345 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346#ifdef FEAT_SEARCH_EXTRA
13347 "extra_search",
13348#endif
13349#ifdef FEAT_FKMAP
13350 "farsi",
13351#endif
13352#ifdef FEAT_SEARCHPATH
13353 "file_in_path",
13354#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013355#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013356 "filterpipe",
13357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358#ifdef FEAT_FIND_ID
13359 "find_in_path",
13360#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013361#ifdef FEAT_FLOAT
13362 "float",
13363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364#ifdef FEAT_FOLDING
13365 "folding",
13366#endif
13367#ifdef FEAT_FOOTER
13368 "footer",
13369#endif
13370#if !defined(USE_SYSTEM) && defined(UNIX)
13371 "fork",
13372#endif
13373#ifdef FEAT_GETTEXT
13374 "gettext",
13375#endif
13376#ifdef FEAT_GUI
13377 "gui",
13378#endif
13379#ifdef FEAT_GUI_ATHENA
13380# ifdef FEAT_GUI_NEXTAW
13381 "gui_neXtaw",
13382# else
13383 "gui_athena",
13384# endif
13385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386#ifdef FEAT_GUI_GTK
13387 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013390#ifdef FEAT_GUI_GNOME
13391 "gui_gnome",
13392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393#ifdef FEAT_GUI_MAC
13394 "gui_mac",
13395#endif
13396#ifdef FEAT_GUI_MOTIF
13397 "gui_motif",
13398#endif
13399#ifdef FEAT_GUI_PHOTON
13400 "gui_photon",
13401#endif
13402#ifdef FEAT_GUI_W16
13403 "gui_win16",
13404#endif
13405#ifdef FEAT_GUI_W32
13406 "gui_win32",
13407#endif
13408#ifdef FEAT_HANGULIN
13409 "hangul_input",
13410#endif
13411#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13412 "iconv",
13413#endif
13414#ifdef FEAT_INS_EXPAND
13415 "insert_expand",
13416#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013417#ifdef FEAT_JOB
13418 "job",
13419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420#ifdef FEAT_JUMPLIST
13421 "jumplist",
13422#endif
13423#ifdef FEAT_KEYMAP
13424 "keymap",
13425#endif
13426#ifdef FEAT_LANGMAP
13427 "langmap",
13428#endif
13429#ifdef FEAT_LIBCALL
13430 "libcall",
13431#endif
13432#ifdef FEAT_LINEBREAK
13433 "linebreak",
13434#endif
13435#ifdef FEAT_LISP
13436 "lispindent",
13437#endif
13438#ifdef FEAT_LISTCMDS
13439 "listcmds",
13440#endif
13441#ifdef FEAT_LOCALMAP
13442 "localmap",
13443#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013444#ifdef FEAT_LUA
13445# ifndef DYNAMIC_LUA
13446 "lua",
13447# endif
13448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449#ifdef FEAT_MENU
13450 "menu",
13451#endif
13452#ifdef FEAT_SESSION
13453 "mksession",
13454#endif
13455#ifdef FEAT_MODIFY_FNAME
13456 "modify_fname",
13457#endif
13458#ifdef FEAT_MOUSE
13459 "mouse",
13460#endif
13461#ifdef FEAT_MOUSESHAPE
13462 "mouseshape",
13463#endif
13464#if defined(UNIX) || defined(VMS)
13465# ifdef FEAT_MOUSE_DEC
13466 "mouse_dec",
13467# endif
13468# ifdef FEAT_MOUSE_GPM
13469 "mouse_gpm",
13470# endif
13471# ifdef FEAT_MOUSE_JSB
13472 "mouse_jsbterm",
13473# endif
13474# ifdef FEAT_MOUSE_NET
13475 "mouse_netterm",
13476# endif
13477# ifdef FEAT_MOUSE_PTERM
13478 "mouse_pterm",
13479# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013480# ifdef FEAT_MOUSE_SGR
13481 "mouse_sgr",
13482# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013483# ifdef FEAT_SYSMOUSE
13484 "mouse_sysmouse",
13485# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013486# ifdef FEAT_MOUSE_URXVT
13487 "mouse_urxvt",
13488# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489# ifdef FEAT_MOUSE_XTERM
13490 "mouse_xterm",
13491# endif
13492#endif
13493#ifdef FEAT_MBYTE
13494 "multi_byte",
13495#endif
13496#ifdef FEAT_MBYTE_IME
13497 "multi_byte_ime",
13498#endif
13499#ifdef FEAT_MULTI_LANG
13500 "multi_lang",
13501#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013502#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013503#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013504 "mzscheme",
13505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507#ifdef FEAT_OLE
13508 "ole",
13509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510#ifdef FEAT_PATH_EXTRA
13511 "path_extra",
13512#endif
13513#ifdef FEAT_PERL
13514#ifndef DYNAMIC_PERL
13515 "perl",
13516#endif
13517#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013518#ifdef FEAT_PERSISTENT_UNDO
13519 "persistent_undo",
13520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521#ifdef FEAT_PYTHON
13522#ifndef DYNAMIC_PYTHON
13523 "python",
13524#endif
13525#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013526#ifdef FEAT_PYTHON3
13527#ifndef DYNAMIC_PYTHON3
13528 "python3",
13529#endif
13530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531#ifdef FEAT_POSTSCRIPT
13532 "postscript",
13533#endif
13534#ifdef FEAT_PRINTER
13535 "printer",
13536#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013537#ifdef FEAT_PROFILE
13538 "profile",
13539#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013540#ifdef FEAT_RELTIME
13541 "reltime",
13542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543#ifdef FEAT_QUICKFIX
13544 "quickfix",
13545#endif
13546#ifdef FEAT_RIGHTLEFT
13547 "rightleft",
13548#endif
13549#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13550 "ruby",
13551#endif
13552#ifdef FEAT_SCROLLBIND
13553 "scrollbind",
13554#endif
13555#ifdef FEAT_CMDL_INFO
13556 "showcmd",
13557 "cmdline_info",
13558#endif
13559#ifdef FEAT_SIGNS
13560 "signs",
13561#endif
13562#ifdef FEAT_SMARTINDENT
13563 "smartindent",
13564#endif
13565#ifdef FEAT_SNIFF
13566 "sniff",
13567#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013568#ifdef STARTUPTIME
13569 "startuptime",
13570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571#ifdef FEAT_STL_OPT
13572 "statusline",
13573#endif
13574#ifdef FEAT_SUN_WORKSHOP
13575 "sun_workshop",
13576#endif
13577#ifdef FEAT_NETBEANS_INTG
13578 "netbeans_intg",
13579#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013580#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013581 "spell",
13582#endif
13583#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 "syntax",
13585#endif
13586#if defined(USE_SYSTEM) || !defined(UNIX)
13587 "system",
13588#endif
13589#ifdef FEAT_TAG_BINS
13590 "tag_binary",
13591#endif
13592#ifdef FEAT_TAG_OLDSTATIC
13593 "tag_old_static",
13594#endif
13595#ifdef FEAT_TAG_ANYWHITE
13596 "tag_any_white",
13597#endif
13598#ifdef FEAT_TCL
13599# ifndef DYNAMIC_TCL
13600 "tcl",
13601# endif
13602#endif
13603#ifdef TERMINFO
13604 "terminfo",
13605#endif
13606#ifdef FEAT_TERMRESPONSE
13607 "termresponse",
13608#endif
13609#ifdef FEAT_TEXTOBJ
13610 "textobjects",
13611#endif
13612#ifdef HAVE_TGETENT
13613 "tgetent",
13614#endif
13615#ifdef FEAT_TITLE
13616 "title",
13617#endif
13618#ifdef FEAT_TOOLBAR
13619 "toolbar",
13620#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013621#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13622 "unnamedplus",
13623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624#ifdef FEAT_USR_CMDS
13625 "user-commands", /* was accidentally included in 5.4 */
13626 "user_commands",
13627#endif
13628#ifdef FEAT_VIMINFO
13629 "viminfo",
13630#endif
13631#ifdef FEAT_VERTSPLIT
13632 "vertsplit",
13633#endif
13634#ifdef FEAT_VIRTUALEDIT
13635 "virtualedit",
13636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638#ifdef FEAT_VISUALEXTRA
13639 "visualextra",
13640#endif
13641#ifdef FEAT_VREPLACE
13642 "vreplace",
13643#endif
13644#ifdef FEAT_WILDIGN
13645 "wildignore",
13646#endif
13647#ifdef FEAT_WILDMENU
13648 "wildmenu",
13649#endif
13650#ifdef FEAT_WINDOWS
13651 "windows",
13652#endif
13653#ifdef FEAT_WAK
13654 "winaltkeys",
13655#endif
13656#ifdef FEAT_WRITEBACKUP
13657 "writebackup",
13658#endif
13659#ifdef FEAT_XIM
13660 "xim",
13661#endif
13662#ifdef FEAT_XFONTSET
13663 "xfontset",
13664#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013665#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013666 "xpm",
13667 "xpm_w32", /* for backward compatibility */
13668#else
13669# if defined(HAVE_XPM)
13670 "xpm",
13671# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673#ifdef USE_XSMP
13674 "xsmp",
13675#endif
13676#ifdef USE_XSMP_INTERACT
13677 "xsmp_interact",
13678#endif
13679#ifdef FEAT_XCLIPBOARD
13680 "xterm_clipboard",
13681#endif
13682#ifdef FEAT_XTERM_SAVE
13683 "xterm_save",
13684#endif
13685#if defined(UNIX) && defined(FEAT_X11)
13686 "X11",
13687#endif
13688 NULL
13689 };
13690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013691 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 for (i = 0; has_list[i] != NULL; ++i)
13693 if (STRICMP(name, has_list[i]) == 0)
13694 {
13695 n = TRUE;
13696 break;
13697 }
13698
13699 if (n == FALSE)
13700 {
13701 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013702 {
13703 if (name[5] == '-'
13704 && STRLEN(name) > 11
13705 && vim_isdigit(name[6])
13706 && vim_isdigit(name[8])
13707 && vim_isdigit(name[10]))
13708 {
13709 int major = atoi((char *)name + 6);
13710 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013711
13712 /* Expect "patch-9.9.01234". */
13713 n = (major < VIM_VERSION_MAJOR
13714 || (major == VIM_VERSION_MAJOR
13715 && (minor < VIM_VERSION_MINOR
13716 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013717 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013718 }
13719 else
13720 n = has_patch(atoi((char *)name + 5));
13721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722 else if (STRICMP(name, "vim_starting") == 0)
13723 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013724#ifdef FEAT_MBYTE
13725 else if (STRICMP(name, "multi_byte_encoding") == 0)
13726 n = has_mbyte;
13727#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013728#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13729 else if (STRICMP(name, "balloon_multiline") == 0)
13730 n = multiline_balloon_available();
13731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732#ifdef DYNAMIC_TCL
13733 else if (STRICMP(name, "tcl") == 0)
13734 n = tcl_enabled(FALSE);
13735#endif
13736#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13737 else if (STRICMP(name, "iconv") == 0)
13738 n = iconv_enabled(FALSE);
13739#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013740#ifdef DYNAMIC_LUA
13741 else if (STRICMP(name, "lua") == 0)
13742 n = lua_enabled(FALSE);
13743#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013744#ifdef DYNAMIC_MZSCHEME
13745 else if (STRICMP(name, "mzscheme") == 0)
13746 n = mzscheme_enabled(FALSE);
13747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748#ifdef DYNAMIC_RUBY
13749 else if (STRICMP(name, "ruby") == 0)
13750 n = ruby_enabled(FALSE);
13751#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013752#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753#ifdef DYNAMIC_PYTHON
13754 else if (STRICMP(name, "python") == 0)
13755 n = python_enabled(FALSE);
13756#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013757#endif
13758#ifdef FEAT_PYTHON3
13759#ifdef DYNAMIC_PYTHON3
13760 else if (STRICMP(name, "python3") == 0)
13761 n = python3_enabled(FALSE);
13762#endif
13763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764#ifdef DYNAMIC_PERL
13765 else if (STRICMP(name, "perl") == 0)
13766 n = perl_enabled(FALSE);
13767#endif
13768#ifdef FEAT_GUI
13769 else if (STRICMP(name, "gui_running") == 0)
13770 n = (gui.in_use || gui.starting);
13771# ifdef FEAT_GUI_W32
13772 else if (STRICMP(name, "gui_win32s") == 0)
13773 n = gui_is_win32s();
13774# endif
13775# ifdef FEAT_BROWSE
13776 else if (STRICMP(name, "browse") == 0)
13777 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13778# endif
13779#endif
13780#ifdef FEAT_SYN_HL
13781 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013782 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783#endif
13784#if defined(WIN3264)
13785 else if (STRICMP(name, "win95") == 0)
13786 n = mch_windows95();
13787#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013788#ifdef FEAT_NETBEANS_INTG
13789 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013790 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 }
13793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795}
13796
13797/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013798 * "has_key()" function
13799 */
13800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013801f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013802{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013803 if (argvars[0].v_type != VAR_DICT)
13804 {
13805 EMSG(_(e_dictreq));
13806 return;
13807 }
13808 if (argvars[0].vval.v_dict == NULL)
13809 return;
13810
13811 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013812 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013813}
13814
13815/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013816 * "haslocaldir()" function
13817 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013819f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013820{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013821 win_T *wp = NULL;
13822
13823 wp = find_tabwin(&argvars[0], &argvars[1]);
13824 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013825}
13826
13827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 * "hasmapto()" function
13829 */
13830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013831f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832{
13833 char_u *name;
13834 char_u *mode;
13835 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013836 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013838 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013839 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 mode = (char_u *)"nvo";
13841 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013843 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013844 if (argvars[2].v_type != VAR_UNKNOWN)
13845 abbr = get_tv_number(&argvars[2]);
13846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847
Bram Moolenaar2c932302006-03-18 21:42:09 +000013848 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013849 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013851 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852}
13853
13854/*
13855 * "histadd()" function
13856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013858f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859{
13860#ifdef FEAT_CMDHIST
13861 int histype;
13862 char_u *str;
13863 char_u buf[NUMBUFLEN];
13864#endif
13865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013866 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 if (check_restricted() || check_secure())
13868 return;
13869#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013870 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13871 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 if (histype >= 0)
13873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013874 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 if (*str != NUL)
13876 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013877 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013879 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 return;
13881 }
13882 }
13883#endif
13884}
13885
13886/*
13887 * "histdel()" function
13888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013890f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891{
13892#ifdef FEAT_CMDHIST
13893 int n;
13894 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013895 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13898 if (str == NULL)
13899 n = 0;
13900 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013902 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013903 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013905 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013906 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 else
13908 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013909 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013910 get_tv_string_buf(&argvars[1], buf));
13911 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912#endif
13913}
13914
13915/*
13916 * "histget()" function
13917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013919f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920{
13921#ifdef FEAT_CMDHIST
13922 int type;
13923 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013924 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013926 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13927 if (str == NULL)
13928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013930 {
13931 type = get_histtype(str);
13932 if (argvars[1].v_type == VAR_UNKNOWN)
13933 idx = get_history_idx(type);
13934 else
13935 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13936 /* -1 on type error */
13937 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013942 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943}
13944
13945/*
13946 * "histnr()" function
13947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013949f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950{
13951 int i;
13952
13953#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013954 char_u *history = get_tv_string_chk(&argvars[0]);
13955
13956 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 if (i >= HIST_CMD && i < HIST_COUNT)
13958 i = get_history_idx(i);
13959 else
13960#endif
13961 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013962 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963}
13964
13965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 * "highlightID(name)" function
13967 */
13968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013969f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013971 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972}
13973
13974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013975 * "highlight_exists()" function
13976 */
13977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013978f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013979{
13980 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13981}
13982
13983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 * "hostname()" function
13985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013987f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988{
13989 char_u hostname[256];
13990
13991 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013992 rettv->v_type = VAR_STRING;
13993 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994}
13995
13996/*
13997 * iconv() function
13998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014000f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001{
14002#ifdef FEAT_MBYTE
14003 char_u buf1[NUMBUFLEN];
14004 char_u buf2[NUMBUFLEN];
14005 char_u *from, *to, *str;
14006 vimconv_T vimconv;
14007#endif
14008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014009 rettv->v_type = VAR_STRING;
14010 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011
14012#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014013 str = get_tv_string(&argvars[0]);
14014 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14015 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 vimconv.vc_type = CONV_NONE;
14017 convert_setup(&vimconv, from, to);
14018
14019 /* If the encodings are equal, no conversion needed. */
14020 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014021 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024
14025 convert_setup(&vimconv, NULL, NULL);
14026 vim_free(from);
14027 vim_free(to);
14028#endif
14029}
14030
14031/*
14032 * "indent()" function
14033 */
14034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014035f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036{
14037 linenr_T lnum;
14038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014039 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044}
14045
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014046/*
14047 * "index()" function
14048 */
14049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014050f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014051{
Bram Moolenaar33570922005-01-25 22:26:29 +000014052 list_T *l;
14053 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014054 long idx = 0;
14055 int ic = FALSE;
14056
14057 rettv->vval.v_number = -1;
14058 if (argvars[0].v_type != VAR_LIST)
14059 {
14060 EMSG(_(e_listreq));
14061 return;
14062 }
14063 l = argvars[0].vval.v_list;
14064 if (l != NULL)
14065 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014066 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014067 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014069 int error = FALSE;
14070
Bram Moolenaar758711c2005-02-02 23:11:38 +000014071 /* Start at specified item. Use the cached index that list_find()
14072 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014073 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014074 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014075 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014076 ic = get_tv_number_chk(&argvars[3], &error);
14077 if (error)
14078 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014079 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014080
Bram Moolenaar758711c2005-02-02 23:11:38 +000014081 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014082 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014083 {
14084 rettv->vval.v_number = idx;
14085 break;
14086 }
14087 }
14088}
14089
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090static int inputsecret_flag = 0;
14091
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014092static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014093
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014095 * This function is used by f_input() and f_inputdialog() functions. The third
14096 * argument to f_input() specifies the type of completion to use at the
14097 * prompt. The third argument to f_inputdialog() specifies the value to return
14098 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 */
14100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014101get_user_input(
14102 typval_T *argvars,
14103 typval_T *rettv,
14104 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014106 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 char_u *p = NULL;
14108 int c;
14109 char_u buf[NUMBUFLEN];
14110 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014111 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014112 int xp_type = EXPAND_NOTHING;
14113 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014115 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014116 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117
14118#ifdef NO_CONSOLE_INPUT
14119 /* While starting up, there is no place to enter text. */
14120 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122#endif
14123
14124 cmd_silent = FALSE; /* Want to see the prompt. */
14125 if (prompt != NULL)
14126 {
14127 /* Only the part of the message after the last NL is considered as
14128 * prompt for the command line */
14129 p = vim_strrchr(prompt, '\n');
14130 if (p == NULL)
14131 p = prompt;
14132 else
14133 {
14134 ++p;
14135 c = *p;
14136 *p = NUL;
14137 msg_start();
14138 msg_clr_eos();
14139 msg_puts_attr(prompt, echo_attr);
14140 msg_didout = FALSE;
14141 msg_starthere();
14142 *p = c;
14143 }
14144 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014146 if (argvars[1].v_type != VAR_UNKNOWN)
14147 {
14148 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14149 if (defstr != NULL)
14150 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014152 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014153 {
14154 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014155 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014156 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014157
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014158 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014159 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014160
Bram Moolenaar4463f292005-09-25 22:20:24 +000014161 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14162 if (xp_name == NULL)
14163 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014164
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014165 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014166
Bram Moolenaar4463f292005-09-25 22:20:24 +000014167 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14168 &xp_arg) == FAIL)
14169 return;
14170 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014171 }
14172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014174 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014175 int save_ex_normal_busy = ex_normal_busy;
14176 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014177 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014178 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14179 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014180 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014181 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014182 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014183 && argvars[1].v_type != VAR_UNKNOWN
14184 && argvars[2].v_type != VAR_UNKNOWN)
14185 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14186 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014187
14188 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190 /* since the user typed this, no need to wait for return */
14191 need_wait_return = FALSE;
14192 msg_didout = FALSE;
14193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 cmd_silent = cmd_silent_save;
14195}
14196
14197/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014198 * "input()" function
14199 * Also handles inputsecret() when inputsecret is set.
14200 */
14201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014202f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014203{
14204 get_user_input(argvars, rettv, FALSE);
14205}
14206
14207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 * "inputdialog()" function
14209 */
14210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014211f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212{
14213#if defined(FEAT_GUI_TEXTDIALOG)
14214 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14215 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14216 {
14217 char_u *message;
14218 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014219 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014221 message = get_tv_string_chk(&argvars[0]);
14222 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014223 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014224 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 else
14226 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227 if (message != NULL && defstr != NULL
14228 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014229 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014230 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231 else
14232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 if (message != NULL && defstr != NULL
14234 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014235 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014236 rettv->vval.v_string = vim_strsave(
14237 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 }
14243 else
14244#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014245 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246}
14247
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014248/*
14249 * "inputlist()" function
14250 */
14251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014252f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014253{
14254 listitem_T *li;
14255 int selected;
14256 int mouse_used;
14257
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014258#ifdef NO_CONSOLE_INPUT
14259 /* While starting up, there is no place to enter text. */
14260 if (no_console_input())
14261 return;
14262#endif
14263 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14264 {
14265 EMSG2(_(e_listarg), "inputlist()");
14266 return;
14267 }
14268
14269 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014270 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014271 lines_left = Rows; /* avoid more prompt */
14272 msg_scroll = TRUE;
14273 msg_clr_eos();
14274
14275 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14276 {
14277 msg_puts(get_tv_string(&li->li_tv));
14278 msg_putchar('\n');
14279 }
14280
14281 /* Ask for choice. */
14282 selected = prompt_for_number(&mouse_used);
14283 if (mouse_used)
14284 selected -= lines_left;
14285
14286 rettv->vval.v_number = selected;
14287}
14288
14289
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14291
14292/*
14293 * "inputrestore()" function
14294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014296f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297{
14298 if (ga_userinput.ga_len > 0)
14299 {
14300 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14302 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014303 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 }
14305 else if (p_verbose > 1)
14306 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014307 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014308 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 }
14310}
14311
14312/*
14313 * "inputsave()" function
14314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014316f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014318 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 if (ga_grow(&ga_userinput, 1) == OK)
14320 {
14321 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14322 + ga_userinput.ga_len);
14323 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014324 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325 }
14326 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328}
14329
14330/*
14331 * "inputsecret()" function
14332 */
14333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014334f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335{
14336 ++cmdline_star;
14337 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014338 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 --cmdline_star;
14340 --inputsecret_flag;
14341}
14342
14343/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014344 * "insert()" function
14345 */
14346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014347f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014348{
14349 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014350 listitem_T *item;
14351 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014353
14354 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014355 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014356 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014357 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014358 {
14359 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 before = get_tv_number_chk(&argvars[2], &error);
14361 if (error)
14362 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014363
Bram Moolenaar758711c2005-02-02 23:11:38 +000014364 if (before == l->lv_len)
14365 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014366 else
14367 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014368 item = list_find(l, before);
14369 if (item == NULL)
14370 {
14371 EMSGN(_(e_listidx), before);
14372 l = NULL;
14373 }
14374 }
14375 if (l != NULL)
14376 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014377 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014378 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014379 }
14380 }
14381}
14382
14383/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014384 * "invert(expr)" function
14385 */
14386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014387f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014388{
14389 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14390}
14391
14392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 * "isdirectory()" function
14394 */
14395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014396f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014398 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399}
14400
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014401/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014402 * "islocked()" function
14403 */
14404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014405f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014406{
14407 lval_T lv;
14408 char_u *end;
14409 dictitem_T *di;
14410
14411 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014412 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14413 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014414 if (end != NULL && lv.ll_name != NULL)
14415 {
14416 if (*end != NUL)
14417 EMSG(_(e_trailing));
14418 else
14419 {
14420 if (lv.ll_tv == NULL)
14421 {
14422 if (check_changedtick(lv.ll_name))
14423 rettv->vval.v_number = 1; /* always locked */
14424 else
14425 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014426 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014427 if (di != NULL)
14428 {
14429 /* Consider a variable locked when:
14430 * 1. the variable itself is locked
14431 * 2. the value of the variable is locked.
14432 * 3. the List or Dict value is locked.
14433 */
14434 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14435 || tv_islocked(&di->di_tv));
14436 }
14437 }
14438 }
14439 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014440 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014441 else if (lv.ll_newkey != NULL)
14442 EMSG2(_(e_dictkey), lv.ll_newkey);
14443 else if (lv.ll_list != NULL)
14444 /* List item. */
14445 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14446 else
14447 /* Dictionary item. */
14448 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14449 }
14450 }
14451
14452 clear_lval(&lv);
14453}
14454
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014455static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014456
14457/*
14458 * Turn a dict into a list:
14459 * "what" == 0: list of keys
14460 * "what" == 1: list of values
14461 * "what" == 2: list of items
14462 */
14463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014464dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014465{
Bram Moolenaar33570922005-01-25 22:26:29 +000014466 list_T *l2;
14467 dictitem_T *di;
14468 hashitem_T *hi;
14469 listitem_T *li;
14470 listitem_T *li2;
14471 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014472 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014473
Bram Moolenaar8c711452005-01-14 21:53:12 +000014474 if (argvars[0].v_type != VAR_DICT)
14475 {
14476 EMSG(_(e_dictreq));
14477 return;
14478 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014479 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014480 return;
14481
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014482 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014483 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014484
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014485 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014486 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014487 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014488 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014489 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014490 --todo;
14491 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014492
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014493 li = listitem_alloc();
14494 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014495 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014496 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014497
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014498 if (what == 0)
14499 {
14500 /* keys() */
14501 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014502 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014503 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14504 }
14505 else if (what == 1)
14506 {
14507 /* values() */
14508 copy_tv(&di->di_tv, &li->li_tv);
14509 }
14510 else
14511 {
14512 /* items() */
14513 l2 = list_alloc();
14514 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014515 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014516 li->li_tv.vval.v_list = l2;
14517 if (l2 == NULL)
14518 break;
14519 ++l2->lv_refcount;
14520
14521 li2 = listitem_alloc();
14522 if (li2 == NULL)
14523 break;
14524 list_append(l2, li2);
14525 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014526 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014527 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14528
14529 li2 = listitem_alloc();
14530 if (li2 == NULL)
14531 break;
14532 list_append(l2, li2);
14533 copy_tv(&di->di_tv, &li2->li_tv);
14534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014535 }
14536 }
14537}
14538
14539/*
14540 * "items(dict)" function
14541 */
14542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014543f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014544{
14545 dict_list(argvars, rettv, 2);
14546}
14547
Bram Moolenaar835dc632016-02-07 14:27:38 +010014548#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014549
14550# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014551/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014552 * "job_getchannel()" function
14553 */
14554 static void
14555f_job_getchannel(typval_T *argvars, typval_T *rettv)
14556{
14557 if (argvars[0].v_type != VAR_JOB)
14558 EMSG(_(e_invarg));
14559 else
14560 {
14561 job_T *job = argvars[0].vval.v_job;
14562
Bram Moolenaar77073442016-02-13 23:23:53 +010014563 rettv->v_type = VAR_CHANNEL;
14564 rettv->vval.v_channel = job->jv_channel;
14565 if (job->jv_channel != NULL)
14566 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014567 }
14568}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014569# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014570
14571/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014572 * "job_start()" function
14573 */
14574 static void
14575f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14576{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014577 job_T *job;
14578 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014579#if defined(UNIX)
14580# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014581 char **argv = NULL;
14582 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014583#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014584 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014585#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014586 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014587
14588 rettv->v_type = VAR_JOB;
14589 job = job_alloc();
14590 rettv->vval.v_job = job;
14591 if (job == NULL)
14592 return;
14593
14594 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014595
14596 /* Default mode is NL. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014597 opt.jo_mode = MODE_NL;
14598 opt.jo_callback = NULL;
14599 if (get_job_options(&argvars[1], &opt, JO_MODE + JO_CALLBACK) == FAIL)
14600 return;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014601
Bram Moolenaar835dc632016-02-07 14:27:38 +010014602#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014603 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014604#endif
14605
14606 if (argvars[0].v_type == VAR_STRING)
14607 {
14608 /* Command is a string. */
14609 cmd = argvars[0].vval.v_string;
14610#ifdef USE_ARGV
14611 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14612 return;
14613 argv[argc] = NULL;
14614#endif
14615 }
14616 else if (argvars[0].v_type != VAR_LIST
14617 || argvars[0].vval.v_list == NULL
14618 || argvars[0].vval.v_list->lv_len < 1)
14619 {
14620 EMSG(_(e_invarg));
14621 return;
14622 }
14623 else
14624 {
14625 list_T *l = argvars[0].vval.v_list;
14626 listitem_T *li;
14627 char_u *s;
14628
14629#ifdef USE_ARGV
14630 /* Pass argv[] to mch_call_shell(). */
14631 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14632 if (argv == NULL)
14633 return;
14634#endif
14635 for (li = l->lv_first; li != NULL; li = li->li_next)
14636 {
14637 s = get_tv_string_chk(&li->li_tv);
14638 if (s == NULL)
14639 goto theend;
14640#ifdef USE_ARGV
14641 argv[argc++] = (char *)s;
14642#else
14643 if (li != l->lv_first)
14644 {
14645 s = vim_strsave_shellescape(s, FALSE, TRUE);
14646 if (s == NULL)
14647 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014648 ga_concat(&ga, s);
14649 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014650 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014651 else
14652 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014653 if (li->li_next != NULL)
14654 ga_append(&ga, ' ');
14655#endif
14656 }
14657#ifdef USE_ARGV
14658 argv[argc] = NULL;
14659#else
14660 cmd = ga.ga_data;
14661#endif
14662 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014663
Bram Moolenaar835dc632016-02-07 14:27:38 +010014664#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014665# ifdef FEAT_CHANNEL
14666 if (ch_log_active())
14667 {
14668 garray_T ga;
14669 int i;
14670
14671 ga_init2(&ga, (int)sizeof(char), 200);
14672 for (i = 0; i < argc; ++i)
14673 {
14674 if (i > 0)
14675 ga_concat(&ga, (char_u *)" ");
14676 ga_concat(&ga, (char_u *)argv[i]);
14677 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010014678 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014679 ga_clear(&ga);
14680 }
14681# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014682 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014683#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014684# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010014685 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014686# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014687 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014688#endif
14689
14690theend:
14691#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014692 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014693#else
14694 vim_free(ga.ga_data);
14695#endif
14696}
14697
14698/*
14699 * "job_status()" function
14700 */
14701 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014702f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014703{
14704 char *result;
14705
14706 if (argvars[0].v_type != VAR_JOB)
14707 EMSG(_(e_invarg));
14708 else
14709 {
14710 job_T *job = argvars[0].vval.v_job;
14711
14712 if (job->jv_status == JOB_ENDED)
14713 /* No need to check, dead is dead. */
14714 result = "dead";
14715 else if (job->jv_status == JOB_FAILED)
14716 result = "fail";
14717 else
14718 result = mch_job_status(job);
14719 rettv->v_type = VAR_STRING;
14720 rettv->vval.v_string = vim_strsave((char_u *)result);
14721 }
14722}
14723
14724/*
14725 * "job_stop()" function
14726 */
14727 static void
14728f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14729{
14730 if (argvars[0].v_type != VAR_JOB)
14731 EMSG(_(e_invarg));
14732 else
14733 {
14734 char_u *arg;
14735
14736 if (argvars[1].v_type == VAR_UNKNOWN)
14737 arg = (char_u *)"";
14738 else
14739 {
14740 arg = get_tv_string_chk(&argvars[1]);
14741 if (arg == NULL)
14742 {
14743 EMSG(_(e_invarg));
14744 return;
14745 }
14746 }
14747 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14748 rettv->vval.v_number = 0;
14749 else
14750 rettv->vval.v_number = 1;
14751 }
14752}
14753#endif
14754
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014756 * "join()" function
14757 */
14758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014759f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014760{
14761 garray_T ga;
14762 char_u *sep;
14763
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014764 if (argvars[0].v_type != VAR_LIST)
14765 {
14766 EMSG(_(e_listreq));
14767 return;
14768 }
14769 if (argvars[0].vval.v_list == NULL)
14770 return;
14771 if (argvars[1].v_type == VAR_UNKNOWN)
14772 sep = (char_u *)" ";
14773 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014774 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014775
14776 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014777
14778 if (sep != NULL)
14779 {
14780 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014781 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014782 ga_append(&ga, NUL);
14783 rettv->vval.v_string = (char_u *)ga.ga_data;
14784 }
14785 else
14786 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014787}
14788
14789/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014790 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014791 */
14792 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014793f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014794{
14795 js_read_T reader;
14796
14797 reader.js_buf = get_tv_string(&argvars[0]);
14798 reader.js_fill = NULL;
14799 reader.js_used = 0;
14800 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14801 EMSG(_(e_invarg));
14802}
14803
14804/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014805 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014806 */
14807 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014808f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014809{
14810 rettv->v_type = VAR_STRING;
14811 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14812}
14813
14814/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014815 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014816 */
14817 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014818f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014819{
14820 js_read_T reader;
14821
14822 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014823 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014824 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014825 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014826 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014827}
14828
14829/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014830 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014831 */
14832 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014833f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014834{
14835 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014836 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014837}
14838
14839/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014840 * "keys()" function
14841 */
14842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014843f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014844{
14845 dict_list(argvars, rettv, 0);
14846}
14847
14848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 * "last_buffer_nr()" function.
14850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014852f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853{
14854 int n = 0;
14855 buf_T *buf;
14856
14857 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14858 if (n < buf->b_fnum)
14859 n = buf->b_fnum;
14860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014861 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014862}
14863
14864/*
14865 * "len()" function
14866 */
14867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014868f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014869{
14870 switch (argvars[0].v_type)
14871 {
14872 case VAR_STRING:
14873 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014874 rettv->vval.v_number = (varnumber_T)STRLEN(
14875 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014876 break;
14877 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014878 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014879 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014880 case VAR_DICT:
14881 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14882 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014883 case VAR_UNKNOWN:
14884 case VAR_SPECIAL:
14885 case VAR_FLOAT:
14886 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014887 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014888 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014889 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014890 break;
14891 }
14892}
14893
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014894static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014895
14896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014897libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014898{
14899#ifdef FEAT_LIBCALL
14900 char_u *string_in;
14901 char_u **string_result;
14902 int nr_result;
14903#endif
14904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014905 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014906 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014907 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014908
14909 if (check_restricted() || check_secure())
14910 return;
14911
14912#ifdef FEAT_LIBCALL
14913 /* The first two args must be strings, otherwise its meaningless */
14914 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14915 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014916 string_in = NULL;
14917 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014918 string_in = argvars[2].vval.v_string;
14919 if (type == VAR_NUMBER)
14920 string_result = NULL;
14921 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014922 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014923 if (mch_libcall(argvars[0].vval.v_string,
14924 argvars[1].vval.v_string,
14925 string_in,
14926 argvars[2].vval.v_number,
14927 string_result,
14928 &nr_result) == OK
14929 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014930 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014931 }
14932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933}
14934
14935/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014936 * "libcall()" function
14937 */
14938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014939f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014940{
14941 libcall_common(argvars, rettv, VAR_STRING);
14942}
14943
14944/*
14945 * "libcallnr()" function
14946 */
14947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014948f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014949{
14950 libcall_common(argvars, rettv, VAR_NUMBER);
14951}
14952
14953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954 * "line(string)" function
14955 */
14956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014957f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958{
14959 linenr_T lnum = 0;
14960 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014961 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014963 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 if (fp != NULL)
14965 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014966 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967}
14968
14969/*
14970 * "line2byte(lnum)" function
14971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014973f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974{
14975#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014976 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977#else
14978 linenr_T lnum;
14979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014980 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014982 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014984 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14985 if (rettv->vval.v_number >= 0)
14986 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987#endif
14988}
14989
14990/*
14991 * "lispindent(lnum)" function
14992 */
14993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014994f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995{
14996#ifdef FEAT_LISP
14997 pos_T pos;
14998 linenr_T lnum;
14999
15000 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015001 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15003 {
15004 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015005 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006 curwin->w_cursor = pos;
15007 }
15008 else
15009#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011}
15012
15013/*
15014 * "localtime()" function
15015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015017f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020}
15021
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015022static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023
15024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015025get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026{
15027 char_u *keys;
15028 char_u *which;
15029 char_u buf[NUMBUFLEN];
15030 char_u *keys_buf = NULL;
15031 char_u *rhs;
15032 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015033 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015034 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015035 mapblock_T *mp;
15036 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037
15038 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015039 rettv->v_type = VAR_STRING;
15040 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015042 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 if (*keys == NUL)
15044 return;
15045
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015046 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015048 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015049 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015050 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015051 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015052 if (argvars[3].v_type != VAR_UNKNOWN)
15053 get_dict = get_tv_number(&argvars[3]);
15054 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056 else
15057 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015058 if (which == NULL)
15059 return;
15060
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061 mode = get_map_mode(&which, 0);
15062
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015063 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015064 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015066
15067 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015069 /* Return a string. */
15070 if (rhs != NULL)
15071 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072
Bram Moolenaarbd743252010-10-20 21:23:33 +020015073 }
15074 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15075 {
15076 /* Return a dictionary. */
15077 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15078 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15079 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080
Bram Moolenaarbd743252010-10-20 21:23:33 +020015081 dict_add_nr_str(dict, "lhs", 0L, lhs);
15082 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15083 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15084 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15085 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15086 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15087 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015088 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015089 dict_add_nr_str(dict, "mode", 0L, mapmode);
15090
15091 vim_free(lhs);
15092 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093 }
15094}
15095
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015096#ifdef FEAT_FLOAT
15097/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015098 * "log()" function
15099 */
15100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015101f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015102{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015103 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015104
15105 rettv->v_type = VAR_FLOAT;
15106 if (get_float_arg(argvars, &f) == OK)
15107 rettv->vval.v_float = log(f);
15108 else
15109 rettv->vval.v_float = 0.0;
15110}
15111
15112/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015113 * "log10()" function
15114 */
15115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015116f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015117{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015118 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015119
15120 rettv->v_type = VAR_FLOAT;
15121 if (get_float_arg(argvars, &f) == OK)
15122 rettv->vval.v_float = log10(f);
15123 else
15124 rettv->vval.v_float = 0.0;
15125}
15126#endif
15127
Bram Moolenaar1dced572012-04-05 16:54:08 +020015128#ifdef FEAT_LUA
15129/*
15130 * "luaeval()" function
15131 */
15132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015133f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015134{
15135 char_u *str;
15136 char_u buf[NUMBUFLEN];
15137
15138 str = get_tv_string_buf(&argvars[0], buf);
15139 do_luaeval(str, argvars + 1, rettv);
15140}
15141#endif
15142
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015144 * "map()" function
15145 */
15146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015147f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015148{
15149 filter_map(argvars, rettv, TRUE);
15150}
15151
15152/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015154 */
15155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015156f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159}
15160
15161/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163 */
15164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015165f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168}
15169
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015170static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171
15172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015173find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015175 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015176 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015177 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178 char_u *pat;
15179 regmatch_T regmatch;
15180 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015181 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 char_u *save_cpo;
15183 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015184 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015185 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015186 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015187 list_T *l = NULL;
15188 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015189 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015190 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191
15192 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15193 save_cpo = p_cpo;
15194 p_cpo = (char_u *)"";
15195
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015196 rettv->vval.v_number = -1;
15197 if (type == 3)
15198 {
15199 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015200 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015201 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015202 }
15203 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015205 rettv->v_type = VAR_STRING;
15206 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015209 if (argvars[0].v_type == VAR_LIST)
15210 {
15211 if ((l = argvars[0].vval.v_list) == NULL)
15212 goto theend;
15213 li = l->lv_first;
15214 }
15215 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015216 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015217 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015218 len = (long)STRLEN(str);
15219 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015221 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15222 if (pat == NULL)
15223 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015224
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015225 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015227 int error = FALSE;
15228
15229 start = get_tv_number_chk(&argvars[2], &error);
15230 if (error)
15231 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015232 if (l != NULL)
15233 {
15234 li = list_find(l, start);
15235 if (li == NULL)
15236 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015237 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015238 }
15239 else
15240 {
15241 if (start < 0)
15242 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015243 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015244 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015245 /* When "count" argument is there ignore matches before "start",
15246 * otherwise skip part of the string. Differs when pattern is "^"
15247 * or "\<". */
15248 if (argvars[3].v_type != VAR_UNKNOWN)
15249 startcol = start;
15250 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015251 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015252 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015253 len -= start;
15254 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015255 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015256
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015257 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015258 nth = get_tv_number_chk(&argvars[3], &error);
15259 if (error)
15260 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261 }
15262
15263 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15264 if (regmatch.regprog != NULL)
15265 {
15266 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015267
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015268 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015269 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015270 if (l != NULL)
15271 {
15272 if (li == NULL)
15273 {
15274 match = FALSE;
15275 break;
15276 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015277 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015278 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015279 if (str == NULL)
15280 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015281 }
15282
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015283 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015284
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015285 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015286 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015287 if (l == NULL && !match)
15288 break;
15289
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015290 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015291 if (l != NULL)
15292 {
15293 li = li->li_next;
15294 ++idx;
15295 }
15296 else
15297 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015298#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015299 startcol = (colnr_T)(regmatch.startp[0]
15300 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015301#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015302 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015303#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015304 if (startcol > (colnr_T)len
15305 || str + startcol <= regmatch.startp[0])
15306 {
15307 match = FALSE;
15308 break;
15309 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015310 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015311 }
15312
15313 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015315 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015317 int i;
15318
15319 /* return list with matched string and submatches */
15320 for (i = 0; i < NSUBEXP; ++i)
15321 {
15322 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015323 {
15324 if (list_append_string(rettv->vval.v_list,
15325 (char_u *)"", 0) == FAIL)
15326 break;
15327 }
15328 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015329 regmatch.startp[i],
15330 (int)(regmatch.endp[i] - regmatch.startp[i]))
15331 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015332 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015333 }
15334 }
15335 else if (type == 2)
15336 {
15337 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015338 if (l != NULL)
15339 copy_tv(&li->li_tv, rettv);
15340 else
15341 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015343 }
15344 else if (l != NULL)
15345 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 else
15347 {
15348 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015349 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 (varnumber_T)(regmatch.startp[0] - str);
15351 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015352 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015354 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 }
15356 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015357 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 }
15359
15360theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015361 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 p_cpo = save_cpo;
15363}
15364
15365/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015366 * "match()" function
15367 */
15368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015369f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015370{
15371 find_some_match(argvars, rettv, 1);
15372}
15373
15374/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015375 * "matchadd()" function
15376 */
15377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015378f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015379{
15380#ifdef FEAT_SEARCH_EXTRA
15381 char_u buf[NUMBUFLEN];
15382 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15383 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15384 int prio = 10; /* default priority */
15385 int id = -1;
15386 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015387 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015388
15389 rettv->vval.v_number = -1;
15390
15391 if (grp == NULL || pat == NULL)
15392 return;
15393 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015394 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015395 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015396 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015397 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015398 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015399 if (argvars[4].v_type != VAR_UNKNOWN)
15400 {
15401 if (argvars[4].v_type != VAR_DICT)
15402 {
15403 EMSG(_(e_dictreq));
15404 return;
15405 }
15406 if (dict_find(argvars[4].vval.v_dict,
15407 (char_u *)"conceal", -1) != NULL)
15408 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15409 (char_u *)"conceal", FALSE);
15410 }
15411 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015412 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015413 if (error == TRUE)
15414 return;
15415 if (id >= 1 && id <= 3)
15416 {
15417 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15418 return;
15419 }
15420
Bram Moolenaar6561d522015-07-21 15:48:27 +020015421 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15422 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015423#endif
15424}
15425
15426/*
15427 * "matchaddpos()" function
15428 */
15429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015430f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015431{
15432#ifdef FEAT_SEARCH_EXTRA
15433 char_u buf[NUMBUFLEN];
15434 char_u *group;
15435 int prio = 10;
15436 int id = -1;
15437 int error = FALSE;
15438 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015439 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015440
15441 rettv->vval.v_number = -1;
15442
15443 group = get_tv_string_buf_chk(&argvars[0], buf);
15444 if (group == NULL)
15445 return;
15446
15447 if (argvars[1].v_type != VAR_LIST)
15448 {
15449 EMSG2(_(e_listarg), "matchaddpos()");
15450 return;
15451 }
15452 l = argvars[1].vval.v_list;
15453 if (l == NULL)
15454 return;
15455
15456 if (argvars[2].v_type != VAR_UNKNOWN)
15457 {
15458 prio = get_tv_number_chk(&argvars[2], &error);
15459 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015460 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015461 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015462 if (argvars[4].v_type != VAR_UNKNOWN)
15463 {
15464 if (argvars[4].v_type != VAR_DICT)
15465 {
15466 EMSG(_(e_dictreq));
15467 return;
15468 }
15469 if (dict_find(argvars[4].vval.v_dict,
15470 (char_u *)"conceal", -1) != NULL)
15471 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15472 (char_u *)"conceal", FALSE);
15473 }
15474 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015475 }
15476 if (error == TRUE)
15477 return;
15478
15479 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15480 if (id == 1 || id == 2)
15481 {
15482 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15483 return;
15484 }
15485
Bram Moolenaar6561d522015-07-21 15:48:27 +020015486 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15487 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015488#endif
15489}
15490
15491/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015492 * "matcharg()" function
15493 */
15494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015495f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015496{
15497 if (rettv_list_alloc(rettv) == OK)
15498 {
15499#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015500 int id = get_tv_number(&argvars[0]);
15501 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015502
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015503 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015504 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015505 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15506 {
15507 list_append_string(rettv->vval.v_list,
15508 syn_id2name(m->hlg_id), -1);
15509 list_append_string(rettv->vval.v_list, m->pattern, -1);
15510 }
15511 else
15512 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015513 list_append_string(rettv->vval.v_list, NULL, -1);
15514 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015515 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015516 }
15517#endif
15518 }
15519}
15520
15521/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015522 * "matchdelete()" function
15523 */
15524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015525f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015526{
15527#ifdef FEAT_SEARCH_EXTRA
15528 rettv->vval.v_number = match_delete(curwin,
15529 (int)get_tv_number(&argvars[0]), TRUE);
15530#endif
15531}
15532
15533/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015534 * "matchend()" function
15535 */
15536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015537f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015538{
15539 find_some_match(argvars, rettv, 0);
15540}
15541
15542/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015543 * "matchlist()" function
15544 */
15545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015546f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015547{
15548 find_some_match(argvars, rettv, 3);
15549}
15550
15551/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015552 * "matchstr()" function
15553 */
15554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015555f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015556{
15557 find_some_match(argvars, rettv, 2);
15558}
15559
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015560static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015561
15562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015563max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015564{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015565 long n = 0;
15566 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015567 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015568
15569 if (argvars[0].v_type == VAR_LIST)
15570 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015571 list_T *l;
15572 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015573
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015574 l = argvars[0].vval.v_list;
15575 if (l != NULL)
15576 {
15577 li = l->lv_first;
15578 if (li != NULL)
15579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015580 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015581 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015582 {
15583 li = li->li_next;
15584 if (li == NULL)
15585 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015586 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015587 if (domax ? i > n : i < n)
15588 n = i;
15589 }
15590 }
15591 }
15592 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015593 else if (argvars[0].v_type == VAR_DICT)
15594 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015595 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015596 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015597 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015598 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015599
15600 d = argvars[0].vval.v_dict;
15601 if (d != NULL)
15602 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015603 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015604 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015606 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015607 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015608 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015609 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015610 if (first)
15611 {
15612 n = i;
15613 first = FALSE;
15614 }
15615 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015616 n = i;
15617 }
15618 }
15619 }
15620 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015621 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015622 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015623 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015624}
15625
15626/*
15627 * "max()" function
15628 */
15629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015630f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015631{
15632 max_min(argvars, rettv, TRUE);
15633}
15634
15635/*
15636 * "min()" function
15637 */
15638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015639f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015640{
15641 max_min(argvars, rettv, FALSE);
15642}
15643
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015644static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015645
15646/*
15647 * Create the directory in which "dir" is located, and higher levels when
15648 * needed.
15649 */
15650 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015651mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015652{
15653 char_u *p;
15654 char_u *updir;
15655 int r = FAIL;
15656
15657 /* Get end of directory name in "dir".
15658 * We're done when it's "/" or "c:/". */
15659 p = gettail_sep(dir);
15660 if (p <= get_past_head(dir))
15661 return OK;
15662
15663 /* If the directory exists we're done. Otherwise: create it.*/
15664 updir = vim_strnsave(dir, (int)(p - dir));
15665 if (updir == NULL)
15666 return FAIL;
15667 if (mch_isdir(updir))
15668 r = OK;
15669 else if (mkdir_recurse(updir, prot) == OK)
15670 r = vim_mkdir_emsg(updir, prot);
15671 vim_free(updir);
15672 return r;
15673}
15674
15675#ifdef vim_mkdir
15676/*
15677 * "mkdir()" function
15678 */
15679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015680f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015681{
15682 char_u *dir;
15683 char_u buf[NUMBUFLEN];
15684 int prot = 0755;
15685
15686 rettv->vval.v_number = FAIL;
15687 if (check_restricted() || check_secure())
15688 return;
15689
15690 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015691 if (*dir == NUL)
15692 rettv->vval.v_number = FAIL;
15693 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015694 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015695 if (*gettail(dir) == NUL)
15696 /* remove trailing slashes */
15697 *gettail_sep(dir) = NUL;
15698
15699 if (argvars[1].v_type != VAR_UNKNOWN)
15700 {
15701 if (argvars[2].v_type != VAR_UNKNOWN)
15702 prot = get_tv_number_chk(&argvars[2], NULL);
15703 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15704 mkdir_recurse(dir, prot);
15705 }
15706 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015707 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015708}
15709#endif
15710
Bram Moolenaar0d660222005-01-07 21:51:51 +000015711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 * "mode()" function
15713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015715f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015717 char_u buf[3];
15718
15719 buf[1] = NUL;
15720 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 if (VIsual_active)
15723 {
15724 if (VIsual_select)
15725 buf[0] = VIsual_mode + 's' - 'v';
15726 else
15727 buf[0] = VIsual_mode;
15728 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015729 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015730 || State == CONFIRM)
15731 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015733 if (State == ASKMORE)
15734 buf[1] = 'm';
15735 else if (State == CONFIRM)
15736 buf[1] = '?';
15737 }
15738 else if (State == EXTERNCMD)
15739 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740 else if (State & INSERT)
15741 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015742#ifdef FEAT_VREPLACE
15743 if (State & VREPLACE_FLAG)
15744 {
15745 buf[0] = 'R';
15746 buf[1] = 'v';
15747 }
15748 else
15749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 if (State & REPLACE_FLAG)
15751 buf[0] = 'R';
15752 else
15753 buf[0] = 'i';
15754 }
15755 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015758 if (exmode_active)
15759 buf[1] = 'v';
15760 }
15761 else if (exmode_active)
15762 {
15763 buf[0] = 'c';
15764 buf[1] = 'e';
15765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015767 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015769 if (finish_op)
15770 buf[1] = 'o';
15771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015773 /* Clear out the minor mode when the argument is not a non-zero number or
15774 * non-empty string. */
15775 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015776 buf[1] = NUL;
15777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015778 rettv->vval.v_string = vim_strsave(buf);
15779 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780}
15781
Bram Moolenaar429fa852013-04-15 12:27:36 +020015782#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015783/*
15784 * "mzeval()" function
15785 */
15786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015787f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015788{
15789 char_u *str;
15790 char_u buf[NUMBUFLEN];
15791
15792 str = get_tv_string_buf(&argvars[0], buf);
15793 do_mzeval(str, rettv);
15794}
Bram Moolenaar75676462013-01-30 14:55:42 +010015795
15796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015797mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015798{
15799 typval_T argvars[3];
15800
15801 argvars[0].v_type = VAR_STRING;
15802 argvars[0].vval.v_string = name;
15803 copy_tv(args, &argvars[1]);
15804 argvars[2].v_type = VAR_UNKNOWN;
15805 f_call(argvars, rettv);
15806 clear_tv(&argvars[1]);
15807}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015808#endif
15809
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015811 * "nextnonblank()" function
15812 */
15813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015814f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015815{
15816 linenr_T lnum;
15817
15818 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015820 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015821 {
15822 lnum = 0;
15823 break;
15824 }
15825 if (*skipwhite(ml_get(lnum)) != NUL)
15826 break;
15827 }
15828 rettv->vval.v_number = lnum;
15829}
15830
15831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832 * "nr2char()" function
15833 */
15834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015835f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836{
15837 char_u buf[NUMBUFLEN];
15838
15839#ifdef FEAT_MBYTE
15840 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015841 {
15842 int utf8 = 0;
15843
15844 if (argvars[1].v_type != VAR_UNKNOWN)
15845 utf8 = get_tv_number_chk(&argvars[1], NULL);
15846 if (utf8)
15847 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15848 else
15849 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 else
15852#endif
15853 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015854 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 buf[1] = NUL;
15856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015857 rettv->v_type = VAR_STRING;
15858 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015859}
15860
15861/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015862 * "or(expr, expr)" function
15863 */
15864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015865f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015866{
15867 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15868 | get_tv_number_chk(&argvars[1], NULL);
15869}
15870
15871/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015872 * "pathshorten()" function
15873 */
15874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015875f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015876{
15877 char_u *p;
15878
15879 rettv->v_type = VAR_STRING;
15880 p = get_tv_string_chk(&argvars[0]);
15881 if (p == NULL)
15882 rettv->vval.v_string = NULL;
15883 else
15884 {
15885 p = vim_strsave(p);
15886 rettv->vval.v_string = p;
15887 if (p != NULL)
15888 shorten_dir(p);
15889 }
15890}
15891
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015892#ifdef FEAT_PERL
15893/*
15894 * "perleval()" function
15895 */
15896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015897f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015898{
15899 char_u *str;
15900 char_u buf[NUMBUFLEN];
15901
15902 str = get_tv_string_buf(&argvars[0], buf);
15903 do_perleval(str, rettv);
15904}
15905#endif
15906
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015907#ifdef FEAT_FLOAT
15908/*
15909 * "pow()" function
15910 */
15911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015912f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015913{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015914 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015915
15916 rettv->v_type = VAR_FLOAT;
15917 if (get_float_arg(argvars, &fx) == OK
15918 && get_float_arg(&argvars[1], &fy) == OK)
15919 rettv->vval.v_float = pow(fx, fy);
15920 else
15921 rettv->vval.v_float = 0.0;
15922}
15923#endif
15924
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015925/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015926 * "prevnonblank()" function
15927 */
15928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015929f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015930{
15931 linenr_T lnum;
15932
15933 lnum = get_tv_lnum(argvars);
15934 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15935 lnum = 0;
15936 else
15937 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15938 --lnum;
15939 rettv->vval.v_number = lnum;
15940}
15941
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015942/* This dummy va_list is here because:
15943 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15944 * - locally in the function results in a "used before set" warning
15945 * - using va_start() to initialize it gives "function with fixed args" error */
15946static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015947
Bram Moolenaar8c711452005-01-14 21:53:12 +000015948/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015949 * "printf()" function
15950 */
15951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015952f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015953{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015954 char_u buf[NUMBUFLEN];
15955 int len;
15956 char_u *s;
15957 int saved_did_emsg = did_emsg;
15958 char *fmt;
15959
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015960 rettv->v_type = VAR_STRING;
15961 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015962
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015963 /* Get the required length, allocate the buffer and do it for real. */
15964 did_emsg = FALSE;
15965 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15966 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15967 if (!did_emsg)
15968 {
15969 s = alloc(len + 1);
15970 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015971 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015972 rettv->vval.v_string = s;
15973 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015974 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015975 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015976 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015977}
15978
15979/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015980 * "pumvisible()" function
15981 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015983f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015984{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015985#ifdef FEAT_INS_EXPAND
15986 if (pum_visible())
15987 rettv->vval.v_number = 1;
15988#endif
15989}
15990
Bram Moolenaardb913952012-06-29 12:54:53 +020015991#ifdef FEAT_PYTHON3
15992/*
15993 * "py3eval()" function
15994 */
15995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015996f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015997{
15998 char_u *str;
15999 char_u buf[NUMBUFLEN];
16000
16001 str = get_tv_string_buf(&argvars[0], buf);
16002 do_py3eval(str, rettv);
16003}
16004#endif
16005
16006#ifdef FEAT_PYTHON
16007/*
16008 * "pyeval()" function
16009 */
16010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016011f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016012{
16013 char_u *str;
16014 char_u buf[NUMBUFLEN];
16015
16016 str = get_tv_string_buf(&argvars[0], buf);
16017 do_pyeval(str, rettv);
16018}
16019#endif
16020
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016021/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016022 * "range()" function
16023 */
16024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016025f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016026{
16027 long start;
16028 long end;
16029 long stride = 1;
16030 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016031 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016033 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016034 if (argvars[1].v_type == VAR_UNKNOWN)
16035 {
16036 end = start - 1;
16037 start = 0;
16038 }
16039 else
16040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016042 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016043 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016044 }
16045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016046 if (error)
16047 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016048 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016049 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016050 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016051 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016052 else
16053 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016054 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016055 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016056 if (list_append_number(rettv->vval.v_list,
16057 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016058 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016059 }
16060}
16061
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016062/*
16063 * "readfile()" function
16064 */
16065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016066f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016067{
16068 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016069 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016070 char_u *fname;
16071 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016072 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16073 int io_size = sizeof(buf);
16074 int readlen; /* size of last fread() */
16075 char_u *prev = NULL; /* previously read bytes, if any */
16076 long prevlen = 0; /* length of data in prev */
16077 long prevsize = 0; /* size of prev buffer */
16078 long maxline = MAXLNUM;
16079 long cnt = 0;
16080 char_u *p; /* position in buf */
16081 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016082
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016083 if (argvars[1].v_type != VAR_UNKNOWN)
16084 {
16085 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16086 binary = TRUE;
16087 if (argvars[2].v_type != VAR_UNKNOWN)
16088 maxline = get_tv_number(&argvars[2]);
16089 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016090
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016091 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016092 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016093
16094 /* Always open the file in binary mode, library functions have a mind of
16095 * their own about CR-LF conversion. */
16096 fname = get_tv_string(&argvars[0]);
16097 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16098 {
16099 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16100 return;
16101 }
16102
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016103 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016104 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016105 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016106
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016107 /* This for loop processes what was read, but is also entered at end
16108 * of file so that either:
16109 * - an incomplete line gets written
16110 * - a "binary" file gets an empty line at the end if it ends in a
16111 * newline. */
16112 for (p = buf, start = buf;
16113 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16114 ++p)
16115 {
16116 if (*p == '\n' || readlen <= 0)
16117 {
16118 listitem_T *li;
16119 char_u *s = NULL;
16120 long_u len = p - start;
16121
16122 /* Finished a line. Remove CRs before NL. */
16123 if (readlen > 0 && !binary)
16124 {
16125 while (len > 0 && start[len - 1] == '\r')
16126 --len;
16127 /* removal may cross back to the "prev" string */
16128 if (len == 0)
16129 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16130 --prevlen;
16131 }
16132 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016133 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016134 else
16135 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016136 /* Change "prev" buffer to be the right size. This way
16137 * the bytes are only copied once, and very long lines are
16138 * allocated only once. */
16139 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016140 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016141 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016142 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016143 prev = NULL; /* the list will own the string */
16144 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016145 }
16146 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016147 if (s == NULL)
16148 {
16149 do_outofmem_msg((long_u) prevlen + len + 1);
16150 failed = TRUE;
16151 break;
16152 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016153
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016154 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016155 {
16156 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016157 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016158 break;
16159 }
16160 li->li_tv.v_type = VAR_STRING;
16161 li->li_tv.v_lock = 0;
16162 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016163 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016164
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016165 start = p + 1; /* step over newline */
16166 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016167 break;
16168 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016169 else if (*p == NUL)
16170 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016171#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016172 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16173 * when finding the BF and check the previous two bytes. */
16174 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016175 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016176 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16177 * + 1, these may be in the "prev" string. */
16178 char_u back1 = p >= buf + 1 ? p[-1]
16179 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16180 char_u back2 = p >= buf + 2 ? p[-2]
16181 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16182 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016183
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016184 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016185 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016186 char_u *dest = p - 2;
16187
16188 /* Usually a BOM is at the beginning of a file, and so at
16189 * the beginning of a line; then we can just step over it.
16190 */
16191 if (start == dest)
16192 start = p + 1;
16193 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016194 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016195 /* have to shuffle buf to close gap */
16196 int adjust_prevlen = 0;
16197
16198 if (dest < buf)
16199 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016200 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016201 dest = buf;
16202 }
16203 if (readlen > p - buf + 1)
16204 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16205 readlen -= 3 - adjust_prevlen;
16206 prevlen -= adjust_prevlen;
16207 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016208 }
16209 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016210 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016211#endif
16212 } /* for */
16213
16214 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16215 break;
16216 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016217 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016218 /* There's part of a line in buf, store it in "prev". */
16219 if (p - start + prevlen >= prevsize)
16220 {
16221 /* need bigger "prev" buffer */
16222 char_u *newprev;
16223
16224 /* A common use case is ordinary text files and "prev" gets a
16225 * fragment of a line, so the first allocation is made
16226 * small, to avoid repeatedly 'allocing' large and
16227 * 'reallocing' small. */
16228 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016229 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016230 else
16231 {
16232 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016233 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016234 prevsize = grow50pc > growmin ? grow50pc : growmin;
16235 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016236 newprev = prev == NULL ? alloc(prevsize)
16237 : vim_realloc(prev, prevsize);
16238 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016239 {
16240 do_outofmem_msg((long_u)prevsize);
16241 failed = TRUE;
16242 break;
16243 }
16244 prev = newprev;
16245 }
16246 /* Add the line part to end of "prev". */
16247 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016248 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016249 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016250 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016251
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016252 /*
16253 * For a negative line count use only the lines at the end of the file,
16254 * free the rest.
16255 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016256 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016257 while (cnt > -maxline)
16258 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016259 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016260 --cnt;
16261 }
16262
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016263 if (failed)
16264 {
16265 list_free(rettv->vval.v_list, TRUE);
16266 /* readfile doc says an empty list is returned on error */
16267 rettv->vval.v_list = list_alloc();
16268 }
16269
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016270 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016271 fclose(fd);
16272}
16273
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016274#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016275static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016276
16277/*
16278 * Convert a List to proftime_T.
16279 * Return FAIL when there is something wrong.
16280 */
16281 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016282list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016283{
16284 long n1, n2;
16285 int error = FALSE;
16286
16287 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16288 || arg->vval.v_list->lv_len != 2)
16289 return FAIL;
16290 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16291 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16292# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016293 tm->HighPart = n1;
16294 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016295# else
16296 tm->tv_sec = n1;
16297 tm->tv_usec = n2;
16298# endif
16299 return error ? FAIL : OK;
16300}
16301#endif /* FEAT_RELTIME */
16302
16303/*
16304 * "reltime()" function
16305 */
16306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016307f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016308{
16309#ifdef FEAT_RELTIME
16310 proftime_T res;
16311 proftime_T start;
16312
16313 if (argvars[0].v_type == VAR_UNKNOWN)
16314 {
16315 /* No arguments: get current time. */
16316 profile_start(&res);
16317 }
16318 else if (argvars[1].v_type == VAR_UNKNOWN)
16319 {
16320 if (list2proftime(&argvars[0], &res) == FAIL)
16321 return;
16322 profile_end(&res);
16323 }
16324 else
16325 {
16326 /* Two arguments: compute the difference. */
16327 if (list2proftime(&argvars[0], &start) == FAIL
16328 || list2proftime(&argvars[1], &res) == FAIL)
16329 return;
16330 profile_sub(&res, &start);
16331 }
16332
16333 if (rettv_list_alloc(rettv) == OK)
16334 {
16335 long n1, n2;
16336
16337# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016338 n1 = res.HighPart;
16339 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016340# else
16341 n1 = res.tv_sec;
16342 n2 = res.tv_usec;
16343# endif
16344 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16345 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16346 }
16347#endif
16348}
16349
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016350#ifdef FEAT_FLOAT
16351/*
16352 * "reltimefloat()" function
16353 */
16354 static void
16355f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16356{
16357# ifdef FEAT_RELTIME
16358 proftime_T tm;
16359# endif
16360
16361 rettv->v_type = VAR_FLOAT;
16362 rettv->vval.v_float = 0;
16363# ifdef FEAT_RELTIME
16364 if (list2proftime(&argvars[0], &tm) == OK)
16365 rettv->vval.v_float = profile_float(&tm);
16366# endif
16367}
16368#endif
16369
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016370/*
16371 * "reltimestr()" function
16372 */
16373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016374f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016375{
16376#ifdef FEAT_RELTIME
16377 proftime_T tm;
16378#endif
16379
16380 rettv->v_type = VAR_STRING;
16381 rettv->vval.v_string = NULL;
16382#ifdef FEAT_RELTIME
16383 if (list2proftime(&argvars[0], &tm) == OK)
16384 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16385#endif
16386}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016387
Bram Moolenaar0d660222005-01-07 21:51:51 +000016388#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016389static void make_connection(void);
16390static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016391
16392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016393make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016394{
16395 if (X_DISPLAY == NULL
16396# ifdef FEAT_GUI
16397 && !gui.in_use
16398# endif
16399 )
16400 {
16401 x_force_connect = TRUE;
16402 setup_term_clip();
16403 x_force_connect = FALSE;
16404 }
16405}
16406
16407 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016408check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016409{
16410 make_connection();
16411 if (X_DISPLAY == NULL)
16412 {
16413 EMSG(_("E240: No connection to Vim server"));
16414 return FAIL;
16415 }
16416 return OK;
16417}
16418#endif
16419
16420#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016421static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016422
16423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016424remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016425{
16426 char_u *server_name;
16427 char_u *keys;
16428 char_u *r = NULL;
16429 char_u buf[NUMBUFLEN];
16430# ifdef WIN32
16431 HWND w;
16432# else
16433 Window w;
16434# endif
16435
16436 if (check_restricted() || check_secure())
16437 return;
16438
16439# ifdef FEAT_X11
16440 if (check_connection() == FAIL)
16441 return;
16442# endif
16443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016444 server_name = get_tv_string_chk(&argvars[0]);
16445 if (server_name == NULL)
16446 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016447 keys = get_tv_string_buf(&argvars[1], buf);
16448# ifdef WIN32
16449 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16450# else
16451 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16452 < 0)
16453# endif
16454 {
16455 if (r != NULL)
16456 EMSG(r); /* sending worked but evaluation failed */
16457 else
16458 EMSG2(_("E241: Unable to send to %s"), server_name);
16459 return;
16460 }
16461
16462 rettv->vval.v_string = r;
16463
16464 if (argvars[2].v_type != VAR_UNKNOWN)
16465 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016466 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016467 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016468 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016469
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016470 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016471 v.di_tv.v_type = VAR_STRING;
16472 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016473 idvar = get_tv_string_chk(&argvars[2]);
16474 if (idvar != NULL)
16475 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016476 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477 }
16478}
16479#endif
16480
16481/*
16482 * "remote_expr()" function
16483 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016485f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016486{
16487 rettv->v_type = VAR_STRING;
16488 rettv->vval.v_string = NULL;
16489#ifdef FEAT_CLIENTSERVER
16490 remote_common(argvars, rettv, TRUE);
16491#endif
16492}
16493
16494/*
16495 * "remote_foreground()" function
16496 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016498f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016499{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500#ifdef FEAT_CLIENTSERVER
16501# ifdef WIN32
16502 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016503 {
16504 char_u *server_name = get_tv_string_chk(&argvars[0]);
16505
16506 if (server_name != NULL)
16507 serverForeground(server_name);
16508 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016509# else
16510 /* Send a foreground() expression to the server. */
16511 argvars[1].v_type = VAR_STRING;
16512 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16513 argvars[2].v_type = VAR_UNKNOWN;
16514 remote_common(argvars, rettv, TRUE);
16515 vim_free(argvars[1].vval.v_string);
16516# endif
16517#endif
16518}
16519
Bram Moolenaar0d660222005-01-07 21:51:51 +000016520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016521f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522{
16523#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525 char_u *s = NULL;
16526# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016527 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016528# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016529 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016530
16531 if (check_restricted() || check_secure())
16532 {
16533 rettv->vval.v_number = -1;
16534 return;
16535 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016536 serverid = get_tv_string_chk(&argvars[0]);
16537 if (serverid == NULL)
16538 {
16539 rettv->vval.v_number = -1;
16540 return; /* type error; errmsg already given */
16541 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016542# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016543 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016544 if (n == 0)
16545 rettv->vval.v_number = -1;
16546 else
16547 {
16548 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16549 rettv->vval.v_number = (s != NULL);
16550 }
16551# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016552 if (check_connection() == FAIL)
16553 return;
16554
16555 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016556 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016557# endif
16558
16559 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016561 char_u *retvar;
16562
Bram Moolenaar33570922005-01-25 22:26:29 +000016563 v.di_tv.v_type = VAR_STRING;
16564 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016565 retvar = get_tv_string_chk(&argvars[1]);
16566 if (retvar != NULL)
16567 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016568 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016569 }
16570#else
16571 rettv->vval.v_number = -1;
16572#endif
16573}
16574
Bram Moolenaar0d660222005-01-07 21:51:51 +000016575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016576f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016577{
16578 char_u *r = NULL;
16579
16580#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016581 char_u *serverid = get_tv_string_chk(&argvars[0]);
16582
16583 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016584 {
16585# ifdef WIN32
16586 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016587 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016588
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016589 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016590 if (n != 0)
16591 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16592 if (r == NULL)
16593# else
16594 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016595 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016596# endif
16597 EMSG(_("E277: Unable to read a server reply"));
16598 }
16599#endif
16600 rettv->v_type = VAR_STRING;
16601 rettv->vval.v_string = r;
16602}
16603
16604/*
16605 * "remote_send()" function
16606 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016608f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016609{
16610 rettv->v_type = VAR_STRING;
16611 rettv->vval.v_string = NULL;
16612#ifdef FEAT_CLIENTSERVER
16613 remote_common(argvars, rettv, FALSE);
16614#endif
16615}
16616
16617/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016618 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016619 */
16620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016621f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016622{
Bram Moolenaar33570922005-01-25 22:26:29 +000016623 list_T *l;
16624 listitem_T *item, *item2;
16625 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016626 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016627 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016628 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016629 dict_T *d;
16630 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016631 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016632
Bram Moolenaar8c711452005-01-14 21:53:12 +000016633 if (argvars[0].v_type == VAR_DICT)
16634 {
16635 if (argvars[2].v_type != VAR_UNKNOWN)
16636 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016637 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016638 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016640 key = get_tv_string_chk(&argvars[1]);
16641 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016643 di = dict_find(d, key, -1);
16644 if (di == NULL)
16645 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016646 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16647 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016648 {
16649 *rettv = di->di_tv;
16650 init_tv(&di->di_tv);
16651 dictitem_remove(d, di);
16652 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016653 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016654 }
16655 }
16656 else if (argvars[0].v_type != VAR_LIST)
16657 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016658 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016659 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016661 int error = FALSE;
16662
16663 idx = get_tv_number_chk(&argvars[1], &error);
16664 if (error)
16665 ; /* type error: do nothing, errmsg already given */
16666 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016667 EMSGN(_(e_listidx), idx);
16668 else
16669 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016670 if (argvars[2].v_type == VAR_UNKNOWN)
16671 {
16672 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016673 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016674 *rettv = item->li_tv;
16675 vim_free(item);
16676 }
16677 else
16678 {
16679 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016680 end = get_tv_number_chk(&argvars[2], &error);
16681 if (error)
16682 ; /* type error: do nothing */
16683 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016684 EMSGN(_(e_listidx), end);
16685 else
16686 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016687 int cnt = 0;
16688
16689 for (li = item; li != NULL; li = li->li_next)
16690 {
16691 ++cnt;
16692 if (li == item2)
16693 break;
16694 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016695 if (li == NULL) /* didn't find "item2" after "item" */
16696 EMSG(_(e_invrange));
16697 else
16698 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016699 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016700 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016701 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016702 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016703 l->lv_first = item;
16704 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016705 item->li_prev = NULL;
16706 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016707 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016708 }
16709 }
16710 }
16711 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016712 }
16713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714}
16715
16716/*
16717 * "rename({from}, {to})" function
16718 */
16719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016720f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721{
16722 char_u buf[NUMBUFLEN];
16723
16724 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016725 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016727 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16728 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729}
16730
16731/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016732 * "repeat()" function
16733 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016735f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016736{
16737 char_u *p;
16738 int n;
16739 int slen;
16740 int len;
16741 char_u *r;
16742 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016743
16744 n = get_tv_number(&argvars[1]);
16745 if (argvars[0].v_type == VAR_LIST)
16746 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016747 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016748 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016749 if (list_extend(rettv->vval.v_list,
16750 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016751 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016752 }
16753 else
16754 {
16755 p = get_tv_string(&argvars[0]);
16756 rettv->v_type = VAR_STRING;
16757 rettv->vval.v_string = NULL;
16758
16759 slen = (int)STRLEN(p);
16760 len = slen * n;
16761 if (len <= 0)
16762 return;
16763
16764 r = alloc(len + 1);
16765 if (r != NULL)
16766 {
16767 for (i = 0; i < n; i++)
16768 mch_memmove(r + i * slen, p, (size_t)slen);
16769 r[len] = NUL;
16770 }
16771
16772 rettv->vval.v_string = r;
16773 }
16774}
16775
16776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 * "resolve()" function
16778 */
16779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016780f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781{
16782 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016783#ifdef HAVE_READLINK
16784 char_u *buf = NULL;
16785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016787 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788#ifdef FEAT_SHORTCUT
16789 {
16790 char_u *v = NULL;
16791
16792 v = mch_resolve_shortcut(p);
16793 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016794 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016796 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 }
16798#else
16799# ifdef HAVE_READLINK
16800 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801 char_u *cpy;
16802 int len;
16803 char_u *remain = NULL;
16804 char_u *q;
16805 int is_relative_to_current = FALSE;
16806 int has_trailing_pathsep = FALSE;
16807 int limit = 100;
16808
16809 p = vim_strsave(p);
16810
16811 if (p[0] == '.' && (vim_ispathsep(p[1])
16812 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16813 is_relative_to_current = TRUE;
16814
16815 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016816 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016819 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821
16822 q = getnextcomp(p);
16823 if (*q != NUL)
16824 {
16825 /* Separate the first path component in "p", and keep the
16826 * remainder (beginning with the path separator). */
16827 remain = vim_strsave(q - 1);
16828 q[-1] = NUL;
16829 }
16830
Bram Moolenaard9462e32011-04-11 21:35:11 +020016831 buf = alloc(MAXPATHL + 1);
16832 if (buf == NULL)
16833 goto fail;
16834
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 for (;;)
16836 {
16837 for (;;)
16838 {
16839 len = readlink((char *)p, (char *)buf, MAXPATHL);
16840 if (len <= 0)
16841 break;
16842 buf[len] = NUL;
16843
16844 if (limit-- == 0)
16845 {
16846 vim_free(p);
16847 vim_free(remain);
16848 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 goto fail;
16851 }
16852
16853 /* Ensure that the result will have a trailing path separator
16854 * if the argument has one. */
16855 if (remain == NULL && has_trailing_pathsep)
16856 add_pathsep(buf);
16857
16858 /* Separate the first path component in the link value and
16859 * concatenate the remainders. */
16860 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16861 if (*q != NUL)
16862 {
16863 if (remain == NULL)
16864 remain = vim_strsave(q - 1);
16865 else
16866 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016867 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868 if (cpy != NULL)
16869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 vim_free(remain);
16871 remain = cpy;
16872 }
16873 }
16874 q[-1] = NUL;
16875 }
16876
16877 q = gettail(p);
16878 if (q > p && *q == NUL)
16879 {
16880 /* Ignore trailing path separator. */
16881 q[-1] = NUL;
16882 q = gettail(p);
16883 }
16884 if (q > p && !mch_isFullName(buf))
16885 {
16886 /* symlink is relative to directory of argument */
16887 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16888 if (cpy != NULL)
16889 {
16890 STRCPY(cpy, p);
16891 STRCPY(gettail(cpy), buf);
16892 vim_free(p);
16893 p = cpy;
16894 }
16895 }
16896 else
16897 {
16898 vim_free(p);
16899 p = vim_strsave(buf);
16900 }
16901 }
16902
16903 if (remain == NULL)
16904 break;
16905
16906 /* Append the first path component of "remain" to "p". */
16907 q = getnextcomp(remain + 1);
16908 len = q - remain - (*q != NUL);
16909 cpy = vim_strnsave(p, STRLEN(p) + len);
16910 if (cpy != NULL)
16911 {
16912 STRNCAT(cpy, remain, len);
16913 vim_free(p);
16914 p = cpy;
16915 }
16916 /* Shorten "remain". */
16917 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016918 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 else
16920 {
16921 vim_free(remain);
16922 remain = NULL;
16923 }
16924 }
16925
16926 /* If the result is a relative path name, make it explicitly relative to
16927 * the current directory if and only if the argument had this form. */
16928 if (!vim_ispathsep(*p))
16929 {
16930 if (is_relative_to_current
16931 && *p != NUL
16932 && !(p[0] == '.'
16933 && (p[1] == NUL
16934 || vim_ispathsep(p[1])
16935 || (p[1] == '.'
16936 && (p[2] == NUL
16937 || vim_ispathsep(p[2]))))))
16938 {
16939 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016940 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 if (cpy != NULL)
16942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943 vim_free(p);
16944 p = cpy;
16945 }
16946 }
16947 else if (!is_relative_to_current)
16948 {
16949 /* Strip leading "./". */
16950 q = p;
16951 while (q[0] == '.' && vim_ispathsep(q[1]))
16952 q += 2;
16953 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016954 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955 }
16956 }
16957
16958 /* Ensure that the result will have no trailing path separator
16959 * if the argument had none. But keep "/" or "//". */
16960 if (!has_trailing_pathsep)
16961 {
16962 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016963 if (after_pathsep(p, q))
16964 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 }
16966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016967 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 }
16969# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016970 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971# endif
16972#endif
16973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016974 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975
16976#ifdef HAVE_READLINK
16977fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016978 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016980 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981}
16982
16983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 */
16986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016987f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988{
Bram Moolenaar33570922005-01-25 22:26:29 +000016989 list_T *l;
16990 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 if (argvars[0].v_type != VAR_LIST)
16993 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016994 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016995 && !tv_check_lock(l->lv_lock,
16996 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997 {
16998 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016999 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017000 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017001 while (li != NULL)
17002 {
17003 ni = li->li_prev;
17004 list_append(l, li);
17005 li = ni;
17006 }
17007 rettv->vval.v_list = l;
17008 rettv->v_type = VAR_LIST;
17009 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017010 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012}
17013
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017014#define SP_NOMOVE 0x01 /* don't move cursor */
17015#define SP_REPEAT 0x02 /* repeat to find outer pair */
17016#define SP_RETCOUNT 0x04 /* return matchcount */
17017#define SP_SETPCMARK 0x08 /* set previous context mark */
17018#define SP_START 0x10 /* accept match at start position */
17019#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17020#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017021#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017022
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017023static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024
17025/*
17026 * Get flags for a search function.
17027 * Possibly sets "p_ws".
17028 * Returns BACKWARD, FORWARD or zero (for an error).
17029 */
17030 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017031get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032{
17033 int dir = FORWARD;
17034 char_u *flags;
17035 char_u nbuf[NUMBUFLEN];
17036 int mask;
17037
17038 if (varp->v_type != VAR_UNKNOWN)
17039 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017040 flags = get_tv_string_buf_chk(varp, nbuf);
17041 if (flags == NULL)
17042 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017043 while (*flags != NUL)
17044 {
17045 switch (*flags)
17046 {
17047 case 'b': dir = BACKWARD; break;
17048 case 'w': p_ws = TRUE; break;
17049 case 'W': p_ws = FALSE; break;
17050 default: mask = 0;
17051 if (flagsp != NULL)
17052 switch (*flags)
17053 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017054 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017055 case 'e': mask = SP_END; break;
17056 case 'm': mask = SP_RETCOUNT; break;
17057 case 'n': mask = SP_NOMOVE; break;
17058 case 'p': mask = SP_SUBPAT; break;
17059 case 'r': mask = SP_REPEAT; break;
17060 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017061 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017062 }
17063 if (mask == 0)
17064 {
17065 EMSG2(_(e_invarg2), flags);
17066 dir = 0;
17067 }
17068 else
17069 *flagsp |= mask;
17070 }
17071 if (dir == 0)
17072 break;
17073 ++flags;
17074 }
17075 }
17076 return dir;
17077}
17078
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017080 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017082 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017083search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017085 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 char_u *pat;
17087 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017088 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089 int save_p_ws = p_ws;
17090 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017091 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017092 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017093 proftime_T tm;
17094#ifdef FEAT_RELTIME
17095 long time_limit = 0;
17096#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017097 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017098 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017100 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017101 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017102 if (dir == 0)
17103 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017104 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017105 if (flags & SP_START)
17106 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017107 if (flags & SP_END)
17108 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017109 if (flags & SP_COLUMN)
17110 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017111
Bram Moolenaar76929292008-01-06 19:07:36 +000017112 /* Optional arguments: line number to stop searching and timeout. */
17113 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017114 {
17115 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17116 if (lnum_stop < 0)
17117 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017118#ifdef FEAT_RELTIME
17119 if (argvars[3].v_type != VAR_UNKNOWN)
17120 {
17121 time_limit = get_tv_number_chk(&argvars[3], NULL);
17122 if (time_limit < 0)
17123 goto theend;
17124 }
17125#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017126 }
17127
Bram Moolenaar76929292008-01-06 19:07:36 +000017128#ifdef FEAT_RELTIME
17129 /* Set the time limit, if there is one. */
17130 profile_setlimit(time_limit, &tm);
17131#endif
17132
Bram Moolenaar231334e2005-07-25 20:46:57 +000017133 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017134 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017135 * Check to make sure only those flags are set.
17136 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17137 * flags cannot be set. Check for that condition also.
17138 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017139 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017140 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017141 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017142 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017143 goto theend;
17144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017146 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017147 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017148 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017149 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017151 if (flags & SP_SUBPAT)
17152 retval = subpatnum;
17153 else
17154 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017155 if (flags & SP_SETPCMARK)
17156 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017158 if (match_pos != NULL)
17159 {
17160 /* Store the match cursor position */
17161 match_pos->lnum = pos.lnum;
17162 match_pos->col = pos.col + 1;
17163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 /* "/$" will put the cursor after the end of the line, may need to
17165 * correct that here */
17166 check_cursor();
17167 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017168
17169 /* If 'n' flag is used: restore cursor position. */
17170 if (flags & SP_NOMOVE)
17171 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017172 else
17173 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017174theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017176
17177 return retval;
17178}
17179
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017180#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017181
17182/*
17183 * round() is not in C90, use ceil() or floor() instead.
17184 */
17185 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017186vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017187{
17188 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17189}
17190
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017191/*
17192 * "round({float})" function
17193 */
17194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017195f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017196{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017197 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017198
17199 rettv->v_type = VAR_FLOAT;
17200 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017201 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017202 else
17203 rettv->vval.v_float = 0.0;
17204}
17205#endif
17206
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017207/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017208 * "screenattr()" function
17209 */
17210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017211f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017212{
17213 int row;
17214 int col;
17215 int c;
17216
17217 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17218 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17219 if (row < 0 || row >= screen_Rows
17220 || col < 0 || col >= screen_Columns)
17221 c = -1;
17222 else
17223 c = ScreenAttrs[LineOffset[row] + col];
17224 rettv->vval.v_number = c;
17225}
17226
17227/*
17228 * "screenchar()" function
17229 */
17230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017231f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017232{
17233 int row;
17234 int col;
17235 int off;
17236 int c;
17237
17238 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17239 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17240 if (row < 0 || row >= screen_Rows
17241 || col < 0 || col >= screen_Columns)
17242 c = -1;
17243 else
17244 {
17245 off = LineOffset[row] + col;
17246#ifdef FEAT_MBYTE
17247 if (enc_utf8 && ScreenLinesUC[off] != 0)
17248 c = ScreenLinesUC[off];
17249 else
17250#endif
17251 c = ScreenLines[off];
17252 }
17253 rettv->vval.v_number = c;
17254}
17255
17256/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017257 * "screencol()" function
17258 *
17259 * First column is 1 to be consistent with virtcol().
17260 */
17261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017262f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017263{
17264 rettv->vval.v_number = screen_screencol() + 1;
17265}
17266
17267/*
17268 * "screenrow()" function
17269 */
17270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017271f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017272{
17273 rettv->vval.v_number = screen_screenrow() + 1;
17274}
17275
17276/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017277 * "search()" function
17278 */
17279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017280f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017281{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017282 int flags = 0;
17283
17284 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285}
17286
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017288 * "searchdecl()" function
17289 */
17290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017291f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017292{
17293 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017294 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017295 int error = FALSE;
17296 char_u *name;
17297
17298 rettv->vval.v_number = 1; /* default: FAIL */
17299
17300 name = get_tv_string_chk(&argvars[0]);
17301 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017302 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017303 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017304 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17305 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17306 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017307 if (!error && name != NULL)
17308 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017309 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017310}
17311
17312/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017313 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017316searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317{
17318 char_u *spat, *mpat, *epat;
17319 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321 int dir;
17322 int flags = 0;
17323 char_u nbuf1[NUMBUFLEN];
17324 char_u nbuf2[NUMBUFLEN];
17325 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017326 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017327 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017328 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017331 spat = get_tv_string_chk(&argvars[0]);
17332 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17333 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17334 if (spat == NULL || mpat == NULL || epat == NULL)
17335 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 /* Handle the optional fourth argument: flags */
17338 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017339 if (dir == 0)
17340 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017341
17342 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017343 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17344 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017345 if ((flags & (SP_END | SP_SUBPAT)) != 0
17346 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017347 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017348 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017349 goto theend;
17350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017352 /* Using 'r' implies 'W', otherwise it doesn't work. */
17353 if (flags & SP_REPEAT)
17354 p_ws = FALSE;
17355
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017356 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017357 if (argvars[3].v_type == VAR_UNKNOWN
17358 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 skip = (char_u *)"";
17360 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017362 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017363 if (argvars[5].v_type != VAR_UNKNOWN)
17364 {
17365 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17366 if (lnum_stop < 0)
17367 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017368#ifdef FEAT_RELTIME
17369 if (argvars[6].v_type != VAR_UNKNOWN)
17370 {
17371 time_limit = get_tv_number_chk(&argvars[6], NULL);
17372 if (time_limit < 0)
17373 goto theend;
17374 }
17375#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017376 }
17377 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017378 if (skip == NULL)
17379 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017381 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017382 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017383
17384theend:
17385 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017386
17387 return retval;
17388}
17389
17390/*
17391 * "searchpair()" function
17392 */
17393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017394f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017395{
17396 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17397}
17398
17399/*
17400 * "searchpairpos()" function
17401 */
17402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017403f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017404{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017405 pos_T match_pos;
17406 int lnum = 0;
17407 int col = 0;
17408
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
17412 if (searchpair_cmn(argvars, &match_pos) > 0)
17413 {
17414 lnum = match_pos.lnum;
17415 col = match_pos.col;
17416 }
17417
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017418 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17419 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017420}
17421
17422/*
17423 * Search for a start/middle/end thing.
17424 * Used by searchpair(), see its documentation for the details.
17425 * Returns 0 or -1 for no match,
17426 */
17427 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017428do_searchpair(
17429 char_u *spat, /* start pattern */
17430 char_u *mpat, /* middle pattern */
17431 char_u *epat, /* end pattern */
17432 int dir, /* BACKWARD or FORWARD */
17433 char_u *skip, /* skip expression */
17434 int flags, /* SP_SETPCMARK and other SP_ values */
17435 pos_T *match_pos,
17436 linenr_T lnum_stop, /* stop at this line if not zero */
17437 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017438{
17439 char_u *save_cpo;
17440 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17441 long retval = 0;
17442 pos_T pos;
17443 pos_T firstpos;
17444 pos_T foundpos;
17445 pos_T save_cursor;
17446 pos_T save_pos;
17447 int n;
17448 int r;
17449 int nest = 1;
17450 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017451 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017452 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017453
17454 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17455 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017456 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017457
Bram Moolenaar76929292008-01-06 19:07:36 +000017458#ifdef FEAT_RELTIME
17459 /* Set the time limit, if there is one. */
17460 profile_setlimit(time_limit, &tm);
17461#endif
17462
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017463 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17464 * start/middle/end (pat3, for the top pair). */
17465 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17466 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17467 if (pat2 == NULL || pat3 == NULL)
17468 goto theend;
17469 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17470 if (*mpat == NUL)
17471 STRCPY(pat3, pat2);
17472 else
17473 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17474 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017475 if (flags & SP_START)
17476 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017477
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 save_cursor = curwin->w_cursor;
17479 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017480 clearpos(&firstpos);
17481 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 pat = pat3;
17483 for (;;)
17484 {
17485 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017486 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17488 /* didn't find it or found the first match again: FAIL */
17489 break;
17490
17491 if (firstpos.lnum == 0)
17492 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017493 if (equalpos(pos, foundpos))
17494 {
17495 /* Found the same position again. Can happen with a pattern that
17496 * has "\zs" at the end and searching backwards. Advance one
17497 * character and try again. */
17498 if (dir == BACKWARD)
17499 decl(&pos);
17500 else
17501 incl(&pos);
17502 }
17503 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017505 /* clear the start flag to avoid getting stuck here */
17506 options &= ~SEARCH_START;
17507
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 /* If the skip pattern matches, ignore this match. */
17509 if (*skip != NUL)
17510 {
17511 save_pos = curwin->w_cursor;
17512 curwin->w_cursor = pos;
17513 r = eval_to_bool(skip, &err, NULL, FALSE);
17514 curwin->w_cursor = save_pos;
17515 if (err)
17516 {
17517 /* Evaluating {skip} caused an error, break here. */
17518 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017519 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 break;
17521 }
17522 if (r)
17523 continue;
17524 }
17525
17526 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17527 {
17528 /* Found end when searching backwards or start when searching
17529 * forward: nested pair. */
17530 ++nest;
17531 pat = pat2; /* nested, don't search for middle */
17532 }
17533 else
17534 {
17535 /* Found end when searching forward or start when searching
17536 * backward: end of (nested) pair; or found middle in outer pair. */
17537 if (--nest == 1)
17538 pat = pat3; /* outer level, search for middle */
17539 }
17540
17541 if (nest == 0)
17542 {
17543 /* Found the match: return matchcount or line number. */
17544 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017545 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017547 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017548 if (flags & SP_SETPCMARK)
17549 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 curwin->w_cursor = pos;
17551 if (!(flags & SP_REPEAT))
17552 break;
17553 nest = 1; /* search for next unmatched */
17554 }
17555 }
17556
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017557 if (match_pos != NULL)
17558 {
17559 /* Store the match cursor position */
17560 match_pos->lnum = curwin->w_cursor.lnum;
17561 match_pos->col = curwin->w_cursor.col + 1;
17562 }
17563
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017565 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 curwin->w_cursor = save_cursor;
17567
17568theend:
17569 vim_free(pat2);
17570 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017571 if (p_cpo == empty_option)
17572 p_cpo = save_cpo;
17573 else
17574 /* Darn, evaluating the {skip} expression changed the value. */
17575 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017576
17577 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578}
17579
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017580/*
17581 * "searchpos()" function
17582 */
17583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017584f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017585{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017586 pos_T match_pos;
17587 int lnum = 0;
17588 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017589 int n;
17590 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017591
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017592 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017593 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017594
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017595 n = search_cmn(argvars, &match_pos, &flags);
17596 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017597 {
17598 lnum = match_pos.lnum;
17599 col = match_pos.col;
17600 }
17601
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017602 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17603 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017604 if (flags & SP_SUBPAT)
17605 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017606}
17607
Bram Moolenaar0d660222005-01-07 21:51:51 +000017608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017609f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017611#ifdef FEAT_CLIENTSERVER
17612 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017613 char_u *server = get_tv_string_chk(&argvars[0]);
17614 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615
Bram Moolenaar0d660222005-01-07 21:51:51 +000017616 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017617 if (server == NULL || reply == NULL)
17618 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017619 if (check_restricted() || check_secure())
17620 return;
17621# ifdef FEAT_X11
17622 if (check_connection() == FAIL)
17623 return;
17624# endif
17625
17626 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017628 EMSG(_("E258: Unable to send to client"));
17629 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017631 rettv->vval.v_number = 0;
17632#else
17633 rettv->vval.v_number = -1;
17634#endif
17635}
17636
Bram Moolenaar0d660222005-01-07 21:51:51 +000017637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017638f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017639{
17640 char_u *r = NULL;
17641
17642#ifdef FEAT_CLIENTSERVER
17643# ifdef WIN32
17644 r = serverGetVimNames();
17645# else
17646 make_connection();
17647 if (X_DISPLAY != NULL)
17648 r = serverGetVimNames(X_DISPLAY);
17649# endif
17650#endif
17651 rettv->v_type = VAR_STRING;
17652 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653}
17654
17655/*
17656 * "setbufvar()" function
17657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017659f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660{
17661 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017664 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 char_u nbuf[NUMBUFLEN];
17666
17667 if (check_restricted() || check_secure())
17668 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017669 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17670 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017671 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 varp = &argvars[2];
17673
17674 if (buf != NULL && varname != NULL && varp != NULL)
17675 {
17676 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678
17679 if (*varname == '&')
17680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017681 long numval;
17682 char_u *strval;
17683 int error = FALSE;
17684
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017686 numval = get_tv_number_chk(varp, &error);
17687 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017688 if (!error && strval != NULL)
17689 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 }
17691 else
17692 {
17693 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17694 if (bufvarname != NULL)
17695 {
17696 STRCPY(bufvarname, "b:");
17697 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017698 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 vim_free(bufvarname);
17700 }
17701 }
17702
17703 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706}
17707
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017709f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017710{
17711 dict_T *d;
17712 dictitem_T *di;
17713 char_u *csearch;
17714
17715 if (argvars[0].v_type != VAR_DICT)
17716 {
17717 EMSG(_(e_dictreq));
17718 return;
17719 }
17720
17721 if ((d = argvars[0].vval.v_dict) != NULL)
17722 {
17723 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17724 if (csearch != NULL)
17725 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017726#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017727 if (enc_utf8)
17728 {
17729 int pcc[MAX_MCO];
17730 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017731
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017732 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17733 }
17734 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017735#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017736 set_last_csearch(PTR2CHAR(csearch),
17737 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017738 }
17739
17740 di = dict_find(d, (char_u *)"forward", -1);
17741 if (di != NULL)
17742 set_csearch_direction(get_tv_number(&di->di_tv)
17743 ? FORWARD : BACKWARD);
17744
17745 di = dict_find(d, (char_u *)"until", -1);
17746 if (di != NULL)
17747 set_csearch_until(!!get_tv_number(&di->di_tv));
17748 }
17749}
17750
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751/*
17752 * "setcmdpos()" function
17753 */
17754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017755f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017757 int pos = (int)get_tv_number(&argvars[0]) - 1;
17758
17759 if (pos >= 0)
17760 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761}
17762
17763/*
17764 * "setline()" function
17765 */
17766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017767f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768{
17769 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017770 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017771 list_T *l = NULL;
17772 listitem_T *li = NULL;
17773 long added = 0;
17774 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017776 lnum = get_tv_lnum(&argvars[0]);
17777 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017779 l = argvars[1].vval.v_list;
17780 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017782 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017783 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017784
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017785 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017786 for (;;)
17787 {
17788 if (l != NULL)
17789 {
17790 /* list argument, get next string */
17791 if (li == NULL)
17792 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017793 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017794 li = li->li_next;
17795 }
17796
17797 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017798 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017799 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017800
17801 /* When coming here from Insert mode, sync undo, so that this can be
17802 * undone separately from what was previously inserted. */
17803 if (u_sync_once == 2)
17804 {
17805 u_sync_once = 1; /* notify that u_sync() was called */
17806 u_sync(TRUE);
17807 }
17808
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017809 if (lnum <= curbuf->b_ml.ml_line_count)
17810 {
17811 /* existing line, replace it */
17812 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17813 {
17814 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017815 if (lnum == curwin->w_cursor.lnum)
17816 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017817 rettv->vval.v_number = 0; /* OK */
17818 }
17819 }
17820 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17821 {
17822 /* lnum is one past the last line, append the line */
17823 ++added;
17824 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17825 rettv->vval.v_number = 0; /* OK */
17826 }
17827
17828 if (l == NULL) /* only one string argument */
17829 break;
17830 ++lnum;
17831 }
17832
17833 if (added > 0)
17834 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835}
17836
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017837static 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 +000017838
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017840 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017841 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017843set_qf_ll_list(
17844 win_T *wp UNUSED,
17845 typval_T *list_arg UNUSED,
17846 typval_T *action_arg UNUSED,
17847 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017848{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017849#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017850 char_u *act;
17851 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017852#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017853
Bram Moolenaar2641f772005-03-25 21:58:17 +000017854 rettv->vval.v_number = -1;
17855
17856#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017857 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017858 EMSG(_(e_listreq));
17859 else
17860 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017861 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017862
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017863 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017864 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017865 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017866 if (act == NULL)
17867 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017868 if (*act == 'a' || *act == 'r')
17869 action = *act;
17870 }
17871
Bram Moolenaar81484f42012-12-05 15:16:47 +010017872 if (l != NULL && set_errorlist(wp, l, action,
17873 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017874 rettv->vval.v_number = 0;
17875 }
17876#endif
17877}
17878
17879/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017880 * "setloclist()" function
17881 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017883f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017884{
17885 win_T *win;
17886
17887 rettv->vval.v_number = -1;
17888
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017889 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017890 if (win != NULL)
17891 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17892}
17893
17894/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017895 * "setmatches()" function
17896 */
17897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017898f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017899{
17900#ifdef FEAT_SEARCH_EXTRA
17901 list_T *l;
17902 listitem_T *li;
17903 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017904 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017905
17906 rettv->vval.v_number = -1;
17907 if (argvars[0].v_type != VAR_LIST)
17908 {
17909 EMSG(_(e_listreq));
17910 return;
17911 }
17912 if ((l = argvars[0].vval.v_list) != NULL)
17913 {
17914
17915 /* To some extent make sure that we are dealing with a list from
17916 * "getmatches()". */
17917 li = l->lv_first;
17918 while (li != NULL)
17919 {
17920 if (li->li_tv.v_type != VAR_DICT
17921 || (d = li->li_tv.vval.v_dict) == NULL)
17922 {
17923 EMSG(_(e_invarg));
17924 return;
17925 }
17926 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017927 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17928 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017929 && dict_find(d, (char_u *)"priority", -1) != NULL
17930 && dict_find(d, (char_u *)"id", -1) != NULL))
17931 {
17932 EMSG(_(e_invarg));
17933 return;
17934 }
17935 li = li->li_next;
17936 }
17937
17938 clear_matches(curwin);
17939 li = l->lv_first;
17940 while (li != NULL)
17941 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017942 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017943 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017944 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017945 char_u *group;
17946 int priority;
17947 int id;
17948 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017949
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017950 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017951 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17952 {
17953 if (s == NULL)
17954 {
17955 s = list_alloc();
17956 if (s == NULL)
17957 return;
17958 }
17959
17960 /* match from matchaddpos() */
17961 for (i = 1; i < 9; i++)
17962 {
17963 sprintf((char *)buf, (char *)"pos%d", i);
17964 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17965 {
17966 if (di->di_tv.v_type != VAR_LIST)
17967 return;
17968
17969 list_append_tv(s, &di->di_tv);
17970 s->lv_refcount++;
17971 }
17972 else
17973 break;
17974 }
17975 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017976
17977 group = get_dict_string(d, (char_u *)"group", FALSE);
17978 priority = (int)get_dict_number(d, (char_u *)"priority");
17979 id = (int)get_dict_number(d, (char_u *)"id");
17980 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17981 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17982 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017983 if (i == 0)
17984 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017985 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017986 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017987 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017988 }
17989 else
17990 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017991 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017992 list_unref(s);
17993 s = NULL;
17994 }
17995
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017996 li = li->li_next;
17997 }
17998 rettv->vval.v_number = 0;
17999 }
18000#endif
18001}
18002
18003/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018004 * "setpos()" function
18005 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018007f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018008{
18009 pos_T pos;
18010 int fnum;
18011 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018012 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018013
Bram Moolenaar08250432008-02-13 11:42:46 +000018014 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018015 name = get_tv_string_chk(argvars);
18016 if (name != NULL)
18017 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018018 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018019 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018020 if (--pos.col < 0)
18021 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018022 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018023 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018024 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018025 if (fnum == curbuf->b_fnum)
18026 {
18027 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018028 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018029 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018030 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018031 curwin->w_set_curswant = FALSE;
18032 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018033 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018034 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018035 }
18036 else
18037 EMSG(_(e_invarg));
18038 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018039 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18040 {
18041 /* set mark */
18042 if (setmark_pos(name[1], &pos, fnum) == OK)
18043 rettv->vval.v_number = 0;
18044 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018045 else
18046 EMSG(_(e_invarg));
18047 }
18048 }
18049}
18050
18051/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018052 * "setqflist()" function
18053 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018055f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018056{
18057 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18058}
18059
18060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 * "setreg()" function
18062 */
18063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018064f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065{
18066 int regname;
18067 char_u *strregname;
18068 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018069 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 int append;
18071 char_u yank_type;
18072 long block_len;
18073
18074 block_len = -1;
18075 yank_type = MAUTO;
18076 append = FALSE;
18077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018078 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018079 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018081 if (strregname == NULL)
18082 return; /* type error; errmsg already given */
18083 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 if (regname == 0 || regname == '@')
18085 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018087 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018089 stropt = get_tv_string_chk(&argvars[2]);
18090 if (stropt == NULL)
18091 return; /* type error */
18092 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 switch (*stropt)
18094 {
18095 case 'a': case 'A': /* append */
18096 append = TRUE;
18097 break;
18098 case 'v': case 'c': /* character-wise selection */
18099 yank_type = MCHAR;
18100 break;
18101 case 'V': case 'l': /* line-wise selection */
18102 yank_type = MLINE;
18103 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 case 'b': case Ctrl_V: /* block-wise selection */
18105 yank_type = MBLOCK;
18106 if (VIM_ISDIGIT(stropt[1]))
18107 {
18108 ++stropt;
18109 block_len = getdigits(&stropt) - 1;
18110 --stropt;
18111 }
18112 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 }
18114 }
18115
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018116 if (argvars[1].v_type == VAR_LIST)
18117 {
18118 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018119 char_u **allocval;
18120 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018121 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018122 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018123 int len = argvars[1].vval.v_list->lv_len;
18124 listitem_T *li;
18125
Bram Moolenaar7d647822014-04-05 21:28:56 +020018126 /* First half: use for pointers to result lines; second half: use for
18127 * pointers to allocated copies. */
18128 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018129 if (lstval == NULL)
18130 return;
18131 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018132 allocval = lstval + len + 2;
18133 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018134
18135 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18136 li = li->li_next)
18137 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018138 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018139 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018140 goto free_lstval;
18141 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018142 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018143 /* Need to make a copy, next get_tv_string_buf_chk() will
18144 * overwrite the string. */
18145 strval = vim_strsave(buf);
18146 if (strval == NULL)
18147 goto free_lstval;
18148 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018149 }
18150 *curval++ = strval;
18151 }
18152 *curval++ = NULL;
18153
18154 write_reg_contents_lst(regname, lstval, -1,
18155 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018156free_lstval:
18157 while (curallocval > allocval)
18158 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018159 vim_free(lstval);
18160 }
18161 else
18162 {
18163 strval = get_tv_string_chk(&argvars[1]);
18164 if (strval == NULL)
18165 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018166 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018168 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018169 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170}
18171
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018172/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018173 * "settabvar()" function
18174 */
18175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018176f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018177{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018178#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018179 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018180 tabpage_T *tp;
18181#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018182 char_u *varname, *tabvarname;
18183 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018184
18185 rettv->vval.v_number = 0;
18186
18187 if (check_restricted() || check_secure())
18188 return;
18189
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018190#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018191 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018192#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018193 varname = get_tv_string_chk(&argvars[1]);
18194 varp = &argvars[2];
18195
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018196 if (varname != NULL && varp != NULL
18197#ifdef FEAT_WINDOWS
18198 && tp != NULL
18199#endif
18200 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018201 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018202#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018203 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018204 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018205#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018206
18207 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18208 if (tabvarname != NULL)
18209 {
18210 STRCPY(tabvarname, "t:");
18211 STRCPY(tabvarname + 2, varname);
18212 set_var(tabvarname, varp, TRUE);
18213 vim_free(tabvarname);
18214 }
18215
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018216#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018217 /* Restore current tabpage */
18218 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018219 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018220#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018221 }
18222}
18223
18224/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018225 * "settabwinvar()" function
18226 */
18227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018228f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018229{
18230 setwinvar(argvars, rettv, 1);
18231}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232
18233/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018234 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018237f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018239 setwinvar(argvars, rettv, 0);
18240}
18241
18242/*
18243 * "setwinvar()" and "settabwinvar()" functions
18244 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018245
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018247setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018248{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 win_T *win;
18250#ifdef FEAT_WINDOWS
18251 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018252 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018253 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254#endif
18255 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018256 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018258 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259
18260 if (check_restricted() || check_secure())
18261 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018262
18263#ifdef FEAT_WINDOWS
18264 if (off == 1)
18265 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18266 else
18267 tp = curtab;
18268#endif
18269 win = find_win_by_nr(&argvars[off], tp);
18270 varname = get_tv_string_chk(&argvars[off + 1]);
18271 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272
18273 if (win != NULL && varname != NULL && varp != NULL)
18274 {
18275#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018276 need_switch_win = !(tp == curtab && win == curwin);
18277 if (!need_switch_win
18278 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018281 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018283 long numval;
18284 char_u *strval;
18285 int error = FALSE;
18286
18287 ++varname;
18288 numval = get_tv_number_chk(varp, &error);
18289 strval = get_tv_string_buf_chk(varp, nbuf);
18290 if (!error && strval != NULL)
18291 set_option_value(varname, numval, strval, OPT_LOCAL);
18292 }
18293 else
18294 {
18295 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18296 if (winvarname != NULL)
18297 {
18298 STRCPY(winvarname, "w:");
18299 STRCPY(winvarname + 2, varname);
18300 set_var(winvarname, varp, TRUE);
18301 vim_free(winvarname);
18302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 }
18304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018306 if (need_switch_win)
18307 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308#endif
18309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310}
18311
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018312#ifdef FEAT_CRYPT
18313/*
18314 * "sha256({string})" function
18315 */
18316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018317f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018318{
18319 char_u *p;
18320
18321 p = get_tv_string(&argvars[0]);
18322 rettv->vval.v_string = vim_strsave(
18323 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18324 rettv->v_type = VAR_STRING;
18325}
18326#endif /* FEAT_CRYPT */
18327
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018329 * "shellescape({string})" function
18330 */
18331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018332f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018333{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018334 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018335 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018336 rettv->v_type = VAR_STRING;
18337}
18338
18339/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018340 * shiftwidth() function
18341 */
18342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018343f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018344{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018345 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018346}
18347
18348/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018349 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 */
18351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018352f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018354 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355
Bram Moolenaar0d660222005-01-07 21:51:51 +000018356 p = get_tv_string(&argvars[0]);
18357 rettv->vval.v_string = vim_strsave(p);
18358 simplify_filename(rettv->vval.v_string); /* simplify in place */
18359 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360}
18361
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018362#ifdef FEAT_FLOAT
18363/*
18364 * "sin()" function
18365 */
18366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018367f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018368{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018369 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018370
18371 rettv->v_type = VAR_FLOAT;
18372 if (get_float_arg(argvars, &f) == OK)
18373 rettv->vval.v_float = sin(f);
18374 else
18375 rettv->vval.v_float = 0.0;
18376}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018377
18378/*
18379 * "sinh()" function
18380 */
18381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018382f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018383{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018384 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018385
18386 rettv->v_type = VAR_FLOAT;
18387 if (get_float_arg(argvars, &f) == OK)
18388 rettv->vval.v_float = sinh(f);
18389 else
18390 rettv->vval.v_float = 0.0;
18391}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018392#endif
18393
Bram Moolenaar0d660222005-01-07 21:51:51 +000018394static int
18395#ifdef __BORLANDC__
18396 _RTLENTRYF
18397#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018398 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018399static int
18400#ifdef __BORLANDC__
18401 _RTLENTRYF
18402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018403 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018404
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018405/* struct used in the array that's given to qsort() */
18406typedef struct
18407{
18408 listitem_T *item;
18409 int idx;
18410} sortItem_T;
18411
Bram Moolenaar0d660222005-01-07 21:51:51 +000018412static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018413static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018414static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018415#ifdef FEAT_FLOAT
18416static int item_compare_float;
18417#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018418static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018419static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018420static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018421static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018422static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018423#define ITEM_COMPARE_FAIL 999
18424
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018426 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018428 static int
18429#ifdef __BORLANDC__
18430_RTLENTRYF
18431#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018432item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018434 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018435 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018436 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018437 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018438 int res;
18439 char_u numbuf1[NUMBUFLEN];
18440 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018442 si1 = (sortItem_T *)s1;
18443 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018444 tv1 = &si1->item->li_tv;
18445 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018446
18447 if (item_compare_numbers)
18448 {
18449 long v1 = get_tv_number(tv1);
18450 long v2 = get_tv_number(tv2);
18451
18452 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18453 }
18454
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018455#ifdef FEAT_FLOAT
18456 if (item_compare_float)
18457 {
18458 float_T v1 = get_tv_float(tv1);
18459 float_T v2 = get_tv_float(tv2);
18460
18461 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18462 }
18463#endif
18464
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018465 /* tv2string() puts quotes around a string and allocates memory. Don't do
18466 * that for string variables. Use a single quote when comparing with a
18467 * non-string to do what the docs promise. */
18468 if (tv1->v_type == VAR_STRING)
18469 {
18470 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18471 p1 = (char_u *)"'";
18472 else
18473 p1 = tv1->vval.v_string;
18474 }
18475 else
18476 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18477 if (tv2->v_type == VAR_STRING)
18478 {
18479 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18480 p2 = (char_u *)"'";
18481 else
18482 p2 = tv2->vval.v_string;
18483 }
18484 else
18485 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018486 if (p1 == NULL)
18487 p1 = (char_u *)"";
18488 if (p2 == NULL)
18489 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018490 if (!item_compare_numeric)
18491 {
18492 if (item_compare_ic)
18493 res = STRICMP(p1, p2);
18494 else
18495 res = STRCMP(p1, p2);
18496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018498 {
18499 double n1, n2;
18500 n1 = strtod((char *)p1, (char **)&p1);
18501 n2 = strtod((char *)p2, (char **)&p2);
18502 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18503 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018504
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018505 /* When the result would be zero, compare the item indexes. Makes the
18506 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018507 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018508 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018509
Bram Moolenaar0d660222005-01-07 21:51:51 +000018510 vim_free(tofree1);
18511 vim_free(tofree2);
18512 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513}
18514
18515 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018516#ifdef __BORLANDC__
18517_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018519item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018520{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018521 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018522 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018523 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018524 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018525 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018527 /* shortcut after failure in previous call; compare all items equal */
18528 if (item_compare_func_err)
18529 return 0;
18530
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018531 si1 = (sortItem_T *)s1;
18532 si2 = (sortItem_T *)s2;
18533
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018534 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018535 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018536 copy_tv(&si1->item->li_tv, &argv[0]);
18537 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018538
18539 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018540 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018541 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18542 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018543 clear_tv(&argv[0]);
18544 clear_tv(&argv[1]);
18545
18546 if (res == FAIL)
18547 res = ITEM_COMPARE_FAIL;
18548 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018549 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18550 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018551 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018552 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018553
18554 /* When the result would be zero, compare the pointers themselves. Makes
18555 * the sort stable. */
18556 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018557 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018558
Bram Moolenaar0d660222005-01-07 21:51:51 +000018559 return res;
18560}
18561
18562/*
18563 * "sort({list})" function
18564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018566do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567{
Bram Moolenaar33570922005-01-25 22:26:29 +000018568 list_T *l;
18569 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018570 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018571 long len;
18572 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573
Bram Moolenaar0d660222005-01-07 21:51:51 +000018574 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018575 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 else
18577 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018578 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018579 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018580 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18581 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018582 return;
18583 rettv->vval.v_list = l;
18584 rettv->v_type = VAR_LIST;
18585 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586
Bram Moolenaar0d660222005-01-07 21:51:51 +000018587 len = list_len(l);
18588 if (len <= 1)
18589 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590
Bram Moolenaar0d660222005-01-07 21:51:51 +000018591 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018592 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018593 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018594#ifdef FEAT_FLOAT
18595 item_compare_float = FALSE;
18596#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018597 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018598 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018599 if (argvars[1].v_type != VAR_UNKNOWN)
18600 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018601 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018602 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018603 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018604 else
18605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018606 int error = FALSE;
18607
18608 i = get_tv_number_chk(&argvars[1], &error);
18609 if (error)
18610 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018611 if (i == 1)
18612 item_compare_ic = TRUE;
18613 else
18614 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018615 if (item_compare_func != NULL)
18616 {
18617 if (STRCMP(item_compare_func, "n") == 0)
18618 {
18619 item_compare_func = NULL;
18620 item_compare_numeric = TRUE;
18621 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018622 else if (STRCMP(item_compare_func, "N") == 0)
18623 {
18624 item_compare_func = NULL;
18625 item_compare_numbers = TRUE;
18626 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018627#ifdef FEAT_FLOAT
18628 else if (STRCMP(item_compare_func, "f") == 0)
18629 {
18630 item_compare_func = NULL;
18631 item_compare_float = TRUE;
18632 }
18633#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018634 else if (STRCMP(item_compare_func, "i") == 0)
18635 {
18636 item_compare_func = NULL;
18637 item_compare_ic = TRUE;
18638 }
18639 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018640 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018641
18642 if (argvars[2].v_type != VAR_UNKNOWN)
18643 {
18644 /* optional third argument: {dict} */
18645 if (argvars[2].v_type != VAR_DICT)
18646 {
18647 EMSG(_(e_dictreq));
18648 return;
18649 }
18650 item_compare_selfdict = argvars[2].vval.v_dict;
18651 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018655 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018656 if (ptrs == NULL)
18657 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658
Bram Moolenaar327aa022014-03-25 18:24:23 +010018659 i = 0;
18660 if (sort)
18661 {
18662 /* sort(): ptrs will be the list to sort */
18663 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018664 {
18665 ptrs[i].item = li;
18666 ptrs[i].idx = i;
18667 ++i;
18668 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018669
18670 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018671 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018672 /* test the compare function */
18673 if (item_compare_func != NULL
18674 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018675 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018676 EMSG(_("E702: Sort compare function failed"));
18677 else
18678 {
18679 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018680 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018681 item_compare_func == NULL ? item_compare : item_compare2);
18682
18683 if (!item_compare_func_err)
18684 {
18685 /* Clear the List and append the items in sorted order. */
18686 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18687 l->lv_len = 0;
18688 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018689 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018690 }
18691 }
18692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018694 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018695 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018696
18697 /* f_uniq(): ptrs will be a stack of items to remove */
18698 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018699 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018700 item_compare_func_ptr = item_compare_func
18701 ? item_compare2 : item_compare;
18702
18703 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18704 li = li->li_next)
18705 {
18706 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18707 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018708 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018709 if (item_compare_func_err)
18710 {
18711 EMSG(_("E882: Uniq compare function failed"));
18712 break;
18713 }
18714 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018716 if (!item_compare_func_err)
18717 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018718 while (--i >= 0)
18719 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018720 li = ptrs[i].item->li_next;
18721 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018722 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018723 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018724 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018725 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018726 list_fix_watch(l, li);
18727 listitem_free(li);
18728 l->lv_len--;
18729 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018730 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018731 }
18732
18733 vim_free(ptrs);
18734 }
18735}
18736
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018737/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018738 * "sort({list})" function
18739 */
18740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018741f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018742{
18743 do_sort_uniq(argvars, rettv, TRUE);
18744}
18745
18746/*
18747 * "uniq({list})" function
18748 */
18749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018750f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018751{
18752 do_sort_uniq(argvars, rettv, FALSE);
18753}
18754
18755/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018756 * "soundfold({word})" function
18757 */
18758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018759f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018760{
18761 char_u *s;
18762
18763 rettv->v_type = VAR_STRING;
18764 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018765#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018766 rettv->vval.v_string = eval_soundfold(s);
18767#else
18768 rettv->vval.v_string = vim_strsave(s);
18769#endif
18770}
18771
18772/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018773 * "spellbadword()" function
18774 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018776f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018777{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018778 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018779 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018780 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018781
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018782 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018783 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018784
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018785#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018786 if (argvars[0].v_type == VAR_UNKNOWN)
18787 {
18788 /* Find the start and length of the badly spelled word. */
18789 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18790 if (len != 0)
18791 word = ml_get_cursor();
18792 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018793 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018794 {
18795 char_u *str = get_tv_string_chk(&argvars[0]);
18796 int capcol = -1;
18797
18798 if (str != NULL)
18799 {
18800 /* Check the argument for spelling. */
18801 while (*str != NUL)
18802 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018803 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018804 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018805 {
18806 word = str;
18807 break;
18808 }
18809 str += len;
18810 }
18811 }
18812 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018813#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018814
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018815 list_append_string(rettv->vval.v_list, word, len);
18816 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018817 attr == HLF_SPB ? "bad" :
18818 attr == HLF_SPR ? "rare" :
18819 attr == HLF_SPL ? "local" :
18820 attr == HLF_SPC ? "caps" :
18821 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018822}
18823
18824/*
18825 * "spellsuggest()" function
18826 */
18827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018828f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018829{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018830#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018831 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018832 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018833 int maxcount;
18834 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018835 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018836 listitem_T *li;
18837 int need_capital = FALSE;
18838#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018839
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018840 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018841 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018842
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018843#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018844 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018845 {
18846 str = get_tv_string(&argvars[0]);
18847 if (argvars[1].v_type != VAR_UNKNOWN)
18848 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018849 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018850 if (maxcount <= 0)
18851 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018852 if (argvars[2].v_type != VAR_UNKNOWN)
18853 {
18854 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18855 if (typeerr)
18856 return;
18857 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018858 }
18859 else
18860 maxcount = 25;
18861
Bram Moolenaar4770d092006-01-12 23:22:24 +000018862 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018863
18864 for (i = 0; i < ga.ga_len; ++i)
18865 {
18866 str = ((char_u **)ga.ga_data)[i];
18867
18868 li = listitem_alloc();
18869 if (li == NULL)
18870 vim_free(str);
18871 else
18872 {
18873 li->li_tv.v_type = VAR_STRING;
18874 li->li_tv.v_lock = 0;
18875 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018876 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018877 }
18878 }
18879 ga_clear(&ga);
18880 }
18881#endif
18882}
18883
Bram Moolenaar0d660222005-01-07 21:51:51 +000018884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018885f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018886{
18887 char_u *str;
18888 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018889 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018890 regmatch_T regmatch;
18891 char_u patbuf[NUMBUFLEN];
18892 char_u *save_cpo;
18893 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018894 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018895 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018896 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018897
18898 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18899 save_cpo = p_cpo;
18900 p_cpo = (char_u *)"";
18901
18902 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018903 if (argvars[1].v_type != VAR_UNKNOWN)
18904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018905 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18906 if (pat == NULL)
18907 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018908 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018909 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018910 }
18911 if (pat == NULL || *pat == NUL)
18912 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018913
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018914 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018916 if (typeerr)
18917 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918
Bram Moolenaar0d660222005-01-07 21:51:51 +000018919 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18920 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018922 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018923 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018924 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018925 if (*str == NUL)
18926 match = FALSE; /* empty item at the end */
18927 else
18928 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929 if (match)
18930 end = regmatch.startp[0];
18931 else
18932 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018933 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18934 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018935 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018936 if (list_append_string(rettv->vval.v_list, str,
18937 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018938 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018939 }
18940 if (!match)
18941 break;
18942 /* Advance to just after the match. */
18943 if (regmatch.endp[0] > str)
18944 col = 0;
18945 else
18946 {
18947 /* Don't get stuck at the same match. */
18948#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018949 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018950#else
18951 col = 1;
18952#endif
18953 }
18954 str = regmatch.endp[0];
18955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956
Bram Moolenaar473de612013-06-08 18:19:48 +020018957 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959
Bram Moolenaar0d660222005-01-07 21:51:51 +000018960 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961}
18962
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018963#ifdef FEAT_FLOAT
18964/*
18965 * "sqrt()" function
18966 */
18967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018968f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018969{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018970 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018971
18972 rettv->v_type = VAR_FLOAT;
18973 if (get_float_arg(argvars, &f) == OK)
18974 rettv->vval.v_float = sqrt(f);
18975 else
18976 rettv->vval.v_float = 0.0;
18977}
18978
18979/*
18980 * "str2float()" function
18981 */
18982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018983f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018984{
18985 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18986
18987 if (*p == '+')
18988 p = skipwhite(p + 1);
18989 (void)string2float(p, &rettv->vval.v_float);
18990 rettv->v_type = VAR_FLOAT;
18991}
18992#endif
18993
Bram Moolenaar2c932302006-03-18 21:42:09 +000018994/*
18995 * "str2nr()" function
18996 */
18997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018998f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018999{
19000 int base = 10;
19001 char_u *p;
19002 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019003 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019004
19005 if (argvars[1].v_type != VAR_UNKNOWN)
19006 {
19007 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019008 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019009 {
19010 EMSG(_(e_invarg));
19011 return;
19012 }
19013 }
19014
19015 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019016 if (*p == '+')
19017 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019018 switch (base)
19019 {
19020 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19021 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19022 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19023 default: what = 0;
19024 }
19025 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019026 rettv->vval.v_number = n;
19027}
19028
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029#ifdef HAVE_STRFTIME
19030/*
19031 * "strftime({format}[, {time}])" function
19032 */
19033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019034f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035{
19036 char_u result_buf[256];
19037 struct tm *curtime;
19038 time_t seconds;
19039 char_u *p;
19040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019041 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019043 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019044 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045 seconds = time(NULL);
19046 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019047 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 curtime = localtime(&seconds);
19049 /* MSVC returns NULL for an invalid value of seconds. */
19050 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019051 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052 else
19053 {
19054# ifdef FEAT_MBYTE
19055 vimconv_T conv;
19056 char_u *enc;
19057
19058 conv.vc_type = CONV_NONE;
19059 enc = enc_locale();
19060 convert_setup(&conv, p_enc, enc);
19061 if (conv.vc_type != CONV_NONE)
19062 p = string_convert(&conv, p, NULL);
19063# endif
19064 if (p != NULL)
19065 (void)strftime((char *)result_buf, sizeof(result_buf),
19066 (char *)p, curtime);
19067 else
19068 result_buf[0] = NUL;
19069
19070# ifdef FEAT_MBYTE
19071 if (conv.vc_type != CONV_NONE)
19072 vim_free(p);
19073 convert_setup(&conv, enc, p_enc);
19074 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019075 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 else
19077# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019078 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079
19080# ifdef FEAT_MBYTE
19081 /* Release conversion descriptors */
19082 convert_setup(&conv, NULL, NULL);
19083 vim_free(enc);
19084# endif
19085 }
19086}
19087#endif
19088
19089/*
19090 * "stridx()" function
19091 */
19092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019093f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094{
19095 char_u buf[NUMBUFLEN];
19096 char_u *needle;
19097 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019098 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019100 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019102 needle = get_tv_string_chk(&argvars[1]);
19103 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019104 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019105 if (needle == NULL || haystack == NULL)
19106 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107
Bram Moolenaar33570922005-01-25 22:26:29 +000019108 if (argvars[2].v_type != VAR_UNKNOWN)
19109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019110 int error = FALSE;
19111
19112 start_idx = get_tv_number_chk(&argvars[2], &error);
19113 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019114 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019115 if (start_idx >= 0)
19116 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019117 }
19118
19119 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19120 if (pos != NULL)
19121 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122}
19123
19124/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019125 * "string()" function
19126 */
19127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019128f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019129{
19130 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019131 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019133 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019134 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019135 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019136 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019137 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138}
19139
19140/*
19141 * "strlen()" function
19142 */
19143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019144f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019146 rettv->vval.v_number = (varnumber_T)(STRLEN(
19147 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148}
19149
19150/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019151 * "strchars()" function
19152 */
19153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019154f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019155{
19156 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019157 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019158#ifdef FEAT_MBYTE
19159 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019160 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019161#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019162
19163 if (argvars[1].v_type != VAR_UNKNOWN)
19164 skipcc = get_tv_number_chk(&argvars[1], NULL);
19165 if (skipcc < 0 || skipcc > 1)
19166 EMSG(_(e_invarg));
19167 else
19168 {
19169#ifdef FEAT_MBYTE
19170 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19171 while (*s != NUL)
19172 {
19173 func_mb_ptr2char_adv(&s);
19174 ++len;
19175 }
19176 rettv->vval.v_number = len;
19177#else
19178 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19179#endif
19180 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019181}
19182
19183/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019184 * "strdisplaywidth()" function
19185 */
19186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019187f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019188{
19189 char_u *s = get_tv_string(&argvars[0]);
19190 int col = 0;
19191
19192 if (argvars[1].v_type != VAR_UNKNOWN)
19193 col = get_tv_number(&argvars[1]);
19194
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019195 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019196}
19197
19198/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019199 * "strwidth()" function
19200 */
19201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019202f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019203{
19204 char_u *s = get_tv_string(&argvars[0]);
19205
19206 rettv->vval.v_number = (varnumber_T)(
19207#ifdef FEAT_MBYTE
19208 mb_string2cells(s, -1)
19209#else
19210 STRLEN(s)
19211#endif
19212 );
19213}
19214
19215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 * "strpart()" function
19217 */
19218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019219f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220{
19221 char_u *p;
19222 int n;
19223 int len;
19224 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019225 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019227 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 slen = (int)STRLEN(p);
19229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019230 n = get_tv_number_chk(&argvars[1], &error);
19231 if (error)
19232 len = 0;
19233 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019234 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 else
19236 len = slen - n; /* default len: all bytes that are available. */
19237
19238 /*
19239 * Only return the overlap between the specified part and the actual
19240 * string.
19241 */
19242 if (n < 0)
19243 {
19244 len += n;
19245 n = 0;
19246 }
19247 else if (n > slen)
19248 n = slen;
19249 if (len < 0)
19250 len = 0;
19251 else if (n + len > slen)
19252 len = slen - n;
19253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019254 rettv->v_type = VAR_STRING;
19255 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256}
19257
19258/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019259 * "strridx()" function
19260 */
19261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019262f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019263{
19264 char_u buf[NUMBUFLEN];
19265 char_u *needle;
19266 char_u *haystack;
19267 char_u *rest;
19268 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019269 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019271 needle = get_tv_string_chk(&argvars[1]);
19272 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019273
19274 rettv->vval.v_number = -1;
19275 if (needle == NULL || haystack == NULL)
19276 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019277
19278 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019279 if (argvars[2].v_type != VAR_UNKNOWN)
19280 {
19281 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019282 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019283 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019284 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019285 }
19286 else
19287 end_idx = haystack_len;
19288
Bram Moolenaar0d660222005-01-07 21:51:51 +000019289 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019290 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019291 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019292 lastmatch = haystack + end_idx;
19293 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019294 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019295 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019296 for (rest = haystack; *rest != '\0'; ++rest)
19297 {
19298 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019299 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019300 break;
19301 lastmatch = rest;
19302 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019303 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019304
19305 if (lastmatch == NULL)
19306 rettv->vval.v_number = -1;
19307 else
19308 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19309}
19310
19311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312 * "strtrans()" function
19313 */
19314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019315f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019317 rettv->v_type = VAR_STRING;
19318 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319}
19320
19321/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019322 * "submatch()" function
19323 */
19324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019325f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019326{
Bram Moolenaar41571762014-04-02 19:00:58 +020019327 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019328 int no;
19329 int retList = 0;
19330
19331 no = (int)get_tv_number_chk(&argvars[0], &error);
19332 if (error)
19333 return;
19334 error = FALSE;
19335 if (argvars[1].v_type != VAR_UNKNOWN)
19336 retList = get_tv_number_chk(&argvars[1], &error);
19337 if (error)
19338 return;
19339
19340 if (retList == 0)
19341 {
19342 rettv->v_type = VAR_STRING;
19343 rettv->vval.v_string = reg_submatch(no);
19344 }
19345 else
19346 {
19347 rettv->v_type = VAR_LIST;
19348 rettv->vval.v_list = reg_submatch_list(no);
19349 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019350}
19351
19352/*
19353 * "substitute()" function
19354 */
19355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019356f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019357{
19358 char_u patbuf[NUMBUFLEN];
19359 char_u subbuf[NUMBUFLEN];
19360 char_u flagsbuf[NUMBUFLEN];
19361
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019362 char_u *str = get_tv_string_chk(&argvars[0]);
19363 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19364 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19365 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19366
Bram Moolenaar0d660222005-01-07 21:51:51 +000019367 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019368 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19369 rettv->vval.v_string = NULL;
19370 else
19371 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019372}
19373
19374/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019375 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019378f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379{
19380 int id = 0;
19381#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019382 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 long col;
19384 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019385 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019387 lnum = get_tv_lnum(argvars); /* -1 on type error */
19388 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19389 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019391 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019392 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019393 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394#endif
19395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019396 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397}
19398
19399/*
19400 * "synIDattr(id, what [, mode])" function
19401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019403f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404{
19405 char_u *p = NULL;
19406#ifdef FEAT_SYN_HL
19407 int id;
19408 char_u *what;
19409 char_u *mode;
19410 char_u modebuf[NUMBUFLEN];
19411 int modec;
19412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019413 id = get_tv_number(&argvars[0]);
19414 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019415 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019417 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019418 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019419 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 modec = 0; /* replace invalid with current */
19421 }
19422 else
19423 {
19424#ifdef FEAT_GUI
19425 if (gui.in_use)
19426 modec = 'g';
19427 else
19428#endif
19429 if (t_colors > 1)
19430 modec = 'c';
19431 else
19432 modec = 't';
19433 }
19434
19435
19436 switch (TOLOWER_ASC(what[0]))
19437 {
19438 case 'b':
19439 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19440 p = highlight_color(id, what, modec);
19441 else /* bold */
19442 p = highlight_has_attr(id, HL_BOLD, modec);
19443 break;
19444
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019445 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 p = highlight_color(id, what, modec);
19447 break;
19448
19449 case 'i':
19450 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19451 p = highlight_has_attr(id, HL_INVERSE, modec);
19452 else /* italic */
19453 p = highlight_has_attr(id, HL_ITALIC, modec);
19454 break;
19455
19456 case 'n': /* name */
19457 p = get_highlight_name(NULL, id - 1);
19458 break;
19459
19460 case 'r': /* reverse */
19461 p = highlight_has_attr(id, HL_INVERSE, modec);
19462 break;
19463
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019464 case 's':
19465 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19466 p = highlight_color(id, what, modec);
19467 else /* standout */
19468 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 break;
19470
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019471 case 'u':
19472 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19473 /* underline */
19474 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19475 else
19476 /* undercurl */
19477 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 break;
19479 }
19480
19481 if (p != NULL)
19482 p = vim_strsave(p);
19483#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019484 rettv->v_type = VAR_STRING;
19485 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486}
19487
19488/*
19489 * "synIDtrans(id)" function
19490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019492f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493{
19494 int id;
19495
19496#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019497 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498
19499 if (id > 0)
19500 id = syn_get_final_id(id);
19501 else
19502#endif
19503 id = 0;
19504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019505 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506}
19507
19508/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019509 * "synconcealed(lnum, col)" function
19510 */
19511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019512f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019513{
19514#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19515 long lnum;
19516 long col;
19517 int syntax_flags = 0;
19518 int cchar;
19519 int matchid = 0;
19520 char_u str[NUMBUFLEN];
19521#endif
19522
19523 rettv->v_type = VAR_LIST;
19524 rettv->vval.v_list = NULL;
19525
19526#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19527 lnum = get_tv_lnum(argvars); /* -1 on type error */
19528 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19529
19530 vim_memset(str, NUL, sizeof(str));
19531
19532 if (rettv_list_alloc(rettv) != FAIL)
19533 {
19534 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19535 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19536 && curwin->w_p_cole > 0)
19537 {
19538 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19539 syntax_flags = get_syntax_info(&matchid);
19540
19541 /* get the conceal character */
19542 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19543 {
19544 cchar = syn_get_sub_char();
19545 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19546 cchar = lcs_conceal;
19547 if (cchar != NUL)
19548 {
19549# ifdef FEAT_MBYTE
19550 if (has_mbyte)
19551 (*mb_char2bytes)(cchar, str);
19552 else
19553# endif
19554 str[0] = cchar;
19555 }
19556 }
19557 }
19558
19559 list_append_number(rettv->vval.v_list,
19560 (syntax_flags & HL_CONCEAL) != 0);
19561 /* -1 to auto-determine strlen */
19562 list_append_string(rettv->vval.v_list, str, -1);
19563 list_append_number(rettv->vval.v_list, matchid);
19564 }
19565#endif
19566}
19567
19568/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019569 * "synstack(lnum, col)" function
19570 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019572f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019573{
19574#ifdef FEAT_SYN_HL
19575 long lnum;
19576 long col;
19577 int i;
19578 int id;
19579#endif
19580
19581 rettv->v_type = VAR_LIST;
19582 rettv->vval.v_list = NULL;
19583
19584#ifdef FEAT_SYN_HL
19585 lnum = get_tv_lnum(argvars); /* -1 on type error */
19586 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19587
19588 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019589 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019590 && rettv_list_alloc(rettv) != FAIL)
19591 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019592 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019593 for (i = 0; ; ++i)
19594 {
19595 id = syn_get_stack_item(i);
19596 if (id < 0)
19597 break;
19598 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19599 break;
19600 }
19601 }
19602#endif
19603}
19604
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019606get_cmd_output_as_rettv(
19607 typval_T *argvars,
19608 typval_T *rettv,
19609 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019611 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019613 char_u *infile = NULL;
19614 char_u buf[NUMBUFLEN];
19615 int err = FALSE;
19616 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019617 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019618 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019620 rettv->v_type = VAR_STRING;
19621 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019622 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019623 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019624
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019625 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019626 {
19627 /*
19628 * Write the string to a temp file, to be used for input of the shell
19629 * command.
19630 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019631 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019632 {
19633 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019634 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019635 }
19636
19637 fd = mch_fopen((char *)infile, WRITEBIN);
19638 if (fd == NULL)
19639 {
19640 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019641 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019642 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019643 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019644 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019645 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19646 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019647 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019648 else
19649 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019650 size_t len;
19651
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019652 p = get_tv_string_buf_chk(&argvars[1], buf);
19653 if (p == NULL)
19654 {
19655 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019656 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019657 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019658 len = STRLEN(p);
19659 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019660 err = TRUE;
19661 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019662 if (fclose(fd) != 0)
19663 err = TRUE;
19664 if (err)
19665 {
19666 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019667 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019668 }
19669 }
19670
Bram Moolenaar52a72462014-08-29 15:53:52 +020019671 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19672 * echoes typeahead, that messes up the display. */
19673 if (!msg_silent)
19674 flags += SHELL_COOKED;
19675
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019676 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019678 int len;
19679 listitem_T *li;
19680 char_u *s = NULL;
19681 char_u *start;
19682 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019683 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684
Bram Moolenaar52a72462014-08-29 15:53:52 +020019685 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019686 if (res == NULL)
19687 goto errret;
19688
19689 list = list_alloc();
19690 if (list == NULL)
19691 goto errret;
19692
19693 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019695 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019696 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019697 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019698 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019699
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019700 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019701 if (s == NULL)
19702 goto errret;
19703
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019704 for (p = s; start < end; ++p, ++start)
19705 *p = *start == NUL ? NL : *start;
19706 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019707
19708 li = listitem_alloc();
19709 if (li == NULL)
19710 {
19711 vim_free(s);
19712 goto errret;
19713 }
19714 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019715 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019716 li->li_tv.vval.v_string = s;
19717 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019719
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019720 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019721 rettv->v_type = VAR_LIST;
19722 rettv->vval.v_list = list;
19723 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019725 else
19726 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019727 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019728#ifdef USE_CR
19729 /* translate <CR> into <NL> */
19730 if (res != NULL)
19731 {
19732 char_u *s;
19733
19734 for (s = res; *s; ++s)
19735 {
19736 if (*s == CAR)
19737 *s = NL;
19738 }
19739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740#else
19741# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019742 /* translate <CR><NL> into <NL> */
19743 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019745 char_u *s, *d;
19746
19747 d = res;
19748 for (s = res; *s; ++s)
19749 {
19750 if (s[0] == CAR && s[1] == NL)
19751 ++s;
19752 *d++ = *s;
19753 }
19754 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756# endif
19757#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019758 rettv->vval.v_string = res;
19759 res = NULL;
19760 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019761
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019762errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019763 if (infile != NULL)
19764 {
19765 mch_remove(infile);
19766 vim_free(infile);
19767 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019768 if (res != NULL)
19769 vim_free(res);
19770 if (list != NULL)
19771 list_free(list, TRUE);
19772}
19773
19774/*
19775 * "system()" function
19776 */
19777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019778f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019779{
19780 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19781}
19782
19783/*
19784 * "systemlist()" function
19785 */
19786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019787f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019788{
19789 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790}
19791
19792/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019793 * "tabpagebuflist()" function
19794 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019796f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019797{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019798#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019799 tabpage_T *tp;
19800 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019801
19802 if (argvars[0].v_type == VAR_UNKNOWN)
19803 wp = firstwin;
19804 else
19805 {
19806 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19807 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019808 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019809 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019810 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019811 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019812 for (; wp != NULL; wp = wp->w_next)
19813 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019814 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019815 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019816 }
19817#endif
19818}
19819
19820
19821/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019822 * "tabpagenr()" function
19823 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019825f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019826{
19827 int nr = 1;
19828#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019829 char_u *arg;
19830
19831 if (argvars[0].v_type != VAR_UNKNOWN)
19832 {
19833 arg = get_tv_string_chk(&argvars[0]);
19834 nr = 0;
19835 if (arg != NULL)
19836 {
19837 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019838 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019839 else
19840 EMSG2(_(e_invexpr2), arg);
19841 }
19842 }
19843 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019844 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019845#endif
19846 rettv->vval.v_number = nr;
19847}
19848
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019849
19850#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019851static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019852
19853/*
19854 * Common code for tabpagewinnr() and winnr().
19855 */
19856 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019857get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019858{
19859 win_T *twin;
19860 int nr = 1;
19861 win_T *wp;
19862 char_u *arg;
19863
19864 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19865 if (argvar->v_type != VAR_UNKNOWN)
19866 {
19867 arg = get_tv_string_chk(argvar);
19868 if (arg == NULL)
19869 nr = 0; /* type error; errmsg already given */
19870 else if (STRCMP(arg, "$") == 0)
19871 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19872 else if (STRCMP(arg, "#") == 0)
19873 {
19874 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19875 if (twin == NULL)
19876 nr = 0;
19877 }
19878 else
19879 {
19880 EMSG2(_(e_invexpr2), arg);
19881 nr = 0;
19882 }
19883 }
19884
19885 if (nr > 0)
19886 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19887 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019888 {
19889 if (wp == NULL)
19890 {
19891 /* didn't find it in this tabpage */
19892 nr = 0;
19893 break;
19894 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019895 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019896 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019897 return nr;
19898}
19899#endif
19900
19901/*
19902 * "tabpagewinnr()" function
19903 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019905f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019906{
19907 int nr = 1;
19908#ifdef FEAT_WINDOWS
19909 tabpage_T *tp;
19910
19911 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19912 if (tp == NULL)
19913 nr = 0;
19914 else
19915 nr = get_winnr(tp, &argvars[1]);
19916#endif
19917 rettv->vval.v_number = nr;
19918}
19919
19920
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019921/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019922 * "tagfiles()" function
19923 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019925f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019926{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019927 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019928 tagname_T tn;
19929 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019930
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019931 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019932 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019933 fname = alloc(MAXPATHL);
19934 if (fname == NULL)
19935 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019936
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019937 for (first = TRUE; ; first = FALSE)
19938 if (get_tagfname(&tn, first, fname) == FAIL
19939 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019940 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019941 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019942 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019943}
19944
19945/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019946 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019947 */
19948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019949f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019950{
19951 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019952
19953 tag_pattern = get_tv_string(&argvars[0]);
19954
19955 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019956 if (*tag_pattern == NUL)
19957 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019958
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019959 if (rettv_list_alloc(rettv) == OK)
19960 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019961}
19962
19963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 * "tempname()" function
19965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019967f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968{
19969 static int x = 'A';
19970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019972 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973
19974 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19975 * names. Skip 'I' and 'O', they are used for shell redirection. */
19976 do
19977 {
19978 if (x == 'Z')
19979 x = '0';
19980 else if (x == '9')
19981 x = 'A';
19982 else
19983 {
19984#ifdef EBCDIC
19985 if (x == 'I')
19986 x = 'J';
19987 else if (x == 'R')
19988 x = 'S';
19989 else
19990#endif
19991 ++x;
19992 }
19993 } while (x == 'I' || x == 'O');
19994}
19995
19996/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019997 * "test(list)" function: Just checking the walls...
19998 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020000f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020001{
20002 /* Used for unit testing. Change the code below to your liking. */
20003#if 0
20004 listitem_T *li;
20005 list_T *l;
20006 char_u *bad, *good;
20007
20008 if (argvars[0].v_type != VAR_LIST)
20009 return;
20010 l = argvars[0].vval.v_list;
20011 if (l == NULL)
20012 return;
20013 li = l->lv_first;
20014 if (li == NULL)
20015 return;
20016 bad = get_tv_string(&li->li_tv);
20017 li = li->li_next;
20018 if (li == NULL)
20019 return;
20020 good = get_tv_string(&li->li_tv);
20021 rettv->vval.v_number = test_edit_score(bad, good);
20022#endif
20023}
20024
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020025#ifdef FEAT_FLOAT
20026/*
20027 * "tan()" function
20028 */
20029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020030f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020031{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020032 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020033
20034 rettv->v_type = VAR_FLOAT;
20035 if (get_float_arg(argvars, &f) == OK)
20036 rettv->vval.v_float = tan(f);
20037 else
20038 rettv->vval.v_float = 0.0;
20039}
20040
20041/*
20042 * "tanh()" function
20043 */
20044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020045f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020046{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020047 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020048
20049 rettv->v_type = VAR_FLOAT;
20050 if (get_float_arg(argvars, &f) == OK)
20051 rettv->vval.v_float = tanh(f);
20052 else
20053 rettv->vval.v_float = 0.0;
20054}
20055#endif
20056
Bram Moolenaard52d9742005-08-21 22:20:28 +000020057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 * "tolower(string)" function
20059 */
20060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020061f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062{
20063 char_u *p;
20064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020065 p = vim_strsave(get_tv_string(&argvars[0]));
20066 rettv->v_type = VAR_STRING;
20067 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068
20069 if (p != NULL)
20070 while (*p != NUL)
20071 {
20072#ifdef FEAT_MBYTE
20073 int l;
20074
20075 if (enc_utf8)
20076 {
20077 int c, lc;
20078
20079 c = utf_ptr2char(p);
20080 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020081 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 /* TODO: reallocate string when byte count changes. */
20083 if (utf_char2len(lc) == l)
20084 utf_char2bytes(lc, p);
20085 p += l;
20086 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020087 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 p += l; /* skip multi-byte character */
20089 else
20090#endif
20091 {
20092 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20093 ++p;
20094 }
20095 }
20096}
20097
20098/*
20099 * "toupper(string)" function
20100 */
20101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020102f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020104 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020105 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106}
20107
20108/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020109 * "tr(string, fromstr, tostr)" function
20110 */
20111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020112f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020113{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020114 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020115 char_u *fromstr;
20116 char_u *tostr;
20117 char_u *p;
20118#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020119 int inlen;
20120 int fromlen;
20121 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020122 int idx;
20123 char_u *cpstr;
20124 int cplen;
20125 int first = TRUE;
20126#endif
20127 char_u buf[NUMBUFLEN];
20128 char_u buf2[NUMBUFLEN];
20129 garray_T ga;
20130
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020131 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020132 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20133 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020134
20135 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020136 rettv->v_type = VAR_STRING;
20137 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020138 if (fromstr == NULL || tostr == NULL)
20139 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020140 ga_init2(&ga, (int)sizeof(char), 80);
20141
20142#ifdef FEAT_MBYTE
20143 if (!has_mbyte)
20144#endif
20145 /* not multi-byte: fromstr and tostr must be the same length */
20146 if (STRLEN(fromstr) != STRLEN(tostr))
20147 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020148#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020149error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020150#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020151 EMSG2(_(e_invarg2), fromstr);
20152 ga_clear(&ga);
20153 return;
20154 }
20155
20156 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020157 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020158 {
20159#ifdef FEAT_MBYTE
20160 if (has_mbyte)
20161 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020162 inlen = (*mb_ptr2len)(in_str);
20163 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020164 cplen = inlen;
20165 idx = 0;
20166 for (p = fromstr; *p != NUL; p += fromlen)
20167 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020168 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020169 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020170 {
20171 for (p = tostr; *p != NUL; p += tolen)
20172 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020173 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020174 if (idx-- == 0)
20175 {
20176 cplen = tolen;
20177 cpstr = p;
20178 break;
20179 }
20180 }
20181 if (*p == NUL) /* tostr is shorter than fromstr */
20182 goto error;
20183 break;
20184 }
20185 ++idx;
20186 }
20187
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020188 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020189 {
20190 /* Check that fromstr and tostr have the same number of
20191 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020192 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020193 first = FALSE;
20194 for (p = tostr; *p != NUL; p += tolen)
20195 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020196 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020197 --idx;
20198 }
20199 if (idx != 0)
20200 goto error;
20201 }
20202
Bram Moolenaarcde88542015-08-11 19:14:00 +020020203 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020204 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020205 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020206
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020207 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020208 }
20209 else
20210#endif
20211 {
20212 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020213 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020214 if (p != NULL)
20215 ga_append(&ga, tostr[p - fromstr]);
20216 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020217 ga_append(&ga, *in_str);
20218 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020219 }
20220 }
20221
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020222 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020223 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020224 ga_append(&ga, NUL);
20225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020226 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020227}
20228
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020229#ifdef FEAT_FLOAT
20230/*
20231 * "trunc({float})" function
20232 */
20233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020234f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020235{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020236 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020237
20238 rettv->v_type = VAR_FLOAT;
20239 if (get_float_arg(argvars, &f) == OK)
20240 /* trunc() is not in C90, use floor() or ceil() instead. */
20241 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20242 else
20243 rettv->vval.v_float = 0.0;
20244}
20245#endif
20246
Bram Moolenaar8299df92004-07-10 09:47:34 +000020247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 * "type(expr)" function
20249 */
20250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020251f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020253 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020254
20255 switch (argvars[0].v_type)
20256 {
20257 case VAR_NUMBER: n = 0; break;
20258 case VAR_STRING: n = 1; break;
20259 case VAR_FUNC: n = 2; break;
20260 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020261 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020262 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020263 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020264 if (argvars[0].vval.v_number == VVAL_FALSE
20265 || argvars[0].vval.v_number == VVAL_TRUE)
20266 n = 6;
20267 else
20268 n = 7;
20269 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020270 case VAR_JOB: n = 8; break;
20271 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020272 case VAR_UNKNOWN:
20273 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20274 n = -1;
20275 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020276 }
20277 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278}
20279
20280/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020281 * "undofile(name)" function
20282 */
20283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020284f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020285{
20286 rettv->v_type = VAR_STRING;
20287#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020288 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020289 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020290
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020291 if (*fname == NUL)
20292 {
20293 /* If there is no file name there will be no undo file. */
20294 rettv->vval.v_string = NULL;
20295 }
20296 else
20297 {
20298 char_u *ffname = FullName_save(fname, FALSE);
20299
20300 if (ffname != NULL)
20301 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20302 vim_free(ffname);
20303 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020304 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020305#else
20306 rettv->vval.v_string = NULL;
20307#endif
20308}
20309
20310/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020311 * "undotree()" function
20312 */
20313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020314f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020315{
20316 if (rettv_dict_alloc(rettv) == OK)
20317 {
20318 dict_T *dict = rettv->vval.v_dict;
20319 list_T *list;
20320
Bram Moolenaar730cde92010-06-27 05:18:54 +020020321 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020322 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020323 dict_add_nr_str(dict, "save_last",
20324 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020325 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20326 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020327 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020328
20329 list = list_alloc();
20330 if (list != NULL)
20331 {
20332 u_eval_tree(curbuf->b_u_oldhead, list);
20333 dict_add_list(dict, "entries", list);
20334 }
20335 }
20336}
20337
20338/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020339 * "values(dict)" function
20340 */
20341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020342f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020343{
20344 dict_list(argvars, rettv, 1);
20345}
20346
20347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 * "virtcol(string)" function
20349 */
20350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020351f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352{
20353 colnr_T vcol = 0;
20354 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020355 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020357 fp = var2fpos(&argvars[0], FALSE, &fnum);
20358 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20359 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 {
20361 getvvcol(curwin, fp, NULL, NULL, &vcol);
20362 ++vcol;
20363 }
20364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020365 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366}
20367
20368/*
20369 * "visualmode()" function
20370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020372f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 char_u str[2];
20375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020376 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 str[0] = curbuf->b_visual_mode_eval;
20378 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020379 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380
20381 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020382 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384}
20385
20386/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020387 * "wildmenumode()" function
20388 */
20389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020390f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020391{
20392#ifdef FEAT_WILDMENU
20393 if (wild_menu_showing)
20394 rettv->vval.v_number = 1;
20395#endif
20396}
20397
20398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 * "winbufnr(nr)" function
20400 */
20401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020402f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403{
20404 win_T *wp;
20405
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020406 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020408 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020410 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411}
20412
20413/*
20414 * "wincol()" function
20415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020417f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418{
20419 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020420 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421}
20422
20423/*
20424 * "winheight(nr)" function
20425 */
20426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020427f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428{
20429 win_T *wp;
20430
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020431 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020433 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020435 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436}
20437
20438/*
20439 * "winline()" function
20440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020442f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443{
20444 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020445 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446}
20447
20448/*
20449 * "winnr()" function
20450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020452f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453{
20454 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020455
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020457 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020459 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460}
20461
20462/*
20463 * "winrestcmd()" function
20464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020466f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467{
20468#ifdef FEAT_WINDOWS
20469 win_T *wp;
20470 int winnr = 1;
20471 garray_T ga;
20472 char_u buf[50];
20473
20474 ga_init2(&ga, (int)sizeof(char), 70);
20475 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20476 {
20477 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20478 ga_concat(&ga, buf);
20479# ifdef FEAT_VERTSPLIT
20480 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20481 ga_concat(&ga, buf);
20482# endif
20483 ++winnr;
20484 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020485 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020487 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020489 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020491 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492}
20493
20494/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020495 * "winrestview()" function
20496 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020498f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020499{
20500 dict_T *dict;
20501
20502 if (argvars[0].v_type != VAR_DICT
20503 || (dict = argvars[0].vval.v_dict) == NULL)
20504 EMSG(_(e_invarg));
20505 else
20506 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020507 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20508 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20509 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20510 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020511#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020512 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20513 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020514#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020515 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20516 {
20517 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20518 curwin->w_set_curswant = FALSE;
20519 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020520
Bram Moolenaar82c25852014-05-28 16:47:16 +020020521 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20522 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020523#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020524 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20525 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020526#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020527 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20528 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20529 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20530 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020531
20532 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020533 win_new_height(curwin, curwin->w_height);
20534# ifdef FEAT_VERTSPLIT
20535 win_new_width(curwin, W_WIDTH(curwin));
20536# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020537 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020538
Bram Moolenaarb851a962014-10-31 15:45:52 +010020539 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020540 curwin->w_topline = 1;
20541 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20542 curwin->w_topline = curbuf->b_ml.ml_line_count;
20543#ifdef FEAT_DIFF
20544 check_topfill(curwin, TRUE);
20545#endif
20546 }
20547}
20548
20549/*
20550 * "winsaveview()" function
20551 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020553f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020554{
20555 dict_T *dict;
20556
Bram Moolenaara800b422010-06-27 01:15:55 +020020557 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020558 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020559 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020560
20561 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20562 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20563#ifdef FEAT_VIRTUALEDIT
20564 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20565#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020566 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020567 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20568
20569 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20570#ifdef FEAT_DIFF
20571 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20572#endif
20573 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20574 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20575}
20576
20577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 * "winwidth(nr)" function
20579 */
20580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020581f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582{
20583 win_T *wp;
20584
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020585 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020587 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 else
20589#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020590 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020592 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593#endif
20594}
20595
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020597 * "wordcount()" function
20598 */
20599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020600f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020601{
20602 if (rettv_dict_alloc(rettv) == FAIL)
20603 return;
20604 cursor_pos_info(rettv->vval.v_dict);
20605}
20606
20607/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020608 * Write list of strings to file
20609 */
20610 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020611write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020612{
20613 listitem_T *li;
20614 int c;
20615 int ret = OK;
20616 char_u *s;
20617
20618 for (li = list->lv_first; li != NULL; li = li->li_next)
20619 {
20620 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20621 {
20622 if (*s == '\n')
20623 c = putc(NUL, fd);
20624 else
20625 c = putc(*s, fd);
20626 if (c == EOF)
20627 {
20628 ret = FAIL;
20629 break;
20630 }
20631 }
20632 if (!binary || li->li_next != NULL)
20633 if (putc('\n', fd) == EOF)
20634 {
20635 ret = FAIL;
20636 break;
20637 }
20638 if (ret == FAIL)
20639 {
20640 EMSG(_(e_write));
20641 break;
20642 }
20643 }
20644 return ret;
20645}
20646
20647/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020648 * "writefile()" function
20649 */
20650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020651f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020652{
20653 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020654 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020655 char_u *fname;
20656 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020657 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020658
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020659 if (check_restricted() || check_secure())
20660 return;
20661
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020662 if (argvars[0].v_type != VAR_LIST)
20663 {
20664 EMSG2(_(e_listarg), "writefile()");
20665 return;
20666 }
20667 if (argvars[0].vval.v_list == NULL)
20668 return;
20669
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020670 if (argvars[2].v_type != VAR_UNKNOWN)
20671 {
20672 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20673 binary = TRUE;
20674 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20675 append = TRUE;
20676 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020677
20678 /* Always open the file in binary mode, library functions have a mind of
20679 * their own about CR-LF conversion. */
20680 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020681 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20682 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020683 {
20684 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20685 ret = -1;
20686 }
20687 else
20688 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020689 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20690 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020691 fclose(fd);
20692 }
20693
20694 rettv->vval.v_number = ret;
20695}
20696
20697/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020698 * "xor(expr, expr)" function
20699 */
20700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020701f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020702{
20703 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20704 ^ get_tv_number_chk(&argvars[1], NULL);
20705}
20706
20707
20708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020710 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 */
20712 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020713var2fpos(
20714 typval_T *varp,
20715 int dollar_lnum, /* TRUE when $ is last line */
20716 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020718 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020720 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721
Bram Moolenaara5525202006-03-02 22:52:09 +000020722 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020723 if (varp->v_type == VAR_LIST)
20724 {
20725 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020726 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020727 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020728 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020729
20730 l = varp->vval.v_list;
20731 if (l == NULL)
20732 return NULL;
20733
20734 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020735 pos.lnum = list_find_nr(l, 0L, &error);
20736 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020737 return NULL; /* invalid line number */
20738
20739 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020740 pos.col = list_find_nr(l, 1L, &error);
20741 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020742 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020743 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020744
20745 /* We accept "$" for the column number: last column. */
20746 li = list_find(l, 1L);
20747 if (li != NULL && li->li_tv.v_type == VAR_STRING
20748 && li->li_tv.vval.v_string != NULL
20749 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20750 pos.col = len + 1;
20751
Bram Moolenaara5525202006-03-02 22:52:09 +000020752 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020753 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020754 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020755 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020756
Bram Moolenaara5525202006-03-02 22:52:09 +000020757#ifdef FEAT_VIRTUALEDIT
20758 /* Get the virtual offset. Defaults to zero. */
20759 pos.coladd = list_find_nr(l, 2L, &error);
20760 if (error)
20761 pos.coladd = 0;
20762#endif
20763
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020764 return &pos;
20765 }
20766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020767 name = get_tv_string_chk(varp);
20768 if (name == NULL)
20769 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020770 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020772 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20773 {
20774 if (VIsual_active)
20775 return &VIsual;
20776 return &curwin->w_cursor;
20777 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020778 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020780 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20782 return NULL;
20783 return pp;
20784 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020785
20786#ifdef FEAT_VIRTUALEDIT
20787 pos.coladd = 0;
20788#endif
20789
Bram Moolenaar477933c2007-07-17 14:32:23 +000020790 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020791 {
20792 pos.col = 0;
20793 if (name[1] == '0') /* "w0": first visible line */
20794 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020795 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020796 pos.lnum = curwin->w_topline;
20797 return &pos;
20798 }
20799 else if (name[1] == '$') /* "w$": last visible line */
20800 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020801 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020802 pos.lnum = curwin->w_botline - 1;
20803 return &pos;
20804 }
20805 }
20806 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020808 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809 {
20810 pos.lnum = curbuf->b_ml.ml_line_count;
20811 pos.col = 0;
20812 }
20813 else
20814 {
20815 pos.lnum = curwin->w_cursor.lnum;
20816 pos.col = (colnr_T)STRLEN(ml_get_curline());
20817 }
20818 return &pos;
20819 }
20820 return NULL;
20821}
20822
20823/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020824 * Convert list in "arg" into a position and optional file number.
20825 * When "fnump" is NULL there is no file number, only 3 items.
20826 * Note that the column is passed on as-is, the caller may want to decrement
20827 * it to use 1 for the first column.
20828 * Return FAIL when conversion is not possible, doesn't check the position for
20829 * validity.
20830 */
20831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020832list2fpos(
20833 typval_T *arg,
20834 pos_T *posp,
20835 int *fnump,
20836 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020837{
20838 list_T *l = arg->vval.v_list;
20839 long i = 0;
20840 long n;
20841
Bram Moolenaar493c1782014-05-28 14:34:46 +020020842 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20843 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020844 if (arg->v_type != VAR_LIST
20845 || l == NULL
20846 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020847 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020848 return FAIL;
20849
20850 if (fnump != NULL)
20851 {
20852 n = list_find_nr(l, i++, NULL); /* fnum */
20853 if (n < 0)
20854 return FAIL;
20855 if (n == 0)
20856 n = curbuf->b_fnum; /* current buffer */
20857 *fnump = n;
20858 }
20859
20860 n = list_find_nr(l, i++, NULL); /* lnum */
20861 if (n < 0)
20862 return FAIL;
20863 posp->lnum = n;
20864
20865 n = list_find_nr(l, i++, NULL); /* col */
20866 if (n < 0)
20867 return FAIL;
20868 posp->col = n;
20869
20870#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020871 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020872 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020873 posp->coladd = 0;
20874 else
20875 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020876#endif
20877
Bram Moolenaar493c1782014-05-28 14:34:46 +020020878 if (curswantp != NULL)
20879 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20880
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020881 return OK;
20882}
20883
20884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 * Get the length of an environment variable name.
20886 * Advance "arg" to the first character after the name.
20887 * Return 0 for error.
20888 */
20889 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020890get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891{
20892 char_u *p;
20893 int len;
20894
20895 for (p = *arg; vim_isIDc(*p); ++p)
20896 ;
20897 if (p == *arg) /* no name found */
20898 return 0;
20899
20900 len = (int)(p - *arg);
20901 *arg = p;
20902 return len;
20903}
20904
20905/*
20906 * Get the length of the name of a function or internal variable.
20907 * "arg" is advanced to the first non-white character after the name.
20908 * Return 0 if something is wrong.
20909 */
20910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020911get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912{
20913 char_u *p;
20914 int len;
20915
20916 /* Find the end of the name. */
20917 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020918 {
20919 if (*p == ':')
20920 {
20921 /* "s:" is start of "s:var", but "n:" is not and can be used in
20922 * slice "[n:]". Also "xx:" is not a namespace. */
20923 len = (int)(p - *arg);
20924 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20925 || len > 1)
20926 break;
20927 }
20928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 if (p == *arg) /* no name found */
20930 return 0;
20931
20932 len = (int)(p - *arg);
20933 *arg = skipwhite(p);
20934
20935 return len;
20936}
20937
20938/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020939 * Get the length of the name of a variable or function.
20940 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020942 * Return -1 if curly braces expansion failed.
20943 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 * If the name contains 'magic' {}'s, expand them and return the
20945 * expanded name in an allocated string via 'alias' - caller must free.
20946 */
20947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020948get_name_len(
20949 char_u **arg,
20950 char_u **alias,
20951 int evaluate,
20952 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953{
20954 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 char_u *p;
20956 char_u *expr_start;
20957 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958
20959 *alias = NULL; /* default to no alias */
20960
20961 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20962 && (*arg)[2] == (int)KE_SNR)
20963 {
20964 /* hard coded <SNR>, already translated */
20965 *arg += 3;
20966 return get_id_len(arg) + 3;
20967 }
20968 len = eval_fname_script(*arg);
20969 if (len > 0)
20970 {
20971 /* literal "<SID>", "s:" or "<SNR>" */
20972 *arg += len;
20973 }
20974
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020976 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020978 p = find_name_end(*arg, &expr_start, &expr_end,
20979 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 if (expr_start != NULL)
20981 {
20982 char_u *temp_string;
20983
20984 if (!evaluate)
20985 {
20986 len += (int)(p - *arg);
20987 *arg = skipwhite(p);
20988 return len;
20989 }
20990
20991 /*
20992 * Include any <SID> etc in the expanded string:
20993 * Thus the -len here.
20994 */
20995 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20996 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020997 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998 *alias = temp_string;
20999 *arg = skipwhite(p);
21000 return (int)STRLEN(temp_string);
21001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002
21003 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021004 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 EMSG2(_(e_invexpr2), *arg);
21006
21007 return len;
21008}
21009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021010/*
21011 * Find the end of a variable or function name, taking care of magic braces.
21012 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21013 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021014 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021015 * Return a pointer to just after the name. Equal to "arg" if there is no
21016 * valid name.
21017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021019find_name_end(
21020 char_u *arg,
21021 char_u **expr_start,
21022 char_u **expr_end,
21023 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021025 int mb_nest = 0;
21026 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021028 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021030 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021032 *expr_start = NULL;
21033 *expr_end = NULL;
21034 }
21035
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021036 /* Quick check for valid starting character. */
21037 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21038 return arg;
21039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021040 for (p = arg; *p != NUL
21041 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021042 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021043 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021044 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021045 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021046 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021047 if (*p == '\'')
21048 {
21049 /* skip over 'string' to avoid counting [ and ] inside it. */
21050 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21051 ;
21052 if (*p == NUL)
21053 break;
21054 }
21055 else if (*p == '"')
21056 {
21057 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21058 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21059 if (*p == '\\' && p[1] != NUL)
21060 ++p;
21061 if (*p == NUL)
21062 break;
21063 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021064 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21065 {
21066 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021067 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021068 len = (int)(p - arg);
21069 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021070 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021071 break;
21072 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021074 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021076 if (*p == '[')
21077 ++br_nest;
21078 else if (*p == ']')
21079 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021082 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021084 if (*p == '{')
21085 {
21086 mb_nest++;
21087 if (expr_start != NULL && *expr_start == NULL)
21088 *expr_start = p;
21089 }
21090 else if (*p == '}')
21091 {
21092 mb_nest--;
21093 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21094 *expr_end = p;
21095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 }
21098
21099 return p;
21100}
21101
21102/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021103 * Expands out the 'magic' {}'s in a variable/function name.
21104 * Note that this can call itself recursively, to deal with
21105 * constructs like foo{bar}{baz}{bam}
21106 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21107 * "in_start" ^
21108 * "expr_start" ^
21109 * "expr_end" ^
21110 * "in_end" ^
21111 *
21112 * Returns a new allocated string, which the caller must free.
21113 * Returns NULL for failure.
21114 */
21115 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021116make_expanded_name(
21117 char_u *in_start,
21118 char_u *expr_start,
21119 char_u *expr_end,
21120 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021121{
21122 char_u c1;
21123 char_u *retval = NULL;
21124 char_u *temp_result;
21125 char_u *nextcmd = NULL;
21126
21127 if (expr_end == NULL || in_end == NULL)
21128 return NULL;
21129 *expr_start = NUL;
21130 *expr_end = NUL;
21131 c1 = *in_end;
21132 *in_end = NUL;
21133
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021134 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021135 if (temp_result != NULL && nextcmd == NULL)
21136 {
21137 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21138 + (in_end - expr_end) + 1));
21139 if (retval != NULL)
21140 {
21141 STRCPY(retval, in_start);
21142 STRCAT(retval, temp_result);
21143 STRCAT(retval, expr_end + 1);
21144 }
21145 }
21146 vim_free(temp_result);
21147
21148 *in_end = c1; /* put char back for error messages */
21149 *expr_start = '{';
21150 *expr_end = '}';
21151
21152 if (retval != NULL)
21153 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021154 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021155 if (expr_start != NULL)
21156 {
21157 /* Further expansion! */
21158 temp_result = make_expanded_name(retval, expr_start,
21159 expr_end, temp_result);
21160 vim_free(retval);
21161 retval = temp_result;
21162 }
21163 }
21164
21165 return retval;
21166}
21167
21168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021170 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 */
21172 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021173eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021175 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21176}
21177
21178/*
21179 * Return TRUE if character "c" can be used as the first character in a
21180 * variable or function name (excluding '{' and '}').
21181 */
21182 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021183eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021184{
21185 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186}
21187
21188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 * Set number v: variable to "val".
21190 */
21191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021192set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021194 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195}
21196
21197/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021198 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 */
21200 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021201get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021203 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204}
21205
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021206/*
21207 * Get string v: variable value. Uses a static buffer, can only be used once.
21208 */
21209 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021211{
21212 return get_tv_string(&vimvars[idx].vv_tv);
21213}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021214
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021216 * Get List v: variable value. Caller must take care of reference count when
21217 * needed.
21218 */
21219 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021220get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021221{
21222 return vimvars[idx].vv_list;
21223}
21224
21225/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021226 * Set v:char to character "c".
21227 */
21228 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021229set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021230{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021231 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021232
21233#ifdef FEAT_MBYTE
21234 if (has_mbyte)
21235 buf[(*mb_char2bytes)(c, buf)] = NUL;
21236 else
21237#endif
21238 {
21239 buf[0] = c;
21240 buf[1] = NUL;
21241 }
21242 set_vim_var_string(VV_CHAR, buf, -1);
21243}
21244
21245/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021246 * Set v:count to "count" and v:count1 to "count1".
21247 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 */
21249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021250set_vcount(
21251 long count,
21252 long count1,
21253 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021255 if (set_prevcount)
21256 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021257 vimvars[VV_COUNT].vv_nr = count;
21258 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259}
21260
21261/*
21262 * Set string v: variable to a copy of "val".
21263 */
21264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021265set_vim_var_string(
21266 int idx,
21267 char_u *val,
21268 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269{
Bram Moolenaara542c682016-01-31 16:28:04 +010021270 clear_tv(&vimvars[idx].vv_di.di_tv);
21271 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021273 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021275 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021277 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278}
21279
21280/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021281 * Set List v: variable to "val".
21282 */
21283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021284set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021285{
Bram Moolenaara542c682016-01-31 16:28:04 +010021286 clear_tv(&vimvars[idx].vv_di.di_tv);
21287 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021288 vimvars[idx].vv_list = val;
21289 if (val != NULL)
21290 ++val->lv_refcount;
21291}
21292
21293/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021294 * Set Dictionary v: variable to "val".
21295 */
21296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021297set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021298{
21299 int todo;
21300 hashitem_T *hi;
21301
Bram Moolenaara542c682016-01-31 16:28:04 +010021302 clear_tv(&vimvars[idx].vv_di.di_tv);
21303 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021304 vimvars[idx].vv_dict = val;
21305 if (val != NULL)
21306 {
21307 ++val->dv_refcount;
21308
21309 /* Set readonly */
21310 todo = (int)val->dv_hashtab.ht_used;
21311 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21312 {
21313 if (HASHITEM_EMPTY(hi))
21314 continue;
21315 --todo;
21316 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21317 }
21318 }
21319}
21320
21321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 * Set v:register if needed.
21323 */
21324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021325set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326{
21327 char_u regname;
21328
21329 if (c == 0 || c == ' ')
21330 regname = '"';
21331 else
21332 regname = c;
21333 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021334 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021335 set_vim_var_string(VV_REG, &regname, 1);
21336}
21337
21338/*
21339 * Get or set v:exception. If "oldval" == NULL, return the current value.
21340 * Otherwise, restore the value to "oldval" and return NULL.
21341 * Must always be called in pairs to save and restore v:exception! Does not
21342 * take care of memory allocations.
21343 */
21344 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021345v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346{
21347 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021348 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349
Bram Moolenaare9a41262005-01-15 22:18:47 +000021350 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 return NULL;
21352}
21353
21354/*
21355 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21356 * Otherwise, restore the value to "oldval" and return NULL.
21357 * Must always be called in pairs to save and restore v:throwpoint! Does not
21358 * take care of memory allocations.
21359 */
21360 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021361v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362{
21363 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021364 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365
Bram Moolenaare9a41262005-01-15 22:18:47 +000021366 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 return NULL;
21368}
21369
21370#if defined(FEAT_AUTOCMD) || defined(PROTO)
21371/*
21372 * Set v:cmdarg.
21373 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21374 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21375 * Must always be called in pairs!
21376 */
21377 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021378set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379{
21380 char_u *oldval;
21381 char_u *newval;
21382 unsigned len;
21383
Bram Moolenaare9a41262005-01-15 22:18:47 +000021384 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021385 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021387 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021388 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021389 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 }
21391
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021392 if (eap->force_bin == FORCE_BIN)
21393 len = 6;
21394 else if (eap->force_bin == FORCE_NOBIN)
21395 len = 8;
21396 else
21397 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021398
21399 if (eap->read_edit)
21400 len += 7;
21401
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021402 if (eap->force_ff != 0)
21403 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21404# ifdef FEAT_MBYTE
21405 if (eap->force_enc != 0)
21406 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021407 if (eap->bad_char != 0)
21408 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021409# endif
21410
21411 newval = alloc(len + 1);
21412 if (newval == NULL)
21413 return NULL;
21414
21415 if (eap->force_bin == FORCE_BIN)
21416 sprintf((char *)newval, " ++bin");
21417 else if (eap->force_bin == FORCE_NOBIN)
21418 sprintf((char *)newval, " ++nobin");
21419 else
21420 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021421
21422 if (eap->read_edit)
21423 STRCAT(newval, " ++edit");
21424
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021425 if (eap->force_ff != 0)
21426 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21427 eap->cmd + eap->force_ff);
21428# ifdef FEAT_MBYTE
21429 if (eap->force_enc != 0)
21430 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21431 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021432 if (eap->bad_char == BAD_KEEP)
21433 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21434 else if (eap->bad_char == BAD_DROP)
21435 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21436 else if (eap->bad_char != 0)
21437 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021438# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021439 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021440 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441}
21442#endif
21443
21444/*
21445 * Get the value of internal variable "name".
21446 * Return OK or FAIL.
21447 */
21448 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021449get_var_tv(
21450 char_u *name,
21451 int len, /* length of "name" */
21452 typval_T *rettv, /* NULL when only checking existence */
21453 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21454 int verbose, /* may give error message */
21455 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456{
21457 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021458 typval_T *tv = NULL;
21459 typval_T atv;
21460 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462
21463 /* truncate the name, so that we can use strcmp() */
21464 cc = name[len];
21465 name[len] = NUL;
21466
21467 /*
21468 * Check for "b:changedtick".
21469 */
21470 if (STRCMP(name, "b:changedtick") == 0)
21471 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021472 atv.v_type = VAR_NUMBER;
21473 atv.vval.v_number = curbuf->b_changedtick;
21474 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 }
21476
21477 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 * Check for user-defined variables.
21479 */
21480 else
21481 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021482 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021484 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021485 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021486 if (dip != NULL)
21487 *dip = v;
21488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489 }
21490
Bram Moolenaare9a41262005-01-15 22:18:47 +000021491 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021493 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021494 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 ret = FAIL;
21496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021497 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021498 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499
21500 name[len] = cc;
21501
21502 return ret;
21503}
21504
21505/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021506 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21507 * Also handle function call with Funcref variable: func(expr)
21508 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21509 */
21510 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021511handle_subscript(
21512 char_u **arg,
21513 typval_T *rettv,
21514 int evaluate, /* do more than finding the end */
21515 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021516{
21517 int ret = OK;
21518 dict_T *selfdict = NULL;
21519 char_u *s;
21520 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021521 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021522
21523 while (ret == OK
21524 && (**arg == '['
21525 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021526 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021527 && !vim_iswhite(*(*arg - 1)))
21528 {
21529 if (**arg == '(')
21530 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021531 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021532 if (evaluate)
21533 {
21534 functv = *rettv;
21535 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021536
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021537 /* Invoke the function. Recursive! */
21538 s = functv.vval.v_string;
21539 }
21540 else
21541 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021542 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021543 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21544 &len, evaluate, selfdict);
21545
21546 /* Clear the funcref afterwards, so that deleting it while
21547 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021548 if (evaluate)
21549 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021550
21551 /* Stop the expression evaluation when immediately aborting on
21552 * error, or when an interrupt occurred or an exception was thrown
21553 * but not caught. */
21554 if (aborting())
21555 {
21556 if (ret == OK)
21557 clear_tv(rettv);
21558 ret = FAIL;
21559 }
21560 dict_unref(selfdict);
21561 selfdict = NULL;
21562 }
21563 else /* **arg == '[' || **arg == '.' */
21564 {
21565 dict_unref(selfdict);
21566 if (rettv->v_type == VAR_DICT)
21567 {
21568 selfdict = rettv->vval.v_dict;
21569 if (selfdict != NULL)
21570 ++selfdict->dv_refcount;
21571 }
21572 else
21573 selfdict = NULL;
21574 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21575 {
21576 clear_tv(rettv);
21577 ret = FAIL;
21578 }
21579 }
21580 }
21581 dict_unref(selfdict);
21582 return ret;
21583}
21584
21585/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021586 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021587 * value).
21588 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021589 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021590alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021591{
Bram Moolenaar33570922005-01-25 22:26:29 +000021592 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021593}
21594
21595/*
21596 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 * The string "s" must have been allocated, it is consumed.
21598 * Return NULL for out of memory, the variable otherwise.
21599 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021600 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021601alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602{
Bram Moolenaar33570922005-01-25 22:26:29 +000021603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021605 rettv = alloc_tv();
21606 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021608 rettv->v_type = VAR_STRING;
21609 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 }
21611 else
21612 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021613 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614}
21615
21616/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021617 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021619 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021620free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621{
21622 if (varp != NULL)
21623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021624 switch (varp->v_type)
21625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021626 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021627 func_unref(varp->vval.v_string);
21628 /*FALLTHROUGH*/
21629 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021630 vim_free(varp->vval.v_string);
21631 break;
21632 case VAR_LIST:
21633 list_unref(varp->vval.v_list);
21634 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021635 case VAR_DICT:
21636 dict_unref(varp->vval.v_dict);
21637 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021638 case VAR_JOB:
21639#ifdef FEAT_JOB
21640 job_unref(varp->vval.v_job);
21641 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021642#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021643 case VAR_CHANNEL:
21644#ifdef FEAT_CHANNEL
21645 channel_unref(varp->vval.v_channel);
21646 break;
21647#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021648 case VAR_NUMBER:
21649 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021650 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021651 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021652 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 vim_free(varp);
21655 }
21656}
21657
21658/*
21659 * Free the memory for a variable value and set the value to NULL or 0.
21660 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021661 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021662clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663{
21664 if (varp != NULL)
21665 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021666 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021668 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021669 func_unref(varp->vval.v_string);
21670 /*FALLTHROUGH*/
21671 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021672 vim_free(varp->vval.v_string);
21673 varp->vval.v_string = NULL;
21674 break;
21675 case VAR_LIST:
21676 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021677 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021678 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021679 case VAR_DICT:
21680 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021681 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021682 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021683 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021684 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021685 varp->vval.v_number = 0;
21686 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021687 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021688#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021689 varp->vval.v_float = 0.0;
21690 break;
21691#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021692 case VAR_JOB:
21693#ifdef FEAT_JOB
21694 job_unref(varp->vval.v_job);
21695 varp->vval.v_job = NULL;
21696#endif
21697 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021698 case VAR_CHANNEL:
21699#ifdef FEAT_CHANNEL
21700 channel_unref(varp->vval.v_channel);
21701 varp->vval.v_channel = NULL;
21702#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021703 case VAR_UNKNOWN:
21704 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021706 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 }
21708}
21709
21710/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021711 * Set the value of a variable to NULL without freeing items.
21712 */
21713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021714init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021715{
21716 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021717 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021718}
21719
21720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721 * Get the number value of a variable.
21722 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021723 * For incompatible types, return 0.
21724 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21725 * caller of incompatible types: it sets *denote to TRUE if "denote"
21726 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 */
21728 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021729get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021731 int error = FALSE;
21732
21733 return get_tv_number_chk(varp, &error); /* return 0L on error */
21734}
21735
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021736 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021737get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021738{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021739 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021741 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021743 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021744 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021745 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021746#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021747 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021748 break;
21749#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021750 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021751 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021752 break;
21753 case VAR_STRING:
21754 if (varp->vval.v_string != NULL)
21755 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021756 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021757 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021758 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021759 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021760 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021761 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021762 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021763 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021764 case VAR_SPECIAL:
21765 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21766 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021767 case VAR_JOB:
21768#ifdef FEAT_JOB
21769 EMSG(_("E910: Using a Job as a Number"));
21770 break;
21771#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021772 case VAR_CHANNEL:
21773#ifdef FEAT_CHANNEL
21774 EMSG(_("E913: Using a Channel as a Number"));
21775 break;
21776#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021777 case VAR_UNKNOWN:
21778 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021779 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021781 if (denote == NULL) /* useful for values that must be unsigned */
21782 n = -1;
21783 else
21784 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021785 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786}
21787
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021788#ifdef FEAT_FLOAT
21789 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021790get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021791{
21792 switch (varp->v_type)
21793 {
21794 case VAR_NUMBER:
21795 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021796 case VAR_FLOAT:
21797 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021798 case VAR_FUNC:
21799 EMSG(_("E891: Using a Funcref as a Float"));
21800 break;
21801 case VAR_STRING:
21802 EMSG(_("E892: Using a String as a Float"));
21803 break;
21804 case VAR_LIST:
21805 EMSG(_("E893: Using a List as a Float"));
21806 break;
21807 case VAR_DICT:
21808 EMSG(_("E894: Using a Dictionary as a Float"));
21809 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021810 case VAR_SPECIAL:
21811 EMSG(_("E907: Using a special value as a Float"));
21812 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021813 case VAR_JOB:
21814# ifdef FEAT_JOB
21815 EMSG(_("E911: Using a Job as a Float"));
21816 break;
21817# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021818 case VAR_CHANNEL:
21819# ifdef FEAT_CHANNEL
21820 EMSG(_("E914: Using a Channel as a Float"));
21821 break;
21822# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021823 case VAR_UNKNOWN:
21824 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021825 break;
21826 }
21827 return 0;
21828}
21829#endif
21830
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021832 * Get the lnum from the first argument.
21833 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021834 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 */
21836 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021837get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838{
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840 linenr_T lnum;
21841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021842 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843 if (lnum == 0) /* no valid number, try using line() */
21844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021845 rettv.v_type = VAR_NUMBER;
21846 f_line(argvars, &rettv);
21847 lnum = rettv.vval.v_number;
21848 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849 }
21850 return lnum;
21851}
21852
21853/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021854 * Get the lnum from the first argument.
21855 * Also accepts "$", then "buf" is used.
21856 * Returns 0 on error.
21857 */
21858 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021859get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021860{
21861 if (argvars[0].v_type == VAR_STRING
21862 && argvars[0].vval.v_string != NULL
21863 && argvars[0].vval.v_string[0] == '$'
21864 && buf != NULL)
21865 return buf->b_ml.ml_line_count;
21866 return get_tv_number_chk(&argvars[0], NULL);
21867}
21868
21869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 * Get the string value of a variable.
21871 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021872 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21873 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 * If the String variable has never been set, return an empty string.
21875 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021876 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21877 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 */
21879 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021880get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881{
21882 static char_u mybuf[NUMBUFLEN];
21883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021884 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885}
21886
21887 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021888get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021890 char_u *res = get_tv_string_buf_chk(varp, buf);
21891
21892 return res != NULL ? res : (char_u *)"";
21893}
21894
Bram Moolenaar7d647822014-04-05 21:28:56 +020021895/*
21896 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21897 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021898 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021899get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021900{
21901 static char_u mybuf[NUMBUFLEN];
21902
21903 return get_tv_string_buf_chk(varp, mybuf);
21904}
21905
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021906 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021907get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021908{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021909 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021911 case VAR_NUMBER:
21912 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21913 return buf;
21914 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021915 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021916 break;
21917 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021918 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021919 break;
21920 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021921 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021922 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021923 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021924#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021925 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021926 break;
21927#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021928 case VAR_STRING:
21929 if (varp->vval.v_string != NULL)
21930 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021931 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021932 case VAR_SPECIAL:
21933 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21934 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021935 case VAR_JOB:
21936#ifdef FEAT_JOB
21937 {
21938 job_T *job = varp->vval.v_job;
21939 char *status = job->jv_status == JOB_FAILED ? "fail"
21940 : job->jv_status == JOB_ENDED ? "dead"
21941 : "run";
21942# ifdef UNIX
21943 vim_snprintf((char *)buf, NUMBUFLEN,
21944 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021945# elif defined(WIN32)
21946 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021947 "process %ld %s",
21948 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021949 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021950# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021951 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021952 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21953# endif
21954 return buf;
21955 }
21956#endif
21957 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021958 case VAR_CHANNEL:
21959#ifdef FEAT_CHANNEL
21960 {
21961 channel_T *channel = varp->vval.v_channel;
21962 char *status = channel_status(channel);
21963
Bram Moolenaar5cefd402016-02-16 12:44:26 +010021964 if (channel == NULL)
21965 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
21966 else
21967 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010021968 "channel %d %s", channel->ch_id, status);
21969 return buf;
21970 }
21971#endif
21972 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021973 case VAR_UNKNOWN:
21974 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021975 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021977 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978}
21979
21980/*
21981 * Find variable "name" in the list of variables.
21982 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021983 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021984 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021985 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021987 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021988find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021991 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992
Bram Moolenaara7043832005-01-21 11:56:39 +000021993 ht = find_var_ht(name, &varname);
21994 if (htp != NULL)
21995 *htp = ht;
21996 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021998 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999}
22000
22001/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022002 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022003 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022006find_var_in_ht(
22007 hashtab_T *ht,
22008 int htname,
22009 char_u *varname,
22010 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022011{
Bram Moolenaar33570922005-01-25 22:26:29 +000022012 hashitem_T *hi;
22013
22014 if (*varname == NUL)
22015 {
22016 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022017 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022018 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022019 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022020 case 'g': return &globvars_var;
22021 case 'v': return &vimvars_var;
22022 case 'b': return &curbuf->b_bufvar;
22023 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022024#ifdef FEAT_WINDOWS
22025 case 't': return &curtab->tp_winvar;
22026#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022027 case 'l': return current_funccal == NULL
22028 ? NULL : &current_funccal->l_vars_var;
22029 case 'a': return current_funccal == NULL
22030 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022031 }
22032 return NULL;
22033 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022034
22035 hi = hash_find(ht, varname);
22036 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022037 {
22038 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022039 * worked find the variable again. Don't auto-load a script if it was
22040 * loaded already, otherwise it would be loaded every time when
22041 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022042 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022043 {
22044 /* Note: script_autoload() may make "hi" invalid. It must either
22045 * be obtained again or not used. */
22046 if (!script_autoload(varname, FALSE) || aborting())
22047 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022048 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022049 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022050 if (HASHITEM_EMPTY(hi))
22051 return NULL;
22052 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022053 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022054}
22055
22056/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022057 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022058 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022059 * Set "varname" to the start of name without ':'.
22060 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022061 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022062find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022064 hashitem_T *hi;
22065
Bram Moolenaar73627d02015-08-11 15:46:09 +020022066 if (name[0] == NUL)
22067 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068 if (name[1] != ':')
22069 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022070 /* The name must not start with a colon or #. */
22071 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072 return NULL;
22073 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022074
22075 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022076 hi = hash_find(&compat_hashtab, name);
22077 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022078 return &compat_hashtab;
22079
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022081 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022082 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 }
22084 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022085 if (*name == 'g') /* global variable */
22086 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022087 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22088 */
22089 if (vim_strchr(name + 2, ':') != NULL
22090 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022091 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022093 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022095 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022096#ifdef FEAT_WINDOWS
22097 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022098 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022099#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022100 if (*name == 'v') /* v: variable */
22101 return &vimvarht;
22102 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022103 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022104 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022105 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106 if (*name == 's' /* script variable */
22107 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22108 return &SCRIPT_VARS(current_SID);
22109 return NULL;
22110}
22111
22112/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022113 * Get function call environment based on bactrace debug level
22114 */
22115 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022116get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022117{
22118 int i;
22119 funccall_T *funccal;
22120 funccall_T *temp_funccal;
22121
22122 funccal = current_funccal;
22123 if (debug_backtrace_level > 0)
22124 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022125 for (i = 0; i < debug_backtrace_level; i++)
22126 {
22127 temp_funccal = funccal->caller;
22128 if (temp_funccal)
22129 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022130 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022131 /* backtrace level overflow. reset to max */
22132 debug_backtrace_level = i;
22133 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022134 }
22135 return funccal;
22136}
22137
22138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022140 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 * Returns NULL when it doesn't exist.
22142 */
22143 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022144get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145{
Bram Moolenaar33570922005-01-25 22:26:29 +000022146 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022148 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 if (v == NULL)
22150 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022151 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152}
22153
22154/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022155 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 * sourcing this script and when executing functions defined in the script.
22157 */
22158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022159new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160{
Bram Moolenaara7043832005-01-21 11:56:39 +000022161 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022162 hashtab_T *ht;
22163 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022164
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22166 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022167 /* Re-allocating ga_data means that an ht_array pointing to
22168 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022169 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022170 for (i = 1; i <= ga_scripts.ga_len; ++i)
22171 {
22172 ht = &SCRIPT_VARS(i);
22173 if (ht->ht_mask == HT_INIT_SIZE - 1)
22174 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022175 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022176 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022177 }
22178
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179 while (ga_scripts.ga_len < id)
22180 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022181 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022182 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022183 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185 }
22186 }
22187}
22188
22189/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022190 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22191 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192 */
22193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022194init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195{
Bram Moolenaar33570922005-01-25 22:26:29 +000022196 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022197 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022198 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022199 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022200 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022201 dict_var->di_tv.vval.v_dict = dict;
22202 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022203 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022204 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22205 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206}
22207
22208/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022209 * Unreference a dictionary initialized by init_var_dict().
22210 */
22211 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022212unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022213{
22214 /* Now the dict needs to be freed if no one else is using it, go back to
22215 * normal reference counting. */
22216 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22217 dict_unref(dict);
22218}
22219
22220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022222 * Frees all allocated variables and the value they contain.
22223 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 */
22225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022226vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022227{
22228 vars_clear_ext(ht, TRUE);
22229}
22230
22231/*
22232 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22233 */
22234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022235vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236{
Bram Moolenaara7043832005-01-21 11:56:39 +000022237 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022238 hashitem_T *hi;
22239 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240
Bram Moolenaar33570922005-01-25 22:26:29 +000022241 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022242 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022243 for (hi = ht->ht_array; todo > 0; ++hi)
22244 {
22245 if (!HASHITEM_EMPTY(hi))
22246 {
22247 --todo;
22248
Bram Moolenaar33570922005-01-25 22:26:29 +000022249 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022250 * ht_array might change then. hash_clear() takes care of it
22251 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022252 v = HI2DI(hi);
22253 if (free_val)
22254 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022255 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022256 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022257 }
22258 }
22259 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022260 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261}
22262
Bram Moolenaara7043832005-01-21 11:56:39 +000022263/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022264 * Delete a variable from hashtab "ht" at item "hi".
22265 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022268delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269{
Bram Moolenaar33570922005-01-25 22:26:29 +000022270 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022271
22272 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022273 clear_tv(&di->di_tv);
22274 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275}
22276
22277/*
22278 * List the value of one internal variable.
22279 */
22280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022281list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022283 char_u *tofree;
22284 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022285 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022286
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022287 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022288 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022289 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022290 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291}
22292
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022294list_one_var_a(
22295 char_u *prefix,
22296 char_u *name,
22297 int type,
22298 char_u *string,
22299 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300{
Bram Moolenaar31859182007-08-14 20:41:13 +000022301 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22302 msg_start();
22303 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 if (name != NULL) /* "a:" vars don't have a name stored */
22305 msg_puts(name);
22306 msg_putchar(' ');
22307 msg_advance(22);
22308 if (type == VAR_NUMBER)
22309 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022310 else if (type == VAR_FUNC)
22311 msg_putchar('*');
22312 else if (type == VAR_LIST)
22313 {
22314 msg_putchar('[');
22315 if (*string == '[')
22316 ++string;
22317 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022318 else if (type == VAR_DICT)
22319 {
22320 msg_putchar('{');
22321 if (*string == '{')
22322 ++string;
22323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 else
22325 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022326
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022328
22329 if (type == VAR_FUNC)
22330 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022331 if (*first)
22332 {
22333 msg_clr_eos();
22334 *first = FALSE;
22335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336}
22337
22338/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022339 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 * If the variable already exists, the value is updated.
22341 * Otherwise the variable is created.
22342 */
22343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022344set_var(
22345 char_u *name,
22346 typval_T *tv,
22347 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348{
Bram Moolenaar33570922005-01-25 22:26:29 +000022349 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022351 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022353 ht = find_var_ht(name, &varname);
22354 if (ht == NULL || *varname == NUL)
22355 {
22356 EMSG2(_(e_illvar), name);
22357 return;
22358 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022359 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022360
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022361 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22362 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022363
Bram Moolenaar33570922005-01-25 22:26:29 +000022364 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022366 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022367 if (var_check_ro(v->di_flags, name, FALSE)
22368 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022369 return;
22370 if (v->di_tv.v_type != tv->v_type
22371 && !((v->di_tv.v_type == VAR_STRING
22372 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022373 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022374 || tv->v_type == VAR_NUMBER))
22375#ifdef FEAT_FLOAT
22376 && !((v->di_tv.v_type == VAR_NUMBER
22377 || v->di_tv.v_type == VAR_FLOAT)
22378 && (tv->v_type == VAR_NUMBER
22379 || tv->v_type == VAR_FLOAT))
22380#endif
22381 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022382 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022383 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022384 return;
22385 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022386
22387 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022388 * Handle setting internal v: variables separately where needed to
22389 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022390 */
22391 if (ht == &vimvarht)
22392 {
22393 if (v->di_tv.v_type == VAR_STRING)
22394 {
22395 vim_free(v->di_tv.vval.v_string);
22396 if (copy || tv->v_type != VAR_STRING)
22397 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22398 else
22399 {
22400 /* Take over the string to avoid an extra alloc/free. */
22401 v->di_tv.vval.v_string = tv->vval.v_string;
22402 tv->vval.v_string = NULL;
22403 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022404 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022405 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022406 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022407 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022408 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022409 if (STRCMP(varname, "searchforward") == 0)
22410 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022411#ifdef FEAT_SEARCH_EXTRA
22412 else if (STRCMP(varname, "hlsearch") == 0)
22413 {
22414 no_hlsearch = !v->di_tv.vval.v_number;
22415 redraw_all_later(SOME_VALID);
22416 }
22417#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022418 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022419 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022420 else if (v->di_tv.v_type != tv->v_type)
22421 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022422 }
22423
22424 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 }
22426 else /* add a new variable */
22427 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022428 /* Can't add "v:" variable. */
22429 if (ht == &vimvarht)
22430 {
22431 EMSG2(_(e_illvar), name);
22432 return;
22433 }
22434
Bram Moolenaar92124a32005-06-17 22:03:40 +000022435 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022436 if (!valid_varname(varname))
22437 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022438
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022439 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22440 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022441 if (v == NULL)
22442 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022443 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022444 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022446 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 return;
22448 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022449 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022451
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022452 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022453 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022454 else
22455 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022456 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022457 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022458 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460}
22461
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022462/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022463 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022464 * Also give an error message.
22465 */
22466 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022467var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022468{
22469 if (flags & DI_FLAGS_RO)
22470 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022471 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022472 return TRUE;
22473 }
22474 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22475 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022476 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022477 return TRUE;
22478 }
22479 return FALSE;
22480}
22481
22482/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022483 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22484 * Also give an error message.
22485 */
22486 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022487var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022488{
22489 if (flags & DI_FLAGS_FIX)
22490 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022491 EMSG2(_("E795: Cannot delete variable %s"),
22492 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022493 return TRUE;
22494 }
22495 return FALSE;
22496}
22497
22498/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022499 * Check if a funcref is assigned to a valid variable name.
22500 * Return TRUE and give an error if not.
22501 */
22502 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022503var_check_func_name(
22504 char_u *name, /* points to start of variable name */
22505 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022506{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022507 /* Allow for w: b: s: and t:. */
22508 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022509 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22510 ? name[2] : name[0]))
22511 {
22512 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22513 name);
22514 return TRUE;
22515 }
22516 /* Don't allow hiding a function. When "v" is not NULL we might be
22517 * assigning another function to the same var, the type is checked
22518 * below. */
22519 if (new_var && function_exists(name))
22520 {
22521 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22522 name);
22523 return TRUE;
22524 }
22525 return FALSE;
22526}
22527
22528/*
22529 * Check if a variable name is valid.
22530 * Return FALSE and give an error if not.
22531 */
22532 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022533valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022534{
22535 char_u *p;
22536
22537 for (p = varname; *p != NUL; ++p)
22538 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22539 && *p != AUTOLOAD_CHAR)
22540 {
22541 EMSG2(_(e_illvar), varname);
22542 return FALSE;
22543 }
22544 return TRUE;
22545}
22546
22547/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022548 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022549 * Also give an error message, using "name" or _("name") when use_gettext is
22550 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022551 */
22552 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022553tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022554{
22555 if (lock & VAR_LOCKED)
22556 {
22557 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022558 name == NULL ? (char_u *)_("Unknown")
22559 : use_gettext ? (char_u *)_(name)
22560 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022561 return TRUE;
22562 }
22563 if (lock & VAR_FIXED)
22564 {
22565 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022566 name == NULL ? (char_u *)_("Unknown")
22567 : use_gettext ? (char_u *)_(name)
22568 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022569 return TRUE;
22570 }
22571 return FALSE;
22572}
22573
22574/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022576 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022577 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022578 * It is OK for "from" and "to" to point to the same item. This is used to
22579 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022580 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022581 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022582copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022584 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022585 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022586 switch (from->v_type)
22587 {
22588 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022589 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022590 to->vval.v_number = from->vval.v_number;
22591 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022592 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022593#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022594 to->vval.v_float = from->vval.v_float;
22595 break;
22596#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022597 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022598#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022599 to->vval.v_job = from->vval.v_job;
22600 ++to->vval.v_job->jv_refcount;
22601 break;
22602#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022603 case VAR_CHANNEL:
22604#ifdef FEAT_CHANNEL
22605 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022606 if (to->vval.v_channel != NULL)
22607 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022608 break;
22609#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022610 case VAR_STRING:
22611 case VAR_FUNC:
22612 if (from->vval.v_string == NULL)
22613 to->vval.v_string = NULL;
22614 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022616 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022617 if (from->v_type == VAR_FUNC)
22618 func_ref(to->vval.v_string);
22619 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022620 break;
22621 case VAR_LIST:
22622 if (from->vval.v_list == NULL)
22623 to->vval.v_list = NULL;
22624 else
22625 {
22626 to->vval.v_list = from->vval.v_list;
22627 ++to->vval.v_list->lv_refcount;
22628 }
22629 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022630 case VAR_DICT:
22631 if (from->vval.v_dict == NULL)
22632 to->vval.v_dict = NULL;
22633 else
22634 {
22635 to->vval.v_dict = from->vval.v_dict;
22636 ++to->vval.v_dict->dv_refcount;
22637 }
22638 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022639 case VAR_UNKNOWN:
22640 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022641 break;
22642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643}
22644
22645/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022646 * Make a copy of an item.
22647 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022648 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22649 * reference to an already copied list/dict can be used.
22650 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022651 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022652 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022653item_copy(
22654 typval_T *from,
22655 typval_T *to,
22656 int deep,
22657 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022658{
22659 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022660 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022661
Bram Moolenaar33570922005-01-25 22:26:29 +000022662 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022663 {
22664 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022665 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022666 }
22667 ++recurse;
22668
22669 switch (from->v_type)
22670 {
22671 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022672 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022673 case VAR_STRING:
22674 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022675 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022676 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022677 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022678 copy_tv(from, to);
22679 break;
22680 case VAR_LIST:
22681 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022682 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022683 if (from->vval.v_list == NULL)
22684 to->vval.v_list = NULL;
22685 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22686 {
22687 /* use the copy made earlier */
22688 to->vval.v_list = from->vval.v_list->lv_copylist;
22689 ++to->vval.v_list->lv_refcount;
22690 }
22691 else
22692 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22693 if (to->vval.v_list == NULL)
22694 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022695 break;
22696 case VAR_DICT:
22697 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022698 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022699 if (from->vval.v_dict == NULL)
22700 to->vval.v_dict = NULL;
22701 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22702 {
22703 /* use the copy made earlier */
22704 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22705 ++to->vval.v_dict->dv_refcount;
22706 }
22707 else
22708 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22709 if (to->vval.v_dict == NULL)
22710 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022711 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022712 case VAR_UNKNOWN:
22713 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022714 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022715 }
22716 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022717 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022718}
22719
22720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 * ":echo expr1 ..." print each argument separated with a space, add a
22722 * newline at the end.
22723 * ":echon expr1 ..." print each argument plain.
22724 */
22725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022726ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727{
22728 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022729 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022730 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 char_u *p;
22732 int needclr = TRUE;
22733 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022734 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735
22736 if (eap->skip)
22737 ++emsg_skip;
22738 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22739 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022740 /* If eval1() causes an error message the text from the command may
22741 * still need to be cleared. E.g., "echo 22,44". */
22742 need_clr_eos = needclr;
22743
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022745 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 {
22747 /*
22748 * Report the invalid expression unless the expression evaluation
22749 * has been cancelled due to an aborting error, an interrupt, or an
22750 * exception.
22751 */
22752 if (!aborting())
22753 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022754 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 break;
22756 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022757 need_clr_eos = FALSE;
22758
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 if (!eap->skip)
22760 {
22761 if (atstart)
22762 {
22763 atstart = FALSE;
22764 /* Call msg_start() after eval1(), evaluating the expression
22765 * may cause a message to appear. */
22766 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022767 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022768 /* Mark the saved text as finishing the line, so that what
22769 * follows is displayed on a new line when scrolling back
22770 * at the more prompt. */
22771 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 }
22775 else if (eap->cmdidx == CMD_echo)
22776 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022777 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022778 if (p != NULL)
22779 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022781 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022783 if (*p != TAB && needclr)
22784 {
22785 /* remove any text still there from the command */
22786 msg_clr_eos();
22787 needclr = FALSE;
22788 }
22789 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 }
22791 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022792 {
22793#ifdef FEAT_MBYTE
22794 if (has_mbyte)
22795 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022796 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022797
22798 (void)msg_outtrans_len_attr(p, i, echo_attr);
22799 p += i - 1;
22800 }
22801 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022802#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022803 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022806 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022808 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 arg = skipwhite(arg);
22810 }
22811 eap->nextcmd = check_nextcmd(arg);
22812
22813 if (eap->skip)
22814 --emsg_skip;
22815 else
22816 {
22817 /* remove text that may still be there from the command */
22818 if (needclr)
22819 msg_clr_eos();
22820 if (eap->cmdidx == CMD_echo)
22821 msg_end();
22822 }
22823}
22824
22825/*
22826 * ":echohl {name}".
22827 */
22828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022829ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830{
22831 int id;
22832
22833 id = syn_name2id(eap->arg);
22834 if (id == 0)
22835 echo_attr = 0;
22836 else
22837 echo_attr = syn_id2attr(id);
22838}
22839
22840/*
22841 * ":execute expr1 ..." execute the result of an expression.
22842 * ":echomsg expr1 ..." Print a message
22843 * ":echoerr expr1 ..." Print an error
22844 * Each gets spaces around each argument and a newline at the end for
22845 * echo commands
22846 */
22847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022848ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849{
22850 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 int ret = OK;
22853 char_u *p;
22854 garray_T ga;
22855 int len;
22856 int save_did_emsg;
22857
22858 ga_init2(&ga, 1, 80);
22859
22860 if (eap->skip)
22861 ++emsg_skip;
22862 while (*arg != NUL && *arg != '|' && *arg != '\n')
22863 {
22864 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022865 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866 {
22867 /*
22868 * Report the invalid expression unless the expression evaluation
22869 * has been cancelled due to an aborting error, an interrupt, or an
22870 * exception.
22871 */
22872 if (!aborting())
22873 EMSG2(_(e_invexpr2), p);
22874 ret = FAIL;
22875 break;
22876 }
22877
22878 if (!eap->skip)
22879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022880 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 len = (int)STRLEN(p);
22882 if (ga_grow(&ga, len + 2) == FAIL)
22883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022884 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 ret = FAIL;
22886 break;
22887 }
22888 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 ga.ga_len += len;
22892 }
22893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 arg = skipwhite(arg);
22896 }
22897
22898 if (ret != FAIL && ga.ga_data != NULL)
22899 {
22900 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022903 out_flush();
22904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 else if (eap->cmdidx == CMD_echoerr)
22906 {
22907 /* We don't want to abort following commands, restore did_emsg. */
22908 save_did_emsg = did_emsg;
22909 EMSG((char_u *)ga.ga_data);
22910 if (!force_abort)
22911 did_emsg = save_did_emsg;
22912 }
22913 else if (eap->cmdidx == CMD_execute)
22914 do_cmdline((char_u *)ga.ga_data,
22915 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22916 }
22917
22918 ga_clear(&ga);
22919
22920 if (eap->skip)
22921 --emsg_skip;
22922
22923 eap->nextcmd = check_nextcmd(arg);
22924}
22925
22926/*
22927 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22928 * "arg" points to the "&" or '+' when called, to "option" when returning.
22929 * Returns NULL when no option name found. Otherwise pointer to the char
22930 * after the option name.
22931 */
22932 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022933find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934{
22935 char_u *p = *arg;
22936
22937 ++p;
22938 if (*p == 'g' && p[1] == ':')
22939 {
22940 *opt_flags = OPT_GLOBAL;
22941 p += 2;
22942 }
22943 else if (*p == 'l' && p[1] == ':')
22944 {
22945 *opt_flags = OPT_LOCAL;
22946 p += 2;
22947 }
22948 else
22949 *opt_flags = 0;
22950
22951 if (!ASCII_ISALPHA(*p))
22952 return NULL;
22953 *arg = p;
22954
22955 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22956 p += 4; /* termcap option */
22957 else
22958 while (ASCII_ISALPHA(*p))
22959 ++p;
22960 return p;
22961}
22962
22963/*
22964 * ":function"
22965 */
22966 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022967ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968{
22969 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022970 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 int j;
22972 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022974 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 char_u *name = NULL;
22976 char_u *p;
22977 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022978 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 garray_T newargs;
22980 garray_T newlines;
22981 int varargs = FALSE;
22982 int mustend = FALSE;
22983 int flags = 0;
22984 ufunc_T *fp;
22985 int indent;
22986 int nesting;
22987 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022988 dictitem_T *v;
22989 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022990 static int func_nr = 0; /* number for nameless function */
22991 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022992 hashtab_T *ht;
22993 int todo;
22994 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022995 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996
22997 /*
22998 * ":function" without argument: list functions.
22999 */
23000 if (ends_excmd(*eap->arg))
23001 {
23002 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023003 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023004 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023005 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023006 {
23007 if (!HASHITEM_EMPTY(hi))
23008 {
23009 --todo;
23010 fp = HI2UF(hi);
23011 if (!isdigit(*fp->uf_name))
23012 list_func_head(fp, FALSE);
23013 }
23014 }
23015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 eap->nextcmd = check_nextcmd(eap->arg);
23017 return;
23018 }
23019
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023020 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023021 * ":function /pat": list functions matching pattern.
23022 */
23023 if (*eap->arg == '/')
23024 {
23025 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23026 if (!eap->skip)
23027 {
23028 regmatch_T regmatch;
23029
23030 c = *p;
23031 *p = NUL;
23032 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23033 *p = c;
23034 if (regmatch.regprog != NULL)
23035 {
23036 regmatch.rm_ic = p_ic;
23037
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023038 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023039 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23040 {
23041 if (!HASHITEM_EMPTY(hi))
23042 {
23043 --todo;
23044 fp = HI2UF(hi);
23045 if (!isdigit(*fp->uf_name)
23046 && vim_regexec(&regmatch, fp->uf_name, 0))
23047 list_func_head(fp, FALSE);
23048 }
23049 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023050 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023051 }
23052 }
23053 if (*p == '/')
23054 ++p;
23055 eap->nextcmd = check_nextcmd(p);
23056 return;
23057 }
23058
23059 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023060 * Get the function name. There are these situations:
23061 * func normal function name
23062 * "name" == func, "fudi.fd_dict" == NULL
23063 * dict.func new dictionary entry
23064 * "name" == NULL, "fudi.fd_dict" set,
23065 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23066 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023067 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023068 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23069 * dict.func existing dict entry that's not a Funcref
23070 * "name" == NULL, "fudi.fd_dict" set,
23071 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023072 * s:func script-local function name
23073 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023076 name = trans_function_name(&p, eap->skip, 0, &fudi);
23077 paren = (vim_strchr(p, '(') != NULL);
23078 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 {
23080 /*
23081 * Return on an invalid expression in braces, unless the expression
23082 * evaluation has been cancelled due to an aborting error, an
23083 * interrupt, or an exception.
23084 */
23085 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023087 if (!eap->skip && fudi.fd_newkey != NULL)
23088 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023089 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 else
23093 eap->skip = TRUE;
23094 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023095
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 /* An error in a function call during evaluation of an expression in magic
23097 * braces should not cause the function not to be defined. */
23098 saved_did_emsg = did_emsg;
23099 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100
23101 /*
23102 * ":function func" with only function name: list function.
23103 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023104 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023105 {
23106 if (!ends_excmd(*skipwhite(p)))
23107 {
23108 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023109 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 }
23111 eap->nextcmd = check_nextcmd(p);
23112 if (eap->nextcmd != NULL)
23113 *p = NUL;
23114 if (!eap->skip && !got_int)
23115 {
23116 fp = find_func(name);
23117 if (fp != NULL)
23118 {
23119 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023120 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023122 if (FUNCLINE(fp, j) == NULL)
23123 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 msg_putchar('\n');
23125 msg_outnum((long)(j + 1));
23126 if (j < 9)
23127 msg_putchar(' ');
23128 if (j < 99)
23129 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023130 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131 out_flush(); /* show a line at a time */
23132 ui_breakcheck();
23133 }
23134 if (!got_int)
23135 {
23136 msg_putchar('\n');
23137 msg_puts((char_u *)" endfunction");
23138 }
23139 }
23140 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023141 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023143 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144 }
23145
23146 /*
23147 * ":function name(arg1, arg2)" Define function.
23148 */
23149 p = skipwhite(p);
23150 if (*p != '(')
23151 {
23152 if (!eap->skip)
23153 {
23154 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023155 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 }
23157 /* attempt to continue by skipping some text */
23158 if (vim_strchr(p, '(') != NULL)
23159 p = vim_strchr(p, '(');
23160 }
23161 p = skipwhite(p + 1);
23162
23163 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23164 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23165
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023166 if (!eap->skip)
23167 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023168 /* Check the name of the function. Unless it's a dictionary function
23169 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023170 if (name != NULL)
23171 arg = name;
23172 else
23173 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023174 if (arg != NULL && (fudi.fd_di == NULL
23175 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023176 {
23177 if (*arg == K_SPECIAL)
23178 j = 3;
23179 else
23180 j = 0;
23181 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23182 : eval_isnamec(arg[j])))
23183 ++j;
23184 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023185 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023186 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023187 /* Disallow using the g: dict. */
23188 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23189 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023190 }
23191
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192 /*
23193 * Isolate the arguments: "arg1, arg2, ...)"
23194 */
23195 while (*p != ')')
23196 {
23197 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23198 {
23199 varargs = TRUE;
23200 p += 3;
23201 mustend = TRUE;
23202 }
23203 else
23204 {
23205 arg = p;
23206 while (ASCII_ISALNUM(*p) || *p == '_')
23207 ++p;
23208 if (arg == p || isdigit(*arg)
23209 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23210 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23211 {
23212 if (!eap->skip)
23213 EMSG2(_("E125: Illegal argument: %s"), arg);
23214 break;
23215 }
23216 if (ga_grow(&newargs, 1) == FAIL)
23217 goto erret;
23218 c = *p;
23219 *p = NUL;
23220 arg = vim_strsave(arg);
23221 if (arg == NULL)
23222 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023223
23224 /* Check for duplicate argument name. */
23225 for (i = 0; i < newargs.ga_len; ++i)
23226 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23227 {
23228 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023229 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023230 goto erret;
23231 }
23232
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23234 *p = c;
23235 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 if (*p == ',')
23237 ++p;
23238 else
23239 mustend = TRUE;
23240 }
23241 p = skipwhite(p);
23242 if (mustend && *p != ')')
23243 {
23244 if (!eap->skip)
23245 EMSG2(_(e_invarg2), eap->arg);
23246 break;
23247 }
23248 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023249 if (*p != ')')
23250 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251 ++p; /* skip the ')' */
23252
Bram Moolenaare9a41262005-01-15 22:18:47 +000023253 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023254 for (;;)
23255 {
23256 p = skipwhite(p);
23257 if (STRNCMP(p, "range", 5) == 0)
23258 {
23259 flags |= FC_RANGE;
23260 p += 5;
23261 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023262 else if (STRNCMP(p, "dict", 4) == 0)
23263 {
23264 flags |= FC_DICT;
23265 p += 4;
23266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267 else if (STRNCMP(p, "abort", 5) == 0)
23268 {
23269 flags |= FC_ABORT;
23270 p += 5;
23271 }
23272 else
23273 break;
23274 }
23275
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023276 /* When there is a line break use what follows for the function body.
23277 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23278 if (*p == '\n')
23279 line_arg = p + 1;
23280 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 EMSG(_(e_trailing));
23282
23283 /*
23284 * Read the body of the function, until ":endfunction" is found.
23285 */
23286 if (KeyTyped)
23287 {
23288 /* Check if the function already exists, don't let the user type the
23289 * whole function before telling him it doesn't work! For a script we
23290 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023291 if (!eap->skip && !eap->forceit)
23292 {
23293 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23294 EMSG(_(e_funcdict));
23295 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023296 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023299 if (!eap->skip && did_emsg)
23300 goto erret;
23301
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 msg_putchar('\n'); /* don't overwrite the function name */
23303 cmdline_row = msg_row;
23304 }
23305
23306 indent = 2;
23307 nesting = 0;
23308 for (;;)
23309 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023310 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023311 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023312 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023313 saved_wait_return = FALSE;
23314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023316 sourcing_lnum_off = sourcing_lnum;
23317
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023318 if (line_arg != NULL)
23319 {
23320 /* Use eap->arg, split up in parts by line breaks. */
23321 theline = line_arg;
23322 p = vim_strchr(theline, '\n');
23323 if (p == NULL)
23324 line_arg += STRLEN(line_arg);
23325 else
23326 {
23327 *p = NUL;
23328 line_arg = p + 1;
23329 }
23330 }
23331 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023332 theline = getcmdline(':', 0L, indent);
23333 else
23334 theline = eap->getline(':', eap->cookie, indent);
23335 if (KeyTyped)
23336 lines_left = Rows - 1;
23337 if (theline == NULL)
23338 {
23339 EMSG(_("E126: Missing :endfunction"));
23340 goto erret;
23341 }
23342
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023343 /* Detect line continuation: sourcing_lnum increased more than one. */
23344 if (sourcing_lnum > sourcing_lnum_off + 1)
23345 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23346 else
23347 sourcing_lnum_off = 0;
23348
Bram Moolenaar071d4272004-06-13 20:20:40 +000023349 if (skip_until != NULL)
23350 {
23351 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23352 * don't check for ":endfunc". */
23353 if (STRCMP(theline, skip_until) == 0)
23354 {
23355 vim_free(skip_until);
23356 skip_until = NULL;
23357 }
23358 }
23359 else
23360 {
23361 /* skip ':' and blanks*/
23362 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23363 ;
23364
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023365 /* Check for "endfunction". */
23366 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023368 if (line_arg == NULL)
23369 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 break;
23371 }
23372
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023373 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 * at "end". */
23375 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23376 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023377 else if (STRNCMP(p, "if", 2) == 0
23378 || STRNCMP(p, "wh", 2) == 0
23379 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 || STRNCMP(p, "try", 3) == 0)
23381 indent += 2;
23382
23383 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023384 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023385 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023386 if (*p == '!')
23387 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023389 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23390 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023392 ++nesting;
23393 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394 }
23395 }
23396
23397 /* Check for ":append" or ":insert". */
23398 p = skip_range(p, NULL);
23399 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23400 || (p[0] == 'i'
23401 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23402 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23403 skip_until = vim_strsave((char_u *)".");
23404
23405 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23406 arg = skipwhite(skiptowhite(p));
23407 if (arg[0] == '<' && arg[1] =='<'
23408 && ((p[0] == 'p' && p[1] == 'y'
23409 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23410 || (p[0] == 'p' && p[1] == 'e'
23411 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23412 || (p[0] == 't' && p[1] == 'c'
23413 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023414 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23415 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023416 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23417 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023418 || (p[0] == 'm' && p[1] == 'z'
23419 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023420 ))
23421 {
23422 /* ":python <<" continues until a dot, like ":append" */
23423 p = skipwhite(arg + 2);
23424 if (*p == NUL)
23425 skip_until = vim_strsave((char_u *)".");
23426 else
23427 skip_until = vim_strsave(p);
23428 }
23429 }
23430
23431 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023432 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023433 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023434 if (line_arg == NULL)
23435 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023436 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023437 }
23438
23439 /* Copy the line to newly allocated memory. get_one_sourceline()
23440 * allocates 250 bytes per line, this saves 80% on average. The cost
23441 * is an extra alloc/free. */
23442 p = vim_strsave(theline);
23443 if (p != NULL)
23444 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023445 if (line_arg == NULL)
23446 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023447 theline = p;
23448 }
23449
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023450 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23451
23452 /* Add NULL lines for continuation lines, so that the line count is
23453 * equal to the index in the growarray. */
23454 while (sourcing_lnum_off-- > 0)
23455 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023456
23457 /* Check for end of eap->arg. */
23458 if (line_arg != NULL && *line_arg == NUL)
23459 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 }
23461
23462 /* Don't define the function when skipping commands or when an error was
23463 * detected. */
23464 if (eap->skip || did_emsg)
23465 goto erret;
23466
23467 /*
23468 * If there are no errors, add the function
23469 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023470 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023471 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023472 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023473 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023474 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023475 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023476 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023477 goto erret;
23478 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023479
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023480 fp = find_func(name);
23481 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023483 if (!eap->forceit)
23484 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023485 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023486 goto erret;
23487 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023488 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023489 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023490 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023491 name);
23492 goto erret;
23493 }
23494 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023495 ga_clear_strings(&(fp->uf_args));
23496 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023497 vim_free(name);
23498 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 }
23501 else
23502 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023503 char numbuf[20];
23504
23505 fp = NULL;
23506 if (fudi.fd_newkey == NULL && !eap->forceit)
23507 {
23508 EMSG(_(e_funcdict));
23509 goto erret;
23510 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023511 if (fudi.fd_di == NULL)
23512 {
23513 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023514 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023515 goto erret;
23516 }
23517 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023518 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023519 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023520
23521 /* Give the function a sequential number. Can only be used with a
23522 * Funcref! */
23523 vim_free(name);
23524 sprintf(numbuf, "%d", ++func_nr);
23525 name = vim_strsave((char_u *)numbuf);
23526 if (name == NULL)
23527 goto erret;
23528 }
23529
23530 if (fp == NULL)
23531 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023532 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023533 {
23534 int slen, plen;
23535 char_u *scriptname;
23536
23537 /* Check that the autoload name matches the script name. */
23538 j = FAIL;
23539 if (sourcing_name != NULL)
23540 {
23541 scriptname = autoload_name(name);
23542 if (scriptname != NULL)
23543 {
23544 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023545 plen = (int)STRLEN(p);
23546 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023547 if (slen > plen && fnamecmp(p,
23548 sourcing_name + slen - plen) == 0)
23549 j = OK;
23550 vim_free(scriptname);
23551 }
23552 }
23553 if (j == FAIL)
23554 {
23555 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23556 goto erret;
23557 }
23558 }
23559
23560 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 if (fp == NULL)
23562 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023563
23564 if (fudi.fd_dict != NULL)
23565 {
23566 if (fudi.fd_di == NULL)
23567 {
23568 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023569 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023570 if (fudi.fd_di == NULL)
23571 {
23572 vim_free(fp);
23573 goto erret;
23574 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023575 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23576 {
23577 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023578 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023579 goto erret;
23580 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023581 }
23582 else
23583 /* overwrite existing dict entry */
23584 clear_tv(&fudi.fd_di->di_tv);
23585 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023586 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023587 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023588 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023589
23590 /* behave like "dict" was used */
23591 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023592 }
23593
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023595 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023596 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23597 {
23598 vim_free(fp);
23599 goto erret;
23600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023602 fp->uf_args = newargs;
23603 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023604#ifdef FEAT_PROFILE
23605 fp->uf_tml_count = NULL;
23606 fp->uf_tml_total = NULL;
23607 fp->uf_tml_self = NULL;
23608 fp->uf_profiling = FALSE;
23609 if (prof_def_func())
23610 func_do_profile(fp);
23611#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023612 fp->uf_varargs = varargs;
23613 fp->uf_flags = flags;
23614 fp->uf_calls = 0;
23615 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023616 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617
23618erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023619 ga_clear_strings(&newargs);
23620 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023621ret_free:
23622 vim_free(skip_until);
23623 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023626 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627}
23628
23629/*
23630 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023631 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023633 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023634 * TFN_INT: internal function name OK
23635 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023636 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637 * Advances "pp" to just after the function name (if no error).
23638 */
23639 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023640trans_function_name(
23641 char_u **pp,
23642 int skip, /* only find the end, don't evaluate */
23643 int flags,
23644 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023646 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 char_u *start;
23648 char_u *end;
23649 int lead;
23650 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023652 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023653
23654 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023655 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023657
23658 /* Check for hard coded <SNR>: already translated function ID (from a user
23659 * command). */
23660 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23661 && (*pp)[2] == (int)KE_SNR)
23662 {
23663 *pp += 3;
23664 len = get_id_len(pp) + 3;
23665 return vim_strnsave(start, len);
23666 }
23667
23668 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23669 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023671 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023673
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023674 /* Note that TFN_ flags use the same values as GLV_ flags. */
23675 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023676 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023677 if (end == start)
23678 {
23679 if (!skip)
23680 EMSG(_("E129: Function name required"));
23681 goto theend;
23682 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023683 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023684 {
23685 /*
23686 * Report an invalid expression in braces, unless the expression
23687 * evaluation has been cancelled due to an aborting error, an
23688 * interrupt, or an exception.
23689 */
23690 if (!aborting())
23691 {
23692 if (end != NULL)
23693 EMSG2(_(e_invarg2), start);
23694 }
23695 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023696 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023697 goto theend;
23698 }
23699
23700 if (lv.ll_tv != NULL)
23701 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023702 if (fdp != NULL)
23703 {
23704 fdp->fd_dict = lv.ll_dict;
23705 fdp->fd_newkey = lv.ll_newkey;
23706 lv.ll_newkey = NULL;
23707 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023708 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023709 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23710 {
23711 name = vim_strsave(lv.ll_tv->vval.v_string);
23712 *pp = end;
23713 }
23714 else
23715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023716 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23717 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023718 EMSG(_(e_funcref));
23719 else
23720 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023721 name = NULL;
23722 }
23723 goto theend;
23724 }
23725
23726 if (lv.ll_name == NULL)
23727 {
23728 /* Error found, but continue after the function name. */
23729 *pp = end;
23730 goto theend;
23731 }
23732
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023733 /* Check if the name is a Funcref. If so, use the value. */
23734 if (lv.ll_exp_name != NULL)
23735 {
23736 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023737 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023738 if (name == lv.ll_exp_name)
23739 name = NULL;
23740 }
23741 else
23742 {
23743 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023744 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023745 if (name == *pp)
23746 name = NULL;
23747 }
23748 if (name != NULL)
23749 {
23750 name = vim_strsave(name);
23751 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023752 if (STRNCMP(name, "<SNR>", 5) == 0)
23753 {
23754 /* Change "<SNR>" to the byte sequence. */
23755 name[0] = K_SPECIAL;
23756 name[1] = KS_EXTRA;
23757 name[2] = (int)KE_SNR;
23758 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23759 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023760 goto theend;
23761 }
23762
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023763 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023764 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023765 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023766 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23767 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23768 {
23769 /* When there was "s:" already or the name expanded to get a
23770 * leading "s:" then remove it. */
23771 lv.ll_name += 2;
23772 len -= 2;
23773 lead = 2;
23774 }
23775 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023776 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023777 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023778 /* skip over "s:" and "g:" */
23779 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023780 lv.ll_name += 2;
23781 len = (int)(end - lv.ll_name);
23782 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023783
23784 /*
23785 * Copy the function name to allocated memory.
23786 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23787 * Accept <SNR>123_name() outside a script.
23788 */
23789 if (skip)
23790 lead = 0; /* do nothing */
23791 else if (lead > 0)
23792 {
23793 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023794 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23795 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023796 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023797 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023798 if (current_SID <= 0)
23799 {
23800 EMSG(_(e_usingsid));
23801 goto theend;
23802 }
23803 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23804 lead += (int)STRLEN(sid_buf);
23805 }
23806 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023807 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023808 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023809 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023810 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023811 goto theend;
23812 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023813 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023814 {
23815 char_u *cp = vim_strchr(lv.ll_name, ':');
23816
23817 if (cp != NULL && cp < end)
23818 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023819 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023820 goto theend;
23821 }
23822 }
23823
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023824 name = alloc((unsigned)(len + lead + 1));
23825 if (name != NULL)
23826 {
23827 if (lead > 0)
23828 {
23829 name[0] = K_SPECIAL;
23830 name[1] = KS_EXTRA;
23831 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023832 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023833 STRCPY(name + 3, sid_buf);
23834 }
23835 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023836 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023837 }
23838 *pp = end;
23839
23840theend:
23841 clear_lval(&lv);
23842 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843}
23844
23845/*
23846 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23847 * Return 2 if "p" starts with "s:".
23848 * Return 0 otherwise.
23849 */
23850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023851eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023853 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23854 * the standard library function. */
23855 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23856 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 return 5;
23858 if (p[0] == 's' && p[1] == ':')
23859 return 2;
23860 return 0;
23861}
23862
23863/*
23864 * Return TRUE if "p" starts with "<SID>" or "s:".
23865 * Only works if eval_fname_script() returned non-zero for "p"!
23866 */
23867 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023868eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869{
23870 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23871}
23872
23873/*
23874 * List the head of the function: "name(arg1, arg2)".
23875 */
23876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023877list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878{
23879 int j;
23880
23881 msg_start();
23882 if (indent)
23883 MSG_PUTS(" ");
23884 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023885 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 {
23887 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023888 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 }
23890 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023891 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023893 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 {
23895 if (j)
23896 MSG_PUTS(", ");
23897 msg_puts(FUNCARG(fp, j));
23898 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023899 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 {
23901 if (j)
23902 MSG_PUTS(", ");
23903 MSG_PUTS("...");
23904 }
23905 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023906 if (fp->uf_flags & FC_ABORT)
23907 MSG_PUTS(" abort");
23908 if (fp->uf_flags & FC_RANGE)
23909 MSG_PUTS(" range");
23910 if (fp->uf_flags & FC_DICT)
23911 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023912 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023913 if (p_verbose > 0)
23914 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915}
23916
23917/*
23918 * Find a function by name, return pointer to it in ufuncs.
23919 * Return NULL for unknown function.
23920 */
23921 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023922find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023924 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023926 hi = hash_find(&func_hashtab, name);
23927 if (!HASHITEM_EMPTY(hi))
23928 return HI2UF(hi);
23929 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930}
23931
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023932#if defined(EXITFREE) || defined(PROTO)
23933 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023934free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023935{
23936 hashitem_T *hi;
23937
23938 /* Need to start all over every time, because func_free() may change the
23939 * hash table. */
23940 while (func_hashtab.ht_used > 0)
23941 for (hi = func_hashtab.ht_array; ; ++hi)
23942 if (!HASHITEM_EMPTY(hi))
23943 {
23944 func_free(HI2UF(hi));
23945 break;
23946 }
23947}
23948#endif
23949
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023950 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023951translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023952{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023953 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023954 return find_internal_func(name) >= 0;
23955 return find_func(name) != NULL;
23956}
23957
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023958/*
23959 * Return TRUE if a function "name" exists.
23960 */
23961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023962function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023963{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023964 char_u *nm = name;
23965 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023966 int n = FALSE;
23967
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023968 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23969 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023970 nm = skipwhite(nm);
23971
23972 /* Only accept "funcname", "funcname ", "funcname (..." and
23973 * "funcname(...", not "funcname!...". */
23974 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023975 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023976 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023977 return n;
23978}
23979
Bram Moolenaara1544c02013-05-30 12:35:52 +020023980 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023981get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023982{
23983 char_u *nm = name;
23984 char_u *p;
23985
23986 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23987
23988 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023989 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023990 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023991
Bram Moolenaara1544c02013-05-30 12:35:52 +020023992 vim_free(p);
23993 return NULL;
23994}
23995
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023996/*
23997 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023998 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23999 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024000 */
24001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024002builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024003{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024004 char_u *p;
24005
24006 if (!ASCII_ISLOWER(name[0]))
24007 return FALSE;
24008 p = vim_strchr(name, AUTOLOAD_CHAR);
24009 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024010}
24011
Bram Moolenaar05159a02005-02-26 23:04:13 +000024012#if defined(FEAT_PROFILE) || defined(PROTO)
24013/*
24014 * Start profiling function "fp".
24015 */
24016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024017func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024018{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024019 int len = fp->uf_lines.ga_len;
24020
24021 if (len == 0)
24022 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024023 fp->uf_tm_count = 0;
24024 profile_zero(&fp->uf_tm_self);
24025 profile_zero(&fp->uf_tm_total);
24026 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024027 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024028 if (fp->uf_tml_total == NULL)
24029 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024030 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024031 if (fp->uf_tml_self == NULL)
24032 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024033 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024034 fp->uf_tml_idx = -1;
24035 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24036 || fp->uf_tml_self == NULL)
24037 return; /* out of memory */
24038
24039 fp->uf_profiling = TRUE;
24040}
24041
24042/*
24043 * Dump the profiling results for all functions in file "fd".
24044 */
24045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024046func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024047{
24048 hashitem_T *hi;
24049 int todo;
24050 ufunc_T *fp;
24051 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024052 ufunc_T **sorttab;
24053 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024054
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024055 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024056 if (todo == 0)
24057 return; /* nothing to dump */
24058
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024059 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024060
Bram Moolenaar05159a02005-02-26 23:04:13 +000024061 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24062 {
24063 if (!HASHITEM_EMPTY(hi))
24064 {
24065 --todo;
24066 fp = HI2UF(hi);
24067 if (fp->uf_profiling)
24068 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024069 if (sorttab != NULL)
24070 sorttab[st_len++] = fp;
24071
Bram Moolenaar05159a02005-02-26 23:04:13 +000024072 if (fp->uf_name[0] == K_SPECIAL)
24073 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24074 else
24075 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24076 if (fp->uf_tm_count == 1)
24077 fprintf(fd, "Called 1 time\n");
24078 else
24079 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24080 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24081 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24082 fprintf(fd, "\n");
24083 fprintf(fd, "count total (s) self (s)\n");
24084
24085 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24086 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024087 if (FUNCLINE(fp, i) == NULL)
24088 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024089 prof_func_line(fd, fp->uf_tml_count[i],
24090 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024091 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24092 }
24093 fprintf(fd, "\n");
24094 }
24095 }
24096 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024097
24098 if (sorttab != NULL && st_len > 0)
24099 {
24100 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24101 prof_total_cmp);
24102 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24103 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24104 prof_self_cmp);
24105 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24106 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024107
24108 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024109}
Bram Moolenaar73830342005-02-28 22:48:19 +000024110
24111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024112prof_sort_list(
24113 FILE *fd,
24114 ufunc_T **sorttab,
24115 int st_len,
24116 char *title,
24117 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024118{
24119 int i;
24120 ufunc_T *fp;
24121
24122 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24123 fprintf(fd, "count total (s) self (s) function\n");
24124 for (i = 0; i < 20 && i < st_len; ++i)
24125 {
24126 fp = sorttab[i];
24127 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24128 prefer_self);
24129 if (fp->uf_name[0] == K_SPECIAL)
24130 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24131 else
24132 fprintf(fd, " %s()\n", fp->uf_name);
24133 }
24134 fprintf(fd, "\n");
24135}
24136
24137/*
24138 * Print the count and times for one function or function line.
24139 */
24140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024141prof_func_line(
24142 FILE *fd,
24143 int count,
24144 proftime_T *total,
24145 proftime_T *self,
24146 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024147{
24148 if (count > 0)
24149 {
24150 fprintf(fd, "%5d ", count);
24151 if (prefer_self && profile_equal(total, self))
24152 fprintf(fd, " ");
24153 else
24154 fprintf(fd, "%s ", profile_msg(total));
24155 if (!prefer_self && profile_equal(total, self))
24156 fprintf(fd, " ");
24157 else
24158 fprintf(fd, "%s ", profile_msg(self));
24159 }
24160 else
24161 fprintf(fd, " ");
24162}
24163
24164/*
24165 * Compare function for total time sorting.
24166 */
24167 static int
24168#ifdef __BORLANDC__
24169_RTLENTRYF
24170#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024171prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024172{
24173 ufunc_T *p1, *p2;
24174
24175 p1 = *(ufunc_T **)s1;
24176 p2 = *(ufunc_T **)s2;
24177 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24178}
24179
24180/*
24181 * Compare function for self time sorting.
24182 */
24183 static int
24184#ifdef __BORLANDC__
24185_RTLENTRYF
24186#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024187prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024188{
24189 ufunc_T *p1, *p2;
24190
24191 p1 = *(ufunc_T **)s1;
24192 p2 = *(ufunc_T **)s2;
24193 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24194}
24195
Bram Moolenaar05159a02005-02-26 23:04:13 +000024196#endif
24197
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024198/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024199 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024200 * Return TRUE if a package was loaded.
24201 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024202 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024203script_autoload(
24204 char_u *name,
24205 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024206{
24207 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024208 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024209 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024210 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024211
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024212 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024213 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024214 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024215 return FALSE;
24216
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024217 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024218
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024219 /* Find the name in the list of previously loaded package names. Skip
24220 * "autoload/", it's always the same. */
24221 for (i = 0; i < ga_loaded.ga_len; ++i)
24222 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24223 break;
24224 if (!reload && i < ga_loaded.ga_len)
24225 ret = FALSE; /* was loaded already */
24226 else
24227 {
24228 /* Remember the name if it wasn't loaded already. */
24229 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24230 {
24231 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24232 tofree = NULL;
24233 }
24234
24235 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024236 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024237 ret = TRUE;
24238 }
24239
24240 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024241 return ret;
24242}
24243
24244/*
24245 * Return the autoload script name for a function or variable name.
24246 * Returns NULL when out of memory.
24247 */
24248 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024249autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024250{
24251 char_u *p;
24252 char_u *scriptname;
24253
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024254 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024255 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24256 if (scriptname == NULL)
24257 return FALSE;
24258 STRCPY(scriptname, "autoload/");
24259 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024260 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024261 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024262 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024263 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024264 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024265}
24266
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24268
24269/*
24270 * Function given to ExpandGeneric() to obtain the list of user defined
24271 * function names.
24272 */
24273 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024274get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024276 static long_u done;
24277 static hashitem_T *hi;
24278 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279
24280 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024282 done = 0;
24283 hi = func_hashtab.ht_array;
24284 }
24285 if (done < func_hashtab.ht_used)
24286 {
24287 if (done++ > 0)
24288 ++hi;
24289 while (HASHITEM_EMPTY(hi))
24290 ++hi;
24291 fp = HI2UF(hi);
24292
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024293 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024294 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024295
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024296 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24297 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298
24299 cat_func_name(IObuff, fp);
24300 if (xp->xp_context != EXPAND_USER_FUNC)
24301 {
24302 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024303 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304 STRCAT(IObuff, ")");
24305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024306 return IObuff;
24307 }
24308 return NULL;
24309}
24310
24311#endif /* FEAT_CMDL_COMPL */
24312
24313/*
24314 * Copy the function name of "fp" to buffer "buf".
24315 * "buf" must be able to hold the function name plus three bytes.
24316 * Takes care of script-local function names.
24317 */
24318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024319cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024320{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024321 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322 {
24323 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024324 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 }
24326 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024327 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328}
24329
24330/*
24331 * ":delfunction {name}"
24332 */
24333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024334ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024336 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024337 char_u *p;
24338 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024339 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340
24341 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024342 name = trans_function_name(&p, eap->skip, 0, &fudi);
24343 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024345 {
24346 if (fudi.fd_dict != NULL && !eap->skip)
24347 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024348 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 if (!ends_excmd(*skipwhite(p)))
24351 {
24352 vim_free(name);
24353 EMSG(_(e_trailing));
24354 return;
24355 }
24356 eap->nextcmd = check_nextcmd(p);
24357 if (eap->nextcmd != NULL)
24358 *p = NUL;
24359
24360 if (!eap->skip)
24361 fp = find_func(name);
24362 vim_free(name);
24363
24364 if (!eap->skip)
24365 {
24366 if (fp == NULL)
24367 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024368 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369 return;
24370 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024371 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372 {
24373 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24374 return;
24375 }
24376
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024377 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024379 /* Delete the dict item that refers to the function, it will
24380 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024381 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024382 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024383 else
24384 func_free(fp);
24385 }
24386}
24387
24388/*
24389 * Free a function and remove it from the list of functions.
24390 */
24391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024392func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024393{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024394 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024395
24396 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024397 ga_clear_strings(&(fp->uf_args));
24398 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024399#ifdef FEAT_PROFILE
24400 vim_free(fp->uf_tml_count);
24401 vim_free(fp->uf_tml_total);
24402 vim_free(fp->uf_tml_self);
24403#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024404
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024405 /* remove the function from the function hashtable */
24406 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24407 if (HASHITEM_EMPTY(hi))
24408 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024409 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024410 hash_remove(&func_hashtab, hi);
24411
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024412 vim_free(fp);
24413}
24414
24415/*
24416 * Unreference a Function: decrement the reference count and free it when it
24417 * becomes zero. Only for numbered functions.
24418 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024420func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024421{
24422 ufunc_T *fp;
24423
24424 if (name != NULL && isdigit(*name))
24425 {
24426 fp = find_func(name);
24427 if (fp == NULL)
24428 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024429 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024430 {
24431 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024432 * when "uf_calls" becomes zero. */
24433 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024434 func_free(fp);
24435 }
24436 }
24437}
24438
24439/*
24440 * Count a reference to a Function.
24441 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024443func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024444{
24445 ufunc_T *fp;
24446
24447 if (name != NULL && isdigit(*name))
24448 {
24449 fp = find_func(name);
24450 if (fp == NULL)
24451 EMSG2(_(e_intern2), "func_ref()");
24452 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024453 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454 }
24455}
24456
24457/*
24458 * Call a user function.
24459 */
24460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024461call_user_func(
24462 ufunc_T *fp, /* pointer to function */
24463 int argcount, /* nr of args */
24464 typval_T *argvars, /* arguments */
24465 typval_T *rettv, /* return value */
24466 linenr_T firstline, /* first line of range */
24467 linenr_T lastline, /* last line of range */
24468 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024469{
Bram Moolenaar33570922005-01-25 22:26:29 +000024470 char_u *save_sourcing_name;
24471 linenr_T save_sourcing_lnum;
24472 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024473 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024474 int save_did_emsg;
24475 static int depth = 0;
24476 dictitem_T *v;
24477 int fixvar_idx = 0; /* index in fixvar[] */
24478 int i;
24479 int ai;
24480 char_u numbuf[NUMBUFLEN];
24481 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024482 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024483#ifdef FEAT_PROFILE
24484 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024485 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487
24488 /* If depth of calling is getting too high, don't execute the function */
24489 if (depth >= p_mfd)
24490 {
24491 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024492 rettv->v_type = VAR_NUMBER;
24493 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024494 return;
24495 }
24496 ++depth;
24497
24498 line_breakcheck(); /* check for CTRL-C hit */
24499
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024500 fc = (funccall_T *)alloc(sizeof(funccall_T));
24501 fc->caller = current_funccal;
24502 current_funccal = fc;
24503 fc->func = fp;
24504 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024505 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024506 fc->linenr = 0;
24507 fc->returned = FALSE;
24508 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024509 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024510 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24511 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512
Bram Moolenaar33570922005-01-25 22:26:29 +000024513 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024514 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024515 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24516 * each argument variable and saves a lot of time.
24517 */
24518 /*
24519 * Init l: variables.
24520 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024521 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024522 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024523 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024524 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24525 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024526 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024527 name = v->di_key;
24528 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024529 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024530 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024531 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024532 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024533 v->di_tv.vval.v_dict = selfdict;
24534 ++selfdict->dv_refcount;
24535 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024536
Bram Moolenaar33570922005-01-25 22:26:29 +000024537 /*
24538 * Init a: variables.
24539 * Set a:0 to "argcount".
24540 * Set a:000 to a list with room for the "..." arguments.
24541 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024542 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024543 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024544 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024545 /* Use "name" to avoid a warning from some compiler that checks the
24546 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024547 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024548 name = v->di_key;
24549 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024550 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024551 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024552 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024553 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024554 v->di_tv.vval.v_list = &fc->l_varlist;
24555 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24556 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24557 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024558
24559 /*
24560 * Set a:firstline to "firstline" and a:lastline to "lastline".
24561 * Set a:name to named arguments.
24562 * Set a:N to the "..." arguments.
24563 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024564 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024565 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024566 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024567 (varnumber_T)lastline);
24568 for (i = 0; i < argcount; ++i)
24569 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024570 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024571 if (ai < 0)
24572 /* named argument a:name */
24573 name = FUNCARG(fp, i);
24574 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024575 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024576 /* "..." argument a:1, a:2, etc. */
24577 sprintf((char *)numbuf, "%d", ai + 1);
24578 name = numbuf;
24579 }
24580 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24581 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024582 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024583 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24584 }
24585 else
24586 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024587 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24588 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024589 if (v == NULL)
24590 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024591 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024592 }
24593 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024594 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024595
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024596 /* Note: the values are copied directly to avoid alloc/free.
24597 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024598 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024599 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024600
24601 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24602 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024603 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24604 fc->l_listitems[ai].li_tv = argvars[i];
24605 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024606 }
24607 }
24608
Bram Moolenaar071d4272004-06-13 20:20:40 +000024609 /* Don't redraw while executing the function. */
24610 ++RedrawingDisabled;
24611 save_sourcing_name = sourcing_name;
24612 save_sourcing_lnum = sourcing_lnum;
24613 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024614 /* need space for function name + ("function " + 3) or "[number]" */
24615 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24616 + STRLEN(fp->uf_name) + 20;
24617 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618 if (sourcing_name != NULL)
24619 {
24620 if (save_sourcing_name != NULL
24621 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024622 sprintf((char *)sourcing_name, "%s[%d]..",
24623 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024624 else
24625 STRCPY(sourcing_name, "function ");
24626 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24627
24628 if (p_verbose >= 12)
24629 {
24630 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024631 verbose_enter_scroll();
24632
Bram Moolenaar555b2802005-05-19 21:08:39 +000024633 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634 if (p_verbose >= 14)
24635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024637 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024638 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024639 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024640
24641 msg_puts((char_u *)"(");
24642 for (i = 0; i < argcount; ++i)
24643 {
24644 if (i > 0)
24645 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024646 if (argvars[i].v_type == VAR_NUMBER)
24647 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024648 else
24649 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024650 /* Do not want errors such as E724 here. */
24651 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024652 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024653 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024654 if (s != NULL)
24655 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024656 if (vim_strsize(s) > MSG_BUF_CLEN)
24657 {
24658 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24659 s = buf;
24660 }
24661 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024662 vim_free(tofree);
24663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664 }
24665 }
24666 msg_puts((char_u *)")");
24667 }
24668 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024669
24670 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024671 --no_wait_return;
24672 }
24673 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024674#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024675 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024676 {
24677 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24678 func_do_profile(fp);
24679 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024680 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024681 {
24682 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024683 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024684 profile_zero(&fp->uf_tm_children);
24685 }
24686 script_prof_save(&wait_start);
24687 }
24688#endif
24689
Bram Moolenaar071d4272004-06-13 20:20:40 +000024690 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024691 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692 save_did_emsg = did_emsg;
24693 did_emsg = FALSE;
24694
24695 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024696 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24698
24699 --RedrawingDisabled;
24700
24701 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024702 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024704 clear_tv(rettv);
24705 rettv->v_type = VAR_NUMBER;
24706 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707 }
24708
Bram Moolenaar05159a02005-02-26 23:04:13 +000024709#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024710 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024711 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024712 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024713 profile_end(&call_start);
24714 profile_sub_wait(&wait_start, &call_start);
24715 profile_add(&fp->uf_tm_total, &call_start);
24716 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024717 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024718 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024719 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24720 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024721 }
24722 }
24723#endif
24724
Bram Moolenaar071d4272004-06-13 20:20:40 +000024725 /* when being verbose, mention the return value */
24726 if (p_verbose >= 12)
24727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024728 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024729 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024730
Bram Moolenaar071d4272004-06-13 20:20:40 +000024731 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024732 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024733 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024734 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024735 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024736 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024737 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024738 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024739 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024740 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024741 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024742
Bram Moolenaar555b2802005-05-19 21:08:39 +000024743 /* The value may be very long. Skip the middle part, so that we
24744 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024745 * truncate it at the end. Don't want errors such as E724 here. */
24746 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024747 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024748 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024749 if (s != NULL)
24750 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024751 if (vim_strsize(s) > MSG_BUF_CLEN)
24752 {
24753 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24754 s = buf;
24755 }
24756 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024757 vim_free(tofree);
24758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024759 }
24760 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024761
24762 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763 --no_wait_return;
24764 }
24765
24766 vim_free(sourcing_name);
24767 sourcing_name = save_sourcing_name;
24768 sourcing_lnum = save_sourcing_lnum;
24769 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024770#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024771 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024772 script_prof_restore(&wait_start);
24773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774
24775 if (p_verbose >= 12 && sourcing_name != NULL)
24776 {
24777 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024778 verbose_enter_scroll();
24779
Bram Moolenaar555b2802005-05-19 21:08:39 +000024780 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024782
24783 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024784 --no_wait_return;
24785 }
24786
24787 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024788 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024790
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024791 /* If the a:000 list and the l: and a: dicts are not referenced we can
24792 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024793 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24794 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24795 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24796 {
24797 free_funccal(fc, FALSE);
24798 }
24799 else
24800 {
24801 hashitem_T *hi;
24802 listitem_T *li;
24803 int todo;
24804
24805 /* "fc" is still in use. This can happen when returning "a:000" or
24806 * assigning "l:" to a global variable.
24807 * Link "fc" in the list for garbage collection later. */
24808 fc->caller = previous_funccal;
24809 previous_funccal = fc;
24810
24811 /* Make a copy of the a: variables, since we didn't do that above. */
24812 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24813 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24814 {
24815 if (!HASHITEM_EMPTY(hi))
24816 {
24817 --todo;
24818 v = HI2DI(hi);
24819 copy_tv(&v->di_tv, &v->di_tv);
24820 }
24821 }
24822
24823 /* Make a copy of the a:000 items, since we didn't do that above. */
24824 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24825 copy_tv(&li->li_tv, &li->li_tv);
24826 }
24827}
24828
24829/*
24830 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024831 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024832 */
24833 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024834can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024835{
24836 return (fc->l_varlist.lv_copyID != copyID
24837 && fc->l_vars.dv_copyID != copyID
24838 && fc->l_avars.dv_copyID != copyID);
24839}
24840
24841/*
24842 * Free "fc" and what it contains.
24843 */
24844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024845free_funccal(
24846 funccall_T *fc,
24847 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024848{
24849 listitem_T *li;
24850
24851 /* The a: variables typevals may not have been allocated, only free the
24852 * allocated variables. */
24853 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24854
24855 /* free all l: variables */
24856 vars_clear(&fc->l_vars.dv_hashtab);
24857
24858 /* Free the a:000 variables if they were allocated. */
24859 if (free_val)
24860 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24861 clear_tv(&li->li_tv);
24862
24863 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864}
24865
24866/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024867 * Add a number variable "name" to dict "dp" with value "nr".
24868 */
24869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024870add_nr_var(
24871 dict_T *dp,
24872 dictitem_T *v,
24873 char *name,
24874 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024875{
24876 STRCPY(v->di_key, name);
24877 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24878 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24879 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024880 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024881 v->di_tv.vval.v_number = nr;
24882}
24883
24884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024885 * ":return [expr]"
24886 */
24887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024888ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889{
24890 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024891 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892 int returning = FALSE;
24893
24894 if (current_funccal == NULL)
24895 {
24896 EMSG(_("E133: :return not inside a function"));
24897 return;
24898 }
24899
24900 if (eap->skip)
24901 ++emsg_skip;
24902
24903 eap->nextcmd = NULL;
24904 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024905 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906 {
24907 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024908 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024910 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 }
24912 /* It's safer to return also on error. */
24913 else if (!eap->skip)
24914 {
24915 /*
24916 * Return unless the expression evaluation has been cancelled due to an
24917 * aborting error, an interrupt, or an exception.
24918 */
24919 if (!aborting())
24920 returning = do_return(eap, FALSE, TRUE, NULL);
24921 }
24922
24923 /* When skipping or the return gets pending, advance to the next command
24924 * in this line (!returning). Otherwise, ignore the rest of the line.
24925 * Following lines will be ignored by get_func_line(). */
24926 if (returning)
24927 eap->nextcmd = NULL;
24928 else if (eap->nextcmd == NULL) /* no argument */
24929 eap->nextcmd = check_nextcmd(arg);
24930
24931 if (eap->skip)
24932 --emsg_skip;
24933}
24934
24935/*
24936 * Return from a function. Possibly makes the return pending. Also called
24937 * for a pending return at the ":endtry" or after returning from an extra
24938 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024939 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024940 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941 * FALSE when the return gets pending.
24942 */
24943 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024944do_return(
24945 exarg_T *eap,
24946 int reanimate,
24947 int is_cmd,
24948 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024949{
24950 int idx;
24951 struct condstack *cstack = eap->cstack;
24952
24953 if (reanimate)
24954 /* Undo the return. */
24955 current_funccal->returned = FALSE;
24956
24957 /*
24958 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24959 * not in its finally clause (which then is to be executed next) is found.
24960 * In this case, make the ":return" pending for execution at the ":endtry".
24961 * Otherwise, return normally.
24962 */
24963 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24964 if (idx >= 0)
24965 {
24966 cstack->cs_pending[idx] = CSTP_RETURN;
24967
24968 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024969 /* A pending return again gets pending. "rettv" points to an
24970 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024971 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024972 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024973 else
24974 {
24975 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024976 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024977 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024978 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024980 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981 {
24982 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024983 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024984 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985 else
24986 EMSG(_(e_outofmem));
24987 }
24988 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024989 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990
24991 if (reanimate)
24992 {
24993 /* The pending return value could be overwritten by a ":return"
24994 * without argument in a finally clause; reset the default
24995 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024996 current_funccal->rettv->v_type = VAR_NUMBER;
24997 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024998 }
24999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025000 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025001 }
25002 else
25003 {
25004 current_funccal->returned = TRUE;
25005
25006 /* If the return is carried out now, store the return value. For
25007 * a return immediately after reanimation, the value is already
25008 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025009 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025011 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025012 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025014 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015 }
25016 }
25017
25018 return idx < 0;
25019}
25020
25021/*
25022 * Free the variable with a pending return value.
25023 */
25024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025025discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026{
Bram Moolenaar33570922005-01-25 22:26:29 +000025027 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025028}
25029
25030/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025031 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025032 * is an allocated string. Used by report_pending() for verbose messages.
25033 */
25034 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025035get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025036{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025037 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025038 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025039 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025041 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025042 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025043 if (s == NULL)
25044 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025045
25046 STRCPY(IObuff, ":return ");
25047 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25048 if (STRLEN(s) + 8 >= IOSIZE)
25049 STRCPY(IObuff + IOSIZE - 4, "...");
25050 vim_free(tofree);
25051 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052}
25053
25054/*
25055 * Get next function line.
25056 * Called by do_cmdline() to get the next line.
25057 * Returns allocated string, or NULL for end of function.
25058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025059 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025060get_func_line(
25061 int c UNUSED,
25062 void *cookie,
25063 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025064{
Bram Moolenaar33570922005-01-25 22:26:29 +000025065 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025066 ufunc_T *fp = fcp->func;
25067 char_u *retval;
25068 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025069
25070 /* If breakpoints have been added/deleted need to check for it. */
25071 if (fcp->dbg_tick != debug_tick)
25072 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025073 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074 sourcing_lnum);
25075 fcp->dbg_tick = debug_tick;
25076 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025077#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025078 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025079 func_line_end(cookie);
25080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025081
Bram Moolenaar05159a02005-02-26 23:04:13 +000025082 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025083 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25084 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085 retval = NULL;
25086 else
25087 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025088 /* Skip NULL lines (continuation lines). */
25089 while (fcp->linenr < gap->ga_len
25090 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25091 ++fcp->linenr;
25092 if (fcp->linenr >= gap->ga_len)
25093 retval = NULL;
25094 else
25095 {
25096 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25097 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025098#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025099 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025100 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025101#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025103 }
25104
25105 /* Did we encounter a breakpoint? */
25106 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25107 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025108 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025110 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111 sourcing_lnum);
25112 fcp->dbg_tick = debug_tick;
25113 }
25114
25115 return retval;
25116}
25117
Bram Moolenaar05159a02005-02-26 23:04:13 +000025118#if defined(FEAT_PROFILE) || defined(PROTO)
25119/*
25120 * Called when starting to read a function line.
25121 * "sourcing_lnum" must be correct!
25122 * When skipping lines it may not actually be executed, but we won't find out
25123 * until later and we need to store the time now.
25124 */
25125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025126func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025127{
25128 funccall_T *fcp = (funccall_T *)cookie;
25129 ufunc_T *fp = fcp->func;
25130
25131 if (fp->uf_profiling && sourcing_lnum >= 1
25132 && sourcing_lnum <= fp->uf_lines.ga_len)
25133 {
25134 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025135 /* Skip continuation lines. */
25136 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25137 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025138 fp->uf_tml_execed = FALSE;
25139 profile_start(&fp->uf_tml_start);
25140 profile_zero(&fp->uf_tml_children);
25141 profile_get_wait(&fp->uf_tml_wait);
25142 }
25143}
25144
25145/*
25146 * Called when actually executing a function line.
25147 */
25148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025149func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025150{
25151 funccall_T *fcp = (funccall_T *)cookie;
25152 ufunc_T *fp = fcp->func;
25153
25154 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25155 fp->uf_tml_execed = TRUE;
25156}
25157
25158/*
25159 * Called when done with a function line.
25160 */
25161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025162func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025163{
25164 funccall_T *fcp = (funccall_T *)cookie;
25165 ufunc_T *fp = fcp->func;
25166
25167 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25168 {
25169 if (fp->uf_tml_execed)
25170 {
25171 ++fp->uf_tml_count[fp->uf_tml_idx];
25172 profile_end(&fp->uf_tml_start);
25173 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025174 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025175 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25176 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025177 }
25178 fp->uf_tml_idx = -1;
25179 }
25180}
25181#endif
25182
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183/*
25184 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025185 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025186 */
25187 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025188func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189{
Bram Moolenaar33570922005-01-25 22:26:29 +000025190 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025191
25192 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25193 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025194 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025195 || fcp->returned);
25196}
25197
25198/*
25199 * return TRUE if cookie indicates a function which "abort"s on errors.
25200 */
25201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025202func_has_abort(
25203 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025205 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206}
25207
25208#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25209typedef enum
25210{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025211 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25212 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25213 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214} var_flavour_T;
25215
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025216static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217
25218 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025219var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220{
25221 char_u *p = varname;
25222
25223 if (ASCII_ISUPPER(*p))
25224 {
25225 while (*(++p))
25226 if (ASCII_ISLOWER(*p))
25227 return VAR_FLAVOUR_SESSION;
25228 return VAR_FLAVOUR_VIMINFO;
25229 }
25230 else
25231 return VAR_FLAVOUR_DEFAULT;
25232}
25233#endif
25234
25235#if defined(FEAT_VIMINFO) || defined(PROTO)
25236/*
25237 * Restore global vars that start with a capital from the viminfo file
25238 */
25239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025240read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025241{
25242 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025243 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025244 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025245 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246
25247 if (!writing && (find_viminfo_parameter('!') != NULL))
25248 {
25249 tab = vim_strchr(virp->vir_line + 1, '\t');
25250 if (tab != NULL)
25251 {
25252 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025253 switch (*tab)
25254 {
25255 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025256#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025257 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025258#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025259 case 'D': type = VAR_DICT; break;
25260 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025261 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025263
25264 tab = vim_strchr(tab, '\t');
25265 if (tab != NULL)
25266 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025267 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025268 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025269 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025271#ifdef FEAT_FLOAT
25272 else if (type == VAR_FLOAT)
25273 (void)string2float(tab + 1, &tv.vval.v_float);
25274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025276 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025277 if (type == VAR_DICT || type == VAR_LIST)
25278 {
25279 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25280
25281 if (etv == NULL)
25282 /* Failed to parse back the dict or list, use it as a
25283 * string. */
25284 tv.v_type = VAR_STRING;
25285 else
25286 {
25287 vim_free(tv.vval.v_string);
25288 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025289 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025290 }
25291 }
25292
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025293 /* when in a function use global variables */
25294 save_funccal = current_funccal;
25295 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025296 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025297 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025298
25299 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025300 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025301 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25302 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025303 }
25304 }
25305 }
25306
25307 return viminfo_readline(virp);
25308}
25309
25310/*
25311 * Write global vars that start with a capital to the viminfo file
25312 */
25313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025314write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025315{
Bram Moolenaar33570922005-01-25 22:26:29 +000025316 hashitem_T *hi;
25317 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025318 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025319 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025320 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025321 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025322 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323
25324 if (find_viminfo_parameter('!') == NULL)
25325 return;
25326
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025327 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025328
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025329 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025330 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025332 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025333 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025334 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025335 this_var = HI2DI(hi);
25336 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025337 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025338 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025339 {
25340 case VAR_STRING: s = "STR"; break;
25341 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025342 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025343 case VAR_DICT: s = "DIC"; break;
25344 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025345 case VAR_SPECIAL: s = "XPL"; break;
25346
25347 case VAR_UNKNOWN:
25348 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025349 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025350 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025351 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025352 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025353 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025354 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025355 if (p != NULL)
25356 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025357 vim_free(tofree);
25358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359 }
25360 }
25361}
25362#endif
25363
25364#if defined(FEAT_SESSION) || defined(PROTO)
25365 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025366store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367{
Bram Moolenaar33570922005-01-25 22:26:29 +000025368 hashitem_T *hi;
25369 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025370 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025371 char_u *p, *t;
25372
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025373 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025374 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025375 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025376 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025377 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025378 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025379 this_var = HI2DI(hi);
25380 if ((this_var->di_tv.v_type == VAR_NUMBER
25381 || this_var->di_tv.v_type == VAR_STRING)
25382 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025383 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025384 /* Escape special characters with a backslash. Turn a LF and
25385 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025386 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025387 (char_u *)"\\\"\n\r");
25388 if (p == NULL) /* out of memory */
25389 break;
25390 for (t = p; *t != NUL; ++t)
25391 if (*t == '\n')
25392 *t = 'n';
25393 else if (*t == '\r')
25394 *t = 'r';
25395 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025396 this_var->di_key,
25397 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25398 : ' ',
25399 p,
25400 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25401 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025402 || put_eol(fd) == FAIL)
25403 {
25404 vim_free(p);
25405 return FAIL;
25406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025407 vim_free(p);
25408 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025409#ifdef FEAT_FLOAT
25410 else if (this_var->di_tv.v_type == VAR_FLOAT
25411 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25412 {
25413 float_T f = this_var->di_tv.vval.v_float;
25414 int sign = ' ';
25415
25416 if (f < 0)
25417 {
25418 f = -f;
25419 sign = '-';
25420 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025421 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025422 this_var->di_key, sign, f) < 0)
25423 || put_eol(fd) == FAIL)
25424 return FAIL;
25425 }
25426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025427 }
25428 }
25429 return OK;
25430}
25431#endif
25432
Bram Moolenaar661b1822005-07-28 22:36:45 +000025433/*
25434 * Display script name where an item was last set.
25435 * Should only be invoked when 'verbose' is non-zero.
25436 */
25437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025438last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025439{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025440 char_u *p;
25441
Bram Moolenaar661b1822005-07-28 22:36:45 +000025442 if (scriptID != 0)
25443 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025444 p = home_replace_save(NULL, get_scriptname(scriptID));
25445 if (p != NULL)
25446 {
25447 verbose_enter();
25448 MSG_PUTS(_("\n\tLast set from "));
25449 MSG_PUTS(p);
25450 vim_free(p);
25451 verbose_leave();
25452 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025453 }
25454}
25455
Bram Moolenaard812df62008-11-09 12:46:09 +000025456/*
25457 * List v:oldfiles in a nice way.
25458 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025459 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025460ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025461{
25462 list_T *l = vimvars[VV_OLDFILES].vv_list;
25463 listitem_T *li;
25464 int nr = 0;
25465
25466 if (l == NULL)
25467 msg((char_u *)_("No old files"));
25468 else
25469 {
25470 msg_start();
25471 msg_scroll = TRUE;
25472 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25473 {
25474 msg_outnum((long)++nr);
25475 MSG_PUTS(": ");
25476 msg_outtrans(get_tv_string(&li->li_tv));
25477 msg_putchar('\n');
25478 out_flush(); /* output one line at a time */
25479 ui_breakcheck();
25480 }
25481 /* Assume "got_int" was set to truncate the listing. */
25482 got_int = FALSE;
25483
25484#ifdef FEAT_BROWSE_CMD
25485 if (cmdmod.browse)
25486 {
25487 quit_more = FALSE;
25488 nr = prompt_for_number(FALSE);
25489 msg_starthere();
25490 if (nr > 0)
25491 {
25492 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25493 (long)nr);
25494
25495 if (p != NULL)
25496 {
25497 p = expand_env_save(p);
25498 eap->arg = p;
25499 eap->cmdidx = CMD_edit;
25500 cmdmod.browse = FALSE;
25501 do_exedit(eap, NULL);
25502 vim_free(p);
25503 }
25504 }
25505 }
25506#endif
25507 }
25508}
25509
Bram Moolenaar53744302015-07-17 17:38:22 +020025510/* reset v:option_new, v:option_old and v:option_type */
25511 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025512reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025513{
25514 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25515 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25516 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25517}
25518
25519
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520#endif /* FEAT_EVAL */
25521
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025523#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025524
25525#ifdef WIN3264
25526/*
25527 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25528 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025529static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25530static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25531static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532
25533/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025534 * Get the short path (8.3) for the filename in "fnamep".
25535 * Only works for a valid file name.
25536 * When the path gets longer "fnamep" is changed and the allocated buffer
25537 * is put in "bufp".
25538 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25539 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025540 */
25541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025542get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025544 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025545 char_u *newbuf;
25546
25547 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025548 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025549 if (l > len - 1)
25550 {
25551 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025552 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553 newbuf = vim_strnsave(*fnamep, l);
25554 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025555 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556
25557 vim_free(*bufp);
25558 *fnamep = *bufp = newbuf;
25559
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025560 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025561 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025562 }
25563
25564 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025565 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025566}
25567
25568/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025569 * Get the short path (8.3) for the filename in "fname". The converted
25570 * path is returned in "bufp".
25571 *
25572 * Some of the directories specified in "fname" may not exist. This function
25573 * will shorten the existing directories at the beginning of the path and then
25574 * append the remaining non-existing path.
25575 *
25576 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025577 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025578 * bufp - Pointer to an allocated buffer for the filename.
25579 * fnamelen - Length of the filename pointed to by fname
25580 *
25581 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025582 */
25583 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025584shortpath_for_invalid_fname(
25585 char_u **fname,
25586 char_u **bufp,
25587 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025588{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025589 char_u *short_fname, *save_fname, *pbuf_unused;
25590 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025592 int old_len, len;
25593 int new_len, sfx_len;
25594 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025595
25596 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025597 old_len = *fnamelen;
25598 save_fname = vim_strnsave(*fname, old_len);
25599 pbuf_unused = NULL;
25600 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025601
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025602 endp = save_fname + old_len - 1; /* Find the end of the copy */
25603 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025604
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025605 /*
25606 * Try shortening the supplied path till it succeeds by removing one
25607 * directory at a time from the tail of the path.
25608 */
25609 len = 0;
25610 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025612 /* go back one path-separator */
25613 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25614 --endp;
25615 if (endp <= save_fname)
25616 break; /* processed the complete path */
25617
25618 /*
25619 * Replace the path separator with a NUL and try to shorten the
25620 * resulting path.
25621 */
25622 ch = *endp;
25623 *endp = 0;
25624 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025625 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025626 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25627 {
25628 retval = FAIL;
25629 goto theend;
25630 }
25631 *endp = ch; /* preserve the string */
25632
25633 if (len > 0)
25634 break; /* successfully shortened the path */
25635
25636 /* failed to shorten the path. Skip the path separator */
25637 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025638 }
25639
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025640 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025642 /*
25643 * Succeeded in shortening the path. Now concatenate the shortened
25644 * path with the remaining path at the tail.
25645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025646
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025647 /* Compute the length of the new path. */
25648 sfx_len = (int)(save_endp - endp) + 1;
25649 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025651 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025653 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025654 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025655 /* There is not enough space in the currently allocated string,
25656 * copy it to a buffer big enough. */
25657 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025659 {
25660 retval = FAIL;
25661 goto theend;
25662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663 }
25664 else
25665 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025666 /* Transfer short_fname to the main buffer (it's big enough),
25667 * unless get_short_pathname() did its work in-place. */
25668 *fname = *bufp = save_fname;
25669 if (short_fname != save_fname)
25670 vim_strncpy(save_fname, short_fname, len);
25671 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025672 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025673
25674 /* concat the not-shortened part of the path */
25675 vim_strncpy(*fname + len, endp, sfx_len);
25676 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025677 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025678
25679theend:
25680 vim_free(pbuf_unused);
25681 vim_free(save_fname);
25682
25683 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025684}
25685
25686/*
25687 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025688 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689 */
25690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025691shortpath_for_partial(
25692 char_u **fnamep,
25693 char_u **bufp,
25694 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025695{
25696 int sepcount, len, tflen;
25697 char_u *p;
25698 char_u *pbuf, *tfname;
25699 int hasTilde;
25700
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025701 /* Count up the path separators from the RHS.. so we know which part
25702 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025704 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025705 if (vim_ispathsep(*p))
25706 ++sepcount;
25707
25708 /* Need full path first (use expand_env() to remove a "~/") */
25709 hasTilde = (**fnamep == '~');
25710 if (hasTilde)
25711 pbuf = tfname = expand_env_save(*fnamep);
25712 else
25713 pbuf = tfname = FullName_save(*fnamep, FALSE);
25714
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025715 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025716
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025717 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25718 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025719
25720 if (len == 0)
25721 {
25722 /* Don't have a valid filename, so shorten the rest of the
25723 * path if we can. This CAN give us invalid 8.3 filenames, but
25724 * there's not a lot of point in guessing what it might be.
25725 */
25726 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025727 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25728 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025729 }
25730
25731 /* Count the paths backward to find the beginning of the desired string. */
25732 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025733 {
25734#ifdef FEAT_MBYTE
25735 if (has_mbyte)
25736 p -= mb_head_off(tfname, p);
25737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 if (vim_ispathsep(*p))
25739 {
25740 if (sepcount == 0 || (hasTilde && sepcount == 1))
25741 break;
25742 else
25743 sepcount --;
25744 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746 if (hasTilde)
25747 {
25748 --p;
25749 if (p >= tfname)
25750 *p = '~';
25751 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025752 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753 }
25754 else
25755 ++p;
25756
25757 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25758 vim_free(*bufp);
25759 *fnamelen = (int)STRLEN(p);
25760 *bufp = pbuf;
25761 *fnamep = p;
25762
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025763 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025764}
25765#endif /* WIN3264 */
25766
25767/*
25768 * Adjust a filename, according to a string of modifiers.
25769 * *fnamep must be NUL terminated when called. When returning, the length is
25770 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025771 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025772 * When there is an error, *fnamep is set to NULL.
25773 */
25774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025775modify_fname(
25776 char_u *src, /* string with modifiers */
25777 int *usedlen, /* characters after src that are used */
25778 char_u **fnamep, /* file name so far */
25779 char_u **bufp, /* buffer for allocated file name or NULL */
25780 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025781{
25782 int valid = 0;
25783 char_u *tail;
25784 char_u *s, *p, *pbuf;
25785 char_u dirname[MAXPATHL];
25786 int c;
25787 int has_fullname = 0;
25788#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025789 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025790 int has_shortname = 0;
25791#endif
25792
25793repeat:
25794 /* ":p" - full path/file_name */
25795 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25796 {
25797 has_fullname = 1;
25798
25799 valid |= VALID_PATH;
25800 *usedlen += 2;
25801
25802 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25803 if ((*fnamep)[0] == '~'
25804#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25805 && ((*fnamep)[1] == '/'
25806# ifdef BACKSLASH_IN_FILENAME
25807 || (*fnamep)[1] == '\\'
25808# endif
25809 || (*fnamep)[1] == NUL)
25810
25811#endif
25812 )
25813 {
25814 *fnamep = expand_env_save(*fnamep);
25815 vim_free(*bufp); /* free any allocated file name */
25816 *bufp = *fnamep;
25817 if (*fnamep == NULL)
25818 return -1;
25819 }
25820
25821 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025822 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025823 {
25824 if (vim_ispathsep(*p)
25825 && p[1] == '.'
25826 && (p[2] == NUL
25827 || vim_ispathsep(p[2])
25828 || (p[2] == '.'
25829 && (p[3] == NUL || vim_ispathsep(p[3])))))
25830 break;
25831 }
25832
25833 /* FullName_save() is slow, don't use it when not needed. */
25834 if (*p != NUL || !vim_isAbsName(*fnamep))
25835 {
25836 *fnamep = FullName_save(*fnamep, *p != NUL);
25837 vim_free(*bufp); /* free any allocated file name */
25838 *bufp = *fnamep;
25839 if (*fnamep == NULL)
25840 return -1;
25841 }
25842
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025843#ifdef WIN3264
25844# if _WIN32_WINNT >= 0x0500
25845 if (vim_strchr(*fnamep, '~') != NULL)
25846 {
25847 /* Expand 8.3 filename to full path. Needed to make sure the same
25848 * file does not have two different names.
25849 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25850 p = alloc(_MAX_PATH + 1);
25851 if (p != NULL)
25852 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025853 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025854 {
25855 vim_free(*bufp);
25856 *bufp = *fnamep = p;
25857 }
25858 else
25859 vim_free(p);
25860 }
25861 }
25862# endif
25863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864 /* Append a path separator to a directory. */
25865 if (mch_isdir(*fnamep))
25866 {
25867 /* Make room for one or two extra characters. */
25868 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25869 vim_free(*bufp); /* free any allocated file name */
25870 *bufp = *fnamep;
25871 if (*fnamep == NULL)
25872 return -1;
25873 add_pathsep(*fnamep);
25874 }
25875 }
25876
25877 /* ":." - path relative to the current directory */
25878 /* ":~" - path relative to the home directory */
25879 /* ":8" - shortname path - postponed till after */
25880 while (src[*usedlen] == ':'
25881 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25882 {
25883 *usedlen += 2;
25884 if (c == '8')
25885 {
25886#ifdef WIN3264
25887 has_shortname = 1; /* Postpone this. */
25888#endif
25889 continue;
25890 }
25891 pbuf = NULL;
25892 /* Need full path first (use expand_env() to remove a "~/") */
25893 if (!has_fullname)
25894 {
25895 if (c == '.' && **fnamep == '~')
25896 p = pbuf = expand_env_save(*fnamep);
25897 else
25898 p = pbuf = FullName_save(*fnamep, FALSE);
25899 }
25900 else
25901 p = *fnamep;
25902
25903 has_fullname = 0;
25904
25905 if (p != NULL)
25906 {
25907 if (c == '.')
25908 {
25909 mch_dirname(dirname, MAXPATHL);
25910 s = shorten_fname(p, dirname);
25911 if (s != NULL)
25912 {
25913 *fnamep = s;
25914 if (pbuf != NULL)
25915 {
25916 vim_free(*bufp); /* free any allocated file name */
25917 *bufp = pbuf;
25918 pbuf = NULL;
25919 }
25920 }
25921 }
25922 else
25923 {
25924 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25925 /* Only replace it when it starts with '~' */
25926 if (*dirname == '~')
25927 {
25928 s = vim_strsave(dirname);
25929 if (s != NULL)
25930 {
25931 *fnamep = s;
25932 vim_free(*bufp);
25933 *bufp = s;
25934 }
25935 }
25936 }
25937 vim_free(pbuf);
25938 }
25939 }
25940
25941 tail = gettail(*fnamep);
25942 *fnamelen = (int)STRLEN(*fnamep);
25943
25944 /* ":h" - head, remove "/file_name", can be repeated */
25945 /* Don't remove the first "/" or "c:\" */
25946 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25947 {
25948 valid |= VALID_HEAD;
25949 *usedlen += 2;
25950 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025951 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025952 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025953 *fnamelen = (int)(tail - *fnamep);
25954#ifdef VMS
25955 if (*fnamelen > 0)
25956 *fnamelen += 1; /* the path separator is part of the path */
25957#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025958 if (*fnamelen == 0)
25959 {
25960 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25961 p = vim_strsave((char_u *)".");
25962 if (p == NULL)
25963 return -1;
25964 vim_free(*bufp);
25965 *bufp = *fnamep = tail = p;
25966 *fnamelen = 1;
25967 }
25968 else
25969 {
25970 while (tail > s && !after_pathsep(s, tail))
25971 mb_ptr_back(*fnamep, tail);
25972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025973 }
25974
25975 /* ":8" - shortname */
25976 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25977 {
25978 *usedlen += 2;
25979#ifdef WIN3264
25980 has_shortname = 1;
25981#endif
25982 }
25983
25984#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025985 /*
25986 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987 */
25988 if (has_shortname)
25989 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025990 /* Copy the string if it is shortened by :h and when it wasn't copied
25991 * yet, because we are going to change it in place. Avoids changing
25992 * the buffer name for "%:8". */
25993 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025994 {
25995 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025996 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997 return -1;
25998 vim_free(*bufp);
25999 *bufp = *fnamep = p;
26000 }
26001
26002 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026003 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026004 if (!has_fullname && !vim_isAbsName(*fnamep))
26005 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026006 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007 return -1;
26008 }
26009 else
26010 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026011 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026012
Bram Moolenaardc935552011-08-17 15:23:23 +020026013 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026014 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026015 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026016 return -1;
26017
26018 if (l == 0)
26019 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026020 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026021 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026022 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026023 return -1;
26024 }
26025 *fnamelen = l;
26026 }
26027 }
26028#endif /* WIN3264 */
26029
26030 /* ":t" - tail, just the basename */
26031 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26032 {
26033 *usedlen += 2;
26034 *fnamelen -= (int)(tail - *fnamep);
26035 *fnamep = tail;
26036 }
26037
26038 /* ":e" - extension, can be repeated */
26039 /* ":r" - root, without extension, can be repeated */
26040 while (src[*usedlen] == ':'
26041 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26042 {
26043 /* find a '.' in the tail:
26044 * - for second :e: before the current fname
26045 * - otherwise: The last '.'
26046 */
26047 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26048 s = *fnamep - 2;
26049 else
26050 s = *fnamep + *fnamelen - 1;
26051 for ( ; s > tail; --s)
26052 if (s[0] == '.')
26053 break;
26054 if (src[*usedlen + 1] == 'e') /* :e */
26055 {
26056 if (s > tail)
26057 {
26058 *fnamelen += (int)(*fnamep - (s + 1));
26059 *fnamep = s + 1;
26060#ifdef VMS
26061 /* cut version from the extension */
26062 s = *fnamep + *fnamelen - 1;
26063 for ( ; s > *fnamep; --s)
26064 if (s[0] == ';')
26065 break;
26066 if (s > *fnamep)
26067 *fnamelen = s - *fnamep;
26068#endif
26069 }
26070 else if (*fnamep <= tail)
26071 *fnamelen = 0;
26072 }
26073 else /* :r */
26074 {
26075 if (s > tail) /* remove one extension */
26076 *fnamelen = (int)(s - *fnamep);
26077 }
26078 *usedlen += 2;
26079 }
26080
26081 /* ":s?pat?foo?" - substitute */
26082 /* ":gs?pat?foo?" - global substitute */
26083 if (src[*usedlen] == ':'
26084 && (src[*usedlen + 1] == 's'
26085 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26086 {
26087 char_u *str;
26088 char_u *pat;
26089 char_u *sub;
26090 int sep;
26091 char_u *flags;
26092 int didit = FALSE;
26093
26094 flags = (char_u *)"";
26095 s = src + *usedlen + 2;
26096 if (src[*usedlen + 1] == 'g')
26097 {
26098 flags = (char_u *)"g";
26099 ++s;
26100 }
26101
26102 sep = *s++;
26103 if (sep)
26104 {
26105 /* find end of pattern */
26106 p = vim_strchr(s, sep);
26107 if (p != NULL)
26108 {
26109 pat = vim_strnsave(s, (int)(p - s));
26110 if (pat != NULL)
26111 {
26112 s = p + 1;
26113 /* find end of substitution */
26114 p = vim_strchr(s, sep);
26115 if (p != NULL)
26116 {
26117 sub = vim_strnsave(s, (int)(p - s));
26118 str = vim_strnsave(*fnamep, *fnamelen);
26119 if (sub != NULL && str != NULL)
26120 {
26121 *usedlen = (int)(p + 1 - src);
26122 s = do_string_sub(str, pat, sub, flags);
26123 if (s != NULL)
26124 {
26125 *fnamep = s;
26126 *fnamelen = (int)STRLEN(s);
26127 vim_free(*bufp);
26128 *bufp = s;
26129 didit = TRUE;
26130 }
26131 }
26132 vim_free(sub);
26133 vim_free(str);
26134 }
26135 vim_free(pat);
26136 }
26137 }
26138 /* after using ":s", repeat all the modifiers */
26139 if (didit)
26140 goto repeat;
26141 }
26142 }
26143
Bram Moolenaar26df0922014-02-23 23:39:13 +010026144 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26145 {
26146 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26147 if (p == NULL)
26148 return -1;
26149 vim_free(*bufp);
26150 *bufp = *fnamep = p;
26151 *fnamelen = (int)STRLEN(p);
26152 *usedlen += 2;
26153 }
26154
Bram Moolenaar071d4272004-06-13 20:20:40 +000026155 return valid;
26156}
26157
26158/*
26159 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26160 * "flags" can be "g" to do a global substitute.
26161 * Returns an allocated string, NULL for error.
26162 */
26163 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026164do_string_sub(
26165 char_u *str,
26166 char_u *pat,
26167 char_u *sub,
26168 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169{
26170 int sublen;
26171 regmatch_T regmatch;
26172 int i;
26173 int do_all;
26174 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026175 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026176 garray_T ga;
26177 char_u *ret;
26178 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026179 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026180
26181 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26182 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026183 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026184
26185 ga_init2(&ga, 1, 200);
26186
26187 do_all = (flags[0] == 'g');
26188
26189 regmatch.rm_ic = p_ic;
26190 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26191 if (regmatch.regprog != NULL)
26192 {
26193 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026194 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026195 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26196 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026197 /* Skip empty match except for first match. */
26198 if (regmatch.startp[0] == regmatch.endp[0])
26199 {
26200 if (zero_width == regmatch.startp[0])
26201 {
26202 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026203 i = MB_PTR2LEN(tail);
26204 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26205 (size_t)i);
26206 ga.ga_len += i;
26207 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026208 continue;
26209 }
26210 zero_width = regmatch.startp[0];
26211 }
26212
Bram Moolenaar071d4272004-06-13 20:20:40 +000026213 /*
26214 * Get some space for a temporary buffer to do the substitution
26215 * into. It will contain:
26216 * - The text up to where the match is.
26217 * - The substituted text.
26218 * - The text after the match.
26219 */
26220 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026221 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026222 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26223 {
26224 ga_clear(&ga);
26225 break;
26226 }
26227
26228 /* copy the text up to where the match is */
26229 i = (int)(regmatch.startp[0] - tail);
26230 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26231 /* add the substituted text */
26232 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26233 + ga.ga_len + i, TRUE, TRUE, FALSE);
26234 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026235 tail = regmatch.endp[0];
26236 if (*tail == NUL)
26237 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238 if (!do_all)
26239 break;
26240 }
26241
26242 if (ga.ga_data != NULL)
26243 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26244
Bram Moolenaar473de612013-06-08 18:19:48 +020026245 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026246 }
26247
26248 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26249 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026250 if (p_cpo == empty_option)
26251 p_cpo = save_cpo;
26252 else
26253 /* Darn, evaluating {sub} expression changed the value. */
26254 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026255
26256 return ret;
26257}
26258
26259#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */